Skip to content

fix(containment): permit copy_file_range and sendfile on Linux - #451

Merged
pgrayy merged 3 commits into
mainfrom
feature/pgrayy/linux-copy-syscalls
Nov 13, 2026
Merged

pgrayy merged 3 commits into
mainfrom
feature/pgrayy/linux-copy-syscalls

Conversation

@pgrayy

@pgrayy pgrayy commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Description

On Linux, the seccomp permit filter refuses copy_file_range and sendfile with EPERM. libuv copies a file with copy_file_range, then sendfile, and treats EPERM from either as a real failure. So Node's fs.copyFile and fs.cp fail inside a box, and scaffolders such as Next.js and Vite cannot copy their templates.

This change adds both calls to WORKLOAD_PERMITTED.

  • Both calls act only on descriptors the workload already holds. The kernel checks the same read and write modes that read and write check, so the mount view and the filesystem grants still bound them.
  • sendfile has no address argument. A socket destination reaches only a peer that connect already reached, and the egress relay copies bytes in user space, so it cannot tell sendfile from write.
  • Go's http.FileServer also sends files with sendfile. On main, its response has status 200 and stops after 512 bytes.

Why permit sendfile instead of answering it with EINVAL. libuv falls back to read and write when sendfile returns EINVAL (not EPERM or ENOSYS), so EINVAL would also fix Node and Go. The permit is kept for speed. On this host, with copy_file_range answering EXDEV (a copy across filesystems), Node copied a 512 MB file from xfs to tmpfs in 217–270 ms with sendfile and 501–533 ms with EINVAL. For 3000 files of 8 KB, the ranges overlapped. The cost is the old-kernel residual below. crates/containment/AGENTS.md records both, and says to switch to EINVAL if Box starts to defend against unpatched kernels.

Decision this depends on: the operator approved widening the workload permit set with these two calls on 2026-11-13. No decisions.md entry records it.

Residual, recorded in crates/containment/AGENTS.md: from Linux 5.12, sendfile into a pipe calls splice_file_to_pipe. On kernels 5.12 to 5.14, 5.15.0 to 5.15.24, and 5.16.0 to 5.16.10, that reaches Dirty Pipe (CVE-2022-0847). splice, vmsplice, tee, and io_uring stay refused. Seccomp cannot see a descriptor's type, so it cannot refuse a pipe destination alone. Box does not defend against an unpatched kernel (tenets non-goals).

Related Issues

Closes #420. Refs #182, #42.

Type of Change

Bug fix

Testing

  • optimized_copies_reach_only_permitted_descriptors runs a probe through the real trampoline. It copies with each call into a file, a pipe, and a Unix socket. It checks that a read-only grant, an ungranted path, and an inherited descriptor stay closed. With either permit removed, the test fails with EPERM (checked by feature-critic mutation).
  • Manual, ARM64 kernel 6.18, Node 22.22.2 --jitless through the trampoline: on main, fs.copyFileSync and fs.cp fail with EPERM; with this change, both copy. sendfile into a loopback TCP socket delivers the bytes; into an unconnected TCP socket it returns EPIPE.
  • cargo build, test, clippy --all-targets (0 warnings), and fmt --check for strands-box-containment --all-features pass. profile_conformance needs TMPDIR outside the operator home on this host; with TMPDIR=/tmp it passes 68/68, on main and here.
  • test-integ/run.sh: 119 pass, 10 macOS-only skips, 12 fail in mcp_fixture.rs setup (no write access to /usr/bin, before strands-box runs).

Not checked locally, so CI must cover it: x86-64 tests (only cargo check of the library and binaries ran). Node without --jitless in a box is still blocked by #42.

  • I ran the relevant suites (cargo test -p strands-box-containment --all-features)
  • If I touched Rust, I ran cargo fmt --all and cargo clippy --all-targets --all-features -- -D warnings (containment crate)

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • My changes generate no new warnings

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

## Problem

