Repository navigation
Conversation
|
7c6b31f to
0e8a204
Compare
|
let's ignore ci error which is fixed by #20960 instead wasting ci resource by retrigger. |
| * not there if the C versions are in use. | ||
| */ | ||
|
|
||
| extern const char arm64_memcpy_start[] weak_data; |
There was a problem hiding this comment.
what's about strcpy/strlen
There was a problem hiding this comment.
can we find a general solution?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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 */ |
There was a problem hiding this comment.
why not always exit the current process
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
but exit should release all kernel resource, otherwise how do we handle the user touch a random inaccessible address without memory leak?
There was a problem hiding this comment.
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.
|
This PR is split to make the review easier. Review order:
|
0e8a204 to
dfc02e9
Compare
🔗 Cross-repo PR dependenciesThe read-only Build run reported the following dependent PR(s) and fetched head SHA(s): |
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]>
dfc02e9 to
b411029
Compare
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, andarm64_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:
TCB_FLAG_SYSCALL), and it is not already exiting;addrenv_select()of another process);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:memcpy()ormemset(). 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. Ifmemcpy()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 ofread()andwrite()with these two functions._exit()does not run below the frames of the system call, and it clearsTCB_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_KERNELfor 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:
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.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 includeFIOGCLEX,FIOC_GETLK/FIOC_SETLK, driverioctl()methods andreaddir().mkfifo(),mount()and the new path ofrename()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._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()copiessiginfo_tto the user stack forSYS_signal_handlerwith the interrupt flag set. A fault there still panics.EFAULTand 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
EFAULTon 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=yandCONFIG_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, thestat()path and buffer, theioctl(FIONBIO)argument,nanosleep(),clock_gettime(),sigprocmask(),read()into the program text (a permission fault), and aread()that starts in a mapped shared memory page and runs into the next, unmapped page.Assertion failed panic: at file: common/arm64_fatal.c:696). The system stops.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:read()cases (4), pipewrite(),stat()path,nanosleep(),clock_gettime(),sigprocmask()stat()bufferioctl(FIONBIO)argumentfstat(),statfs(),fstatfs()buffer_exit(11)(control)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):
mkfifo(),mount()target,rename()new pathmkdir(),rmdir(),unlink(),link(),symlink(),shm_open(),shm_unlink(),mq_open(),mq_unlink(),sem_unlink(),rename()old pathsem_open()With #21473 and this commit, the prober runs in all of these cases, and also after a bad name in
LOOPIOC_SETUPandMTD_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/vfscommits of this PR moved to #21468.