Repository navigation
feat(suggest): confine local model execution on macOS and Linux (F16) - #286
Conversation
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 45 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
Code review: F16 Unix confinement — Request changesReviewed Blocking
Should fix before relying on the boundary
Minor
What looks goodFail-closed on missing mechanisms, Generated by Claude Code |
|
@coderabbitai review |
Action not completed
|
There was a problem hiding this comment.
Note
Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.
🟡 Changes recommended
Approval is blocked because there are critical confinement/timeout safety issues in confinement.rs, including a potential indefinite hang when joining stdout/stderr reader threads after kill attempts and unsafe allocation/locking inside a Linux pre_exec closure, alongside smaller wording and test-flakiness nits.
4 open issues
What changed in this PR
Implements OS-backed confinement for consented local model execution in forge suggest run on macOS and Linux, replacing the prior fail-closed stub while preserving fail-closed behavior on unsupported platforms.
Changes:
- Added macOS sandbox-exec and Linux Landlock+seccomp confinement with env clearing, executable re-verification, bounded stdout/stderr, and timeout supervision.
- Routed
ProcessModelAdapter::invokethrough the new confinement layer and updated CLI/help and user-facing notice text. - Added/updated integration tests, documentation, and changelog notes describing and validating the confinement boundary and refusal behavior.
| File | Description |
|---|---|
| tests/suggest_run_cli_test.rs | Updates end-to-end suggest run CLI expectations to align with confined process execution behavior and refusal modes. |
| tests/suggest_prepare_cli_test.rs | Adjusts suggest prepare CLI tests to match the updated preview/notice messaging. |
| tests/suggest_confinement_test.rs | Adds platform-gated integration tests covering confinement boundaries, refusal paths, timeouts, and cleanup behavior. |
| tests/pr155_adversarial_review.rs | Updates adversarial coverage to validate post-consent executable integrity checks and replacement refusal behavior. |
| src/suggest/run.rs | Updates the suggest run path documentation/behavior to reflect invoking a local adapter under OS confinement where supported. |
| src/suggest/request.rs | Rewords the prepared preview notice text to describe the confinement boundary and execution behavior more accurately. |
| src/suggest/mod.rs | Registers and wires the new confinement module into the suggest feature area. |
| src/suggest/confinement.rs | Implements macOS and Linux confinement (sandbox-exec, Landlock, seccomp) plus process supervision, timeouts, and bounded I/O handling. |
| src/suggest/adapter.rs | Switches the process model adapter from a stub to invoking the confinement layer and updates related documentation. |
| src/cli/mod.rs | Updates CLI help text and messaging for suggest run to reflect confined local execution vs refusal/recorded-response paths. |
| docs/local-model-confinement.md | Adds documentation describing the confinement model, guarantees/limitations, and verification guidance. |
| CHANGELOG.md | Adds a changelog entry describing the confined local model execution feature and its platform scope. |
Review effort: Balanced
| let stdout = stdout_reader.join().map_err(|_| shared::error("the stdout reader failed"))?; | ||
| let stderr = stderr_reader.join().map_err(|_| shared::error("the stderr reader failed"))?; | ||
| let _ = stdin_writer.join(); |
There was a problem hiding this comment.
Fixed in 663b108: supervise() now waits for the stdout/stderr/stdin threads only until a DRAIN_GRACE (2s) deadline via a finished_by poll helper; a reader still blocked by an escaped descendant is detached (handles dropped, never joined) and the failure result returns, so the CLI always returns within a bounded time. Clean-exit path still drains fully. On detach, invoke_confined calls scratch.keep() so a surviving descendant can never write into a recycled scratch path. Regression: deterministic unit test finished_by_detaches_a_thread_that_outlives_the_deadline (a real escaped-descendant repro wasn't feasible on this host — sandbox-exec aborts interpreters — so the detach primitive is unit-tested). Confinement tests 11/11, full suite green.
| command.pre_exec(move || { | ||
| // no_new_privs must precede both Landlock and seccomp. | ||
| set_no_new_privs().map_err(std::io::Error::other)?; | ||
| apply_landlock(&exe, &scratch).map_err(std::io::Error::other)?; | ||
| apply_seccomp().map_err(std::io::Error::other)?; |
There was a problem hiding this comment.
Fixed in 663b108: all allocation moved before fork into linux_confinement() — ruleset attr, exe/scratch CStrings, runtime-path rules, seccomp filter Vec + pre-validated u16 len. The pre_exec closure calls plan.install(), doing only prctl/landlock syscalls/open/close on pre-built data; error paths return raw std::io::Error::last_os_error() (no allocation post-fork). Fail-closed preserved (any failure -> child exits without exec). Cross-checked in Docker (rust:1.94 aarch64-linux): cargo check clean, zero new clippy lints vs base.
| pub const DATA_HANDLING_NOTICE: &str = "The payload below is the exact prepared byte sequence. \ | ||
| Process adapter execution is disabled until OS filesystem/network confinement is available. \ | ||
| Use --recorded-response to import a separately reviewed response. FORGE starts no adapter and transmits nothing. \ | ||
| On macOS and Linux, process adapter execution is confined to this executable and a private scratch directory, with no network access. \ | ||
| Use --recorded-response to import a separately reviewed response instead. FORGE transmits nothing. \ | ||
| The adapter fingerprint and model identifier record the operator's prospective selection, not evidence of execution. \ |
There was a problem hiding this comment.
Fixed in 663b108: the preview notice now describes the filesystem/network boundary accurately — confined to a private writable scratch directory with read-only access to system runtimes and no network access — without implying exec is restricted to a single binary (the profile allows OS runtime binaries for loaders/interpreters).
| fn a_timeout_kills_every_descendant() { | ||
| let adapter = sh_adapter("sleep 30 & sleep 30 & echo started; sleep 30"); | ||
| let started = Instant::now(); | ||
| let result = adapter.invoke(b"", Duration::from_millis(300)); | ||
| assert!(result.is_err(), "a hanging adapter must time out"); | ||
| assert!(started.elapsed() < Duration::from_secs(5), "timeout was not honored"); | ||
| // The whole process group must be dead, not just the leader. | ||
| std::thread::sleep(Duration::from_millis(200)); | ||
| let survivors = std::process::Command::new("pgrep").args(["-f", "sleep 30"]).output().unwrap(); |
There was a problem hiding this comment.
Fixed in 663b108: the descendant-cleanup marker changed from "sleep 30" to a unique "sleep 31.4159" in both the spawn and the pgrep, so parallel tests / unrelated sleep processes can't collide.
Implement ProcessModelAdapter::invoke with real OS confinement, replacing the fail-closed stub on Unix while keeping fail-closed behavior everywhere no approved mechanism exists. - macOS: spawn via /usr/bin/sandbox-exec with a deny-default Seatbelt profile (read-only runtime + executable, one private writable scratch dir, no network); refuse if sandbox-exec is unavailable. - Linux: Landlock filesystem ruleset (kernel >=5.13, clear refusal on older kernels) + seccomp-BPF denying socket/io_uring/socketcall, with foreign-ABI and x32 kill guards; libc FFI only, no new dependency. - Both: env_clear, verified-byte re-hash immediately before spawn, fresh process group with killpg on timeout/refusal, bounded stdout/ stderr, honored timeout including pipe-holding descendants. - Windows and other platforms keep returning PROCESS_EXECUTION_UNAVAILABLE. - RecordedResponseAdapter unchanged. Tests: platform-gated refusal, verified-byte, descendant-cleanup, timeout, output-bound, and filesystem/network boundary tests (reachable-listener control). Docs: docs/local-model-confinement.md boundary note + CHANGELOG. Ticket: #72
Windows CI clippy failed on unused Command/Stdio imports in confinement.rs; they are only used by the Unix spawn path, so gate them with cfg(unix). Child stays ungated (supervise/kill_group use it on every platform) and the non-Unix path still refuses with PROCESS_EXECUTION_UNAVAILABLE. The Linux socket test failed although seccomp correctly returned EPERM: Python 3.14's traceback echoes the -c source line, which contained the NET-ESCAPED success marker, so the test read the echoed source as an escape. Ubuntu's apport excepthook then added apt permission noise. Replace the child with a guarded probe delivered on stdin (argv rejects control characters): it attempts socket, AF_INET6, AF_UNIX and socketpair, requires EPERM for each, and the verdict is an exact stdout match, immune to stderr startup noise. socketpair was a real gap in the seccomp deny list: it creates connected sockets without socket(). Add SYS_socketpair (present on every Linux architecture) and document it. Verified on a Landlock-capable Linux kernel: all 12 confinement tests pass.
…ment before fork - supervise(): after kill_group, wait for the stdout/stderr/stdin threads only until a short grace deadline; a reader still blocked by an escaped descendant holding a pipe is detached instead of joined forever, so the invocation always returns within a bounded time. The scratch directory is kept (not recycled) whenever readers are detached. - Linux pre_exec: build the Landlock ruleset attr, path CStrings, runtime rules and seccomp filter before fork; the post-fork closure performs only async-signal-safe syscalls and reports raw errno on failure (fail closed, no allocation between fork and exec). - Preview notice: describe the filesystem/network boundary instead of implying exec is restricted to a single binary. - Descendant-cleanup test: unique 'sleep 31.4159' marker in spawn and pgrep so parallel tests cannot collide.
663b108 to
89377fd
Compare


Summary
Implements F16 (#72):
ProcessModelAdapter::invokenow executes the consented local model under real OS confinement on macOS and Linux, replacing the fail-closed stub. Fail-closed behavior is preserved everywhere no approved mechanism exists. Owner-approved Unix-first (2026-11-13 decision record); no new dependencies (libc only). Windows stays refused.What is confined
/usr/bin/sandbox-execwith a deny-default Seatbelt profile — read-only runtime + executable, one private writable scratch dir, no network. Refuses if sandbox-exec is absent.socket/io_uring_*/socketcall, with foreign-ABI and x32 kill guards.env_clear, verified-byte re-hash immediately before spawn (refuse on replacement), fresh process group +killpgon timeout/refusal so all descendants die, bounded stdout/stderr (8 MiB / 64 KiB), honored timeout including pipe-holding descendants.PROCESS_EXECUTION_UNAVAILABLE.RecordedResponseAdapterunchanged.Boundary limits (documented)
Filesystem + network only — no CPU/memory/process-count limits; the executable runs as the operator's user inside the sandbox. Linux containers with a blocking seccomp profile (Docker default) fail closed; verify with
--security-opt seccomp=unconfined. Seedocs/local-model-confinement.md.Tests
tests/suggest_confinement_test.rs(platform-gated): replaced/deleted executable refusal, read/write boundary, reachable-listener network deny with unconfined control, exact payload delivery, timeout + descendant cleanup, pipe-hold timeout, output bound, non-zero exit. Updated dependent refusal tests.Verification
cargo fmt --check,cargo clippy --all-targets -- -D warnings: clean (macOS).cargo test: green via pre-commit hook (3377 lib + integration).rust:1.90-bookwormcontainer (--security-opt seccomp=unconfined): confinement, run CLI, pr155, prepare, adversarial, isolation suites all pass; my files clippy-clean.Ticket: #72