From: Yaohan Li <shunsheng1218@gmail.com>
To: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Cc: linux1394-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org
Subject: [RFC PATCH] firewire: keep the request alive while the event is queued
Date: Thu, 19 Nov 2026 16:32:05 +0800 [thread overview]
Message-ID: <20261119083205.3437874-1-shunsheng1218@gmail.com> (raw)
handle_request() builds an event whose payload points into the
fw_request that carries the packet. The reference that keeps that
request alive belongs to the client resource, which can be released
from another thread with FW_CDEV_IOC_SEND_RESPONSE as soon as the event
header, including the handle, has been copied to userspace.
dequeue_event() copies the header and the payload with two separate
copy_to_user() calls, so the request can be freed between them and the
payload copy then reads freed memory. The same happens for an event
that is still on the client list when the request is released.
Let the event hold its own reference to the request and release it in
the event destructor, which the three paths that free an event now use.
The other event types are allocated with kzalloc_obj() so that the new
release hook is NULL for them.
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
Prepared against linux1394 for-next (d99221005abf), where the cdev
context-analysis work has already landed. Compared with the same change on
mainline, the only difference is the hunk context in
fw_device_op_release(), which now frees the events under client->mutex with
scoped_guard(); the change itself is unchanged.
Tested with a controlled lifetime test: drivers/firewire/core-cdev.c is
compiled verbatim into a module on a KASAN kernel built from this tree
(x86_64, QEMU/KVM). The test queues an inbound request event, releases the
client reference the way a SEND_RESPONSE ioctl does, and only then copies the
event out with dequeue_event(). With this patch the payload is intact and
the request is released when the event is freed; the same test against
666539eecdf4 reports a slab-use-after-free in _copy_to_user from
dequeue_event(). All three event free paths - dequeue_event(), the shutdown
branch of queue_event() and fw_device_op_release() - are exercised. The
FireWire controller was not available, so no bus timing was exercised.
The test was re-run against the for-next version of the file: the module
compiles drivers/firewire/core-cdev.c from the replayed tree, and all 13
checks pass there as well.
All event frees were re-audited: the three struct event allocations go
through queue_event() and are released only by free_event(), and the
iso_resource_event objects hand their ownership over with
retain_and_null_ptr() / no_free_ptr() before they are queued, so no path
frees a queued event with a plain kfree().
Checked against the list traffic and the maintainer tree: Takashi
Sakamoto's "firewire: core: consolidate lock scopes for event waiting in
dequeue function" (2026-09-20) is merged in for-next. It fixes a different
race and does not touch the free path, and this patch is rebased on top of
it.
AI assistance: the analysis, the change and its review notes were
prepared with Claude (Anthropic) as a coding assistant. The author
reviewed the reasoning and takes responsibility for the content.
---
drivers/firewire/core-cdev.c | 44 ++++++++++++++++++++++++++++--------
1 file changed, 35 insertions(+), 9 deletions(-)
diff --git a/drivers/firewire/core-cdev.c b/drivers/firewire/core-cdev.c
index 03598f7e55470..d96226a5f45c8 100644
--- a/drivers/firewire/core-cdev.c
+++ b/drivers/firewire/core-cdev.c
@@ -201,12 +201,13 @@ static void schedule_iso_resource_auto(struct iso_resource_auto *r, unsigned lon
}
/*
- * dequeue_event() just kfree()'s the event, so the event has to be
+ * free_event() releases an event with kfree(), so the event has to be
* the first field in a struct XYZ_event.
*/
struct event {
struct { void *data; size_t size; } v[2];
struct list_head link;
+ void (*release)(struct event *event);
};
struct bus_reset_event {
@@ -226,6 +227,7 @@ struct outbound_transaction_event {
struct inbound_transaction_event {
struct event event;
+ struct fw_request *request;
union {
struct fw_cdev_event_request request;
struct fw_cdev_event_request2 request2;
@@ -330,6 +332,13 @@ static int fw_device_op_open(struct inode *inode, struct file *file)
return nonseekable_open(inode, file);
}
+static void free_event(struct event *event)
+{
+ if (event->release)
+ event->release(event);
+ kfree(event);
+}
+
static void queue_event(struct client *client, struct event *event,
void *data0, size_t size0, void *data1, size_t size1)
{
@@ -340,7 +349,7 @@ static void queue_event(struct client *client, struct event *event,
scoped_guard(mutex, &client->mutex) {
if (client->in_shutdown) {
- kfree(event);
+ free_event(event);
} else {
list_add_tail(&event->link, &client->event_list);
@@ -393,7 +402,7 @@ static ssize_t dequeue_event(struct client *client, char __user *buffer, size_t
ret += size;
}
- kfree(event);
+ free_event(event);
return ret;
}
@@ -756,6 +765,14 @@ static void release_request(struct client *client,
kfree(r);
}
+static void inbound_transaction_event_release(struct event *event)
+{
+ struct inbound_transaction_event *e =
+ container_of(event, struct inbound_transaction_event, event);
+
+ fw_request_put(e->request);
+}
+
static void handle_request(struct fw_card *card, struct fw_request *request,
int tcode, int destination, int source,
int generation, unsigned long long offset,
@@ -777,7 +794,7 @@ static void handle_request(struct fw_card *card, struct fw_request *request,
fw_request_get(request);
r = kmalloc_obj(*r);
- e = kmalloc_obj(*e);
+ e = kzalloc_obj(*e);
if (r == NULL || e == NULL)
goto failed;
@@ -835,6 +852,15 @@ static void handle_request(struct fw_card *card, struct fw_request *request,
event_size0 = sizeof(*req);
}
+ /*
+ * Hold a reference for the event so that the payload stays valid until
+ * userspace has copied it, even if the client releases the request
+ * from another thread in the meantime.
+ */
+ fw_request_get(request);
+ e->request = request;
+ e->event.release = inbound_transaction_event_release;
+
queue_event(handler->client, &e->event,
&e->req, event_size0, r->data, length);
return;
@@ -1027,7 +1053,7 @@ static void iso_mc_callback(struct fw_iso_context *context,
struct client *client = data;
struct iso_interrupt_mc_event *e;
- e = kmalloc_obj(*e);
+ e = kzalloc_obj(*e);
if (e == NULL)
return;
@@ -1441,8 +1467,8 @@ static void release_iso_resource_auto(struct client *client, struct client_resou
static int ioctl_allocate_iso_resource(struct client *client, union ioctl_arg *arg)
{
struct fw_cdev_allocate_iso_resource *request = &arg->allocate_iso_resource;
- struct iso_resource_event *e1 __free(kfree) = kmalloc_obj(*e1);
- struct iso_resource_event *e2 __free(kfree) = kmalloc_obj(*e2);
+ struct iso_resource_event *e1 __free(kfree) = kzalloc_obj(*e1);
+ struct iso_resource_event *e2 __free(kfree) = kzalloc_obj(*e2);
struct iso_resource_auto *r __free(kfree) = kmalloc_obj(*r);
int err;
@@ -1514,7 +1540,7 @@ static void iso_resource_once_work(struct work_struct *work)
static int init_iso_resource_once(struct client *client,
struct fw_cdev_allocate_iso_resource *request, int todo)
{
- struct iso_resource_event *e __free(kfree) = kmalloc_obj(*e);
+ struct iso_resource_event *e __free(kfree) = kzalloc_obj(*e);
struct iso_resource_once *r __free(kfree) = kmalloc_obj(*r);
int err;
@@ -1949,7 +1975,7 @@ static int fw_device_op_release(struct inode *inode, struct file *file)
scoped_guard(mutex, &client->mutex) {
list_for_each_entry_safe(event, next_event, &client->event_list, link)
- kfree(event);
+ free_event(event);
}
client_put(client);
--
2.34.1
reply other threads:[~2026-11-19 8:32 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261119083205.3437874-1-shunsheng1218@gmail.com \
--to=shunsheng1218@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux1394-devel@lists.sourceforge.net \
--cc=o-takashi@sakamocchi.jp \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®