Static binary crashes at startup when the command ends in an option-like argument (`bash -i`)
Describe the bug
The static release binary crashes at startup (SIGSEGV) when the command's last argument looks like a ttyd option that takes a value, for example bash -i.
calc_command_start() in src/server.c copies argv into a buffer of exactly argc pointers, with no NULL terminator, and runs getopt_long over the copy. The command's trailing -i is parsed as ttyd's -i <interface> with its value missing. musl's getopt then advances optind to argc + 1 and permutes the array up to index argc, writing one pointer past the end of the heap buffer. musl's allocator detects the corruption at free(argv_copy) and traps (hlt, reported as SIGSEGV with si_code=SI_KERNEL).
On a real argv, slot argc is the NULL terminator, so the same permutation is harmless there. glibc builds may not show the problem; I only tested the musl static release binary.
To Reproduce
With the x86_64 static release binary of 1.7.7:
$ ttyd -p 7699 -i lo bash -i # Segmentation fault, exit 139
$ ttyd -p 7699 -i lo bash -l # runs
$ ttyd -p 7699 -i lo bash --noprofile # runs
$ ttyd -p 7699 -i lo -- bash -i # runs (workaround)Two core dumps from real launches show the out-of-bounds slot directly:
ttyd -p 7682 -i 127.0.0.1 -W -o bash --noprofile -i(argc = 10): the copy has room for 10 pointers; slot 9 holds NULL and slot 10, one past the end, holds"bash".- A 20-argument launch ending in
bash --noprofile --rcfile <file> -i: slot 20 holds a pointer to<file>.
Expected behavior
ttyd starts and runs bash -i.
Environment:
- ttyd 1.7.7-40e79c7 (x86_64 static release binary, libwebsockets 4.3.3)
- Arch Linux, kernel 7.2.5
Additional context
The code is unchanged on main as of 2922cb8. PR with a fix: keep a NULL terminator in the copy (#1576).
Source: tsl0922/ttyd