Skip to content

sched, binfmt: Fail with E2BIG for arguments that are too large. - #20957

Merged
acassis merged 4 commits into
apache:masterfrom
casaroli:b-exec-e2big
Oct 25, 2026
Merged

acassis merged 4 commits into
apache:masterfrom
casaroli:b-exec-e2big

Conversation

@casaroli

Copy link
Copy Markdown
Contributor

Summary

POSIX: exec and posix_spawn() fail with E2BIG when the arguments and the environment of the new image are larger than ARG_MAX bytes. CONFIG_TASK_ARG_MAX sets ARG_MAX and sysconf(_SC_ARG_MAX), but nothing checked it. An argument list that did not fit the stack of the new task reached a DEBUGASSERT in nxtask_setup_stackargs(), so a program could stop the system with one long argument. Without assertions the loop set -ENAMETOOLONG but did not advance, so it never ended.

  • nxtask_init() undoes the scheduler setup when a later step fails. nxtask_setup_scheduler() gives the TCB a PID and puts it on the inactive task list; when tls_init_info() or nxtask_setup_stackargs() then failed, both stayed in place and the caller freed the TCB with kmm_free(). The next task creation could then crash the kernel. The kernel stack of the new task also leaked. This path was reachable without assertions before; the E2BIG below makes it reachable with them, so it comes first.
  • nxtask_setup_stackargs() ends with E2BIG when the arguments do not fit the stack, when the argv[] array does not fit with them (up_stack_frame() fails), or when there are more than CONFIG_TASK_MAX_ARGS.
  • exec_internal() counts the bytes of the argument and environment strings, with their terminators and pointers, before it loads the program, and fails with E2BIG above ARG_MAX.
  • Documentation: posix_spawn() and the TASK_ARG_MAX help.

Impact

exec() and posix_spawn() of a program with more than ARG_MAX bytes of arguments and environment fail with E2BIG (the default ARG_MAX is 4096). A configuration that passes more must raise CONFIG_TASK_ARG_MAX. All build modes and architectures.

Testing

Test program e2big.c on qemu-armv8a:knsh (QEMU with HVF, CONFIG_TASK_ARG_MAX=65536): a small argument, an argument and an environment larger than ARG_MAX, execv() with a too large argument, an argument within ARG_MAX that does not fit an 8 KiB stack (twice), then 8 small spawns. ostest runs after it in the same boot.

master this PR without its first commit this PR
e2big the argument larger than ARG_MAX stops the system with an assertion in the kernel (task/task_setup.c:557) 7 of 7 pass 7 of 7 pass
ostest after e2big (not reached) kernel data abort when NSH starts ostest (arm64_fatal.c:696, fault address 0x74736574736f in one run, 0x10 in another): a freed TCB in the task lists status 0

ostest also exits with status 0 on qemu-armv8a:nsh with this PR, and on master with CONFIG_TASK_ARG_MAX=65536 (knsh and nsh), so the option alone changes nothing.

The stack case depends on the configuration: with a larger stack the argument fits, and the case gives 0, not E2BIG.

Build, each OK: qemu-armv8a:nsh (flat, arm64), qemu-armv8a:knsh (kernel, arm64), lm3s6965-ek:qemu-protected (protected, armv7-m), qemu-armv7a:knsh (kernel, armv7-a), rv-virt:nsh64 (flat, rv64), rv-virt:knsh64 (kernel, rv64). No new warnings.

tools/checkpatch.sh -c -u -m -g passes.

nxtask_setup_scheduler() gives the TCB a PID and puts it on the inactive
task list.  When a later step of nxtask_init() failed (tls_init_info()
or nxtask_setup_stackargs()), the error path left both in place, and the
caller then freed the TCB with kmm_free().  The PID hash table and the
inactive task list kept a pointer to freed memory, and the next task
creation could crash the kernel.  The kernel stack of the new task also
leaked.

On failure, take the TCB off the inactive task list, release its PID and
free its kernel stack before the group is released.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
nxtask_setup_stackargs() copies the arguments to the stack of the new
task.  When they did not fit, a DEBUGASSERT stopped the system, which a
program can cause with a long argument.  Without assertions the loop
set -ENAMETOOLONG but did not advance, so it never ended.  The check of
CONFIG_TASK_MAX_ARGS did not end the loop either.

Strings that fit but leave no room for the argv[] array made
up_stack_frame() fail, and a DEBUGASSERT stopped the system there too.

Now both checks end the loop with E2BIG, the error that POSIX gives exec
for an argument list that is too large, and a failed up_stack_frame()
gives E2BIG too.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
CONFIG_TASK_ARG_MAX sets ARG_MAX, but nothing checked it.  POSIX: exec
fails with E2BIG when the arguments and the environment of the new image
are larger than ARG_MAX bytes.

exec_internal() now counts the bytes of the argument and environment
strings, with their terminators and the pointers to them, before it
loads the program, and fails with E2BIG above ARG_MAX.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
posix_spawn() and exec() fail with E2BIG for arguments and environment
larger than ARG_MAX, and for arguments that do not fit the stack of the
new task.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
@github-actions github-actions Bot added Size: M The size of the change in this PR is medium Area: BINFMT labels Oct 24, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +110 B (+0.2%, 64,087 B / 262,144 B, total: 24% used)

esp32-devkitc

  • ROM: .flash.text +40 B (+0.0%, 127,348 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +40 B (+0.0%, 91,368 B / 3,342,304 B, total: 3% used)

mirtoo

  • kseg0_progmem: .text +68 B (+0.1%, 66,192 B / 131,072 B, total: 51% used)

qemu-armv8a

  • Code: .text.exec_internal +120 B, .text.nxsched_release_tcb -36 B, .text.nxtask_init +72 B, .text.nxtask_setup_stackargs -72 B (+0.0%, 352,368 B)

qemu-intel64

  • Code: .text +220 B (+0.0%, 8,686,246 B)
  • Data: .rodata -64 B (-0.1%, 124,985 B)

rx65n-rsk2mb

  • ROM: .text +32 B (+0.0%, 89,116 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +64 B (+0.0%, 379,824 B)

stm32-nucleo-f103rb

  • flash: .text +36 B (+0.1%, 33,704 B / 131,072 B, total: 26% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@acassis
acassis merged commit 42a4ba5 into apache:master Oct 25, 2026
46 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: BINFMT Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants