* [PATCH v5] usb: gadget: f_uac1_legacy: fix copy_buf lifetime on queue handoff and free
@ 2026-11-20 21:53 Tristan Madani
2026-11-20 22:05 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Tristan Madani @ 2026-11-20 21:53 UTC (permalink / raw)
To: Neill Kapron, Greg Kroah-Hartman; +Cc: linux-usb, stable, Tristan Madani
f_audio_free() tears down the f_audio structure without cancelling
playback_work or releasing audio buffers. The pending work can access
the freed structure, and queued audio buffers leak.
Additionally, f_audio_set_alt() and f_audio_out_ep_complete() transfer
copy_buf to play_queue without clearing audio->copy_buf. When
playback_work later consumes the buffer from the queue, audio->copy_buf
becomes a stale reference.
Furthermore, f_audio_buffer_alloc() returns ERR_PTR on failure, and
f_audio_set_alt() stores that directly in audio->copy_buf, leaving an
invalid pointer in the struct field.
Clear audio->copy_buf to NULL in the alloc error path, and when its
buffer is queued to play_queue, so the pointer reliably reflects
ownership. In f_audio_free(), cancel playback_work, drain the
play_queue, and free copy_buf when it was not queued.
Fixes: c6994e6f067c ("USB: gadget: add USB Audio Gadget driver")
Cc: stable@vger.kernel.org
Signed-off-by: Tristan Madani <tristan@talencesecurity.com>
---
Changes in v5:
- Fix the root cause of IS_ERR_OR_NULL: clear audio->copy_buf to NULL
in the f_audio_set_alt() alloc error path so an ERR_PTR is never
left in the struct field, as pointed out by Greg Kroah-Hartman
- Drop the linux/err.h include now that IS_ERR_OR_NULL is no longer used
in f_audio_free()
- Use a plain NULL check for copy_buf in f_audio_free() since
audio->copy_buf can now only be NULL or a valid pointer
Changes in v4:
- Fix the root cause of the stale copy_buf reference: clear
audio->copy_buf at the two sites that transfer ownership to
play_queue (f_audio_out_ep_complete inside the spinlock, and
f_audio_set_alt alt 0) so playback_work consuming the buffer
can no longer leave a stale pointer (sashiko-bot)
- Simplify f_audio_free drain: no buf == copy_buf comparison
needed since copy_buf is now reliably NULL when its buffer
is on the queue
Changes in v3:
- Guard copy_buf free with IS_ERR_OR_NULL because
f_audio_buffer_alloc() returns ERR_PTR on failure (sashiko-bot)
Changes in v2:
- Added play_queue drain and copy_buf free (sashiko-bot)
drivers/usb/gadget/function/f_uac1_legacy.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/gadget/function/f_uac1_legacy.c b/drivers/usb/gadget/function/f_uac1_legacy.c
index 7c8d60c7e87d1..b541002d2500a 100644
--- a/drivers/usb/gadget/function/f_uac1_legacy.c
+++ b/drivers/usb/gadget/function/f_uac1_legacy.c
@@ -338,6 +338,7 @@ static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req)
if (audio_buf_size - copy_buf->actual < req->actual) {
spin_lock_irq(&audio->lock);
list_add_tail(©_buf->list, &audio->play_queue);
+ audio->copy_buf = NULL;
spin_unlock_irq(&audio->lock);
schedule_work(&audio->playback_work);
copy_buf = f_audio_buffer_alloc(audio_buf_size);
@@ -636,8 +637,10 @@ static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
usb_ep_enable(out_ep);
audio->copy_buf = f_audio_buffer_alloc(audio_buf_size);
- if (IS_ERR(audio->copy_buf))
+ if (IS_ERR(audio->copy_buf)) {
+ audio->copy_buf = NULL;
return -ENOMEM;
+ }
/*
* allocate a bunch of read buffers
@@ -670,6 +673,7 @@ static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
if (copy_buf) {
list_add_tail(©_buf->list,
&audio->play_queue);
+ audio->copy_buf = NULL;
schedule_work(&audio->playback_work);
}
}
@@ -942,6 +946,15 @@ static void f_audio_free(struct usb_function *f)
{
struct f_audio *audio = func_to_audio(f);
struct f_uac1_legacy_opts *opts;
+ struct f_audio_buf *buf, *tmp;
+
+ cancel_work_sync(&audio->playback_work);
+ list_for_each_entry_safe(buf, tmp, &audio->play_queue, list) {
+ list_del(&buf->list);
+ f_audio_buffer_free(buf);
+ }
+ if (audio->copy_buf)
+ f_audio_buffer_free(audio->copy_buf);
gaudio_cleanup(&audio->card);
opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
--
2.47.3
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v5] usb: gadget: f_uac1_legacy: fix copy_buf lifetime on queue handoff and free
2026-11-20 21:53 [PATCH v5] usb: gadget: f_uac1_legacy: fix copy_buf lifetime on queue handoff and free Tristan Madani
@ 2026-11-20 22:05 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-20 22:05 UTC (permalink / raw)
To: Tristan Madani; +Cc: linux-usb
> f_audio_free() tears down the f_audio structure without cancelling
> playback_work or releasing audio buffers. The pending work can access
> the freed structure, and queued audio buffers leak.
>
> Additionally, f_audio_set_alt() and f_audio_out_ep_complete() transfer
> copy_buf to play_queue without clearing audio->copy_buf. When
> playback_work later consumes the buffer from the queue, audio->copy_buf
> becomes a stale reference.
>
> Furthermore, f_audio_buffer_alloc() returns ERR_PTR on failure, and
> f_audio_set_alt() stores that directly in audio->copy_buf, leaving an
> invalid pointer in the struct field.
>
> Clear audio->copy_buf to NULL in the alloc error path, and when its
> buffer is queued to play_queue, so the pointer reliably reflects
> ownership. In f_audio_free(), cancel playback_work, drain the
> play_queue, and free copy_buf when it was not queued.
>
> Fixes: c6994e6f067c ("USB: gadget: add USB Audio Gadget driver")
> Cc: stable@vger.kernel.org
> Signed-off-by: Tristan Madani <tristan@talencesecurity.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120215351.64406-1-tristan@talencesecurity.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-20 22:05 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 21:53 [PATCH v5] usb: gadget: f_uac1_legacy: fix copy_buf lifetime on queue handoff and free Tristan Madani
2026-11-20 22:05 ` sashiko-bot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.