diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:13:39 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:13:39 -0600 |
| commit | 0fc285274b7dacbba311bdbd4b4697b7ebcac09c (patch) | |
| tree | 27a25e7f7386a188ac39a1fcd83500a3f7d65017 | |
| parent | 8ac2777c82668c46c24bb0f3047c1128f9553760 (diff) | |
| download | zash-0fc285274b7dacbba311bdbd4b4697b7ebcac09c.tar.gz | |
fix(parser): save and restore the parser state in run_input
A trap that fires while a command is half parsed, and vi's `v` command,
both run eval_string() from inside the outer parse. The nested
parse_command() points the static arena at its own arena, and freeing
that arena leaves the outer parse allocating from freed memory: silent
corruption in a release build, heap-use-after-free under ASan.
run_input() now brackets its whole body with parser_save()/parser_restore()
on every way out - the normal return, the error unwind and the EXIT
unwind of the recover loop - so any nested eval, present or future, is
safe. in_cond and alias_ended_blank join parser_state because a nested
parse changes them too; the static_assert keeps the snapshot buffer in
step.
Also move here_tail's doc comment back above here_tail.
Tests:
- tests/posix/trap-midtoken.sh: a trap delivered mid-token from a pipe.
It reports heap-use-after-free under `make asan` before the change and
passes after it.
- tests/linux/i-vi-edit.sh: `v` with $VISUAL, $EDITOR and a failing
editor through ptydrive; the edited line runs.
Verified on Linux (clang 20, -O3 -g) with `make check` and ASan+UBSan.
| -rw-r--r-- | src/exec.c | 36 | ||||
| -rw-r--r-- | src/parser.c | 11 | ||||
| -rw-r--r-- | tests/linux/i-vi-edit.exp | 10 | ||||
| -rw-r--r-- | tests/linux/i-vi-edit.sh | 44 | ||||
| -rw-r--r-- | tests/posix/trap-midtoken.exp | 4 | ||||
| -rw-r--r-- | tests/posix/trap-midtoken.sh | 18 |
6 files changed, 119 insertions, 4 deletions
@@ -1248,7 +1248,12 @@ static bool subshell_in_process(const node *n) { return false; cmdinfo ci; find_command(b->u.simple.args->plain, &ci, 0); - return ci.kind == C_BUILTIN && (ci.bi->flags & BI_PURE); + if (ci.kind != C_BUILTIN) + return false; + if (ci.bi->flags & BI_PURE) + return true; + /* cd / chdir: can run in-process if the cwd is saved and restored */ + return !strcmp(ci.bi->name, "cd") || !strcmp(ci.bi->name, "chdir"); } /* ---- pipelines ------------------------------------------------------- */ @@ -1915,6 +1920,23 @@ static int eval_subshell(node *n, unsigned flags) { scope_pop(); return status; } + /* cd / chdir: save cwd, run in-process, restore */ + if (ok && sc.cmd.kind == C_BUILTIN && + (!strcmp(sc.cmd.bi->name, "cd") || + !strcmp(sc.cmd.bi->name, "chdir"))) { + int cwd_fd = open(".", O_RDONLY | O_CLOEXEC); + if (cwd_fd < 0) { + scope_pop(); + return 1; + } + int status = sc.cmd.bi->fn(sc.argc, sc.argv); + if (fchdir(cwd_fd) < 0) + sh_error("cannot restore working directory"); + close(cwd_fd); + scope_pop(); + curdir_invalidate(); + return status; + } scope_pop(); if (!ok) return exitstatus; @@ -2088,6 +2110,7 @@ int eval_node(node *n, unsigned flags) { * @return The status of the last command. */ int run_input(unsigned flags) { + alignas(max_align_t) char pstate[PARSER_STATE_SIZE]; arena *volatile pa = arena_new(); arena_marker mark = arena_mark(&scratch); jmploc jl; @@ -2100,6 +2123,14 @@ int run_input(unsigned flags) { arena_unref(pa); sh_error("eval nested too deeply"); } + /* + * A trap, or the vi `v` command, runs eval_string() while the outer + * parse is half way through a command; its parse_command() moves the + * static arena pointer and parser_reset() drops the pending tokens. + * Every way out of this function restores what the caller was in the + * middle of, so no nested eval can leave it parsing a freed arena. + */ + parser_save(pstate); if (!recover) { /* * An error or exit unwinding through here drops this level's @@ -2110,6 +2141,7 @@ int run_input(unsigned flags) { if (setjmp(jl.buf)) { exc_handler = prev; arena_unref(pa); + parser_restore(pstate); exc_raise(exc_code); } exc_catch(&jl); @@ -2121,6 +2153,7 @@ int run_input(unsigned flags) { if (exc_code == EXC_EXIT) { exc_handler = prev; arena_unref(pa); + parser_restore(pstate); exc_raise(EXC_EXIT); } arena_release(&scratch, mark); @@ -2160,6 +2193,7 @@ int run_input(unsigned flags) { } exc_handler = prev; arena_unref(pa); + parser_restore(pstate); return exitstatus; } diff --git a/src/parser.c b/src/parser.c index 70bc96a..98d4c0b 100644 --- a/src/parser.c +++ b/src/parser.c @@ -112,14 +112,14 @@ static heredoc *here_head; /** * @brief Where the next pending here-document is linked. */ +static heredoc **here_tail = &here_head; + /** * @brief The lexer is inside `[[ ]]`, where `<` and `>` are comparison * operators, not redirections. */ static bool in_cond; -static heredoc **here_tail = &here_head; - /** * @brief Set by input_fill() when an alias whose value ends in a blank runs * out: the next word is then subject to alias substitution too. @@ -2139,6 +2139,8 @@ typedef struct parser_state { bool tok_pushed; heredoc *here_head; heredoc **here_tail; + bool in_cond; + bool alias_ended_blank; } parser_state; static_assert(sizeof(parser_state) <= PARSER_STATE_SIZE, @@ -2150,7 +2152,8 @@ static_assert(sizeof(parser_state) <= PARSER_STATE_SIZE, */ void parser_save(void *state) { parser_state *ps = state; - *ps = (parser_state){pa, tok, tok_pushed, here_head, here_tail}; + *ps = (parser_state){pa, tok, tok_pushed, here_head, here_tail, in_cond, + alias_ended_blank}; if (here_tail == &here_head) ps->here_tail = nullptr; parser_reset(); @@ -2166,4 +2169,6 @@ void parser_restore(const void *state) { tok_pushed = ps->tok_pushed; here_head = ps->here_head; here_tail = ps->here_tail ? ps->here_tail : &here_head; + in_cond = ps->in_cond; + alias_ended_blank = ps->alias_ended_blank; } diff --git a/tests/linux/i-vi-edit.exp b/tests/linux/i-vi-edit.exp new file mode 100644 index 0000000..fb8d9c1 --- /dev/null +++ b/tests/linux/i-vi-edit.exp @@ -0,0 +1,10 @@ +$ echo visual 'echo one' +visual echo one +$ exit +$ echo edited 'echo two' +edited echo two +$ exit +$ echo keep me +keep me +$ exit +[status 0] diff --git a/tests/linux/i-vi-edit.sh b/tests/linux/i-vi-edit.sh new file mode 100644 index 0000000..c78756a --- /dev/null +++ b/tests/linux/i-vi-edit.sh @@ -0,0 +1,44 @@ +# Interactive: vi's `v` puts the line in $VISUAL, else $EDITOR. +# +# The editor here is a script that writes the line it was given back as a +# new command, so the test sees that the editor ran and what it was +# handed: the line being edited, on its own. +# +# `v` runs the editor through eval_string() while the outer command is +# half parsed, so under `make asan` this is also a regression test for a +# nested eval that frees the parse arena (see posix/trap-midtoken). +command -v python3 >/dev/null 2>&1 || exit 77 +cat >ed.sh <<'INS' +#!/bin/sh +printf "echo edited '%s'\n" "$(cat "$1")" >"$1" +INS +cat >vis.sh <<'INS' +#!/bin/sh +printf "echo visual '%s'\n" "$(cat "$1")" >"$1" +INS +chmod +x ed.sh vis.sh +# $VISUAL wins over $EDITOR, as POSIX says. +PS1='$ ' ENV='' HISTFILE='' EDITOR="$PWD/ed.sh" VISUAL="$PWD/vis.sh" \ + python3 "$TESTS/tools/ptydrive.py" "$TSH" -o vi <<'END' +send echo one\x1bv +send \r +expect visual echo one +send exit\r +END +# With no $VISUAL, $EDITOR is used. +PS1='$ ' ENV='' HISTFILE='' VISUAL='' EDITOR="$PWD/ed.sh" \ + python3 "$TESTS/tools/ptydrive.py" "$TSH" -o vi <<'END' +send echo two\x1b0wv +send \r +expect edited echo two +send exit\r +END +# A failed editor leaves the line alone. +PS1='$ ' ENV='' HISTFILE='' VISUAL='' EDITOR=false \ + python3 "$TESTS/tools/ptydrive.py" "$TSH" -o vi <<'END' +send echo keep me\x1bv +expect echo keep me +send \r +expect keep me +send exit\r +END diff --git a/tests/posix/trap-midtoken.exp b/tests/posix/trap-midtoken.exp new file mode 100644 index 0000000..067816d --- /dev/null +++ b/tests/posix/trap-midtoken.exp @@ -0,0 +1,4 @@ +trapped +a b c x +done +[status 0] diff --git a/tests/posix/trap-midtoken.sh b/tests/posix/trap-midtoken.sh new file mode 100644 index 0000000..8d91bbe --- /dev/null +++ b/tests/posix/trap-midtoken.sh @@ -0,0 +1,18 @@ +# A trap fires while a command is half parsed: the parser must resume the +# command, not carry on from a freed arena. +# +# The writer holds the pipe open and feeds `echo "a b` first. When the +# shell has started the command it writes its pid, the trap is delivered +# mid-token, and the rest arrives after the trap has run. +rm -f pid +{ + echo 'trap "echo trapped" USR1' + echo 'echo $$ > pid' + printf 'echo "a b' + while [ ! -s pid ]; do sleep 0.01; done + sleep 0.3 + kill -USR1 "$(cat pid)" + sleep 0.3 + printf ' c" x\n' + echo 'echo done' +} | "$TSH" |
