eBPF-based security platform
bpf: label a new inode under a sealed directory at its creation
Daniel Borkmanna5f331213d97
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex 9ad34ddc..6cd04eeb 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -300,7 +300,18 @@ strings, nothing a bind mount or a mount namespace can confuse.exec a labelled binary in task-local storage, ``task_alloc`` inheritsthe mark, and ``ptrace_access_check`` and ``perf_event_alloc`` denyagainst marked tasks, so a root debugger or profiler cannot attach to a-running shirudo; a forged label can only mark its forger.+running shirudo; a forged label can only mark its forger. An inode+created under a labelled directory is born with the label:+``inode_init_security`` hands it to the file system inside the+transaction that creates the inode, through+``bpf_inode_init_xattr()``, so a new file of shirudo's never exists+unlabelled. That hook sees the parent as a bare inode and cannot+sleep to read its label, so the seal remembers a labelled directory in+inode storage from the hooks that hold a trusted handle on it:+``file_open`` of the directory, ``inode_post_setxattr`` and+``inode_post_removexattr`` on it, and the creation of a directory+under a marked parent, which marks the child in turn. The label a new+inode gets is its parent's.Process hardening. ``util/harden.c`` runs at startup on the target:``PR_SET_DUMPABLE`` off, ``PR_SET_NO_NEW_PRIVS``, ``PR_SET_PTRACER`` to@@ -662,9 +673,8 @@ flight rather than by shirudo code:(residual risk 1);- BPF in-kernel sockets, so keepalive, status and alerting can be sentfrom kernel context without a userspace timer;-- xattr getters and setters for dentries, files and kernfs, so- ``security.bpf.*`` labels can be propagated race-free at inode- creation and enforced on cgroups;+- xattr getters for kernfs, so ``security.bpf.*`` labels can be+ enforced on cgroups;- ``fmod_ret`` coverage of the whole kernel build, so a shield canreject a call at any function rather than at the instrumented ones;- signed-loader support for every program type, including struct_ops.diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rstindex 059670d3..1f48a801 100644--- a/Documentation/threat-model.rst+++ b/Documentation/threat-model.rst@@ -150,6 +150,12 @@ T3 Tampering with shirudo's files and processthe label. The label marks what is protected and is public; it isnot what identifies the caller, as a file system root mounts canpresent it on any binary.+ An inode created under a labelled directory is born with the+ label, written by the seal inside the transaction that creates it,+ so there is no instant at which a new file of shirudo's can be+ opened, unlinked or renamed by anyone else; what another process+ creates there is sealed from the same instant, out of its own+ reach as well.Marked shirudo tasks cannot be ptraced or perf-sampled, and theprocess drops dumpability, forbids new privileges and lockssensitive buffers. The bpf keyring is sealed with@@ -376,6 +382,17 @@ Each of these is known, documented and bounded; none is silent.the sealed keyring, so what used to be a denial of service withone keyctl is now shirudo's revocation primitive and nothing else.+14. The label at creation follows a mark the seal keeps on a directory+ it has seen since the inode came into memory: one opened, one+ relabelled, or one created under a marked parent. shirudo opens a+ directory before creating in it and puts the label on afterwards+ where the kernel could not, so its own files never exist+ unlabelled; another process creating under a labelled directory+ the seal has not seen since the inode was evicted gets an+ unlabelled inode, as every creation did before, until the next+ ``init`` labels the tree. Rename and link bring an existing inode+ in without creating one, so they bring it in unlabelled.+Deferred mitigations~~~~~~~~~~~~~~~~~~~~@@ -385,8 +402,12 @@ values of each boot and systemd-aware verifiers find it in PCR 9 beforethe separator, and measuring the anchor through ``systemd-pcrextend`` sosystemd's measurement log explains it (residual risk 11); signed PCRpolicies for the kernel and initrd PCRs so a verifier needs no golden-values; and an in-kernel detach so the unload compare runs against-signed ``.rodata`` (residual risk 1).+values; an in-kernel detach so the unload compare runs against+signed ``.rodata`` (residual risk 1); and refusing creation, rename+and link into a labelled directory from any process but shirudo, which+would keep foreign entries out of the tree altogether, and a+``d_instantiate`` hook the BPF LSM can sleep in, which would mark a+directory the moment its dentry is bound (residual risk 14).6) Out of scopediff --git a/bpf/seal.c b/bpf/seal.cindex 0b2e9ac8..78180648 100644--- a/bpf/seal.c+++ b/bpf/seal.c@@ -23,6 +23,15 @@* TOCTOU games. Reads are not gated; only write-mode opens and the* mutating inode ops are.*+ * An inode created under a labelled directory is born with the label:+ * lsm/inode_init_security hands it to the filesystem inside the+ * transaction that creates the inode, so there is no instant at which+ * one of shirudo's files exists unlabelled. That hook gets the parent+ * as a bare inode and cannot sleep to read its label, so a labelled+ * directory is remembered in inode storage by the hooks that hold a+ * trusted handle on it: file_open of the directory itself, a setxattr+ * or removexattr on it, and its own creation under a marked parent.+ ** The filesystem and exec xattr hooks are sleepable (lsm.s/) because* reading an xattr can sleep. The xattr-reading kfuncs write into a* bpf_dynptr over map memory (the verifier rejects stack), so the@@ -53,6 +62,12 @@#ifndef FMODE_WRITE#define FMODE_WRITE 0x2#endif+#ifndef S_IFMT+#define S_IFMT 00170000+#endif+#ifndef S_IFDIR+#define S_IFDIR 0040000+#endifchar _license[] SEC("license") = "Dual BSD/GPL";@@ -74,10 +89,26 @@ struct {__type(value, struct seal_scratch);} seal_scratch_map SEC(".maps");+/* The label of a directory, as the last hook with a handle on it read it. */+struct seal_dir_mark {+ char label[LABEL_LEN];+};++struct {+ __uint(type, BPF_MAP_TYPE_INODE_STORAGE);+ __uint(map_flags, BPF_F_NO_PREALLOC);+ __type(key, int);+ __type(value, struct seal_dir_mark);+} seal_dir_marks SEC(".maps");+extern int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__str,struct bpf_dynptr *value_p) __ksym;extern int bpf_get_file_xattr(struct file *file, const char *name__str,struct bpf_dynptr *value_p) __ksym;+extern int bpf_inode_init_xattr(struct xattr *xattrs,+ int *xattr_count__ctx_out,+ 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)@@ -132,6 +163,27 @@ static __always_inline struct seal_scratch *seal_scratch_get(BPF_LOCAL_STORAGE_GET_F_CREATE);}+static __always_inline bool seal_is_dir(struct inode *inode)+{+ return (BPF_CORE_READ(inode, i_mode) & S_IFMT) == S_IFDIR;+}++/* Remember a directory's label as just read (len bytes), or forget it. */+static __always_inline void seal_mark(struct inode *inode, const char *label,+ int len)+{+ struct seal_dir_mark *m;++ if (len != LABEL_LEN) {+ bpf_inode_storage_delete(&seal_dir_marks, inode);+ return;+ }+ m = bpf_inode_storage_get(&seal_dir_marks, inode, NULL,+ BPF_LOCAL_STORAGE_GET_F_CREATE);+ if (m)+ __builtin_memcpy(m->label, label, LABEL_LEN);+}+/** Stage the candidate executable's seal label during exec. A later LSM* hook may still reject the exec, so the persistent task marker is only@@ -242,13 +294,24 @@ int BPF_PROG(shirudo_seal_ptrace_access_check,SEC("lsm.s/file_open")int BPF_PROG(shirudo_seal_file_open, struct file *file){+ struct inode *inode = file->f_inode;struct task_struct *task;struct seal_scratch *s;struct bpf_dynptr tdp;+ task = bpf_get_current_task_btf();+ if (seal_is_dir(inode)) {+ /* Only ever opened for reading; its label marks it. */+ s = seal_scratch_get(task);+ if (!s)+ return 0;+ bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);+ seal_mark(inode, s->target,+ bpf_get_file_xattr(file, SHIRUDO_XATTR, &tdp));+ return 0;+ }if (!(BPF_CORE_READ(file, f_mode) & FMODE_WRITE))return 0; /* reads are fine */- task = bpf_get_current_task_btf();s = seal_scratch_get(task);if (!s)return seal_caller(); /* cannot read the label */@@ -256,6 +319,73 @@ int BPF_PROG(shirudo_seal_file_open, struct file *file)return seal_verdict(bpf_get_file_xattr(file, SHIRUDO_XATTR, &tdp));}+/* Refresh a directory's mark from its label after an xattr change. */+static __always_inline void seal_mark_dentry(struct dentry *dentry)+{+ struct inode *inode = dentry->d_inode;+ struct seal_scratch *s;+ struct bpf_dynptr tdp;++ if (!inode || !seal_is_dir(inode))+ return;+ s = seal_scratch_get(bpf_get_current_task_btf());+ if (!s)+ return;+ bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);+ seal_mark(inode, s->target,+ bpf_get_dentry_xattr(dentry, SHIRUDO_XATTR, &tdp));+}++SEC("lsm.s/inode_post_setxattr")+void BPF_PROG(shirudo_seal_inode_post_setxattr, struct dentry *dentry,+ const char *name, const void *value, __u64 size, int flags)+{+ seal_mark_dentry(dentry);+}++SEC("lsm.s/inode_post_removexattr")+void BPF_PROG(shirudo_seal_inode_post_removexattr, struct dentry *dentry,+ const char *name)+{+ seal_mark_dentry(dentry);+}++/*+ * Label a new inode inside the transaction that creates it when its+ * parent directory is marked, with the parent's label, and mark a new+ * directory in turn so a subtree created in one go needs no reopen.+ * The hook cannot sleep, so the label comes from the mark, not from+ * disk. A claim that fails (no slot left, no memory) aborts the+ * creation: better no inode than one of shirudo's without its label,+ * and the verifier wants a constant in the LSM's error range rather+ * than the kfunc's. A filesystem that takes no xattrs at creation+ * passes none; the label follows from userspace there.+ */+SEC("lsm/inode_init_security")+int BPF_PROG(shirudo_seal_inode_init_security, struct inode *inode,+ struct inode *dir, const struct qstr *qstr,+ struct xattr *xattrs, int *xattr_count)+{+ struct seal_dir_mark *m, *child;+ struct bpf_dynptr dp;++ if (!xattrs)+ return 0;+ m = bpf_inode_storage_get(&seal_dir_marks, dir, NULL, 0);+ if (!m)+ return 0;+ bpf_dynptr_from_mem(m->label, LABEL_LEN, 0, &dp);+ if (bpf_inode_init_xattr(xattrs, xattr_count, SHIRUDO_XATTR, &dp))+ return -EACCES;+ if (!seal_is_dir(inode))+ return 0;+ child = bpf_inode_storage_get(&seal_dir_marks, inode, NULL,+ BPF_LOCAL_STORAGE_GET_F_CREATE);+ if (child)+ __builtin_memcpy(child->label, m->label, LABEL_LEN);+ return 0;+}+static __always_inline int seal_dentry(struct dentry *dentry){struct task_struct *task = bpf_get_current_task_btf();diff --git a/builtin-init.c b/builtin-init.cindex 18ab3e8e..29fc2a6b 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -632,6 +632,33 @@ static int attach_pin(struct bpf_program *p, const char *pin,return 0;}+/*+ * The seal's programs and their pins. The directory-marking hooks+ * (file_open and the post-xattr hooks) precede inode_init_security,+ * which labels on the strength of those marks.+ */+static const struct {+ const char *prog;+ const char *pin;+} seal_hooks[] = {+ { "shirudo_seal_bprm_check_security", SHIRUDO_SEAL_BPRM_CHECK_PIN },+ { "shirudo_seal_bprm_committed_creds",+ SHIRUDO_SEAL_BPRM_COMMITTED_PIN },+ { "shirudo_seal_task_alloc", SHIRUDO_SEAL_TASK_ALLOC_PIN },+ { "shirudo_seal_file_open", SHIRUDO_SEAL_FILE_OPEN_PIN },+ { "shirudo_seal_inode_post_setxattr",+ SHIRUDO_SEAL_POST_SETXATTR_PIN },+ { "shirudo_seal_inode_post_removexattr",+ SHIRUDO_SEAL_POST_RMXATTR_PIN },+ { "shirudo_seal_inode_init_security",+ SHIRUDO_SEAL_INIT_SECURITY_PIN },+ { "shirudo_seal_inode_unlink", SHIRUDO_SEAL_UNLINK_PIN },+ { "shirudo_seal_inode_rename", SHIRUDO_SEAL_RENAME_PIN },+ { "shirudo_seal_inode_setattr", SHIRUDO_SEAL_SETATTR_PIN },+ { "shirudo_seal_inode_setxattr", SHIRUDO_SEAL_SETXATTR_PIN },+ { "shirudo_seal_inode_removexattr", SHIRUDO_SEAL_RMXATTR_PIN },+};+/** Load seal.bpf.o and attach + pin its filesystem-sealing and exec hooks.* Keeps the object open via *obj_out (the caller tears it and the links@@ -644,22 +671,6 @@ static int attach_pin(struct bpf_program *p, const char *pin,static int attach_seal(struct bpf_object **obj_out, struct bpf_link **links,int identity_fd){- static const struct {- const char *prog;- const char *pin;- } hooks[] = {- { "shirudo_seal_bprm_check_security",- SHIRUDO_SEAL_BPRM_CHECK_PIN },- { "shirudo_seal_bprm_committed_creds",- SHIRUDO_SEAL_BPRM_COMMITTED_PIN },- { "shirudo_seal_task_alloc", SHIRUDO_SEAL_TASK_ALLOC_PIN },- { "shirudo_seal_file_open", SHIRUDO_SEAL_FILE_OPEN_PIN },- { "shirudo_seal_inode_unlink", SHIRUDO_SEAL_UNLINK_PIN },- { "shirudo_seal_inode_rename", SHIRUDO_SEAL_RENAME_PIN },- { "shirudo_seal_inode_setattr", SHIRUDO_SEAL_SETATTR_PIN },- { "shirudo_seal_inode_setxattr", SHIRUDO_SEAL_SETXATTR_PIN },- { "shirudo_seal_inode_removexattr", SHIRUDO_SEAL_RMXATTR_PIN },- };const struct shirudo_embedded_bpf *blob = shirudo_seal_blob();struct bpf_object *obj;struct bpf_map *m;@@ -694,18 +705,19 @@ static int attach_seal(struct bpf_object **obj_out, struct bpf_link **links,bpf_object__close(obj);return err;}- for (i = 0; i < (int)ARRAY_SIZE(hooks); i++) {+ for (i = 0; i < (int)ARRAY_SIZE(seal_hooks); i++) {struct bpf_program *p =- bpf_object__find_program_by_name(obj, hooks[i].prog);+ bpf_object__find_program_by_name(obj,+ seal_hooks[i].prog);if (!p) {- pr_err("seal: program %s missing", hooks[i].prog);+ pr_err("seal: program %s missing", seal_hooks[i].prog);err = -ENOENT;break;}- err = attach_pin(p, hooks[i].pin, &links[i]);+ err = attach_pin(p, seal_hooks[i].pin, &links[i]);if (err) {- pr_err("seal: attach %s: %s", hooks[i].prog,+ pr_err("seal: attach %s: %s", seal_hooks[i].prog,strerror(-err));break;}@@ -775,7 +787,7 @@ static int init_lsm(bool attach_filesystem_seal)struct bpf_link *link_unlink = NULL;struct bpf_link *link_setx = NULL, *link_rmx = NULL, *link_pload = NULL;struct bpf_link *link_keyperm = NULL;- struct bpf_link *seal_links[9] = {};+ struct bpf_link *seal_links[ARRAY_SIZE(seal_hooks)] = {};struct bpf_program *p_keyperm;struct anchor_keys keys = {};key_serial_t keyring;diff --git a/util/paths.h b/util/paths.hindex eb0fe61d..bfa9b2ce 100644--- a/util/paths.h+++ b/util/paths.h@@ -75,6 +75,9 @@#define SHIRUDO_SEAL_SETATTR_PIN SHIRUDO_BPFFS_DIR "/seal_inode_setattr_link"#define SHIRUDO_SEAL_SETXATTR_PIN SHIRUDO_BPFFS_DIR "/seal_inode_setxattr_link"#define SHIRUDO_SEAL_RMXATTR_PIN SHIRUDO_BPFFS_DIR "/seal_inode_removexattr_link"+#define SHIRUDO_SEAL_POST_SETXATTR_PIN SHIRUDO_BPFFS_DIR "/seal_inode_post_setxattr_link"+#define SHIRUDO_SEAL_POST_RMXATTR_PIN SHIRUDO_BPFFS_DIR "/seal_inode_post_removexattr_link"+#define SHIRUDO_SEAL_INIT_SECURITY_PIN SHIRUDO_BPFFS_DIR "/seal_inode_init_security_link"#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"