#!/bin/bash -e bt=/usr/bin/bpftrace default_allow_filter=$(cat << EOF { scmp_filter_ctx ctx; ctx = seccomp_init(SCMP_ACT_ALLOW); seccomp_load(ctx); seccomp_release(ctx); } EOF ) default_block_filter=$(cat << EOF { scmp_filter_ctx ctx; ctx = seccomp_init(SCMP_ACT_KILL); for (int i = 0; i < num_syscalls; i++) { seccomp_rule_add(ctx, SCMP_ACT_ALLOW, seccomp_syscall_resolve_name(syscalls[i]), 0); } seccomp_load(ctx); seccomp_release(ctx); } EOF ) allow_uretprobe_filter=$(cat << EOF { scmp_filter_ctx ctx; ctx = seccomp_init(SCMP_ACT_KILL); for (int i = 0; i < num_syscalls; i++) { seccomp_rule_add(ctx, SCMP_ACT_ALLOW, seccomp_syscall_resolve_name(syscalls[i]), 0); } seccomp_rule_add(ctx, SCMP_ACT_ALLOW, 335, 0); seccomp_load(ctx); seccomp_release(ctx); } EOF ) block_uretprobe_filter=$(cat << EOF { scmp_filter_ctx ctx; ctx = seccomp_init(SCMP_ACT_ALLOW); seccomp_rule_add(ctx, SCMP_ACT_KILL, 335, 0); seccomp_load(ctx); seccomp_release(ctx); } EOF ) t() { with_uretprobe=$1; filter_name=$2; filter=${!filter_name}; echo "Test: uretprobe $with_uretprobe, filter $filter_name" cat > /tmp/x.c << EOF #include #include char *syscalls[] = { "exit_group", }; __attribute__((noinline)) int probed(void) { return 1; } void apply_seccomp_filter(char **syscalls, int num_syscalls) $filter int main(int argc, char *argv[]) { int num_syscalls = sizeof(syscalls) / sizeof(syscalls[0]); apply_seccomp_filter(syscalls, num_syscalls); probed(); return 0; } EOF cat > /tmp/trace.bt << EOF uretprobe:/tmp/x:probed { printf("ret=%d\n", retval); } EOF gcc -o /tmp/x /tmp/x.c -lseccomp $with_uretprobe && { $bt /tmp/trace.bt & btpid=$! sleep 5 # wait for uretprobe attach } /tmp/x $with_uretprobe && kill $btpid rm /tmp/x /tmp/x.c /tmp/trace.bt } t false "default_allow_filter" t true "default_allow_filter" t false "default_block_filter" t true "default_block_filter" t false "allow_uretprobe_filter" t true "allow_uretprobe_filter" t false "block_uretprobe_filter" t true "block_uretprobe_filter"