Repository navigation
fix(interp): bound shell nesting, budgets, and variable memory - #875
Merged
Merged
Conversation
…ets-review-554338 # Conflicts: # internal/runtime/runtime.go # internal/runtime/test_helpers_test.go
…ets-review-554338 # Conflicts: # SPEC.md
This was referenced Oct 19, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
local,declare -gand dynamic scope behave as before (checked against bash 5.3)( ): 8.2 GB → 0.2soverlayEnviron.Setlooked a name up in its parent before passing the write through, which made each write O(d²)MaxSubstitutionDepthwas only checked on the parsed ASTMaxFunctionDepthlimit (default 1000, like bash'sFUNCNEST)f(){ (f); }: 3.7 GB and growing at 15s → stops at 50 levels in 0.9s.f(){ f; }→ exit 126 in 0.2sbash -c/sh -cstarted fresh command and loop budgetsbash -cloops of 9,000: all 54k commands ran → stops at 10k. Self-recursivebash -c: over 22s → 0.2s, exit 126MaxVariableBytesapplied per variable with no overall totalMaxTotalVariableBytes(default 256 MiB), tracked per environ layerv32in 3.1s, exit 126r.exit.code) were touched by background goroutines without a lock$?inside the trap is read when the trap runs, on the shell's own goroutine-racereported 16 races → nonecat <(echo hi)failed with ENOENT when stdout was a host file, because the same-file check opened, and so consumed, the process substitutionhiUser-visible behavior
policy.Limitsfield to raise. It then exits the current shell with status126, the same as the existing budgets (an interactive shell aborts only the command):subshell nesting limit exceeded (N), increase policy.Limits.MaxSubstitutionDepthNAME: maximum function nesting level exceeded (N), increase policy.Limits.MaxFunctionDepthNAME: total variable size too large (limit N bytes), increase policy.Limits.MaxTotalVariableBytesMaxCommandCount, loop and glob budgets.SPEC / docs
SPEC.md §12 now covers the runtime depth check,
MaxFunctionDepth,MaxTotalVariableBytesand the shared nested-shell budgets. The limit tables inwebsite/content/security/{index,policy}.mdxare updated to match.Notes for reviewers
policy.Limitshas two new fields. That makes it 88 bytes, which trips gocritic's hugeParam check. Rather than change the publicWithLimitOverridessignature, I added//nolint:gocriticto it and to three internal functions.internal/runtime/runtime.go(defaults and override merge) andinternal/builtins/cat.go(a 6-line guard) are also touched by other review-fix PRs, so there may be small merge conflicts.trapActionreads the trap actions map from the goroutine sending the signal. The repro doesn't hit it, but atrapcommand running at the same moment could.Testing
internal/shell/interp/deep_nesting_test.gointernal/runtime/nesting_limits_test.gointernal/runtime/total_variable_limits_test.gointernal/runtime/trap_signal_race_test.gointernal/builtins/cat_procsubst_test.gogo test -race ./internal/shell/... .passes.make lintreports 0 issues.make conformance-testpasses.go test ./...passes except the diff/ripgrep oracle tests (their env vars weren't set) andTestExpansionStressScenarioMatchesExpectedStdoutUnderBash, which also fails on base.