casaroli opened a new pull request, #21615: URL: https://github.com/apache/nuttx/pull/21615
## Summary Three fixes in the semaphore wait code. Each one makes the kernel use the wrong memory for the wait list or the count of a semaphore: in a kernel build a user program can stop the kernel with an ordinary pthread mutex or semaphore. One commit for each fix, and one whitespace commit: - **sched/semaphore: Do not use a semaphore after its kernel mapping is gone.** With `CONFIG_MM_KMAP`, `nxsem_wait_slow()` maps a semaphore in user memory into the kernel while the thread waits. A semaphore that crosses a page boundary gets a temporary mapping, and `kmm_unmap()` removes it after the wait. The code for a mutex then used the same pointer to read the wait list and to set the holder: the kernel faulted. The fix keeps the address of the caller and uses it after the unmap. - **sched/semaphore: Add a blank line after a declaration in sem_recover.c.** Whitespace only: an older nxstyle error in the file that the next commit changes. - **sched/semaphore: Do not remove a killed waiter from the wait list twice.** `nxtask_terminate()` removes a task from its list with `nxsched_remove_readytorun()` and then restores `task_state`, so `nxsem_recover()` sees `TSTATE_WAIT_SEM` and calls `dq_rem()` again. The links of the task are NULL after the first removal, so the second `dq_rem()` sets the head and the tail of the wait list to NULL: the other waiters never wake, and the next waiter that is killed or times out hits the assertion in `nxsem_recover()` or `nxsem_wait_irq()`. `task_restart()` has the same problem in the opposite order. `nxsem_recover()` now calls `dq_rem()` only if the task is still in the list (`dq_inqueue()`), and `nxtask_reset_task()` does not call `nxsched_remove_readytorun()` for a task that waits on a semaphore. - **sched/semaphore: Use the memory of the waiter in a signal or a timeout.** Without `CONFIG_MM_KMAP`, the wait keeps the user address of the semaphore. `nxsem_wait_irq()` runs in the address environment of its caller: the thread of another process that sends a signal, or a timer interrupt that comes while another process runs. After `fork()` both processes have the semaphore at the same address, so the kernel changed the wait list and the count of the semaphore of the other process. `nxsem_waiter_select()` and `nxsem_waiter_restore()` select the address environment of the waiter around the access in `nxsem_wait_irq()` and in `nxsched_set_priority()` of a waiter (it moves the waiter in the wait list). They do nothing for a semaphore that is not at a user address, or when the caller already uses the address environment of the waiter. ## Impact - Kernel builds: a pthread mutex across a page boundary (`CONFIG_MM_KMAP`), a kill or cancel of one of several waiters, and a signal, a timeout or `sched_setparam()` of a waiter in another process (without `CONFIG_MM_KMAP`) no longer fault, assert or change the memory of another process. - All build modes: other waiters of a semaphore or mutex now wake after one waiter is killed, cancelled or restarted. - Flat and protected builds have no address environments: the first fix does not change their code, and in the last fix `nxsem_waiter_select()` and `nxsem_waiter_restore()` are empty inline functions. Text size with optimization: mps2-an500:nsh (`-Os`) the same as without this commit; qemu-armv8a:nsh (`DEBUG_FULLOPT`) +16 bytes (one `if` became two). The `CONFIG_MM_KMAP` code changes only in the first fix. - No header, macro, function result or errno changes. No documentation change: Documentation/ does not describe these paths. ## Testing Test programs (POSIX, one line per case; the last two need `fork()`, so a kernel build): - `restartsem.c` (flat and protected: `task_restart()` is not in a kernel build): a task that waits on a semaphore or a mutex is restarted while another task waits on the same object; both must wake. - `semcross.c`: three threads contend for a pthread mutex at three offsets across a page boundary; then a thread waits on a semaphore across a page boundary and gets a signal, and a wait on it times out. - `killmxn.c`: a child process with 3 threads on one mutex is killed with SIGTERM and SIGKILL, with the threads busy or blocked, 8 rounds each; one of 4 waiters on a mutex is cancelled; the threads of a process that wait on a process-shared mutex or semaphore are killed, and two other processes that wait on it must still get it. - `semwaitenv.c`: after `fork()`, the sender and the waiter have the semaphore at the same address. A `sem_timedwait()` and a `pthread_mutex_timedlock()` time out while the other process runs; `sched_setparam()` of a waiter from the other process; a signal with a handler to a waiter on a semaphore (`EINTR`); SIGKILL to a waiter on a semaphore. The copy of the sender must not change. | Configuration | Test | master | this PR | |---|---|---|---| | qemu-armv8a:nsh (flat, arm64, HVF) | restartsem | the other task never wakes (sem and mutex) | pass; ostest status 0 | | mps2-an500:knsh (protected, Cortex-M7, QEMU) | restartsem | the other task never wakes (sem and mutex) | pass | | qemu-armv8a:knsh (kernel, `MM_KMAP`, arm64, HVF) | semcross | data abort in the kmap area (FAR 0x7f001000), panic in the third mutex case | 5 of 5 pass | | qemu-armv8a:knsh (kernel, `MM_KMAP`) | killmxn | assertion at sem_recover.c:116 in the first round | all cases pass | | qemu-armv8a:knsh (kernel, `MM_KMAP`) | semwaitenv | - | 5 of 5 pass; ostest status 0 | | qemu-armv8a:knsh without `MM_KMAP` | semwaitenv, each case in its own boot | timeout of `sem_timedwait()`: assertion at sem_waitirq.c:82; signal with a handler: the same; SIGKILL: the same; `sched_setparam()`: the copy of the semaphore in the sender changes; `pthread_mutex_timedlock()` timeout: pass | 5 of 5 pass | | qemu-armv8a:knsh without `MM_KMAP` | semcross, killmxn | - | 5 of 5 pass; all cases pass; ostest status 0 (knsh and nsh) | | rv-virt:knsh64 (kernel, no `MM_KMAP` in the defconfig, riscv64, QEMU TCG) | semwaitenv | assertion at sem_waitirq.c:82 in the idle task (timer interrupt), first case | 5 of 5 pass; ostest status 0 | The knsh and rv-virt runs add `PIPES` (the test uses a pipe); rv-virt:knsh64 adds `SIG_DEFAULT` for the SIGKILL case. The master runs were on 02ba3229954, d215909fd9b and c8c0661c997 (no commit between them changes sched/ or mm/); the runs of this PR on the same masters (the rebase did not change the patches). Build only, with `-Werror`: qemu-armv7a:knsh and qemu-armv7a:knsh_smp (armv7-a, no `MM_KMAP`), rv-virt:knsh (riscv32, `MM_KMAP`); arm-none-eabi-gcc 15.3, riscv-none-elf-gcc 14.3. No test in nuttx-apps covers these cases. Not run: x86_64 and xtensa kernel builds, SMP kernel builds at run time, CMake builds, sim. `tools/checkpatch.sh -c -u -m -g` passes. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
