eBPF-based security platform
testing: cover the remaining tamperproof cases
Daniel Borkmann664843b15258
diff --git a/Documentation/developer-guide.rst b/Documentation/developer-guide.rstindex c8fcdfdb..f726f2c3 100644--- a/Documentation/developer-guide.rst+++ b/Documentation/developer-guide.rst@@ -222,20 +222,6 @@ software TPM on the host for development without a VM.8) Open work-------------Cases no test covers yet, for whoever adds the next tamperproof test:--* obtaining or passing shirudo's map descriptors through- ``/proc/<pid>/fd``, ``pidfd_getfd`` or ``SCM_RIGHTS``, and using one to- change a protected map;-* repeated and concurrent serializer runs losing or changing records;-* an inode created under a labelled directory being born labelled;-* ``PTRACE_ATTACH`` to, or a perf event bound to, a running shirudo task;-* starting shirudo with ``LD_PRELOAD``, ``LD_AUDIT`` or- ``LD_LIBRARY_PATH`` naming foreign code, and exec'ing it from a traced- task;-* a trusted loader mutated with its userspace digest recomputed, which the- kernel's signature check must refuse.-The export path has room for more exact assertions: endpoint, CA,client-certificate and header precedence across the command line, thesignal-specific and generic environment, the config and the defaults;diff --git a/testing/functional/tamperproof/t6133-seal-init-xattr.sh b/testing/functional/tamperproof/t6133-seal-init-xattr.shnew file mode 100755index 00000000..b0f8ca6b--- /dev/null+++ b/testing/functional/tamperproof/t6133-seal-init-xattr.sh@@ -0,0 +1,115 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t6133 -- labelling at creation (bpf/seal.c inode_init_security): an+# inode the runtime creates under a labelled directory the seal has seen+# is born with the label, before userspace could set it, and is sealed+# from that instant. The creator is the policy probe in the runtime+# image, which also removes what it made; root may create nothing there.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"++: "${SHIRUDO_TEST_RUNTIME_PROBE:=/run/shirudo/runtime/usr/bin/shirudo-policy-probe}"+P=$SHIRUDO_TEST_RUNTIME_PROBE+LABEL=94442b45cfcdc59aabc2a91364ed2ae129418e05a5c1985231b9c032e673f469+DIR=/var/lib/shirudo/t6133.$(cat /proc/sys/kernel/random/uuid)+OUTSIDE=/root/t6133.$$+[ -x "$P" ] || test_skip_all "no policy probe in the runtime image"+[ -e /run/shirudo/bpffs/seal_inode_init_security_link ] ||+ test_skip_all "the seal does not label at creation on this target"++# Under the library's errexit a failing step would end the cleanup.+cleanup() {+ "$P" unlink "$DIR/sub/child" 2>/dev/null || true+ "$P" rmdir "$DIR/sub" 2>/dev/null || true+ "$P" unlink "$DIR/link" 2>/dev/null || true+ "$P" unlink "$DIR/file" 2>/dev/null || true+ "$P" rmdir "$DIR" 2>/dev/null || true+ rm -f "$OUTSIDE" 2>/dev/null || true+}+trap cleanup EXIT++label_of() {+ python3 -c '+import errno, os, sys+try:+ print(os.getxattr(sys.argv[1], "security.bpf.shirudo",+ follow_symlinks=False).decode())+except OSError as e:+ print("unsupported" if e.errno == errno.EOPNOTSUPP else "none")' "$1"+}++labelled() {+ for f in "$@"; do+ [ "$(label_of "$f")" = "$LABEL" ] || {+ echo "$f: label $(label_of "$f")"+ return 1+ }+ done+}++if [ "$(label_of /run/shirudo)" = unsupported ]; then+ echo "# SKIP /run does not support security xattrs"+else+ test_expect_success 'the run directory carries the label' '+ labelled /run/shirudo+ '+fi++# Opening the state root records its mark for inode_init_security.+test_expect_success 'a directory the runtime makes in sealed state is born labelled' '+ ls /var/lib/shirudo >/dev/null &&+ "$P" mkdir "$DIR" &&+ labelled "$DIR"+'++test_expect_success 'and so is what it creates in there, without a reopen' '+ "$P" create "$DIR/file" &&+ "$P" symlink file "$DIR/link" &&+ "$P" mkdir "$DIR/sub" &&+ "$P" create "$DIR/sub/child" &&+ labelled "$DIR/file" "$DIR/link" "$DIR/sub" "$DIR/sub/child"+'++test_expect_success 'root can neither write, unlink nor rename any of it' '+ ! sh -c "echo x >\"$DIR/file\"" 2>/dev/null &&+ ! rm -f "$DIR/file" 2>/dev/null &&+ ! mv "$DIR/file" "$DIR/moved" 2>/dev/null &&+ ! rm -f "$DIR/link" 2>/dev/null &&+ ! rm -f "$DIR/sub/child" 2>/dev/null &&+ [ -e "$DIR/file" ] && [ ! -s "$DIR/file" ] && [ -L "$DIR/link" ]+'++test_expect_success 'nor create anything beside it' '+ ! touch "$DIR/root-file" 2>/dev/null &&+ ! mkdir "$DIR/root-dir" 2>/dev/null &&+ ! ln -s file "$DIR/root-link" 2>/dev/null &&+ [ ! -e "$DIR/root-file" ] && [ ! -e "$DIR/root-dir" ] &&+ [ ! -L "$DIR/root-link" ]+'++test_expect_success 'nor open an O_TMPFILE in it for writing' '+ ! python3 -c "import os,sys; os.open(sys.argv[1], os.O_TMPFILE | os.O_WRONLY, 0o600)" \+ "$DIR" 2>/dev/null+'++test_expect_success 'what the runtime creates outside the tree stays unlabelled' '+ "$P" create "$OUTSIDE" &&+ [ "$(label_of "$OUTSIDE")" = none ] &&+ rm -f "$OUTSIDE"+'++test_expect_success 'the runtime removes what it made' '+ "$P" unlink "$DIR/sub/child" &&+ "$P" rmdir "$DIR/sub" &&+ "$P" unlink "$DIR/link" &&+ "$P" unlink "$DIR/file" &&+ "$P" rmdir "$DIR" &&+ [ ! -e "$DIR" ]+'++test_donediff --git a/testing/functional/tamperproof/t6134-seal-task.sh b/testing/functional/tamperproof/t6134-seal-task.shnew file mode 100755index 00000000..00b9ee85--- /dev/null+++ b/testing/functional/tamperproof/t6134-seal-task.sh@@ -0,0 +1,110 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t6134 -- the seal's task hooks (bpf/seal.c ptrace_access_check and+# perf_event_alloc): a task that runs the runtime shirudo binary can+# neither be ptrace-attached nor have a perf event bound to it, even by+# root. The task is a shirudo blocked on opening a fifo, released at+# the end. Non-destructive.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"+[ -e /run/shirudo/bpffs/seal_ptrace_access_check_link ] ||+ test_skip_all "the seal's task hooks are not attached on this target"++W=$(mktemp -d) || test_skip_all "cannot make a scratch directory"+PID=+cleanup() {+ # A writer lets the blocked open return; shirudo then rejects the+ # input and exits. ENXIO means it is not waiting any more.+ [ -n "$PID" ] && python3 -c '+import os, sys+try:+ fd = os.open(sys.argv[1], os.O_WRONLY | os.O_NONBLOCK)+ os.write(fd, b"x")+ os.close(fd)+except OSError:+ pass' "$W/fifo" 2>/dev/null+ if [ -n "$PID" ]; then+ wait "$PID" 2>/dev/null || :+ fi+ rm -rf "$W"+}+trap cleanup EXIT++mkfifo "$W/fifo" || test_skip_all "cannot make a fifo"+"$SHIRUDO_BIN" bpm info "$W/fifo" >/dev/null 2>&1 &+PID=$!+# Blocked in open(2) of the fifo: alive and sleeping.+i=0+while [ "$i" -lt 50 ]; do+ [ -d "/proc/$PID" ] || break+ [ "$(awk '{print $3}' "/proc/$PID/stat" 2>/dev/null)" = S ] && break+ sleep 0.1+ i=$((i + 1))+done+[ -d "/proc/$PID" ] || test_skip_all "shirudo did not stay around"++# ptrace(PTRACE_ATTACH): 0 attached (and detached again), 1 refused by+# the seal, 3 refused otherwise.+ATTACH='+import ctypes, sys+libc = ctypes.CDLL(None, use_errno=True)+pid = int(sys.argv[1])+if libc.ptrace(16, pid, None, None) == 0:+ libc.ptrace(17, pid, None, None)+ sys.exit(0)+sys.exit(1 if ctypes.get_errno() == 13 else 3)+'+# perf_event_open(PERF_COUNT_SW_CPU_CLOCK) bound to a task: the same+# exit codes.+PERF='+import ctypes, os, struct, sys+nr = {"x86_64": 298, "aarch64": 241, "riscv64": 241}.get(os.uname().machine)+if nr is None:+ sys.exit(4)+libc = ctypes.CDLL(None, use_errno=True)+pid = os.getpid() if sys.argv[1] == "self" else int(sys.argv[1])+attr = ctypes.create_string_buffer(+ struct.pack("IIQQQQQQQ", 1, 64, 0, 0, 0, 0, 0, 0, 0), 64)+fd = libc.syscall(nr, attr, pid, -1, -1, 0)+if fd >= 0:+ os.close(fd)+ sys.exit(0)+sys.exit(1 if ctypes.get_errno() == 13 else 3)+'++test_expect_success 'root cannot ptrace-attach to a running shirudo' '+ python3 -c "$ATTACH" "$PID"; [ "$?" = 1 ]+'++if python3 -c "$PERF" self 2>/dev/null; then+ test_expect_success 'root cannot bind a perf event to a running shirudo' '+ python3 -c "$PERF" "$PID"; [ "$?" = 1 ]+ '+else+ echo "# perf_event_open is unavailable here; the perf case is not run"+fi++# The task is the main shell's child: the case's subshell cannot reap it,+# so it watches the task become a zombie instead.+test_expect_success 'the task is unharmed and finishes once released' '+ [ "$(cut -d" " -f3 "/proc/$PID/stat")" = S ] &&+ python3 -c "+import os, sys+fd = os.open(sys.argv[1], os.O_WRONLY | os.O_NONBLOCK)+os.write(fd, b\"x\")+os.close(fd)" "$W/fifo" &&+ i=0 &&+ while s=$(cut -d" " -f3 "/proc/$PID/stat" 2>/dev/null) &&+ [ "$s" != Z ]; do+ i=$((i + 1))+ [ "$i" -lt 100 ] || return 1+ sleep 0.1+ done+'++test_donediff --git a/testing/functional/tamperproof/t6135-task-descriptors.sh b/testing/functional/tamperproof/t6135-task-descriptors.shnew file mode 100755index 00000000..59750fba--- /dev/null+++ b/testing/functional/tamperproof/t6135-task-descriptors.sh@@ -0,0 +1,109 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t6135 -- a running shirudo's descriptors are not root's to take. Opening+# one through /proc/<pid>/fd and copying one with pidfd_getfd both need+# ptrace access to the task, which the seal refuses for a task the runtime+# image started, counted as SealPtrace; another process's descriptors stay+# reachable as the control. Passing one with SCM_RIGHTS would need shirudo+# to send it, which it never does. Non-destructive.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"++W=$(mktemp -d) || test_skip_all "cannot make a scratch directory"+PID=+OTHER=+release() {+ [ -n "$PID" ] || return 0+ python3 -c '+import os, sys+try:+ fd = os.open(sys.argv[1], os.O_WRONLY | os.O_NONBLOCK)+ os.write(fd, b"x")+ os.close(fd)+except OSError:+ pass' "$W/fifo" 2>/dev/null || true+ wait "$PID" 2>/dev/null || true+ PID=+}+cleanup() {+ release+ [ -z "$OTHER" ] || kill "$OTHER" 2>/dev/null || true+ rm -rf "$W"+}+trap cleanup EXIT++counter() {+ "$SHIRUDO_BIN" status --json | python3 -c '+import json, sys+print(json.load(sys.stdin)["tamper_attempts"]["counters"][sys.argv[1]])+' "$1"+}++# The descriptor routes, for pid $1 and descriptor $2: 0 when root got+# the descriptor, 1 when the kernel refused, 3 on anything else.+PROC='+import errno, os, sys+try:+ os.close(os.open("/proc/%s/fd/%s" % (sys.argv[1], sys.argv[2]), os.O_RDONLY))+except OSError as e:+ sys.exit(1 if e.errno in (errno.EACCES, errno.EPERM) else 3)+'+GETFD='+import ctypes, errno, os, sys+libc = ctypes.CDLL(None, use_errno=True)+pidfd = libc.syscall(434, int(sys.argv[1]), 0)+if pidfd < 0:+ sys.exit(3)+fd = libc.syscall(438, pidfd, int(sys.argv[2]), 0)+err = ctypes.get_errno()+os.close(pidfd)+if fd >= 0:+ os.close(fd)+ sys.exit(0)+sys.exit(1 if err in (errno.EACCES, errno.EPERM) else 3)+'++before=$(counter SealPtrace) || test_skip_all "no tamper counters"+mkfifo "$W/fifo" || test_skip_all "cannot make a fifo"+"$SHIRUDO_BIN" bpm info "$W/fifo" >/dev/null 2>&1 &+PID=$!+i=0+while [ "$i" -lt 50 ]; do+ [ -d "/proc/$PID" ] || break+ [ "$(awk '{print $3}' "/proc/$PID/stat" 2>/dev/null)" = S ] && break+ sleep 0.1+ i=$((i + 1))+done+[ -d "/proc/$PID" ] || test_skip_all "shirudo did not stay around"+sleep 600 </dev/null &+OTHER=$!++test_expect_success 'another process gives its descriptors up to root' '+ python3 -c "$PROC" "$OTHER" 0 &&+ python3 -c "$GETFD" "$OTHER" 0+'++test_expect_success 'root cannot open a running shirudo descriptor through /proc' '+ for fd in 0 1 2; do+ python3 -c "$PROC" "$PID" "$fd"; [ "$?" = 1 ] || return 1+ done+'++test_expect_success 'nor copy one with pidfd_getfd' '+ for fd in 0 1 2; do+ python3 -c "$GETFD" "$PID" "$fd"; [ "$?" = 1 ] || return 1+ done+'++release++test_expect_success 'every refusal is counted as SealPtrace' '+ [ "$(counter SealPtrace)" -ge $((before + 6)) ]+'++test_donediff --git a/testing/functional/tamperproof/t6136-secure-exec.sh b/testing/functional/tamperproof/t6136-secure-exec.shnew file mode 100755index 00000000..4659e775--- /dev/null+++ b/testing/functional/tamperproof/t6136-secure-exec.sh@@ -0,0 +1,59 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t6136 -- nothing in the environment of whoever starts shirudo steers it.+# shirudo is static, with no program interpreter for LD_PRELOAD, LD_AUDIT+# or LD_LIBRARY_PATH to reach, and the kernel runs every executable of the+# runtime image in secure-exec mode, directly or as a script's interpreter,+# so its C library ignores the variables it would otherwise honour. The+# policy probe in the test runtime image reports the AT_SECURE it got; its+# copy outside the image is the control.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"++PROBE=/run/shirudo/runtime/usr/bin/shirudo-policy-probe+BIN=$(runtime_binary)+[ -x "$PROBE" ] || test_skip_all "the runtime image has no policy probe"+[ -x "$BIN" ] || test_skip_all "cannot find the runtime shirudo"+W=$(mktemp -d)+trap 'rm -rf "$W"' EXIT++test_expect_success 'shirudo has no program interpreter' '+ python3 - "$BIN" <<"PY"+import struct, sys+data = open(sys.argv[1], "rb").read()+assert data[:4] == b"\x7fELF" and data[4] == 2, "not a 64-bit ELF"+phoff, = struct.unpack_from("<Q", data, 32)+phentsize, phnum = struct.unpack_from("<HH", data, 54)+for i in range(phnum):+ p_type, = struct.unpack_from("<I", data, phoff + i * phentsize)+ assert p_type != 3, "PT_INTERP"+PY+'++test_expect_success 'a runtime executable runs with AT_SECURE' '+ [ "$("$PROBE" secure)" = 1 ]+'++test_expect_success 'whatever loader variables it is started with' '+ out=$(LD_PRELOAD=/nonexistent.so LD_AUDIT=/nonexistent.so \+ LD_LIBRARY_PATH="$W" "$PROBE" secure) &&+ [ "$out" = 1 ]+'++test_expect_success 'and as the interpreter of a script' '+ printf "#!%s secure\n" "$PROBE" >"$W/script" &&+ chmod 0755 "$W/script" &&+ [ "$("$W/script")" = 1 ]+'++test_expect_success 'its copy outside the image runs without' '+ cp "$PROBE" "$W/probe" &&+ [ "$("$W/probe" secure)" = 0 ]+'++test_donediff --git a/testing/functional/tamperproof/t6137-traced-exec.sh b/testing/functional/tamperproof/t6137-traced-exec.shnew file mode 100755index 00000000..1c6be5e7--- /dev/null+++ b/testing/functional/tamperproof/t6137-traced-exec.sh@@ -0,0 +1,58 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t6137 -- no traced task can exec shirudo, so no tracer carries over+# into it. The seal's exec check refuses it with EACCES before the+# admission check, which would refuse it with EPERM and count it as+# ExecTraced, is reached. The traced exec is of the runtime binary+# itself, not of the wrapper script that starts it.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"+ENV=$(command -v env) || test_skip_all "no env"+BIN=$(runtime_binary)+[ -x "$BIN" ] || test_skip_all "cannot find the runtime shirudo"++# Exec $@ in a child that asked to be traced: print the errno of a+# refused exec, or "ran" once the child stopped at the exec and was let go.+traced_exec() {+ python3 - "$@" <<'EOF'+import ctypes, os, sys+libc = ctypes.CDLL(None, use_errno=True)+PTRACE_TRACEME, PTRACE_DETACH = 0, 17+pid = os.fork()+if pid == 0:+ libc.ptrace(PTRACE_TRACEME, 0, None, None)+ try:+ os.execv(sys.argv[1], sys.argv[1:])+ except OSError as e:+ os.write(1, b"%d\n" % e.errno)+ os._exit(0)+_, status = os.waitpid(pid, 0)+if os.WIFSTOPPED(status):+ libc.ptrace(PTRACE_DETACH, pid, None, None)+ os.waitpid(pid, 0)+ print("ran")+EOF+}++test_expect_success 'a traced task execs an ordinary program' '+ [ "$(traced_exec "$ENV" true)" = ran ]+'++test_expect_success 'a traced task cannot exec shirudo' '+ out=$(traced_exec "$BIN" version) &&+ [ "$out" = 13 ] || {+ echo "traced exec of $BIN: ${out:-no outcome}"+ false+ }+'++test_expect_success 'shirudo still runs untraced' '+ "$SHIRUDO_BIN" version >/dev/null+'++test_donediff --git a/testing/functional/tamperproof/t6138-upatch-integrity.sh b/testing/functional/tamperproof/t6138-upatch-integrity.shnew file mode 100755index 00000000..5c3fa4a5--- /dev/null+++ b/testing/functional/tamperproof/t6138-upatch-integrity.sh@@ -0,0 +1,66 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t6138 -- a loaded upatch stays the one its signer made: a trusted+# loader with one bit flipped and its userspace digest recomputed passes+# every userspace check and is refused by the kernel's signature check,+# leaving no pin or archive, and root can neither change the pin's+# payload binding nor detach or update its link. The mutated loader keeps+# its signed name, which is covered by the signature; it is the lifecycle+# fixture's, which is not loaded.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"+FIXTURES=$SHIRUDO_TEST_FIXTURES_DIR+NAME=shirudo-test-basic+LOAD="$FIXTURES/$NAME.upatch"+PIN="/run/shirudo/bpffs/upatches/$NAME/link"+PINDIR="/run/shirudo/bpffs/upatches/$NAME"+ARCHIVE="/var/lib/shirudo/upatches/$NAME.upatch"+MUTATE=shirudo-test-lifecycle+HELPER=helper/test-upatch-link-lockdown+WORK=$(mktemp -d)+trap 'rm -rf "$WORK"' EXIT+[ -s "$LOAD" ] || test_skip_all "missing trusted basic upatch"+[ -s "$FIXTURES/$MUTATE.upatch" ] || test_skip_all "missing lifecycle fixture"+[ ! -e "/run/shirudo/bpffs/upatches/$MUTATE" ] ||+ test_skip_all "$MUTATE is loaded"+[ -x "$HELPER" ] || test_skip_all "upatch link helper is not built"+require_loaded "$NAME" || test_skip_all "cannot load the basic fixture"+[ -s "$ARCHIVE" ] || test_skip_all "basic fixture archive is missing"+python3 - "$FIXTURES/$MUTATE.upatch" "$WORK/mutated.upatch" <<'PY'+import hashlib, pathlib, struct, sys+src, dst = pathlib.Path(sys.argv[1]), pathlib.Path(sys.argv[2])+data = bytearray(src.read_bytes())+loader_off, loader_len = struct.unpack_from("<II", data, 304)+data_off, data_len = struct.unpack_from("<II", data, 328)+data[loader_off] ^= 1+payload = data[loader_off:loader_off + loader_len]+if data_len:+ payload += data[data_off:data_off + data_len]+data[272:304] = hashlib.sha256(payload).digest()+dst.write_bytes(data)+PY+test_expect_success 'trusted certificate cannot authorize a mutated loader' '+ "$SHIRUDO_BIN" upatch load "$WORK/mutated.upatch" --no-precheck \+ --report="$WORK/report.json" >/dev/null 2>&1 || true+ cat=$(report_field category <"$WORK/report.json") &&+ [ "$cat" = signature_rejected ] &&+ [ ! -e "/run/shirudo/bpffs/upatches/$MUTATE" ] &&+ [ ! -e "/var/lib/shirudo/upatches/$MUTATE.upatch" ]+'+test_expect_success 'root cannot change the live payload binding xattr' '+ before=$(python3 -c "import os,sys; print(os.getxattr(sys.argv[1], \"security.bpf.shirudo.payload\").hex())" "$PINDIR") &&+ ! python3 -c "import os,sys; os.setxattr(sys.argv[1], \"security.bpf.shirudo.payload\", bytes(32))" "$PINDIR" 2>/dev/null &&+ ! python3 -c "import os,sys; os.removexattr(sys.argv[1], \"security.bpf.shirudo.payload\")" "$PINDIR" 2>/dev/null &&+ after=$(python3 -c "import os,sys; print(os.getxattr(sys.argv[1], \"security.bpf.shirudo.payload\").hex())" "$PINDIR") &&+ [ "$before" = "$after" ]+'+test_expect_success 'root cannot detach or update a live upatch link' '+ "$HELPER" "$PIN" &&+ [ -e "$PIN" ]+'+test_donediff --git a/testing/functional/tamperproof/t6139-serializer-runs.sh b/testing/functional/tamperproof/t6139-serializer-runs.shnew file mode 100755index 00000000..948cda6b--- /dev/null+++ b/testing/functional/tamperproof/t6139-serializer-runs.sh@@ -0,0 +1,83 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t6139 -- repeated and concurrent serializer runs neither lose nor change+# records. A peek leaves the interval as it was, so two peeks report the+# same records; two drains started at once take turns at the operation+# lock, and every record ends up in exactly one of them, counted whole.+# The records are the execs of a marker program kernel-obs observes.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"++FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}+UNLOAD="$FIXTURES/kernel-obs.unload.upatch"+RUNS=7+[ -s "$UNLOAD" ] || test_skip_all "missing kernel-obs unload fixture"+[ -x /usr/bin/true ] || test_skip_all "no /usr/bin/true to copy"+require_loaded kernel-obs || test_skip_all "cannot load kernel-obs"+W=$(mktemp -d)+trap '"$SHIRUDO_BIN" upatch unload "$UNLOAD" >/dev/null 2>&1 || true; rm -rf "$W"' EXIT+MARKER=$W/t6139-marker+cp /usr/bin/true "$MARKER"++# The exec count of the marker across the records of the NDJSON files.+execs() {+ python3 - "$MARKER" "$@" <<'PY'+import json, sys+marker, total = sys.argv[1], 0+for path in sys.argv[2:]:+ with open(path) as f:+ for line in f:+ doc = json.loads(line)+ if isinstance(doc, dict) and doc.get("map") == "exec":+ total += sum(int(r["exec_count"]) for r in doc["records"]+ if r["path"] == marker)+print(total)+PY+}++# Export the exec table to the NDJSON file $1, with options to follow.+export_exec() {+ out=$1+ shift+ "$SHIRUDO_BIN" upatch map kernel-obs exec "--json=$out" "$@" \+ >/dev/null 2>"$out.err"+}++test_expect_success 'the marker runs into a drained table' '+ export_exec "$W/before.ndjson" &&+ i=0 &&+ while [ "$i" -lt "$RUNS" ]; do+ "$MARKER" || return 1+ i=$((i + 1))+ done+'++test_expect_success 'two peeks report the same records' '+ export_exec "$W/peek1.ndjson" --peek &&+ export_exec "$W/peek2.ndjson" --peek &&+ [ "$(execs "$W/peek1.ndjson")" = "$RUNS" ] &&+ [ "$(execs "$W/peek2.ndjson")" = "$RUNS" ]+'++test_expect_success 'two drains at once take each record once and whole' '+ export_exec "$W/drain1.ndjson" &+ a=$!+ export_exec "$W/drain2.ndjson" &+ b=$!+ wait "$a" && wait "$b" &&+ [ "$(execs "$W/drain1.ndjson" "$W/drain2.ndjson")" = "$RUNS" ] &&+ { [ "$(execs "$W/drain1.ndjson")" = 0 ] ||+ [ "$(execs "$W/drain2.ndjson")" = 0 ]; }+'++test_expect_success 'and leave none of them behind' '+ export_exec "$W/after.ndjson" --peek &&+ [ "$(execs "$W/after.ndjson")" = 0 ]+'++test_donediff --git a/testing/helper/policy-probe.c b/testing/helper/policy-probe.cindex edc44244..1fdc3565 100644--- a/testing/helper/policy-probe.c+++ b/testing/helper/policy-probe.c@@ -1,6 +1,8 @@// SPDX-License-Identifier: BSD-2-Clause/* Copyright (c) 2026 Authors of shirudo */+#include <sys/auxv.h>+#include "../../include/shirudo/misc.h"#include <errno.h>@@ -112,6 +114,11 @@ int main(int argc, char **argv)pid_t child;int status;+ /* As a script's interpreter the script's path follows the mode. */+ if (argc >= 2 && !strcmp(argv[1], "secure")) {+ printf("%lu\n", getauxval(AT_SECURE));+ return 0;+ }if (argc < 3)return 2;if (!strcmp(argv[1], "unlink"))@@ -120,6 +127,8 @@ int main(int argc, char **argv)return mkdir(argv[2], 0700) ? 1 : 0;if (!strcmp(argv[1], "rmdir"))return rmdir(argv[2]) ? 1 : 0;+ if (!strcmp(argv[1], "symlink") && argc == 4)+ return symlink(argv[2], argv[3]) ? 1 : 0;if (!strcmp(argv[1], "create")) {int fd = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, 0600);diff --git a/testing/integration-lib.sh b/testing/integration-lib.shindex 473ad092..198748b3 100644--- a/testing/integration-lib.sh+++ b/testing/integration-lib.sh@@ -29,13 +29,23 @@ target_guard() {test_skip_all "LSM not attached; run \`shirudo init\` first"}+# The executable $SHIRUDO_BIN runs: itself, or the one the runtime image's+# wrapper script hands over to.+runtime_binary() {+ if [ "$(head -c 2 "$SHIRUDO_BIN")" = '#!' ]; then+ sed -n 's/^exec \([^ ]*\).*/\1/p' "$SHIRUDO_BIN"+ else+ printf '%s\n' "$SHIRUDO_BIN"+ fi+}+# Load the signed fixture $1 unless it is loaded already: a family's tests# boot a VM of their own and find loaded only what they load themselves.require_loaded() {- [ -e "/run/shirudo/bpffs/upatches/$1/link" ] ||+ [ -d "/run/shirudo/bpffs/upatches/$1" ] ||"$SHIRUDO_BIN" upatch load \"${SHIRUDO_TEST_FIXTURES_DIR:?}/$1.upatch" >/dev/null 2>&1- [ -e "/run/shirudo/bpffs/upatches/$1/link" ]+ [ -d "/run/shirudo/bpffs/upatches/$1" ]}# Extract a top-level JSON field from a --report=- document on stdin.