eBPF-based security platform
bpf2: deny PERF_EVENT_IOC_SET_BPF to every task but shirudo
Daniel Borkmann58ea2523330f
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex 82f0229f..22e2108e 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -249,7 +249,10 @@ restricted to tasks authorized by successful exec from the runtime image;kernel-context calls from verified loaders remain allowed. This lets Shirudorun loaders and serializers while preventing root from invoking a serializerto drain its maps. Map creation, update and freeze, object pin, link creation-and raw tracepoint open also require that task authority.+and raw tracepoint open also require that task authority, and so does+``PERF_EVENT_IOC_SET_BPF``, the perf event ioctl that attaches a program+without a ``bpf()`` command, which ``lsm/file_ioctl`` and+``lsm/file_ioctl_compat`` deny to every other caller.The read-only commands (``*_GET_FD_BY_ID``,``*_GET_NEXT_ID``, ``OBJ_GET_INFO_BY_FD``,``MAP_LOOKUP_ELEM``, ``MAP_LOOKUP_BATCH``, ``MAP_GET_NEXT_KEY``,@@ -396,7 +399,7 @@ and disk sequence. Runtime records require the authenticated bootstrap root.Config records require an authorized task and the expected root it stagedfor that mapping. ``bdev_free_security`` attempts cleanup, but upstreamcan omit the notification.-The ``file_ioctl`` hook denies untrusted+The ``file_ioctl`` and ``file_ioctl_compat`` hooks deny untrustedcallers mutating device-mapper control operations, including through old orinherited FDs. This restriction applies to all DM devices on the target.Trusted image tasks retain control access. These checks use existing LSMdiff --git a/Documentation/config-images.rst b/Documentation/config-images.rstindex 826505de..c38869d2 100644--- a/Documentation/config-images.rst+++ b/Documentation/config-images.rst@@ -402,7 +402,7 @@ and cleanup are distinct concerns.Image mode requires a kernel without 32-bit compatibility support, aslisted in the `kernel requirements <requirements.rst>`__.-The ``file_ioctl`` hook denies mutating+The ``file_ioctl`` and ``file_ioctl_compat`` hooks deny mutatingDM-control commands from untrusted callers, including commands on old orinherited FDs. The restriction covers all DM devices in image mode, soordinary untrusted volume-management tools cannot mutate unrelated DMdiff --git a/Documentation/requirements.rst b/Documentation/requirements.rstindex 94a23bdd..0c8c3225 100644--- a/Documentation/requirements.rst+++ b/Documentation/requirements.rst@@ -124,8 +124,9 @@ Image-mode requirementsImage mode supports 64-bit userspace only. Disable 32-bit compatibility(``CONFIG_COMPAT=n``); on x86-64, disable ``CONFIG_IA32_EMULATION`` and-``CONFIG_X86_X32_ABI``. Device-mapper restrictions use the native-``file_ioctl`` hook and do not cover compat ioctls.+``CONFIG_X86_X32_ABI``. The device-mapper and perf event restrictions+hook both ``file_ioctl`` and ``file_ioctl_compat``, but 32-bit callers are+not otherwise considered.Every target also requires ``CONFIG_BLK_DEV_DM``,``CONFIG_DM_VERITY``, ``CONFIG_BLK_DEV_LOOP`` and ``CONFIG_EXT4_FS``. Runtimediff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rstindex e03b70da..ac20c263 100644--- a/Documentation/threat-model.rst+++ b/Documentation/threat-model.rst@@ -122,7 +122,11 @@ T2 Detaching or replacing enforcement``lsm/bpf`` denies ``LINK_DETACH``, ``LINK_UPDATE``,``PROG_ATTACH``, ``PROG_DETACH`` and ``BTF_LOAD`` from userspace,and leaves ``LINK_CREATE``, ``RAW_TRACEPOINT_OPEN`` and ``OBJ_PIN``- to a task authorized by successful runtime exec. Root cannot re-attach a vetted program+ to a task authorized by successful runtime exec. The one attach that+ never passes through ``bpf()``, ``PERF_EVENT_IOC_SET_BPF`` on a perf+ event, which would bind a kprobe program reached by id to a probe of+ the caller's own, is kept to the same task by ``file_ioctl`` and+ ``file_ioctl_compat``. Root cannot re-attach a vetted programat a site its signer never meant it for, nor pin objects of its own.The site a load attaches at is partly the caller's to arrange, androot runs ``shirudo upatch load``: a cgroup_skb program under a@@ -323,7 +327,8 @@ T10 An attempt that leaves no tracesignature was verified against the wrong kind of keyring or thewrong keyring, a writable map descriptor, a rename of a pin, anunmount of 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+ shirudo task, a program bound to a perf event, and so on), in a+ map ``init`` pins and freezes, sothat no task, shirudo included, can reset it from userspace whilethe hooks still add to it. ``shirudo status`` lists the countersthat are not zero with what each means, ``status --json`` carries@@ -354,8 +359,9 @@ T11 Image-mode config and executable substitutionimage tasks, including through old or inherited FDs; this restrictionapplies to unrelated DM devices too. Config device classification alsorequires the root expected by the trusted task creating that mapping.- Image mode requires 32-bit compatibility disabled in the kernel; its- device-mapper checks cover native ioctls only.+ Image mode requires 32-bit compatibility disabled in the kernel; the+ device-mapper and perf event ioctl checks hook the native and the+ compat path alike.Enforcement uses existing LSM hooks; direct access to the dm-veritykeyring requires the upstream keyring-ID API. The mount hooks do notcover every mount attribute change ordiff --git a/bpf/bpf2.c b/bpf/bpf2.cindex b1839369..0e13e5d4 100644--- a/bpf/bpf2.c+++ b/bpf/bpf2.c@@ -235,9 +235,32 @@ static __always_inline int shirudo_dm_ioctl(struct file *file,}}+/*+ * PERF_EVENT_IOC_SET_BPF binds a program to a perf event without a bpf()+ * command, and a program's fd is root's for the asking by id: a kprobe+ * program a signed loader created would run at a probe of root's own.+ * Keep it to the caller BPF_LINK_CREATE is kept to.+ */+static __always_inline int shirudo_ioctl(struct file *file, unsigned int cmd,+ int ret)+{+ if (ret)+ return ret;+ if (cmd == SHIRUDO_PERF_EVENT_IOC_SET_BPF)+ return shirudo_self_or_deny(SHIRUDO_TAMPER_PERF_ATTACH);+ return shirudo_dm_ioctl(file, cmd, ret);+}+SEC("lsm/file_ioctl")int BPF_PROG(shirudo_lsm_file_ioctl, struct file *file, unsigned int cmd,unsigned long arg, int ret){- return shirudo_dm_ioctl(file, cmd, ret);+ return shirudo_ioctl(file, cmd, ret);+}++SEC("lsm/file_ioctl_compat")+int BPF_PROG(shirudo_lsm_file_ioctl_compat, struct file *file,+ unsigned int cmd, unsigned long arg, int ret)+{+ return shirudo_ioctl(file, cmd, ret);}diff --git a/bpf/lib/shirudo_tamper.h b/bpf/lib/shirudo_tamper.hindex 675f98fb..97e2edba 100644--- a/bpf/lib/shirudo_tamper.h+++ b/bpf/lib/shirudo_tamper.h@@ -41,6 +41,7 @@ enum shirudo_tamper_point {SHIRUDO_TAMPER_SEAL_PERF,SHIRUDO_TAMPER_EXEC_TRACED,SHIRUDO_TAMPER_SEAL_ENTRY,+ SHIRUDO_TAMPER_PERF_ATTACH,SHIRUDO_TAMPER_MAX};diff --git a/include/shirudo_enforcement.h b/include/shirudo_enforcement.hindex 48a5e408..59684b9e 100644--- a/include/shirudo_enforcement.h+++ b/include/shirudo_enforcement.h@@ -7,4 +7,7 @@#define SHIRUDO_PR_GET_ENFORCEMENT 0x53520001#define SHIRUDO_PR_ENFORCING_ERR 1002+/* PERF_EVENT_IOC_SET_BPF, _IOW('$', 8, __u32), on every supported architecture. */+#define SHIRUDO_PERF_EVENT_IOC_SET_BPF 0x40042408u+#endif /* SHIRUDO_ENFORCEMENT_H */diff --git a/testing/tamperproof/t1138-mode-objects.sh b/testing/tamperproof/t1138-mode-objects.shindex 6e94bbfa..c0ac8e08 100755--- a/testing/tamperproof/t1138-mode-objects.sh+++ b/testing/tamperproof/t1138-mode-objects.sh@@ -7,7 +7,8 @@ cd "$(dirname "$0")/.."target_guardtest_expect_success 'device, memory and config-root guards are pinned' '- for pin in devices config_roots file_ioctl_link bdev_integrity_link \+ for pin in devices config_roots file_ioctl_link file_ioctl_compat_link \+ bdev_integrity_link \bdev_free_link seal_mmap_link seal_mprotect_link; do[ -e "/run/shirudo/bpffs/$pin" ] || return 1donediff --git a/util/image_guards.c b/util/image_guards.cindex 3da9efac..f88c5110 100644--- a/util/image_guards.c+++ b/util/image_guards.c@@ -10,6 +10,7 @@#include <linux/bpf.h>#include <linux/btf.h>+#include <linux/perf_event.h>#include <bpf/bpf.h>#include <bpf/btf.h>#include <bpf/libbpf.h>@@ -25,6 +26,9 @@#include "shirudo_enforcement.h"#include "tamper.h"+_Static_assert(SHIRUDO_PERF_EVENT_IOC_SET_BPF == PERF_EVENT_IOC_SET_BPF,+ "the ioctl number the BPF side denies");+const struct shirudo_image_guardshirudo_seal_guards[SHIRUDO_SEAL_HOOK_COUNT] = {{ "shirudo_seal_bprm_check_security",@@ -84,6 +88,8 @@ shirudo_observer_guards[SHIRUDO_OBSERVER_HOOK_COUNT] = {const struct shirudo_image_guardshirudo_image_observer_guards[SHIRUDO_IMAGE_OBSERVER_HOOK_COUNT] = {{ "shirudo_lsm_file_ioctl", SHIRUDO_BPFFS_DIR "/file_ioctl_link" },+ { "shirudo_lsm_file_ioctl_compat",+ SHIRUDO_BPFFS_DIR "/file_ioctl_compat_link" },{ "shirudo_lsm_bdev_setintegrity",SHIRUDO_BPFFS_DIR "/bdev_integrity_link" },{ "shirudo_lsm_bdev_free_security",diff --git a/util/image_guards.h b/util/image_guards.hindex 22aba36d..df9d5e21 100644--- a/util/image_guards.h+++ b/util/image_guards.h@@ -12,7 +12,7 @@#define SHIRUDO_IMAGE_SEAL_HOOK_COUNT 2#define SHIRUDO_MOUNT_HOOK_COUNT 4#define SHIRUDO_OBSERVER_HOOK_COUNT 4-#define SHIRUDO_IMAGE_OBSERVER_HOOK_COUNT 3+#define SHIRUDO_IMAGE_OBSERVER_HOOK_COUNT 4struct shirudo_image_guard {const char *prog;diff --git a/util/tamper.c b/util/tamper.cindex fae86ef4..32ab0e73 100644--- a/util/tamper.c+++ b/util/tamper.c@@ -80,6 +80,8 @@ static const struct {"exec of shirudo by a traced task" },[SHIRUDO_TAMPER_SEAL_ENTRY] = { "SealEntry","entry created, linked or renamed into a sealed directory" },+ [SHIRUDO_TAMPER_PERF_ATTACH] = { "PerfAttach",+ "program attached to a perf event by another executable" },};const char *tamper_name(enum shirudo_tamper_point point)