Skip to content

eBPF-based security platform

testing: assert credential anomaly identity

John Fastabendf80301f04d5e
diff --git a/testing/upatch/kernel-obs/priv/kernel-obs-priv.sh b/testing/upatch/kernel-obs/priv/kernel-obs-priv.sh
index 48b34be3..4caeee21 100755
--- a/testing/upatch/kernel-obs/priv/kernel-obs-priv.sh
+++ b/testing/upatch/kernel-obs/priv/kernel-obs-priv.sh
@@ -133,10 +133,11 @@ test_expect_success 'credential anomalies are recorded once per task' '
>/dev/null &&
ACTIVE_UNLOAD="$ANOMALY_UNLOAD" &&
load_observability "$ANOMALY_LOAD" &&
- python3 upatch/kernel-obs/priv/priv_events.py anomaly &&
+ python3 upatch/kernel-obs/priv/priv_events.py anomaly \
+ "$WORK/anomaly-expected.json" &&
export_priv "$WORK/anomaly.ndjson" &&
python3 upatch/kernel-obs/priv/priv_events.py \
- check-anomaly "$WORK/anomaly.ndjson"
+ check-anomaly "$WORK/anomaly-expected.json" "$WORK/anomaly.ndjson"
'
test_done
diff --git a/testing/upatch/kernel-obs/priv/priv_events.py b/testing/upatch/kernel-obs/priv/priv_events.py
index a4d15624..9c7aa073 100644
--- a/testing/upatch/kernel-obs/priv/priv_events.py
+++ b/testing/upatch/kernel-obs/priv/priv_events.py
@@ -51,12 +51,16 @@ def syscall_numbers():
return SYSCALLS[machine]
-def executable_identity():
- info = os.stat("/proc/self/exe")
+def file_identity(path):
+ info = os.stat(path)
return {"dev": (os.major(info.st_dev) << 20) | os.minor(info.st_dev),
"ino": info.st_ino}
+def executable_identity():
+ return file_identity("/proc/self/exe")
+
+
def setuid_event():
os.seteuid(65534)
try:
@@ -378,14 +382,18 @@ def check_existing_exec(path, expected_path):
assert int(mmap_exec["count"]) == 1, mmap_exec
-def check_anomaly(path):
- records = records_from(path)
+def check_anomaly(expected_path, records_path):
+ with open(expected_path, encoding="utf-8") as source:
+ expected = json.load(source)
+ records = records_from(records_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["executable"] == expected["identity"], anomaly
+ assert anomaly["pid"] == expected["pid"], anomaly
assert anomaly["name"], anomaly
assert int(anomaly["count"]) == 1, anomaly
assert int(anomaly["pid"]) > 0, anomaly
@@ -394,7 +402,9 @@ def check_anomaly(path):
int(anomaly["first_seen_unix_nano"]), anomaly
-def anomaly_event():
+def anomaly_event(expected_path):
+ write_expected(expected_path,
+ {"pid": os.getpid(), "identity": file_identity("/bin/true")})
os.execv("/bin/true", ["/bin/true"])
@@ -519,7 +529,8 @@ def main():
raise SystemExit("usage: priv_events.py serve IN OUT EXPECTED | "
"negative | generate FILE | "
"check EXPECTED NDJSON | check-negative NDJSON | "
- "check-anomaly NDJSON | check-exec-unknown NDJSON")
+ "check-anomaly EXPECTED NDJSON | "
+ "check-exec-unknown NDJSON")
mode = sys.argv[1]
if mode == "serve" and len(sys.argv) == 5:
serve(sys.argv[2], sys.argv[3], sys.argv[4])
@@ -533,10 +544,10 @@ def main():
check_negative(sys.argv[2], sys.argv[3])
elif mode == "check-existing-exec" and len(sys.argv) == 4:
check_existing_exec(sys.argv[2], sys.argv[3])
- 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-anomaly" and len(sys.argv) == 4:
+ check_anomaly(sys.argv[2], sys.argv[3])
+ elif mode == "anomaly" and len(sys.argv) == 3:
+ anomaly_event(sys.argv[2])
elif mode == "check-exec-unknown" and len(sys.argv) == 3:
check_exec_unknown(sys.argv[2])
else: