Skip to content

[bug] exec cmd inside command substitution loses inherited stdin - $(exec cat) sees EOF (modernish stdin-script mode runs only its first line; rubash#282 receipt follow-up) #575

Closed

Description

@caomengxuan666

Found by the rubash#282 verification pass (PR #572 receipt rerun): past the fatal.sh battery (fixed by #572), echo "$script" | bin/modernish runs ONLY the first line of the stdin script and silently drops the rest. GNU Bash 5.3.0 runs all of it.

Reproducer (rubash, PR-branch binary 054c654 and master)

$ printf 'echo 1\necho 2\necho 3\necho 4\n' | rubash bin/modernish
1
[rc=0]                        # GNU: 1 2 3 4

Minimal, no modernish needed — same script file, both engines:

# p.sh
printf 'l1\nl2\nl3\n' | { read -r x; printf 'got=%s rest=[%s]\n' "$x" "$(PATH=/usr/bin exec cat)"; }
printf 'p\nq\n' | { printf 'noread=[%s]\n' "$(PATH=/usr/bin exec cat)"; }
  • GNU (WSL Bash 5.3.0): got=l1 rest=[l2\nl3] / noread=[p\nq]
  • rubash: got=l1 rest=[] / noread=[]

Consistent 3/3 on every shape. The plain $(cat) form is fine (8/8); it is specifically exec cmd whose child cannot read inherited stdin.

Observed matrix (rubash)

context plain $(cat) $(exec cat)
top level rubash -c 'exec cat', pipe stdin works works
command substitution, pipe stdin works (8/8) EMPTY
pipeline element `(exec cat) head -1`, pipe stdin -
file-redirect stdin, top level works works

Root cause (rubash)

src/builtins/exec.rs has no child-stdin parameter on any spawn path - execute_with_child_stdio/run_external_exec_with_stdio only take child_stdout/child_stderr. run_external_exec (the capture path used inside command substitutions, where fd1 lands in the in-process capture - see the rubash#193 comment in src/executor/trap_exec.rs execute_exec) spawns via Command::output():

let mut process = external_exec_process(program, operands, env_vars, clean_env);
process.stdout(Stdio::piped()).stderr(Stdio::piped());
match process.output() { ... }

std semantics: Command::output() defaults stdin to Stdio::null() unless .stdin() is explicitly set - so the exec'd program reads EOF immediately. Paths that spawn via .status() with unset stdin inherit it, which is why top-level exec cat works.

GNU owner: builtins/exec.def exec_builtin -> shell_execve (execute_cmd.c:6126) - the exec'd program IS the shell process image replaced in place, so it keeps the shell's fd 0; inside a command substitution subst.c keeps the outer stdin open for the subshell (subst.c:7143 command_substitute only dup2's fd 1 for the capture). The correct rubash fix must resolve child stdin from the fd table (endpoint 0), the same way exec_inherited_stdio(1)/exec_inherited_stdio(2) already resolve stdout/stderr, and pass it explicitly at every spawn site (.stdin(...) before .output()/.status()).

modernish impact

bin/modernish stdin-script mode ends with eval "... $(PATH=$DEFPATH exec cat)" after the hashbang preload consumed line 1 (bin/modernish:2275 _Msh_tmp_doHashbangPreload + the eval at the stdin branch) - so exactly the first line runs and the rest is dropped. This is what makes use safe / set -x payloads look like they "kill" modernish under rubash: they are line 2+. -c mode and file-argument mode are unaffected; plain init (echo 'exit 0' | rubash bin/modernish) is unaffected (single line), which is why the #572 receipt passed.

Verified against WSL GNU Bash 5.3.0 (file-based oracle, stdin-redirect invocation to avoid the wsl.exe relay quirk). Repro artifacts under C:/Users/cmx/AppData/Local/Temp/p282*.sh, clone D:/repo/research/282-verify (branch fix/282-eval-comment-blind-probe @054c654e), corpus D:/repo/research/282-verify-modernish (modernish @63bdae0, LF, harvest goodsh patch).

Activity

  1. added
    bug
    P1-high
    on Oct 12, 2026
  2. added a commit that references this issue on Oct 13, 2026
  3. caomengxuan666 commented on Oct 13, 2026

    @caomengxuan666
    MemberAuthor

    Fixed by #583 (squash-merged as 52b303e): exec's spawn paths now carry the shell's stdin channel explicitly (fd-table endpoint 0 resolution), with the command-node fd-0 redirect binding ordered before the handoff. GNU-oracle verified byte-identical on the minimal repro, the 8-shape extension matrix, and the modernish stdin-script case; new regression suite issue575_exec_child_stdin_regressions (12 cases) pins it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1-highbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions