Skip to content

[bug] P0 External script files (shebang scripts) execute IN-PROCESS: variables assigned by the script leak into the caller's scope — direnv stdlib use()/log_status() stub chain corrupted ('use_log: command not found') #363

Closed

Description

@caomengxuan666

Found by ecosweep4 lane (wt22/nest22; corpus: direnv stdlib.sh per-function drive; worktree base a522eb3, release build; oracle WSL GNU Bash 5.3.0 script-file runs).

Reproducer

stub (executable, #!/usr/bin/env bash):

#!/usr/bin/env bash
MARKER_FROM_STUB=set-by-stub

caller:

./stub
echo "after: MARKER=[${MARKER_FROM_STUB:-unset}]"
  • rubash: MARKER=[set-by-stub] — the script's variable assignments landed in the CALLER's scope
  • GNU 5.3.0: MARKER=[unset]
  • Git Bash 5.3.15 (control): unset (separate process)

Side observations proving the mechanism is in-process execution with partial isolation (target/issue-suites/results/ecosweep4/direnv/m2.sh):

S=./side   # stub body: cd /tmp; echo "stub-ran pid=$$"; exit 3
before=$$
"$S"
echo "after-stub rc=$? cwd=$(pwd) pid=$$"

rubash output: stub-ran pid=19120 then after-stub rc=3 cwd=<original cwd> pid=19120 —

  1. same $$ inside the script and the caller (a real child shell would report its own pid),
  2. exit 3 did NOT terminate the caller (contained),
  3. cd /tmp did NOT persist (cwd restored),
  4. exit status propagated correctly.

So rubash emulates external script execution by interpreting the script inside the current shell with cwd/exit containment — but variable assignments are not isolated (and $$ is the caller's). Works via direct path AND via PATH command lookup (both probed).

Real-world hit

direnv stdlib.sh use() (stdlib.sh:1132-1137):

use() {
  local cmd=$1
  log_status "using $*"
  shift
  "use_$cmd" "$@"
}

log_status() runs the direnv binary ("$direnv" log -status "$*"). Under rubash, whatever variables the (stubbed or real) child script assigns — cmd is an extremely common name — clobber the caller's locals. In this lane's harness the stub's cmd=$1 turned "use_$cmd" into use_log → use_log: command not found (rc=127) for use/use_julia/use_node/use_nodenv/use_rbenv/use_flake/use_guix — the entire use family dies. Any real tool invoked by an .envrc that internally assigns a variable colliding with an .envrc/direnv local silently corrupts it. GNU anchor: execute_cmd.c execute_disk_command → fork/exec (execute_cmd.c:5740s family); the child is a separate process whose variable table never touches the parent's (variables.c: no such sharing exists).

Probes

  • target/issue-suites/results/ecosweep4/direnv/st/marker + m1.sh (+ .rub.out/.gnu.out)
  • st/side + m2.sh (pid/cwd/exit/rc matrix)
  • use1.sh (+ wsl control) — the use() replica showing before-call cmd=[log] after the stub call
  • per-function verdicts: verdict.txt rows use/use_julia/use_node/use_nodenv/use_rbenv/use_flake/use_guix

Suggested fix direction

The in-process script executor needs a full variable-table save/restore (at minimum: unset/restore every variable the script assigns, like GNU's fork isolation), or a real child-process boundary. Note cwd/exit are already contained — variables are the missing wall. $$ inside such scripts is a secondary divergence (caller pid instead of child pid).

Activity

  1. added
    bug
    on Sep 30, 2026
  2. caomengxuan666 commented on Sep 30, 2026

    @caomengxuan666
    MemberAuthor

    ecosweep4 follow-up evidence (same lane, wt22/nest22): the in-process script execution also leaks readonly attributes across script invocations.

    bats-core runs bats-exec-test as an external script once per test. Under rubash, once a test's frame declares a variable readonly, the NEXT test's in-process run inherits the readonly mark and its unset fails:

    libexec/bats-core/bats-exec-test: line 53: unset: BATS_RETRY_RETURN_CODE: cannot unset: readonly variable
    libexec/bats-core/bats-exec-test: line 53: unset: BATS_TEST_TAGS: cannot unset: readonly variable
    

    GNU (fresh process per test): no such errors. Verified the plain readonly-unset semantics are symmetric (subshell/function unset of a readonly var fails on both shells — probe target/issue-suites/results/ecosweep4/rupro.sh), so the divergence is specifically the leaked attribute, not unset behavior.

    Artifacts: rub-only stderr in target/issue-suites/results/ecosweep4/bats/{rub/file_setup_teardown.bats.err, rub/junit-formatter.bats.out} (22 lines accumulated), GNU side empty.

  3. added 3 commits that reference this issue on Sep 30, 2026
  4. caomengxuan666 commented on Sep 30, 2026

    @caomengxuan666
    MemberAuthor

    Fixed by wt22/p0fix (83fc21e, master 3ade4b2): classified as DESIGN (Windows has no kernel #!, in-process execution is the established exec model) — but the direct-script branch was the one remaining fork-model caller: typed-store writes leaked through without rollback and children saw parent UNEXPORTED variables. GNU anchor: execute_cmd.c:6139-6233 shell_execve (#! = new process) AND the ENOEXEC tail 6237-6260 -> shell.c:429-464 shell_reinitialize — BOTH are fresh shells inheriting only exported variables (WSL-proven: an unshebang +x child prints PLOCAL=[unset]). Fix: the direct branch passes exec_model_entry=true (one flag, reusing the existing full fresh-shell isolation machinery). Issue probes + 9 new tests byte-aligned; direnv use/use_node/use_vim turn green (28->31/52). $$ documented residual.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions