Repository navigation
syscall: Read a path from user memory once in a kernel build. - #21473
Merged
Merged
Conversation
casaroli
requested review from
Donny9,
gustavonihei,
jerpelea,
pkarashchenko and
xiaoxiang781216
as code owners
November 5, 2026 03:46
|
casaroli
force-pushed
the
fs-path-before-lock-up
branch
from
November 6, 2026 10:35
bd290b1 to
2f26302
Compare
casaroli
force-pushed
the
fs-path-before-lock-up
branch
from
November 9, 2026 08:38
2f26302 to
2816e7a
Compare
Only white space and one wrapped line. The next commit changes this file. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
casaroli
force-pushed
the
fs-path-before-lock-up
branch
from
November 11, 2026 13:19
2816e7a to
2c0b428
Compare
🔗 Cross-repo PR dependenciesThe read-only Build run reported the following dependent PR(s) and fetched head SHA(s): |
casaroli
force-pushed
the
fs-path-before-lock-up
branch
from
November 15, 2026 14:42
2c0b428 to
f1ba3ac
Compare
A system call gives the kernel the pointer to a path in user memory, and the kernel reads it many times, also with the inode tree lock held. A bad pointer, or a thread that changes or unmaps the path during the call, then faults with the lock held. When a fault ends the caller, the lock stays held, and every later user of the inode tree blocks. In a kernel build, copy the path to the kernel stack in the system call stub, where no lock is held, and call the function with the copy. A "|path" after the type in syscall.csv marks these parameters. The kernel reads user memory one time, and the reads with the lock held read the copy. The loop ioctls get the name of the device, and the setup ioctls also the name of the file, in their argument, so they make the same copy. Flat and protected builds do not change: a fault there stops the system. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
casaroli
force-pushed
the
fs-path-before-lock-up
branch
from
November 15, 2026 22:37
f1ba3ac to
e77505f
Compare
xiaoxiang781216
approved these changes
Nov 16, 2026
casaroli
marked this pull request as draft
November 16, 2026 15:13
casaroli
marked this pull request as ready for review
November 16, 2026 15:13
acassis
approved these changes
Nov 16, 2026
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
A system call gives the kernel a pointer to a path in user memory, and the kernel reads that path many times, also with the inode tree lock held. A bad pointer, or another thread that changes or unmaps the path during the call, then faults with the lock held. If that fault ends the caller, the lock stays held, and every later user of the inode tree blocks. The lock is an rwsem, and nothing gives it to the next task.
This PR reads the path from user memory once, before any lock. In a kernel build the system call stub copies the path to the kernel stack and calls the function with the copy:
syscall.csvmarks each path parameter with|pathafter its type, for example"FAR const char *|path".tools/mksyscallgenerates the copy.Documentation/os/syscall.rstdescribes the mark.tools/mksyscall.ckeeps no list of system calls.|pathis a type attribute insyscall.csv, like theformal|actual|fieldnameform, and each row sets it for its own parameters (31 rows, 35 parameters). This is not a table of system call names in the stub generator, as the one that was removed from syscall: refuse kernel addresses from user space in a kernel build #20412 in review (syscall: refuse kernel addresses from user space in a kernel build #20412 (comment)).memccpy()reads the path only to its end and does not fill the rest of the buffer, asstrncpy()does.LOOPIOC_SETUP,LOOPIOC_TEARDOWN,MTD_LOOPIOC_SETUPandMTD_LOOPIOC_TEARDOWNhave the device name inside their argument, and the two setup ioctls also the file name, so they make the same copy, with one small function in each driver.The first commit only fixes older nxstyle errors in
tools/mksyscall.c, because checkpatch checks the whole file.Impact
On master alone this PR shows no change that a program can see. On master a bad user pointer in a system call panics with or without this PR. The change takes effect with a change that ends the caller on such a fault, such as #20822. With #20822 and without this PR, one bad path to
mkfifo()blocks the file system for all processes.Kernel builds only. Flat builds have no stubs. Protected builds generate the same code as before: a fault there stops the system. No ABI change. Not a breaking change.
Good paths do not change. A path of
PATH_MAXbytes or more still givesENAMETOOLONG.Flash, image text: +1,024 bytes on
qemu-armv8a:knsh, +708 onrv-virt:knsh64. Same text as master onmps2-an500:knsh,rv-virt:pnsh64andfvp-armv8r:pnsh.Kernel stack: +
PATH_MAX + 1bytes for each path, two paths inrename(),link(),symlink(),mount()and the two loop setup ioctls. The most used kernel stack inostestandpathops.conrv-virt:knsh64, with a test-only paint of the stack: 2,400 of 3,072 bytes on master, 2,544 of 3,072 with this PR.Limits:
ioctl()other than the four above is not covered, for example the file name ofSMART_LOOPIOC_SETUP.argvandenvpstrings and the file actions ofexecve()andposix_spawn()need no change:exec_module()copies them to kernel memory, with no lock held.Testing
All on qemu-armv8a:knsh (QEMU, HVF), with the arm64 commit of #20822.
Results of this head. Test program
pathrace.c(see below), SMP with 4 CPUs, with new loop cases. A lookup reads a name to its end only when the name matches an inode. So the loop cases flip the byte after the name of an inode: for teardown/dev/zerando; for setup/dev/lz(a loop device that the test makes) and/, because the kernel first stats a copy of the file name and then opens it with the lock held. 60 children per call:LOOPIOC_SETUPfile nameMTD_LOOPIOC_SETUPfile nameLOOPIOC_TEARDOWNdevice nameMTD_LOOPIOC_TEARDOWNdevice namemkfifo(),open(),stat(),mkdir(),rename(),unlink()uaccess-locks2.c(see below) on this head, SMP with 4 CPUs: all 21 cases run, also the new ones (a file name and a teardown device name that run off the page, for both drivers). Each child ends with exit status 11.Test program
loopops.cwith good and wrong names, for both drivers: setup, stat of the device, setup again, teardown, teardown again, no such file, NULL names, names of about 200 bytes. 31 checks PASS, and the output is the same as master.ostest exits with status 0 on qemu-armv8a:knsh and qemu-armv8a:nsh, also with
CONFIG_DEV_LOOPandCONFIG_MTD_LOOP. The apps are before the close-during-poll test of ostest, which needs #21490.Builds with
EXTRAFLAGS="-Wno-cpp -Werror", each OK: qemu-armv8a:knsh, rv-virt:knsh64, mps2-an500:knsh; withCONFIG_DEV_LOOPandCONFIG_MTD_LOOP: qemu-armv8a:knsh (kernel), rv-virt:pnsh64 (protected), qemu-armv8a:nsh (flat).The results below are from the previous head, which used
strncpy()and did not copy the file name of the loop setup ioctls:Test program
pathrace.c, SMP with 4 CPUs, one boot per call. A thread flips the last byte of a path between 0 anda, and the next page is not mapped, so the kernel can see the end of the string on one read and run off the page on the next. Each child ends with a fault. A second process then takes the inode tree lock. 60 children per call:mkfifo()mkdir()rename()new pathopen(),stat(),unlink()Test program
uaccess-locks2.c, SMP off, one boot per case. A child gives the call a bad path, and a second process then takes the lock. "Cross" is a path that runs off the end of a mapped page into an unmapped page.mkfifo(), bad and crossmount()target, bad and crossrename()new path, crossLOOPIOC_SETUPdevice name, badMTD_LOOPIOC_SETUPdevice name, badmkdir(),rmdir(),unlink(),link(),symlink(),shm_open(),shm_unlink(),mq_open(),mq_unlink(),rename()old path, badIn each row the child ends with exit status 11. Not run: the named semaphore calls and the two loop teardown ioctls.
Test program
pathops.cwith good paths:mkfifo(),mount()andrename()with new names, existing names, relative paths and paths of about 200 bytes;rename()across directories, over an existing target, into a directory, onto itself and below itself, in the pseudo file system and on tmpfs; and the expected errors. 36 checks PASS, and the output is the same as master on qemu-armv8a:knsh. They also PASS on rv-virt:knsh64 (QEMU, TCG).ostest exits with status 0 on qemu-armv8a:knsh, qemu-armv8a:nsh and rv-virt:knsh64.
Kernel stack margin on rv-virt:knsh64,
ostestandpathops.c:mm_free.cafterostestendedBuilds with
EXTRAFLAGS="-Wno-cpp -Werror", each OK: qemu-armv8a:knsh, rv-virt:knsh64, rv-virt:knsh, qemu-armv7a:knsh (kernel); mps2-an500:knsh, rv-virt:pnsh64, fvp-armv8r:pnsh, lm3s6965-ek:qemu-protected (protected); qemu-armv8a:nsh, rv-virt:nsh (flat). CMake, qemu-armv8a:knsh: OK, and the stub has the copy.Not built: x86_64 and Xtensa kernel builds. Not run on hardware.
tools/checkpatch.sh -c -u -m -gpasses.