[PATCH] usb: gadget: f_uac1_legacy: disable OUT ep and drain work on teardown

From: hyunjungg

Date: Sun Nov 08 2026 - 07:27:44 EST


From: Hyunjung Ko <hj351016@xxxxxxxxx>

f_uac1_legacy enables the OUT endpoint in f_audio_set_alt() and queues
usb_requests whose completion handler f_audio_complete() dereferences the
f_audio (req->context) and audio->out_ep, and which schedules
audio->playback_work. However f_audio_disable() is empty and neither
f_audio_unbind() nor f_audio_free() disables the endpoint or drains the
work, so on teardown:

- f_audio_complete() can run after kfree(audio) (the UDC gives back
pending requests on the still-enabled endpoint during unbind), using
freed memory -- a use-after-free; and
- a playback_work still pending runs f_audio_playback_work() against the
freed f_audio -- a second use-after-free.

Implement f_audio_disable() to quiesce the function properly: disable the
OUT endpoint (which gives back outstanding requests, so f_audio_complete()
stops touching @audio and no new work is queued), then cancel_work_sync()
the playback work, then free the buffers left on audio->play_queue and
audio->copy_buf (f_audio_playback_work() is the only consumer, so a
cancelled work would otherwise leak them).

The endpoint is disabled before the work is cancelled, and the work is
cancelled before f_audio_free() runs gaudio_cleanup() on audio->card that
u_audio_playback() dereferences, so none of them race the teardown.

Found by static analysis and an automated reviewer; the playback_work
use-after-free was reproduced with KASAN under dummy_hcd (slab-use-after-
free in u_audio_playback() from f_audio_playback_work() after
f_audio_free(); a delay was added to the streaming path to make the
schedule-vs-free race deterministic). The out_ep/f_audio_complete
use-after-free is established by code inspection.

Fixes: f3a3406b3f56 ("usb: gadget: f_uac1: convert to new function interface with backward compatibility")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hyunjung Ko <hj351016@xxxxxxxxx>
---
v2: Completely reworked after review of v1 (which only cancel_work_sync'd
playback_work in f_audio_free()). v1 did not address the more serious
out_ep/f_audio_complete() use-after-free, cancelled after gaudio_cleanup()
rather than before, and leaked audio->play_queue buffers. v2 implements
f_audio_disable() to disable the endpoint, drain the work, and free the
buffers, and corrects the Fixes: tag (v1 cited the file-rename commit).
v1: https://lore.kernel.org/linux-usb/20261108075116.251501-1-hj351016@xxxxxxxxx/
drivers/usb/gadget/function/f_uac1_legacy.c | 30 ++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/function/f_uac1_legacy.c b/drivers/usb/gadget/function/f_uac1_legacy.c
index 7c8d60c..0f937a7 100644
--- a/drivers/usb/gadget/function/f_uac1_legacy.c
+++ b/drivers/usb/gadget/function/f_uac1_legacy.c
@@ -697,7 +697,35 @@ static int f_audio_get_alt(struct usb_function *f, unsigned intf)

static void f_audio_disable(struct usb_function *f)
{
- return;
+ struct f_audio *audio = func_to_audio(f);
+ struct f_audio_buf *buf;
+
+ /*
+ * Disable the OUT endpoint first: the UDC gives back any outstanding
+ * usb_requests, so f_audio_complete() stops dereferencing @audio and
+ * no new playback_work is queued. Then drain the work and free the
+ * buffers it would otherwise have consumed, before the function is
+ * unbound and freed.
+ */
+ if (audio->out_ep)
+ usb_ep_disable(audio->out_ep);
+
+ cancel_work_sync(&audio->playback_work);
+
+ /*
+ * The endpoint is disabled and the work is drained, so nothing else
+ * can touch the queue now; free the leftover buffers.
+ */
+ while (!list_empty(&audio->play_queue)) {
+ buf = list_first_entry(&audio->play_queue,
+ struct f_audio_buf, list);
+ list_del(&buf->list);
+ f_audio_buffer_free(buf);
+ }
+ if (audio->copy_buf) {
+ f_audio_buffer_free(audio->copy_buf);
+ audio->copy_buf = NULL;
+ }
}

/*-------------------------------------------------------------------------*/
--
2.43.0