Skip to content

Commit

Permalink
tests: add tests for the binary tree
Browse files Browse the repository at this point in the history
This commit adds tests to ensure the validity of the
binary tree and the resultant pfc and bpf output.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
  • Loading branch information
drakenclimber committed Nov 15, 2019
1 parent 6130b32 commit 2e7089f
Show file tree
Hide file tree
Showing 13 changed files with 2,067 additions and 4 deletions.
3 changes: 3 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ util.pyc
50-sim-hash_collision
51-live-user_notification
52-basic-load
53-sim-binary_tree
54-live-binary_tree
55-basic-pfc_binary_tree
3 changes: 3 additions & 0 deletions tests/13-basic-attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def test():
f.set_attr(Attr.CTL_SSB, 1)
if f.get_attr(Attr.CTL_SSB) != 1:
raise RuntimeError("Failed getting Attr.CTL_SSB")
f.set_attr(Attr.CTL_OPTIMIZE, 2)
if f.get_attr(Attr.CTL_OPTIMIZE) != 2:
raise RuntimeError("Failed getting Attr.CTL_OPTIMIZE")

test()

Expand Down
95 changes: 95 additions & 0 deletions tests/53-sim-binary_tree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Seccomp Library test program
*
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
* Author: Tom Hromatka <tom.hromatka@oracle.com>
*/

/*
* 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 <http://www.gnu.org/licenses>.
*/

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <seccomp.h>

#include "util.h"

#define MAX_SYSCALL (330)

#include <stdio.h>

int main(int argc, char *argv[])
{
int rc, i;
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) {
rc = ENOMEM;
goto out;
}

rc = seccomp_arch_remove(ctx, SCMP_ARCH_NATIVE);
if (rc < 0)
goto out;
rc = seccomp_arch_add(ctx, SCMP_ARCH_X86_64);
if (rc < 0)
goto out;
rc = seccomp_arch_add(ctx, SCMP_ARCH_X86);
if (rc < 0)
goto out;
rc = seccomp_attr_set(ctx, SCMP_FLTATR_CTL_OPTIMIZE, 2);
if (rc < 0)
goto out;

/* NOTE: this test is entirely fabricated and should not be
* replicated in the real world.
*
* The MAX_SYSCALL number (330) was chosen to force seccomp to
* build an unbalanced binary tree - and it happens to be less
* than the current syscall max. The syscall numbers are
* hardcoded to simplify the test. A few syscalls have
* argument chains to further complicate the filter.
*/

for (i = 0; i < MAX_SYSCALL; i++) {
/* arbitrarily make the filter more complex by filtering
* on arguments for a few syscalls
*/
if (i == 10 || i == 53 || i == 61 || i == 255)
rc = seccomp_rule_add(ctx, SCMP_ACT_ERRNO(i), i, 1,
SCMP_A0(SCMP_CMP_EQ, i));
else
rc = seccomp_rule_add(ctx, SCMP_ACT_ERRNO(i), i, 0);
if (rc < 0)
goto out;
}

rc = util_filter_output(&opts, ctx);
if (rc)
goto out;

out:
seccomp_release(ctx);
return (rc < 0 ? -rc : rc);
}
52 changes: 52 additions & 0 deletions tests/53-sim-binary_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python

#
# Seccomp Library test program
#
# Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
# Author: Tom Hromatka <tom.hromatka@oracle.com>
#

#
# 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 <http://www.gnu.org/licenses>.
#

import argparse
import sys

import util

from seccomp import *

def test(args):
f = SyscallFilter(ALLOW)

f.remove_arch(Arch())
f.add_arch(Arch("x86_64"))
f.add_arch(Arch("x86"))
f.set_attr(Attr.CTL_OPTIMIZE, 2)

for i in range(330):
if (i == 10 or i == 53 or i == 61 or i == 255):
f.add_rule(ERRNO(i), i, Arg(0, EQ, i))
else:
f.add_rule(ERRNO(i), i)

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;

0 comments on commit 2e7089f

Please sign in to comment.