Skip to content

Commit

Permalink
tests: compile test_syscall with musl-gcc
Browse files Browse the repository at this point in the history
In addition do a few more cleanups to the test:

- Use `atol` to read longs

- musl makes an ioctl that is not permitted in seccomp. We didn't use
  the return code anyway, so remove it.

Signed-off-by: Pablo Barbáchano <[email protected]>
  • Loading branch information
pb8o committed Dec 12, 2024
1 parent 8c7ee82 commit 30d0260
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
13 changes: 6 additions & 7 deletions tests/host_tools/test_syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ void install_bpf_filter(char *bpf_file) {
exit(EXIT_FAILURE);
}
size_t size = sb.st_size;
size_t insn_len = size / sizeof(struct sock_filter);
struct sock_filter *filterbuf = (struct sock_filter*)malloc(size);
if (read(fd, filterbuf, size) == -1) {
perror("read");
exit(EXIT_FAILURE);
}

/* Install seccomp filter */
size_t insn_len = size / sizeof(struct sock_filter);
struct sock_fprog prog = {
.len = (unsigned short)(insn_len),
.filter = filterbuf,
Expand All @@ -60,18 +60,17 @@ int main(int argc, char **argv) {
char *bpf_file = argv[1];
long syscall_id = atoi(argv[2]);
long arg0, arg1, arg2, arg3;
arg0 = arg1 = arg2 = arg3 = 0;
if (argc > 3) arg0 = atoi(argv[3]);
if (argc > 4) arg1 = atoi(argv[4]);
if (argc > 5) arg2 = atoi(argv[5]);
if (argc > 6) arg3 = atoi(argv[6]);
arg0 = arg1 = arg2 = arg3 = 0L;
if (argc > 3) arg0 = atol(argv[3]);
if (argc > 4) arg1 = atol(argv[4]);
if (argc > 5) arg2 = atol(argv[5]);
if (argc > 6) arg3 = atol(argv[6]);

/* read seccomp filter from file */
if (strcmp(bpf_file, "/dev/null") != 0) {
install_bpf_filter(bpf_file);
}

long res = syscall(syscall_id, arg0, arg1, arg2, arg3);
printf("%ld\n", res);
return EXIT_SUCCESS;
}
16 changes: 8 additions & 8 deletions tests/integration_tests/security/test_seccomp_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
import seccomp

from framework import utils
from host_tools import cargo_build

ARCH = platform.machine()


@pytest.fixture(scope="session")
def bin_test_syscall(test_fc_session_root_path):
@pytest.fixture
def bin_test_syscall(tmp_path):
"""Build the test_syscall binary."""
test_syscall_bin = Path(test_fc_session_root_path) / "test_syscall"
cargo_build.gcc_compile("host_tools/test_syscalls.c", test_syscall_bin)
test_syscall_bin = tmp_path / "test_syscall"
compile_cmd = f"musl-gcc -static host_tools/test_syscalls.c -o {test_syscall_bin}"
utils.check_output(compile_cmd)
assert test_syscall_bin.exists()
yield test_syscall_bin
yield test_syscall_bin.resolve()


class BpfMapReader:
Expand Down Expand Up @@ -77,11 +77,11 @@ def split(self):
for _ in range(map_len):
# read key
key_str_len = self.read_format("<Q")
key_str = self.read_format(f"{key_str_len}s")
key_str = self.read_format(f"{key_str_len}s").decode("ascii")
# read value: vec of instructions
insn_len = self.read_format("<Q")
data = self.lookahead(insn_len * self.INSN_SIZEOF)
threads[key_str.decode("ascii")] = data
threads[key_str] = data
self.offset += len(data)

assert self.is_eof()
Expand Down

0 comments on commit 30d0260

Please sign in to comment.