Repository navigation
fix(sandbox): prebuild seccomp filters before fork and report Landlock degradation from the parent - #5216
fix(sandbox): prebuild seccomp filters before fork and report Landlock degradation from the parent#5216EmilienM wants to merge 4 commits into
Conversation
|
/ok to test 0488fdb |
0488fdb to
f9e08dd
Compare
|
Label |
|
/ok to test f9e08dd |
f9e08dd to
f3c078f
Compare
|
/ok to test f3c078f |
FrostGod
left a comment
There was a problem hiding this comment.
Looks good overall, nothing blocking. Could you rebase? The base (9e004a746) is ~4 weeks old, so CI ran against old main.
Tested merged with current main on a Landlock-capable VM: the 71 targeted tests pass and clippy is clean. The full sandbox suite shows the same 7 env failures as main.
f3c078f to
8e1da5a
Compare
|
Rebased onto current main and addressed all four comments. The child-side error path now returns errno-based io::Errors end to end (harden_child_process, seccomp install, landlock enforce, ChildSandboxInstall), so std's pre-exec pipe carries the real errno instead of the EINVAL fallback; the drain renders it via io::Error::from_raw_os_error; and the Landlock-guarded tests this branch adds announce their skips. The SAFETY comments now state what std actually does: forward the raw errno, never allocate. Sandbox suite, server suite, clippy, and fmt are green on the new head. CI needs a fresh /ok to test. |
|
/ok to test 8e1da5a |
|
The k3s-ha conformance failure on the latest run looks like a flake. I can't rerun the failed jobs from the fork, so this needs a rerun (or another /ok to test) when someone with access has a moment. |
The workload pre_exec hooks ran in the forked child of a multithreaded process and compiled both seccomp filters there: BTreeMap rule building, seccompiler BPF compilation, and a debug! callsite whose output landed on the workload's own pipe or PTY. Post-fork allocation and tracing can deadlock when another thread holds the allocator or subscriber lock at fork time. Split seccomp::apply into a parent-side prepare(policy) that compiles the main and compatibility filters into BpfPrograms (with the socket-domain debug log moved into it), and a non-allocating PreparedSeccomp::install for the child that keeps set_no_new_privs and the filter order verbatim (compatibility first, then main). PreparedSandbox now carries the prebuilt programs instead of the SandboxPolicy it kept only so the child could rebuild them, so child-side compilation cannot be reintroduced. Bundle the prepared sandbox and the child self-protection program into a ChildSandboxInstall struct that replaces the cfg-gated positional parameters of pty::install_pre_exec* and is consumed by an install-only enforce in the forked child. seccomp::apply is removed with no remaining caller; the fresh-process paths keep using prepare plus install through sandbox::linux::enforce. Add a behavioral fork test that compiles the filters parent-side, installs them in the forked child in the enforce_capability_free order, and probes one role-specific outcome per filter (memfd_create EPERM for the main filter, pidfd_open ENOSYS for the compatibility filter, close_range with CLOSE_RANGE_UNSHARE EPERM for the child self-protection filter). Part of ai-openshell#5180 Signed-off-by: Emilien Macchi <emacchi@redhat.com>
On a best-effort Landlock failure, landlock::enforce emitted an OCSF DetectionFinding from the forked child between fork and exec, where format! allocations and the OCSF thread-local plus tracing dispatch can deadlock after fork of a multithreaded process, and where the emission was already unreliable because the child has no working tracing subscriber. Return the failure from landlock::enforce as a LandlockOutcome instead: Enforced, or Degraded with a cause extracted without allocation (the restrict_self errno, or an explicit NoErrno sentinel when the error chain carries no io::Error; the landlock crate's EINVAL-defaulting helper is deliberately avoided so the sentinel cannot conflate with a genuine EINVAL). hard_requirement failures keep returning Err through the existing spawn-error path. Spawn surfaces create a LandlockReportPipe (both ends CLOEXEC) before fork. The pre_exec closure captures only the raw write fd, never an OwnedFd: the Command moves onto the serialized workload launcher thread, and an owned write end dropped there would hold EOF hostage to launcher-thread scheduling. The forked child writes one fixed-size, async-signal-safe record per degraded ruleset before installing the child self-protection filter, so the write cannot block on the seccomp broker; the parent drops its own write end right after spawn returns (on success or failure) and drains to EOF on the spawning thread, emitting one finding per record through a builder shared with the fresh-process enforce path. Best-effort spawns still succeed. The report fd is a required parameter of ChildSandboxInstall, so a spawn surface that forgets to plumb it is a compile error. The finding keeps its uid, title, and High severity; the message and description are now errno-based because the child cannot format the landlock error across the pipe. Part of ai-openshell#5180 Signed-off-by: Emilien Macchi <emacchi@redhat.com>
The SAFETY comments on the workload pre_exec hooks claimed that "all allocations and policy compilation happened before spawn" and that the process.rs hook only calls setpgid and setns — none of which matched the code, which compiled both seccomp filters and emitted OCSF from the forked child (and never called setns at all). Rewrite the three comments to describe what the hooks actually do now that the filters are prebuilt and best-effort Landlock failures cross to the parent as fixed-size pipe records: raw session/TTY syscalls and installation of parent-prebuilt sandbox state, with the success and degradation paths free of allocation, tracing, and OCSF emission. State the remaining caveat precisely — only a hard error return may allocate, like std's own pre_exec error reporting — instead of overclaiming. Closes ai-openshell#5180 Signed-off-by: Emilien Macchi <emacchi@redhat.com>
The pre_exec hooks converted child-side failures with io::Error::other(err.to_string()), which allocates between fork and exec and collapses to std's EINVAL fallback: std reports a pre_exec error across its CLOEXEC pipe by raw errno only, so neither the formatted message nor the real errno ever reached the parent. The child-side failure path now returns errno-based io::Errors end to end. harden_child_process, PreparedSeccomp::install, landlock::enforce, enforce_capability_free, and ChildSandboxInstall::enforce return std::io::Result and map each failure to its innermost kernel errno, which the rustix, prctl, seccompiler, and landlock errors all carry. The pre_exec SAFETY comments now describe std's actual error transport instead of claiming it allocates. Part of ai-openshell#5180 Signed-off-by: Emilien Macchi <emacchi@redhat.com>
Head branch was pushed to by a user without write access
8e1da5a to
2f0e014
Compare
Summary
The workload pre_exec hooks compiled both seccomp filters and logged from the forked child of a multithreaded process, where allocation and tracing can deadlock if another thread holds the lock at fork time. The child now only installs prebuilt state: both filters are compiled in the parent, and best-effort Landlock failures cross to the parent over a CLOEXEC pipe before the finding is emitted.
Related Issue
Closes #5180
Changes
seccomp::applyinto a parent-sideprepare()and a non-allocatinginstall()for the child;PreparedSandboxcarries the compiledBpfPrograms instead of the policy, so child-side compilation cannot be reintroducedChildSandboxInstall; all three spawn surfaces run onespawn_and_drainhelper instead of copying the choreography by handlandlock::enforcereturns a degradation outcome instead of emitting OCSF from the child. The child writes one fixed-size record per degraded ruleset before any seccomp filter is installed, and the parent drains the pipe and emits thelandlock-enforce-failedfindingio::Errors —harden_child_process,PreparedSeccomp::install,landlock::enforce,enforce_capability_free, andChildSandboxInstall::enforceall returnio::Result— so std's pre-exec pipe carries the real errno to the parent instead of the EINVAL fallback it uses for non-os errorsRelease Notes
Workload sandbox seccomp filters are now compiled before fork instead of in the forked child. Landlock best-effort failures are now reported reliably from the parent process, once per degraded ruleset per spawn; the OCSF message for failures seen through a spawned workload is errno-based rather than the full error text, and a hard pre_exec failure now surfaces the real errno instead of EINVAL.
Testing
cargo test -p openshell-sandbox(381 lib tests, including new fork and re-exec tests for filter installation, pipe transport, and finding emission),cargo test -p openshell-server --features test-support(3001 tests), clippy and fmt clean. Thelandlock,bypass_detection, andcore_dump_hardeninge2e suites plus conformance pass against a Podman gateway.Checklist