usb: gadget: f_uac1_legacy: cancel playback_work before freeing audio
Subsystems (2)
PATCH 1/1 usb: gadget: f_uac1_legacy: cancel playback_work before freeing audio
From: Tristan Madani <[email protected]>
f_audio_free() frees the f_audio struct without first cancelling playback_work. If the work function is still running or pending at the time of the free, it will access freed memory via container_of.
Add cancel_work_sync() before cleanup and deallocation.
--- drivers/usb/gadget/function/f_uac1_legacy.c | 1 + 1 file changed, 1 insertion(+)
drivers/usb/gadget/function/f_uac1_legacy.c +1 -0
@@ -943,6 +943,7 @@ static void f_audio_free(struct usb_function *f)struct f_audio *audio =func_to_audio (f);struct f_uac1_legacy_opts *opts; +cancel_work_sync (&audio->playback_work);gaudio_cleanup (&audio->card); opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);kfree (audio);
Signature · 1 line
2.47.3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] usb: gadget: f_uac1_legacy: f_audio_free() leaks play_queue and copy_buf buffers --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] usb: gadget: f_uac1_legacy: f_audio_free() leaks play_queue and copy_buf buffers --
commit 7b0893434cf97360fc2c156524033f06ad05654d
Author: Tristan Madani <[email protected]>
usb: gadget: f_uac1_legacy: cancel playback_work before freeing audio
This commit calls cancel_work_sync(&audio->playback_work) in f_audio_free() before cleaning up the audio card and freeing the f_audio structure so that a pending or running playback_work does not access freed memory.
drivers/usb/gadget/function/f_uac1_legacy.c static void f_audio_free(struct usb_function *f) · line 949
Quoted 12 lines
drivers/usb/gadget/function/f_uac1_legacy.c +1
-0@@ -943,6 +943,7 @@ static void f_audio_free(struct usb_function *f)struct f_audio *audio =func_to_audio (f);struct f_uac1_legacy_opts *opts; +cancel_work_sync (&audio->playback_work);gaudio_cleanup (&audio->card); opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);kfree (audio);
[Severity: Medium]
Does cancelling playback_work here without draining audio->play_queue or freeing audio->copy_buf leak struct f_audio_buf buffers?
During streaming, f_audio_out_ep_complete() appends full copy_buf buffers to audio->play_queue and schedules audio->playback_work:
if (audio_buf_size - copy_buf->actual < req->actual) {
spin_lock_irq(&audio->lock);
list_add_tail(©_buf->list, &audio->play_queue);
spin_unlock_irq(&audio->lock);
schedule_work(&audio->playback_work);
copy_buf = f_audio_buffer_alloc(audio_buf_size);f_audio_playback_work() is the only consumer that removes entries from audio->play_queue and frees them with f_audio_buffer_free(), and it only processes one entry per invocation:
play_buf = list_first_entry(&audio->play_queue, struct f_audio_buf, list); list_del(&play_buf->list); spin_unlock_irq(&audio->lock); u_audio_playback(&audio->card, play_buf->buf, play_buf->actual); f_audio_buffer_free(play_buf);
When cancel_work_sync(&audio->playback_work) cancels a pending work item in f_audio_free() (or when multiple buffers are queued on audio->play_queue, or audio->copy_buf is still allocated), those f_audio_buf structures and their backing data buffers are never freed before kfree(audio).
Should f_audio_free() drain and free any remaining entries on audio->play_queue as well as audio->copy_buf after cancelling audio->playback_work?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1