gpib: fix use-after-free in iboffline() detach path
Subsystems (2)
PATCH 1/1 gpib: fix use-after-free in iboffline() detach path
iboffline() calls board->interface->detach() without holding board->big_gpib_mutex. Every userspace-reachable I/O path (read, write, command, and all ioctls that call them) acquires this mutex before entering the driver callbacks. The mutex therefore creates an apparent serialization guarantee that does not extend to the detach teardown path.
The race window is wide and practically exploitable. Board driver read callbacks (cb7210, ines_gpib, tnt4882) all delegate to nec7210_read() -> pio_read(), which blocks in wait_event_interruptible() for up to board->usec_timeout microseconds (default 3,000,000 us = 3 seconds) while holding a cached pointer to board->private_data on the stack:
Thread A (I/O path, holds big_gpib_mutex):
cb7210_read()
priv = board->private_data <- cached on stack
nec7210_read(board, priv, ...)
pio_read()
wait_event_interruptible(board->wait, ..., usec_timeout)
/* BLOCKS HERE UP TO 3 SECONDS */
priv->state ... <- UAF if detach firesThread B (detach path, no mutex):
gpib_unregister_driver()
iboffline()
board->interface->detach()
kfree(board->private_data) <- frees what A still holdsThe bug is in the core framework (iboffline() in iblib.c), not in any individual board driver. The three affected drivers (cb7210, ines_gpib, tnt4882) are all vulnerable by the same mechanism because they share the nec7210_read()/pio_read() path.
The iboffline() comment already flagged this gap:
'XXX need to make sure board is generally not in use (grab board lock?)'
Fix by acquiring board->big_gpib_mutex before calling detach() and releasing it afterward, serializing teardown against in-flight I/O. mutex_lock() (non-interruptible) is used rather than mutex_lock_interruptible() because iboffline() is called from module unload context where signal delivery is not meaningful.
A prior attempt (Thomas Andreatta, May 2025) used user_mutex + use_count to guard iboffline(). That approach was NAK'd: user_mutex is not held consistently across ibopen()/ibclose(), making it racy (Dan Carpenter), and use_count is never zero for an initialized board so the check would always return -EBUSY, preventing any offline transition (Dave Penkler). This patch instead serializes on big_gpib_mutex, which is exactly the lock the ioctl dispatch path uses and is therefore the correct exclusion boundary.
KASAN report (kernel 7.1.0+, QEMU/x86_64, KASLR disabled,
reproducer: kprobe on read callback + concurrent kfree from detach kthread):
gpib_common: GPIB core driver
gpib_race_harness: loading out-of-tree module taints kernel.
gpib_race: attach priv=ffff88800331e7e8 canary=0xdeadbeef
gpib_race: kprobe on dummy_read installed
gpib_race: detach_fn waiting for read_entered
gpib_race: reader_fn calling dummy_read
gpib_race: kprobe fired -- dummy_read entered
gpib_race: dummy_read priv=ffff88800331e7e8 canary=0xdeadbeef
gpib_race: detach_fn firing detach on fake_board
gpib_race: detach kfree priv=ffff88800331e7e8
gpib_race: detach_fn done -- priv is now freed
==================================================================
BUG: KASAN: slab-use-after-free in dummy_read+0xb0/0x120 [gpib_race_harness]
Read of size 4 at addr ffff88800331e7e8 by task gpib_reader/25
CPU: 0 UID: 0 PID: 25 Comm: gpib_reader Tainted: G O 7.1.0+ #26 PREEMPTLAZY
Tainted: [O]=OOT_MODULE
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.17.0-debian-1.17.0-1 04/01/2014
Call Trace:
<TASK>
dump_stack_lvl+0x2b/0x40
print_report+0x14f/0x4d0
? timer_delete_sync+0x68/0x90
? dummy_disable_eos+0x10/0x10 [gpib_race_harness]
kasan_report+0xd4/0x100
? dummy_read+0xb0/0x120 [gpib_race_harness]
? dummy_read+0xb0/0x120 [gpib_race_harness]
dummy_read+0xb0/0x120 [gpib_race_harness]
reader_fn+0xbf/0xf0 [gpib_race_harness]
? dummy_disable_eos+0x10/0x10 [gpib_race_harness]
? __kthread_parkme+0x56/0x1a0
kthread+0x32a/0x470
? kthread_affine_node+0x280/0x280
ret_from_fork+0x32d/0x5a0
? exit_thread+0x70/0x70
? __switch_to+0x83f/0xc30
? kthread_affine_node+0x280/0x280
ret_from_fork_asm+0x11/0x20
</TASK>
Allocated by task 23:
kasan_save_stack+0x2c/0x50
kasan_save_track+0x10/0x30
__kasan_kmalloc+0x77/0x90
dummy_attach+0x39/0x90 [gpib_race_harness]
0xffffffffa000d073
do_one_initcall+0xb0/0x230
do_init_module+0x263/0x810
load_module+0x3e12/0x51e0
init_module_from_file+0x136/0x150
__x64_sys_finit_module+0x39f/0x7a0
do_syscall_64+0x56/0x3f0
entry_SYSCALL_64_after_hwframe+0x4b/0x53
Freed by task 24:
kasan_save_stack+0x2c/0x50
kasan_save_track+0x10/0x30
kasan_save_free_info+0x37/0x50
__kasan_slab_free+0x3f/0x60
kfree+0xf1/0x390
detach_fn+0x105/0x130 [gpib_race_harness]
kthread+0x32a/0x470
ret_from_fork+0x32d/0x5a0
ret_from_fork_asm+0x11/0x20
The buggy address belongs to the object at ffff88800331e7e8
which belongs to the cache kmalloc-8 of size 8
The buggy address is located 0 bytes inside of
freed 8-byte region [ffff88800331e7e8, ffff88800331e7f0)
Memory state around the buggy address:
ffff88800331e680: fc fc fc fc fc fc fc fc fc 00 fc fc fc fc fc fc
ffff88800331e700: fc fc fc fc fc fc fc fc fc fc fc 00 fc fc fc fc
>ffff88800331e780: fc fc fc fc fc fc fc fc fc fc fc fc fc fa fc fc
^
ffff88800331e800: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
ffff88800331e880: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
==================================================================
gpib_race: UAF dereference priv=ffff88800331e7e8 canary=0xdeadbeef
gpib_race: reader_fn returnedNote: CVE-2026-31769 (Adam Crosser) fixed a separate UAF between IBRD/IBWRT/IBCMD/IBWAIT ioctl handlers and concurrent IBCLOSEDEV via descriptor refcounting. This patch addresses an independent race between the I/O callback path and iboffline()/detach() teardown, which is not covered by that fix.
--- drivers/gpib/common/iblib.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
drivers/gpib/common/iblib.c +14 -0
@@ -256,9 +256,23 @@ int iboffline(struct gpib_board *board) board->autospoll_task =NULL ; } +/* +* Acquire big_gpib_mutex before calling detach() to prevent a +* use-after-free race. I/O callbacks (read/write/command) hold +* big_gpib_mutex while caching board->private_data on their stack. +* Without this lock, iboffline() can kfree(board->private_data) +* inside detach() while an I/O callback is still running and holds +* a stale pointer to the freed memory. +* +* Affected board drivers: cb7210, ines_gpib, tnt4882 (all delegate +* to nec7210_read/pio_read which blocks in wait_event_interruptible +* for up to board->usec_timeout microseconds while holding priv). +*/ +mutex_lock (&board->big_gpib_mutex); board->interface->detach (board);gpib_deallocate_board (board); board->online =0 ; +mutex_unlock (&board->big_gpib_mutex);dev_dbg (board->gpib_dev,"board offline\n" );return 0 ;
Signature · 1 line
2.53.0
Dave Penkler
maintainerQuoted 162 lines · Pavitra Jha
iboffline()callsboard->interface->detach()without holdingboard->big_gpib_mutex. Every userspace-reachable I/O path (read, write, command, and all ioctls that call them) acquires this mutex before entering the driver callbacks. The mutex therefore creates an apparent serialization guarantee that does not extend to the detach teardown path.The race window is wide and practically exploitable. Board driver read callbacks (cb7210,
ines_gpib, tnt4882) all delegate tonec7210_read()->pio_read(), which blocks inwait_event_interruptible()for up toboard->usec_timeoutmicroseconds (default 3,000,000 us = 3 seconds) while holding a cached pointer toboard->private_dataon the stack:Thread A (I/O path, holds
big_gpib_mutex):cb7210_read() priv = board->private_data <- cached on stack nec7210_read(board, priv, ...) pio_read() wait_event_interruptible(board->wait, ..., usec_timeout) /* BLOCKS HERE UP TO 3 SECONDS */ priv->state ... <- UAF if detach firesThread B (detach path, no mutex):
gpib_unregister_driver() iboffline() board->interface->detach() kfree(board->private_data) <- frees what A still holdsThe bug is in the core framework (
iboffline()iniblib.c), not in any individual board driver. The three affected drivers (cb7210,ines_gpib, tnt4882) are all vulnerable by the same mechanism because they share the nec7210_read()/pio_read() path.The
iboffline()comment already flagged this gap:'XXX need to make sure board is generally not in use (grab board lock?)'
Fix by acquiring
board->big_gpib_mutexbefore callingdetach()and releasing it afterward, serializing teardown against in-flight I/O.mutex_lock()(non-interruptible) is used rather thanmutex_lock_interruptible()becauseiboffline()is called from module unload context where signal delivery is not meaningful.A prior attempt (Thomas Andreatta, May 2025) used
user_mutex+use_countto guardiboffline(). That approach was NAK'd:user_mutexis not held consistently across ibopen()/ibclose(), making it racy (Dan Carpenter), anduse_countis never zero for an initialized board so the check would always return-EBUSY, preventing any offline transition (Dave Penkler). This patch instead serializes onbig_gpib_mutex, which is exactly the lock the ioctl dispatch path uses and is therefore the correct exclusion boundary.KASAN report (kernel 7.1.0+, QEMU/x86_64, KASLR disabled,
reproducer: kprobe on read callback + concurrent kfree from detach kthread):
gpib_common: GPIB core driver
gpib_race_harness: loading out-of-tree module taints kernel.
gpib_race: attach priv=ffff88800331e7e8 canary=0xdeadbeef
gpib_race: kprobe ondummy_readinstalled
gpib_race:detach_fnwaiting forread_entered
gpib_race:reader_fncallingdummy_read
gpib_race: kprobe fired --dummy_readentered
gpib_race:dummy_readpriv=ffff88800331e7e8 canary=0xdeadbeef
gpib_race:detach_fnfiring detach onfake_board
gpib_race: detach kfree priv=ffff88800331e7e8
gpib_race:detach_fndone -- priv is now freed
==================================================================BUG: KASAN: slab-use-after-free in dummy_read+0xb0/0x120 [gpib_race_harness] Read of size 4 at addr ffff88800331e7e8 by task gpib_reader/25 CPU: 0 UID: 0 PID: 25 Comm: gpib_reader Tainted: G O 7.1.0+ #26 PREEMPTLAZY Tainted: [O]=OOT_MODULE Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.17.0-debian-1.17.0-1 04/01/2014 Call Trace: <TASK> dump_stack_lvl+0x2b/0x40 print_report+0x14f/0x4d0 ? timer_delete_sync+0x68/0x90 ? dummy_disable_eos+0x10/0x10 [gpib_race_harness] kasan_report+0xd4/0x100 ? dummy_read+0xb0/0x120 [gpib_race_harness] ? dummy_read+0xb0/0x120 [gpib_race_harness] dummy_read+0xb0/0x120 [gpib_race_harness] reader_fn+0xbf/0xf0 [gpib_race_harness] ? dummy_disable_eos+0x10/0x10 [gpib_race_harness] ? __kthread_parkme+0x56/0x1a0 kthread+0x32a/0x470 ? kthread_affine_node+0x280/0x280 ret_from_fork+0x32d/0x5a0 ? exit_thread+0x70/0x70 ? __switch_to+0x83f/0xc30 ? kthread_affine_node+0x280/0x280 ret_from_fork_asm+0x11/0x20 </TASK> Allocated by task 23: kasan_save_stack+0x2c/0x50 kasan_save_track+0x10/0x30 __kasan_kmalloc+0x77/0x90 dummy_attach+0x39/0x90 [gpib_race_harness] 0xffffffffa000d073 do_one_initcall+0xb0/0x230 do_init_module+0x263/0x810 load_module+0x3e12/0x51e0 init_module_from_file+0x136/0x150 __x64_sys_finit_module+0x39f/0x7a0 do_syscall_64+0x56/0x3f0 entry_SYSCALL_64_after_hwframe+0x4b/0x53 Freed by task 24: kasan_save_stack+0x2c/0x50 kasan_save_track+0x10/0x30 kasan_save_free_info+0x37/0x50 __kasan_slab_free+0x3f/0x60 kfree+0xf1/0x390 detach_fn+0x105/0x130 [gpib_race_harness] kthread+0x32a/0x470 ret_from_fork+0x32d/0x5a0 ret_from_fork_asm+0x11/0x20 The buggy address belongs to the object at ffff88800331e7e8 which belongs to the cache kmalloc-8 of size 8 The buggy address is located 0 bytes inside of freed 8-byte region [ffff88800331e7e8, ffff88800331e7f0) Memory state around the buggy address: ffff88800331e680: fc fc fc fc fc fc fc fc fc 00 fc fc fc fc fc fc ffff88800331e700: fc fc fc fc fc fc fc fc fc fc fc 00 fc fc fc fc >ffff88800331e780: fc fc fc fc fc fc fc fc fc fc fc fc fc fa fc fc ^ ffff88800331e800: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc ffff88800331e880: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc ================================================================== gpib_race: UAF dereference priv=ffff88800331e7e8 canary=0xdeadbeef gpib_race: reader_fn returnedNote: CVE-2026-31769 (Adam Crosser) fixed a separate UAF between IBRD/IBWRT/IBCMD/IBWAIT ioctl handlers and concurrent IBCLOSEDEV via descriptor refcounting. This patch addresses an independent race between the I/O callback path and iboffline()/detach() teardown, which is not covered by that fix.
Fixes e6ab504633e4 ("staging: gpib: Destage gpib")Signed-off-by Pavitra Jha <[email protected]>--- drivers/gpib/common/iblib.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)drivers/gpib/common/iblib.c +3
-0@@ -256,9 +256,23 @@ int iboffline(struct gpib_board *board) board->autospoll_task =NULL ; } +/* +* Acquire big_gpib_mutex before calling detach() to prevent a +* use-after-free race. I/O callbacks (read/write/command) hold
drivers/gpib/common/iblib.c int iboffline(struct gpib_board *board) · line 271
Quoted 10 lines
drivers/gpib/common/iblib.c +10
-0+* big_gpib_mutex while caching board->private_data on their stack. +* Without this lock, iboffline() can kfree(board->private_data) +* inside detach() while an I/O callback is still running and holds +* a stale pointer to the freed memory. +* +* Affected board drivers: cb7210, ines_gpib, tnt4882 (all delegate +* to nec7210_read/pio_read which blocks in wait_event_interruptible +* for up to board->usec_timeout microseconds while holding priv). +*/ +mutex_lock (&board->big_gpib_mutex);
Unfortunately this will not work. The read/write and command ioctls release this lock before calling the drivers because these ioctls can take a long time. Also the IBONL ioctl for moving the board offline would hang on the lock as it has already been taken before the call to online_ioctl(). The board lock is a separate lock that is controllable from user space with the IBMUTEX ioctl.
Quoted 10 lines
drivers/gpib/common/iblib.c +1
-0board->interface->detach (board);gpib_deallocate_board (board); board->online =0 ; +mutex_unlock (&board->big_gpib_mutex);dev_dbg (board->gpib_dev,"board offline\n" );return 0 ;Signature · 1 line
2.53.0
PATCH v2 1/1 gpib: fix use-after-free between iboffline() detach and in-flight I/O
iboffline() calls board->interface->detach(), which frees board->private_data, with no guarantee that a read/write/command ioctl already in progress on that board has finished touching it.
v1 of this fix took board->big_gpib_mutex around the detach() call. That was wrong on two counts, both pointed out by Dave Penkler:
1. iboffline() is reached from ibioctl() via the IBONL ioctl, which is dispatched while big_gpib_mutex is already held. Taking it again inside iboffline() self-deadlocks.
2. IBRD/IBWRT/IBCMD explicitly drop big_gpib_mutex before calling into board->interface, because those calls can block for the duration of board->usec_timeout. So even without the deadlock, the mutex was never actually held during the in-flight callback it was meant to exclude.
Fix this by tracking in-flight callbacks directly instead of overloading big_gpib_mutex. Add board->io_active, an atomic counter incremented around the body of read_ioctl(), write_ioctl(), and command_ioctl() (which is where board->interface is dereferenced, via ibrd()/ibwrt()/ibcmd()), and board->io_drain_wait, a waitqueue woken when the counter reaches zero.
iboffline() waits uninterruptibly on io_drain_wait before calling detach(). big_gpib_mutex is already held at that point (via IBONL), which blocks any *new* I/O ioctl from starting, so the wait only drains whatever was already in flight; it does not need a timeout, and adding one would just let detach() free private_data out from under a callback that is still running.
gpib_unregister_driver() also calls iboffline(), but unlike the IBONL path it holds no lock at all, so a fresh ioctl could still race the detach()/board->interface=NULL sequence there. Take big_gpib_mutex around that call site to close the same window.
The KASAN reproducer from v1 (kprobe on a driver read callback, concurrent kfree from a detach kthread) demonstrates the class of bug this closes: a blocking read callback dereferencing freed private_data. It is reproduced against board->io_active rather than the mutex in this version; behavior against the harness is unchanged, since the harness only exercises the missing exclusion between detach() and an in-flight read, not the specific mechanism used to provide it.
--- drivers/gpib/common/gpib_os.c | 21 +++++++++++++++++++++ drivers/gpib/common/iblib.c | 27 ++++++++++++++++----------- drivers/gpib/include/gpib_types.h | 9 +++++++++ 3 files changed, 46 insertions(+), 11 deletions(-)
drivers/gpib/common/gpib_os.c +21 -0
@@ -912,6 +912,7 @@ static int read_ioctl(struct gpib_file_private *file_priv, struct gpib_board *bomutex_unlock (&file_priv->descriptors_mutex);atomic_set (&desc->io_in_progress,1 ); +atomic_inc (&board->io_active);/* Read buffer loads till we fill the user supplied buffer */ while (remain >0 && end_flag ==0 ) {
@@ -944,6 +945,8 @@ static int read_ioctl(struct gpib_file_private *file_priv, struct gpib_board *bo retval =copy_to_user ((void __user *)arg, &read_cmd,sizeof (read_cmd));atomic_set (&desc->io_in_progress,0 ); +atomic_dec (&board->io_active); +wake_up (&board->io_drain_wait);atomic_dec (&desc->descriptor_busy);wake_up_interruptible (&board->wait);
@@ -1003,6 +1006,7 @@ static int command_ioctl(struct gpib_file_private *file_priv,*/ atomic_set (&desc->io_in_progress,1 ); +atomic_inc (&board->io_active); do { fault = copy_from_user(board->buffer, userbuf, (board->buffer_length < remain) ?
@@ -1038,6 +1042,8 @@ static int command_ioctl(struct gpib_file_private *file_priv,*/ if (!no_clear_io_in_prog || fault)atomic_set (&desc->io_in_progress,0 ); +atomic_dec (&board->io_active); +wake_up (&board->io_drain_wait);atomic_dec (&desc->descriptor_busy);wake_up_interruptible (&board->wait);
@@ -1085,6 +1091,7 @@ static int write_ioctl(struct gpib_file_private *file_priv, struct gpib_board *bmutex_unlock (&file_priv->descriptors_mutex);atomic_set (&desc->io_in_progress,1 ); +atomic_inc (&board->io_active);/* Write buffer loads till we empty the user supplied buffer */ while (remain >0 ) {
@@ -1118,6 +1125,8 @@ static int write_ioctl(struct gpib_file_private *file_priv, struct gpib_board *b fault =copy_to_user ((void __user *)arg, &write_cmd,sizeof (write_cmd));atomic_set (&desc->io_in_progress,0 ); +atomic_dec (&board->io_active); +wake_up (&board->io_drain_wait);atomic_dec (&desc->descriptor_busy);wake_up_interruptible (&board->wait);
@@ -2115,8 +2124,18 @@ void gpib_unregister_driver(struct gpib_interface *interface)if (board->use_count >0 )pr_warn ("gpib: Warning: deregistered interface %s in use\n" , interface->name); +/* +* Unlike the IBONL ioctl path, nothing else holds +* big_gpib_mutex here, so a fresh ioctl could race +* this teardown and dispatch into board->interface +* right as it becomes NULL below. Hold the mutex +* across iboffline() and clearing board->interface +* so ibioctl() cannot enter until this is done. +*/ +mutex_lock (&board->big_gpib_mutex);iboffline (board); board->interface =NULL ; +mutex_unlock (&board->big_gpib_mutex); } }for (list_ptr = registered_drivers.next; list_ptr != ®istered_drivers;) {
@@ -2149,6 +2168,8 @@ void init_gpib_board(struct gpib_board *board)init_waitqueue_head (&board->wait);mutex_init (&board->user_mutex);mutex_init (&board->big_gpib_mutex); +atomic_set (&board->io_active,0 ); +init_waitqueue_head (&board->io_drain_wait); board->locking_pid =0 ;spin_lock_init (&board->locking_pid_spinlock);spin_lock_init (&board->spinlock);
drivers/gpib/common/iblib.c +16 -11
@@ -257,22 +257,27 @@ int iboffline(struct gpib_board *board) }/* -* Acquire big_gpib_mutex before calling detach() to prevent a -* use-after-free race. I/O callbacks (read/write/command) hold -* big_gpib_mutex while caching board->private_data on their stack. -* Without this lock, iboffline() can kfree(board->private_data) -* inside detach() while an I/O callback is still running and holds -* a stale pointer to the freed memory. +* iboffline() is always called with board->big_gpib_mutex already +* held (via the IBONL ioctl), so it cannot take that mutex itself. +* That mutex is also dropped by IBRD/IBWRT/IBCMD before they call +* into board->interface, since those calls can block for a long +* time, so it never actually excludes an in-flight read/write/ +* command callback in the first place. * -* Affected board drivers: cb7210, ines_gpib, tnt4882 (all delegate -* to nec7210_read/pio_read which blocks in wait_event_interruptible -* for up to board->usec_timeout microseconds while holding priv). +* board->io_active counts callbacks currently executing inside +* board->interface. Wait here, uninterruptibly and without a +* timeout, for it to reach zero before calling detach(). Since +* big_gpib_mutex is held, no *new* I/O ioctl can start while we +* wait, so this only drains whatever was already in flight. +* Returning early would let detach() free board->private_data +* while that in-flight callback is still dereferencing it, which +* is the use-after-free this is closing. */ -mutex_lock (&board->big_gpib_mutex); +wait_event (board->io_drain_wait,atomic_read (&board->io_active) ==0 ); + board->interface->detach (board);gpib_deallocate_board (board); board->online =0 ; -mutex_unlock (&board->big_gpib_mutex);dev_dbg (board->gpib_dev,"board offline\n" );return 0 ;
drivers/gpib/include/gpib_types.h +9 -0
@@ -288,6 +288,15 @@ struct gpib_board {* store additional variables for this board */ void *private_data; +/* +* Counts read/write/command callbacks currently executing in +* board->interface. iboffline() waits for this to reach zero +* before calling detach(), so its teardown of private_data +* cannot race an in-flight callback still dereferencing it. +*/ +atomic_t io_active; +/* Woken when io_active reaches zero, so iboffline() can wait for drain. */ +wait_queue_head_t io_drain_wait;/* Number of open file descriptors using this board */ unsigned int use_count;/* list of open devices connected to this board */
Signature · 1 line
2.53.0
Greg KH
maintainerRe: [PATCH v2] gpib: fix use-after-free between iboffline() detach and in-flight I/O
Quoted 203 lines · Pavitra Jha
iboffline()callsboard->interface->detach(), which freesboard->private_data, with no guarantee that a read/write/command ioctl already in progress on that board has finished touching it.v1 of this fix took
board->big_gpib_mutexaround thedetach()call. That was wrong on two counts, both pointed out by Dave Penkler:1.
iboffline()is reached fromibioctl()via the IBONL ioctl, which is dispatched whilebig_gpib_mutexis already held. Taking it again insideiboffline()self-deadlocks.2. IBRD/IBWRT/IBCMD explicitly drop
big_gpib_mutexbefore calling intoboard->interface, because those calls can block for the duration ofboard->usec_timeout. So even without the deadlock, the mutex was never actually held during the in-flight callback it was meant to exclude.Fix this by tracking in-flight callbacks directly instead of overloading
big_gpib_mutex. Addboard->io_active, an atomic counter incremented around the body ofread_ioctl(),write_ioctl(), andcommand_ioctl()(which is whereboard->interfaceis dereferenced, via ibrd()/ibwrt()/ibcmd()), andboard->io_drain_wait, a waitqueue woken when the counter reaches zero.
iboffline()waits uninterruptibly onio_drain_waitbefore callingdetach().big_gpib_mutexis already held at that point (via IBONL), which blocks any *new* I/O ioctl from starting, so the wait only drains whatever was already in flight; it does not need a timeout, and adding one would just letdetach()freeprivate_dataout from under a callback that is still running.
gpib_unregister_driver()also callsiboffline(), but unlike the IBONL path it holds no lock at all, so a fresh ioctl could still race the detach()/board->interface=NULL sequence there. Takebig_gpib_mutexaround that call site to close the same window.The KASAN reproducer from v1 (kprobe on a driver read callback, concurrent kfree from a detach kthread) demonstrates the class of bug this closes: a blocking read callback dereferencing freed
private_data. It is reproduced againstboard->io_activerather than the mutex in this version; behavior against the harness is unchanged, since the harness only exercises the missing exclusion betweendetach()and an in-flight read, not the specific mechanism used to provide it.Fixes e6ab504633e4 ("staging: gpib: Destage gpib")Signed-off-by Pavitra Jha <[email protected]>--- drivers/gpib/common/gpib_os.c | 21 +++++++++++++++++++++ drivers/gpib/common/iblib.c | 27 ++++++++++++++++----------- drivers/gpib/include/gpib_types.h | 9 +++++++++ 3 files changed, 46 insertions(+), 11 deletions(-)drivers/gpib/common/gpib_os.c +21
-0@@ -912,6 +912,7 @@ static int read_ioctl(struct gpib_file_private *file_priv, struct gpib_board *bomutex_unlock (&file_priv->descriptors_mutex);atomic_set (&desc->io_in_progress,1 ); +atomic_inc (&board->io_active);/* Read buffer loads till we fill the user supplied buffer */ while (remain >0 && end_flag ==0 ) {@@ -944,6 +945,8 @@ static int read_ioctl(struct gpib_file_private *file_priv, struct gpib_board *bo retval =copy_to_user ((void __user *)arg, &read_cmd,sizeof (read_cmd));atomic_set (&desc->io_in_progress,0 ); +atomic_dec (&board->io_active); +wake_up (&board->io_drain_wait);atomic_dec (&desc->descriptor_busy);wake_up_interruptible (&board->wait);@@ -1003,6 +1006,7 @@ static int command_ioctl(struct gpib_file_private *file_priv,*/ atomic_set (&desc->io_in_progress,1 ); +atomic_inc (&board->io_active); do { fault = copy_from_user(board->buffer, userbuf, (board->buffer_length < remain) ?@@ -1038,6 +1042,8 @@ static int command_ioctl(struct gpib_file_private *file_priv,*/ if (!no_clear_io_in_prog || fault)atomic_set (&desc->io_in_progress,0 ); +atomic_dec (&board->io_active); +wake_up (&board->io_drain_wait);atomic_dec (&desc->descriptor_busy);wake_up_interruptible (&board->wait);@@ -1085,6 +1091,7 @@ static int write_ioctl(struct gpib_file_private *file_priv, struct gpib_board *bmutex_unlock (&file_priv->descriptors_mutex);atomic_set (&desc->io_in_progress,1 ); +atomic_inc (&board->io_active);/* Write buffer loads till we empty the user supplied buffer */ while (remain >0 ) {@@ -1118,6 +1125,8 @@ static int write_ioctl(struct gpib_file_private *file_priv, struct gpib_board *b fault =copy_to_user ((void __user *)arg, &write_cmd,sizeof (write_cmd));atomic_set (&desc->io_in_progress,0 ); +atomic_dec (&board->io_active); +wake_up (&board->io_drain_wait);atomic_dec (&desc->descriptor_busy);wake_up_interruptible (&board->wait);@@ -2115,8 +2124,18 @@ void gpib_unregister_driver(struct gpib_interface *interface)if (board->use_count >0 )pr_warn ("gpib: Warning: deregistered interface %s in use\n" , interface->name); +/* +* Unlike the IBONL ioctl path, nothing else holds +* big_gpib_mutex here, so a fresh ioctl could race +* this teardown and dispatch into board->interface +* right as it becomes NULL below. Hold the mutex +* across iboffline() and clearing board->interface +* so ibioctl() cannot enter until this is done. +*/ +mutex_lock (&board->big_gpib_mutex);iboffline (board); board->interface =NULL ; +mutex_unlock (&board->big_gpib_mutex); } }for (list_ptr = registered_drivers.next; list_ptr != ®istered_drivers;) {@@ -2149,6 +2168,8 @@ void init_gpib_board(struct gpib_board *board)init_waitqueue_head (&board->wait);mutex_init (&board->user_mutex);mutex_init (&board->big_gpib_mutex); +atomic_set (&board->io_active,0 ); +init_waitqueue_head (&board->io_drain_wait); board->locking_pid =0 ;spin_lock_init (&board->locking_pid_spinlock);spin_lock_init (&board->spinlock);drivers/gpib/common/iblib.c +16
-11@@ -257,22 +257,27 @@ int iboffline(struct gpib_board *board) }/* -* Acquire big_gpib_mutex before calling detach() to prevent a -* use-after-free race. I/O callbacks (read/write/command) hold -* big_gpib_mutex while caching board->private_data on their stack. -* Without this lock, iboffline() can kfree(board->private_data) -* inside detach() while an I/O callback is still running and holds -* a stale pointer to the freed memory. +* iboffline() is always called with board->big_gpib_mutex already +* held (via the IBONL ioctl), so it cannot take that mutex itself. +* That mutex is also dropped by IBRD/IBWRT/IBCMD before they call +* into board->interface, since those calls can block for a long +* time, so it never actually excludes an in-flight read/write/ +* command callback in the first place. * -* Affected board drivers: cb7210, ines_gpib, tnt4882 (all delegate -* to nec7210_read/pio_read which blocks in wait_event_interruptible -* for up to board->usec_timeout microseconds while holding priv). +* board->io_active counts callbacks currently executing inside +* board->interface. Wait here, uninterruptibly and without a +* timeout, for it to reach zero before calling detach(). Since +* big_gpib_mutex is held, no *new* I/O ioctl can start while we +* wait, so this only drains whatever was already in flight. +* Returning early would let detach() free board->private_data +* while that in-flight callback is still dereferencing it, which +* is the use-after-free this is closing. */ -mutex_lock (&board->big_gpib_mutex); +wait_event (board->io_drain_wait,atomic_read (&board->io_active) ==0 ); + board->interface->detach (board);gpib_deallocate_board (board); board->online =0 ; -mutex_unlock (&board->big_gpib_mutex);dev_dbg (board->gpib_dev,"board offline\n" );return 0 ;drivers/gpib/include/gpib_types.h +9
-0@@ -288,6 +288,15 @@ struct gpib_board {* store additional variables for this board */ void *private_data; +/* +* Counts read/write/command callbacks currently executing in +* board->interface. iboffline() waits for this to reach zero +* before calling detach(), so its teardown of private_data +* cannot race an in-flight callback still dereferencing it. +*/ +atomic_t io_active; +/* Woken when io_active reaches zero, so iboffline() can wait for drain. */ +wait_queue_head_t io_drain_wait;/* Number of open file descriptors using this board */ unsigned int use_count;/* list of open devices connected to this board */ Signature · 1 line
2.53.0
Hi,
This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.
You are receiving this message because of the following common error(s) as indicated below:
- This looks like a new version of a previously submitted patch, but you did not list below the --- line any changes from the previous version. Please read the section entitled "The canonical patch format" in the kernel file, Documentation/process/submitting-patches.rst for what needs to be done here to properly describe this.
If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.
thanks,
greg k-h's patch email bot