Skip to content

policy: resolve symlinked parents for write, remove, and rename checks - #852

Merged
ewhauser merged 3 commits into
mainfrom
claude/full-code-review-500403
Oct 19, 2026
Merged

ewhauser merged 3 commits into
mainfrom
claude/full-code-review-500403

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

Summary

Fixes sandbox escapes in policy.CheckPath that let scripts write, delete, or move files outside the configured write roots by going through a symlink.

Before this change:

  • Remove/rename: the check looked only at the lexical path, but backends follow symlinks in parent components. With SymlinkFollow, rm -rf /safe/out/keep (where /safe/out -> /denied) deleted /denied/keep, and mv could move files out of it. Under SymlinkDeny only the rm/mv builtins' own lstat pre-checks stopped this; a third-party command calling CommandFS.Remove/Rename directly bypassed both protections.
  • Write/mkdir with a missing parent: Realpath(parent) returned ErrNotExist, which CheckPath treated as "allow". MemoryFS.OpenFile(O_CREATE) then created the missing parents through the symlink. Even under the default SymlinkDeny policy, echo pwn > /safe/out/new/f created /denied/new/f.
  • Dangling symlinks: echo pwn > /safe/dangling (where /safe/dangling -> /denied/created) created the target outside the write roots in both modes.

Fix: a new resolveMaybeMissing helper. It resolves existing prefixes with Realpath, 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, and readlink checks resolve every parent component but leave the final entry unresolved. rm link still removes the link itself.
  • write and mkdir checks resolve through the deepest existing ancestor and follow dangling symlinks.
  • Read, stat, and readdir checks are unchanged.

User-visible behavior

Mutations that reach a path through a symlink now get a policy denial (exit 126). The message is symlink traversal denied in deny mode, or outside allowed roots when the resolved target is outside the roots.

$ echo pwn > /safe/out/new/f      # /safe/out -> /denied, WriteRoots=[/safe]
write "/safe/out/new/f" denied: symlink traversal denied

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/f with inner -> 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 old pathcheck.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 /denied is untouched. Run against the old pathcheck.go, 6 of its cases fail.
  • make lint: 0 issues.
  • make test: everything passes except scripts/bench-compare TestExpansionStressScenarioMatchesExpectedStdoutUnderBash, which compares against the host /bin/bash (ifs=6 vs 5). It fails the same way with the original pathcheck.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 Realpath cannot 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.

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
ewhauser enabled auto-merge (squash) October 19, 2026 03:37
@ewhauser
ewhauser merged commit b59ab99 into main Oct 19, 2026
16 checks passed
@ewhauser
ewhauser deleted the claude/full-code-review-500403 branch October 19, 2026 06:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant