Skip to content

feat(suggest): confine local model execution on macOS and Linux (F16) - #286

Merged
brianluby merged 3 commits into
mainfrom
feat/f16-unix-confinement
Nov 14, 2026
Merged

brianluby merged 3 commits into
mainfrom
feat/f16-unix-confinement

Conversation

@brianluby

Copy link
Copy Markdown
Collaborator

Summary

Implements F16 (#72): ProcessModelAdapter::invoke now 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

  • macOS: /usr/bin/sandbox-exec with a deny-default Seatbelt profile — read-only runtime + executable, one private writable scratch dir, no network. Refuses if sandbox-exec is absent.
  • 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.
  • Both: env_clear, verified-byte re-hash immediately before spawn (refuse on replacement), fresh process group + killpg on timeout/refusal so all descendants die, bounded stdout/stderr (8 MiB / 64 KiB), honored timeout including pipe-holding descendants.
  • Windows / other platforms: keep returning PROCESS_EXECUTION_UNAVAILABLE.
  • RecordedResponseAdapter unchanged.

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. See docs/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).
  • Full cargo test: green via pre-commit hook (3377 lib + integration).
  • Linux verified in rust:1.90-bookworm container (--security-opt seccomp=unconfined): confinement, run CLI, pr155, prepare, adversarial, isolation suites all pass; my files clippy-clean.

Ticket: #72

@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 985136ae-965b-4bcf-8d47-5f4963db655d
Commits

Reviewing files that changed from the base of the PR and between a0257c2 and 89377fd.

Files ignored due to path filters: 1

  • docs/local-model-confinement.md is excluded by !docs/**
Files selected for processing: 11

  • CHANGELOG.md
  • src/cli/mod.rs
  • src/suggest/adapter.rs
  • src/suggest/confinement.rs
  • src/suggest/mod.rs
  • src/suggest/request.rs
  • src/suggest/run.rs
  • tests/pr155_adversarial_review.rs
  • tests/suggest_confinement_test.rs
  • tests/suggest_prepare_cli_test.rs
  • tests/suggest_run_cli_test.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

Copy link
Copy Markdown
Collaborator Author

Code review: F16 Unix confinement — Request changes

Reviewed src/suggest/confinement.rs, the adapter and CLI wiring, the tests and docs/local-model-confinement.md. Items marked unverified are from reading the code and could not be exercised in my environment.

Blocking

  1. Linux does not compile under clippy (confirmed in CI). Test (ubuntu-latest) fails with 15 cast_possible_truncation errors in src/suggest/confinement.rs (e.g. libc::SYS_socket as u32, SYS_socketpair, SYS_io_uring_*, around lines 645–649). The PR notes clippy was run on macOS only, so Linux-gated code was never linted and no Linux confinement test has run in CI. Use u32::try_from(...) or a justified #[allow].

  2. Timeout can hang (high confidence). After a timeout supervise calls kill_group and then stdout_reader.join() / stderr_reader.join() with no bound. A descendant that leaves the process group (setsid/setpgid, both allowed by the sandbox) and keeps the pipe open is not reached by killpg, so the join blocks indefinitely. This contradicts "descendants holding the pipes open cannot extend [the timeout]". Bound the drain with a deadline (non-blocking reads or detached readers) instead of an unbounded join.

Should fix before relying on the boundary

  1. macOS: "no network" is likely not airtight (unverified). The Seatbelt profile allows mach*, process* and signal. Mach lookups to system services (e.g. DNS via mDNSResponder) and LaunchServices open are well-known ways around a network-denied profile, and (allow signal) is not limited to the child's own group. Please narrow mach-lookup to an explicit list, restrict signal, add DNS and open probes to the tests, and soften the "no network access" wording in the preview notice (request.rs) and docs until verified.

  2. Linux: deny-list seccomp leaves same-user attack surface (unverified). Only socket, socketpair and io_uring_* are denied. ptrace, process_vm_readv/writev, pidfd_* and kill are allowed; depending on kernel.yama.ptrace_scope, the child may be able to act on same-user processes outside the Landlock domain. Consider denying those syscalls (or moving to an allow-list).

  3. Stale-PID killpg. try_wait reaps the child; kill_group later calls raw libc::killpg(child.id()) on the over-bound/drain paths. If the group has emptied, the PID may be recycled. Only signal while the child is unreaped, or record that it was reaped.

  4. "Verified-byte execution" is hash-then-spawn by path. The bytes are re-read and hashed, then Command::new(path) is executed (and Landlock re-opens the path), so there is a short replace window. Either execute from the hashed descriptor (fexecve/execveat on an fd) or state the residual window in the docs.

Minor

  • pre_exec allocates (CString::new, format!, Vec) after fork; not async-signal-safe by Rust's documentation.
  • The PATH-substitution regression test (bare_adapter_name_cannot_bypass…) was replaced rather than kept; the new test no longer covers that case.
  • The data-handling notice now asserts "no network access" unconditionally for macOS and Linux.

What looks good

Fail-closed on missing mechanisms, env_clear, scratch directory lifecycle, bounded output, x32/foreign-ABI kill guards, and the docs are candid about CPU/memory not being limited.


Generated by Claude Code

@brianluby
brianluby marked this pull request as ready for review November 14, 2026 22:15
Copilot AI balanced review requested due to automatic review settings November 14, 2026 22:15
@brianluby

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copilot AI 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.

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::invoke through 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

Comment thread src/suggest/confinement.rs Outdated
Comment on lines +160 to +162
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();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

Comment thread src/suggest/confinement.rs Outdated
Comment on lines +329 to +333
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)?;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

Comment thread src/suggest/request.rs
Comment on lines 38 to 41
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. \

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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).

Comment thread tests/suggest_confinement_test.rs Outdated
Comment on lines +107 to +115
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();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.
@brianluby
brianluby force-pushed the feat/f16-unix-confinement branch from 663b108 to 89377fd Compare November 14, 2026 23:39
@brianluby
brianluby merged commit 7b95873 into main Nov 14, 2026
8 checks passed
@brianluby
brianluby deleted the feat/f16-unix-confinement branch November 14, 2026 23:41
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.

2 participants