Skip to content

eBPF-based security platform

init: attach the seal's ptrace and perf hooks

Daniel Borkmann254c324de3fe
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rst
index 17028648..b6426b7f 100644
--- a/Documentation/architecture.rst
+++ b/Documentation/architecture.rst
@@ -299,8 +299,9 @@ strings, nothing a bind mount or a mount namespace can confuse.
``bprm_check_security`` and ``bprm_committed_creds`` mark tasks that
exec a labelled binary in task-local storage, ``task_alloc`` inherits
the mark, and ``ptrace_access_check`` and ``perf_event_alloc`` deny
-against marked tasks, so a root debugger or profiler cannot attach to a
-running shirudo; a forged label can only mark its forger. An inode
+against marked tasks, so a root debugger cannot attach to a running
+shirudo and a profiler cannot bind an event to one; a forged label can
+only mark its forger. An inode
created under a labelled directory is born with the label:
``inode_init_security`` hands it to the file system inside the
transaction that creates the inode, through
diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rst
index 1f48a801..9113b99d 100644
--- a/Documentation/threat-model.rst
+++ b/Documentation/threat-model.rst
@@ -156,9 +156,11 @@ T3 Tampering with shirudo's files and process
opened, unlinked or renamed by anyone else; what another process
creates there is sealed from the same instant, out of its own
reach as well.
- Marked shirudo tasks cannot be ptraced or perf-sampled, and the
- process drops dumpability, forbids new privileges and locks
- sensitive buffers. The bpf keyring is sealed with
+ Marked shirudo tasks cannot be ptraced, which also closes the
+ ``/proc`` files that need ptrace access, and no perf event can be
+ bound to one of them; a system-wide profile still sees them as it
+ sees every task. The process drops dumpability, forbids new
+ privileges and locks sensitive buffers. The bpf keyring is sealed with
``KEYCTL_RESTRICT_KEYRING`` so no key can be linked into it for the
rest of the boot, ``lsm/key_permission`` leaves writing, unlinking,
revoking, invalidating and re-permissioning it and the keys it
diff --git a/builtin-init.c b/builtin-init.c
index 0a98ad04..fc41b152 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -649,6 +649,8 @@ static const struct {
{ "shirudo_seal_bprm_committed_creds",
SHIRUDO_SEAL_BPRM_COMMITTED_PIN },
{ "shirudo_seal_task_alloc", SHIRUDO_SEAL_TASK_ALLOC_PIN },
+ { "shirudo_seal_ptrace_access_check", SHIRUDO_SEAL_PTRACE_PIN },
+ { "shirudo_seal_perf_event_alloc", SHIRUDO_SEAL_PERF_PIN },
{ "shirudo_seal_file_open", SHIRUDO_SEAL_FILE_OPEN_PIN },
{ "shirudo_seal_inode_post_setxattr",
SHIRUDO_SEAL_POST_SETXATTR_PIN },
diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.sh
index d876141b..c51fe17a 100755
--- a/testing/run-target-tests.sh
+++ b/testing/run-target-tests.sh
@@ -36,6 +36,7 @@ for t in \
tamperproof/t1101-seal.sh \
tamperproof/t1132-seal-scratch.sh \
tamperproof/t1133-seal-init-xattr.sh \
+ tamperproof/t1134-seal-task.sh \
tamperproof/t1105-siggate.sh \
tamperproof/t1110-lsm-deny.sh \
tamperproof/t1112-prog-load-keyring-gate.sh \
diff --git a/testing/tamperproof/t1134-seal-task.sh b/testing/tamperproof/t1134-seal-task.sh
new file mode 100755
index 00000000..18cdb0f1
--- /dev/null
+++ b/testing/tamperproof/t1134-seal-task.sh
@@ -0,0 +1,100 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t1134 -- the seal's task hooks (bpf/seal.c ptrace_access_check and
+# perf_event_alloc): a task that runs the labelled 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
+ [ -n "$PID" ] && wait "$PID" 2>/dev/null
+ 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
+
+test_expect_success 'the task is unharmed and finishes once released' '
+ [ -d "/proc/$PID" ] &&
+ 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" &&
+ wait "$PID"; PID=; true
+'
+
+test_done
diff --git a/testing/tamperproof/test.plan.md b/testing/tamperproof/test.plan.md
index b4895090..0d4a620c 100644
--- a/testing/tamperproof/test.plan.md
+++ b/testing/tamperproof/test.plan.md
@@ -45,6 +45,8 @@ checks that Shirudo can still export the pre-existing Python exec record.
| TP-23 | Root renames an unlabelled file over a labelled file and so replaces it | Covered | `t1132-seal-scratch.sh` | The seal checks the destination of a rename as well as its source; only the recorded executable may be replaced, as a package upgrade does. |
| TP-24 | Root truncates, chmods or chowns a labelled file without opening it | Covered | `t1132-seal-scratch.sh` | `truncate(2)` changes the contents without a write open, and `chmod` can make a labelled file unreadable to a caller that may still write it; the seal guards attribute changes like writes. |
| TP-25 | Root creates a file, directory, symlink or O_TMPFILE under a labelled directory | Covered | `t1133-seal-init-xattr.sh` | The inode is born labelled and sealed from that instant; a child of a new directory needs no reopen; the run directory is labelled; a file outside the tree is not. |
+| 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. |
## Review notes
diff --git a/util/paths.h b/util/paths.h
index bfa9b2ce..8c28783f 100644
--- a/util/paths.h
+++ b/util/paths.h
@@ -70,6 +70,8 @@
#define SHIRUDO_SEAL_BPRM_CHECK_PIN SHIRUDO_BPFFS_DIR "/seal_bprm_check_security_link"
#define SHIRUDO_SEAL_BPRM_COMMITTED_PIN SHIRUDO_BPFFS_DIR "/seal_bprm_committed_creds_link"
#define SHIRUDO_SEAL_TASK_ALLOC_PIN SHIRUDO_BPFFS_DIR "/seal_task_alloc_link"
+#define SHIRUDO_SEAL_PTRACE_PIN SHIRUDO_BPFFS_DIR "/seal_ptrace_access_check_link"
+#define SHIRUDO_SEAL_PERF_PIN SHIRUDO_BPFFS_DIR "/seal_perf_event_alloc_link"
#define SHIRUDO_SEAL_UNLINK_PIN SHIRUDO_BPFFS_DIR "/seal_inode_unlink_link"
#define SHIRUDO_SEAL_RENAME_PIN SHIRUDO_BPFFS_DIR "/seal_inode_rename_link"
#define SHIRUDO_SEAL_SETATTR_PIN SHIRUDO_BPFFS_DIR "/seal_inode_setattr_link"