Skip to content

System calls require address checks #1329

Open

Description

@patacongo

As a security measure, system calls such as read() or any other system call that modifies memory, must verify that the user-provided addresses lie in user-space. Otherwise, this opens a back door that may be exploited to take control of the OS or to crash the OS.

This applies only to PROTECTED and KERNEL build modes.

Activity

  1. added
    Area: Security
    on Jun 29, 2020
  2. patacongo commented on Jul 3, 2020

    @patacongo
    ContributorAuthor

    Related to Issue #1359

  3. casaroli commented on Oct 19, 2026

    @casaroli
    Contributor

    I reviewed and tested the four open PRs of @royzah that address this issue, and I want to keep the points that concern all of them in one place.

    PR What it does State
    #20412 Helpers that refuse kernel addresses from user space in a kernel build (uaccess_ok(), uaccess_check(), uaccess_fault()), page-table walkers for riscv and x86_64 Open; nothing calls the checks yet, the others use them
    #20418 Copies in the ioctls and calls that carry user pointers, and refuses kernel pointers Draft, on top of #20412
    #20415 Keeps user semaphores in the kernel Draft, on top of #20412
    #20425 Signalling or rescheduling another process needs PR_CAP_ADMIN Draft; independent of #20412 (#20417 is merged)

    All four rebase cleanly on master (c7cba68), build, and pass ostest on qemu-armv8a:knsh and :nsh. My reviews: #20412 (review), #20418 (review), #20415 (review), #20425 (review).

    Merge order. #20412 first: it is the base and it is closest to merge. Then #20418 and #20415, each as one commit on top of it. #20425 can go at any time.

    Open design point. #20418 (and #20415) still generate per-call checks in tools/mksyscall.c, which xiaoxiang asked to keep out of the general stub/proxy creator in the #20412 review. #20425 puts its checks into the functions instead. Can we agree on one place before the next round?

    Gap that none of the four closes yet. The checks accept any address inside the user window, also an address that is not mapped. Then the kernel faults on it at EL1, and on arm64 a kernel fault is a panic. So any program in a kernel build can stop the system:

    read(fd, (void *)0xF0000000, 1); /* in the user window, not mapped */

    On master, and with all four PRs, this panics the kernel (arm64_fatal.c:696). A kernel pointer (0x1000) is refused with #20418, which is good. To close the gap, the kernel must recover from a fault on a user address during a system call: either a fixup table for the user-access code (as copy_from_user() does on Linux), or a rule that a fault at EL1 on a user address in a system call ends the caller instead of the system.

    Small, older point. ARCH_ADDRENV_VEND and ARCH_SHM_VEND are inclusive, but arm64_uservaddr(), riscv_uservaddr(), the armv7-a copy and the new x86_64 up_addrenv_user_vaddr() test vaddr < VEND, so the last byte of each window counts as "not user". It is older than these PRs; a small separate cleanup would fix it.

    I am happy to test new versions.

  4. patacongo commented on Oct 19, 2026

    @patacongo
    ContributorAuthor
  5. casaroli commented on Oct 19, 2026

    @casaroli
    Contributor

    A note on how a fault on a user address fits the current series. #20412 and #20418 check that a user pointer is in the user address window, and end the caller when it is a kernel address. A pointer in the window that has no page passes that check, for example read(fd, (void *)0xf0000000, 1) on qemu-armv8a:knsh. The kernel then faults at EL1 when it touches the pointer, and arm64 panics.

    #20822 is an arm64 change for that case, as a fallback: when the kernel takes a translation, access flag or permission fault at EL1 on a user address during a system call of a user task, the handler ends the caller with _exit(SIGSEGV), the same result that uaccess_fault() gives for a kernel address. Faults on kernel addresses still panic. It touches arch/arm64 (the fault handler and dispatch_syscall()), two labels in libs/libc/machine/arm64/arch_memcpy.S/arch_memset.S, and in two more commits fs/vfs (the stat family and nx_vioctl()), so it does not overlap the files of the series.

    Result on qemu-armv8a:knsh (HVF) with a test program that gives 11 system calls a bad user pointer, each in a forked child (read, write to a pipe, stat, ioctl, nanosleep, clock_gettime, sigprocmask, a read into program text, a read that runs from a mapped page into an unmapped page):

    • master: kernel panic on the first case (common/arm64_fatal.c:696).
    • with the change: each child ends with exit status 11 and NSH continues, 231 of 231 over 21 passes; ostest exits 0 on knsh and nsh.
    • kernel heap: no growth for 20 faulting children of any case (also fstat(), statfs() and fstatfs() with a bad buffer).

    A fault in the arm64 memcpy() or memset() returns from that function, so the system call finishes and releases its locks and references; the caller ends when the system call returns. The stat family now writes the user buffer with one memcpy() after it releases its references, and ioctl(FIONBIO) reads its int before it takes the file reference. FIONBIO is not one of the ioctls that #20418 copies in, so the two do not overlap. The limit: a fault in other code stops the system call in the middle, so a lock or a reference that it holds stays as it is, and the caller gets no EFAULT. A user-access layer with fault fixups (copy_from_user()-style helpers that return -EFAULT) can come later, path by path, starting with the #20418 wrappers. The kill path then stays as the fallback for code that touches user memory directly.

  6. royzah commented on Oct 24, 2026

    @royzah
    Contributor

    Thanks @casaroli for testing these and opening #20822.

    Yeah, the window check isnt enough. Lets keep #20412 as the base, then move the per-call checks out of mksyscall.c in #20418 and check full buffers.

    For #20415, I'll check the IRQ fallback and semaphore overhead first. I'll rebase #20425 now that #20417 landed.

    Your arm64 fallback helps, but we still need safe user copies returning EFAULT with proper cleanup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: Security

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions