Skip to content

eBPF-based security platform

lsm: count every refusal in a frozen tamper map

Daniel Borkmann5bbc8543b03a
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rst
index 9273e032..959f585f 100644
--- a/Documentation/architecture.rst
+++ b/Documentation/architecture.rst
@@ -284,7 +284,9 @@ the bpffs beneath it, which a lazy unmount would take down too. This
last set is load-bearing, not
defence in depth: shirudo is agentless, the pins are the only thing
keeping the enforcement programs alive, and one ``umount`` would
-otherwise detach everything.
+otherwise detach everything. Every guard counts what it refuses, by
+point, in a frozen map pinned beside the identity map
+(``bpf/lib/shirudo_tamper.h``); ``shirudo status`` reads it back.
Filesystem seal. ``bpf/seal.c`` relies on every inode under
``/var/lib/shirudo/`` and the installed binary carrying the xattr
diff --git a/Documentation/operations.rst b/Documentation/operations.rst
index a4b7770a..0a0c01eb 100644
--- a/Documentation/operations.rst
+++ b/Documentation/operations.rst
@@ -124,6 +124,19 @@ Conditions
Two init records for one TPM reset means the register was bound
again, which only a TPM reset should allow.
+``tamper_attempts``
+ What the guards refused this boot: ``total`` and one counter per
+ point of refusal under ``counters`` (``LoadNotLoader``,
+ ``LoadUnsigned``, ``LoadUnverified``, ``LoadKeyringKind``,
+ ``LoadKeyring``, ``MapFdWrite``, ``PinRename``,
+ ``BpffsUmountAbove``, ``SealWrite``, ``SealPtrace`` and the rest,
+ named in ``util/tamper.c`` like the condition types), or
+ ``null`` before ``init`` has run. The human report lists the
+ counters that are not zero with what each means under ``Tamper
+ attempts``. A count is an attempt that was refused, so the
+ conditions stay ``OK``; it is the operator's cue to look at the
+ journal and the host.
+
The same conditions leave the target every five seconds as the OTLP
keepalive, so a collector sees a target go ``WARN`` or ``FAIL``, or go
silent, without anyone logging in.
diff --git a/Documentation/telemetry.rst b/Documentation/telemetry.rst
index 5e3ccaf2..a7d7213a 100644
--- a/Documentation/telemetry.rst
+++ b/Documentation/telemetry.rst
@@ -216,8 +216,10 @@ Keepalive, one log record from ``shirudo status --otel``::
17 / ERROR lsm-off, tampered
body the status --json document as a key-value
list (schema, status, conditions, tpm,
- system, ...)
- attributes shirudo.status, shirudo.version_id
+ tamper_attempts, system, ...)
+ attributes shirudo.status, shirudo.version_id,
+ shirudo.tamper_attempts (the total of the
+ refusals the guards counted this boot)
The keepalive is sent only when an endpoint is configured explicitly,
through ``map_export.otel_endpoint`` or the
@@ -385,6 +387,13 @@ its ``Body``, ``SeverityNumber``, ``ResourceAttributes`` and
FROM shirudo.upatch_logs
WHERE SeverityNumber >= 13 AND Timestamp > now() - INTERVAL 5 MINUTE;
+ -- hosts where a guard refused something this boot
+ SELECT ResourceAttributes['host.name'] AS host,
+ max(toInt64(LogAttributes['shirudo.tamper_attempts'])) AS refused
+ FROM shirudo.upatch_logs
+ WHERE LogAttributes['shirudo.tamper_attempts'] != ''
+ GROUP BY host HAVING refused > 0;
+
-- the latest snapshot of one map on one host
SELECT Body FROM shirudo.upatch_logs
WHERE LogAttributes['shirudo.upatch.name'] = 'kernel-obs'
@@ -399,8 +408,9 @@ attributes.
Splunk. The ``splunk_hec`` exporter turns each log record into an HEC
event with the body as the event and the attributes as fields, so
``shirudo.status``, ``shirudo.upatch.name`` and ``shirudo.map.name``
-are searchable and ``index=shirudo shirudo.status!=ok`` lists the
-unhealthy hosts. Metrics go to a metrics index through the same
+are searchable, ``index=shirudo shirudo.status!=ok`` lists the
+unhealthy hosts and ``index=shirudo shirudo.tamper_attempts>0`` the
+hosts where a guard refused something. Metrics go to a metrics index through the same
exporter. An HEC endpoint can also be the direct target of a custom
sink as described below.
diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rst
index 205f9b94..687bd5c0 100644
--- a/Documentation/threat-model.rst
+++ b/Documentation/threat-model.rst
@@ -273,6 +273,22 @@ T9 A target lying about its state
owner's policy; ``--expect-pcr`` pins them and the logs in the
evidence explain them.
+T10 An attempt that leaves no trace
+ Every guard counts what it refuses, by the point of refusal (a
+ load that is no signed loader, one without a signature, one whose
+ signature was verified against the wrong kind of keyring or the
+ wrong keyring, a writable map descriptor, a rename of a pin, an
+ unmount above the bpffs, a write to a sealed file, a ptrace of a
+ shirudo task, and so on), in a map ``init`` pins and freezes, so
+ that no task, shirudo included, can reset it from userspace while
+ the hooks still add to it. ``shirudo status`` lists the counters
+ that are not zero with what each means, ``status --json`` carries
+ them all, and the keepalive takes them to the collector with the
+ total as the ``shirudo.tamper_attempts`` attribute, so an attempt
+ is seen where it was made and not only where it failed. A counter
+ is evidence of an attempt, not of a compromise: the refusal is what
+ moved it.
+
5) Residual risks
-----------------
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 0badf575..4bc13b99 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -138,6 +138,7 @@ UTIL_SRCS := \
util/keyring.c \
util/keys.c \
util/identity.c \
+ util/tamper.c \
util/initrd.c \
util/paths.c \
util/harden.c \
diff --git a/bpf/bpf2.c b/bpf/bpf2.c
index d44358a1..bd530b0c 100644
--- a/bpf/bpf2.c
+++ b/bpf/bpf2.c
@@ -88,6 +88,9 @@
* links' only reference. See the hook block below.
* lsm/task_prctl -- deny PR_SET_MM executable changes to callers
* outside the frozen Shirudo executable identity.
+ *
+ * Every refusal is counted at its point in the tamper map
+ * (shirudo_tamper.h), which `shirudo status` reads back.
*/
#include "vmlinux.h"
@@ -117,14 +120,25 @@
char _license[] SEC("license") = "Dual BSD/GPL";
#include "shirudo_identity.h"
+#include "shirudo_tamper.h"
+
+/* 0 for shirudo's own executable, else a counted -EACCES. */
+static __always_inline int shirudo_self_or_deny(enum shirudo_tamper_point point)
+{
+ struct shirudo_sb_id *id;
+ __u32 zero = 0;
+
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id || !shirudo_task_is_self(id))
+ return shirudo_tamper_deny(point, -EACCES);
+ return 0;
+}
SEC("lsm/bpf")
int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,
unsigned int size, bool kernel)
{
- struct shirudo_sb_id *id;
- __u32 prog_type, sig_sz;
- __u32 zero = 0;
+ enum shirudo_tamper_point point;
if (kernel)
return 0;
@@ -136,13 +150,13 @@ int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,
* non-zero signature; the kernel verifies the CMS blob
* against attr->keyring_id immediately after this hook.
*/
- prog_type = BPF_CORE_READ(attr, prog_type);
- if (prog_type != BPF_PROG_TYPE_SYSCALL)
- return -EACCES;
- sig_sz = BPF_CORE_READ(attr, signature_size);
- if (sig_sz == 0)
- return -EACCES;
- return 0;
+ if (BPF_CORE_READ(attr, prog_type) != BPF_PROG_TYPE_SYSCALL)
+ point = SHIRUDO_TAMPER_LOAD_NOT_LOADER;
+ else if (BPF_CORE_READ(attr, signature_size) == 0)
+ point = SHIRUDO_TAMPER_LOAD_UNSIGNED;
+ else
+ return 0;
+ return shirudo_tamper_deny(point, -EACCES);
/*
* Vetted mutating cmds the signed-loader data-map + attach/pin
@@ -158,36 +172,21 @@ int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,
* root's into the private bpffs is an entry shirudo did not make.
*/
case BPF_MAP_CREATE:
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id || !shirudo_task_is_self(id))
- return -EACCES;
- return 0;
+ return shirudo_self_or_deny(SHIRUDO_TAMPER_MAP_CREATE);
case BPF_MAP_UPDATE_ELEM:
case BPF_MAP_FREEZE:
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id || !shirudo_task_is_self(id))
- return -EACCES;
- return 0;
+ return shirudo_self_or_deny(SHIRUDO_TAMPER_MAP_WRITE);
case BPF_PROG_TEST_RUN:
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id || !shirudo_task_is_self(id))
- return -EACCES;
- return 0;
+ return shirudo_self_or_deny(SHIRUDO_TAMPER_PROG_RUN);
case BPF_LINK_CREATE:
case BPF_RAW_TRACEPOINT_OPEN:
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id || !shirudo_task_is_self(id))
- return -EACCES;
- return 0;
+ return shirudo_self_or_deny(SHIRUDO_TAMPER_ATTACH);
case BPF_OBJ_PIN:
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id || !shirudo_task_is_self(id))
- return -EACCES;
- return 0;
+ return shirudo_self_or_deny(SHIRUDO_TAMPER_OBJ_PIN);
/*
* Read-only / observability cmds, so `bpftool prog show`,
@@ -228,7 +227,7 @@ int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,
* denial errno used throughout (threat-model.rst T1/T2).
*/
default:
- return -EACCES;
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_BPF_CMD, -EACCES);
}
}
@@ -241,15 +240,9 @@ int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,
SEC("lsm/bpf_map")
int BPF_PROG(shirudo_lsm_bpf_map, struct bpf_map *map, fmode_t fmode)
{
- struct shirudo_sb_id *id;
- __u32 zero = 0;
-
if (!(fmode & FMODE_WRITE))
return 0;
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id || !shirudo_task_is_self(id))
- return -EACCES;
- return 0;
+ return shirudo_self_or_deny(SHIRUDO_TAMPER_MAP_FD);
}
/*
@@ -288,10 +281,18 @@ int BPF_PROG(shirudo_lsm_bpf_prog_load, struct bpf_prog *prog,
ktype = BPF_CORE_READ(prog, aux, sig.keyring_type);
serial = BPF_CORE_READ(prog, aux, sig.keyring_serial);
+ /*
+ * Three ways to fail, each counted apart: no verified signature
+ * on the program, a signature verified against a keyring of the
+ * wrong kind, or against a keyring of the right kind that is not
+ * the one init froze.
+ */
if (verdict != BPF_SIG_VERIFIED)
- return -EACCES;
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_UNVERIFIED,
+ -EACCES);
if (ktype != id->keyring_type)
- return -EACCES;
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_KEYRING_KIND,
+ -EACCES);
/*
* The bpf keyring is the kernel's own singleton, resolved by the
* kernel and recorded without a serial (0) because it is held
@@ -301,9 +302,11 @@ int BPF_PROG(shirudo_lsm_bpf_prog_load, struct bpf_prog *prog,
*/
if (ktype == BPF_SIG_KEYRING_BPF) {
if (serial && serial != id->keyring_serial)
- return -EACCES;
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_KEYRING,
+ -EACCES);
} else if (serial != id->keyring_serial) {
- return -EACCES;
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_KEYRING,
+ -EACCES);
}
return 0;
@@ -327,19 +330,20 @@ int BPF_PROG(shirudo_lsm_task_prctl, int option, unsigned long arg2,
if (!id)
return -EACCES;
if (!shirudo_task_is_self(id))
- return -EPERM;
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_EXE_IDENTITY, -EPERM);
return -ENOSYS;
}
/*
* Verdict for an operation targeting an inode in superblock target_dev:
* allow (0) if it is not shirudo's bpffs, or if the calling task's
- * exe_file matches the identity recorded at init; deny (-EPERM)
- * otherwise. The single guard shared by inode_unlink, inode_rename,
- * inode_setxattr and inode_removexattr -- "only shirudo may mutate its
- * own bpffs".
+ * exe_file matches the identity recorded at init; deny (-EPERM),
+ * counted at `point`, otherwise. The single guard shared by
+ * inode_unlink, inode_rename, inode_setxattr and inode_removexattr --
+ * "only shirudo may mutate its own bpffs".
*/
-static __always_inline int shirudo_bpffs_guard(__u32 target_dev)
+static __always_inline int shirudo_bpffs_guard(__u32 target_dev,
+ enum shirudo_tamper_point point)
{
struct shirudo_sb_id *id;
__u32 zero = 0;
@@ -349,7 +353,8 @@ static __always_inline int shirudo_bpffs_guard(__u32 target_dev)
return 0;
if (target_dev != id->sb_dev)
return 0; /* not shirudo's bpffs -- not ours to guard */
- return shirudo_task_is_self(id) ? 0 : -EPERM;
+ return shirudo_task_is_self(id) ? 0 :
+ shirudo_tamper_deny(point, -EPERM);
}
/*
@@ -397,7 +402,8 @@ int BPF_PROG(shirudo_lsm_key_permission, key_ref_t key_ref,
}
if (need == KEY_NEED_SEARCH && possessed)
return 0;
- return shirudo_task_is_self(id) ? 0 : -EACCES;
+ return shirudo_task_is_self(id) ? 0 :
+ shirudo_tamper_deny(SHIRUDO_TAMPER_KEYRING, -EACCES);
}
/*
@@ -410,7 +416,8 @@ SEC("lsm/inode_unlink")
int BPF_PROG(shirudo_lsm_inode_unlink, struct inode *dir,
struct dentry *dentry)
{
- return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev));
+ return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),
+ SHIRUDO_TAMPER_PIN_UNLINK);
}
/*
@@ -425,7 +432,8 @@ int BPF_PROG(shirudo_lsm_inode_rename, struct inode *old_dir,
struct dentry *old_dentry, struct inode *new_dir,
struct dentry *new_dentry)
{
- return shirudo_bpffs_guard(BPF_CORE_READ(old_dir, i_sb, s_dev));
+ return shirudo_bpffs_guard(BPF_CORE_READ(old_dir, i_sb, s_dev),
+ SHIRUDO_TAMPER_PIN_RENAME);
}
/*
@@ -438,7 +446,8 @@ int BPF_PROG(shirudo_lsm_inode_setxattr, struct mnt_idmap *idmap,
struct dentry *dentry, const char *name, const void *value,
__u64 size, int flags)
{
- return shirudo_bpffs_guard(BPF_CORE_READ(dentry, d_inode, i_sb, s_dev));
+ return shirudo_bpffs_guard(BPF_CORE_READ(dentry, d_inode, i_sb, s_dev),
+ SHIRUDO_TAMPER_PIN_XATTR);
}
/* Symmetric to setxattr: deny removing a pin's xattrs from non-shirudo. */
@@ -446,7 +455,8 @@ SEC("lsm/inode_removexattr")
int BPF_PROG(shirudo_lsm_inode_removexattr, struct mnt_idmap *idmap,
struct dentry *dentry, const char *name)
{
- return shirudo_bpffs_guard(BPF_CORE_READ(dentry, d_inode, i_sb, s_dev));
+ return shirudo_bpffs_guard(BPF_CORE_READ(dentry, d_inode, i_sb, s_dev),
+ SHIRUDO_TAMPER_PIN_XATTR);
}
/*
@@ -473,15 +483,24 @@ int BPF_PROG(shirudo_lsm_inode_removexattr, struct mnt_idmap *idmap,
* bpffs beneath it. Nothing unmounts /run or the root on a live
* system; switch-root moves /run and never unmounts it.
*/
-static __always_inline int shirudo_deny_bpffs_mount(__u32 sb_dev)
+static __always_inline __u32 shirudo_bpffs_dev(void)
{
struct shirudo_sb_id *id;
__u32 zero = 0;
id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id || id->sb_dev == 0)
+ return id ? id->sb_dev : 0;
+}
+
+/* -EPERM, counted at `point`, when sb_dev is the bpffs; else 0. */
+static __always_inline int
+shirudo_deny_bpffs_mount(__u32 sb_dev, enum shirudo_tamper_point point)
+{
+ __u32 bpffs = shirudo_bpffs_dev();
+
+ if (!bpffs || sb_dev != bpffs)
return 0;
- return sb_dev == id->sb_dev ? -EPERM : 0;
+ return shirudo_tamper_deny(point, -EPERM);
}
/* The address a trusted pointer holds, as a plain number. */
@@ -547,26 +566,30 @@ static __always_inline bool shirudo_mount_tree_has(struct mount *root,
SEC("lsm/sb_umount")
int BPF_PROG(shirudo_lsm_sb_umount, struct vfsmount *mnt, int flags)
{
- struct shirudo_sb_id *id;
+ __u32 bpffs = shirudo_bpffs_dev();
struct mount *target;
- __u32 zero = 0;
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id || id->sb_dev == 0)
+ if (!bpffs)
return 0;
- if (BPF_CORE_READ(mnt, mnt_sb, s_dev) == id->sb_dev)
- return -EPERM;
+ if (BPF_CORE_READ(mnt, mnt_sb, s_dev) == bpffs)
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_BPFFS_UMOUNT, -EPERM);
target = shirudo_mount_of(shirudo_ptr_addr(mnt), mnt);
- return shirudo_mount_tree_has(target, id->sb_dev) ? -EPERM : 0;
+ if (shirudo_mount_tree_has(target, bpffs))
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_BPFFS_UMOUNT_ABOVE,
+ -EPERM);
+ return 0;
}
SEC("lsm/move_mount")
int BPF_PROG(shirudo_lsm_move_mount, const struct path *from_path,
const struct path *to_path)
{
- if (shirudo_deny_bpffs_mount(BPF_CORE_READ(from_path, mnt, mnt_sb, s_dev)))
+ __u32 from = BPF_CORE_READ(from_path, mnt, mnt_sb, s_dev);
+ __u32 to = BPF_CORE_READ(to_path, mnt, mnt_sb, s_dev);
+
+ if (shirudo_deny_bpffs_mount(from, SHIRUDO_TAMPER_BPFFS_MOUNT))
return -EPERM;
- return shirudo_deny_bpffs_mount(BPF_CORE_READ(to_path, mnt, mnt_sb, s_dev));
+ return shirudo_deny_bpffs_mount(to, SHIRUDO_TAMPER_BPFFS_MOUNT);
}
/*
@@ -581,11 +604,13 @@ int BPF_PROG(shirudo_lsm_sb_mount, const char *dev_name,
const struct path *path, const char *type,
unsigned long flags, void *data)
{
- return shirudo_deny_bpffs_mount(BPF_CORE_READ(path, mnt, mnt_sb, s_dev));
+ return shirudo_deny_bpffs_mount(BPF_CORE_READ(path, mnt, mnt_sb, s_dev),
+ SHIRUDO_TAMPER_BPFFS_MOUNT);
}
SEC("lsm/sb_remount")
int BPF_PROG(shirudo_lsm_sb_remount, struct super_block *sb, void *mnt_opts)
{
- return shirudo_deny_bpffs_mount(BPF_CORE_READ(sb, s_dev));
+ return shirudo_deny_bpffs_mount(BPF_CORE_READ(sb, s_dev),
+ SHIRUDO_TAMPER_BPFFS_MOUNT);
}
diff --git a/bpf/lib/shirudo_tamper.h b/bpf/lib/shirudo_tamper.h
new file mode 100644
index 00000000..ae4707ac
--- /dev/null
+++ b/bpf/lib/shirudo_tamper.h
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The tamper counters: one per point at which a guard refuses a caller,
+ * counted where the refusal happens and read back by `shirudo status`.
+ * Include after vmlinux.h and the libbpf BPF headers.
+ */
+
+#ifndef SHIRUDO_BPF_TAMPER_H
+#define SHIRUDO_BPF_TAMPER_H
+
+/*
+ * Must match SHIRUDO_TAMPER_POINTS in util/tamper.h in order and
+ * number; the reader refuses a map of another size.
+ */
+enum shirudo_tamper_point {
+ SHIRUDO_TAMPER_LOAD_NOT_LOADER,
+ SHIRUDO_TAMPER_LOAD_UNSIGNED,
+ SHIRUDO_TAMPER_LOAD_UNVERIFIED,
+ SHIRUDO_TAMPER_LOAD_KEYRING_KIND,
+ SHIRUDO_TAMPER_LOAD_KEYRING,
+ SHIRUDO_TAMPER_MAP_CREATE,
+ SHIRUDO_TAMPER_MAP_WRITE,
+ SHIRUDO_TAMPER_MAP_FD,
+ SHIRUDO_TAMPER_PROG_RUN,
+ SHIRUDO_TAMPER_ATTACH,
+ SHIRUDO_TAMPER_OBJ_PIN,
+ SHIRUDO_TAMPER_BPF_CMD,
+ SHIRUDO_TAMPER_EXE_IDENTITY,
+ SHIRUDO_TAMPER_KEYRING,
+ SHIRUDO_TAMPER_PIN_UNLINK,
+ SHIRUDO_TAMPER_PIN_RENAME,
+ SHIRUDO_TAMPER_PIN_XATTR,
+ SHIRUDO_TAMPER_BPFFS_UMOUNT,
+ SHIRUDO_TAMPER_BPFFS_UMOUNT_ABOVE,
+ SHIRUDO_TAMPER_BPFFS_MOUNT,
+ SHIRUDO_TAMPER_SEAL_WRITE,
+ SHIRUDO_TAMPER_SEAL_UNLINK,
+ SHIRUDO_TAMPER_SEAL_RENAME,
+ SHIRUDO_TAMPER_SEAL_SETATTR,
+ SHIRUDO_TAMPER_SEAL_XATTR,
+ SHIRUDO_TAMPER_SEAL_LABEL,
+ SHIRUDO_TAMPER_SEAL_PTRACE,
+ SHIRUDO_TAMPER_SEAL_PERF,
+ SHIRUDO_TAMPER_MAX
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, SHIRUDO_TAMPER_MAX);
+ __type(key, __u32);
+ __type(value, __u64);
+} shirudo_tamper SEC(".maps");
+
+/*
+ * Count a refusal at `point` and hand back `err`, so that a deny reads
+ * `return shirudo_tamper_deny(SHIRUDO_TAMPER_X, -EACCES);`. Atomic,
+ * since refusals on two CPUs may race, and cheap enough for a path
+ * that is the exception.
+ */
+static __always_inline int shirudo_tamper_deny(enum shirudo_tamper_point point,
+ int err)
+{
+ __u32 key = point;
+ __u64 *count;
+
+ count = bpf_map_lookup_elem(&shirudo_tamper, &key);
+ if (count)
+ __sync_fetch_and_add(count, 1);
+ return err;
+}
+
+#endif /* SHIRUDO_BPF_TAMPER_H */
diff --git a/bpf/seal.c b/bpf/seal.c
index 78180648..1afdc242 100644
--- a/bpf/seal.c
+++ b/bpf/seal.c
@@ -21,7 +21,8 @@
* not read the file, is taken to be there. No bpf_d_path(), no path
* strings: the label rides the inode, immune to bind-mount / namespace /
* TOCTOU games. Reads are not gated; only write-mode opens and the
- * mutating inode ops are.
+ * mutating inode ops are. Every refusal is counted in the tamper map
+ * bpf2 shares with the seal (shirudo_tamper.h).
*
* An inode created under a labelled directory is born with the label:
* lsm/inode_init_security hands it to the filesystem inside the
@@ -46,6 +47,7 @@
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "shirudo_identity.h"
+#include "shirudo_tamper.h"
#ifndef EPERM
#define EPERM 1
@@ -110,8 +112,11 @@ extern int bpf_inode_init_xattr(struct xattr *xattrs,
const char *name__str,
const struct bpf_dynptr *value_p) __ksym;
-/* 0 if the caller runs the executable recorded at init, else -EACCES. */
-static __always_inline int seal_caller(void)
+/*
+ * 0 if the caller runs the executable recorded at init, else -EACCES,
+ * counted at `point`.
+ */
+static __always_inline int seal_caller(enum shirudo_tamper_point point)
{
struct shirudo_sb_id *id;
__u32 zero = 0;
@@ -119,7 +124,8 @@ static __always_inline int seal_caller(void)
id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
if (!id)
return -EACCES;
- return shirudo_task_is_self(id) ? 0 : -EACCES;
+ return shirudo_task_is_self(id) ? 0 :
+ shirudo_tamper_deny(point, -EACCES);
}
/*
@@ -131,7 +137,8 @@ static __always_inline int seal_caller(void)
* unreadable file. A label of any size, or any other error, requires the
* caller to be shirudo. 0 = allow, -EACCES = deny.
*/
-static __always_inline int seal_verdict(int tlen)
+static __always_inline int seal_verdict(int tlen,
+ enum shirudo_tamper_point point)
{
switch (tlen) {
case -ENODATA:
@@ -140,7 +147,7 @@ static __always_inline int seal_verdict(int tlen)
case -EPERM:
return 0;
default:
- return seal_caller();
+ return seal_caller(point);
}
}
@@ -268,7 +275,7 @@ int BPF_PROG(shirudo_seal_perf_event_alloc,
s = bpf_task_storage_get(&seal_scratch_map, target, NULL, 0);
if (s && s->exe_is_shirudo)
- return -EACCES;
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_SEAL_PERF, -EACCES);
return 0;
}
@@ -286,7 +293,7 @@ int BPF_PROG(shirudo_seal_ptrace_access_check,
s = bpf_task_storage_get(&seal_scratch_map, child, NULL, 0);
if (s && s->exe_is_shirudo)
- return -EACCES;
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_SEAL_PTRACE, -EACCES);
return 0;
}
@@ -313,10 +320,11 @@ int BPF_PROG(shirudo_seal_file_open, struct file *file)
if (!(BPF_CORE_READ(file, f_mode) & FMODE_WRITE))
return 0; /* reads are fine */
s = seal_scratch_get(task);
- if (!s)
- return seal_caller(); /* cannot read the label */
+ if (!s) /* cannot read the label */
+ return seal_caller(SHIRUDO_TAMPER_SEAL_WRITE);
bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);
- return seal_verdict(bpf_get_file_xattr(file, SHIRUDO_XATTR, &tdp));
+ return seal_verdict(bpf_get_file_xattr(file, SHIRUDO_XATTR, &tdp),
+ SHIRUDO_TAMPER_SEAL_WRITE);
}
/* Refresh a directory's mark from its label after an xattr change. */
@@ -386,23 +394,25 @@ int BPF_PROG(shirudo_seal_inode_init_security, struct inode *inode,
return 0;
}
-static __always_inline int seal_dentry(struct dentry *dentry)
+static __always_inline int seal_dentry(struct dentry *dentry,
+ enum shirudo_tamper_point point)
{
struct task_struct *task = bpf_get_current_task_btf();
struct seal_scratch *s = seal_scratch_get(task);
struct bpf_dynptr tdp;
if (!s)
- return seal_caller();
+ return seal_caller(point);
bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);
- return seal_verdict(bpf_get_dentry_xattr(dentry, SHIRUDO_XATTR, &tdp));
+ return seal_verdict(bpf_get_dentry_xattr(dentry, SHIRUDO_XATTR, &tdp),
+ point);
}
SEC("lsm.s/inode_unlink")
int BPF_PROG(shirudo_seal_inode_unlink, struct inode *dir,
struct dentry *dentry)
{
- return seal_dentry(dentry);
+ return seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_UNLINK);
}
/* Whether an inode is the executable recorded at init. */
@@ -429,12 +439,12 @@ int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,
struct inode *target = BPF_CORE_READ(new_dentry, d_inode);
int err;
- err = seal_dentry(old_dentry);
+ err = seal_dentry(old_dentry, SHIRUDO_TAMPER_SEAL_RENAME);
if (err)
return err;
if (!target || seal_is_exe(target))
return 0;
- return seal_dentry(new_dentry);
+ return seal_dentry(new_dentry, SHIRUDO_TAMPER_SEAL_RENAME);
}
/* Truncate, chmod, chown and utimes all come through here. */
@@ -442,7 +452,7 @@ SEC("lsm.s/inode_setattr")
int BPF_PROG(shirudo_seal_inode_setattr, struct mnt_idmap *idmap,
struct dentry *dentry, struct iattr *attr)
{
- return seal_dentry(dentry);
+ return seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_SETATTR);
}
SEC("lsm.s/inode_setxattr")
@@ -451,13 +461,13 @@ int BPF_PROG(shirudo_seal_inode_setxattr, struct mnt_idmap *idmap,
__u64 size, int flags)
{
if (seal_is_label(name))
- return seal_caller();
- return seal_dentry(dentry);
+ return seal_caller(SHIRUDO_TAMPER_SEAL_LABEL);
+ return seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_XATTR);
}
SEC("lsm.s/inode_removexattr")
int BPF_PROG(shirudo_seal_inode_removexattr, struct mnt_idmap *idmap,
struct dentry *dentry, const char *name)
{
- return seal_dentry(dentry);
+ return seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_XATTR);
}
diff --git a/builtin-init.c b/builtin-init.c
index bd5972de..12646551 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -672,10 +672,11 @@ static const struct {
* unsigned and is not prog_type SYSCALL, so once the bpf() gate is live
* it would refuse this very load -- the same bootstrap window bpf2 uses.
* identity_fd is bpf2's frozen identity map, which the seal reads to
- * recognise shirudo's executable instead of declaring its own copy.
+ * recognise shirudo's executable instead of declaring its own copy, and
+ * tamper_fd its counters, so one set counts the refusals of both.
*/
static int attach_seal(struct bpf_object **obj_out, struct bpf_link **links,
- int identity_fd)
+ int identity_fd, int tamper_fd)
{
const struct shirudo_embedded_bpf *blob = shirudo_seal_blob();
struct bpf_object *obj;
@@ -705,6 +706,18 @@ static int attach_seal(struct bpf_object **obj_out, struct bpf_link **links,
bpf_object__close(obj);
return err;
}
+ m = bpf_object__find_map_by_name(obj, "shirudo_tamper");
+ if (!m) {
+ pr_err("seal: shirudo_tamper map not in embedded seal");
+ bpf_object__close(obj);
+ return -ENOENT;
+ }
+ err = bpf_map__reuse_fd(m, tamper_fd);
+ if (err) {
+ pr_err("seal: share the tamper map: %s", strerror(-err));
+ bpf_object__close(obj);
+ return err;
+ }
err = bpf_object__load(obj);
if (err) {
pr_err("seal: bpf_object__load: %d", err);
@@ -797,10 +810,10 @@ static int init_lsm(bool attach_filesystem_seal)
struct bpf_program *p_keyperm;
struct anchor_keys keys = {};
key_serial_t keyring;
- struct bpf_map *m_sb;
+ struct bpf_map *m_sb, *m_tamper;
struct stat st;
uint32_t zero = 0;
- int err, map_fd, i;
+ int err, map_fd, tamper_fd, i;
if (access(SHIRUDO_LSM_LINK_PIN, F_OK) == 0) {
pr_info("bpf2 already attached (%s exists)",
@@ -813,6 +826,11 @@ static int init_lsm(bool attach_filesystem_seal)
strerror(errno));
return -errno;
}
+ if (unlink(SHIRUDO_TAMPER_PIN) != 0 && errno != ENOENT) {
+ pr_err("unlink stale %s: %s", SHIRUDO_TAMPER_PIN,
+ strerror(errno));
+ return -errno;
+ }
if (!blob || !blob->data || !blob->size) {
pr_err("embedded bpf2 blob missing");
@@ -920,6 +938,30 @@ static int init_lsm(bool attach_filesystem_seal)
goto out;
}
+ /*
+ * The tamper counters, pinned for status to read and frozen so
+ * that no task, shirudo included, can reset them from userspace;
+ * the hooks write them.
+ */
+ m_tamper = bpf_object__find_map_by_name(obj, "shirudo_tamper");
+ if (!m_tamper) {
+ pr_err("shirudo_tamper map not in embedded bpf2");
+ err = -ENOENT;
+ goto out;
+ }
+ tamper_fd = bpf_map__fd(m_tamper);
+ if (bpf_map_freeze(tamper_fd) != 0) {
+ err = -errno;
+ pr_err("freeze tamper map: %s", strerror(errno));
+ goto out;
+ }
+ if (bpf_obj_pin(tamper_fd, SHIRUDO_TAMPER_PIN) != 0) {
+ err = -errno;
+ pr_err("pin tamper map at %s: %s", SHIRUDO_TAMPER_PIN,
+ strerror(errno));
+ goto out;
+ }
+
/* Install the narrow prctl guard before enabling the bpf() gates. */
for (i = 0; i < (int)ARRAY_SIZE(prctl_hooks); i++) {
struct bpf_program *p = bpf_object__find_program_by_name(
@@ -1022,7 +1064,7 @@ static int init_lsm(bool attach_filesystem_seal)
}
if (attach_filesystem_seal) {
- err = attach_seal(&seal_obj, seal_links, map_fd);
+ err = attach_seal(&seal_obj, seal_links, map_fd, tamper_fd);
if (err)
goto out;
}
diff --git a/builtin-status.c b/builtin-status.c
index f9f66d4f..49798d3c 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -53,6 +53,7 @@
#include "util/timefmt.h"
#include "util/upatch_maps.h"
#include "util/upatch_maps_otel.h"
+#include "util/tamper.h"
#include "util/upatch_resolve.h"
#include "util/upatch_table.h"
@@ -585,6 +586,8 @@ int cmd_status(int argc, const char **argv)
struct shirudo_config otel_cfg = {};
struct shirudo_otel_opts otel_opts = {};
struct asset_view assets;
+ struct shirudo_tamper_counts tamper = {};
+ int tamper_rc = -ENOENT;
struct dirent **ups = NULL;
char pcr_before_hex[2 * SHIRUDO_CHAIN_DIGEST_LEN + 1] = "";
char phase_before_hex[2 * SHIRUDO_CHAIN_DIGEST_LEN + 1] = "";
@@ -731,6 +734,7 @@ int cmd_status(int argc, const char **argv)
n_ups = 0;
gather_assets(&assets);
+ tamper_rc = shirudo_tamper_read(&tamper);
{
snprintf(nv_index, sizeof(nv_index), "0x%08X", SHIRUDO_NV_INDEX);
@@ -907,6 +911,23 @@ int cmd_status(int argc, const char **argv)
shirudo_json_kv_null(&e, "incoming");
shirudo_json_kv_bool(&e, "bpffs_mounted", assets.bpffs_mounted);
shirudo_json_obj_close(&e);
+ /*
+ * What the guards refused this boot, every counter by name
+ * so a collector's keys stay the same from one record to
+ * the next; null until init has pinned the map.
+ */
+ if (tamper_rc == 0) {
+ shirudo_json_obj_open_key(&e, "tamper_attempts");
+ shirudo_json_kv_int(&e, "total", (int64_t)tamper.total);
+ shirudo_json_obj_open_key(&e, "counters");
+ for (i = 0; i < SHIRUDO_TAMPER_MAX; i++)
+ shirudo_json_kv_int(&e, shirudo_tamper_name(i),
+ (int64_t)tamper.count[i]);
+ shirudo_json_obj_close(&e);
+ shirudo_json_obj_close(&e);
+ } else {
+ shirudo_json_kv_null(&e, "tamper_attempts");
+ }
if (include_system)
shirudo_sysinfo_emit_json(&e, &sysi);
{
@@ -1055,6 +1076,8 @@ int cmd_status(int argc, const char **argv)
strerror(-e.err));
else {
if (otel && otel_endpoint_configured) {
+ int64_t refused = tamper_rc == 0 ?
+ (int64_t)tamper.total : -1;
const char *severity_text;
int severity_number;
int rc;
@@ -1065,8 +1088,8 @@ int cmd_status(int argc, const char **argv)
&otel_opts, "shirudo.keepalive",
"shirudo.keepalive", severity_number,
severity_text, otel_status,
- shirudo_version_id, json_buf,
- strlen(json_buf));
+ shirudo_version_id, refused,
+ json_buf, strlen(json_buf));
if (rc) {
pr_err("status: OTEL export failed: %s",
strerror(-rc));
@@ -1123,6 +1146,21 @@ int cmd_status(int argc, const char **argv)
printf("\n");
shirudo_conditions_print(conds, nc);
+ printf("\nTamper attempts (refused this boot):\n");
+ if (tamper_rc == -ENOENT)
+ printf(" (none recorded: init has not pinned the counters "
+ "this boot)\n");
+ else if (tamper_rc)
+ printf(" (unavailable: %s)\n", strerror(-tamper_rc));
+ else if (tamper.total == 0)
+ printf(" none\n");
+ for (i = 0; tamper_rc == 0 && i < SHIRUDO_TAMPER_MAX; i++)
+ if (tamper.count[i])
+ printf(" %-20s %6llu %s\n",
+ shirudo_tamper_name(i),
+ (unsigned long long)tamper.count[i],
+ shirudo_tamper_meaning(i));
+
printf("\nKeyring (%s):\n", SHIRUDO_KEYRING_NAME);
if (keyring_present)
printf(" %-22s %lld\n", "Serial:",
diff --git a/testing/t0018-status-otel.sh b/testing/t0018-status-otel.sh
index 5e8d8689..1c42016d 100644
--- a/testing/t0018-status-otel.sh
+++ b/testing/t0018-status-otel.sh
@@ -76,6 +76,7 @@ doc = json.load(open(sys.argv[1]))
assert doc["schema"] == 1
assert "status" in doc
assert "tpm" in doc
+assert "tamper_attempts" in doc
PY
'
diff --git a/testing/t0048-tamper-points.sh b/testing/t0048-tamper-points.sh
new file mode 100755
index 00000000..a10f2059
--- /dev/null
+++ b/testing/t0048-tamper-points.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t0048 -- the tamper points are declared twice, as the enum the LSM
+# counts with (bpf/lib/shirudo_tamper.h) and as the enum status names
+# them from (util/tamper.h), and a counter is only as good as the two
+# agreeing; this keeps them in the same order with the same members,
+# each with a name and a meaning in the table util/tamper.c holds.
+# Also checks that status reports the counters as absent, not as an
+# error, where init has not run.
+
+cd "$(dirname "$0")"
+. ./test-lib.sh
+
+S=../shirudo
+W=$(mktemp -d)
+trap 'rm -rf "$W"' EXIT
+
+test_expect_success 'the BPF enum and the userspace enum agree' '
+ sed -n "s/^\tSHIRUDO_TAMPER_\([A-Z_]*\),$/\1/p" \
+ ../bpf/lib/shirudo_tamper.h >"$W/bpf" &&
+ sed -n "s/^\tSHIRUDO_TAMPER_\([A-Z_]*\),$/\1/p" \
+ ../util/tamper.h >"$W/user" &&
+ [ -s "$W/bpf" ] && [ -s "$W/user" ] &&
+ cmp "$W/bpf" "$W/user"
+'
+
+test_expect_success 'every point has a name and a meaning' '
+ sed -n "s/^\t\[SHIRUDO_TAMPER_\([A-Z_]*\)\] = { \"[A-Za-z]*\",$/\1/p" \
+ ../util/tamper.c >"$W/table" &&
+ cmp "$W/user" "$W/table"
+'
+
+test_expect_success 'status --json reports the counters as null without init' '
+ if [ -e /run/shirudo/bpffs/tamper ]; then
+ echo "skipping: a tamper map is pinned on this host"
+ else
+ "$S" status --json | python3 -c "
+import json, sys
+d = json.load(sys.stdin)
+assert \"tamper_attempts\" in d and d[\"tamper_attempts\"] is None
+"
+ fi
+'
+
+test_done
diff --git a/util/paths.h b/util/paths.h
index b146771c..9f5a10ed 100644
--- a/util/paths.h
+++ b/util/paths.h
@@ -84,6 +84,8 @@
#define SHIRUDO_BPFFS_UPATCHES_DIR SHIRUDO_BPFFS_DIR "/upatches"
/* The frozen identity map: bpffs device, executable, trust anchor. */
#define SHIRUDO_IDENTITY_PIN SHIRUDO_BPFFS_DIR "/identity"
+/* The tamper counters: one per guard, counted where it refuses. */
+#define SHIRUDO_TAMPER_PIN SHIRUDO_BPFFS_DIR "/tamper"
/* Operator-side. */
#define SHIRUDO_SIGNING_KEY_FILE "signing_key.pem"
diff --git a/util/tamper.c b/util/tamper.c
new file mode 100644
index 00000000..d5501971
--- /dev/null
+++ b/util/tamper.c
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * Read back the tamper counters init pinned in the private bpffs.
+ */
+#include <errno.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <linux/bpf.h>
+#include <bpf/bpf.h>
+
+#include "paths.h"
+#include "tamper.h"
+
+static const struct {
+ const char *name;
+ const char *meaning;
+} tamper_points[SHIRUDO_TAMPER_MAX] = {
+ [SHIRUDO_TAMPER_LOAD_NOT_LOADER] = { "LoadNotLoader",
+ "program load that is not a signed loader" },
+ [SHIRUDO_TAMPER_LOAD_UNSIGNED] = { "LoadUnsigned",
+ "loader-type program load without a signature" },
+ [SHIRUDO_TAMPER_LOAD_UNVERIFIED] = { "LoadUnverified",
+ "program load whose signature the kernel did not verify" },
+ [SHIRUDO_TAMPER_LOAD_KEYRING_KIND] = { "LoadKeyringKind",
+ "program signed against a keyring of the wrong kind" },
+ [SHIRUDO_TAMPER_LOAD_KEYRING] = { "LoadKeyring",
+ "program signed against a keyring that is not the anchor" },
+ [SHIRUDO_TAMPER_MAP_CREATE] = { "MapCreate",
+ "map creation by another executable" },
+ [SHIRUDO_TAMPER_MAP_WRITE] = { "MapWrite",
+ "map update or freeze by another executable" },
+ [SHIRUDO_TAMPER_MAP_FD] = { "MapFdWrite",
+ "writable map descriptor for another executable" },
+ [SHIRUDO_TAMPER_PROG_RUN] = { "ProgRun",
+ "test run of a program by another executable" },
+ [SHIRUDO_TAMPER_ATTACH] = { "ProgAttach",
+ "link or raw tracepoint attach by another executable" },
+ [SHIRUDO_TAMPER_OBJ_PIN] = { "ObjPin",
+ "pin of a BPF object by another executable" },
+ [SHIRUDO_TAMPER_BPF_CMD] = { "BpfCommand",
+ "detach, update, delete or unclassified bpf() command" },
+ [SHIRUDO_TAMPER_EXE_IDENTITY] = { "ExeIdentity",
+ "executable change through PR_SET_MM" },
+ [SHIRUDO_TAMPER_KEYRING] = { "KeyringWrite",
+ "write, unlink, revoke or invalidate on the anchor keyring" },
+ [SHIRUDO_TAMPER_PIN_UNLINK] = { "PinUnlink",
+ "unlink of a pin in the private bpffs" },
+ [SHIRUDO_TAMPER_PIN_RENAME] = { "PinRename",
+ "rename of a pin in the private bpffs" },
+ [SHIRUDO_TAMPER_PIN_XATTR] = { "PinXattr",
+ "xattr change on a pin in the private bpffs" },
+ [SHIRUDO_TAMPER_BPFFS_UMOUNT] = { "BpffsUmount",
+ "unmount of the private bpffs" },
+ [SHIRUDO_TAMPER_BPFFS_UMOUNT_ABOVE] = { "BpffsUmountAbove",
+ "unmount of a mount above the private bpffs" },
+ [SHIRUDO_TAMPER_BPFFS_MOUNT] = { "BpffsMount",
+ "move, mount over or remount of the private bpffs" },
+ [SHIRUDO_TAMPER_SEAL_WRITE] = { "SealWrite",
+ "write open of a sealed file" },
+ [SHIRUDO_TAMPER_SEAL_UNLINK] = { "SealUnlink",
+ "unlink of a sealed file" },
+ [SHIRUDO_TAMPER_SEAL_RENAME] = { "SealRename",
+ "rename of or over a sealed file" },
+ [SHIRUDO_TAMPER_SEAL_SETATTR] = { "SealSetattr",
+ "truncate, chmod, chown or utimes of a sealed file" },
+ [SHIRUDO_TAMPER_SEAL_XATTR] = { "SealXattr",
+ "xattr change on a sealed file" },
+ [SHIRUDO_TAMPER_SEAL_LABEL] = { "SealLabel",
+ "seal label set by another executable" },
+ [SHIRUDO_TAMPER_SEAL_PTRACE] = { "SealPtrace",
+ "ptrace of a shirudo task" },
+ [SHIRUDO_TAMPER_SEAL_PERF] = { "SealPerf",
+ "perf event bound to a shirudo task" },
+};
+
+const char *shirudo_tamper_name(enum shirudo_tamper_point point)
+{
+ if (point >= SHIRUDO_TAMPER_MAX || !tamper_points[point].name)
+ return "?";
+ return tamper_points[point].name;
+}
+
+const char *shirudo_tamper_meaning(enum shirudo_tamper_point point)
+{
+ if (point >= SHIRUDO_TAMPER_MAX || !tamper_points[point].meaning)
+ return "?";
+ return tamper_points[point].meaning;
+}
+
+int shirudo_tamper_read(struct shirudo_tamper_counts *out)
+{
+ struct bpf_map_info info = {};
+ uint32_t len = sizeof(info);
+ uint32_t key;
+ int fd, rc;
+
+ memset(out, 0, sizeof(*out));
+ fd = bpf_obj_get(SHIRUDO_TAMPER_PIN);
+ if (fd < 0)
+ return -errno;
+ rc = bpf_map_get_info_by_fd(fd, &info, &len);
+ if (rc) {
+ rc = -errno;
+ } else if (info.max_entries != SHIRUDO_TAMPER_MAX ||
+ info.value_size != sizeof(out->count[0])) {
+ rc = -EPROTO;
+ } else {
+ for (key = 0; key < SHIRUDO_TAMPER_MAX; key++) {
+ rc = bpf_map_lookup_elem(fd, &key, &out->count[key]);
+ if (rc) {
+ rc = -errno;
+ break;
+ }
+ out->total += out->count[key];
+ }
+ }
+ close(fd);
+ return rc;
+}
diff --git a/util/tamper.h b/util/tamper.h
new file mode 100644
index 00000000..f1ce6ad4
--- /dev/null
+++ b/util/tamper.h
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The tamper counters the LSM keeps, read back from their bpffs pin.
+ */
+#ifndef SHIRUDO_UTIL_TAMPER_H
+#define SHIRUDO_UTIL_TAMPER_H
+
+#include <stdint.h>
+
+/*
+ * One counter per point at which a guard refuses a caller. Must match
+ * enum shirudo_tamper_point in bpf/lib/shirudo_tamper.h in order and
+ * number; the reader refuses a map of another size, and the names and
+ * meanings status reports are the table in tamper.c.
+ */
+enum shirudo_tamper_point {
+ SHIRUDO_TAMPER_LOAD_NOT_LOADER,
+ SHIRUDO_TAMPER_LOAD_UNSIGNED,
+ SHIRUDO_TAMPER_LOAD_UNVERIFIED,
+ SHIRUDO_TAMPER_LOAD_KEYRING_KIND,
+ SHIRUDO_TAMPER_LOAD_KEYRING,
+ SHIRUDO_TAMPER_MAP_CREATE,
+ SHIRUDO_TAMPER_MAP_WRITE,
+ SHIRUDO_TAMPER_MAP_FD,
+ SHIRUDO_TAMPER_PROG_RUN,
+ SHIRUDO_TAMPER_ATTACH,
+ SHIRUDO_TAMPER_OBJ_PIN,
+ SHIRUDO_TAMPER_BPF_CMD,
+ SHIRUDO_TAMPER_EXE_IDENTITY,
+ SHIRUDO_TAMPER_KEYRING,
+ SHIRUDO_TAMPER_PIN_UNLINK,
+ SHIRUDO_TAMPER_PIN_RENAME,
+ SHIRUDO_TAMPER_PIN_XATTR,
+ SHIRUDO_TAMPER_BPFFS_UMOUNT,
+ SHIRUDO_TAMPER_BPFFS_UMOUNT_ABOVE,
+ SHIRUDO_TAMPER_BPFFS_MOUNT,
+ SHIRUDO_TAMPER_SEAL_WRITE,
+ SHIRUDO_TAMPER_SEAL_UNLINK,
+ SHIRUDO_TAMPER_SEAL_RENAME,
+ SHIRUDO_TAMPER_SEAL_SETATTR,
+ SHIRUDO_TAMPER_SEAL_XATTR,
+ SHIRUDO_TAMPER_SEAL_LABEL,
+ SHIRUDO_TAMPER_SEAL_PTRACE,
+ SHIRUDO_TAMPER_SEAL_PERF,
+ SHIRUDO_TAMPER_MAX
+};
+
+struct shirudo_tamper_counts {
+ uint64_t count[SHIRUDO_TAMPER_MAX];
+ uint64_t total;
+};
+
+/* The name status and the keepalive report a point under. */
+const char *shirudo_tamper_name(enum shirudo_tamper_point point);
+/* What a count at that point means. */
+const char *shirudo_tamper_meaning(enum shirudo_tamper_point point);
+
+/*
+ * The counters the LSM has kept this boot. -ENOENT when init has not
+ * run, -EPROTO when the pinned map is not the shape this binary knows,
+ * another -errno when the pin cannot be read.
+ */
+int shirudo_tamper_read(struct shirudo_tamper_counts *out);
+
+#endif /* SHIRUDO_UTIL_TAMPER_H */
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index 5504948a..4a281b2b 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -1835,6 +1835,7 @@ static int build_generic_log_payload(
const char *scope_name, const char *event_name,
int severity_number, const char *severity_text,
const char *status, const char *version_id,
+ int64_t tamper_attempts,
const char *body_json, size_t body_len,
char **out, size_t *out_len, size_t *alloc_len)
{
@@ -1888,6 +1889,8 @@ static int build_generic_log_payload(
otlp_kv_string(&e, "shirudo.status", status);
if (version_id)
otlp_kv_string(&e, "shirudo.version_id", version_id);
+ if (tamper_attempts >= 0)
+ otlp_kv_int(&e, "shirudo.tamper_attempts", tamper_attempts);
shirudo_json_arr_close(&e);
shirudo_json_obj_close(&e);
shirudo_json_arr_close(&e);
@@ -1913,6 +1916,7 @@ int shirudo_otel_send_log_json(const struct shirudo_otel_opts *opts,
const char *severity_text,
const char *status,
const char *version_id,
+ int64_t tamper_attempts,
const char *body_json, size_t body_len)
{
char *payload = NULL;
@@ -1927,6 +1931,7 @@ int shirudo_otel_send_log_json(const struct shirudo_otel_opts *opts,
rc = build_generic_log_payload(scope_name, event_name,
severity_number, severity_text,
status, version_id,
+ tamper_attempts,
body_json, body_len,
&payload, &payload_len, &alloc_len);
if (!rc)
diff --git a/util/upatch_maps_otel.h b/util/upatch_maps_otel.h
index ce4bbfa8..e31e896d 100644
--- a/util/upatch_maps_otel.h
+++ b/util/upatch_maps_otel.h
@@ -68,7 +68,11 @@ int shirudo_upatch_maps_otel_send_decoded(const struct shirudo_map_view *v,
enum shirudo_otel_signal signal,
const struct shirudo_otel_opts *opts);
-/* Send an already-built JSON object as the structured body of one OTLP log. */
+/*
+ * Send an already-built JSON object as the structured body of one OTLP
+ * log. A tamper_attempts of 0 or more rides along as an attribute; a
+ * negative one leaves it out.
+ */
int shirudo_otel_send_log_json(const struct shirudo_otel_opts *opts,
const char *scope_name,
const char *event_name,
@@ -76,6 +80,7 @@ int shirudo_otel_send_log_json(const struct shirudo_otel_opts *opts,
const char *severity_text,
const char *status,
const char *version_id,
+ int64_t tamper_attempts,
const char *body_json, size_t body_len);
#endif /* SHIRUDO_UTIL_UPATCH_MAPS_OTEL_H */