Skip to content

Linux: record seccomp refusals as kernel_refused telemetry - #101

Open
mufaddalq wants to merge 12 commits into
strands-agents:mainfrom
mufaddalq:fix/33-kernel-refusal-telemetry
Open

mufaddalq wants to merge 12 commits into
strands-agents:mainfrom
mufaddalq:fix/33-kernel-refusal-telemetry

Conversation

@mufaddalq

@mufaddalq mufaddalq commented Oct 11, 2026 •

Copy link
Copy Markdown

Description

New feature, with interface changes approved in design. On Linux, a call the seccomp filter refuses now leaves a telemetry record. The boundary does not change: every call refused today is still refused. It gets EPERM as before, or ENOSYS if the box itself has gone away.

Today a workload that opens an AF_PACKET socket, maps W|X memory, or calls bpf, ptrace, or mount is refused silently. Only enforcement-point decisions reach the decision log, so a review of a session cannot answer "did the code try to open a raw socket?".

How it works:

  1. One observed program. SyscallPolicy::compile builds a third program beside the existing pair. It runs permit and then restrictions. Each ALLOW in permit becomes a jump into restrictions, and each ERRNO(EPERM) becomes USER_NOTIF. It is installed last, with SECCOMP_FILTER_FLAG_NEW_LISTENER. Two notified filters cannot work, because a filter chain holds one listener (EBUSY otherwise) and ERRNO outranks USER_NOTIF. Both facts were measured on kernel 6.18. A cBPF interpreter test runs the spliced program and the pair against every arm64 syscall number, and against the edge arguments of each argument-scoped rule. It checks that the spliced program notifies exactly where the pair answers EPERM.
  2. PID 1 forwards the listener. Once filtered, the workload cannot sendmsg. So the namespace's PID 1 copies the listener out of the workload with pidfd_getfd, after the workload drops its capabilities, and sends it to the box over the existing --relay-control-fd socket. No process in the box holds the listener or the relay descriptor after exec.
  3. Fallback, not refusal. PID 1 first checks that the copy will work. If it won't, as under Yama scope 2 or 3, or if the kernel refuses the observed install, the workload gets today's two ERRNO filters, byte-identical to main (pinned by golden digests on arm64). One stderr line and one refusals_unobserved control record say so. A failure after a successful install refuses the apply instead, because nothing can be stacked over the observed filter.
  4. The box always answers EPERM. One thread per launch answers every notification through a single builder, which can produce nothing else and never CONTINUE. It names the syscall and decodes the scalar arguments of the argument-scoped rules, for example family=AF_PACKET type=SOCK_RAW. It reads /proc/<pid>/exe and cmdline best effort. When the notification id goes stale, it drops what /proc returned but still records the call.
  5. Rate limited per box. The key is (executable, syscall, arguments). The first refusal under a key is recorded at once, and repeats are counted for 10 s. At most 256 keys are tracked. Past the cap, a new key is counted under its syscall alone, so arguments the workload picks cannot hide a later kind of call. Records plus their suppressed counts always add up to the number of refused calls, and pending counts flush when the box stops.

Interface changes:

  • A new signal kernel_refused: event strands.box.containment.refusal, scope strands-box.containment, log only, not a decision. Its keys are strands.box.containment.{mechanism,syscall,arguments,errno,suppressed}, process.pid, and the redacted process.command / process.command_args.
  • A new include word kernel. A target that names no words receives it, so the default file target gets these records with no config change.
  • A new control operation refusals_unobserved. Its reason is the errno name, or rate_cap when the key cap is reached. It reaches a target through trace, not kernel.
  • Following the per-feature versioning main adopted in feat(box): add opt-in policy checks for native file operations on macOS #112/feat(box): choose native file checks for each workload #113, only a box whose telemetry includes kernel is stored at record version 24, so a build that predates the word refuses it. Every other box keeps the version main gives it (21, 22, or 23). No longer collides with feat(containment): share the container's /proc on masked hosts via [containment] private_proc #58, which uses 25.
  • On the crate façades: a containment::refusal module (the listener, the refusal description, and the handoff reader), and Collector::refusal / RefusalRecord in telemetry.
  • The --relay-control-fd contract: after the egress listeners comes one last message, the listener (O) or the fallback errno (U). The containment README describes it.

