Skip to content

fs/vfs: Fill stat buffers locally, and read the FIONBIO argument first. - #21468

Merged
acassis merged 3 commits into
apache:masterfrom
casaroli:vfs-user-buffer-up
Nov 5, 2026
Merged

acassis merged 3 commits into
apache:masterfrom
casaroli:vfs-user-buffer-up

Conversation

@casaroli

@casaroli casaroli commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Three fixes in the stat and ioctl paths of the VFS. Each stands on its own.

  • fs/vfs: Fill a local stat buffer and copy it out at the end. stat(), lstat(), fstat(), statfs() and fstatfs() wrote the caller's buffer field by field while they held an inode reference, the temporary path buffer of inode_search_setup() or a file reference, and a file system fills only some of the fields (binfs_stat() sets six; no file system sets st_dev). Now these functions fill a buffer on the kernel stack, clear it first, and copy it to the caller's buffer with one memcpy() 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.
  • fs/vfs/fs_ioctl.c: Add blank lines after declarations. Whitespace only; the file had three nxstyle errors and checkpatch checks the whole file.
  • fs/vfs: Read the FIONBIO argument before the file reference is taken. 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. Another thread can change the int between the two reads, and then the driver and f_oflags do not agree. Now nx_vioctl() reads the int once before file_get2() and gives the driver and file_vioctl() a pointer to the kernel copy, so no user memory is read with the reference held. Kernel callers of file_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

  • The stat family and FIONBIO behave the same, except that the stat buffer is no longer changed when the call fails, and fields a file system does not set now read 0.
  • Cost: one struct stat or struct statfs on the stack of the five functions (80 bytes in nx_stat() and nx_fstat(), 48 in statfs() and fstatfs(), arm64 -fstack-usage).
  • Generic: flat, protected and kernel builds, every architecture. No ABI change. Not a breaking change.

Testing

Test program statfill.c: the buffer is filled with 0xa5 before each call. After a successful call st_dev and st_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_BINFS and CONFIG_FS_LINKS, master 6b9b216:

Case master this PR
binfs, /bin: stat(), lstat() FAIL: st_dev and st_rdev are 0xa5 PASS
binfs, /bin/statfill: stat(), lstat(), fstat() FAIL: st_dev and st_rdev are 0xa5 PASS
failed stat() of a soft link with no target FAIL: the buffer changes PASS: the buffer does not change
pseudo file system, /dev, /dev/zero PASS PASS

qemu-armv8a:knsh (kernel, HVF): statfill.c on v9fs and /dev/zero, and statleak.c (ten calls of each function after other system calls: st_dev and st_rdev stay 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 int between the two reads, at the right time. The change is from reading the code.

Builds with EXTRAFLAGS="-Wno-cpp -Werror", each OK and with fs_stat.c compiled: 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 -g passes.

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]>
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +130 B (+0.2%, 66,355 B / 262,144 B, total: 25% used)

esp32-devkitc

  • ROM: .flash.text +156 B (+0.1%, 129,240 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +156 B (+0.2%, 95,088 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +92 B (+0.1%, 85,816 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text +148 B (+0.2%, 68,528 B / 131,072 B, total: 52% used)

qemu-armv8a

  • Code: .text.ioctl +116 B, .text.nx_fstat +36 B, .text.nx_stat +36 B (+0.1%, 360,428 B)

qemu-intel64

  • Code: .text +210 B (+0.0%, 8,693,393 B)

rx65n-rsk2mb

  • ROM: .text +112 B (+0.1%, 91,516 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +288 B (+0.1%, 389,360 B)

stm32-nucleo-f103rb

  • flash: .text +68 B (+0.2%, 34,864 B / 131,072 B, total: 27% used)

Catch memory regressions in CI with MemBrowse. Free for open source.

@acassis
acassis merged commit b71c713 into apache:master Nov 5, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: File System File System issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants