nuttx/sched/task
Junbo Zheng ef37425f71 sched: fix 1-byte overflow in prctl(PR_GET_NAME)
strlcpy() was given sizeof(tcb->name), i.e. CONFIG_TASK_NAME_SIZE + 1,
but the documented caller contract is a buffer of CONFIG_TASK_NAME_SIZE
bytes (include/sys/prctl.h). When a task name is exactly
CONFIG_TASK_NAME_SIZE chars (the normal result of nxtask_setup_name()
truncation), the terminating NUL lands one byte past the caller buffer.
Pass CONFIG_TASK_NAME_SIZE to strlcpy() so the copy is truncated
in-bounds, and drop the stale forced-NUL line left over from the strncpy
era (it ran after the overflow had already happened).

Before:
```
guard byte placed right after a CONFIG_TASK_NAME_SIZE caller buffer
reads 0x00 (expected 0xAA) after the call: strlcpy writes its
terminating NUL one byte past the buffer when the task name is exactly
CONFIG_TASK_NAME_SIZE chars.
```

After:
```
strlcpy(name, tcb->name, CONFIG_TASK_NAME_SIZE) writes at most
CONFIG_TASK_NAME_SIZE bytes; the caller buffer stays intact.
```

Testing:

Simulated (sim:nsh, CONFIG_TASK_NAME_SIZE=31).

Build and run:
```
cmake -B build -DBOARD_CONFIG=sim:nsh -GNinja
cmake --build build -j$(nproc)
echo hello | ./build/nuttx
```
then run "hello" at the NSH prompt.

The test was carried by apps/examples/hello/hello_main.c (scratch only,
not part of this commit); its diff:

```
--- a/examples/hello/hello_main.c
+++ b/examples/hello/hello_main.c
@@ -24,6 +24,8 @@

 #include <nuttx/config.h>
 #include <stdio.h>
+#include <string.h>
+#include <sys/prctl.h>

 /****************************************************************************
  * Public Functions
@@ -35,6 +37,55 @@

 int main(int argc, FAR char *argv[])
 {
+  /* Longest-legal task name: exactly CONFIG_TASK_NAME_SIZE chars, the
+   * normal result of nxtask_setup_name() truncation.
+   */
+
+  static const char longname[] =
+    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+
+  /* Caller buffer per the documented prctl(PR_GET_NAME) contract, with a
+   * guard byte immediately after it to detect the 1-byte overflow.
+   */
+
+  struct
+    {
+      char buf[CONFIG_TASK_NAME_SIZE];
+      volatile unsigned char guard;
+    } s;
+
+  _Static_assert(sizeof(longname) - 1 > CONFIG_TASK_NAME_SIZE,
+                 "test name must exceed CONFIG_TASK_NAME_SIZE");
+
   printf("Hello, World!!\n");
+  printf("prctl test: CONFIG_TASK_NAME_SIZE=%d\n", CONFIG_TASK_NAME_SIZE);
+
+  s.guard = 0xaa;
+  s.buf[0] = '\0';
+
+  if (prctl(PR_SET_NAME, (unsigned long)longname) != 0)
+    {
+      printf("prctl test: PR_SET_NAME failed\n");
+      return 1;
+    }
+
+  if (prctl(PR_GET_NAME, (unsigned long)s.buf) != 0)
+    {
+      printf("prctl test: PR_GET_NAME failed\n");
+      return 1;
+    }
+
+  printf("prctl test: guard=0x%02x (expected 0xaa), name len=%zu, "
+         "last char=0x%02x\n",
         s.guard, strlen(s.buf), (unsigned char)s.buf[strlen(s.buf)]);
+
+  if (s.guard != 0xaa)
+    {
+      printf("prctl test: FAIL - terminating NUL written 1 byte past "
+             "the caller buffer\n");
+      return 1;
+    }
+
+  printf("prctl test: PASS - caller buffer intact\n");
   return 0;
 }
```

Before the fix:
```
prctl test: guard=0x00 (expected 0xaa), name len=30, last char=0x00
prctl test: FAIL - terminating NUL written 1 byte past the caller buffer
```

After the fix:
```
prctl test: guard=0xaa (expected 0xaa), name len=30, last char=0x00
prctl test: PASS - caller buffer intact
```

Assisted-by: Claude Code (GLM-5.3) <claude@anthropic.com>
Signed-off-by: Junbo Zheng <zhengjunbo1@xiaomi.com>
2026-09-14 18:51:55 -03:00
..
CMakeLists.txt !sched/arch/libc: Give fork() and vfork() their real, separate semantics. 2026-08-10 08:57:30 -03:00
exit.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
Make.defs !sched/arch/libc: Give fork() and vfork() their real, separate semantics. 2026-08-10 08:57:30 -03:00
spawn.h sched: migrate to SPDX identifier 2024-09-12 01:10:14 +08:00
task.h sched:use tcb_s inside of task_tcb_s , remove all cast 2025-12-22 17:55:45 +08:00
task_activate.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
task_argvstr.c sched/tls: fix uninitialized argv pointer in task info 2026-01-15 16:10:11 -03:00
task_cancelpt.c sched/cancelpt: Fix MISRA C 2012 Rule 10.4 violations 2026-02-02 21:09:40 +08:00
task_create.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
task_delete.c sched: migrate to SPDX identifier 2024-09-12 01:10:14 +08:00
task_execve.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
task_exit.c !sched/arch/libc: Give fork() and vfork() their real, separate semantics. 2026-08-10 08:57:30 -03:00
task_exithook.c arch, sched/signal: Fix compilation with ENABLE_PARTIAL_SIGNALS=y 2026-06-16 17:07:32 +08:00
task_fork.c !sched/arch/libc: Give fork() and vfork() their real, separate semantics. 2026-08-10 08:57:30 -03:00
task_getgroup.c sched: migrate to SPDX identifier 2024-09-12 01:10:14 +08:00
task_getpid.c task_getpid.c:coverity HIS_metric_violation: RETURN 2026-01-21 20:31:35 +08:00
task_getppid.c sched: migrate to SPDX identifier 2024-09-12 01:10:14 +08:00
task_gettid.c sched/gettid: Move thread ID to TLS 2025-04-08 22:53:10 +08:00
task_init.c sched/pthread: move pthread mutex from syscall to user-space 2026-01-22 12:40:49 -03:00
task_join.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
task_posixspawn.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
task_prctl.c sched: fix 1-byte overflow in prctl(PR_GET_NAME) 2026-09-14 18:51:55 -03:00
task_recover.c pthread: move pthread_cond to userspace 2026-01-26 16:26:39 +08:00
task_reparent.c sched: migrate to SPDX identifier 2024-09-12 01:10:14 +08:00
task_restart.c arch, sched/signal: Fix compilation with ENABLE_PARTIAL_SIGNALS=y 2026-06-16 17:07:32 +08:00
task_setup.c arch, sched/signal: Fix compilation with ENABLE_PARTIAL_SIGNALS=y 2026-06-16 17:07:32 +08:00
task_spawn.c spawn: allow zero stacksize/priority to use binary loader defaults 2026-07-15 12:24:44 -03:00
task_spawnparms.c sched/spawn: Propagate scheduler setup errors 2026-09-13 10:29:49 +08:00
task_start.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
task_terminate.c sched: Remove unnecessary affinity restriction & restoration over smp calls 2025-08-20 10:16:43 +08:00