Skip to content

fix(fs): harden file backends from second review - #890

Merged
ewhauser merged 1 commit into
mainfrom
claude/xenodochial-gagarin-9e71b3
Oct 20, 2026
Merged

ewhauser merged 1 commit into
mainfrom
claude/xenodochial-gagarin-9e71b3

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

Fixes the fs/ and policy findings from the second review of main at 19ff56a.

User-visible behavior

  • Write size cap: files opened for writing through shell redirections or CommandFS.OpenFile now stop at MaxFileBytes. A write that crosses the cap stores what fits and then fails with File too large. printf "%0200000d" 0 > big under --max-file-bytes 1024 now fails, and cat /dev/zero > big stops right away at 1024 bytes instead of running until the timeout. The cap is a fs.LimitFileSize wrapper. It only exposes Seek and Truncate when the wrapped file has them, so callers that check for those methods keep their fallbacks.
  • In-memory FIFOs buffer at most 64 KiB. A writer blocks while the buffer is full, like a host pipe.
  • Hard links: CommandFS.Link now also requires write access to the source. Before, a link created in a writable root could be used to modify a file in a read-only root.
  • Host ReadDir now opens with O_DIRECTORY|O_NONBLOCK. gbash --readwrite-root DIR -c 'mkfifo /p; echo /p/*' no longer hangs.
  • ReadWriteFS read cap now applies to every open that can read, including O_RDWR.
  • .. after a symlink is now applied to the link's target, as POSIX does. This covers CommandFS operations and the builtins' shared stat/open helpers. rm -rf /lnk/../data now removes /a/data, like bash, instead of the top-level /data. The new CommandFS.ResolveAt(ctx, name) does this. Shell-level redirection paths are still cleaned lexically.
  • MemoryFS seeding now rejects directory, symlink, socket and other type bits with EINVAL, which removes the nil children-map panic. FIFO and char-device bits are still allowed, because the snapshot clone path seeds device nodes on purpose.
  • Copy-up of host special files (cloneSpecialFile) now keeps only the permission bits and the allowed type bits.
  • O_CREATE|O_EXCL now fails with EEXIST on a dangling symlink in MemoryFS and TrieFS, instead of creating the link's target.
  • Cross-mount rename: FIFOs are recreated as FIFOs, and other special files are refused. If the copy or the source removal fails, the new copy is removed, unless something already existed at the destination.
  • OverlayFS.Symlink now fails with EEXIST when the lower layer already has the entry.
  • Data race fixed: memoryFile.Read now advances the offset under the write lock.

Notes

  • Finding 7's "check the source is removable first" is handled by rolling back instead. A rename onto itself can't serve as a probe, because MemoryFS returns EEXIST for it.
  • To keep changes small, this touches files outside fs/: commands/invocation_capabilities.go for Link, the write cap and ResolveAt; internal/shell/core.go for capping redirections; and internal/builtins/command_helpers.go for physical ... TestWCFiles0FromMaterializedReportsMaxFileBytes now seeds its oversized file, since writing it is now refused.
  • SPEC.md and commands/AGENTS.md are updated.

Testing

  • go test -race ./fs/... ./policy/... ./commands/ passes, with new regression tests for each finding.
  • make lint is clean.

Cap writes at MaxFileBytes, bound in-memory FIFO buffers, require write on hard-link sources, stop host ReadDir blocking on FIFOs, apply the read cap to O_RDWR, resolve .. after symlinks, reject unsupported seeded type bits, refuse O_EXCL on dangling links, roll back failed cross-mount renames, refuse overlay symlinks over lower entries, mask cloned special modes, and fix a shared-handle read race.
@ewhauser
ewhauser merged commit fe39231 into main Oct 20, 2026
17 checks passed
@ewhauser
ewhauser deleted the claude/xenodochial-gagarin-9e71b3 branch October 20, 2026 15:16
@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