Skip to content

fix: validate TEST_TMPDIR in getWritableDirs to prevent sandbox escape via arbitrary bind mounts#29644

Closed
Ashutosh0x wants to merge 1 commit into
bazelbuild:masterfrom
Ashutosh0x:security/validate-test-tmpdir-sandbox
Closed

fix: validate TEST_TMPDIR in getWritableDirs to prevent sandbox escape via arbitrary bind mounts#29644
Ashutosh0x wants to merge 1 commit into
bazelbuild:masterfrom
Ashutosh0x:security/validate-test-tmpdir-sandbox

Conversation

@Ashutosh0x
Copy link
Copy Markdown
Contributor

@Ashutosh0x Ashutosh0x commented May 26, 2026

Fixes #29457

Summary

AbstractSandboxSpawnRunner.getWritableDirs() reads TEST_TMPDIR from the action environment and passes it directly to addWritablePath() without validation. A malicious rule can set TEST_TMPDIR to a relative path containing ../ traversal sequences. When addWritablePath() resolves this via sandboxExecRoot.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 TMPDIR in PosixLocalEnvProvider (#3296). The fix was partial -- TMPDIR was sanitized but TEST_TMPDIR was missed.

Variable Sanitized? Risk
TMPDIR Yes -- stripped and replaced with safe value by PosixLocalEnvProvider.rewriteLocalEnv() None (fixed in #3296)
TEST_TMPDIR No -- used directly from action environment Traversal sequences can escape sandbox subtree

Fix

Added validateTestTmpdir() that uses PathFragment.containsUplevelReferences() to reject ../ traversal sequences before the value reaches addWritablePath() and the bind mount infrastructure.

Design note: Absolute TEST_TMPDIR values are intentionally allowed. Bazel legitimately sets TEST_TMPDIR to an absolute path when the user specifies --test_tmpdir or when the tmp directory falls outside the exec root (see StandaloneTestStrategy.createEnvironment() and TestPolicy.computeTestEnvironment()). The security concern is specifically with traversal sequences in relative paths.

The validation throws IOException, which is the declared exception type for getWritableDirs(). This causes the sandbox setup to fail safely rather than granting the traversal.

Testing

The validation throws IOException for malicious values, which propagates up through the existing error handling in AbstractSandboxSpawnRunner.exec() -> UserExecException, causing the action to fail with a clear error message.

Reproduction from #29457:

sh_test(
    name = "exploit",
    srcs = ["exploit.sh"],
    env = {"TEST_TMPDIR": "../../escape"},
)

Before fix: sandboxExecRoot.getRelative("../../escape") resolves outside the sandbox and gets added as a writable bind mount.
After fix: IOException thrown, action fails safely.

Related

/cc @iancha1992

@github-actions github-actions Bot added team-Local-Exec Issues and PRs for the Execution (Local) team awaiting-review PR is awaiting review from an assigned reviewer labels May 26, 2026
@Ashutosh0x Ashutosh0x closed this May 26, 2026
@Ashutosh0x Ashutosh0x force-pushed the security/validate-test-tmpdir-sandbox branch from 4bcbb29 to a9fe32c Compare May 26, 2026 07:50
@github-actions github-actions Bot removed the awaiting-review PR is awaiting review from an assigned reviewer label May 26, 2026
@Ashutosh0x Ashutosh0x reopened this May 26, 2026
@github-actions github-actions Bot added the awaiting-review PR is awaiting review from an assigned reviewer label May 26, 2026
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
@Ashutosh0x Ashutosh0x force-pushed the security/validate-test-tmpdir-sandbox branch from 69d33aa to 972ce36 Compare May 26, 2026 08:06
@Ashutosh0x Ashutosh0x closed this May 26, 2026
@github-actions github-actions Bot removed the awaiting-review PR is awaiting review from an assigned reviewer label May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team-Local-Exec Issues and PRs for the Execution (Local) team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

linux-sandbox: TEST_TMPDIR from action env is used as -w bind mount without validation; TMPDIR is sanitized, TEST_TMPDIR is not

1 participant