Skip to contentSkip to conversation

usb: gadget: f_uac1_legacy: fix copy_buf lifetime on queue handoff and free

PATCH Superseded Tristan Madani 3 participants 3 messages Nov 4 – Nov 20

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 usb: gadget: f_uac1_legacy: fix copy_buf lifetime on queue handoff and free

From: Tristan Madani <[email protected]>

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.

Clear audio->copy_buf 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. Use IS_ERR_OR_NULL for the copy_buf check because f_audio_buffer_alloc() returns ERR_PTR on failure and f_audio_set_alt() stores the result directly in audio->copy_buf.

Fixes c6994e6f067c ("USB: gadget: add USB Audio Gadget driver")

---

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)
- Attempted to detect copy_buf during queue drain, but that missed the case where playback_work already consumed it

Changes in v2:
- Added play_queue drain and copy_buf free (sashiko-bot)

 drivers/usb/gadget/function/f_uac1_legacy.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/usb/gadget/function/f_uac1_legacy.c +11 -0
index 7c8d60c7e87d1..cba7b447b09e3 100644
@@ -9,6 +9,7 @@
 #include <linux/slab.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/err.h>
 #include <linux/device.h>
 #include <linux/atomic.h>
 
@@ -338,6 +339,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(&copy_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);
@@ -670,6 +672,7 @@ static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
 			if (copy_buf) {
 				list_add_tail(&copy_buf->list,
 						&audio->play_queue);
+				audio->copy_buf = NULL;
 				schedule_work(&audio->playback_work);
 			}
 		}
@@ -942,7 +945,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 (!IS_ERR_OR_NULL(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);
 	kfree(audio);
Signature · 1 line
2.47.3
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 18 lines

From: Tristan Madani <[email protected]>

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.

Clear audio->copy_buf 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. Use IS_ERR_OR_NULL for the copy_buf check because f_audio_buffer_alloc() returns ERR_PTR on failure and f_audio_set_alt() stores the result directly in audio->copy_buf.

Quoted 3 lines
Fixes c6994e6f067c ("USB: gadget: add USB Audio Gadget driver")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Tristan Madani
Quoted 50 lines · Tristan Madani

From: Tristan Madani <[email protected]>

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.

Clear audio->copy_buf 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. Use IS_ERR_OR_NULL for the copy_buf check because f_audio_buffer_alloc() returns ERR_PTR on failure and f_audio_set_alt() stores the result directly in audio->copy_buf.

Fixes c6994e6f067c ("USB: gadget: add USB Audio Gadget driver")

---

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)
- Attempted to detect copy_buf during queue drain, but that missed the case where playback_work already consumed it

Changes in v2:
- Added play_queue drain and copy_buf free (sashiko-bot)

 drivers/usb/gadget/function/f_uac1_legacy.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/usb/gadget/function/f_uac1_legacy.c +0 -0
index 7c8d60c7e87d1..cba7b447b09e3 100644

drivers/usb/gadget/function/f_uac1_legacy.c line 12

Quoted 4 lines
drivers/usb/gadget/function/f_uac1_legacy.c +1 -0
@@ -9,6 +9,7 @@
 #include <linux/slab.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/err.h>

Are you sure this is needed? If so, why here in the list?

Quoted 21 lines
drivers/usb/gadget/function/f_uac1_legacy.c +2 -0
 #include <linux/device.h>
 #include <linux/atomic.h>
 
@@ -338,6 +339,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(&copy_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);
@@ -670,6 +672,7 @@ static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
 			if (copy_buf) {
 				list_add_tail(&copy_buf->list,
 						&audio->play_queue);
+				audio->copy_buf = NULL;
 				schedule_work(&audio->playback_work);
 			}
 		}
@@ -942,7 +945,15 @@ static void f_audio_free(struct usb_function *f)
 {

drivers/usb/gadget/function/f_uac1_legacy.c static void f_audio_free(struct usb_function *f) · line 955

Quoted 10 lines
drivers/usb/gadget/function/f_uac1_legacy.c +7 -0
 	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 (!IS_ERR_OR_NULL(audio->copy_buf))

How can it be an error? This is usually a sign something else is wrong, right?

thanks,

greg k-h