fix: validate TEST_TMPDIR in getWritableDirs to prevent sandbox escape via arbitrary bind mounts#29644
Closed
Ashutosh0x wants to merge 1 commit into
Closed
Conversation
4bcbb29 to
a9fe32c
Compare
Add validateTestTmpdir() using PathFragment.containsUplevelReferences() to reject '../' traversal sequences in TEST_TMPDIR before it reaches addWritablePath(). Absolute paths are allowed (legitimate via --test_tmpdir). Fixes bazelbuild#29457
69d33aa to
972ce36
Compare
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.
Fixes #29457
Summary
AbstractSandboxSpawnRunner.getWritableDirs()readsTEST_TMPDIRfrom the action environment and passes it directly toaddWritablePath()without validation. A malicious rule can setTEST_TMPDIRto a relative path containing../traversal sequences. WhenaddWritablePath()resolves this viasandboxExecRoot.getRelative(), the resulting path escapes the sandbox exec root, and the directory gets added as a writable bind mount -- granting the sandboxed action write access to host directories outside the sandbox subtree.Root Cause
This is the same class of issue that motivated stripping
TMPDIRinPosixLocalEnvProvider(#3296). The fix was partial --TMPDIRwas sanitized butTEST_TMPDIRwas missed.TMPDIRPosixLocalEnvProvider.rewriteLocalEnv()TEST_TMPDIRFix
Added
validateTestTmpdir()that usesPathFragment.containsUplevelReferences()to reject../traversal sequences before the value reachesaddWritablePath()and the bind mount infrastructure.Design note: Absolute
TEST_TMPDIRvalues are intentionally allowed. Bazel legitimately setsTEST_TMPDIRto an absolute path when the user specifies--test_tmpdiror when the tmp directory falls outside the exec root (seeStandaloneTestStrategy.createEnvironment()andTestPolicy.computeTestEnvironment()). The security concern is specifically with traversal sequences in relative paths.The validation throws
IOException, which is the declared exception type forgetWritableDirs(). This causes the sandbox setup to fail safely rather than granting the traversal.Testing
The validation throws
IOExceptionfor malicious values, which propagates up through the existing error handling inAbstractSandboxSpawnRunner.exec()->UserExecException, causing the action to fail with a clear error message.Reproduction from #29457:
Before fix:
sandboxExecRoot.getRelative("../../escape")resolves outside the sandbox and gets added as a writable bind mount.After fix:
IOExceptionthrown, action fails safely.Related
TEST_SRCDIRviaSandboxStash.getCurrentRunfilesDir()), fix in PR fix: validate TEST_SRCDIR in SandboxStash to prevent path traversal outside sandboxExecRoot #29631TMPDIRinPosixLocalEnvProvider/cc @iancha1992