Skip to content

fix(sandbox): prebuild seccomp filters before fork and report Landlock degradation from the parent - #5216

Open
EmilienM wants to merge 4 commits into
ai-openshell:mainfrom
EmilienM:fix/5180-preexec-seccomp-prebuild/EmilienM
Open

EmilienM wants to merge 4 commits into
ai-openshell:mainfrom
EmilienM:fix/5180-preexec-seccomp-prebuild/EmilienM

Conversation

@EmilienM

@EmilienM EmilienM commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Split seccomp::apply into a parent-side prepare() and a non-allocating install() for the child; PreparedSandbox carries the compiled BpfPrograms instead of the policy, so child-side compilation cannot be reintroduced
  • Bundle the prepared sandbox, the child self-protection program, and the report fd into ChildSandboxInstall; all three spawn surfaces run one spawn_and_drain helper instead of copying the choreography by hand
  • landlock::enforce returns 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 the landlock-enforce-failed finding
  • The fresh-process path reports the full landlock error chain. Failures observed through a spawned workload are errno-based, since the child cannot format between fork and exec; uid, title, and severity are unchanged
  • The child-side hard-error path returns errno-based io::Errors — harden_child_process, PreparedSeccomp::install, landlock::enforce, enforce_capability_free, and ChildSandboxInstall::enforce all return io::Result — so std's pre-exec pipe carries the real errno to the parent instead of the EINVAL fallback it uses for non-os errors
  • Rewrote the three pre_exec SAFETY comments to match what the hooks actually do

Release 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

  • Checks appropriate to the affected code and behavior pass
  • Unit tests added/updated (if applicable)
  • E2E tests added/updated (if applicable)

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. The landlock, bypass_detection, and core_dump_hardening e2e suites plus conformance pass against a Podman gateway.

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)
  • Architecture docs updated (if applicable)

@copy-pr-bot

copy-pr-bot Bot commented Nov 3, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@EmilienM
EmilienM marked this pull request as ready for review November 3, 2026 20:03
@mrunalp

mrunalp commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

/ok to test 0488fdb

@EmilienM
EmilienM force-pushed the fix/5180-preexec-seccomp-prebuild/EmilienM branch from 0488fdb to f9e08dd Compare November 4, 2026 03:17
@mrunalp mrunalp added retired:area:sandbox Retired; do not apply. See #5151 for item-specific migration to the current taxonomy. test:e2e Requires end-to-end coverage labels Nov 4, 2026
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Label test:e2e applied, but pull-request/5216 is at 0488fdb while the PR head is f9e08dd. A maintainer needs to comment /ok to test f9e08ddf3bd62ea9a8ad494db06f724fa78e72a7 to refresh the mirror. Once the mirror catches up, re-run Branch E2E Checks from the Actions tab.

@mrunalp

mrunalp commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

/ok to test f9e08dd

@EmilienM
EmilienM force-pushed the fix/5180-preexec-seccomp-prebuild/EmilienM branch from f9e08dd to f3c078f Compare November 9, 2026 21:19
@purp purp added area:runtime Sandbox execution, isolation, supervision, policy enforcement, networking, and provider lifecycle. topic:reliability High availability, recovery, retries, graceful degradation, and correctness across restarts. topic:security Vulnerability management, security analysis/auditing, bypass detection, and abuse prevention. and removed retired:area:sandbox Retired; do not apply. See #5151 for item-specific migration to the current taxonomy. labels Nov 15, 2026
@FrostGod

Copy link
Copy Markdown
Contributor

/ok to test f3c078f

@purp purp added topic:security Vulnerability management, security analysis/auditing, bypass detection, and abuse prevention. and removed topic:security Vulnerability management, security analysis/auditing, bypass detection, and abuse prevention. labels Nov 30, 2026
@purp purp added meta:labeler-checkpoint Temporary label used by /label-issue to record a verified classification checkpoint. and removed meta:labeler-checkpoint Temporary label used by /label-issue to record a verified classification checkpoint. labels Nov 30, 2026

@FrostGod FrostGod 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.

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.

Comment thread crates/openshell-sandbox/src/process.rs Resolved
Comment thread crates/openshell-sandbox/src/process.rs Resolved
@EmilienM
EmilienM force-pushed the fix/5180-preexec-seccomp-prebuild/EmilienM branch from f3c078f to 8e1da5a Compare December 1, 2026 15:10
@EmilienM

EmilienM commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

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.

@FrostGod

FrostGod commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

/ok to test 8e1da5a

@FrostGod FrostGod 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.

LGTM

@FrostGod
FrostGod enabled auto-merge December 2, 2026 05:09
@FrostGod
FrostGod disabled auto-merge December 2, 2026 05:09
@FrostGod
FrostGod enabled auto-merge December 2, 2026 22:57
@EmilienM

EmilienM commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

The k3s-ha conformance failure on the latest run looks like a flake. lifecycle::stopped_sandbox_can_be_deleted failed with create sandbox failed: sandbox not found: the gateway wrapping a Kubernetes API 404 from the compute driver mid-create (compute/mod.rs:1773). In the run's diagnostics, that sandbox's PVC never got past WaitForFirstConsumer, so no pod was ever scheduled and the preexec path this PR changes never ran. The same suite passed on the other five lanes (including the other k3s-ha on ipv6), and 74/75 passed on this lane.

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.

@FrostGod
FrostGod disabled auto-merge December 3, 2026 00:58
@FrostGod
FrostGod enabled auto-merge December 3, 2026 00:58
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>
auto-merge was automatically disabled December 3, 2026 01:15

Head branch was pushed to by a user without write access

@EmilienM
EmilienM force-pushed the fix/5180-preexec-seccomp-prebuild/EmilienM branch from 8e1da5a to 2f0e014 Compare December 3, 2026 01:15
@FrostGod
FrostGod enabled auto-merge December 3, 2026 01:31

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:runtime Sandbox execution, isolation, supervision, policy enforcement, networking, and provider lifecycle. test:e2e Requires end-to-end coverage topic:reliability High availability, recovery, retries, graceful degradation, and correctness across restarts. topic:security Vulnerability management, security analysis/auditing, bypass detection, and abuse prevention.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: workload pre_exec hooks compile the seccomp filter and log after fork

4 participants