From 19e6c4aeca9818c4b288b09911dfa4be9a831236 Mon Sep 17 00:00:00 2001 From: Paul Moore Date: Wed, 6 May 2015 15:36:04 -0400 Subject: tests: test the bpf accumulator checking logic When building the BPF filter code we need to ensure that we take the state of the BPF state machine accumulator into account. This test creates a situation where the BPF filter code generator needs to perform some extra work to ensure the accumulator state is correct. This test is based on a bug reproducer by Matthew Heon. Signed-off-by: Paul Moore (imported from commit 4992bc217387c44dfbd9a4d290cdc42ba098b124) --- tests/27-sim-bpf_blk_state.c | 103 +++++++++++++++++++++++++++++++++++++++ tests/27-sim-bpf_blk_state.py | 53 ++++++++++++++++++++ tests/27-sim-bpf_blk_state.tests | 24 +++++++++ tests/Makefile.am | 3 +- 4 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 tests/27-sim-bpf_blk_state.c create mode 100755 tests/27-sim-bpf_blk_state.py create mode 100644 tests/27-sim-bpf_blk_state.tests diff --git a/tests/27-sim-bpf_blk_state.c b/tests/27-sim-bpf_blk_state.c new file mode 100644 index 0000000..39c53dd --- /dev/null +++ b/tests/27-sim-bpf_blk_state.c @@ -0,0 +1,103 @@ +/** + * Seccomp Library test program + * + * Copyright (c) 2015 Red Hat + * Author: Paul Moore + */ + +/* + * This library is free software; you can redistribute it and/or modify it + * under the terms of version 2.1 of the GNU Lesser General Public License as + * published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, see . + */ + +#include +#include + +#include + +#include "util.h" + +int main(int argc, char *argv[]) +{ + int rc; + struct util_options opts; + scmp_filter_ctx ctx = NULL; + + rc = util_getopt(argc, argv, &opts); + if (rc < 0) + goto out; + + ctx = seccomp_init(SCMP_ACT_ALLOW); + if (ctx == NULL) + return ENOMEM; + + rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1, + SCMP_A0(SCMP_CMP_EQ, 3)); + if (rc != 0) + goto out; + rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1, + SCMP_A0(SCMP_CMP_EQ, 4)); + if (rc != 0) + goto out; + rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1, + SCMP_A0(SCMP_CMP_EQ, 5)); + if (rc != 0) + goto out; + rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1, + SCMP_A0(SCMP_CMP_EQ, 6)); + if (rc != 0) + goto out; + rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1, + SCMP_A0(SCMP_CMP_EQ, 7)); + if (rc != 0) + goto out; + rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1, + SCMP_A0(SCMP_CMP_EQ, 8)); + if (rc != 0) + goto out; + rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1, + SCMP_A0(SCMP_CMP_EQ, 9)); + if (rc != 0) + goto out; + rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1, + SCMP_A0(SCMP_CMP_EQ, 11)); + if (rc != 0) + goto out; + rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1, + SCMP_A0(SCMP_CMP_EQ, 12)); + if (rc != 0) + goto out; + rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1, + SCMP_A0(SCMP_CMP_EQ, 13)); + if (rc != 0) + goto out; + rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1, + SCMP_A0(SCMP_CMP_EQ, 14)); + if (rc != 0) + goto out; + rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1, + SCMP_A0(SCMP_CMP_EQ, 15)); + if (rc != 0) + goto out; + rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1, + SCMP_A0(SCMP_CMP_GE, 16)); + if (rc != 0) + goto out; + + rc = util_filter_output(&opts, ctx); + if (rc) + goto out; + +out: + seccomp_release(ctx); + return (rc < 0 ? -rc : rc); +} diff --git a/tests/27-sim-bpf_blk_state.py b/tests/27-sim-bpf_blk_state.py new file mode 100755 index 0000000..647c549 --- /dev/null +++ b/tests/27-sim-bpf_blk_state.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +# +# Seccomp Library test program +# +# Copyright (c) 2015 Red Hat +# Author: Paul Moore +# + +# +# This library is free software; you can redistribute it and/or modify it +# under the terms of version 2.1 of the GNU Lesser General Public License as +# published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this library; if not, see . +# + +import argparse +import sys + +import util + +from seccomp import * + +def test(args): + f = SyscallFilter(ALLOW) + f.add_rule_exactly(KILL, "socket", Arg(0, EQ, 3)) + f.add_rule_exactly(KILL, "socket", Arg(0, EQ, 4)) + f.add_rule_exactly(KILL, "socket", Arg(0, EQ, 5)) + f.add_rule_exactly(KILL, "socket", Arg(0, EQ, 6)) + f.add_rule_exactly(KILL, "socket", Arg(0, EQ, 7)) + f.add_rule_exactly(KILL, "socket", Arg(0, EQ, 8)) + f.add_rule_exactly(KILL, "socket", Arg(0, EQ, 9)) + f.add_rule_exactly(KILL, "socket", Arg(0, EQ, 11)) + f.add_rule_exactly(KILL, "socket", Arg(0, EQ, 12)) + f.add_rule_exactly(KILL, "socket", Arg(0, EQ, 13)) + f.add_rule_exactly(KILL, "socket", Arg(0, EQ, 14)) + f.add_rule_exactly(KILL, "socket", Arg(0, EQ, 15)) + f.add_rule_exactly(KILL, "socket", Arg(0, GE, 16)) + return f + +args = util.get_opt() +ctx = test(args) +util.filter_output(args, ctx) + +# kate: syntax python; +# kate: indent-mode python; space-indent on; indent-width 4; mixedindent off; diff --git a/tests/27-sim-bpf_blk_state.tests b/tests/27-sim-bpf_blk_state.tests new file mode 100644 index 0000000..dd72b05 --- /dev/null +++ b/tests/27-sim-bpf_blk_state.tests @@ -0,0 +1,24 @@ +# +# libseccomp regression test automation data +# +# Copyright (c) 2015 Red Hat +# Author: Paul Moore