From 23bf74dad6a0cdbd195691b45b6d759fc9d370a8 Mon Sep 17 00:00:00 2001 From: Mathias Sven Date: Sun, 19 Oct 2025 01:22:03 +0100 Subject: [PATCH 1/3] Add `add-seccomp` combinator This combinator allows for the use of bubblewrap's `--add-seccomp-fd` option. Since multiple invocations of this combinator are allowed, this commit also introduces a new state attribute `seccompPermissions`. The implementation uses `test -x` against the given argument to decide whether to use input redirection to a FD or process substitution. --- lib/combinators.nix | 1 + lib/combinators/add-seccomp.nix | 48 +++++++++++++++++++++++++++++++++ lib/jail.nix | 1 + 3 files changed, 50 insertions(+) create mode 100644 lib/combinators/add-seccomp.nix diff --git a/lib/combinators.nix b/lib/combinators.nix index 2553e7b..4d984f0 100644 --- a/lib/combinators.nix +++ b/lib/combinators.nix @@ -22,6 +22,7 @@ let add-path = ./combinators/add-path.nix; add-pkg-deps = ./combinators/add-pkg-deps.nix; add-runtime = ./combinators/add-runtime.nix; + add-seccomp = ./combinators/add-seccomp.nix; base = ./combinators/base.nix; bind-nix-store-runtime-closure = ./combinators/bind-nix-store-runtime-closure.nix; bind-pkg = ./combinators/bind-pkg.nix; diff --git a/lib/combinators/add-seccomp.nix b/lib/combinators/add-seccomp.nix new file mode 100644 index 0000000..191b471 --- /dev/null +++ b/lib/combinators/add-seccomp.nix @@ -0,0 +1,48 @@ +{ + combinators, + helpers, + ... +}: +let + inherit (combinators) + add-runtime + compose + defer + escape + include-once + ; +in +{ + sig = "String -> Permission"; + doc = '' + Adds the cBPF program as a bubblewrap argument. + + Accepts a path or a derivation that resolves to either a compiled cBPF program + or an executable that writes the program to stdout. In the latter case, + the executable will be run at each invocation of the wrapped binary at runtime. + + This combinator can be composed according to the same rules as + bubblewrap's `--add-seccomp-fd` option. + + See BWRAP(1) for more information. + ''; + impl = path: compose [ + (helpers.pushState "seccompPermissions" (escape path)) + (include-once "seccomp" ( + defer ( + state: + add-runtime '' + declare -a SECCOMP_FILTERS=(${toString (state.seccompPermissions)}) + for FILTER in "''${SECCOMP_FILTERS[@]}"; do + if [[ -x "$FILTER" ]]; then + exec {FILTER_FD}< <("$FILTER") + else + exec {FILTER_FD}<"$FILTER" + fi + RUNTIME_ARGS+=(--add-seccomp-fd "$FILTER_FD") + done + '' state + ) + )) + ]; +} diff --git a/lib/jail.nix b/lib/jail.nix index cca88da..182eca6 100644 --- a/lib/jail.nix +++ b/lib/jail.nix @@ -44,6 +44,7 @@ let deferredPermissions = [ ]; # See defer combinator additionalRuntimeClosures = [ ]; # See bind-nix-store-runtime-closure dbusPermissions = [ ]; # See dbus combinator + seccompPermissions = [ ]; # See add-seccomp combinator }; in lib.pipe initialState ( From 9857627b480a8366874e37e5350c1060e3df401e Mon Sep 17 00:00:00 2001 From: Mathias Sven Date: Sun, 19 Oct 2025 01:39:39 +0100 Subject: [PATCH 2/3] Add tests for the seccomp combinator --- tests/jail-nix-tests.cabal | 1 + tests/spec/SeccompSpec.hs | 102 +++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 tests/spec/SeccompSpec.hs diff --git a/tests/jail-nix-tests.cabal b/tests/jail-nix-tests.cabal index 2325493..ed673cc 100644 --- a/tests/jail-nix-tests.cabal +++ b/tests/jail-nix-tests.cabal @@ -16,6 +16,7 @@ test-suite spec DBusCombinatorsSpec JailSpec OverlaySpec + SeccompSpec TestPrelude Paths_jail_nix_tests hs-source-dirs: diff --git a/tests/spec/SeccompSpec.hs b/tests/spec/SeccompSpec.hs new file mode 100644 index 0000000..f10811e --- /dev/null +++ b/tests/spec/SeccompSpec.hs @@ -0,0 +1,102 @@ +module SeccompSpec (spec) where + +import System.Info (arch) +import TestPrelude +import Test.Hspec + +spec :: Spec +spec = parallel $ inTestM . before_ checkArch $ do + describe "add-seccomp" $ do + it "blocks syscalls with the given filter" $ do + nixExprWithGenBpf [i| + let + bpf = simpleBpf "SCMP_ACT_ALLOW" '' + seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(mkdir), 0); + ''; + in + jail "test" (sh "{ mkdir foo ; } &>/dev/null || echo $?") (c: [ + (c.add-seccomp bpf) + ]) + |] + `shouldOutput` "159\n" + + it "works with bpf producing executables" $ do + nixExprWithGenBpf [i| + let + bpf = simpleBpf "SCMP_ACT_ALLOW" '' + seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(mkdir), 0); + ''; + in + jail "test" (sh "{ mkdir foo ; } &>/dev/null || echo $?") (c: [ + (c.add-seccomp (lib.getExe (sh "cat ${bpf}"))) + ]) + |] + `shouldOutput` "159\n" + + it "composes multiple filters" $ do + out <- runNixDrv $ nixExprWithGenBpf [i| + let + bpfA = simpleBpf "SCMP_ACT_ALLOW" '' + seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(mkdir), 1, + SCMP_CMP(1, SCMP_CMP_EQ, 0755)); + ''; + bpfB = simpleBpf "SCMP_ACT_ALLOW" '' + seccomp_rule_add(ctx, SCMP_ACT_ERRNO(1000), SCMP_SYS(mkdir), 0); + ''; + bpfC = simpleBpf "SCMP_ACT_ALLOW" '' + seccomp_rule_add(ctx, SCMP_ACT_ERRNO(1001), SCMP_SYS(mkdir), 0); + ''; + shj' = script: (sh script).overrideAttrs (_: { + runtimeInputs = with pkgs; [ strace gnugrep ]; + bashOptions = []; + }); + in + jail "test" (sh '' + { + mkdir -m777 foo 2>&1; echo $? + mkdir -m755 foo 2>&1; echo $? + } 2>&1 || true + '') (c: [ + (c.add-seccomp bpfA) + (c.add-seccomp bpfB) + (c.add-seccomp bpfC) + ]) + |] + -- Last filter defined is prioritized (bpfC), unless a rule in a preceding + -- filter matches and has a "stricter" action (bpfA for 755 mkdir args). + liftIO $ case lines out of + [err1, status1, err2, status2] -> do + err1 `shouldEndWith` "1001" + status1 `shouldBe` "1" + err2 `shouldContain` "Bad system call" + status2 `shouldBe` "159" + _ -> expectationFailure "failed to pattern match on output" + +nixExprWithGenBpf :: String -> String +nixExprWithGenBpf expr = + [i| + let + generateBpf = code: pkgs.runCommandCC "bpf-bytecode" { + inherit code; + passAsFile = [ "code" ]; + buildInputs = [ pkgs.libseccomp ]; + } '' + $CC -x c "$codePath" -lseccomp -o generator + ./generator > $out + ''; + simpleBpf = defaultAction: code: generateBpf '' + #include + int main(void) { + scmp_filter_ctx ctx = seccomp_init(${defaultAction}); + ${code} + seccomp_export_bpf(ctx, 1); + } + ''; + in + #{expr} + |] + +checkArch :: IO () +checkArch = case arch of + "x86_64" -> return () + _ -> pendingWith $ "test not validated for arch " ++ arch From 5c3e611da7ea05144ea7498b3a75bd6601d4626a Mon Sep 17 00:00:00 2001 From: Alex David Date: Sat, 18 Oct 2025 22:44:45 -0700 Subject: [PATCH 3/3] Run fmt --- lib/combinators/add-seccomp.nix | 40 +++++++++++++++++---------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/combinators/add-seccomp.nix b/lib/combinators/add-seccomp.nix index 191b471..7012543 100644 --- a/lib/combinators/add-seccomp.nix +++ b/lib/combinators/add-seccomp.nix @@ -26,23 +26,25 @@ in See BWRAP(1) for more information. ''; - impl = path: compose [ - (helpers.pushState "seccompPermissions" (escape path)) - (include-once "seccomp" ( - defer ( - state: - add-runtime '' - declare -a SECCOMP_FILTERS=(${toString (state.seccompPermissions)}) - for FILTER in "''${SECCOMP_FILTERS[@]}"; do - if [[ -x "$FILTER" ]]; then - exec {FILTER_FD}< <("$FILTER") - else - exec {FILTER_FD}<"$FILTER" - fi - RUNTIME_ARGS+=(--add-seccomp-fd "$FILTER_FD") - done - '' state - ) - )) - ]; + impl = + path: + compose [ + (helpers.pushState "seccompPermissions" (escape path)) + (include-once "seccomp" ( + defer ( + state: + add-runtime '' + declare -a SECCOMP_FILTERS=(${toString (state.seccompPermissions)}) + for FILTER in "''${SECCOMP_FILTERS[@]}"; do + if [[ -x "$FILTER" ]]; then + exec {FILTER_FD}< <("$FILTER") + else + exec {FILTER_FD}<"$FILTER" + fi + RUNTIME_ARGS+=(--add-seccomp-fd "$FILTER_FD") + done + '' state + ) + )) + ]; }