decisions.md gains the entry "A kernel refusal is observed, not decided". It records why the record is not a decision, the single spliced filter, why falling back is a deliberate exception to "every failed apply refuses the workload", the never-CONTINUE rule, and the rate-limit numbers.

Costs and residuals (limitations.md):

  • Each refused call now waits for the box's answer, about 60 µs on arm64 against about 1 µs before. A permitted call costs nothing more. Startup is unchanged within box-bench's noise (interleaved runs: main 330 and 323 ms, this branch 343 and 315 ms). Found while testing: CPython's startup makes one refused mremap, and glibc's realloc of a large block tries mremap first. Programs that retry a refused call pay the 60 µs each time.
  • Refusals made by the view (ENOENT, EROFS) and by the network namespace (ENETUNREACH) are still unobserved.
  • An interrupted refused call can return EINTR or restart, instead of returning EPERM at once.

Out of scope (follow-ups):

CI workflow change: the Linux leg of containment-deterministic.yml now installs uv's python-build-standalone 3.12, as rust-test-lint.yml already does on macOS. The new CN-K cases drive refused syscalls from CPython, and the harness refuses /usr/bin/python3.

For maintainers: as an outside contributor, my CI and Auto Strands Review runs wait for approval (action_required / manual-approval). Could someone approve them? Thanks.

Related Issues

Part of #33: Linux seccomp. The issue stays open for macOS Seatbelt.
Related: #21 / #40 (x86_64)

Type of Change

New feature

Testing

  • Rebased onto main at 069c39c on 2026-10-19; fork CI on the rebased head: run. The run below is from before the rebase.
  • Fork CI (run): every job green, including Unit Tests on ubuntu-latest (x86_64) and macos-latest, both Containment / Deterministic legs (CN-K-01..03 run and pass on ubuntu-24.04-arm), Lint, and Verdict. The first run (38091871145) failed twice, both fixed in 58fa0ae:
    • The golden-digest test ran on x86_64 with arm64 digests. It now runs on aarch64 only.
    • The Linux det leg had no CPython the harness accepts. It now installs one, as described above.
  • Containment (refusal_notify_linux.rs, 6 tests, on privileged and on unprivileged hosts with masked /proc): a refused call answers EPERM and reaches the listener; the workload holds neither the listener nor the relay descriptor; a closed listener answers ENOSYS; a refused observed install and a refused pidfd_getfd each fall back to the refusing pair; a send failure after the install refuses the apply. Unit tests cover the splice, a well-formed program, byte-identical fallback filters, the response builder, and the decoders.
  • Telemetry: the record names the call and carries no verdict; an unlisted call has no arguments key; the seventh signal's spelling, slot, and severity; a refusal reaches a file target under the containment scope; a target without kernel gets none.
  • Box: the kernel word parses, round-trips, and is listed when an include word is misspelled. The limiter and recorder tests cover first-hit records, windowed counts, the sum invariant (also past the cap), per-syscall counting past the cap (300 junk setpgid keys do not hide a later bpf), stale notifications, the fallback and rate_cap records, and a watcher that stops within 2 s.
  • Deterministic cases:
    • CN-K-01: socket(AF_PACKET) and an unlisted call each leave a record.
    • CN-K-02: 100 000 refused calls end with at most 2 records, and the counts add up to 100 000.
    • CN-K-03: include = ["deny"] gets no refusal record.
    • Forcing the fallback filters makes CN-K-01 and CN-K-02 fail.
  • Linux aarch64 locally (finch, rust:1-bookworm):
    • strands-box-containment: all tests pass.
    • strands-box-telemetry: 86/86.
    • strands-box unit tests: 591 pass. The 2 failures are the run::hardening tests, which fail the same way on main in that environment.
    • strands-box integration suites: the failures match main's in that environment.
  • I ran cargo fmt --all and cargo clippy --all-targets --all-features -- -D warnings on strands-box-containment, strands-box, and strands-box-telemetry. The one lint left, chunks_exact in view.rs, also fires on main.
  • Not run: x86_64 locally (CI covers it) and Kata.

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs. The change is one feature, about 3.1k lines, much of it tests. macOS is split into its own PR.
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • My changes generate no new warnings

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

🤖 Generated with Claude Code

muffadalq and others added 12 commits October 19, 2026 13:08
## Problem

