* [PATCH v3 1/2] usb: gadget: f_fs: register cancellation handler only after request queueing
2026-11-19 15:19 [PATCH v3 0/2] usb: gadget: f_fs: cancellation fix Ingo Rohloff
@ 2026-11-19 15:19 ` Ingo Rohloff
2026-11-19 15:19 ` [PATCH v3 2/2] usb: gadget: f_fs: simplify error handling using goto Ingo Rohloff
2026-11-20 10:16 ` [PATCH v3 0/2] usb: gadget: f_fs: cancellation fix Greg KH
2 siblings, 0 replies; 5+ messages in thread
From: Ingo Rohloff @ 2026-11-19 15:19 UTC (permalink / raw)
To: gregkh
Cc: viro, nkapron, me, michael.bommarito, linux-usb, Ingo Rohloff,
stable
Call kiocb_set_cancel_fn() only after usb_ep_queue() completes
successfully.
This also undoes commit e78dcb1f7ec2 ("usb: gadget: f_fs: Fix
Use-After-Free in AIO error path"): The described error is avoided by
making sure the cancellation handler is only registered, if there is
something to cancel.
Registering the cancellation handler adds the kiocb to the context's
active request list (active_reqs in aio.c). If usb_ep_queue() fails, the
request was never actually submitted to the hardware, which means the
request is not active. The aio framework already deals with calling the
completion function ki_complete() automatically if you do not return
-EIOCBQUEUED.
Fixes: e78dcb1f7ec2 ("usb: gadget: f_fs: Fix Use-After-Free in AIO error path")
Link: https://lore.kernel.org/linux-usb/20261110171222.22633-3-ingo.rohloff@lauterbach.com/
Cc: stable@vger.kernel.org
Signed-off-by: Ingo Rohloff <ingo.rohloff@lauterbach.com>
---
drivers/usb/gadget/function/f_fs.c | 30 ++++++++++++++++--------------
1 file changed, 16 insertions(+), 14 deletions(-)
diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index 704cfae49f11..cf0287064923 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -246,6 +246,7 @@ struct ffs_buffer {
struct ffs_io_data {
bool aio;
bool read;
+ bool aio_active;
struct kiocb *kiocb;
struct iov_iter data;
@@ -887,6 +888,8 @@ static void ffs_user_copy_worker(struct work_struct *work)
}
mmdrop(io_data->mm);
+ /* Make sure kiocb_set_cancel_fn() was already called. */
+ smp_cond_load_acquire(&io_data->aio_active, VAL == true);
io_data->kiocb->ki_complete(io_data->kiocb, ret);
if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd)
@@ -1014,8 +1017,11 @@ static struct ffs_ep *ffs_epfile_wait_ep(struct ffs_epfile *epfile, struct file
return ep;
}
+static int ffs_aio_cancel(struct kiocb *kiocb);
+
static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
{
+ struct kiocb *kiocb = io_data->kiocb;
struct ffs_epfile *epfile = file->private_data;
struct usb_request *req;
struct ffs_ep *ep;
@@ -1205,6 +1211,14 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
usb_ep_free_request(ep->ep, req);
goto error_lock;
}
+ kiocb->private = io_data;
+ kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
+ /*
+ * prevent ffs_user_copy_worker() from calling
+ * io_data->kiocb->ki_complete()
+ * before kiocb_set_cancel_fn() was called.
+ */
+ smp_store_release(&io_data->aio_active, true);
ret = -EIOCBQUEUED;
/*
@@ -1285,21 +1299,15 @@ static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
p->data = *from;
p->mm = current->mm;
- kiocb->private = p;
-
- if (p->aio) {
+ if (p->aio)
mmgrab(p->mm);
- kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
- }
res = ffs_epfile_io(kiocb->ki_filp, p);
if (res == -EIOCBQUEUED)
return res;
if (p->aio) {
- kiocb->ki_complete(kiocb, res);
mmdrop(p->mm);
kfree(p);
- return -EIOCBQUEUED;
} else {
*from = p->data;
}
@@ -1335,23 +1343,17 @@ static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
}
p->mm = current->mm;
- kiocb->private = p;
-
- if (p->aio) {
+ if (p->aio)
mmgrab(p->mm);
- kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
- }
res = ffs_epfile_io(kiocb->ki_filp, p);
if (res == -EIOCBQUEUED)
return res;
if (p->aio) {
- kiocb->ki_complete(kiocb, res);
mmdrop(p->mm);
kfree(p->to_free);
kfree(p);
- return -EIOCBQUEUED;
} else {
*to = p->data;
}
--
2.55.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v3 2/2] usb: gadget: f_fs: simplify error handling using goto
2026-11-19 15:19 [PATCH v3 0/2] usb: gadget: f_fs: cancellation fix Ingo Rohloff
2026-11-19 15:19 ` [PATCH v3 1/2] usb: gadget: f_fs: register cancellation handler only after request queueing Ingo Rohloff
@ 2026-11-19 15:19 ` Ingo Rohloff
2026-11-20 10:16 ` [PATCH v3 0/2] usb: gadget: f_fs: cancellation fix Greg KH
2 siblings, 0 replies; 5+ messages in thread
From: Ingo Rohloff @ 2026-11-19 15:19 UTC (permalink / raw)
To: gregkh; +Cc: viro, nkapron, me, michael.bommarito, linux-usb, Ingo Rohloff
Refactor conditional checks to use direct error returns/gotos instead of a
long if/else chain. This makes failure paths more obvious.
At the end of the function AIO request handling is done.
Signed-off-by: Ingo Rohloff <ingo.rohloff@lauterbach.com>
---
drivers/usb/gadget/function/f_fs.c | 98 ++++++++++++++++--------------
1 file changed, 54 insertions(+), 44 deletions(-)
diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index cf0287064923..723c1d57f437 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -1019,9 +1019,10 @@ static struct ffs_ep *ffs_epfile_wait_ep(struct ffs_epfile *epfile, struct file
static int ffs_aio_cancel(struct kiocb *kiocb);
-static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
+static ssize_t ffs_epfile_io(struct ffs_io_data *io_data)
{
struct kiocb *kiocb = io_data->kiocb;
+ struct file *file = kiocb->ki_filp;
struct ffs_epfile *epfile = file->private_data;
struct usb_request *req;
struct ffs_ep *ep;
@@ -1110,11 +1111,17 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
if (epfile->ep != ep) {
/* In the meantime, endpoint got disabled or changed. */
ret = -ESHUTDOWN;
- } else if (halt) {
+ goto error_lock;
+ }
+
+ if (halt) {
ret = usb_ep_set_halt(ep->ep);
if (!ret)
ret = -EBADMSG;
- } else if (data_len == -EINVAL) {
+ goto error_lock;
+ }
+
+ if (data_len == -EINVAL) {
/*
* Sanity Check: even though data_len can't be used
* uninitialized at the time I write this comment, some
@@ -1128,7 +1135,10 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
*/
WARN(1, "%s: data_len == -EINVAL\n", __func__);
ret = -EINVAL;
- } else if (!io_data->aio) {
+ goto error_lock;
+ }
+
+ if (!io_data->aio) {
bool interrupted = false;
req = ep->req;
@@ -1182,51 +1192,51 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
else
ret = io_data->status;
goto error_mutex;
- } else if (!(req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC))) {
+ }
+
+ /* It's an AIO request */
+ req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC);
+ if (!req) {
ret = -ENOMEM;
- } else {
- if (io_data->use_sg) {
- req->buf = NULL;
- req->sg = io_data->sgt.sgl;
- req->num_sgs = io_data->sgt.nents;
- } else {
- req->buf = data;
- req->num_sgs = 0;
- }
+ goto error_lock;
+ }
- req->zero = !io_data->read ? epfile->zlp_enabled : 0;
- req->length = data_len;
+ if (io_data->use_sg) {
+ req->buf = NULL;
+ req->sg = io_data->sgt.sgl;
+ req->num_sgs = io_data->sgt.nents;
+ } else {
+ req->buf = data;
+ req->num_sgs = 0;
+ }
- io_data->buf = data;
- io_data->ep = ep->ep;
- io_data->req = req;
- io_data->ffs = epfile->ffs;
+ req->zero = !io_data->read ? epfile->zlp_enabled : 0;
+ req->length = data_len;
- req->context = io_data;
- req->complete = ffs_epfile_async_io_complete;
+ io_data->buf = data;
+ io_data->ep = ep->ep;
+ io_data->req = req;
+ io_data->ffs = epfile->ffs;
- ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
- if (ret) {
- io_data->req = NULL;
- usb_ep_free_request(ep->ep, req);
- goto error_lock;
- }
- kiocb->private = io_data;
- kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
- /*
- * prevent ffs_user_copy_worker() from calling
- * io_data->kiocb->ki_complete()
- * before kiocb_set_cancel_fn() was called.
- */
- smp_store_release(&io_data->aio_active, true);
+ req->context = io_data;
+ req->complete = ffs_epfile_async_io_complete;
- ret = -EIOCBQUEUED;
- /*
- * Do not kfree the buffer in this function. It will be freed
- * by ffs_user_copy_worker.
- */
- data = NULL;
+ ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
+ if (ret) {
+ io_data->req = NULL;
+ usb_ep_free_request(ep->ep, req);
+ goto error_lock;
}
+ kiocb->private = io_data;
+ kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
+ /*
+ * prevent ffs_user_copy_worker() from calling
+ * io_data->kiocb->ki_complete()
+ * before kiocb_set_cancel_fn() was called.
+ */
+ smp_store_release(&io_data->aio_active, true);
+
+ ret = -EIOCBQUEUED;
error_lock:
spin_unlock_irq(&epfile->ffs->eps_lock);
@@ -1302,7 +1312,7 @@ static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
if (p->aio)
mmgrab(p->mm);
- res = ffs_epfile_io(kiocb->ki_filp, p);
+ res = ffs_epfile_io(p);
if (res == -EIOCBQUEUED)
return res;
if (p->aio) {
@@ -1346,7 +1356,7 @@ static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
if (p->aio)
mmgrab(p->mm);
- res = ffs_epfile_io(kiocb->ki_filp, p);
+ res = ffs_epfile_io(p);
if (res == -EIOCBQUEUED)
return res;
--
2.55.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v3 0/2] usb: gadget: f_fs: cancellation fix
2026-11-19 15:19 [PATCH v3 0/2] usb: gadget: f_fs: cancellation fix Ingo Rohloff
2026-11-19 15:19 ` [PATCH v3 1/2] usb: gadget: f_fs: register cancellation handler only after request queueing Ingo Rohloff
2026-11-19 15:19 ` [PATCH v3 2/2] usb: gadget: f_fs: simplify error handling using goto Ingo Rohloff
@ 2026-11-20 10:16 ` Greg KH
2026-11-20 10:29 ` Greg KH
2 siblings, 1 reply; 5+ messages in thread
From: Greg KH @ 2026-11-20 10:16 UTC (permalink / raw)
To: Ingo Rohloff; +Cc: viro, nkapron, me, michael.bommarito, linux-usb
On Thu, Nov 19, 2026 at 04:19:40PM +0100, Ingo Rohloff wrote:
> This 2 part patch series fixes a previous commit and does
> some refactoring.
>
> Note: The first patch in this series only applies cleanly, if
> "[PATCH] usb: gadget: f_fs: Fix mmgrab()/mmdrop() balance."
> is applied first.
I don't see that in my queue, pleasae submit all of these as a single
series.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v3 0/2] usb: gadget: f_fs: cancellation fix
2026-11-20 10:16 ` [PATCH v3 0/2] usb: gadget: f_fs: cancellation fix Greg KH
@ 2026-11-20 10:29 ` Greg KH
0 siblings, 0 replies; 5+ messages in thread
From: Greg KH @ 2026-11-20 10:29 UTC (permalink / raw)
To: Ingo Rohloff; +Cc: viro, nkapron, me, michael.bommarito, linux-usb
On Fri, Nov 20, 2026 at 11:16:44AM +0100, Greg KH wrote:
> On Thu, Nov 19, 2026 at 04:19:40PM +0100, Ingo Rohloff wrote:
> > This 2 part patch series fixes a previous commit and does
> > some refactoring.
> >
> > Note: The first patch in this series only applies cleanly, if
> > "[PATCH] usb: gadget: f_fs: Fix mmgrab()/mmdrop() balance."
> > is applied first.
>
> I don't see that in my queue, pleasae submit all of these as a single
> series.
Ah nevermind, found it...
^ permalink raw reply [flat|nested] 5+ messages in thread