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 (