On Linux, the seccomp permit filter refuses `copy_file_range` and `sendfile` with `EPERM`. libuv
copies a file with `copy_file_range`, then `sendfile`, and treats `EPERM` from either as a real
failure. So `fs.copyFile` and `fs.cp` fail inside a box, and Node project scaffolders such as
Next.js and Vite cannot copy their templates between permitted paths (#420).

## Solution

Add `copy_file_range` and `sendfile` to `WORKLOAD_PERMITTED`. Both calls act only on descriptors
the workload already holds, and the kernel checks the same read and write modes that `read` and
`write` check. `sendfile` has no address argument, so a socket destination reaches only a peer that
`connect` already reached. The operator approved this permit set change on 2026-11-13.

Returning `ENOSYS` instead (as in the closed #182) does not fix Node: libuv falls back from
`copy_file_range` to `sendfile`, but not from `sendfile` to read and write.

`crates/containment/AGENTS.md` records the measurement and one residual: on kernels 5.12 to 5.14,
5.15.0 to 5.15.24, and 5.16.0 to 5.16.10, `sendfile` into a pipe reaches the Dirty Pipe defect
(CVE-2022-0847). `docs/design/linux-enforcement.md` states the new permits.

No interface moves.

## Tests

- `optimized_copies_reach_only_permitted_descriptors` runs a probe through the real trampoline. It
  copies with each call into a file, a pipe, and a Unix socket. It also checks that a read-only
  grant (`EROFS`, then `EBADF`), an ungranted path (`ENOENT`), and an inherited descriptor (`EBADF`)
  stay closed. Without either permit, the test fails with `EPERM`.
- `permit_filter_has_the_expected_workload_and_installer_entries` counts the two new entries.
- Manual, ARM64 kernel 6.18: Node 22.22.2 `--jitless` through the trampoline. On main,
  `fs.copyFileSync` and `fs.cp` fail with `EPERM`; with this change, both copy the file. A
  `sendfile` into a loopback TCP socket delivers the bytes; into an unconnected TCP socket it
  returns `EPIPE`.
- `cargo build`, `cargo test`, `cargo clippy --all-targets`, and `cargo fmt --check` for
  `strands-box-containment --all-features` pass. `profile_conformance` needs a `$TMPDIR` outside
  the operator home.

Not run: x86-64 tests (only `cargo check` of the library and binaries). Node without `--jitless`
in a box is still blocked by #42. Twelve `test-integ` MCP cases need write access to `/usr/bin` and
did not run on this host.
@github-actions

Copy link
Copy Markdown

Assessment: Request Changes

The copy_file_range permit is well justified and well tested. The sendfile permit depends on a fallback claim that holds only for ENOSYS. libuv falls back to read and write when sendfile returns EINVAL, so Node works without the permit, and leaving it out avoids the new Dirty Pipe residual. See the inline comment for measurements and the narrower fix.

Checked and held
  • x86-64: optimized_copies_linux passes 2/2 and permit_filter_has_the_expected_workload_and_installer_entries passes on kernel 7.0 x86-64. That covers the gap the PR leaves to CI.
  • The permit is unconditional on both architectures, with no misplaced #[cfg(target_arch)]. The 113→115 and 116→118 counts are correct.
  • An inherited descriptor (fd 900) is closed by the trampoline, so both calls return EBADF.
  • A read-only grant is refused at open (EROFS), and the descriptor-mode checks refuse it again (EBADF).
  • A socket destination gives no new network reach: sendto is already permitted, and an unconnected socket gets EPIPE or EDESTADDRREQ.
  • copy_file_range reaches regular files only, never a pipe or a socket.
  • WORKLOAD_PERMITTED is the only permit table. The tools, MCP servers, and agent all use it, so no second table needs the change.
  • The probe report is checked with a single whole-object assert_eq!, not field by field.
  • Root AGENTS.md says a code change adds no obligation to create a decisions.md entry, so the missing entry is not a finding.
  • docs/user/ does not list system calls, so no user page needs an update.

The probe design, which pins the effect through the real trampoline, is a solid pattern.

@github-actions

Copy link
Copy Markdown

Assessment: Request Changes (unchanged)

8590a99 only merges main into the branch. Since 0b56663, the PR's own changes are the same, apart from updating the permit counts to 116 and 119 now that main has the waitid permit from #447. The open thread on sendfile still applies: the code comment, the AGENTS.md text, and the Dirty Pipe residual are all unchanged.

Re-checked at 8590a99

@github-actions

Copy link
Copy Markdown

Assessment: Approve

ef09b24 resolves the open thread. The wrong claim about the libuv fallback is corrected, and keeping sendfile now rests on a speed reason that was measured and recorded. The Dirty Pipe residual and the switch to EINVAL are written down where the next maintainer will look. Only comments and AGENTS.md changed, so the code I tested at 8590a99 (on x86-64, optimized_copies_linux 2/2 and the syscall unit tests) still applies.

Re-checked at ef09b24
  • The new fallback sentence matches libuv's uv__fs_sendfile: EINVAL, EIO, ENOTSOCK, and EXDEV fall back to read and write, while EPERM and ENOSYS stop the copy.
  • The Go claim matches internal/poll.SendFile: with nothing written, EINVAL falls back to a generic copy and EPERM returns an error, so the response is cut short.
  • The crates/containment/AGENTS.md rule that a new syscall needs measured evidence and a rationale: it is met, with the measurements and the operator decision both recorded.
  • The code comment and AGENTS.md now give the same reason, so the fact has one owner.
  • cargo fmt --check is clean, and the code itself is unchanged since 8590a99.

@pgrayy
pgrayy enabled auto-merge (squash) November 13, 2026 20:33
@pgrayy
pgrayy merged commit f40db21 into main Nov 13, 2026
23 checks passed
@pgrayy
pgrayy deleted the feature/pgrayy/linux-copy-syscalls branch November 13, 2026 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area - Agent Containment bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Linux: optimized file copies fail with EPERM on permitted files

2 participants