Add add-seccomp combinator
https://lists.sr.ht/~alexdavid/general/patches/62972
This commit is contained in:
commit
0a6da614ea
5 changed files with 155 additions and 0 deletions
|
|
@ -22,6 +22,7 @@ let
|
||||||
add-path = ./combinators/add-path.nix;
|
add-path = ./combinators/add-path.nix;
|
||||||
add-pkg-deps = ./combinators/add-pkg-deps.nix;
|
add-pkg-deps = ./combinators/add-pkg-deps.nix;
|
||||||
add-runtime = ./combinators/add-runtime.nix;
|
add-runtime = ./combinators/add-runtime.nix;
|
||||||
|
add-seccomp = ./combinators/add-seccomp.nix;
|
||||||
base = ./combinators/base.nix;
|
base = ./combinators/base.nix;
|
||||||
bind-nix-store-runtime-closure = ./combinators/bind-nix-store-runtime-closure.nix;
|
bind-nix-store-runtime-closure = ./combinators/bind-nix-store-runtime-closure.nix;
|
||||||
bind-pkg = ./combinators/bind-pkg.nix;
|
bind-pkg = ./combinators/bind-pkg.nix;
|
||||||
|
|
|
||||||
50
lib/combinators/add-seccomp.nix
Normal file
50
lib/combinators/add-seccomp.nix
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
{
|
||||||
|
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
|
||||||
|
)
|
||||||
|
))
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
@ -44,6 +44,7 @@ let
|
||||||
deferredPermissions = [ ]; # See defer combinator
|
deferredPermissions = [ ]; # See defer combinator
|
||||||
additionalRuntimeClosures = [ ]; # See bind-nix-store-runtime-closure
|
additionalRuntimeClosures = [ ]; # See bind-nix-store-runtime-closure
|
||||||
dbusPermissions = [ ]; # See dbus combinator
|
dbusPermissions = [ ]; # See dbus combinator
|
||||||
|
seccompPermissions = [ ]; # See add-seccomp combinator
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
lib.pipe initialState (
|
lib.pipe initialState (
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ test-suite spec
|
||||||
DBusCombinatorsSpec
|
DBusCombinatorsSpec
|
||||||
JailSpec
|
JailSpec
|
||||||
OverlaySpec
|
OverlaySpec
|
||||||
|
SeccompSpec
|
||||||
TestPrelude
|
TestPrelude
|
||||||
Paths_jail_nix_tests
|
Paths_jail_nix_tests
|
||||||
hs-source-dirs:
|
hs-source-dirs:
|
||||||
|
|
|
||||||
102
tests/spec/SeccompSpec.hs
Normal file
102
tests/spec/SeccompSpec.hs
Normal file
|
|
@ -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 <seccomp.h>
|
||||||
|
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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue