Skip to content

arch/arm64: End the caller on a bad user address in a system call. - #20822

Open
casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:uaccess-fault-kill
Open

casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:uaccess-fault-kill

Conversation

@casaroli

@casaroli casaroli commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In a kernel build a system call uses the user pointers that it gets as they are. A pointer can be in the user address window but have no page behind it, for example read(fd, (void *)0xf0000000, 1) on qemu-armv8a:knsh. The kernel then takes a data abort at EL1 when it touches the pointer, and arm64_fatal_handler() panics, because only a fault at EL0 ends a task. So one bad pointer from any process stops the whole system.

The handler treats such an abort as a fault of the caller when all of these are true:

  • the abort is a data abort at EL1 with a valid FAR, and the fault is a translation, access flag or permission fault;
  • the CPU is not in interrupt context;
  • the task is a user task in a system call (TCB_FLAG_SYSCALL), and it is not already exiting;
  • the address environment of the caller is active (no addrenv_select() of another process);
  • FAR is a user address (up_addrenv_user_vaddr()).

Then the caller ends with _exit(SIGSEGV), the same as for a fault at EL0, and the handler prints the usual "Segmentation fault in ..." report with the bad address. There are two ways:

  • The fault is in the arm64 memcpy() or memset(). Both are leaf functions, so the handler returns from the function to its caller, and the system call finishes and releases its locks, file references and memory. If memcpy() faulted on a read of the user source, the handler clears the kernel destination first, so that old kernel data does not go on in place of the user data. When the system call returns, dispatch_syscall() ends the caller. Most drivers and file systems copy the data of read() and write() with these two functions.
  • The fault is in other code. The handler ends the caller at once. It moves the exception frame to the top of the kernel stack, so that _exit() does not run below the frames of the system call, and it clears TCB_FLAG_SYSCALL, so that a second fault in the exit path still panics.

In all other cases the handler panics as before. A fault on a kernel address still panics: that is a kernel bug or a missing pointer check.

Impact

Only arm64 with CONFIG_BUILD_KERNEL for the fault handling. Flat and protected builds, other architectures and faults on kernel addresses do not change: they panic as before.

A process that gives a system call a user address with no page (or a read-only page for a write) now ends with exit status 11, and the system continues. Before, the system stopped.

Limits:

  • In memcpy()/memset() the system call finishes with partial data: a copy from a bad user buffer gives zeros (a faulting pipe write puts zero bytes into the pipe). The caller ends only when the system call returns.
  • In other code the system call does not finish, so its cleanup code does not run. A plain mutex recovers, because nxsem_wait_slow() gives a mutex whose holder has exited to the next caller. A recursive mutex keeps the count of the dead holder, a semaphore used as a lock stays taken, and a file reference or a kernel allocation of the system call is not released. Paths that still touch user memory in C with something held include FIOGCLEX, FIOC_GETLK/FIOC_SETLK, driver ioctl() methods and readdir().
  • mkfifo(), mount() and the new path of rename() first read a user path under the inode tree lock. A bad path there would end the caller with the lock held, and the next caller of that lock would block and could not be killed (see Testing). The lock is an rwsem, so nothing gives it to the next caller. syscall: Read a path from user memory once in a kernel build. #21473 reads these paths before the lock, so this PR depends on it.
  • In other code _exit() runs on the kernel stack of the task, so the frames of the system call are overwritten. An object on that stack that the system call linked into a kernel list is lost.
  • arm64_syscall() copies siginfo_t to the user stack for SYS_signal_handler with the interrupt flag set. A fault there still panics.
  • The caller gets no EFAULT and no real signal. Like a fault at EL0 today, the task ends with _exit(SIGSEGV), so the parent sees exit status 11.

A return of EFAULT on every path needs user-copy helpers with a fault fixup for each access to user memory. NuttX drivers and file systems use user pointers directly, so that is a larger change for later. Until then this change is the fallback for all paths.

Testing

qemu-armv8a:knsh with CONFIG_PIPES=y and CONFIG_SCHED_CHILD_STATUS=y, under HVF, on master 05d9754. A test program forks a child for each case. Each child gives one system call a bad user pointer: read() into an unmapped data address and into an unmapped shared memory address, write() to a pipe, the stat() path and buffer, the ioctl(FIONBIO) argument, nanosleep(), clock_gettime(), sigprocmask(), read() into the program text (a permission fault), and a read() that starts in a mapped shared memory page and runs into the next, unmapped page.

  • master: kernel panic on the first case (Assertion failed panic: at file: common/arm64_fatal.c:696). The system stops.
  • this branch: each child ends with exit status 11, and NSH continues. 11 of 11 cases in one pass, then 220 of 220 in 20 passes.
  • this branch: a child writes from a bad pointer to a pipe (the fault comes in memcpy() with the pipe mutex held). A second child then writes 1 byte to the same pipe: the write returns 1, and the second child ends at once.

Kernel heap growth (free, Kmem used) over 20 faulting children of one case, after one warm-up child:

Case this PR this PR and #21468
read() cases (4), pipe write(), stat() path, nanosleep(), clock_gettime(), sigprocmask() 0 0
stat() buffer +4896 bytes, 18 allocations 0
ioctl(FIONBIO) argument +48 bytes, 1 allocation (one per open file) 0
fstat(), statfs(), fstatfs() buffer not run 0
child calls _exit(11) (control) 0 0
child writes the bad address at EL0 (control) 0 0

The two controls also give 0 on master. Over the whole test program (231 faulting children) Kmem grows by 392 bytes once; a run with 338 children grows by the same 392 bytes.

