Issue #3494 has been reported by tuxillo.

----------------------------------------
Bug #3494: kernel: a failed fchdir() leaks the open file and keeps its mount 
busy
http://bugs.dragonflybsd.org/issues/3494

* Author: tuxillo
* Status: New
* Priority: Normal
* Target version: 6.6
* Start date: 2026-12-04
----------------------------------------
As of master e00a1f8398, every fchdir(2) that fails with ENOTDIR or EACCES 
leaks the file behind the descriptor. Any user can do it: fchdir() on a regular 
file, or on a directory they can read but not search.

sys_fchdir() holds the file with holdvnode(), but when checkvp_chdir() fails it 
jumps to the done label, which comes after the fdrop(), so the reference is 
never given back. The label moved there in 3919ced024 (2009), which replaced 
"fdrop(fp); return (error);" with "goto done".

Each leaked file keeps its vnode, namecache entry and a mount reference, so the 
file system can't be unmounted (EBUSY, and shutdown has to force it). The files 
also stay charged to the user who opened them: after kern.maxfilesperuser 
failures, that user gets ENFILE until reboot. From the code, find(1) triggers 
it through fts, which fchdir()s into each directory it reads, so the weekly 
locate update, run as nobody, would leak a file for every directory nobody can 
read but not search. I haven't tested that.

The attached patch moves the label above the fdrop().

I tested it in an 8-CPU QEMU/NVMM guest with the attached fchdirleak.c, run as 
nobody on a scratch tmpfs. On master 175de3bdf7 (sys_fchdir() hasn't changed 
since), 1000 failed calls each with ENOTDIR and EACCES left 2000 open files 
behind, the tmpfs couldn't be unmounted, and after 63096 failed calls nobody 
couldn't start a program. With the patch nothing leaked, the tmpfs unmounted, 
and 260224 failed calls went through. Successful calls leak nothing either way.

---Files--------------------------------
0001-kernel-give-back-the-file-when-fchdir-2-fails.patch (1.11 KB)
fchdirleak.c (4.16 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