Skip to contentSkip to conversation

usb: gadget: f_fs: cancellation fix

PATCH In review Ingo Rohloff 2 participants 5 messages Nov 19 – Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover usb: gadget: f_fs: cancellation fix

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.

---
Changes in v3:
- Neill remarked, that to avoid lockdep issues, spin_lock_irq/spin_unlock_irq is needed in ffs_user_copy_worker().
Now solved differently via smp_cond_load_acquire(), avoiding a spinlock altogether.
- Revert order of patches:
First cancellation fix, because this also applies to stable kernels, then refactoring (because this only applies to recent kernels).
- Add Stable and Fixes tags to (now) first patch.

Link to v2: https://lore.kernel.org/linux-usb/[email protected]/

Changes in v2:
- First patch "Fix mmgrab()/mmdrop() balance." now posted separately.
- Fix comment style and whitespace as requested by
Neill Kapron <[email protected]>
- Make sure io_data->kiocb->ki_complete() == aio_complete_rw() can't be called before kiocb_set_cancel_fn() has been called. That should it make safe to call kiocb_set_cancel_fn() after usb_ep_queue().

Link to v1: https://lore.kernel.org/r/[email protected]

---
Ingo Rohloff (2):
usb: gadget: f_fs: register cancellation handler only after request
queueing
usb: gadget: f_fs: simplify error handling using goto

 drivers/usb/gadget/function/f_fs.c | 116 ++++++++++++++++-------------
 1 file changed, 66 insertions(+), 50 deletions(-)

--
2.55.0

#1 lore raw

PATCH v3 1/2 usb: gadget: f_fs: register cancellation handler only after request queueing

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")
---
 drivers/usb/gadget/function/f_fs.c | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
drivers/usb/gadget/function/f_fs.c +16 -14
index 704cfae49f11..cf0287064923 100644
@@ -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;
 	}
Signature · 1 line
2.55.0
#2 lore raw

PATCH v3 2/2 usb: gadget: f_fs: simplify error handling using goto

In reply to 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.

---
 drivers/usb/gadget/function/f_fs.c | 98 ++++++++++++++++--------------
 1 file changed, 54 insertions(+), 44 deletions(-)
drivers/usb/gadget/function/f_fs.c +54 -44
index cf0287064923..723c1d57f437 100644
@@ -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;
 
Signature · 1 line
2.55.0
maintainer
#3 lore raw
In reply to Ingo Rohloff
Quoted 6 lines · Ingo Rohloff

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

maintainer
#4 lore raw
Quoted 10 lines · Greg KH
Quoted 6 lines · Ingo Rohloff

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...