Repository navigation
Conversation
linguini1
left a comment
There was a problem hiding this comment.
Test logs required please.
|
commented
Sep 30, 2026
|
@royzah please rebase to fix the issue |
Blank lines after declarations, one declaration per line and case alignment, so the check passes on the files the next commits change. No code changes. Signed-off-by: Royyan Zahir <[email protected]>
uaccess_ok() says whether a range lies in the caller's address environment; uaccess_check() kills the caller when it does not, and uaccess_nested() checks a pointer found inside a user structure. They are how a kernel build refuses a kernel address passed in by a process. Signed-off-by: Royyan Zahir <[email protected]>
uaccess_ok() trusts the user address range, so a kernel mapping inside it would pass as user memory. Refuse such a mapping at boot. Signed-off-by: Royyan Zahir <[email protected]>
uaccess_ok() trusts the user address range, so a global kernel leaf inside it would pass as user memory. Refuse such a mapping. Signed-off-by: Royyan Zahir <[email protected]>
arch.h declares it for an address environment; x86_64 did not have it. The syscall checks need it in every kernel build. Signed-off-by: Royyan Zahir <[email protected]>
risc-v and x86_64 walk the active page tables for up_addrenv_va_to_pa() the way arm64 does. drivers/misc/addrenv.c defined the same symbol from a static table, which cannot see user mappings: in a kernel build it now steps aside and arm64 builds its walker there too. Outside a kernel build nothing changes. Signed-off-by: Royyan Zahir <[email protected]>
Every pointer argument must be user memory. Nested pointers are copied in first, ioctl, fcntl and boardctl arguments the kernel alone can reach fail with EFAULT, and user syslog is formatted in the caller. Signed-off-by: Royyan Zahir <[email protected]>
DIOC_GETPRIV and BIOC_XIPBASE handed kernel addresses to the caller, and CAIOC_REGISTERCB stored a user function the kernel later called. Signed-off-by: Royyan Zahir <[email protected]>
Blank lines after declarations, so the check passes on the files the next commit changes. No code changes. Signed-off-by: Royyan Zahir <[email protected]>
commented
Oct 1, 2026
|
Rebased on master, and the full QEMU |
|
|
||
| void addrenv_drop(FAR struct addrenv_s *addrenv, bool deferred); | ||
|
|
||
| #ifdef CONFIG_BUILD_KERNEL |
There was a problem hiding this comment.
Yeah, protected trusts user ptrs too. There the check is vs the userspace regions, not an addrenv. Follow up PR ok?
| #ifdef CONFIG_BUILD_KERNEL | ||
| bool uaccess_ok(FAR const void *ptr, size_t len); | ||
| bool uaccess_nested(FAR const void *parent, FAR const void *ptr); | ||
| void uaccess_check(FAR const void *ptr, size_t len); |
There was a problem hiding this comment.
please follow linux naming and prototype
There was a problem hiding this comment.
Will do, access_ok(ptr, len). uaccess_check kills the caller, linux gives -EFAULT. Want -EFAULT here too?
| #ifdef CONFIG_BUILD_KERNEL | ||
| if (size > 0 && arm64_overlaps_user(virt, size)) | ||
| { | ||
| PANIC(); |
There was a problem hiding this comment.
why panic kernel, not abort the current process
There was a problem hiding this comment.
No process behind it, only boot tables and arm64_mmu_set_memregion(). Will make set_memregion() return -EINVAL like the misaligned case, assert stays at boot.
| return (FAR void *)pa; | ||
| } | ||
|
|
||
| #ifndef CONFIG_BUILD_KERNEL |
There was a problem hiding this comment.
should skip build this file in kernel mode
There was a problem hiding this comment.
Cant drop the whole file: up_addrenv_pa_to_va() only lives here for arm64, riscv and x86_64 (rpmsg, xhci use it), and x86_64 calls simple_addrenv_initialize(). Only va_to_pa has an MMU walk, so only that steps aside.
| #if defined(CONFIG_BUILD_KERNEL) && !defined(__KERNEL__) | ||
| FAR char *msg; | ||
|
|
||
| if (vasprintf(&msg, fmt, ap) >= 0) |
There was a problem hiding this comment.
Else the kernel formats it, and a %s pointing at kernel memory gets read with kernel rights into the log. In the caller those reads stay at user rights.
|
|
||
| switch (req) | ||
| { | ||
| case BIOC_XIPBASE: |
There was a problem hiding this comment.
Read only yes, but in kernel build the process cant map it. All it gets is the kernel layout, any use faults.
| SYSCALL_LOOKUP(nxsem_set_protocol, 2) | ||
| SYSCALL_LOOKUP(nxsem_get_value, 2) | ||
|
|
||
| #ifdef CONFIG_CUSTOM_SEMAPHORE_MAXVALUE |
There was a problem hiding this comment.
libs/libc/semaphore/sem_getvalue.c, built for flat and KERNEL. User side goes thru the syscall.
| } | ||
|
|
||
| return -EINVAL; | ||
| resolved = nxsem_resolve(sem); |
There was a problem hiding this comment.
this patch will impact the performance a lot in the normal path.
There was a problem hiding this comment.
Flat: nxsem_resolve() compiles to nothing. Protected: right, no shadow there, will keep the syscall kernel build only.
| * Public Function Prototypes | ||
| ****************************************************************************/ | ||
|
|
||
| #if defined(CONFIG_BUILD_KERNEL) && defined(__KERNEL__) |
There was a problem hiding this comment.
drop the first 9 patch from this pr
There was a problem hiding this comment.
Will do once #20412 lands, it needs its va_to_pa on riscv and x86_64. Draft till then.
A semaphore in user-writable memory kept the kernel's wait queue and holder list there, so a process could steer the kernel's list writes, and a timeout expiring in another process wrote through that process's mappings. Such a semaphore now lives in a kernel shadow keyed by its physical address; the user copy only mirrors its value and settings. Reading the value and setting the protocol, ceiling and maximum became syscalls in the kernel build: libc links ahead of the proxies, so they read or wrote the user copy, stale while a waiter blocks and ignored by an existing shadow. Signed-off-by: Royyan Zahir <[email protected]>
left a comment
There was a problem hiding this comment.
This is the hardest of the four, and the shadow design is interesting.
The performance point that xiaoxiang raised is the main thing: in a kernel build every user semaphore and mutex operation now skips the libc fast path and calls nxsem_resolve(), which does a page-table walk (up_addrenv_va_to_pa()), a critical section and a hash lookup on each wait and post. That is a lot on the hot path. Could the shadow be cached, so that the walk runs once and not on each operation?
One correctness question: in interrupt context with no shadow yet, nxsem_resolve() returns the user semaphore, so a timeout post with no waiter would touch user memory from the interrupt handler. Is that path reachable?
As you noted, this wants to be one commit on top of #20412 once that lands.
Test: I rebased on master (c7cba68) and ran qemu-armv8a:knsh and :nsh; ostest exits 0 on both. A semaphore test gives the right values through the shadow: 2 after init, 0 after two waits, 1 after a post, and trywait drains it and then returns EAGAIN.
Note: Please adhere to Contributing Guidelines.
Summary
Depends on #20412; its commits are included until it merges.
In a kernel build a
sem_tin user memory holds the kernel's wait queue and holder list. A process can rewrite them and steer the kernel's list operations, and a timeout firing in another process writes through the first process's mappings.sem_shadow.csem_init()drops a stale shadowmm_pgfree()Impact
Kernel builds: user semaphores run on a kernel shadow. Protected and kernel builds: reading the value and setting protocol, ceiling or maximum become syscalls. Flat builds unchanged.
Testing
Host: Ubuntu 24.04, x86_64. Builds and QEMU runs in
ghcr.io/apache/nuttx/apache-nuttx-ci-linux(QEMU 6.2, Arm GNU GCC 13.2, xPack RISC-V GCC 14.3). Hardware: i.MX93 (Cortex-A55), PX4 kernel build.qemu-armv8a:knshostestpasses, semaphore test included;hellorv-virt:knsh64hello;osteststops afterStarted user_main at PID=6, as master doesBefore the value fix,
ostest's semaphore test hung pollingsem_getvalue()for -2.qemu-armv8a:knsh:hello, thenostestrv-virt:knsh64:hello, thenostest; identical to master'si.MX93, PX4 kernel build, this series applied:
tests isolation, every semaphore through the shadowtools/checkpatch.sh -c -u -m -gclean.