Skip to content

eBPF-based security platform

testing: exercise the tamper counters on a target

Daniel Borkmann0822470ddb66
diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.sh
index ec91ec7a..00348eae 100755
--- a/testing/run-target-tests.sh
+++ b/testing/run-target-tests.sh
@@ -43,6 +43,7 @@ for t in \
tamperproof/t1113-mount-lockdown.sh \
tamperproof/t1135-pin-rename.sh \
tamperproof/t1137-bpf-cmd-deny.sh \
+ tamperproof/t1136-tamper-counters.sh \
tamperproof/t1130-map-lockdown.sh \
tamperproof/t1131-pr-set-mm-identity.sh \
tamperproof/t1125-key-permission.sh \
diff --git a/testing/tamperproof/t1136-tamper-counters.sh b/testing/tamperproof/t1136-tamper-counters.sh
new file mode 100755
index 00000000..027ea1c2
--- /dev/null
+++ b/testing/tamperproof/t1136-tamper-counters.sh
@@ -0,0 +1,122 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t1136 -- the tamper counters. Every guard counts what it refuses, by
+# the point of refusal, in a frozen map init pins; `shirudo status`
+# lists the non-zero counters and `status --json` carries them all, so
+# an operator sees an attempt and not only its failure. Each refusal
+# provoked here must move its own counter and the total, root must not
+# be able to reset the map, and the human report must name what moved.
+# Non-destructive: every operation is one the guards refuse.
+
+cd "$(dirname "$0")/.."
+. ./integration-lib.sh
+target_guard
+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"
+
+H=helper/test-bpf-load
+C=helper/test-bpf-cmd
+[ -x "$H" ] || test_skip_all "test-bpf-load helper not built"
+[ -x "$C" ] || test_skip_all "test-bpf-cmd helper not built"
+B=/run/shirudo/bpffs
+[ -e "$B/tamper" ] || test_skip_all "no tamper map pinned at $B/tamper"
+W=$(mktemp -d) || test_skip_all "cannot make a scratch directory"
+trap 'rm -rf "$W"' EXIT
+
+# The counter $1 (or "total") from status --json.
+counter() {
+ "$SHIRUDO_BIN" status --json | python3 -c '
+import json, sys
+d = json.load(sys.stdin)["tamper_attempts"]
+print(d["total"] if sys.argv[1] == "total" else d["counters"][sys.argv[1]])
+' "$1"
+}
+
+# Whether counter $1 has moved past $2.
+grew() {
+ [ "$(counter "$1")" -gt "$2" ]
+}
+
+RENAME='
+import os, sys
+try:
+ os.rename(sys.argv[1], sys.argv[2])
+except OSError:
+ pass
+'
+
+test_expect_success 'status reports the counters and all start as numbers' '
+ "$SHIRUDO_BIN" status --json | python3 -c "
+import json, sys
+d = json.load(sys.stdin)[\"tamper_attempts\"]
+assert isinstance(d[\"total\"], int)
+assert all(isinstance(v, int) for v in d[\"counters\"].values())
+assert \"PinRename\" in d[\"counters\"] and \"ProgAttach\" in d[\"counters\"]
+assert \"LoadNotLoader\" in d[\"counters\"]
+"
+'
+
+test_expect_success 'an unsigned load of a plain program moves LoadNotLoader' '
+ before=$(counter LoadNotLoader) && total=$(counter total) &&
+ [ "$(./$H)" = "errno=13" ] &&
+ grew LoadNotLoader "$before" && grew total "$total"
+'
+
+test_expect_success 'a rename inside the bpffs moves PinRename' '
+ before=$(counter PinRename) &&
+ python3 -c "$RENAME" "$B/identity" "$B/t1136" &&
+ [ -e "$B/identity" ] &&
+ grew PinRename "$before"
+'
+
+test_expect_success 'an unlink inside the bpffs moves PinUnlink' '
+ before=$(counter PinUnlink) &&
+ rm -f "$B/identity" 2>/dev/null
+ [ -e "$B/identity" ] &&
+ grew PinUnlink "$before"
+'
+
+test_expect_success 'an unmount of the bpffs moves BpffsUmount' '
+ before=$(counter BpffsUmount) &&
+ umount "$B" 2>/dev/null
+ mountpoint -q "$B" &&
+ grew BpffsUmount "$before"
+'
+
+test_expect_success 'an unmount above the bpffs moves BpffsUmountAbove' '
+ before=$(counter BpffsUmountAbove) &&
+ unshare -m sh -c "umount -l /run" 2>/dev/null
+ mountpoint -q "$B" &&
+ grew BpffsUmountAbove "$before"
+'
+
+test_expect_success 'a link creation moves ProgAttach and a pin moves ObjPin' '
+ ProgAttach=$(counter ProgAttach) && pin=$(counter ObjPin) &&
+ [ "$(./$C LINK_CREATE)" = "errno=13" ] &&
+ [ "$(./$C OBJ_PIN)" = "errno=13" ] &&
+ grew ProgAttach "$ProgAttach" && grew ObjPin "$pin"
+'
+
+test_expect_success 'a denied tear-down command moves BpfCommand' '
+ before=$(counter BpfCommand) &&
+ [ "$(./$C LINK_DETACH)" = "errno=13" ] &&
+ grew BpfCommand "$before"
+'
+
+test_expect_success 'root cannot reset the counters' '
+ before=$(counter total) &&
+ [ "$(./$C MAP_UPDATE_ELEM)" = "errno=13" ] &&
+ [ "$(counter total)" -ge "$before" ]
+'
+
+test_expect_success 'the human report lists the counters that moved' '
+ "$SHIRUDO_BIN" status >"$W/status" &&
+ grep -q "^Tamper attempts (refused this boot):" "$W/status" &&
+ grep -q "^ PinRename *[1-9][0-9]* rename of a pin" "$W/status" &&
+ grep -q "^ BpffsUmountAbove *[1-9][0-9]* unmount of a mount above" \
+ "$W/status" &&
+ ! grep -q "^ none$" "$W/status"
+'
+
+test_done
diff --git a/testing/tamperproof/test.plan.md b/testing/tamperproof/test.plan.md
index 36f6e045..160e2eb4 100644
--- a/testing/tamperproof/test.plan.md
+++ b/testing/tamperproof/test.plan.md
@@ -48,6 +48,7 @@ checks that Shirudo can still export the pre-existing Python exec record.
| TP-26 | Root attaches a debugger to a running shirudo task with `PTRACE_ATTACH` | Covered | `t1134-seal-task.sh` | The seal refuses with `EACCES`, Yama's `EPERM` would not do; the task is unharmed and finishes. |
| TP-27 | Root binds a perf event to a running shirudo task | Covered | `t1134-seal-task.sh` | `perf_event_open` on the task is refused with `EACCES`; the same event on the caller's own task is the positive control. |
| TP-28 | Root renames a pin in the private bpffs, or renames one over the lsm/bpf link's pin | Covered | `t1135-pin-rename.sh` | Both are refused with `EPERM`; the pins stay and enforcement survives. Before `lsm/inode_rename` the rename over a pin dropped the link with no guard firing. |
+| TP-29 | A refused attempt leaves no trace | Covered | `t1136-tamper-counters.sh` | Every guard counts its refusals by point in a frozen pinned map; `status` lists the non-zero counters, `status --json` and the keepalive carry them all, and root cannot reset them. |
## Review notes