Skip to content

fix(interp): bound shell nesting, budgets, and variable memory - #875

Merged
ewhauser merged 9 commits into
mainfrom
claude/fix-shell-budgets-review-554338
Oct 19, 2026
Merged

ewhauser merged 9 commits into
mainfrom
claude/fix-shell-budgets-review-554338

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

Summary

Fixes seven verified findings from the security and bug review of the shell interpreter. Before this change, all of them could be triggered under the default policy with no timeout set.

# Finding Fix Before → after (CLI, default policy)
1 Every subshell, command substitution or pipeline stage cloned the whole environ chain, so memory grew O(d²) with nesting depth A fork that finds more than 32 stacked layers collapses them into the global layer plus one merged layer. Function scope is kept, so local, declare -g and dynamic scope behave as before (checked against bash 5.3) 5000-stage pipeline: 4.5 GB / 13.2s → 150 MB / 0.7s. 10k nested ( ): 8.2 GB → 0.2s
2 overlayEnviron.Set looked a name up in its parent before passing the write through, which made each write O(d²) Decide on pass-through before the lookup Recursion to depth 1000: 20.8s → 0.9s
3 MaxSubstitutionDepth was only checked on the parsed AST Also checked at runtime. Command substitutions, backquotes, user-written subshells, process substitutions and nested shells each add a level; pipeline-stage subshells don't. New MaxFunctionDepth limit (default 1000, like bash's FUNCNEST) f(){ (f); }: 3.7 GB and growing at 15s → stops at 50 levels in 0.9s. f(){ f; } → exit 126 in 0.2s
4 Each nested bash -c / sh -c started fresh command and loop budgets Nested shells inherit the parent's budget and nesting depth through ctx Six bash -c loops of 9,000: all 54k commands ran → stops at 10k. Self-recursive bash -c: over 22s → 0.2s, exit 126
5 MaxVariableBytes applied per variable with no overall total New MaxTotalVariableBytes (default 256 MiB), tracked per environ layer 300 × 8 MB variables: 7 GB / 35s → stops at v32 in 3.1s, exit 126
6 The pending signal-trap queue (and r.exit.code) were touched by background goroutines without a lock Mutex on the queue. $? inside the trap is read when the trap runs, on the shell's own goroutine -race reported 16 races → none
7 cat <(echo hi) failed with ENOENT when stdout was a host file, because the same-file check opened, and so consumed, the process substitution Only probe when both input and output are regular files prints hi

User-visible behavior

  • Exceeding the new or now-enforced limits prints a message naming the policy.Limits field to raise. It then exits the current shell with status 126, the same as the existing budgets (an interactive shell aborts only the command):
    • subshell nesting limit exceeded (N), increase policy.Limits.MaxSubstitutionDepth
    • NAME: maximum function nesting level exceeded (N), increase policy.Limits.MaxFunctionDepth
    • NAME: total variable size too large (limit N bytes), increase policy.Limits.MaxTotalVariableBytes
  • Nested shells now count against the parent execution's MaxCommandCount, loop and glob budgets.

SPEC / docs

SPEC.md §12 now covers the runtime depth check, MaxFunctionDepth, MaxTotalVariableBytes and the shared nested-shell budgets. The limit tables in website/content/security/{index,policy}.mdx are updated to match.

Notes for reviewers

  • policy.Limits has two new fields. That makes it 88 bytes, which trips gocritic's hugeParam check. Rather than change the public WithLimitOverrides signature, I added //nolint:gocritic to it and to three internal functions.
  • internal/runtime/runtime.go (defaults and override merge) and internal/builtins/cat.go (a 6-line guard) are also touched by other review-fix PRs, so there may be small merge conflicts.
  • Building PIPESTATUS for very long pipelines still churns O(n²) short-lived slices (about 650 MB allocated over the run for 5000 stages), but peak memory stays low. Not addressed here.
  • Still a race risk: trapAction reads the trap actions map from the goroutine sending the signal. The repro doesn't hit it, but a trap command running at the same moment could.

Testing

  • New regression tests:
    • internal/shell/interp/deep_nesting_test.go
    • internal/runtime/nesting_limits_test.go
    • internal/runtime/total_variable_limits_test.go
    • internal/runtime/trap_signal_race_test.go
    • internal/builtins/cat_procsubst_test.go
  • go test -race ./internal/shell/... . passes.
  • make lint reports 0 issues.
  • make conformance-test passes.
  • go test ./... passes except the diff/ripgrep oracle tests (their env vars weren't set) and TestExpansionStressScenarioMatchesExpectedStdoutUnderBash, which also fails on base.

@ewhauser
ewhauser merged commit 5c55c14 into main Oct 19, 2026
17 checks passed
@ewhauser
ewhauser deleted the claude/fix-shell-budgets-review-554338 branch October 19, 2026 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant