Repository navigation
feat(sandbox): confine bash writes in the kernel - #1875
Conversation
…h check gh -q/--jq filters, gh --title/--body, git commit -m messages and gh api endpoints are text the command never opens as a file, and /dev/null carries no data, so an allow-listed read-only command naming them runs without approval again instead of asking in headless runs.
Co-authored-by: Eden Reich <[email protected]>
Drop the per-command text flag rules. They duplicated the regex allow-list and let real paths past the check. The default sandbox policy now allows reading /dev/null, so an allow-listed command naming it runs without approval while writes to it still ask.
Every Bash command the allow-list runs unattended, which in auto mode is every command, now runs under an OS sandbox that lets it read and execute anything but write only under the writable allowed paths, the grants, the carve-outs, the temp dir and the build caches. Landlock on Linux through a hidden re-exec step, Seatbelt on macOS, plain bash on Windows. A command the user approved runs as approved, unconfined, so approving a path outside the sandbox keeps its meaning. The allow-list path check stays as the read guard, since the kernel cannot tell a tool reading its own token from a command printing it. A refused write carries a hint naming the policy. sandbox.yaml filesystem.enforce (default true) turns it off.
The Linux confinement re-ran the binary through a hidden __sandbox command, which pointed at os.Executable: the infer binary in production, but the test binary under go test, where a confined command would have re-run the whole suite. The ruleset is now applied on a pinned thread that forks bash and dies with it, so only the child inherits it, nothing is re-executed, and the hidden command and cmd/sandboxexec are gone. Streams left unset get the null device before the thread is restricted, since exec would otherwise open it for writing there.
The Linux-only file is linted on Linux alone, where errcheck flagged the three deferred closes, and errorlint prefers errors.Is in the Linux test.
|
@infer can you resolve this merge conflict? |
|
Model: Todos
Pull Request✅ Infer Result: SuccessThe merge conflict is resolved and all CI is green. Final summary: Merge conflict resolved on PR #1875What conflicted: main had absorbed the squashed [DOCS]/#1872 fix ( Resolution: four files changed on both sides; three auto-merged cleanly ( Verification:
One transparency note: Model: Tokens: 1,413,746 in · 28,616 out · 1,442,362 total (27 requests) Tool calls: 43 total · 100% success rate TracesStatsTriggered by edenreich · Infer Action · OSS |
# Conflicts: # config/sandbox.go
Auto mode's unrestricted allow-list skips the argv path check, so a bash child could read a blocked deny entry (auth.yaml, *.env, keys) that the file tools exclude for security. The Bash tool now refuses a command naming such a path in every mode with the same exclusion message, and an e2e pins the refusal in auto mode. Indirect spellings stay with the kernel layer, which is the hard boundary where the OS allows one.
The Seatbelt profile refuses file-read-data and file-write-unlink on the files the blocking deny entries name, so an unattended bash command cannot read, copy, clone, rename or reach through a symlink auth.yaml, *.key and the other secrets, whatever spelling it uses. Directory entries such as .git/ stay with the path check, since git must keep reading its own tree. An except is carved out with require-all, since sibling filters are or-ed and the previous form denied reading nearly everything. Denied entries now match case-insensitively in the userspace policy too, so .ENV no longer slips past the file tools on a case-insensitive filesystem. The sandbox hint names reads, the storage layer no longer probes the conversations dir for writability, and the docs describe the read denial.
…lidate infer debug sandbox prints, for the current directory, the allowed and denied entries of sandbox.yaml with what each covers, the built-in carve-outs, the paths the kernel lets an unattended Bash command write in each agent mode, and the files it refuses to read. The sandbox package exports the carve-out lists and the kernel layer names itself and the entries it guards, per platform. infer tools validate, and the Bash tool before an approval, now run the blocking-deny walker first, so a denied path is refused as excluded for security instead of being presented as a command that needs approval.
… plan mode (#1877) Follow-up to #1875, whose later commits (3472d44, 255cb31) made the kernel refuse reads, renames and deletes of the files the blocking deny entries name on macOS. ## Summary Two in-repo texts still described the old boundary: - `AGENTS.md`, Security Gotchas, said reads follow the argv path check. It now says a write outside `sandbox.WritePaths` fails with EACCES, on macOS a read, rename or delete of a file a blocking denied entry names fails too while directory entries such as `.git/` stay with the argv path check, and denied entries match case-insensitively. - `docs/plan-mode.md` called it a known limit that an allow-listed command can reach outside the scratchpad through absolute arguments. It now says such a command can still read outside the scratchpad while a write outside it fails in the kernel and, on macOS, so does reading a file the deny list names. Docs site: inference-gateway/docs#1233 was amended with the matching acceptance criteria.
|
🎉 This PR is included in version 0.262.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Resolves #1874
Based on #1872.
Docs follow-up: inference-gateway/docs#1233.
Summary
The Bash sandbox was an approval decision only: the allow-list decides what runs without a prompt and the path check keeps an unattended command off paths its arguments name. Once a command runs, nothing confines it. In
automode, or after approval, a write can land anywhere the CLI process can write.Every Bash command the allow-list runs unattended, which in
automode is every command, now runs under an OS sandbox that lets it read and execute anything but write only under the writable allowed paths, the grants, the writable carve-outs, the temp dir and the build caches. A write elsewhere fails in the kernel and the tool result tells the model why. A command the user approved runs as approved, unconfined, so the approval flow for a path outside the sandbox keeps its meaning. On macOS the kernel also refuses to read, rename or delete the files the blocking deny entries name, so a spelling the path check cannot predict still fails. Directory entries such as.git/stay with the path check, since git must keep reading its own tree.Changes
config/sandbox.go:filesystem.enforce(defaulttrue).falseturns the kernel wrapper off.internal/sandbox/enforce.go:WritePaths(cfg, mode)lists what a Bash child may write: writable allowed entries and grants, the writable carve-outs (skills, plugins, runtime dirs, plans, insights, memory), the temp dir, the Go, cargo and npm caches, and/dev. Plan mode gives the project up for the scratchpad, asValidateWritedoes.internal/sandbox/paths.go: the carve-out predicates now share list helpers withWritePaths. Behaviour unchanged.internal/sandbox/infrastructure/kernel*.go:Command(ctx, cfg, mode, command)builds the confinedChild, andChild.Startis the one call that goes through the package. Linux applies a Landlock ruleset on the thread that forks bash (read and execute everywhere, write under the listed paths, every right the kernel's ABI knows), so only the child inherits it and the process stays as it was. No re-exec, no hidden command, and it works under Docker's default seccomp profile. macOS runs/usr/bin/sandbox-execwith an allow-default profile that deniesfile-write*outside the listed paths, with each path in its written and symlink-resolved spelling since Seatbelt matches the resolved one. Other platforms run plain bash.DeniedHintnames the policy when the output shows a refused write.internal/tools/bash.go:confinesdecides per command - the allow-list runs it unattended, so the kernel wraps it, or somebody approved it, so it runs plain. A failed confined command gets the hint appended to its output.WritePathsper mode, theconfinesdecision per mode and allow-list outcome, the Seatbelt profile rendering and escaping, a Linux test that a confined child writes only under a listed directory while the parent keeps writing anywhere, and an e2e in auto mode that proves an outsidemkdiris refused with the hint while an inside one lands.file-read-dataandfile-write-unlinkon the files the blocking deny entries name, with an except carved out throughrequire-all, while directory entries stay with the path check. Denied entries match case-insensitively, so.ENVcannot slip past the file tools on a case-insensitive filesystem.infer debug sandboxprints the effective policy, the carve-outs and the kernel write paths per mode, andinfer tools validaterefuses a denied path as excluded for security instead of reporting it as needing approval.Not covered
*.envare not kernel-enforced: Landlock is allow-only and cannot deny inside a granted tree. The path check and the file tools keep enforcing them for unattended commands there.enforcepromises best effort. A kernel that has Landlock but refuses the ruleset fails the command with a message namingfilesystem.enforce.~/.bun) needs anallowedentry.Verification
go testfor sandbox, infrastructure, config, cmd/sandboxexec, cmd/runtime and tools is green on macOS, with the tools tests running under Seatbelt.go vetcross-compiled for Linux (CGO_ENABLED=0) and Windows is clean.task precommit:runis green.