Skip to content

fix(builtins): harden file builtins against host probes, size bombs and symlink escapes - #874

Merged
ewhauser merged 10 commits into
mainfrom
claude/fix-file-builtins-review-30fcad
Oct 19, 2026
Merged

ewhauser merged 10 commits into
mainfrom
claude/fix-file-builtins-review-30fcad

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

Fixes the verified security and correctness findings from the review of file-touching builtins under internal/builtins/. Each fix has a regression test that checks exit codes, stdout/stderr and sandbox filesystem effects.

User-visible changes

Compat host-path remap (HIGH)

  • Builtins no longer call filepath.EvalSymlinks on the real host filesystem. Before, a script could probe whether arbitrary host paths existed (for example export GBASH_COMPAT_ROOT=/private/tmp; mkdir /claude-501 succeeded because the host path existed).
  • The remap is now purely lexical, and only the embedder can enable it, through the new Config.CompatHostRoots / gbash.WithCompatHostRoots or the repeatable gbash --compat-host-root DIR flag. GBASH_COMPAT_ROOT, abs_top_builddir and abs_top_srcdir in the script env no longer rewrite paths.
  • scripts/gnu-test.sh was the only thing relying on the old behaviour. Its launcher now passes both the logical and physical workdir.

Size limits and timeouts (HIGH)

  • truncate, dd (block sizes, seeks, writes), gunzip and tar -x reject sizes beyond MaxFileBytes before allocating and report File too large.
  • Buffers sized directly by user input (dd block sizes and in-memory seeks, truncate) stay under 1 GiB even when MaxFileBytes is unlimited. Before, echo a | dd bs=200000000000000 caused a fatal Go out-of-memory error.
  • split creates at most 100000 files per invocation. The widths for split -a, csplit -n and --suffix-format are capped at 4096. csplit reads its input through the bounded commands.ReadAll.
  • Copy loops and recursive walks (dd, truncate, gzip, tar, cp, rm, du, chmod/chown, find) check the context, so they stop at the request timeout.

tar (HIGH/MEDIUM)

  • A symlink entry no longer recursively deletes an existing non-empty directory. Like GNU, it fails with "File exists".
  • Follow-mode only: tar now rejects writes and hardlink sources whose existing parent resolves outside the -C root through archive-created symlink chains (a -> ., a/b -> .., a/b/escaped.txt).
  • tar -cf out.tar . now names members ./…, as GNU does. It also skips its own archive with a warning instead of trying to archive it.

cp / install (HIGH/MEDIUM)

  • Same-file detection now uses backend node identity, with a Realpath fallback. Before, ln a b; cp a b silently truncated a on MemoryFS, and in follow-mode cp a link-to-a did the same.
  • cp -b refuses to replace a directory backup (cannot backup … Is a directory).
  • The copy-into-itself check now resolves symlinks. Before, cp -r d dlink/sub recursed forever and ignored the timeout.

find (MEDIUM)

  • find now walks with Lstat by default (GNU -P) and follows symlinks only with -H, -L or -follow. It supports -type l, and -L reports loops instead of following them.
  • In follow-mode, -delete can no longer delete outside the tree through symlinked directories. Under the default SymlinkDeny policy, a symlink in the tree no longer aborts the whole search with exit 126.
  • Existing find tests that encoded the old always-follow behaviour were updated. Dangling symlinks are now reported as links, as GNU does.

mktemp (MEDIUM)

  • With an all-X template, mktemp -p /tmp XXXXXX, --tmpdir=/tmp XXXXXX and TMPDIR=/tmp mktemp -t XXXXXX now create the file inside /tmp. Before, they created /tmpXXXXXX in /.

SPEC updates

  • Adds the --compat-host-root CLI flag entry and CompatHostRoots to the Config listing.
  • Adds one bullet in the command-authoring section on file-size limits, allocation ceilings, split/csplit caps and context checks.

Notes for reviewers

  • To plumb CompatHostRoots from the embedder down to builtins, this PR touches files outside internal/builtins: api.go, options.go, internal/runtime, internal/shell/core.go, commands/ (new InvocationOptions.CompatHostRoots and (*Invocation).CompatHostRoots()) and cli/runtime_options.go.
  • pwd and mkdir still read GBASH_COMPAT_ROOT from the env, but only to format pwd output and to pick the umask; neither touches the host.
  • grep -r following symlinked dirs is left to the text-builtins work.
  • I did not run the GNU compat harness.

Testing

  • go test ./internal/builtins/... ./internal/runtime/... ./cli/... ./commands/... . passes, with the pinned diff and ripgrep oracles set.
  • go test ./cmd/gbash passes except for TestRunCLIHostUtilityTailGroupedQuietFollowFlags, which failed once under the full parallel run. It passed 3/3 alone and looks timing-flaky.
  • golangci-lint (make lint's custom binary, run with --allow-parallel-runners because other sessions held the lock): 0 issues.

@ewhauser
ewhauser merged commit 3f35f21 into main Oct 19, 2026
17 checks passed
@ewhauser
ewhauser deleted the claude/fix-file-builtins-review-30fcad branch October 19, 2026 18:53
@ewhauser ewhauser mentioned this pull request Oct 20, 2026
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