eBPF-based security platform
bpf: refuse an exec of shirudo by a traced task
Daniel Borkmann3c39f531ba86
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex 407f35d4..aa268628 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -249,8 +249,14 @@ set-user-ID program does. The dynamic loader then ignores``LD_PRELOAD``, ``LD_AUDIT``, ``LD_LIBRARY_PATH`` and the othervariables glibc treats as unsafe, and libraries ignore what they readwith ``secure_getenv()``, among them OpenSSL's ``OPENSSL_CONF`` and-``OPENSSL_MODULES``. The hook runs on every binary handler pass, so-a script that names shirudo as its interpreter gets the same.+``OPENSSL_MODULES``. The same hook refuses the exec, counted as+``ExecTraced``, when the task is traced (``LSM_UNSAFE_PTRACE``): a+tracer that carries over into the new image, set up with+``PTRACE_TRACEME`` or attached to the parent, would own it, and the+seal's ptrace guard only stops a tracer attaching later. ``strace``+and ``gdb`` therefore cannot start shirudo on a target. The hook runs+on every binary handler pass, so a script that names shirudo as its+interpreter gets the same.``lsm/bpf_prog_load`` runs after the kernel's signature check and admitsa program only when the verdict is ``BPF_SIG_VERIFIED`` and thediff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rstindex 3d28e1d3..36bc863e 100644--- a/Documentation/threat-model.rst+++ b/Documentation/threat-model.rst@@ -165,7 +165,8 @@ T3 Tampering with shirudo's files and processMarked shirudo tasks cannot be ptraced, which also closes the``/proc`` files that need ptrace access, and no perf event can bebound to one of them; a system-wide profile still sees them as it- sees every task. The process drops dumpability, forbids new+ sees every task. No traced task can exec shirudo either, so no+ tracer carries over into it. The process drops dumpability, forbids newprivileges and locks sensitive buffers, and it runs in secure-execmode, so the dynamic loader takes no code from the environment ofwhoever started it (residual risk 2). The bpf keyring is sealed with@@ -309,11 +310,11 @@ Each of these is known, documented and bounded; none is silent.executes the recorded Shirudo binary is still identified as Shirudo,so its command interface remains the authorization boundary. Thatprocess runs in secure-exec mode, so ``LD_PRELOAD``, ``LD_AUDIT``- and the like cannot put the caller's code into it; the loader still- reads ``/etc/ld.so.preload`` and the shared libraries the binary- names, which root controls, and a tracer that attached before the- exec keeps its hold. A statically linked executable and a refusal- of traced execs would close those. Bound+ and the like cannot put the caller's code into it, and a traced+ task cannot start it at all. The loader still reads+ ``/etc/ld.so.preload`` and the shared libraries the binary names,+ which root controls; a statically linked executable would close+ that. Bound``CAP_SYS_RESOURCE`` out of untrusted contexts with``CapabilityBoundingSet=`` and container capability profiles.diff --git a/bpf/bpf2.c b/bpf/bpf2.cindex 24c6f8d8..bddbd99a 100644--- a/bpf/bpf2.c+++ b/bpf/bpf2.c@@ -26,6 +26,7 @@#ifndef FMODE_WRITE#define FMODE_WRITE 0x2#endif+#define LSM_UNSAFE_PTRACE 2#define PR_SET_MM 35#define PR_SET_MM_EXE_FILE 13#define PR_SET_MM_MAP 14@@ -180,8 +181,11 @@ int BPF_PROG(shirudo_lsm_bprm_check_security, struct linux_binprm *bprm,if (ret)return ret;id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);- if (id && shirudo_file_is_self(id, bprm->file))- bpf_bprm_opts_set(bprm, BPF_F_BPRM_SECUREEXEC);+ if (!id || !shirudo_file_is_self(id, bprm->file))+ return 0;+ if (BPF_CORE_READ(bprm, unsafe) & LSM_UNSAFE_PTRACE)+ return shirudo_tamper_deny(SHIRUDO_TAMPER_EXEC_TRACED, -EPERM);+ bpf_bprm_opts_set(bprm, BPF_F_BPRM_SECUREEXEC);return 0;}diff --git a/bpf/lib/shirudo_tamper.h b/bpf/lib/shirudo_tamper.hindex 955d8408..dfac84cf 100644--- a/bpf/lib/shirudo_tamper.h+++ b/bpf/lib/shirudo_tamper.h@@ -39,6 +39,7 @@ enum shirudo_tamper_point {SHIRUDO_TAMPER_SEAL_LABEL,SHIRUDO_TAMPER_SEAL_PTRACE,SHIRUDO_TAMPER_SEAL_PERF,+ SHIRUDO_TAMPER_EXEC_TRACED,SHIRUDO_TAMPER_MAX};diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.shindex c455e653..999ebf0c 100755--- a/testing/run-target-tests.sh+++ b/testing/run-target-tests.sh@@ -47,6 +47,7 @@ for t in \tamperproof/t1130-map-lockdown.sh \tamperproof/t1131-pr-set-mm-identity.sh \tamperproof/t1138-secure-exec.sh \+ tamperproof/t1139-traced-exec.sh \tamperproof/t1125-key-permission.sh \tamperproof/t1132-upatch-integrity.sh \t1133-upatch-arena-serializer.sh \diff --git a/testing/tamperproof/t1139-traced-exec.sh b/testing/tamperproof/t1139-traced-exec.shnew file mode 100644index 00000000..51029909--- /dev/null+++ b/testing/tamperproof/t1139-traced-exec.sh@@ -0,0 +1,58 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t1139 -- no traced task can exec shirudo, so no tracer carries over+# into it; the refusal is counted as ExecTraced.++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"++# 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+}++counter() {+ "$SHIRUDO_BIN" status --json | python3 -c '+import json, sys+print(json.load(sys.stdin)["tamper_attempts"]["counters"][sys.argv[1]])+' "$1"+}++test_expect_success 'a traced task execs an ordinary program' '+ [ "$(traced_exec "$ENV" true)" = ran ]+'++test_expect_success 'a traced task cannot exec shirudo, and the refusal is counted' '+ before=$(counter ExecTraced) &&+ [ "$(traced_exec "$SHIRUDO_BIN" version)" = 1 ] &&+ [ "$(counter ExecTraced)" -gt "$before" ]+'++test_expect_success 'shirudo still runs untraced' '+ "$SHIRUDO_BIN" version >/dev/null+'++test_donediff --git a/testing/tamperproof/test.plan.md b/testing/tamperproof/test.plan.mdindex 57818955..7ef1569d 100644--- a/testing/tamperproof/test.plan.md+++ b/testing/tamperproof/test.plan.md@@ -50,6 +50,7 @@ checks that Shirudo can still export the pre-existing Python exec record.| 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. || TP-30 | Root starts shirudo with `LD_PRELOAD`, `LD_AUDIT` or `LD_LIBRARY_PATH` naming its own code, directly or through a script that names shirudo as its interpreter | Covered | `t1138-secure-exec.sh` | `lsm/bprm_check_security` puts the recorded executables in secure-exec mode; `LD_SHOW_AUXV`, which glibc also ignores then, stands in for the others, with `env` as the positive control. `/etc/ld.so.preload` and a tracer attached before the exec stay open (residual risk 2). |+| TP-31 | Root execs shirudo from a traced task (`PTRACE_TRACEME`, or a tracer on the parent) so the tracer carries over into it | Covered | `t1139-traced-exec.sh` | `lsm/bprm_check_security` refuses the exec with `EPERM` when `LSM_UNSAFE_PTRACE` is set and counts `ExecTraced`; a traced exec of `env` is the positive control. || TP-22 | Mutate a trusted upatch loader and recompute its userspace digest | Covered | `t1132-upatch-integrity.sh` | The kernel signature gate rejects the altered loader as `signature_rejected`; no pin or archive is created. |diff --git a/util/tamper.c b/util/tamper.cindex 1ebd433e..592c3752 100644--- a/util/tamper.c+++ b/util/tamper.c@@ -76,6 +76,8 @@ static const struct {"ptrace of a shirudo task" },[SHIRUDO_TAMPER_SEAL_PERF] = { "SealPerf","perf event bound to a shirudo task" },+ [SHIRUDO_TAMPER_EXEC_TRACED] = { "ExecTraced",+ "exec of shirudo by a traced task" },};const char *tamper_name(enum shirudo_tamper_point point)