Issue #3481 has been reported by tuxillo.

----------------------------------------
Bug #3481: devfs: fstat() on a device that is being destroyed can panic
http://bugs.dragonflybsd.org/issues/3481

* Author: tuxillo
* Status: New
* Priority: Normal
* Target version: 6.6
* Start date: 2026-11-26
----------------------------------------
As of master efcec8993c, fstat() on an open descriptor of a device that is 
being destroyed can panic the box. I hit it with Xorg running when a USB 
keyboard went away (a KVM switch): the hub and both ukbd devices detached, and 
Xorg panicked in fstat() on one of its input devices.

<pre>
Fatal trap 12: page fault while in kernel mode
fault virtual address   = 0x0
devfs_fo_stat() at devfs_fo_stat+0xd8
kern_fstat() at kern_fstat+0x68
</pre>

devfs_fo_stat() (sys/vfs/devfs/devfs_vnops.c) doesn't lock the vnode. When the 
device is destroyed, devfs_freep() takes the vnode lock exclusive, clears 
vp->v_data and the vnode's device, and then frees the devfs node. 
devfs_vop_getattr() returns EIO when the node is already gone, but the node can 
go after that check and before the st_rdev line, which then reads 
DEVFS_NODE(vp)->d_dev through NULL. In the dump the descriptor's vnode had 
v_data and the device both NULL. A NULL check alone wouldn't be enough: a node 
pointer read without the lock may already have been freed.

The attached patch takes the vnode lock shared in devfs_fo_stat(), as stat(2) 
has it for vn_stat(), so devfs_freep() waits until fstat() is done. It checks 
the node once, uses that pointer, and returns EIO when the node is gone, as 
devfs_vop_getattr() does.

TESTING: on a 12-thread x86_64 desktop running master 64a78aecd3 (devfs hasn't 
changed since), with the attached devfsrace.c, a module that destroys and 
recreates /dev/devfsrace one tick apart, and dfrace.c, which has 8 threads open 
it and call fstat() until it fails. Before the patch the first run of 1000 
cycles panicked as above. After it, 1000 and 5000 cycles ran with 338 million 
fstat() calls and no panic; each thread got EIO about once per destroyed device.

To run it, as root: in kmod/, make SYSDIR=/usr/src/sys and kldload 
./devfsrace.ko; then cc -o dfrace dfrace.c -lpthread and ./dfrace 8 1000.

---Files--------------------------------
Makefile (60 Bytes)
devfsrace.c (2.09 KB)
0001-devfs-hold-the-vnode-lock-in-devfs_fo_stat.patch (3.13 KB)
dfrace.c (2.39 KB)


-- 
You have received this notification because you have either subscribed to it, 
or are involved in it.
To change your notification preferences, please click here: 
http://bugs.dragonflybsd.org/my/account

Reply via email to