On Linux the syscall filter answers every refusal with ERRNO(EPERM), and nothing records it (strands-agents#33).
A workload that opens an AF_PACKET socket, maps W|X memory, or calls bpf, ptrace, or mount is
refused without a trace. Only enforcement-point decisions reach the decision log. A security review
of a session cannot tell whether the code tried to open a raw socket.

## Solution

Compile one more program beside the permit/restrictions pair. It runs permit and then restrictions,
with each permit ALLOW jumping into restrictions and each ERRNO(EPERM) rewritten to USER_NOTIF.
Install it last with SECCOMP_FILTER_FLAG_NEW_LISTENER. A filter chain holds one listener (EBUSY
otherwise), and ERRNO outranks USER_NOTIF, so two notified filters could not work. Both were
measured on kernel 6.18.

The workload cannot sendmsg once it is filtered, so PID 1 forwards the listener. It copies it out
of the workload with pidfd_getfd after the workload drops its caps, and sends it to the box over
the relay-control socket. PID 1 first checks that the copy will succeed. If it will not, or if the
kernel refuses the observed install, today's two ERRNO filters are installed unchanged and a
`refusals_unobserved` control record and a stderr line say so. A failure after a successful install
fails the apply closed. No process in the box keeps the listener or the relay descriptor after exec.

In the box, one thread per launch answers every notification with EPERM, through one builder that
can produce nothing else, and never CONTINUE. It names the syscall and decodes the scalar arguments
of the argument-scoped rules. It reads the process's exe and cmdline best effort. When the
notification id goes stale, it drops them but still records the call. A refused call therefore still
returns EPERM, or ENOSYS if the box is gone. Records pass a per-box limiter: the key is (exe,
syscall, arguments), with a 10 s window and 256 keys; past the cap a new key is counted under its
syscall alone, so later kinds of call keep their own record. Records and suppressed counts sum to
every call, and pending counts flush when the box stops.

The new signal is `kernel_refused` (event `strands.box.containment.refusal`, scope
`strands-box.containment`, Warn2, log only, not a decision). It is gated by a new include word
`kernel`, so a target naming no words receives it. A box whose telemetry names `kernel` is stored at record version 24; every
other box keeps its version. macOS Seatbelt, view refusals, and netns refusals stay unobserved (limitations.md).

## Tests

- syscall.rs: the_observed_program_notifies_exactly_where_the_pair_answers_eperm (a cBPF interpreter
  over every arm64 syscall number and the edge arguments of each argument-scoped rule),
  the_observed_program_is_well_formed, the_fallback_filters_are_unchanged (golden digests from main).
- refusal.rs (containment): the_response_always_refuses_with_eperm, the decoder tests, and
  the_handoff_round_trips_both_outcomes_and_eof.
- refusal_notify_linux.rs: a_refused_call_still_answers_eperm_and_reaches_the_listener,
  the_workload_holds_no_listener_and_no_relay_descriptor, a_closed_listener_answers_enosys,
  a_refused_observed_install_falls_back_to_the_refusing_filters,
  a_refused_listener_copy_falls_back_before_the_observed_install,
  a_listener_that_cannot_reach_the_box_refuses_the_apply (it fails if PID 1 answers FORWARDED on a
  send error).
- telemetry: a_refusal_record_names_the_call_and_never_a_verdict,
  an_unlisted_refusal_carries_no_arguments_key,
  the_seventh_signal_has_its_own_spelling_slot_and_severity,
  a_refusal_reaches_a_file_target_under_the_containment_scope,
  a_target_without_kernel_receives_no_refusal.
- box: the_kernel_word_parses_round_trips_and_is_listed_when_misspelled; the limiter and recorder
  tests in run/refusal.rs, including records_and_suppressed_counts_sum_to_every_call,
  past_the_cap_new_keys_are_counted_per_syscall,
  a_flood_of_distinct_arguments_does_not_hide_a_later_syscall,
  a_stale_notification_is_recorded_without_its_process,
  a_fallback_is_announced_with_its_launch_and_errno,
  the_cap_is_announced_once_and_its_count_flushed_at_stop, and
  dropping_a_watch_joins_within_two_seconds.
- det cases CN-K-01 (socket AF_PACKET and an unlisted call each leave a record), CN-K-02 (100 000
  refused calls end with at most 2 records summing to 100 000), and CN-K-03 (`include = ["deny"]`
  gets none). Forcing the fallback filters makes CN-K-01 and CN-K-02 fail.
- Ran on Linux aarch64 (finch, rust:1-bookworm), privileged and masked. strands-box-containment
  passes in full, refusal_notify_linux 6/6, and telemetry 86/86. strands-box failures are the same
  ones that fail on main there: hardening, box_shell signalled binary, box_direct_filesystem
  rename, native_egress_mcp, runtime_mcp_start, runtime_mcp_policy_staging. The det harness passes
  CN-K 3/3 and TL 8/8. macOS: containment and box unit tests pass. box-bench startup, run
  alternately from saved binaries: base 330/323 ms, this branch 343/315 ms, inside the noise.
- Found: CPython startup makes one refused mremap call (now syscall_216 in a record). Each refused
  call costs about 60 µs, against about 1 µs before.
- Not run: x86_64 (syscall names there wait for strands-agents#21) and Kata.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…doff format

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…, fall back on refusal

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
….containment

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A box whose telemetry includes `kernel` is stored at record version 24,
so a build that predates the word refuses the box by version. Every other
box keeps the version main gives it (21, 22, or 23).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

; Conflicts:
;	crates/box/src/run/contain/supervise.rs
;	crates/box/src/run/hosted.rs
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
- A refusal whose notification id went stale (the caller was interrupted or exited while /proc
  was read) is still recorded. The syscall and arguments come from the kernel; only the process
  details are dropped. Before, a workload could interrupt its own refused call and leave no trace.
  Test: a_stale_notification_is_recorded_without_its_process.
- refusal_notify_linux: a_listener_that_cannot_reach_the_box_refuses_the_apply pins that a send
  failure after the observed install refuses the apply (EACCES injected on sendmsg). It fails when
  PID 1 answers FORWARDED on that error.
- The box-level refusals_unobserved records are pinned:
  a_fallback_is_announced_with_its_launch_and_errno and
  the_cap_is_announced_once_and_its_count_flushed_at_stop.
- dropping_a_watch_joins_within_two_seconds drops on its own thread, so a hang fails the test
  instead of hanging it. two_watches_share_one_limiter is renamed to what it pins.
- PID 1 formats an unknown errno without allocating after the fork (ErrnoName), in place of a leaked
  String.
- Docs: refusals_unobserved reaches a target through `trace`, not `kernel`. limitations.md gains
  the workload-fillable 256-kind cap and the ~60 µs cost per refused call (glibc realloc's
  mremap). The five- and six-word counts are corrected, and so are the include error text and the
  trampoline's --relay-control-fd contract.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The workload picks the arguments that are part of a refusal's key. About 256 junk calls (setpgid
with distinct pids, say) filled the box-wide cap, and every later kind of call then became one
anonymous count. Past the cap, a new key is now counted under its syscall alone, windowed like any
key. So a later bpf or ptrace still gets its own record. Syscall numbers from 1024 up share one
bucket, so the coarse set stays bounded. The one rate_cap notice and the
Σ(1 + suppressed) = calls invariant are unchanged.

Tests: past_the_cap_new_keys_are_counted_per_syscall,
records_and_suppressed_counts_sum_to_every_call_past_the_cap,
a_numbered_syscall_past_the_known_range_shares_one_bucket,
a_flood_of_distinct_arguments_does_not_hide_a_later_syscall, and the updated
the_cap_is_announced_once_and_its_count_flushed_at_stop. All four fail with the old anonymous
overflow restored.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The Linux deterministic leg had no CPython the harness accepts (it refuses /usr/bin), so CN-K-01..03
failed with DET_ERROR. It now installs uv's python-build-standalone 3.12, as the unit-test leg does
on macOS. the_fallback_filters_are_unchanged holds arm64 digests, and the program differs per
arch, so it runs on aarch64 only; x86_64's lists are still being settled (strands-agents#21).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mufaddalq

Copy link
Copy Markdown
Author

Status: this branch conflicts with main (18 files, and record version 24 has since been taken), so it needs a re-port rather than a simple rebase.

I am holding that until the direction on #33 is settled. The maintainers plan to mediate some syscalls through the policy engine, which changes what this PR should cover. I have written up the implications and two options there: #33 (comment)

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant