mirror-linux/drivers/media/pci
Duoming Zhou 01e03fb7db media: b2c2: Fix use-after-free causing by irq_check_work in flexcop_pci_remove
The original code uses cancel_delayed_work() in flexcop_pci_remove(), which
does not guarantee that the delayed work item irq_check_work has fully
completed if it was already running. This leads to use-after-free scenarios
where flexcop_pci_remove() may free the flexcop_device while irq_check_work
is still active and attempts to dereference the device.

A typical race condition is illustrated below:

CPU 0 (remove)                         | CPU 1 (delayed work callback)
flexcop_pci_remove()                   | flexcop_pci_irq_check_work()
  cancel_delayed_work()                |
  flexcop_device_kfree(fc_pci->fc_dev) |
                                       |   fc = fc_pci->fc_dev; // UAF

This is confirmed by a KASAN report:

==================================================================
BUG: KASAN: slab-use-after-free in __run_timer_base.part.0+0x7d7/0x8c0
Write of size 8 at addr ffff8880093aa8c8 by task bash/135
...
Call Trace:
 <IRQ>
 dump_stack_lvl+0x55/0x70
 print_report+0xcf/0x610
 ? __run_timer_base.part.0+0x7d7/0x8c0
 kasan_report+0xb8/0xf0
 ? __run_timer_base.part.0+0x7d7/0x8c0
 __run_timer_base.part.0+0x7d7/0x8c0
 ? __pfx___run_timer_base.part.0+0x10/0x10
 ? __pfx_read_tsc+0x10/0x10
 ? ktime_get+0x60/0x140
 ? lapic_next_event+0x11/0x20
 ? clockevents_program_event+0x1d4/0x2a0
 run_timer_softirq+0xd1/0x190
 handle_softirqs+0x16a/0x550
 irq_exit_rcu+0xaf/0xe0
 sysvec_apic_timer_interrupt+0x70/0x80
 </IRQ>
...

Allocated by task 1:
 kasan_save_stack+0x24/0x50
 kasan_save_track+0x14/0x30
 __kasan_kmalloc+0x7f/0x90
 __kmalloc_noprof+0x1be/0x460
 flexcop_device_kmalloc+0x54/0xe0
 flexcop_pci_probe+0x1f/0x9d0
 local_pci_probe+0xdc/0x190
 pci_device_probe+0x2fe/0x470
 really_probe+0x1ca/0x5c0
 __driver_probe_device+0x248/0x310
 driver_probe_device+0x44/0x120
 __driver_attach+0xd2/0x310
 bus_for_each_dev+0xed/0x170
 bus_add_driver+0x208/0x500
 driver_register+0x132/0x460
 do_one_initcall+0x89/0x300
 kernel_init_freeable+0x40d/0x720
 kernel_init+0x1a/0x150
 ret_from_fork+0x10c/0x1a0
 ret_from_fork_asm+0x1a/0x30

Freed by task 135:
 kasan_save_stack+0x24/0x50
 kasan_save_track+0x14/0x30
 kasan_save_free_info+0x3a/0x60
 __kasan_slab_free+0x3f/0x50
 kfree+0x137/0x370
 flexcop_device_kfree+0x32/0x50
 pci_device_remove+0xa6/0x1d0
 device_release_driver_internal+0xf8/0x210
 pci_stop_bus_device+0x105/0x150
 pci_stop_and_remove_bus_device_locked+0x15/0x30
 remove_store+0xcc/0xe0
 kernfs_fop_write_iter+0x2c3/0x440
 vfs_write+0x871/0xd70
 ksys_write+0xee/0x1c0
 do_syscall_64+0xac/0x280
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
...

Replace cancel_delayed_work() with cancel_delayed_work_sync() to ensure
that the delayed work item is properly canceled and any executing delayed
work has finished before the device memory is deallocated.

This bug was initially identified through static analysis. To reproduce
and test it, I simulated the B2C2 FlexCop PCI device in QEMU and introduced
artificial delays within the flexcop_pci_irq_check_work() function to
increase the likelihood of triggering the bug.

Fixes: 382c5546d6 ("V4L/DVB (10694): [PATCH] software IRQ watchdog for Flexcop B2C2 DVB PCI cards")
Cc: stable@vger.kernel.org
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
Signed-off-by: Hans Verkuil <hverkuil+cisco@kernel.org>
2025-09-17 12:14:06 +02:00
..
b2c2 media: b2c2: Fix use-after-free causing by irq_check_work in flexcop_pci_remove 2025-09-17 12:14:06 +02:00
bt8xx media: v4l2: Rename second ioctl handlers argument to 'void *priv' 2025-08-13 08:34:01 +02:00
cobalt media: update Hans Verkuil's email address 2025-09-09 15:58:44 +02:00
cx18 media: update Hans Verkuil's email address 2025-09-09 15:59:02 +02:00
cx88 media: Switch to use hrtimer_setup() 2025-02-18 11:19:04 +01:00
cx23885 media: cx23885: Add analog support for AVerMedia H789-C PCIe card 2025-03-04 15:56:11 +01:00
cx25821 media: pci: drop vb2_ops_wait_prepare/finish 2024-10-28 09:19:53 +01:00
ddbridge media: Convert from tasklet to BH workqueue 2024-06-21 08:57:09 +02:00
dm1105 media: add HAS_IOPORT dependencies 2023-06-28 11:09:25 +02:00
dt3155 media: pci: drop vb2_ops_wait_prepare/finish 2024-10-28 09:19:53 +01:00
intel media: ipu6: isys: Set embedded data type correctly for metadata formats 2025-08-25 15:40:34 +02:00
ivtv media: update Hans Verkuil's email address 2025-09-09 15:59:02 +02:00
mantis media: mantis: remove orphan mantis_core.h 2024-10-28 15:35:56 +01:00
mgb4 media: pci: mgb4: Fix timings comparison in VIDIOC_S_DV_TIMINGS 2025-08-29 11:04:03 +02:00
netup_unidvb treewide, timers: Rename from_timer() to timer_container_of() 2025-06-08 09:07:37 +02:00
ngene media: Convert from tasklet to BH workqueue 2024-06-21 08:57:09 +02:00
pluto2
pt1
pt3 media: pt3: Replace deprecated PCI functions 2025-04-25 15:14:30 +02:00
saa7134 media: v4l2: Rename second ioctl handlers argument to 'void *priv' 2025-08-13 08:34:01 +02:00
saa7146
saa7164 media: Reset file->private_data to NULL in v4l2_fh_del() 2025-08-13 08:33:44 +02:00
smipcie media: Convert from tasklet to BH workqueue 2024-06-21 08:57:09 +02:00
solo6x10 treewide: rename GPIO set callbacks back to their original names 2025-08-07 10:07:06 +02:00
ttpci media: Convert from tasklet to BH workqueue 2024-06-21 08:57:09 +02:00
tw68 media: update Hans Verkuil's email address 2025-09-09 15:59:02 +02:00
tw686x treewide, timers: Rename from_timer() to timer_container_of() 2025-06-08 09:07:37 +02:00
tw5864 media: tw5864: Replace deprecated PCI functions 2025-04-25 15:14:31 +02:00
zoran media: pci: zoran: Remove unused debug parameter 2025-08-29 11:04:01 +02:00
Kconfig media: remove STA2x11 media pci driver 2025-04-25 15:14:25 +02:00
Makefile media: remove STA2x11 media pci driver 2025-04-25 15:14:25 +02:00