usb: gadget: raw_gadget: fix dangling ep->desc on enable failure
Subsystems (3)
PATCH 1/1 usb: gadget: raw_gadget: fix dangling ep->desc on enable failure
raw_ioctl_ep_enable() assigns the user-supplied endpoint descriptor to ep->ep->desc before calling usb_ep_enable(). If usb_ep_enable() fails, or if the subsequent usb_ep_alloc_request() fails, the error path frees desc via out_free but leaves ep->ep->desc pointing at the freed memory.
The UDC driver may have captured the stale descriptor pointer during the partial enable sequence and may dereference it in subsequent operations, resulting in a use-after-free.
Clear ep->ep->desc before jumping to out_free on both failure paths so the pointer never outlives the descriptor it references.
--- drivers/usb/gadget/legacy/raw_gadget.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/usb/gadget/legacy/raw_gadget.c +2 -0
@@ -886,6 +886,7 @@ static int raw_ioctl_ep_enable(struct raw_dev *dev, unsigned long value)if (ret <0 ) {dev_err (&dev->gadget->dev,"fail, usb_ep_enable returned %d\n" , ret); + ep->ep->desc =NULL ; goto out_free; } ep->req =usb_ep_alloc_request (ep->ep,GFP_ATOMIC );
@@ -893,6 +894,7 @@ static int raw_ioctl_ep_enable(struct raw_dev *dev, unsigned long value)dev_err (&dev->gadget->dev,"fail, usb_ep_alloc_request failed\n" );usb_ep_disable (ep->ep); + ep->ep->desc =NULL ; ret = -ENOMEM ; goto out_free; }
--
2.50.1
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 10 lines
raw_ioctl_ep_enable()assigns the user-supplied endpoint descriptor toep->ep->descbefore callingusb_ep_enable(). Ifusb_ep_enable()fails, or if the subsequentusb_ep_alloc_request()fails, the error path frees desc viaout_freebut leavesep->ep->descpointing at the freed memory.The UDC driver may have captured the stale descriptor pointer during the partial enable sequence and may dereference it in subsequent operations, resulting in a use-after-free.
Quoted 7 lines
Clear
ep->ep->descbefore jumping toout_freeon both failure paths so the pointer never outlives the descriptor it references.Fixes 97df5e5758f7 ("usb: raw-gadget: fix gadget endpoint selection")Reviewed-by Guanglei Zhu <[email protected]>Signed-off-by Liu Chao <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Greg KH
maintainerQuoted 13 lines · Liu Chao
raw_ioctl_ep_enable()assigns the user-supplied endpoint descriptor toep->ep->descbefore callingusb_ep_enable(). Ifusb_ep_enable()fails, or if the subsequentusb_ep_alloc_request()fails, the error path frees desc viaout_freebut leavesep->ep->descpointing at the freed memory.The UDC driver may have captured the stale descriptor pointer during the partial enable sequence and may dereference it in subsequent operations, resulting in a use-after-free.
Clear
ep->ep->descbefore jumping toout_freeon both failure paths so the pointer never outlives the descriptor it references.
Quoted 7 lines
Fixes 97df5e5758f7 ("usb: raw-gadget: fix gadget endpoint selection")Reviewed-by Guanglei Zhu <[email protected]>Signed-off-by Liu Chao <[email protected]>--- drivers/usb/gadget/legacy/raw_gadget.c | 2 ++ 1 file changed, 2 insertions(+)
How was this found and tested?
thanks,
greg k-h
Quoted 1 line · Greg KH
How was this found and tested?
By code audit of the raw-gadget endpoint ownership, not from a crash report or a fuzzer.
Reaching the failing path from userspace is straightforward. usb_gadget_ep_match_desc() does not look at bDescriptorType, but dummy_hcd's dummy_enable() does and rejects anything that is not USB_DT_ENDPOINT with -EINVAL. So a single USB_RAW_IOCTL_EP_ENABLE with an otherwise valid descriptor (0x81, bulk, 512 bytes, i.e. ep1in-bulk at high speed) and bDescriptorType = 0 makes usb_ep_enable() fail and drops us into out_free:
raw-gadget.0 gadget.0: fail, usb_ep_enable returned -22
one line before kfree(desc), with ep->ep->desc still pointing at it.
Tested on mainline with CONFIG_USB_RAW_GADGET=y, CONFIG_USB_DUMMY_HCD=y and KASAN, userspace side running in a QEMU guest: the descriptor gets freed and ep->ep->desc keeps the stale value.
I could not find any in-tree reader of that pointer along this path, though. usb_ep_enable() only reads a descriptor assigned right before it, raw_process_ep_io() and the halt/wedge ioctls return early on the endpoint state, and dummy_hcd keeps its own private descriptor pointer which dummy_disable() clears, so the value left behind is only the core struct usb_ep.desc. So a stock kernel stays silent under KASAN: as of today this is a latent dangling reference, not a reachable crash. Reading the pointer from a temporary probe patch (not part of the submission) right after the kfree() gives:
BUG: KASAN: slab-use-after-free in raw_ioctl
Read of size 1 at 0xffff88800a7425a0 by task repro/71
...
Allocated by task 71:
memdup_user+0x26/0x80
raw_ioctl+0xbfb/0x1ea0
Freed by task 71:
kfree+0x189/0x3b0
raw_ioctl+0x1648/0x1ea0and with the patch applied there is nothing left to read.
What makes it worth fixing anyway is that the stale pointer outlives the file descriptor: a UDC's struct usb_ep is owned by the UDC, not by raw_gadget, so the dangling value survives dev_free() and is still there for whichever gadget driver binds that UDC next. The core keeps the opposite invariant elsewhere -- usb_ep_autoconfig_ss() sets ep->desc = NULL, composite.c assigns _ep->desc before enabling, and f_fs reads ep->ep->desc.
thanks,
Quoted 43 lines · Liu Chao
raw_ioctl_ep_enable()assigns the user-supplied endpoint descriptor toep->ep->descbefore callingusb_ep_enable(). Ifusb_ep_enable()fails, or if the subsequentusb_ep_alloc_request()fails, the error path frees desc viaout_freebut leavesep->ep->descpointing at the freed memory.The UDC driver may have captured the stale descriptor pointer during the partial enable sequence and may dereference it in subsequent operations, resulting in a use-after-free.
Clear
ep->ep->descbefore jumping toout_freeon both failure paths so the pointer never outlives the descriptor it references.Fixes 97df5e5758f7 ("usb: raw-gadget: fix gadget endpoint selection")Reviewed-by Guanglei Zhu <[email protected]>Signed-off-by Liu Chao <[email protected]>--- drivers/usb/gadget/legacy/raw_gadget.c | 2 ++ 1 file changed, 2 insertions(+)drivers/usb/gadget/legacy/raw_gadget.c +2
-1@@ -886,6 +886,7 @@ static int raw_ioctl_ep_enable(struct raw_dev *dev, unsigned long value)if (ret <0 ) {dev_err (&dev->gadget->dev,"fail, usb_ep_enable returned %d\n" , ret); + ep->ep->desc =NULL ; goto out_free; } ep->req =usb_ep_alloc_request (ep->ep,GFP_ATOMIC );@@ -893,6 +894,7 @@ static int raw_ioctl_ep_enable(struct raw_dev *dev, unsigned long value)dev_err (&dev->gadget->dev,"fail, usb_ep_alloc_request failed\n" );usb_ep_disable (ep->ep); + ep->ep->desc =NULL ; ret = -ENOMEM ; goto out_free; } --
Quoted 2 lines
2.50.1
Commit message should ideally mention that the use-after-free is not actually reachable.
But the clean-up makes sense:
Andrey Konovalov
reviewerQuoted 51 lines · Andrey Konovalov
Quoted 43 lines · Liu Chao
raw_ioctl_ep_enable()assigns the user-supplied endpoint descriptor toep->ep->descbefore callingusb_ep_enable(). Ifusb_ep_enable()fails, or if the subsequentusb_ep_alloc_request()fails, the error path frees desc viaout_freebut leavesep->ep->descpointing at the freed memory.The UDC driver may have captured the stale descriptor pointer during the partial enable sequence and may dereference it in subsequent operations, resulting in a use-after-free.
Clear
ep->ep->descbefore jumping toout_freeon both failure paths so the pointer never outlives the descriptor it references.Fixes 97df5e5758f7 ("usb: raw-gadget: fix gadget endpoint selection")Reviewed-by Guanglei Zhu <[email protected]>Signed-off-by Liu Chao <[email protected]>--- drivers/usb/gadget/legacy/raw_gadget.c | 2 ++ 1 file changed, 2 insertions(+)drivers/usb/gadget/legacy/raw_gadget.c +2
-1@@ -886,6 +886,7 @@ static int raw_ioctl_ep_enable(struct raw_dev *dev, unsigned long value)if (ret <0 ) {dev_err (&dev->gadget->dev,"fail, usb_ep_enable returned %d\n" , ret); + ep->ep->desc =NULL ; goto out_free; } ep->req =usb_ep_alloc_request (ep->ep,GFP_ATOMIC );@@ -893,6 +894,7 @@ static int raw_ioctl_ep_enable(struct raw_dev *dev, unsigned long value)dev_err (&dev->gadget->dev,"fail, usb_ep_alloc_request failed\n" );usb_ep_disable (ep->ep); + ep->ep->desc =NULL ; ret = -ENOMEM ; goto out_free; } --Quoted 2 lines
2.50.1
Commit message should ideally mention that the use-after-free is not actually reachable.
Quoted 3 lines
But the clean-up makes sense:
Reviewed-by Andrey Konovalov <[email protected]>
Hm, looking at this more, it seems that the UDC drivers that care about the value of ep->desc for disabled endpoints, NULL it out during usb_ep_disable() themselves. And other gadget drivers also don't seem to be NULLing out ep->desc explicitly. So I'm not sure if this clean-up makes sense anymore. At least, until we get an actual reproducer for a crash.