Repository navigation
contrib: stop python, jq and async panics from crashing the process - #860
Merged
Merged
Conversation
- python: pass Monty resource limits (256 MiB heap, recursion depth 1000, duration tied to the request deadline, 60s fallback) so huge allocations raise MemoryError and runaway loops honor the request timeout (exit 124). - jq: vendor gojq v0.12.18 under contrib/jq/internal/gojq and bound every value-recursive helper (compare, encode, contains, flatten, object merge, setpath/delpaths) at MaxDepth=10000; the output encoder checks the same limit. Deep values now fail with exit 5 instead of a fatal stack overflow. - interp: recover panics in pipeline left-hand sides, background jobs and process substitutions so the subshell exits 2 with a sanitized message. - SPEC: document the python limits, jq depth limit and async panic recovery.
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
Untrusted scripts could kill the whole gbash process with contrib interpreters. Under
gbash --serverthat process hosts every session. This PR fixes the three crash paths found in the contrib interpreter security review.1. python: Monty resource limits
Before,
python3 -c 'x="a"*(10**15)'aborted the process with SIGABRT, andwhile True: passignored the request timeout.contrib/pythonnow passesmonty.ResourceLimitsto every run:MaxMemory256 MiB: oversized allocations raiseMemoryError.10**(10**12)is also rejected up front this way.MaxRecursionDepth1000 (the CPython default).MaxDuration= time left before the request deadline. Monty can't see Go context cancellation while it runs bytecode, so this limit is what stops it. When it fires,pythonreturns the context error, so the runtime reportsexecution timed outwith exit124.2. jq: value nesting depth limit (vendored gojq)
jq -c -n 'reduce range(3000000) as $i (0;[.])'(alsotojson,==,sort,contains,flatten,*,setpathwith long paths,del,error) caused a fatal Go stack overflow, which can't be recovered. The recursion is inside gojq. gojq has no hooks for it, and builtins such asto_entriescan deepen values without any user-written construction. So this PR vendors gojq v0.12.18 undercontrib/jq/internal/gojq, the same pattern as the vendored goawk. It includes the upstream root tests,builtin.jqand the upstream license, and a README lists the local changes.MaxDepth = 10000(the same as jq's parser depth). Comparison, the encoder,contains,flatten, object deep merge anddelpathscleanup all track depth.setpath/delpathsreject paths longer than the limit.*DepthLimitError.env.Nextturns that into the iterator's final value and stops, sotrycan't catch it and loop on it.Marshalreturns the error, anderror($deep)messages report it.3. interp: recover panics in goroutine-run subshells
Foreground panics are already recovered in
shell.core.Run. Pipeline left-hand sides (runner_cmd.go),&background jobs and process substitutions run on their own goroutines, and nothing recovered panics there. Those goroutines now deferrecoverAsyncPanic: the subshell prints the sanitized message (shell execution failed), exits with status2, and the shell carries on. The sanitizer moved tointerp.SanitizeRunnerPanicso the foreground and goroutine paths share it.SPEC
SPEC.md now documents the python limits, the jq depth limit and the panic recovery in goroutine-run subshells.
Tests
Each regression test was confirmed to fail first: SIGABRT, hang,
fatal error: stack overflow, andpanic:, in that order.contrib/python/python_limits_test.go: MemoryError, RecursionError, and timeout → 124 for a busy loop run with-cand from stdin.contrib/jq/jq_depth_test.go: 15 deep-value cases plus a within-limit case.internal/gojq/depth_test.go: unit tests for the depth checks.internal/runtime/async_panic_test.go: a panicking command on the left of a pipe, in a background job and in a process substitution (also run with-race).Commands run:
make build,go test ./internal/shell/... ./internal/runtime/...,go test ./...in contrib/jq, contrib/python and contrib/extras, the jq conformance corpus (GBASH_RUN_JQ_CONFORMANCE=1 … -run '^TestJQConformance$'),make lintandmake lint-contrib(0 issues). contrib/python has no conformance suite.Notes
go.modchanges: contrib/jq dropsitchyny/gojqand requirestimefmt-godirectly. The stale gojq lines are removed from the extras, bashtool and examples modules.examplesdoesn't build withGOWORK=offonmaineither (it needsgo mod tidy). That predates this PR.