eBPF-based security platform
treewide: name the seal and policy xattrs XATTR_SEAL and XATTR_POLICY
Daniel Borkmann565f4fde63ed
diff --git a/Documentation/upatches/publish/beefile.rst b/Documentation/upatches/publish/beefile.rstindex 4aeb3abe..2d90dedf 100644--- a/Documentation/upatches/publish/beefile.rst+++ b/Documentation/upatches/publish/beefile.rst@@ -229,11 +229,12 @@ non-executable files.For an upatch with at least one policy table, include ``policy.h`` inits BPF source so it declares both shared maps with the required ABI.-The header is installed beside the shirudo BPF headers. The signed-``SHPL`` section opts the upatch into these bindings; declaring the maps-without policy tables leaves them as ordinary upatch-owned maps. Signed-BPF programs can write the maps, so enforcement programs must treat them-as read-only by contract.+It also brings ``XATTR_POLICY``, the name of the xattr, for a program+that reads it. The header is installed beside the shirudo BPF headers.+The signed ``SHPL`` section opts the upatch into these bindings;+declaring the maps without policy tables leaves them as ordinary+upatch-owned maps. Signed BPF programs can write the maps, so enforcement+programs must treat them as read-only by contract.The generator and the updater run on the operator host with theoperator's rights. The target receives only the signed result. Withoutdiff --git a/Makefile.shirudo b/Makefile.shirudoindex 835bfad7..fc488c3b 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -305,6 +305,7 @@ BPF_LIBBPF_HEADERS := $(addprefix $(LIBBPF_DIR)/,\BPF_LIB_HEADERS := bpf/lib/misc.h \bpf/lib/serializer.h \bpf/lib/policy.h \+ bpf/lib/xattr.h \bpf/lib/decoder.h# bpf/*.bpf.c sources compiled with clang -target bpf and embedded via@@ -612,7 +613,7 @@ TPMSIM_HELPER := $(if $(TPMSIM_LIBS),testing/helper/tpmsim-cmd,)VERITY_TEST_HELPERS := build/verity-tests/shirudo-policy-probebuild/verity-tests/shirudo-policy-probe: \- testing/helper/policy-probe.c util/identity.h+ testing/helper/policy-probe.c util/identity.h util/paths.h@printf ' CC $@\n'$(Q)mkdir -p $(dir $@)$(Q)$(CC) -O2 $(DEBUG_CFLAGS) -std=gnu11 $(WARNINGS) \diff --git a/bpf/lib/policy.h b/bpf/lib/policy.hindex d82cc948..92e65cbe 100644--- a/bpf/lib/policy.h+++ b/bpf/lib/policy.h@@ -8,6 +8,8 @@#ifndef SHIRUDO_BPF_POLICY_H#define SHIRUDO_BPF_POLICY_H+#include "xattr.h"+struct task_policy {__u64 active;__u64 candidate;diff --git a/bpf/lib/xattr.h b/bpf/lib/xattr.hindex c612cec1..cc372835 100644--- a/bpf/lib/xattr.h+++ b/bpf/lib/xattr.h@@ -4,8 +4,8 @@#ifndef SHIRUDO_BPF_XATTR_H#define SHIRUDO_BPF_XATTR_H-#define XATTR "security.bpf.shirudo"-#define SHIRUDO_POLICY_XATTR "security.bpf.shirudo.policy"+#define XATTR_SEAL "security.bpf.shirudo"+#define XATTR_POLICY "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 95fb203d..2f29704b 100644--- a/bpf/seal.c+++ b/bpf/seal.c@@ -85,25 +85,24 @@ static __always_inline int seal_verdict(int tlen,/* Whether an xattr name is the label itself, not a longer name under it. */static __always_inline bool seal_is_label(const char *name){- char buf[sizeof(XATTR) + 1];+ char buf[sizeof(XATTR_SEAL) + 1];long n;n = bpf_probe_read_kernel_str(buf, sizeof(buf), name);- if (n != sizeof(XATTR))+ if (n != sizeof(XATTR_SEAL))return false;- return !bpf_strncmp(buf, sizeof(XATTR), XATTR);+ return !bpf_strncmp(buf, sizeof(XATTR_SEAL), XATTR_SEAL);}static __always_inline bool seal_is_policy(const char *name){- char buf[sizeof(SHIRUDO_POLICY_XATTR) + 1];+ char buf[sizeof(XATTR_POLICY) + 1];long n;n = bpf_probe_read_kernel_str(buf, sizeof(buf), name);- if (n != sizeof(SHIRUDO_POLICY_XATTR))+ if (n != sizeof(XATTR_POLICY))return false;- return !bpf_strncmp(buf, sizeof(SHIRUDO_POLICY_XATTR),- SHIRUDO_POLICY_XATTR);+ return !bpf_strncmp(buf, sizeof(XATTR_POLICY), XATTR_POLICY);}static __always_inline struct seal_scratch *@@ -242,7 +241,7 @@ int BPF_PROG(shirudo_seal_file_open, struct file *file, int ret)return 0;bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);seal_mark(inode, s->target,- bpf_get_file_xattr(file, XATTR, &tdp));+ bpf_get_file_xattr(file, XATTR_SEAL, &tdp));return 0;}if (!(BPF_CORE_READ(file, f_mode) & FMODE_WRITE))@@ -251,7 +250,7 @@ int BPF_PROG(shirudo_seal_file_open, struct file *file, int ret)if (!s) /* cannot read the label */return seal_caller(TAMPER_SEAL_WRITE);bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);- tlen = bpf_get_file_xattr(file, XATTR, &tdp);+ tlen = bpf_get_file_xattr(file, XATTR_SEAL, &tdp);if (tlen == LABEL_LEN)seal_mark(inode, s->target, tlen);return seal_verdict(tlen, TAMPER_SEAL_WRITE);@@ -292,7 +291,7 @@ static __always_inline void seal_mark_dentry(struct dentry *dentry)return;bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);seal_mark(inode, s->target,- bpf_get_dentry_xattr(dentry, XATTR, &tdp));+ bpf_get_dentry_xattr(dentry, XATTR_SEAL, &tdp));}SEC("lsm.s/inode_post_setxattr")@@ -334,7 +333,7 @@ int BPF_PROG(shirudo_seal_inode_init_security, struct inode *inode,if (!m)return 0;bpf_dynptr_from_mem(m->label, LABEL_LEN, 0, &dp);- if (bpf_inode_init_xattr(xattrs, xattr_count, XATTR, &dp))+ if (bpf_inode_init_xattr(xattrs, xattr_count, XATTR_SEAL, &dp))return -EACCES;if (!seal_is_dir(inode))return 0;@@ -355,7 +354,7 @@ static __always_inline int seal_dentry(struct dentry *dentry,if (!s)return seal_caller(point);bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);- return seal_verdict(bpf_get_dentry_xattr(dentry, XATTR, &tdp),+ return seal_verdict(bpf_get_dentry_xattr(dentry, XATTR_SEAL, &tdp),point);}@@ -556,7 +555,7 @@ int BPF_PROG(shirudo_seal_bprm_check_security,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);+ len = bpf_get_file_xattr(bprm->file, XATTR_POLICY, &tdp);if (len == -ENODATA || len == -EOPNOTSUPP)value = 0;else if (len != sizeof(value))diff --git a/builtin-upatch.c b/builtin-upatch.cindex fa60d50c..b9ab0b44 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -2780,8 +2780,7 @@ static void apply_policy_labels(const char *artifact)entry->path, entry->policy, strerror(errno));continue;}- n = fgetxattr(fd, SHIRUDO_POLICY_XATTR, ¤t,- sizeof(current));+ n = fgetxattr(fd, XATTR_POLICY, ¤t, sizeof(current));if (n < 0 && (errno == ENODATA || errno == EOPNOTSUPP)) {current = 0;} else if (n != (ssize_t)sizeof(current)) {@@ -2791,8 +2790,7 @@ static void apply_policy_labels(const char *artifact)continue;}current |= bits;- if (fsetxattr(fd, SHIRUDO_POLICY_XATTR, ¤t,- sizeof(current), 0) != 0)+ if (fsetxattr(fd, XATTR_POLICY, ¤t, sizeof(current), 0))pr_warn("upatch: cannot set policy label on %s: %s",entry->path, strerror(errno));close(fd);diff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.hindex 320ab54a..58225df7 100644--- a/include/shirudo/upatch.h+++ b/include/shirudo/upatch.h@@ -91,7 +91,6 @@ enum upatch_section_tag {#define UPATCH_MAPS_MAX 64u#define UPATCH_POLICY_NAME_MAX 64u#define UPATCH_POLICY_PATH_MAX 4096u-#define SHIRUDO_POLICY_XATTR "security.bpf.shirudo.policy"struct upatch_policy_label_header {__u32 magic;diff --git a/testing/fixtures/policy-exec-graph/policy-exec-graph.bpf.c b/testing/fixtures/policy-exec-graph/policy-exec-graph.bpf.cindex 01a734b9..c422e795 100644--- a/testing/fixtures/policy-exec-graph/policy-exec-graph.bpf.c+++ b/testing/fixtures/policy-exec-graph/policy-exec-graph.bpf.c@@ -7,7 +7,6 @@char _license[] SEC("license") = "Dual BSD/GPL";-#define SHIRUDO_POLICY_XATTR "security.bpf.shirudo.policy"#define AF_INET 2#ifndef ENODATA#define ENODATA 61@@ -51,8 +50,7 @@ int BPF_PROG(shirudo_test_policy_exec_graph,bpf_dynptr_from_mem(&scratch->target, sizeof(scratch->target),0, &value_ptr))return -13;- len = bpf_get_file_xattr(bprm->file, SHIRUDO_POLICY_XATTR,- &value_ptr);+ len = bpf_get_file_xattr(bprm->file, XATTR_POLICY, &value_ptr);if (len == -ENODATA || len == -EOPNOTSUPP)target = 0;else if (len != sizeof(scratch->target))diff --git a/testing/helper/policy-probe.c b/testing/helper/policy-probe.cindex 1878b692..a9ea9c08 100644--- a/testing/helper/policy-probe.c+++ b/testing/helper/policy-probe.c@@ -23,6 +23,7 @@#include <unistd.h>#include "../../util/identity.h"+#include "../../util/paths.h"static __u64 probe_word = 0x5348495255444fUL;@@ -191,8 +192,7 @@ static int socket_inheritance(const char *script, __u64 extra){__u64 value = extra;- if (setxattr(acceptor, "security.bpf.shirudo.policy", &value,- sizeof(value), 0))+ if (setxattr(acceptor, XATTR_POLICY, &value, sizeof(value), 0))return 1;}length = snprintf(number, sizeof(number), "%d", listener);@@ -246,8 +246,7 @@ static int set_policy(const char *path, const char *text)if (parse_policy(text, &value))return 2;- return setxattr(path, "security.bpf.shirudo.policy", &value,- sizeof(value), 0) ? 1 : 0;+ return setxattr(path, XATTR_POLICY, &value, sizeof(value), 0) ? 1 : 0;}static int remove_policy(const char *path)@@ -255,10 +254,9 @@ static int remove_policy(const char *path)__u64 value;ssize_t n;- if (removexattr(path, "security.bpf.shirudo.policy"))+ if (removexattr(path, XATTR_POLICY))return 1;- n = getxattr(path, "security.bpf.shirudo.policy", &value,- sizeof(value));+ n = getxattr(path, XATTR_POLICY, &value, sizeof(value));return n < 0 && errno == ENODATA ? 0 : 1;}diff --git a/util/paths.h b/util/paths.hindex 286f872a..59df93ff 100644--- a/util/paths.h+++ b/util/paths.h@@ -118,6 +118,7 @@#define SIGNING_KEY_PATH KEYS_DIR "/" SIGNING_KEY_FILE#define XATTR_NAME "security.bpf.shirudo"+#define XATTR_POLICY "security.bpf.shirudo.policy"/** Per-upatch payload_sha256 recorded on the pin directory in bpffs.