Hi,
While fuzzing `print -P` with an ASan+UBSan build of current master (64159322d0e78f78d17d4c61a87d21256eb532e9), I found a deterministic segfault in prompt expansion. It also reproduces on stock macOS system zsh 5.9 (/bin/zsh, arm64, no sanitizer).
Minimal reproducer (any of these):
print -P '%(a.%<)' print -P '%(a.%>)' print -P '%(a.%[)' print -P '%(a.%[0123)' print -P '%(a.%D{'
All exit 139 (SIGSEGV) on an unmodified /bin/zsh 5.9. The common shape is: a conditional `%(...)` whose condition is FALSE ('a' is not a recognized test char), so the true branch is SKIPPED, and the skipped branch contains an unterminated truncation/bracket/date sequence.
Root cause ----------
In putpromptchar() (Src/prompt.c), a false condition causes the branch to be scanned with !doprint instead of being executed:
if (!doprint) switch(*bv->fm) { case '[': while(idigit(*++bv->fm)); while(*++bv->fm != ']'); continue; case '<': while(*++bv->fm != '<'); continue; case '>': while(*++bv->fm != '>'); continue; case 'D': if(bv->fm[1]=='{') while(*++bv->fm != '}'); continue; ... }
(lines 520-538 at the commit above). These four fast-forward loops have no end-of-string check, so an unterminated sequence walks past the NUL of the heap-allocated format string and reads until it hits an unmapped page.
The executed path does not have this problem: prompttrunc() scans with
while (*bv->fm && *bv->fm != truncchar)
(prompt.c:1298). The skip switch is missing the same `*bv->fm &&` guard.
Crash frames (macOS ASan and Linux ASan builds agree):
putpromptchar prompt.c:527 (case '<' loop; also 524 / 530 / 534 for the other cases) putpromptchar prompt.c:511 (recursive call that scans the skipped branch) promptexpand prompt.c:231 bin_print builtin.c:4741 (print -P)
ASan reports SEGV on unknown address (wild read past the end of the string); the read is out-of-bounds but I have not observed any write or info leak.
Non-crashing contrast cases (verified):
print -P '%<' (executed path, guard present) print -P '%(1.true%<)' (condition TRUE -> branch executed, guarded) print -P '%(a.%D{)}' (the conditional's ')' is preceded by '}', so the '}' scan terminates before the NUL) print -P '%(a.X)' (no skip-trigger)
Reachability ------------
`print -P` (and `print -R -P`) from scripts/non-interactive use is confirmed. I could not crash the interactive PS1/zle prompt path in pty tests, and `select`'s PROMPT3 / `read -e` did not crash with piped or </dev/null stdin. Practical exposure is therefore anything that passes attacker-influenced strings through prompt expansion: `print -P` helpers, prompt-theme builders, or programs embedding zsh that call promptexpand() on external data. Impact is a local DoS/crash; no RCE.
Environment where it reproduces
- macOS 15, system /bin/zsh 5.9 (arm64): rc 139, 100% deterministic - ASan+UBSan builds of master 64159322d0e on macOS (clang) and Linux (Alpine musl, clang): SEGV READ with identical symbolized frames - Linux glibc (Ubuntu 26.04, glibc 2.43): Ubuntu's zsh 5.9 and an ASan build of master do NOT crash, but the over-read still happens there, silently. The string lives in zsh's zhalloc heap arena, so bytes past its NUL are still addressable (ASan can't see it) and the scan usually hits a stray '<'/'>'/']'/'}' in older arena contents. A stderr probe added after each skip loop shows it reading 14-28 KB past the terminating NUL (e.g. '%(a.%<)': NUL crossed at +2, loop stopped at +17931). The visible symptom is that the rest of the prompt string is dropped: `print -P 'HEAD%(a.%>)TAIL'` prints only `HEAD`. Whether it crashes depends on allocator/heap layout; the out-of-bounds read happens on every platform tested. - I verified the unguarded loops are unchanged on current master at the time of writing.
Suggested fix -------------
Mirror the executed path's guard in the skip switch:
case '<': while (*++bv->fm && *bv->fm != '<'); continue;
and the same `*bv->fm &&` guard for the '>' (530), ']' (524) and '}' (534) loops. There is also a question of stop semantics on hitting the NUL: the executed path returns 0 from prompttrunc() on unterminated input, and the skip path should likewise bail out of putpromptchar() rather than `continue`.
Happy to provide the ASan reports and the fuzzing harness if useful.
Thanks, Todd Bruss |