Skip to content

Commit 2796646

Browse files
mjbommargregkh
authored andcommitted
usb: gadget: f_fs: serialize DMABUF cancel against request completion
ffs_epfile_dmabuf_io_complete() calls usb_ep_free_request() on the completed request but leaves priv->req, the back-pointer that ffs_dmabuf_transfer() set on submission, pointing at the freed memory. A later FUNCTIONFS_DMABUF_DETACH ioctl or ffs_epfile_release() on the close path still sees priv->req non-NULL under ffs->eps_lock: if (priv->ep && priv->req) usb_ep_dequeue(priv->ep, priv->req); so usb_ep_dequeue() is called on a freed usb_request. On dummy_hcd the dequeue path only walks a live queue and pointer-compares, so the freed pointer reads without faulting and KASAN requires an explicit check at the FunctionFS call site to surface the use-after-free. On SG-capable in-tree UDCs the dequeue path dereferences the supplied request immediately: * chipidea's ep_dequeue() does container_of(req, struct ci_hw_req, req) and reads hwreq->req.status before acquiring its own lock. * cdnsp's cdnsp_gadget_ep_dequeue() reads request->status first. The narrower option of clearing priv->req via cmpxchg() in the completion does not close the race: the completion runs without eps_lock, so a cancel path holding eps_lock can still observe priv->req non-NULL, race a concurrent completion that clears and frees, and pass the freed pointer to usb_ep_dequeue(). A slightly longer fix that moves the free into the cleanup work is needed. Same class of lifetime race as the recent usbip-vudc timer fix [1]. Take eps_lock in the sole place that mutates priv->req from the callback direction by moving usb_ep_free_request() out of the completion into ffs_dmabuf_cleanup(), the existing work handler scheduled by ffs_dmabuf_signal_done() on ffs->io_completion_wq. Clear priv->req there under eps_lock before freeing, and only clear if priv->req still names our request (a subsequent ffs_dmabuf_transfer() on the same attachment may have queued a new one). This keeps the existing dummy_hcd sync-dequeue invariant: the completion callback is still invoked by the UDC without eps_lock held (dummy_hcd drops its own lock before calling the callback), and the callback now takes no f_fs lock at all. Serialization against the cancel path happens in cleanup, which runs from the workqueue with no f_fs lock held on entry. The priv ref count protects the containing ffs_dmabuf_priv: ffs_dmabuf_transfer() takes a ref via ffs_dmabuf_get(), cleanup drops it via ffs_dmabuf_put(), so priv stays live for the cleanup even after the cancel path's list_del + ffs_dmabuf_put. The ffs_dmabuf_transfer() error path no longer frees usb_req inline: fence->req and fence->ep are set before usb_ep_queue(), so ffs_dmabuf_cleanup() (scheduled by the error-path ffs_dmabuf_signal_done()) owns the free regardless of whether the queue succeeded. Reproduced under KASAN on both detach and close paths against dummy_hcd with an observability hook (kasan_check_byte(priv->req) immediately before usb_ep_dequeue) at the two FunctionFS cancel sites to surface the stale-pointer access; the hook is not part of this patch. The KASAN allocator / free stacks in the captured splats identify the same request: alloc in dummy_alloc_request, free in dummy_timer, fault reached from ffs_epfile_release (close) and from the FUNCTIONFS_DMABUF_DETACH ioctl (detach). With the patch applied, both paths are silent under the same hook. The bug is reached from the FunctionFS device node, which in real deployments is owned by the privileged gadget daemon (adbd, UMS, composite gadget services, etc.); it is not reachable from unprivileged userspace or from a USB host on the cable. FunctionFS mounts default to GLOBAL_ROOT_UID, but the filesystem supports uid=, gid=, and fmode= delegation to a non-root gadget daemon, so on real deployments the attacker may be a less-privileged service rather than root. Fixes: 7b07a2a ("usb: gadget: functionfs: Add DMABUF import interface") Link: https://lore.kernel.org/all/20260417163552.807548-1-michael.bommarito@gmail.com/ [1] Cc: stable <stable@kernel.org> Assisted-by: Claude:claude-opus-4-7 Signed-off-by: Michael Bommarito <michael.bommarito@gmail.com> Link: https://patch.msgid.link/20260419161227.1587668-1-michael.bommarito@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4e036c1 commit 2796646

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

  • drivers/usb/gadget/function

drivers/usb/gadget/function/f_fs.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ struct ffs_dma_fence {
150150
struct dma_fence base;
151151
struct ffs_dmabuf_priv *priv;
152152
struct work_struct work;
153+
struct usb_ep *ep;
154+
struct usb_request *req;
153155
};
154156

155157
struct ffs_epfile {
@@ -1385,6 +1387,21 @@ static void ffs_dmabuf_cleanup(struct work_struct *work)
13851387
struct ffs_dmabuf_priv *priv = dma_fence->priv;
13861388
struct dma_buf_attachment *attach = priv->attach;
13871389
struct dma_fence *fence = &dma_fence->base;
1390+
struct usb_request *req = dma_fence->req;
1391+
struct usb_ep *ep = dma_fence->ep;
1392+
1393+
/*
1394+
* eps_lock pairs with the cancel paths so they cannot pass a freed
1395+
* req to usb_ep_dequeue(). Only clear if priv->req still names ours;
1396+
* a re-queue on the same attachment may have taken that slot.
1397+
*/
1398+
spin_lock_irq(&priv->ffs->eps_lock);
1399+
if (priv->req == req)
1400+
priv->req = NULL;
1401+
spin_unlock_irq(&priv->ffs->eps_lock);
1402+
1403+
if (ep && req)
1404+
usb_ep_free_request(ep, req);
13881405

13891406
ffs_dmabuf_put(attach);
13901407
dma_fence_put(fence);
@@ -1414,8 +1431,8 @@ static void ffs_epfile_dmabuf_io_complete(struct usb_ep *ep,
14141431
struct usb_request *req)
14151432
{
14161433
pr_vdebug("FFS: DMABUF transfer complete, status=%d\n", req->status);
1434+
/* req is freed by ffs_dmabuf_cleanup() under eps_lock. */
14171435
ffs_dmabuf_signal_done(req->context, req->status);
1418-
usb_ep_free_request(ep, req);
14191436
}
14201437

14211438
static const char *ffs_dmabuf_get_driver_name(struct dma_fence *fence)
@@ -1699,6 +1716,10 @@ static int ffs_dmabuf_transfer(struct file *file,
16991716
usb_req->context = fence;
17001717
usb_req->complete = ffs_epfile_dmabuf_io_complete;
17011718

1719+
/* ffs_dmabuf_cleanup() frees usb_req via these two fields. */
1720+
fence->req = usb_req;
1721+
fence->ep = ep->ep;
1722+
17021723
cookie = dma_fence_begin_signalling();
17031724
ret = usb_ep_queue(ep->ep, usb_req, GFP_ATOMIC);
17041725
dma_fence_end_signalling(cookie);
@@ -1708,7 +1729,6 @@ static int ffs_dmabuf_transfer(struct file *file,
17081729
} else {
17091730
pr_warn("FFS: Failed to queue DMABUF: %d\n", ret);
17101731
ffs_dmabuf_signal_done(fence, ret);
1711-
usb_ep_free_request(ep->ep, usb_req);
17121732
}
17131733

17141734
spin_unlock_irq(&epfile->ffs->eps_lock);

0 commit comments

Comments
 (0)