eBPF-based security platform
testing: exercise kernel observability credential anomalies
John Fastabendd300dfd75c22
diff --git a/contrib/build-test-vm-bundle.sh b/contrib/build-test-vm-bundle.shindex c9de9841..e373a93d 100755--- a/contrib/build-test-vm-bundle.sh+++ b/contrib/build-test-vm-bundle.sh@@ -539,6 +539,19 @@ EOF--key "$keys_dir/signing_key.pem" \--cert "$keys_dir/cert.der")+ sed 's/^#define BPF_NO_KFUNC_PROTOTYPES/#define SHIRUDO_TEST_CRED_ANOMALY\+#define BPF_NO_KFUNC_PROTOTYPES/' \+ "$source_dir/kernel-obs.bpf.c" \+ >"$source_dir/kernel-obs-cred-anomaly.bpf.c"+ (+ cd "$REPO_ROOT"+ "$REPO_ROOT/tools/shirudo-build/shirudo-build" \+ "$source_dir/kernel-obs-cred-anomaly.bpf.c" \+ -o "$TMP_DIR/fixtures/kernel-obs-cred-anomaly.upatch" \+ --name kernel-obs --vmlinux "$REPO_ROOT/vendor/linux/vmlinux.h" \+ --key "$keys_dir/signing_key.pem" \+ --cert "$keys_dir/cert.der"+ )}log "building target-test fixtures on the operator host"@@ -598,6 +611,9 @@ serialized_metric_payload="$($REPO_ROOT/shirudo upatch info \observability_payload="$($REPO_ROOT/shirudo upatch info \"$TMP_DIR/fixtures/kernel-obs.upatch" --json |python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"+observability_anomaly_payload="$($REPO_ROOT/shirudo upatch info \+ "$TMP_DIR/fixtures/kernel-obs-cred-anomaly.upatch" --json |+ python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')""$REPO_ROOT/tools/shirudo-build/shirudo-build" --unload \shirudo-test-basic --payload-sha256 "$basic_payload" \-o "$TMP_DIR/fixtures/shirudo-test-basic.unload.upatch" \@@ -652,6 +668,11 @@ build_tree_test_registry-o "$TMP_DIR/fixtures/kernel-obs.unload.upatch" \--key "$FIXTURE_WORK/trusted/signing_key.pem" \--cert "$FIXTURE_WORK/trusted/cert.der"+"$REPO_ROOT/tools/shirudo-build/shirudo-build" --unload \+ kernel-obs --payload-sha256 "$observability_anomaly_payload" \+ -o "$TMP_DIR/fixtures/kernel-obs-cred-anomaly.unload.upatch" \+ --key "$FIXTURE_WORK/trusted/signing_key.pem" \+ --cert "$FIXTURE_WORK/trusted/cert.der""$REPO_ROOT/tools/shirudo-build/shirudo-build" --unload \shirudo-test-uprobe-multi --payload-sha256 "$uprobe_payload" \-o "$TMP_DIR/fixtures/shirudo-test-uprobe-multi.unload.upatch" \diff --git a/testing/upatch/kernel-obs/priv/kernel-obs-priv.sh b/testing/upatch/kernel-obs/priv/kernel-obs-priv.shindex c0f7e5b8..9ba33af2 100755--- a/testing/upatch/kernel-obs/priv/kernel-obs-priv.sh+++ b/testing/upatch/kernel-obs/priv/kernel-obs-priv.sh@@ -11,9 +11,12 @@ FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}NAME=kernel-obsLOAD="$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"PIN="/run/shirudo/bpffs/upatches/$NAME/link_0"MODPROBE_FILE=/proc/sys/kernel/modprobeWORK=$(mktemp -d)+ACTIVE_UNLOAD=$UNLOADMODPROBE_BACKUP="$WORK/kernel.modprobe"cleanup()@@ -22,7 +25,7 @@ cleanup()cat "$MODPROBE_BACKUP" >"$MODPROBE_FILE" || truefiif [ -e "$PIN" ] || [ -e "/run/shirudo/bpffs/upatches/$NAME/link" ]; then- "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck \+ "$SHIRUDO_BIN" upatch unload "$ACTIVE_UNLOAD" --no-precheck \>/dev/null 2>&1 || truefirm -f "/var/lib/shirudo/upatches/$NAME.upatch"@@ -36,7 +39,7 @@ trap cleanup EXIT HUP INT TERMload_observability(){- if "$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck --report=- \+ if "$SHIRUDO_BIN" upatch load "$1" --no-precheck --report=- \>"$WORK/load.log" 2>&1; thenreturn 0fi@@ -53,7 +56,7 @@ export_priv()}test_expect_success 'kernel-obs upatch loads with the priv serializer' '- load_observability &&+ load_observability "$LOAD" &&test -e "/run/shirudo/bpffs/upatches/$NAME/serializers/priv"'@@ -80,4 +83,16 @@ test_expect_success 'privilege events produce attributed priv records' '"$WORK/expected.json" "$WORK/priv.ndjson"'+test_expect_success 'credential anomalies are recorded once per task' '+ [ -s "$ANOMALY_LOAD" ] && [ -s "$ANOMALY_UNLOAD" ] &&+ "$SHIRUDO_BIN" upatch unload "$ACTIVE_UNLOAD" --no-precheck \+ >/dev/null &&+ ACTIVE_UNLOAD="$ANOMALY_UNLOAD" &&+ load_observability "$ANOMALY_LOAD" &&+ python3 upatch/kernel-obs/priv/priv_events.py anomaly &&+ export_priv "$WORK/anomaly.ndjson" &&+ python3 upatch/kernel-obs/priv/priv_events.py \+ check-anomaly "$WORK/anomaly.ndjson"+'+test_donediff --git a/testing/upatch/kernel-obs/priv/priv_events.py b/testing/upatch/kernel-obs/priv/priv_events.pyindex 3a016464..fad5ef06 100644--- a/testing/upatch/kernel-obs/priv/priv_events.py+++ b/testing/upatch/kernel-obs/priv/priv_events.py@@ -291,6 +291,26 @@ def check_negative(path):for record in records), records+def check_anomaly(path):+ records = records_from(path)+ anomalies = [record for record in records+ if record["kind"] == "cred_anomaly"]+ assert len(anomalies) == 1, anomalies+ anomaly = anomalies[0]+ assert anomaly["subtype"] == 1 and anomaly["euid"] == 0, anomaly+ assert anomaly["uid"] == 0 and int(anomaly["aux"]) == 0, anomaly+ assert anomaly["name"], anomaly+ assert int(anomaly["count"]) == 1, anomaly+ assert int(anomaly["pid"]) > 0, anomaly+ assert int(anomaly["first_seen_unix_nano"]) > 0, anomaly+ assert int(anomaly["last_seen_unix_nano"]) >= \+ int(anomaly["first_seen_unix_nano"]), anomaly+++def anomaly_event():+ os.execv("/bin/true", ["/bin/true"])++def check_exec_unknown(path):with open(path, encoding="utf-8") as source:documents = [json.loads(line) for line in source]@@ -326,7 +346,8 @@ def check_positive(expected_path, records_path):raise AssertionError({"missing": sorted(kinds - found),"expected_executable": identity,"available_records": available})- assert not any(record["kind"] == "cred_anomaly" for record in records), records+ assert not any(record["kind"] == "cred_anomaly"+ for record in records), recordsfor record in selected:assert int(record["count"]) > 0, recordassert int(record["pid"]) > 0, record@@ -371,7 +392,8 @@ def check_positive(expected_path, records_path):module_request = kind("module_request")[0]assert module_request["name"] == "net-pf-16-proto-31", module_request- assert module_request["subtype"] == 0 and int(module_request["aux"]) == 0, module_request+ assert module_request["subtype"] == 0 and \+ int(module_request["aux"]) == 0, module_requestassert module_request["pid"] == expected["pid"], module_requestuserns = kind("userns_create")[0]@@ -385,18 +407,20 @@ def check_positive(expected_path, records_path):assert int(bpf["aux"]) == 0 and int(bpf["count"]) == 1, bpfmmap_exec = kind("mmap_exec")[0]- assert mmap_exec["subtype"] == PROT_READ | PROT_EXEC, mmap_exec+ assert mmap_exec["subtype"] == (PROT_READ | PROT_EXEC), mmap_execassert mmap_exec["name"] == "true", mmap_execassert int(mmap_exec["aux"]) == MAP_PRIVATE, mmap_execmprotect_exec = kind("mprotect_exec")[0]- assert mprotect_exec["subtype"] == PROT_READ | PROT_EXEC, mprotect_exec+ assert mprotect_exec["subtype"] == (PROT_READ | PROT_EXEC), mprotect_execassert mprotect_exec["name"] == "anonymous", mprotect_execassert int(mprotect_exec["aux"]) == PAGE_SIZE, mprotect_execdef main():if len(sys.argv) < 2:- raise SystemExit("usage: priv_events.py negative | generate FILE | check EXPECTED NDJSON | check-negative NDJSON | check-exec-unknown NDJSON")+ raise SystemExit("usage: priv_events.py negative | generate FILE | "+ "check EXPECTED NDJSON | check-negative NDJSON | "+ "check-anomaly NDJSON | check-exec-unknown NDJSON")mode = sys.argv[1]if mode == "negative":nonexec_mapping_events()@@ -406,6 +430,10 @@ def main():check_positive(sys.argv[2], sys.argv[3])elif mode == "check-negative" and len(sys.argv) == 3:check_negative(sys.argv[2])+ elif mode == "check-anomaly" and len(sys.argv) == 3:+ check_anomaly(sys.argv[2])+ elif mode == "anomaly" and len(sys.argv) == 2:+ anomaly_event()elif mode == "check-exec-unknown" and len(sys.argv) == 3:check_exec_unknown(sys.argv[2])else:diff --git a/upatches/kernel-obs/obs_exec.bpf.c b/upatches/kernel-obs/obs_exec.bpf.cindex 43c283c8..74715e32 100644--- a/upatches/kernel-obs/obs_exec.bpf.c+++ b/upatches/kernel-obs/obs_exec.bpf.c@@ -165,3 +165,28 @@ int BPF_PROG(obs_task_alloc, struct task_struct *child, __u64 clone_flags)meta->flags = inherited->flags & OBS_TASK_INFERRED;return 0;}++#ifdef SHIRUDO_TEST_CRED_ANOMALY+SEC("lsm/bprm_committed_creds")+int BPF_PROG(obs_test_cred_anomaly, const struct linux_binprm *bprm)+{+ char name[OBS_NAME_LEN] = {};+ struct obs_task_meta *meta;+ struct task_struct *task;++ if (!bprm->file)+ return 0;+ obs_name_from_file(bprm->file, name);+ if (name[0] != 't' || name[1] != 'r' || name[2] != 'u' ||+ name[3] != 'e' || name[4])+ return 0;+ task = bpf_get_current_task_btf();+ meta = obs_task_meta(task);+ if (!meta)+ return 0;+ meta->euid = 1;+ obs_cred_check(task, meta, obs_now());+ obs_cred_check(task, meta, obs_now());+ return 0;+}+#endif