eBPF-based security platform
bpf: load inode policy bits into task storage
John Fastabend4f670e074771
diff --git a/Documentation/internals/enforcement.rst b/Documentation/internals/enforcement.rstindex 1e58f00f..c93b47f6 100644--- a/Documentation/internals/enforcement.rst+++ b/Documentation/internals/enforcement.rst@@ -42,7 +42,7 @@ device records of :src:`bpf/lib/`.``init`` loads both without a signature, from the binary theauthenticated initrd carries.-The objects share five maps, which ``init`` hands from ``bpf2`` to+The objects share six maps, which ``init`` hands from ``bpf2`` to``seal`` with ``bpf_map__reuse_fd()`` and pins in the private bpffs:.. list-table::@@ -62,6 +62,9 @@ The objects share five maps, which ``init`` hands from ``bpf2`` to* - ``shirudo_tasks``- ``task_authority``- Task storage with each task's mark and exec candidate.+ * - ``policy_map``+ - ``policy_map``+ - Task storage with committed policy bits and the exec candidate.* - ``shirudo_devices``- ``devices``- The dm-verity devices recorded as runtime or config, up to 128.@@ -75,7 +78,7 @@ The objects share five maps, which ``init`` hands from ``bpf2`` to``init`` freezes every one of them against userspace writes except``shirudo_roots``, which an authorized task writes before each config-mapping. ``bpf2`` keeps a sixth map, ``shirudo_mnt_rec``, with the bpffs+mapping. ``bpf2`` keeps a seventh map, ``shirudo_mnt_rec``, with the bpffsmount it records, and ``seal`` keeps its scratch buffer and the inodestorage of directory marks.@@ -414,8 +417,18 @@ when the record it needs is missing.- None- None- No task storage leaves the task without authority.+ * - ``lsm.s/bprm_creds_for_exec``, ``lsm.s/bprm_check_security``,+ ``lsm.s/bprm_committed_creds`` (``seal``)+ - Reset, accumulate and commit the policy bitset from the original+ executable and each interpreter. An absent policy or a file system+ without xattrs contributes zero bits; a malformed or unreadable+ policy xattr refuses exec.+ - ``EACCES``+ - Uncounted+ - Failed exec leaves committed policy unchanged.* - ``lsm/task_alloc`` (``seal``)- - Copies a marked parent's mark to the child.+ - Copies a marked parent's mark and any committed policy bits to the+ child.- ``ENOMEM``- None- Closed: no storage refuses the fork of a marked parent.@@ -461,13 +474,16 @@ when the record it needs is missing.* - ``lsm.s/inode_unlink``, ``lsm.s/inode_setattr``,``lsm.s/inode_removexattr`` (``seal``)- The operation on a labeled inode to a task without authority.- Setattr covers truncate, chmod, chown and timestamps.+ Setattr covers truncate, chmod, chown and timestamps. Removing the+ policy xattr is refused on any inode without authority.- ``EACCES``- ``SealUnlink``, ``SealSetattr``, ``SealXattr``- See the label verdict below.* - ``lsm.s/inode_setxattr`` (``seal``)- - Setting ``security.bpf.shirudo`` on any inode, and any other name- on a labeled inode, to a task without authority.+ - Setting ``security.bpf.shirudo`` or+ ``security.bpf.shirudo.policy`` on any inode, and any other name+ on a labeled inode, to a task without authority. Policy values+ must be exactly 8 bytes.- ``EACCES``- ``SealLabel``, ``SealXattr``- See the label verdict below.diff --git a/Documentation/internals/seal.rst b/Documentation/internals/seal.rstindex d71c0ce8..22059d65 100644--- a/Documentation/internals/seal.rst+++ b/Documentation/internals/seal.rst@@ -70,6 +70,20 @@ that is not on the runtime image commits an untrusted candidate and soclears the mark. ``lsm/task_alloc`` copies a parent's mark to everychild.+Policy bits+-----------++An executable may carry ``security.bpf.shirudo.policy``, an 8-byte native+``__u64`` bitset. During exec, ``bprm_check_security`` ORs the bitsets+from the original file and each interpreter into a task-local candidate+in ``policy_map``. ``bprm_committed_creds`` commits that value only after+exec succeeds; a successful exec with no policy bits clears the previous+value, while a failed exec leaves it unchanged. A fork or clone inherits+the committed bits. The xattr remains on the inode. Only a task with+runtime authority may set, update or remove it. An absent policy or a+file system without xattrs contributes zero bits; invalid lengths and+other read errors refuse the exec.+The device record that the exec check consults comes from``lsm.s/bdev_setintegrity``: it records a dm-verity device as runtimewhen its root hash equals the runtime root the UKI authenticated, whichdiff --git a/Documentation/reference/files.rst b/Documentation/reference/files.rstindex b09843ba..9754acec 100644--- a/Documentation/reference/files.rst+++ b/Documentation/reference/files.rst@@ -389,6 +389,12 @@ Extended attributesOnly a task with runtime authority may set it (``SealLabel``).A file created in a labeled directory receives the label atcreation.+ * - ``security.bpf.shirudo.policy``+ - Executable inodes carrying task policy bits.+ - Exactly 8 bytes, the target-native representation of a ``__u64``+ bitset. Exec ORs the original file and interpreter values into+ task storage. Only a task with runtime authority may set, update+ or remove it (``SealLabel``).* - ``security.bpf.shirudo.payload``- Each ``upatches/<bpffs-name>/`` directory in the private bpffs.- 32 bytes, the ``payload_sha256`` of the loaded upatch. Set afterdiff --git a/Documentation/security/tamper-counters.rst b/Documentation/security/tamper-counters.rstindex 71d9774e..1af7eb69 100644--- a/Documentation/security/tamper-counters.rst+++ b/Documentation/security/tamper-counters.rst@@ -146,9 +146,10 @@ counter.- ``lsm.s/inode_setxattr``, ``lsm.s/inode_removexattr``- ``EACCES``* - ``SealLabel``- - seal label set by another executable- - ``lsm.s/inode_setxattr``: the exact name ``security.bpf.shirudo``- on any inode+ - seal or policy label changed by another executable+ - ``lsm.s/inode_setxattr`` or ``lsm.s/inode_removexattr``: the exact+ names ``security.bpf.shirudo`` or+ ``security.bpf.shirudo.policy`` on any inode- ``EACCES``* - ``SealPtrace``- ptrace of a shirudo taskdiff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex a2fa1120..23bb954b 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -300,6 +300,10 @@ Assets- ``shirudo_tasks`` task storage, pinned as ``task_authority``- Frozen against ``bpf()`` writes; only the seal's programs set amark (T3).+ * - Task policy bits+ - ``policy_map`` task storage, pinned as ``policy_map``+ - Frozen against userspace writes; exec hooks OR protected inode+ values and commit only after a successful exec.* - Device records and expected roots- ``shirudo_devices`` (frozen) and ``shirudo_roots`` (not frozen,written by authorized tasks for their own config mapping)@@ -641,7 +645,8 @@ refused, since it is a tree bound over the path rather than the oneunlink, either end of a rename, a setattr (truncate, chmod, chown,utimes) and an xattr change need runtime authority (``SealWrite``,``SealUnlink``, ``SealRename``, ``SealSetattr``, ``SealXattr``). Only an-authorized task sets the exact label, on any inode (``SealLabel``).+authorized task sets the exact seal label or changes the policy label on+any inode (``SealLabel``).The label is a public constant, the same on every build. It marks atarget and never grants authority: a file system root mounts can carry@@ -652,6 +657,16 @@ seal's scratch memory. The two permission errors count as unlabeledbecause the kernel checks read permission for the caller before itreturns the label (U12).+Executables may carry ``security.bpf.shirudo.policy``, an 8-byte+target-native ``__u64`` bitset. During exec, the seal ORs the value from+the original file and each interpreter into ``policy_map``. ``ENODATA``+or ``EOPNOTSUPP`` means no policy; malformed or unreadable values refuse+exec.+Successful exec commits the accumulated bits, an unlabeled exec clears+them, failed exec preserves the previous value, and children inherit the+committed value. The xattr remains on the inode, and only a task with+runtime authority may set, update or remove it (``SealLabel``).+The seal keeps a mark in inode storage on every labeled directory ithas seen since the inode came into memory. A new inode under a markeddirectory receives the label inside the transaction that creates itdiff --git a/bpf/lib/lsm.h b/bpf/lib/lsm.hindex a2b7f1a5..2fdd3b90 100644--- a/bpf/lib/lsm.h+++ b/bpf/lib/lsm.h@@ -48,6 +48,18 @@ struct {__type(value, struct task_authority);} shirudo_tasks SEC(".maps");+struct shirudo_task_policy {+ __u64 active;+ __u64 candidate;+};++struct {+ __uint(type, BPF_MAP_TYPE_TASK_STORAGE);+ __uint(map_flags, BPF_F_NO_PREALLOC);+ __type(key, int);+ __type(value, struct shirudo_task_policy);+} policy_map SEC(".maps");+static __always_inline struct sb_id *shirudo_policy(void){__u32 zero = 0;diff --git a/bpf/lib/xattr.h b/bpf/lib/xattr.hindex 30281f32..c612cec1 100644--- a/bpf/lib/xattr.h+++ b/bpf/lib/xattr.h@@ -5,6 +5,7 @@#define SHIRUDO_BPF_XATTR_H#define XATTR "security.bpf.shirudo"+#define SHIRUDO_POLICY_XATTR "security.bpf.shirudo.policy"/* The label is a SHA-256 hex string (util/seal.h) -- always 64 bytes. */#define LABEL_LEN 64diff --git a/bpf/seal.c b/bpf/seal.cindex 323a3d59..1f4e7890 100644--- a/bpf/seal.c+++ b/bpf/seal.c@@ -94,6 +94,18 @@ static __always_inline bool seal_is_label(const char *name)return !bpf_strncmp(buf, sizeof(XATTR), XATTR);}+static __always_inline bool seal_is_policy(const char *name)+{+ char buf[sizeof(SHIRUDO_POLICY_XATTR) + 1];+ long n;++ n = bpf_probe_read_kernel_str(buf, sizeof(buf), name);+ if (n != sizeof(SHIRUDO_POLICY_XATTR))+ return false;+ return !bpf_strncmp(buf, sizeof(SHIRUDO_POLICY_XATTR),+ SHIRUDO_POLICY_XATTR);+}+static __always_inline struct seal_scratch *seal_scratch_get(struct task_struct *task){@@ -128,8 +140,14 @@ void BPF_PROG(shirudo_seal_bprm_committed_creds,const struct linux_binprm *bprm){struct task_struct *task = bpf_get_current_task_btf();+ struct shirudo_task_policy *policy;struct task_authority *authority;+ policy = bpf_task_storage_get(&policy_map, task, NULL, 0);+ if (policy) {+ policy->active = policy->candidate;+ policy->candidate = 0;+ }authority = bpf_task_storage_get(&shirudo_tasks, task, NULL, 0);if (!authority)return;@@ -144,11 +162,21 @@ int BPF_PROG(shirudo_seal_task_alloc, struct task_struct *child,__u64 clone_flags, int ret){struct task_struct *parent = bpf_get_current_task_btf();+ struct shirudo_task_policy *parent_policy;struct task_authority *parent_authority;+ struct shirudo_task_policy *policy;struct task_authority *authority;if (ret)return ret;+ parent_policy = bpf_task_storage_get(&policy_map, parent, NULL, 0);+ if (parent_policy && parent_policy->active) {+ policy = bpf_task_storage_get(&policy_map, child, NULL,+ BPF_LOCAL_STORAGE_GET_F_CREATE);+ if (!policy)+ return -ENOMEM;+ policy->active = parent_policy->active;+ }parent_authority = bpf_task_storage_get(&shirudo_tasks, parent,NULL, 0);if (!parent_authority || !parent_authority->trusted)@@ -427,7 +455,7 @@ int BPF_PROG(shirudo_seal_inode_setxattr, struct mnt_idmap *idmap,{if (ret)return ret;- if (seal_is_label(name))+ if (seal_is_label(name) || seal_is_policy(name))return seal_caller(TAMPER_SEAL_LABEL);return seal_dentry(dentry, TAMPER_SEAL_XATTR);}@@ -436,7 +464,11 @@ SEC("lsm.s/inode_removexattr")int BPF_PROG(shirudo_seal_inode_removexattr, struct mnt_idmap *idmap,struct dentry *dentry, const char *name, int ret){- return ret ? ret : seal_dentry(dentry, TAMPER_SEAL_XATTR);+ if (ret)+ return ret;+ if (seal_is_policy(name))+ return seal_caller(TAMPER_SEAL_LABEL);+ return seal_dentry(dentry, TAMPER_SEAL_XATTR);}char _license[] SEC("license") = "Dual BSD/GPL";@@ -454,6 +486,24 @@ static __always_inline bool shirudo_task_has_seccomp(struct task_struct *task)BPF_CORE_READ(seccomp, mode);}+SEC("lsm.s/bprm_creds_for_exec")+int BPF_PROG(shirudo_seal_bprm_creds_for_exec,+ struct linux_binprm *bprm, int ret)+{+ struct task_struct *task = bpf_get_current_task_btf();+ struct shirudo_task_policy *policy;++ (void)bprm;+ if (ret)+ return ret;+ policy = bpf_task_storage_get(&policy_map, task, NULL,+ BPF_LOCAL_STORAGE_GET_F_CREATE);+ if (!policy)+ return -ENOMEM;+ policy->candidate = policy->active;+ return 0;+}+/** shirudo grants authority only to a direct file on the verified runtime.* Overlay copies cannot qualify. Ptrace or seccomp supplied before exec must@@ -465,13 +515,33 @@ int BPF_PROG(shirudo_seal_bprm_check_security,{struct task_struct *task = bpf_get_current_task_btf();struct task_authority *authority;+ struct shirudo_task_policy *policy;+ struct seal_scratch *scratch;+ struct bpf_dynptr tdp;struct inode *real_inode;+ __u64 value;+ int len;bool trusted;if (ret)return ret;if (!shirudo_policy())return -EACCES;+ scratch = seal_scratch_get(task);+ if (!scratch)+ return -ENOMEM;+ bpf_dynptr_from_mem(scratch->target, sizeof(value), 0, &tdp);+ len = bpf_get_file_xattr(bprm->file, SHIRUDO_POLICY_XATTR, &tdp);+ if (len == -ENODATA || len == -EOPNOTSUPP)+ value = 0;+ else if (len != sizeof(value))+ return -EACCES;+ if (len == sizeof(value))+ __builtin_memcpy(&value, scratch->target, sizeof(value));+ policy = bpf_task_storage_get(&policy_map, task, NULL, 0);+ if (!policy)+ return -ENOMEM;+ policy->candidate |= value;real_inode = bpf_real_data_inode(bprm->file);trusted = real_inode == BPF_CORE_READ(bprm, file, f_inode) &&shirudo_file_runtime(bprm->file);diff --git a/builtin-init.c b/builtin-init.cindex 079643c7..d2ef25c7 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -884,6 +884,7 @@ out:unlink(IDENTITY_PIN);unlink(TAMPER_PIN);unlink(BPFFS_DIR "/task_authority");+ unlink(BPFFS_DIR "/policy_map");unlink(BPFFS_DIR "/devices");unlink(BPFFS_DIR "/config_roots");}diff --git a/util/identity.h b/util/identity.hindex 8c89e658..8cf58ca6 100644--- a/util/identity.h+++ b/util/identity.h@@ -68,6 +68,14 @@ BUILD_BUG_ON(sizeof(struct device_authority) != 48);BUILD_BUG_ON(sizeof(struct task_authority) != 8);BUILD_BUG_ON(sizeof(struct identity) != 584);+struct shirudo_task_policy {+ __u64 active;+ __u64 candidate;+};++_Static_assert(sizeof(struct shirudo_task_policy) == 16,+ "task policy ABI");+/* The bpffs mount init made, which the unmount guard keeps mounted. */struct mount_record {__u64 mount;diff --git a/util/image_guards.c b/util/image_guards.cindex 9584be38..66ef4805 100644--- a/util/image_guards.c+++ b/util/image_guards.c@@ -27,6 +27,7 @@const struct image_guardshirudo_seal_guards[SEAL_HOOK_COUNT] = {+ { "shirudo_seal_bprm_creds_for_exec", SEAL_BPRM_CREDS_PIN },{ "shirudo_seal_bprm_check_security",SEAL_BPRM_CHECK_PIN },{ "shirudo_seal_bprm_committed_creds",@@ -143,6 +144,11 @@ const struct guard_map shirudo_guard_maps[GUARD_MAP_COUNT] = {BPF_MAP_TYPE_TASK_STORAGE, sizeof(__u32),sizeof(struct task_authority), 0, BPF_F_NO_PREALLOC,},+ [GUARD_POLICY] = {+ "policy_map", POLICY_PIN,+ BPF_MAP_TYPE_TASK_STORAGE, sizeof(__u32),+ sizeof(struct shirudo_task_policy), 0, BPF_F_NO_PREALLOC,+ },[GUARD_DEVICES] = {"shirudo_devices", BPFFS_DIR "/devices",BPF_MAP_TYPE_HASH, sizeof(__u64),diff --git a/util/image_guards.h b/util/image_guards.hindex 51a038f6..b5900ab6 100644--- a/util/image_guards.h+++ b/util/image_guards.h@@ -8,12 +8,11 @@#include <stdint.h>-#define SEAL_HOOK_COUNT 20+#define SEAL_HOOK_COUNT 21#define IMAGE_SEAL_HOOK_COUNT 2#define MOUNT_HOOK_COUNT 5#define OBSERVER_HOOK_COUNT 9#define IMAGE_OBSERVER_HOOK_COUNT 4-struct image_guard {const char *prog;const char *pin;@@ -37,6 +36,7 @@ enum guard_map_index {GUARD_DEVICES,GUARD_ROOTS,GUARD_TAMPER,+ GUARD_POLICY,GUARD_MAP_COUNT,};diff --git a/util/paths.h b/util/paths.hindex 6344fce5..c5b9c6b4 100644--- a/util/paths.h+++ b/util/paths.h@@ -76,6 +76,7 @@#define BPRM_CHECK_LINK_PIN BPFFS_DIR "/bpf2_bprm_check_security_link"#define SEAL_FILE_OPEN_PIN BPFFS_DIR "/seal_file_open_link"#define SEAL_BPRM_CHECK_PIN BPFFS_DIR "/seal_bprm_check_security_link"+#define SEAL_BPRM_CREDS_PIN BPFFS_DIR "/seal_bprm_creds_link"#define SEAL_BPRM_COMMITTED_PIN BPFFS_DIR "/seal_bprm_committed_creds_link"#define SEAL_TASK_ALLOC_PIN BPFFS_DIR "/seal_task_alloc_link"#define SEAL_PTRACE_PIN BPFFS_DIR "/seal_ptrace_access_check_link"@@ -98,6 +99,7 @@#define BPFFS_UPATCHES_DIR BPFFS_DIR "/upatches"#define IDENTITY_PIN BPFFS_DIR "/identity"#define TAMPER_PIN BPFFS_DIR "/tamper"+#define POLICY_PIN BPFFS_DIR "/policy_map"#define RUNTIME_DIR RUN_DIR "/runtime"#define CONFIG_IMAGE_DIR RUN_DIR "/config"diff --git a/util/tamper.c b/util/tamper.cindex cf03ce1f..b8bef963 100644--- a/util/tamper.c+++ b/util/tamper.c@@ -71,7 +71,7 @@ static const struct {[TAMPER_SEAL_XATTR] = { "SealXattr","xattr change on a sealed file" },[TAMPER_SEAL_LABEL] = { "SealLabel",- "seal label set by another executable" },+ "seal or policy label changed by another executable" },[TAMPER_SEAL_PTRACE] = { "SealPtrace","ptrace of a shirudo task" },[TAMPER_SEAL_PERF] = { "SealPerf",