Skip to content

fix(cache): confine file operations to the configured root - #5231

Open
singh4jitendra wants to merge 2 commits into
GoogleCloudPlatform:masterfrom
singh4jitendra:fix/cache-root-confinement
Open

singh4jitendra wants to merge 2 commits into
GoogleCloudPlatform:masterfrom
singh4jitendra:fix/cache-root-confinement

Conversation

@singh4jitendra

Copy link
Copy Markdown
Contributor

Description

  • Use Go's root-confined filesystem operations for traditional cache creation, reads/stat, sparse writes, CRC validation, and eviction truncation/removal.
  • Confine shared-chunk cache reads, directory creation, exclusive temporary writes, atomic publication, and cleanup to the same configured root.
  • Carry the trusted cache root explicitly in file specifications instead of relying only on lexical path containment. Preserve configured-root symlinks and relative descendant links that stay inside the root.
  • Add regressions for final and intermediate symlink escapes, outside-target preservation, required root metadata, configured-root symlink compatibility, and shared-cache fallback behavior. Update affected fixtures and API documentation.

The root remains operator-trusted. Fresh private permissions already mitigate ordinary cross-user access, and bucket-object control alone is insufficient to exploit this class of issue. The change does not authenticate cache contents from untrusted writers or provide a general filesystem sandbox. It addresses a different boundary from #5225 and does not restore the reverted log-file change #4637/#4654.

The shared-reader edits overlap #5225. If that PR lands first, this branch should be rebased so rooted paths retain its full-object cache identity; the two fixes are kept as separate review units rather than duplicating the namespace fix here.

Link to the issue in case of a bug fix.

Fixes #5226

Testing details

Checks ran with Go 1.27.2 on native Linux storage in Ubuntu 24.04 under WSL. Native storage was used because drvfs does not preserve the permission semantics and raw LF checksum fixtures required by existing tests. No test expectations were weakened. Only the verified formatted changes were copied back to the contribution worktree.

  1. Manual -
    • make build - passed, including generation, imports/formatting, dependency hygiene, vet, golangci-lint (0 issues), and compilation.
    • git diff --check - passed.
  2. Unit tests -
    • Before confinement: go test -mod=readonly -count=1 -timeout=60s -run '^TestCreateFileRejectsSymlinkEscapes$' ./internal/cache/util - failed as expected for all four read/truncate escape cases, including truncation of the owned sentinel target.
    • go test -mod=readonly -p 1 -count=1 -timeout=8m ./internal/cache/... ./internal/gcsx - passed in all seven packages.
    • go test -mod=readonly -p 1 -count=1 -race -timeout=8m ./internal/cache/... ./internal/gcsx - passed in all seven packages, with no race reports.
    • Symlink probes use only owned temporary directories and sentinel files, not privileged host files.
  3. Integration tests - Not run. No real GCS bucket, FUSE mount, or NFS deployment was accessed.

Any backward incompatible change? If so, please explain.

Absolute or escaping descendant symlinks inside the cache are intentionally rejected. The configured cache root itself can still point to the intended storage through a symlink, and relative descendant links confined to the root remain supported. Cache keys/layout, configured permissions, and ordinary/shared-cache modes are unchanged. Cache writers must still be trusted for data integrity.

Confine traditional and shared-chunk cache filesystem operations to their configured root with os.Root. Protect creation, reads/stat, sparse writes, CRC validation, eviction and atomic shared-cache publication against final and intermediate symlink escapes. Preserve configured-root symlinks, in-root relative links, cache layout and permissions, with target-preservation regressions.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances the security of the file cache mechanism by preventing symlink escape vulnerabilities. It introduces a RootDir field to FileSpec to track the trusted cache root and refactors file operations—including creation, opening, stat, and removal—to use Go's os.Root (via os.OpenRoot), ensuring all filesystem operations are strictly confined within the trusted cache directory. Comprehensive unit tests have been added to verify that symlink escapes are rejected while legitimate root symlinks remain supported. As there are no review comments provided, I have no further feedback to offer.

@singh4jitendra
singh4jitendra marked this pull request as ready for review November 6, 2026 07:38
@singh4jitendra
singh4jitendra requested a review from a team as a code owner November 6, 2026 07:38
@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @charith87, could you take a look when you have a moment? The cache-root hardening is ready for review, and the local build and race tests passed. The GitHub workflows are still waiting for approval. Happy to make any changes you suggest. Thanks!

Merge current master without rewriting PR history. Preserve the PR behavior while adopting the upstream v4 module path, Go 1.27.4 and dependency security updates. Validated with relevant race tests and make build.
@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Merged the latest master in c01ded9 and resolved the two v4 import conflicts, keeping the confined cache helpers in place.

Rechecked on native Linux with Go 1.27.4:

  • go test -mod=readonly -p 1 -count=1 -race -timeout=8m ./internal/cache/... ./internal/gcsx - passed in all seven packages.
  • make build - passed with 0 lint issues.

The branch is updated without rewriting history, and the cache-root protections and compatibility behavior are unchanged. Ready for the updated CI run and review.

@charith87
charith87 requested review from abhishek10004 and removed request for charith87 November 13, 2026 09:30
@charith87

Copy link
Copy Markdown
Collaborator

Adding @abhishek10004 for review since seems to relate to shared chunk cache.

@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @abhishek10004, @charith87 pointed this your way for review. Could you have a look when you get a chance? Thanks!

@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @abhishek10004, could you take a final look when you get a chance and merge if all looks good? Thanks!

@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @abhishek10004, just checking in on the cache-root change. Could you review it when you have a chance and merge if everything looks good? Happy to make any adjustments. Thanks!

@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @abhishek10004, could you take a look at the cache-root fix when you have time? Happy to address any feedback before merge. Thanks!

@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @abhishek10004, could you review the cache-root fix when you have a chance? Thanks!

@vipnydav

Copy link
Copy Markdown
Member

Hi @trinadhkotturu @abhishek10004, this issue can only happen if a process or user already has write access to the --cache-dir to plant symlinks inside it. PR #5231 adds defense-in-depth using os.OpenRoot, though it opens and closes a new root handle on every cache read/open/stat (which adds extra syscalls on the hot read path). Do you think it is worth taking this hardening change given the threat model and perf tradeoff?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants