eBPF-based security platform
testing: cover privilege bpf command filtering
John Fastabend472e4e7077da
diff --git a/testing/upatch/kernel-obs/priv/kernel-obs-priv.sh b/testing/upatch/kernel-obs/priv/kernel-obs-priv.shindex b9ad0442..48b34be3 100755--- a/testing/upatch/kernel-obs/priv/kernel-obs-priv.sh+++ b/testing/upatch/kernel-obs/priv/kernel-obs-priv.sh@@ -13,6 +13,8 @@ LOAD="$FIXTURES/$NAME.upatch"UNLOAD="$FIXTURES/$NAME.unload.upatch"ANOMALY_LOAD="$FIXTURES/kernel-obs-cred-anomaly.upatch"ANOMALY_UNLOAD="$FIXTURES/kernel-obs-cred-anomaly.unload.upatch"+BPF_FILTER_LOAD="$FIXTURES/shirudo-test-map-export.upatch"+BPF_FILTER_UNLOAD="$FIXTURES/shirudo-test-map-export.unload.upatch"PIN="/run/shirudo/bpffs/upatches/$NAME/link_0"MODPROBE_FILE=/proc/sys/kernel/modprobeWORK=$(mktemp -d)@@ -26,6 +28,11 @@ cleanup()kill "$EVENT_PID" 2>/dev/null || truewait "$EVENT_PID" 2>/dev/null || truefi+ if [ -e "/run/shirudo/bpffs/upatches/shirudo-test-map-export/link_0" ] ||+ [ -e "/run/shirudo/bpffs/upatches/shirudo-test-map-export/link" ]; then+ "$SHIRUDO_BIN" upatch unload "$BPF_FILTER_UNLOAD" --no-precheck \+ >/dev/null 2>&1 || true+ fiif [ -s "$MODPROBE_BACKUP" ] || [ -e "$MODPROBE_BACKUP" ]; thencat "$MODPROBE_BACKUP" >"$MODPROBE_FILE" || truefi@@ -40,6 +47,8 @@ trap cleanup EXIT HUP INT TERM[ -s "$LOAD" ] || test_skip_all "missing kernel-obs fixture"[ -s "$UNLOAD" ] || test_skip_all "missing kernel-obs unload fixture"+[ -s "$BPF_FILTER_LOAD" ] || test_skip_all "missing BPF filter fixture"+[ -s "$BPF_FILTER_UNLOAD" ] || test_skip_all "missing BPF filter unload fixture"[ -w "$MODPROBE_FILE" ] || test_skip_all "kernel.modprobe is not writable"mkfifo "$WORK/events.in" "$WORK/events.out"@@ -105,6 +114,10 @@ test_expect_success 'privilege events produce attributed priv records' 'run_event generate &&cat "$MODPROBE_BACKUP" >"$MODPROBE_FILE" &&rm -f "$MODPROBE_BACKUP" &&+ "$SHIRUDO_BIN" upatch load "$BPF_FILTER_LOAD" --no-precheck \+ --report=- >"$WORK/bpf-filter-load.log" 2>&1 &&+ "$SHIRUDO_BIN" upatch unload "$BPF_FILTER_UNLOAD" --no-precheck \+ >/dev/null &&export_priv "$WORK/priv.ndjson" &&"$SHIRUDO_BIN" upatch map "$NAME" exec \--json="$WORK/exec.ndjson" >/dev/null &&diff --git a/testing/upatch/kernel-obs/priv/priv_events.py b/testing/upatch/kernel-obs/priv/priv_events.pyindex 300cb3b1..a4d15624 100644--- a/testing/upatch/kernel-obs/priv/priv_events.py+++ b/testing/upatch/kernel-obs/priv/priv_events.py@@ -18,6 +18,7 @@ PROT_WRITE = 2PROT_EXEC = 4MAP_PRIVATE = 2MAP_ANONYMOUS = 0x20+BPF_MAP_GET_NEXT_ID = 12PTRACE_ATTACH = 16PTRACE_DETACH = 17PR_SET_NAME = 15@@ -192,6 +193,18 @@ def bpf_event():raise OSError(error, os.strerror(error))+def bpf_readonly_event():+ attr = bytearray(72)+ buf = ctypes.create_string_buffer(bytes(attr))+ number = syscall_numbers()["bpf"]+ ctypes.set_errno(0)+ result = libc.syscall(number, ctypes.c_int(BPF_MAP_GET_NEXT_ID),+ ctypes.byref(buf), ctypes.c_uint(len(attr)))+ if result < 0:+ error = ctypes.get_errno()+ raise OSError(error, os.strerror(error))++def mmap_exec_event():fd = os.open("/bin/true", os.O_RDONLY)try:@@ -313,6 +326,7 @@ def generate(expected_path):module_path = module_load_events(workdir)module_request_event()bpf_event()+ bpf_readonly_event()mmap_exec_event()mprotect_exec_event()with open(expected_path, "w", encoding="utf-8") as output:@@ -479,6 +493,16 @@ def check_positive(expected_path, records_path):assert bpf["subtype"] == 0, bpfassert bpf["pid"] == expected["pid"], bpfassert int(bpf["aux"]) == 0 and int(bpf["count"]) == 1, bpf+ map_creates = [record for record in records+ if record["kind"] == "bpf" and record["subtype"] == 0]+ assert len(map_creates) == 1, map_creates+ assert (map_creates[0]["executable"]["dev"] == identity["dev"] and+ map_creates[0]["executable"]["ino"] == identity["ino"]), map_creates+ assert not any(record["kind"] == "bpf" and+ record["subtype"] == BPF_MAP_GET_NEXT_ID and+ record["executable"]["dev"] == identity["dev"] and+ record["executable"]["ino"] == identity["ino"]+ for record in records), recordsmmap_exec = kind("mmap_exec")[0]assert mmap_exec["subtype"] == (PROT_READ | PROT_EXEC), mmap_exec