Repository navigation
fs/vfs: Fill stat buffers locally, and read the FIONBIO argument first. - #21468
Merged
Merged
Conversation
stat(), lstat() and fstat() give the stat method of a mount point the caller's buffer as it is, and a file system sets only some of the fields: binfs_stat() sets six of them, and no file system sets st_dev. The other fields keep what the buffer held before the call. These functions, statfs() and fstatfs() also write the caller's buffer field by field while they hold an inode reference, the temporary path buffer of inode_search_setup() or a file reference, and they change the buffer when the call fails. Fill a buffer on the kernel stack instead, and copy it to the caller's buffer with one memcpy() after the references and the path buffer are released. The local buffer is cleared first, so the fields that a file system does not set are 0, and old kernel stack data does not reach the caller. The buffer of the caller is changed only on success. This costs one struct stat or struct statfs on the stack of these five functions. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
nxstyle reports "Missing blank line after declarations" three times in this file. Add the blank lines. No change in function. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
ioctl(fd, FIONBIO, &value) passes a pointer to an int. The driver reads the int, and file_vioctl() reads it again after the driver ioctl, while nx_vioctl() holds a reference to the file. In a kernel build or a protected build the pointer comes from user space, so another thread can change the int between the two reads, and then the driver and f_oflags do not agree. Read the int once in nx_vioctl() before file_get2(), and give the driver and file_vioctl() a pointer to the kernel copy. No user memory is read with the reference held. Kernel callers of file_ioctl() do not change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
casaroli
requested review from
Donny9,
GUIDINGLI,
jerpelea,
pkarashchenko,
pussuw and
xiaoxiang781216
as code owners
November 5, 2026 02:24
|
xiaoxiang781216
approved these changes
Nov 5, 2026
acassis
approved these changes
Nov 5, 2026
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three fixes in the stat and ioctl paths of the VFS. Each stands on its own.
stat(),lstat(),fstat(),statfs()andfstatfs()wrote the caller's buffer field by field while they held an inode reference, the temporary path buffer ofinode_search_setup()or a file reference, and a file system fills only some of the fields (binfs_stat()sets six; no file system setsst_dev). Now these functions fill a buffer on the kernel stack, clear it first, and copy it to the caller's buffer with onememcpy()after the references are released. So an unset field is 0, not what the buffer held before, and the caller's buffer changes only on success.ioctl(fd, FIONBIO, &value)passes a pointer to anint. The driver reads theint, andfile_vioctl()reads it again after the driver ioctl, whilenx_vioctl()holds a reference to the file. Another thread can change theintbetween the two reads, and then the driver andf_oflagsdo not agree. Nownx_vioctl()reads theintonce beforefile_get2()and gives the driver andfile_vioctl()a pointer to the kernel copy, so no user memory is read with the reference held. Kernel callers offile_ioctl()do not change.This is separate from #20418 ("copy in ioctls that carry user pointers"): #20418 copies nested ioctl structures in the system call wrapper; this commit changes only
nx_vioctl(), which #20418 does not touch.Impact
struct statorstruct statfson the stack of the five functions (80 bytes innx_stat()andnx_fstat(), 48 instatfs()andfstatfs(), arm64-fstack-usage).Testing
Test program
statfill.c: the buffer is filled with 0xa5 before each call. After a successful callst_devandst_rdev, which these file systems do not set, must not read 0xa5. After a failed call the buffer must not change.qemu-armv8a:nsh (flat, HVF) with
CONFIG_FS_BINFSandCONFIG_FS_LINKS, master 6b9b216:/bin:stat(),lstat()st_devandst_rdevare 0xa5/bin/statfill:stat(),lstat(),fstat()st_devandst_rdevare 0xa5stat()of a soft link with no target/dev,/dev/zeroqemu-armv8a:knsh (kernel, HVF):
statfill.con v9fs and/dev/zero, andstatleak.c(ten calls of each function after other system calls:st_devandst_rdevstay 0), PASS on master and with this PR. v9fs clears the buffer itself, so these do not show the change.ostest exits with status 0 on qemu-armv8a:knsh, qemu-armv8a:nsh and on the flat configuration above.
No test shows the FIONBIO commit: a test needs a second thread that changes the
intbetween the two reads, at the right time. The change is from reading the code.Builds with
EXTRAFLAGS="-Wno-cpp -Werror", each OK and withfs_stat.ccompiled: qemu-armv8a:knsh, qemu-armv8a:nsh, rv-virt:knsh64, rv-virt:pnsh64, mps2-an500:knsh, lm3s6965-ek:qemu-protected, sim:nsh.tools/checkpatch.sh -c -u -m -gpasses.