The capture completion handler prepares and submits the playback URBs: when a capture URB completes, usx2y_usbframe_complete() takes the URB stored in playbacksubs->completed_urb and passes it to usx2y_urb_play_prepare(). snd_usx2y_pcm_hw_free() frees the playback URBs first, and only releases the capture URBs afterwards, and only when the capture substream is not open and prepared. Nothing waits for a capture completion that is still running, so it can write the ISO descriptors of a playback URB that was already freed: BUG: KASAN: slab-use-after-free in usx2y_urb_play_prepare Write of size 4 at addr ffff8880108620c0 Call Trace: usx2y_urb_play_prepare sound/usb/usx2y/usbusx2yaudio.c:129 usx2y_usbframe_complete sound/usb/usx2y/usbusx2yaudio.c:235 i_usx2y_urb_complete sound/usb/usx2y/usbusx2yaudio.c:332 __usb_hcd_giveback_urb usb_giveback_urb_bh usb_kill_urb() only waits for the completion of the URB it is called on, so killing the playback URBs does not wait for the capture completion that uses them. Release the capture stream before the playback URBs instead: its state is set to STATE_STOPPED first, so a completion that runs later does not touch the playback path, and usb_kill_urb() on the capture URBs waits for one that is still in flight. The same order also covers the full-duplex case where the capture substream is open and prepared: the playback URBs cannot be freed while the capture completion is driving them. usx2y_usbframe_complete() also ignores playbacksubs->completed_urb once the playback stream was stopped, so a completion that is already running cannot use the URB after it was freed. A KASAN slab-use-after-free write was observed when an OSS PCM stream was closed while a capture transfer was completing. The change is compile-tested only, as no US-122 hardware was available. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Co-developed-by: Bin Yu Signed-off-by: Bin Yu Co-developed-by: Cheng Wen Signed-off-by: Cheng Wen Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn> --- sound/usb/usx2y/usbusx2yaudio.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/sound/usb/usx2y/usbusx2yaudio.c b/sound/usb/usx2y/usbusx2yaudio.c index ef24c3ec690f..6cee877833a9 100644 --- a/sound/usb/usx2y/usbusx2yaudio.c +++ b/sound/usb/usx2y/usbusx2yaudio.c @@ -214,6 +214,8 @@ static int usx2y_usbframe_complete(struct snd_usx2y_substream *capsubs, struct urb *urb = playbacksubs->completed_urb; state = atomic_read(&playbacksubs->state); + if (state == STATE_STOPPED) + urb = NULL; if (urb) { if (state == STATE_RUNNING) usx2y_urb_play_retire(playbacksubs, urb); @@ -824,14 +826,14 @@ static int snd_usx2y_pcm_hw_free(struct snd_pcm_substream *substream) if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { cap_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]; + /* + * The capture completion feeds the playback URBs; release + * the capture stream first so usb_kill_urb() waits for it. + */ atomic_set(&subs->state, STATE_STOPPED); + atomic_set(&cap_subs->state, STATE_STOPPED); + usx2y_urbs_release(cap_subs); usx2y_urbs_release(subs); - if (!cap_subs->pcm_substream || - !cap_subs->pcm_substream->runtime || - cap_subs->pcm_substream->runtime->state < SNDRV_PCM_STATE_PREPARED) { - atomic_set(&cap_subs->state, STATE_STOPPED); - usx2y_urbs_release(cap_subs); - } } else { playback_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]; if (atomic_read(&playback_subs->state) < STATE_PREPARED) { -- 2.34.1