Repository navigation
policy: resolve symlinked parents for write, remove, and rename checks - #852
Merged
Merged
Conversation
CheckPath only checked the lexical path for remove/rename, and for write/mkdir it allowed any target whose parent did not exist yet. Since backends follow symlinks in parent components (and MemoryFS creates missing parents on O_CREATE), a script could write, delete, or move files outside the configured write roots through a symlink, including under the default SymlinkDeny mode. Resolve every parent component for remove/rename/lstat/readlink, and for write/mkdir resolve through the deepest existing ancestor and follow dangling symlinks before checking the resolved path.
ewhauser
enabled auto-merge (squash)
October 19, 2026 03:37
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 sandbox escapes in
policy.CheckPaththat let scripts write, delete, or move files outside the configured write roots by going through a symlink.Before this change:
SymlinkFollow,rm -rf /safe/out/keep(where/safe/out -> /denied) deleted/denied/keep, andmvcould move files out of it. UnderSymlinkDenyonly therm/mvbuiltins' ownlstatpre-checks stopped this; a third-party command callingCommandFS.Remove/Renamedirectly bypassed both protections.Realpath(parent)returnedErrNotExist, whichCheckPathtreated as "allow".MemoryFS.OpenFile(O_CREATE)then created the missing parents through the symlink. Even under the defaultSymlinkDenypolicy,echo pwn > /safe/out/new/fcreated/denied/new/f.echo pwn > /safe/dangling(where/safe/dangling -> /denied/created) created the target outside the write roots in both modes.Fix: a new
resolveMaybeMissinghelper. It resolves existing prefixes withRealpath, so backend namespaces such as mounts are respected. Missing components are appended literally, and dangling symlinks are followed from their resolved parent directory.remove,rename,lstat, andreadlinkchecks resolve every parent component but leave the final entry unresolved.rm linkstill removes the link itself.writeandmkdirchecks resolve through the deepest existing ancestor and follow dangling symlinks.User-visible behavior
Mutations that reach a path through a symlink now get a policy denial (exit 126). The message is
symlink traversal deniedin deny mode, oroutside allowed rootswhen the resolved target is outside the roots.One stricter case: under
SymlinkDeny, creating a file under an in-root symlink whose remaining components don't exist yet (e.g./safe/inner/new/fwithinner -> real) is now denied. Before, it was allowed only because the missing parent short-circuited the check. This matches the SPEC rule that any traversal through a symlink fails in deny mode.Tests
policy/pathcheck_test.go: a table test covering write, mkdir, remove, and rename through symlinked parents, dangling symlinks, missing directories, and allowed controls, in both symlink modes. Run against the oldpathcheck.go, 13 of its cases fail.internal/runtime/symlink_policy_test.go: end-to-end script tests that run a mutation, then check in the same session that/deniedis untouched. Run against the oldpathcheck.go, 6 of its cases fail.make lint: 0 issues.make test: everything passes exceptscripts/bench-compareTestExpansionStressScenarioMatchesExpectedStdoutUnderBash, which compares against the host/bin/bash(ifs=6vs5). It fails the same way with the originalpathcheck.go, so this change didn't cause it.Known limitation
A dangling symlink's target is resolved lexically from the link's resolved parent, because
Realpathcannot resolve a missing target. For a dangling link with an absolute target inside a mounted filesystem, the checked path is in the global namespace, not the mount's, so the check may deny a path the backend would actually create inside the mount.SPEC
Updated the path-policy enforcement rule in
SPEC.md(§12) to describe how parent components and missing paths are resolved for each action.