pangzhen1xiaomi opened a new pull request, #22124:
URL: https://github.com/apache/nuttx/pull/22124

   ## Summary
   
   * Why: `nx_pthread_create()` is a system call (`syscall/syscall.csv`), so in
     PROTECTED and KERNEL builds user space can call it directly and bypass the
     checks that libc does in `pthread_create()` and
     `pthread_attr_setstacksize()`. The kernel side only asserted the trampoline
     and relied on the attribute setters for the stack size:
     * a NULL trampoline trips `DEBUGASSERT()` and panics the system, or starts
       the new thread at address 0 when assertions are off;
     * a NULL entry point is accepted and the new thread then fails the
       assertion in `pthread_start()`;
     * a stack size below `PTHREAD_STACK_MIN` is accepted and gives a stack that
       can be too small for the TLS data that `tls_init_info()` writes at its
       base. On qemu-armv7a:nsh a 16-byte stack corrupts the heap (see Testing).
   * What: in `nx_pthread_create()`, return `EINVAL` for a NULL trampoline, a
     NULL entry point and a stack size below `PTHREAD_STACK_MIN`. The
     `DEBUGASSERT(trampoline != NULL)` is replaced by the check. Nothing else
     changes: `pthread_create()` and the attribute setters already reject these
     values, so valid callers see no difference.
   * Related: N/A
   
   ## Impact
   
   * Is new feature added? Is existing feature changed? YES: invalid arguments
     of `nx_pthread_create()` now return `EINVAL` instead of asserting, crashing
     or corrupting memory.
   * Impact on user (will user need to adapt to change)? NO. Calls through
     `pthread_create()` with valid attributes behave as before.
   * Impact on build (will build process change)? NO.
   * Impact on hardware (will arch(s) / board(s) / driver(s) change)? NO.
   * Impact on documentation (is update required / provided)? NO.
   * Impact on security (any sort of implications)? YES, positive: user space 
can
     no longer panic the system or corrupt the heap through invalid arguments to
     this system call.
   * Impact on compatibility (backward/forward/interoperability)? NO.
   * Anything else to consider or add? NO.
   
   ## Testing
   
   I confirm that changes are verified on local setup and works as intended:
   * Build Host(s): Ubuntu 22.04.5 LTS, x86_64, host gcc 13.4.0,
     arm-none-eabi-gcc 13.2.1, QEMU 6.2.0.
   * Target(s): sim:nsh, qemu-armv7a:nsh (both with `CONFIG_DEBUG_ASSERTIONS=y`,
     `CONFIG_PTHREAD_STACK_MIN=256`); sim:smp for regression.
   
   A temporary test program calls `nx_pthread_create()` directly, the way a user
   program issues the system call without libc. Each case runs on a fresh boot:
   
   * `ok`: valid trampoline and entry, default attributes
   * `stack`: `attr.stacksize = 16`
   * `entry`: NULL entry point
   * `trampoline`: NULL trampoline
   
   ```c
   static void trampoline(pthread_startroutine_t entry, pthread_addr_t arg)
   {
     pthread_exit(entry(arg));
   }
   
   ret = nx_pthread_create(trampoline, &thread, NULL, thread_entry, NULL); /* 
ok */
   attr.stacksize = 16;
   ret = nx_pthread_create(trampoline, &thread, &attr, thread_entry, NULL); /* 
stack */
   ret = nx_pthread_create(trampoline, &thread, NULL, NULL, NULL);          /* 
entry */
   ret = nx_pthread_create(NULL, &thread, NULL, thread_entry, NULL);        /* 
trampoline */
   ```
   
   Testing logs before change (qemu-armv7a:nsh):
   
   ```
   pthchk ok: ret=0
   pthchk ok: thread ran=1
   
   pthchk stack: ret=12
   dump_assert_info: Assertion failed : at file: mm_heap/mm_malloc_size.c:73 
task: pthchk
   dump_assert_info: Assertion failed : at file: semaphore/sem_wait.c:138 task: 
pthchk
   ...
   
   pthchk entry: ret=0
   dump_assert_info: Assertion failed : at file: pthread/pthread_create.c:144 
task: pthchk
   
   pthchk trampoline: start
   dump_assert_info: Assertion failed : at file: pthread/pthread_create.c:195 
task: pthchk
   ```
   
   On sim:nsh the `entry` and `trampoline` cases fail the same assertions;
   `stack` passes there only because sim adds
   `CONFIG_SIM_STACKSIZE_ADJUSTMENT` (64 KiB) to every stack.
   
   Testing logs after change (same on sim:nsh and qemu-armv7a:nsh):
   
   ```
   pthchk ok: ret=0
   pthchk ok: thread ran=1
   pthchk stack: ret=22
   pthchk entry: ret=22
   pthchk trampoline: ret=22
   ```
   
   `free` after each case on qemu-armv7a:nsh shows the same heap usage as
   the `ok` case.
   
   Regression:
   
   ```
   checkpatch: rc=0
   [sim:nsh] make build: rc=0
   [sim:nsh] ostest: Exiting with status 0
   [sim:smp] make build: rc=0
   [sim:smp] ostest: Exiting with status 0
   [sim:nsh] cmake build: rc=0
   ```
   
   Only tested on sim and QEMU, in the FLAT build where the test program can
   call `nx_pthread_create()` directly; the checks do not depend on the build
   mode.
   
   ## PR verification Self-Check
   
   * [x] This PR introduces only one functional change.
   * [x] I have updated all required description fields above.
   * [x] My PR adheres to Contributing Guidelines and Documentation (git commit 
title and message, coding standard, etc).
   * [ ] My PR is still work in progress (not ready for review).
   * [x] My PR is ready for review and can be safely merged into a codebase.
   


-- 
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]

Reply via email to