Skip to content

syscall: Read a path from user memory once in a kernel build. - #21473

Merged
xiaoxiang781216 merged 2 commits into
apache:masterfrom
casaroli:fs-path-before-lock-up
Nov 17, 2026
Merged

xiaoxiang781216 merged 2 commits into
apache:masterfrom
casaroli:fs-path-before-lock-up

Conversation

@casaroli

@casaroli casaroli commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

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:

uintptr_t STUB_mkdir(int nbr, uintptr_t parm1, uintptr_t parm2)
{
#ifdef CONFIG_BUILD_KERNEL
  char _path1[PATH_MAX + 1];

  if (parm1 != 0)
    {
      memccpy(_path1, (FAR const char *)parm1, '\0', PATH_MAX);
      _path1[PATH_MAX] = '\0';
      parm1 = (uintptr_t)_path1;
    }
#endif

  return (uintptr_t)mkdir((FAR const char *)parm1, (mode_t)parm2);
}
  • syscall.csv marks each path parameter with |path after its type, for example "FAR const char *|path". tools/mksyscall generates the copy. Documentation/os/syscall.rst describes the mark.
  • tools/mksyscall.c keeps no list of system calls. |path is a type attribute in syscall.csv, like the formal|actual|fieldname form, 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, as strncpy() does.
  • All later reads in the kernel, with or without a lock, read the copy, so no file system code changes.
  • LOOPIOC_SETUP, LOOPIOC_TEARDOWN, MTD_LOOPIOC_SETUP and MTD_LOOPIOC_TEARDOWN have 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_MAX bytes or more still gives ENAMETOOLONG.

Flash, image text: +1,024 bytes on qemu-armv8a:knsh, +708 on rv-virt:knsh64. Same text as master on mps2-an500:knsh, rv-virt:pnsh64 and fvp-armv8r:pnsh.

Kernel stack: +PATH_MAX + 1 bytes for each path, two paths in rename(), link(), symlink(), mount() and the two loop setup ioctls. The most used kernel stack in ostest and pathops.c on rv-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:

  • The path in the argument of an ioctl() other than the four above is not covered, for example the file name of SMART_LOOPIOC_SETUP.
  • The argv and envp strings and the file actions of execve() and posix_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/zer and o; 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:

Call master This PR
LOOPIOC_SETUP file name the second process blocks after 1 child runs
MTD_LOOPIOC_SETUP file name blocks after 23 children runs
LOOPIOC_TEARDOWN device name blocks after 9 children runs
MTD_LOOPIOC_TEARDOWN device name blocks after 11 children runs
mkfifo(), open(), stat(), mkdir(), rename(), unlink() see the table below runs

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.c with 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_LOOP and CONFIG_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; with CONFIG_DEV_LOOP and CONFIG_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 and a, 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:

Call Reads the path before the lock only (the first version of this PR) This PR
mkfifo() the second process blocks after 2 children runs
mkdir() blocks after 2 children runs
rename() new path blocks after 6 children runs
open(), stat(), unlink() runs runs

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.

Call master This PR
mkfifo(), bad and cross the second process blocks runs
mount() target, bad and cross the second process blocks runs
rename() new path, cross the second process blocks runs
LOOPIOC_SETUP device name, bad the second process blocks runs
MTD_LOOPIOC_SETUP device name, bad the second process blocks runs
mkdir(), rmdir(), unlink(), link(), symlink(), shm_open(), shm_unlink(), mq_open(), mq_unlink(), rename() old path, bad runs runs

In each row the child ends with exit status 11. Not run: the named semaphore calls and the two loop teardown ioctls.

Test program pathops.c with good paths: mkfifo(), mount() and rename() 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, ostest and pathops.c:

Kernel stack master This PR
3,072 (the default of the config) not run again status 0, no assertion, 3 of 3
2,816 not run status 0, no assertion, 1 of 1
2,560 status 0, no assertion, 2 of 2 status 0, 2 of 2; one run asserted in mm_free.c after ostest ended
2,048 fails not run

Builds 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 -g passes.

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


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

Comment thread drivers/pipes/fifo.c Resolved
@casaroli casaroli changed the title fs: Read a user path before the inode tree lock is taken. syscall: Read a path from user memory once in a kernel build. Nov 6, 2026
@casaroli
casaroli force-pushed the fs-path-before-lock-up branch from bd290b1 to 2f26302 Compare November 6, 2026 10:35
@github-actions github-actions Bot added Size: M The size of the change in this PR is medium and removed Size: S The size of the change in this PR is small labels Nov 6, 2026
@casaroli
casaroli force-pushed the fs-path-before-lock-up branch from 2f26302 to 2816e7a Compare November 9, 2026 08:38
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
casaroli force-pushed the fs-path-before-lock-up branch from 2816e7a to 2c0b428 Compare November 11, 2026 13:19
@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/43028139165

Comment thread syscall/syscall.csv Resolved
@casaroli
casaroli force-pushed the fs-path-before-lock-up branch from 2c0b428 to f1ba3ac Compare November 15, 2026 14:42
Comment thread tools/mksyscall.c Resolved
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
casaroli force-pushed the fs-path-before-lock-up branch from f1ba3ac to e77505f Compare November 15, 2026 22:37
@casaroli
casaroli marked this pull request as draft November 16, 2026 15:13
@casaroli
casaroli marked this pull request as ready for review November 16, 2026 15:13
@xiaoxiang781216
xiaoxiang781216 merged commit 1aedf86 into apache:master Nov 17, 2026
85 of 91 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants