Skip to contentSkip to conversation

KASAN: slab-use-after-free in snd_usbtv_pcm_close

BUG New Shuangpeng Bai 1 participant 2 messages Jun 15 – Jul 6 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

Hi Kernel Maintainers,

I hit the following report while testing current upstream kernel:

KASAN: slab-use-after-free in snd_usbtv_pcm_close

I reproduced this on commit: e8c2f9fdadee7cbc75134dc463c1e0d856d6e5c7 (May 25 2026)

The reproducer and .config files are here.
https://gist.github.com/shuangpengbai/d75fcb55c539aa707f95debbbc541511

I'm happy to test debug patches or provide additional information.

[  143.650776][ T8284] ==================================================================
[  143.653135][ T8284] BUG: KASAN: slab-use-after-free in snd_usbtv_pcm_close (include/linux/instrumented.h:82 include/linux/atomic/atomic-instrumented.h:32 drivers/media/usb/usbtv/usbtv-audio.c:80)
[  143.655348][ T8284] Read of size 4 at addr ffff88816f8b0858 by task repro_usbtv_als/8284
[  143.658360][ T8284] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
[  143.658373][ T8284] Call Trace:
[  143.658401][ T8284]  <TASK>
[  143.658412][ T8284]  dump_stack_lvl (lib/dump_stack.c:94 lib/dump_stack.c:120)
[  143.658466][ T8284]  print_report (mm/kasan/report.c:378 mm/kasan/report.c:482)
[  143.658590][ T8284]  kasan_report (mm/kasan/report.c:595)
[  143.658627][ T8284]  kasan_check_range (mm/kasan/generic.c:? mm/kasan/generic.c:200)
[  143.658645][ T8284]  snd_usbtv_pcm_close (include/linux/instrumented.h:82 include/linux/atomic/atomic-instrumented.h:32 drivers/media/usb/usbtv/usbtv-audio.c:80)
[  143.658661][ T8284]  snd_pcm_release_substream (sound/core/pcm_native.c:2777)
[  143.658708][ T8284]  snd_pcm_release (sound/core/pcm_native.c:2965)
[  143.658720][ T8284]  __fput (fs/file_table.c:510)
[  143.658744][ T8284]  fput_close_sync (fs/file_table.c:615)
[  143.658797][ T8284]  __x64_sys_close (fs/open.c:1507 fs/open.c:1492 fs/open.c:1492)
[  143.658817][ T8284]  do_syscall_64 (arch/x86/entry/syscall_64.c:63 arch/x86/entry/syscall_64.c:94)
[  143.658841][ T8284]  entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
[  143.658929][ T8284]  </TASK>
[  143.675971][ T8284] Freed by task 10 on cpu 0 at 142.640459s:
[  143.676382][ T8284]  kasan_save_track (mm/kasan/common.c:57 mm/kasan/common.c:78)
[  143.676713][ T8284]  kasan_save_free_info (mm/kasan/generic.c:584)
[  143.677057][ T8284]  __kasan_slab_free (mm/kasan/common.c:253 mm/kasan/common.c:285)
[  143.677388][ T8284]  kfree (include/linux/kasan.h:235 mm/slub.c:2689 mm/slub.c:6251 mm/slub.c:6566)
[  143.677659][ T8284]  v4l2_device_put (drivers/media/v4l2-core/v4l2-device.c:51 include/linux/kref.h:65 drivers/media/v4l2-core/v4l2-device.c:56)
[  143.677981][ T8284]  usb_unbind_interface (drivers/usb/core/driver.c:458)
[  143.678538][ T8284]  device_release_driver_internal (drivers/base/dd.c:621 drivers/base/dd.c:1352 drivers/base/dd.c:1375)
[  143.678995][ T8284]  bus_remove_device (drivers/base/bus.c:657)
[  143.679349][ T8284]  device_del (drivers/base/core.c:3895)
[  143.679650][ T8284]  usb_disable_device (drivers/usb/core/message.c:1478)
[  143.680005][ T8284]  usb_disconnect (drivers/usb/core/hub.c:2315)
[  143.680340][ T8284]  hub_event (drivers/usb/core/hub.c:5407 drivers/usb/core/hub.c:5707 drivers/usb/core/hub.c:5871 drivers/usb/core/hub.c:5953)

Best,
Shuangpeng

#1 lore raw

PATCH 1/1 media: usbtv: keep device alive while ALSA card exists

The ALSA PCM callbacks store the driver state in pcm->private_data. An open PCM file can outlive USB disconnect because usbtv_audio_free() uses snd_card_free_when_closed(). The disconnect path can then drop the V4L2 device reference and free struct usbtv before ALSA releases the substream, so a later close dereferences freed memory in snd_usbtv_pcm_close().

Take a V4L2 device reference for the ALSA card and drop it from the card private_free callback. This keeps struct usbtv valid until ALSA has closed the remaining files and freed the card.

Fixes 63ddf68de52e ("[media] usbtv: add audio support")
---
 drivers/media/usb/usbtv/usbtv-audio.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/media/usb/usbtv/usbtv-audio.c +11 -0
index 333bd305a4f9..ae0a14e5ed2a 100644
@@ -317,6 +317,13 @@ static const struct snd_pcm_ops snd_usbtv_pcm_ops = {
 	.pointer = snd_usbtv_pointer,
 };
 
+static void usbtv_audio_card_free(struct snd_card *card)
+{
+	struct usbtv *usbtv = card->private_data;
+
+	v4l2_device_put(&usbtv->v4l2_dev);
+}
+
 int usbtv_audio_init(struct usbtv *usbtv)
 {
 	int rv;
@@ -331,6 +338,10 @@ int usbtv_audio_init(struct usbtv *usbtv)
 	if (rv < 0)
 		return rv;
 
+	v4l2_device_get(&usbtv->v4l2_dev);
+	card->private_data = usbtv;
+	card->private_free = usbtv_audio_card_free;
+
 	strscpy(card->driver, usbtv->dev->driver->name, sizeof(card->driver));
 	strscpy(card->shortname, "usbtv", sizeof(card->shortname));
 	snprintf(card->longname, sizeof(card->longname),
Signature · 1 line
2.43.0