usb: gadget: f_midi: fix UAF race during IN request handling
Subsystems (2)
PATCH v2 1/1 usb: gadget: f_midi: fix UAF race during IN request handling
f_midi_disable() drains and frees pre-allocated IN requests from in_req_fifo without holding midi->transmit_lock. If f_midi_transmit() runs concurrently on the workqueue, f_midi_do_transmit() can peek a request from in_req_fifo and write into its buffer while f_midi_disable() frees it, leading to a use-after-free.
Additionally, when usb_ep_queue() fails in f_midi_do_transmit() (such as with -ESHUTDOWN when the endpoint is disabled concurrently), it resets req->length = 0 and returns active instead of the error code, causing the do-while loop in f_midi_transmit() to keep spinning under midi->transmit_lock until the substream is drained.
Fix this by acquiring midi->transmit_lock in f_midi_disable() while releasing IN requests from in_req_fifo, and returning the error code from f_midi_do_transmit() when usb_ep_queue() fails so f_midi_transmit() aborts immediately and drops active substreams.
---
Changes in v2:
- Remove the transmit_lock and ep->enabled checks added in v1 from f_midi_complete() and f_midi_transmit().
- Return error code from f_midi_do_transmit() when usb_ep_queue() fails
so f_midi_transmit() aborts the loop immediately.
- Link to v1: https://lore.kernel.org/all/[email protected]/
drivers/usb/gadget/function/f_midi.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/usb/gadget/function/f_midi.c +4 -0
@@ -420,6 +420,7 @@ static void f_midi_disable(struct usb_function *f)struct f_midi *midi =func_to_midi (f);struct usb_composite_dev *cdev = f->config->cdev;struct usb_request *req =NULL ; +unsigned long flags;DBG (cdev,"disable\n" );
@@ -431,8 +432,10 @@ static void f_midi_disable(struct usb_function *f)usb_ep_disable (midi->out_ep);/* release IN requests */ +spin_lock_irqsave (&midi->transmit_lock, flags);while (kfifo_get (&midi->in_req_fifo, &req))free_ep_req (midi->in_ep, req); +spin_unlock_irqrestore (&midi->transmit_lock, flags);f_midi_drop_out_substreams (midi); }
@@ -661,6 +664,7 @@ static int f_midi_do_transmit(struct f_midi *midi, struct usb_ep *ep)ERROR (midi,"%s failed to queue req: %d\n" , midi->in_ep->name, err); req->length =0 ;/* Re-use request next time. */ +return err; }else {/* Upon success, put request at the back of the queue. */ kfifo_skip (&midi->in_req_fifo);
Signature · 1 line
2.56.0.307.g5fe7bd680c-goog
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 12 lines
f_midi_disable()drains and frees pre-allocated IN requests fromin_req_fifowithout holdingmidi->transmit_lock. Iff_midi_transmit()runs concurrently on the workqueue,f_midi_do_transmit()can peek a request fromin_req_fifoand write into its buffer whilef_midi_disable()frees it, leading to a use-after-free.Additionally, when
usb_ep_queue()fails inf_midi_do_transmit()(such as with-ESHUTDOWNwhen the endpoint is disabled concurrently), it resetsreq->length= 0 and returns active instead of the error code, causing the do-while loop inf_midi_transmit()to keep spinning undermidi->transmit_lockuntil the substream is drained.
Quoted 8 lines
Fix this by acquiring
midi->transmit_lockinf_midi_disable()while releasing IN requests fromin_req_fifo, and returning the error code fromf_midi_do_transmit()whenusb_ep_queue()fails sof_midi_transmit()aborts immediately and drops active substreams.Fixes e1e3d7ec5da3 ("usb: gadget: f_midi: pre-allocate IN requests")Signed-off-by Sonali Pradhan <[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
Quoted 13 lines · Sonali Pradhan
f_midi_disable()drains and frees pre-allocated IN requests fromin_req_fifowithout holdingmidi->transmit_lock. Iff_midi_transmit()runs concurrently on the workqueue,f_midi_do_transmit()can peek a request fromin_req_fifoand write into its buffer whilef_midi_disable()frees it, leading to a use-after-free.Additionally, when
usb_ep_queue()fails inf_midi_do_transmit()(such as with-ESHUTDOWNwhen the endpoint is disabled concurrently), it resetsreq->length= 0 and returns active instead of the error code, causing the do-while loop inf_midi_transmit()to keep spinning undermidi->transmit_lockuntil the substream is drained.
Quoted 8 lines
Fix this by acquiring
midi->transmit_lockinf_midi_disable()while releasing IN requests fromin_req_fifo, and returning the error code fromf_midi_do_transmit()whenusb_ep_queue()fails sof_midi_transmit()aborts immediately and drops active substreams.Fixes e1e3d7ec5da3 ("usb: gadget: f_midi: pre-allocate IN requests")Signed-off-by Sonali Pradhan <[email protected]>
thanks,
Takashi
Quoted 44 lines
---
Changes in v2:
- Remove thetransmit_lockandep->enabledchecks added in v1 fromf_midi_complete()andf_midi_transmit().
- Return error code fromf_midi_do_transmit()whenusb_ep_queue()fails
sof_midi_transmit()aborts the loop immediately.
- Link to v1: https://lore.kernel.org/all/[email protected]/drivers/usb/gadget/function/f_midi.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/usb/gadget/function/f_midi.c +4
-0@@ -420,6 +420,7 @@ static void f_midi_disable(struct usb_function *f)struct f_midi *midi =func_to_midi (f);struct usb_composite_dev *cdev = f->config->cdev;struct usb_request *req =NULL ; +unsigned long flags;DBG (cdev,"disable\n" );@@ -431,8 +432,10 @@ static void f_midi_disable(struct usb_function *f)usb_ep_disable (midi->out_ep);/* release IN requests */ +spin_lock_irqsave (&midi->transmit_lock, flags);while (kfifo_get (&midi->in_req_fifo, &req))free_ep_req (midi->in_ep, req); +spin_unlock_irqrestore (&midi->transmit_lock, flags);f_midi_drop_out_substreams (midi); }@@ -661,6 +664,7 @@ static int f_midi_do_transmit(struct f_midi *midi, struct usb_ep *ep)ERROR (midi,"%s failed to queue req: %d\n" , midi->in_ep->name, err); req->length =0 ;/* Re-use request next time. */ +return err; }else {/* Upon success, put request at the back of the queue. */ kfifo_skip (&midi->in_req_fifo);Signature · 1 line
2.56.0.307.g5fe7bd680c-goog