A prober takes the inode tree lock after each bad path, one boot per case, on 0e8a204 of this PR merged with master 6b9b216 (this commit and the three commits now in #21468):

Call master this PR
mkfifo(), mount() target, rename() new path panic the caller ends, the prober blocks
mkdir(), rmdir(), unlink(), link(), symlink(), shm_open(), shm_unlink(), mq_open(), mq_unlink(), sem_unlink(), rename() old path panic the caller ends, the prober runs
sem_open() the caller ends, the prober runs the caller ends, the prober runs

With #21473 and this commit, the prober runs in all of these cases, and also after a bad name in LOOPIOC_SETUP and MTD_LOOPIOC_SETUP.

ostest: exit status 0 on qemu-armv8a:knsh and qemu-armv8a:nsh. rv-virt:knsh64 (kernel) and lm3s6965-ek:qemu-protected (protected) build.

The three fs/vfs commits of this PR moved to #21468.

@github-actions github-actions Bot added Arch: arm64 Issues related to ARM64 (64-bit) architecture Area: File System File System issues Size: M The size of the change in this PR is medium labels Oct 19, 2026
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

qemu-armv8a

  • Code: .text.arm64_fatal_handler +16 B (+0.0%, 378,200 B)

No memory changes detected for:


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

acassis
acassis previously approved these changes Oct 20, 2026
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

let's ignore ci error which is fixed by #20960 instead wasting ci resource by retrigger.

@casaroli casaroli closed this Oct 26, 2026
@casaroli casaroli reopened this Oct 26, 2026
acassis
acassis previously approved these changes Oct 26, 2026
Comment thread fs/vfs/fs_fstat.c Resolved
Comment thread fs/vfs/fs_stat.c Resolved
* not there if the C versions are in use.
*/

extern const char arm64_memcpy_start[] weak_data;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's about strcpy/strlen

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we find a general solution?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A fault in strlen() or strcpy() ends the caller at once, as in any other kernel code: the system does not panic. A return from them is not safe: memcpy() and memset() return their first argument, which x0 still holds at the fault, but strlen() returns a length that the faulting call did not compute. The general solution is a fault fixup for each access to user memory (copy-in and copy-out helpers that return -EFAULT); NuttX drivers and file systems use user pointers directly, so that needs a change in each of them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xiaoxiang781216 do you want me to try the fix on each of them?

tcb->flags |= TCB_FLAG_FORCED_CANCEL;
if (resume)
{
/* Return from memcpy() or memset() to its caller */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not always exit the current process

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An exit at the fault does not release what the system call holds. The first version of this PR did that: over 231 faulting children the kernel heap grew by 33816 bytes in 110 allocations (the file reference and the 1 KiB pipe buffer of a faulting pipe write, and a file reference for each faulting read). With the return from memcpy() and memset() the system call finishes, and the growth is 0 for these cases.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but exit should release all kernel resource, otherwise how do we handle the user touch a random inaccessible address without memory leak?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exit releases what the task group owns (its file descriptors, memory and threads), so a fault in user mode leaks nothing. A fault inside a system call is different: the call also holds resources that only its own code releases. read() and write() take a file reference with file_get() and drop it with file_put() after the access, and a pipe write holds its buffer. An exit at the fault skips that code, so the file stays allocated after the last close(): these were the 33816 bytes of the first version.

@casaroli
casaroli requested review from xiaoxiang781216 and removed request for Donny9, GUIDINGLI, jerpelea, pkarashchenko and pussuw October 30, 2026 12:58
@casaroli

casaroli commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

This PR is split to make the review easier. Review order:

  1. In either order:
  2. This PR: the arm64 commit.

@casaroli
casaroli marked this pull request as draft November 5, 2026 02:47
@casaroli casaroli changed the title arch/arm64, fs/vfs: End the caller on a bad user address in a system call. arch/arm64: End the caller on a bad user address in a system call. Nov 5, 2026
@github-actions github-actions Bot added Area: OS Components OS Components issues and removed Area: File System File System issues labels Nov 5, 2026
@casaroli
casaroli marked this pull request as ready for review November 17, 2026 07:34
@casaroli
casaroli marked this pull request as draft November 17, 2026 13:04
@casaroli
casaroli marked this pull request as ready for review November 17, 2026 13:04
@casaroli
casaroli marked this pull request as draft November 18, 2026 00:42
@casaroli
casaroli marked this pull request as ready for review November 18, 2026 00:42
@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/44210047903

In a kernel build a system call uses the user pointers that it gets as
they are.  If a pointer is in the user address window but has no page,
for example read(fd, (void *)0xf0000000, 1), the kernel takes a data
abort at EL1, and arm64_fatal_handler() calls PANIC_WITH_REGS().  So any
process can stop the system.

Treat that abort as a fault of the caller when it is a translation,
access flag or permission fault on a user address, taken outside
interrupt context during a system call of a user task that has its own
address environment active.

If the fault is in memcpy() or memset(), return from that function and
let the system call finish, so that it releases its locks, file
references and memory.  If memcpy() faulted on a read of the user
source, clear the kernel destination first, so that old kernel data does
not go on in place of the user data.  When the system call returns,
dispatch_syscall() ends the caller with _exit(SIGSEGV).  Most drivers
and file systems copy the data of read() and write() with these two
functions.

For a fault in other code, end the caller at once with _exit(SIGSEGV),
as for a fault at EL0.  Move the exception frame to the top of the
kernel stack first, so that _exit() does not run below the frames of the
system call.  Clear TCB_FLAG_SYSCALL, so that a second fault in the exit
path still panics.  This system call does not finish, so what it holds
at the fault stays held.

A fault on a kernel address still panics: that is a kernel bug or a
missing check of the pointer.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm64 Issues related to ARM64 (64-bit) architecture Area: OS Components OS Components 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