Skip to content

eBPF-based security platform

bpf: run shirudo's executables in secure-exec mode

Daniel Borkmann44aacf678e7f
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rst
index 35a1e60e..8200af37 100644
--- a/Documentation/architecture.rst
+++ b/Documentation/architecture.rst
@@ -242,6 +242,18 @@ process that directly executes the recorded Shirudo binary is still
identified as Shirudo; its command interface is the authorization
boundary for those operations.
+``lsm/bprm_check_security`` keeps that boundary from being stepped
+around through the environment: every exec of the recorded executable
+or its initrd copy runs in secure-exec mode (``AT_SECURE``), as a
+set-user-ID program does. The dynamic loader then ignores
+``LD_PRELOAD``, ``LD_AUDIT``, ``LD_LIBRARY_PATH`` and the other
+variables glibc treats as unsafe, and libraries ignore what they read
+with ``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. glibc
+drops ``TMPDIR`` in this mode too, so the map export's snapshot file
+goes to ``/tmp`` on a target.
+
``lsm/bpf_prog_load`` runs after the kernel's signature check and admits
a program only when the verdict is ``BPF_SIG_VERIFIED`` and the
signature was resolved from the keyring whose kind shirudo froze into
diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rst
index f238ebe0..3d28e1d3 100644
--- a/Documentation/threat-model.rst
+++ b/Documentation/threat-model.rst
@@ -166,7 +166,9 @@ T3 Tampering with shirudo's files and process
``/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
+ privileges and locks sensitive buffers, and it runs in secure-exec
+ mode, so the dynamic loader takes no code from the environment of
+ whoever started it (residual risk 2). 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
@@ -305,7 +307,13 @@ Each of these is known, documented and bounded; none is silent.
Shirudo through these operations. This also denies those two
operations to checkpoint and restore tools. A process that directly
executes the recorded Shirudo binary is still identified as Shirudo,
- so its command interface remains the authorization boundary. Bound
+ so its command interface remains the authorization boundary. That
+ process 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
``CAP_SYS_RESOURCE`` out of untrusted contexts with
``CapabilityBoundingSet=`` and container capability profiles.
diff --git a/bpf/bpf2.c b/bpf/bpf2.c
index 2e9405d7..24c6f8d8 100644
--- a/bpf/bpf2.c
+++ b/bpf/bpf2.c
@@ -170,6 +170,21 @@ int BPF_PROG(shirudo_lsm_task_prctl, int option, unsigned long arg2,
return -ENOSYS;
}
+SEC("lsm/bprm_check_security")
+int BPF_PROG(shirudo_lsm_bprm_check_security, struct linux_binprm *bprm,
+ int ret)
+{
+ struct shirudo_sb_id *id;
+ __u32 zero = 0;
+
+ 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);
+ return 0;
+}
+
static __always_inline int shirudo_bpffs_guard(__u32 target_dev,
enum shirudo_tamper_point point)
{
diff --git a/bpf/lib/shirudo_identity.h b/bpf/lib/shirudo_identity.h
index e3435df8..61cfff26 100644
--- a/bpf/lib/shirudo_identity.h
+++ b/bpf/lib/shirudo_identity.h
@@ -36,21 +36,27 @@ struct {
__type(value, struct shirudo_sb_id);
} shirudo_bpffs_sb SEC(".maps");
-static __always_inline bool shirudo_task_is_self(const struct shirudo_sb_id *id)
+static __always_inline bool shirudo_file_is_self(const struct shirudo_sb_id *id,
+ struct file *file)
{
- struct task_struct *task = bpf_get_current_task_btf();
- struct file *exe = BPF_CORE_READ(task, mm, exe_file);
- __u32 exe_dev;
- __u64 exe_ino;
+ __u32 dev;
+ __u64 ino;
- if (!exe)
+ if (!file)
return false;
- exe_dev = BPF_CORE_READ(exe, f_inode, i_sb, s_dev);
- exe_ino = BPF_CORE_READ(exe, f_inode, i_ino);
- if (exe_dev == id->exe_dev && exe_ino == id->exe_ino)
+ dev = BPF_CORE_READ(file, f_inode, i_sb, s_dev);
+ ino = BPF_CORE_READ(file, f_inode, i_ino);
+ if (dev == id->exe_dev && ino == id->exe_ino)
return true;
- return id->alt_exe_ino && exe_dev == id->alt_exe_dev &&
- exe_ino == id->alt_exe_ino;
+ return id->alt_exe_ino && dev == id->alt_exe_dev &&
+ ino == id->alt_exe_ino;
+}
+
+static __always_inline bool shirudo_task_is_self(const struct shirudo_sb_id *id)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+
+ return shirudo_file_is_self(id, BPF_CORE_READ(task, mm, exe_file));
}
#endif /* SHIRUDO_BPF_IDENTITY_H */
diff --git a/builtin-init.c b/builtin-init.c
index 92ac05d6..ef94ce9b 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -759,10 +759,12 @@ static int init_lsm(bool attach_filesystem_seal)
static const struct {
const char *prog;
const char *pin;
- } prctl_hooks[] = {
+ } identity_hooks[] = {
{ "shirudo_lsm_task_prctl", SHIRUDO_TASK_PRCTL_LINK_PIN },
+ { "shirudo_lsm_bprm_check_security",
+ SHIRUDO_BPRM_CHECK_LINK_PIN },
};
- struct bpf_link *prctl_links[ARRAY_SIZE(prctl_hooks)] = {};
+ struct bpf_link *identity_links[ARRAY_SIZE(identity_hooks)] = {};
struct bpf_link *mount_links[ARRAY_SIZE(mount_hooks)] = {};
const struct shirudo_embedded_bpf *blob = bpf2_blob();
struct bpf_program *p_bpf, *p_bpf_map, *p_unlink, *p_setx, *p_rmx;
@@ -924,20 +926,21 @@ static int init_lsm(bool attach_filesystem_seal)
goto out;
}
- /* Install the narrow prctl guard before enabling the bpf() gates. */
- for (i = 0; i < (int)ARRAY_SIZE(prctl_hooks); i++) {
+ /* Install the identity guards before enabling the bpf() gates. */
+ for (i = 0; i < (int)ARRAY_SIZE(identity_hooks); i++) {
struct bpf_program *p;
- p = bpf_object__find_program_by_name(obj, prctl_hooks[i].prog);
+ p = bpf_object__find_program_by_name(obj,
+ identity_hooks[i].prog);
if (!p) {
pr_err("program %s missing in bpf2 object",
- prctl_hooks[i].prog);
+ identity_hooks[i].prog);
err = -ENOENT;
goto out;
}
- err = attach_pin(p, prctl_hooks[i].pin, &prctl_links[i]);
+ err = attach_pin(p, identity_hooks[i].pin, &identity_links[i]);
if (err) {
- pr_err("attach %s: %s", prctl_hooks[i].prog,
+ pr_err("attach %s: %s", identity_hooks[i].prog,
strerror(-err));
goto out;
}
@@ -1053,10 +1056,10 @@ static int init_lsm(bool attach_filesystem_seal)
", filesystem seal unavailable on this kernel");
err = 0;
out:
- for (i = 0; i < (int)ARRAY_SIZE(prctl_links); i++) {
- if (err && prctl_links[i])
- bpf_link__unpin(prctl_links[i]);
- bpf_link__destroy(prctl_links[i]);
+ for (i = 0; i < (int)ARRAY_SIZE(identity_links); i++) {
+ if (err && identity_links[i])
+ bpf_link__unpin(identity_links[i]);
+ bpf_link__destroy(identity_links[i]);
}
for (i = 0; i < (int)ARRAY_SIZE(seal_links); i++)
bpf_link__destroy(seal_links[i]);
diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.sh
index 94bd528e..c455e653 100755
--- a/testing/run-target-tests.sh
+++ b/testing/run-target-tests.sh
@@ -46,6 +46,7 @@ for t in \
tamperproof/t1136-tamper-counters.sh \
tamperproof/t1130-map-lockdown.sh \
tamperproof/t1131-pr-set-mm-identity.sh \
+ tamperproof/t1138-secure-exec.sh \
tamperproof/t1125-key-permission.sh \
tamperproof/t1132-upatch-integrity.sh \
t1133-upatch-arena-serializer.sh \
diff --git a/testing/tamperproof/t1138-secure-exec.sh b/testing/tamperproof/t1138-secure-exec.sh
new file mode 100644
index 00000000..4c93ac7f
--- /dev/null
+++ b/testing/tamperproof/t1138-secure-exec.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t1138 -- shirudo's executable runs in secure-exec mode, so the dynamic
+# loader takes nothing from the environment of whoever started it.
+
+cd "$(dirname "$0")/.."
+. ./integration-lib.sh
+target_guard
+grep -qa '/ld-linux' "$SHIRUDO_BIN" ||
+ test_skip_all "shirudo has no dynamic loader"
+ENV=$(command -v env) || test_skip_all "no env"
+W=$(mktemp -d)
+trap 'rm -f "$W/out" "$W/control" "$W/script"; rmdir "$W"' EXIT HUP INT TERM
+
+test_expect_success 'the loader shows other programs their auxiliary vector' '
+ LD_SHOW_AUXV=1 "$ENV" true >"$W/out" &&
+ grep -q "^AT_SECURE: *0$" "$W/out"
+'
+
+test_expect_success 'shirudo runs with AT_SECURE and its loader ignores LD_SHOW_AUXV' '
+ LD_SHOW_AUXV=1 "$SHIRUDO_BIN" version >"$W/out" &&
+ ! grep -q "^AT_" "$W/out"
+'
+
+test_expect_success 'shirudo started as a script interpreter runs with AT_SECURE too' '
+ printf "#!%s true\n" "$ENV" >"$W/control" &&
+ printf "#!%s\n" "$SHIRUDO_BIN" >"$W/script" &&
+ chmod 0755 "$W/control" "$W/script" &&
+ LD_SHOW_AUXV=1 "$W/control" >"$W/out" &&
+ grep -q "^AT_SECURE: *0$" "$W/out" &&
+ { LD_SHOW_AUXV=1 "$W/script" >"$W/out" 2>&1 || :; } &&
+ ! grep -q "^AT_" "$W/out"
+'
+
+test_done
diff --git a/testing/tamperproof/test.plan.md b/testing/tamperproof/test.plan.md
index bbe4b94f..57818955 100644
--- a/testing/tamperproof/test.plan.md
+++ b/testing/tamperproof/test.plan.md
@@ -49,6 +49,7 @@ checks that Shirudo can still export the pre-existing Python exec record.
| 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. |
+| 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-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/paths.h b/util/paths.h
index 5e59e6d6..9115ef81 100644
--- a/util/paths.h
+++ b/util/paths.h
@@ -69,6 +69,7 @@
#define SHIRUDO_INODE_RMXATTR_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_inode_removexattr_link"
#define SHIRUDO_KEY_PERMISSION_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_key_permission_link"
#define SHIRUDO_TASK_PRCTL_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_task_prctl_link"
+#define SHIRUDO_BPRM_CHECK_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_bprm_check_security_link"
#define SHIRUDO_SEAL_FILE_OPEN_PIN SHIRUDO_BPFFS_DIR "/seal_file_open_link"
#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"