eBPF-based security platform
seal: refuse the overlay copy-up of a marked inode
Daniel Borkmann296846580e9f
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex ea2099e7..7538bb7c 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -340,8 +340,10 @@ The ``file_open`` (for write), ``inode_unlink``, ``inode_rename``,the caller's committed task authority, and so do ``inode_create``,``inode_mkdir``, ``inode_symlink``, ``inode_mknod``, ``inode_link`` and arename into a marked directory: a sealed directory takes new entries from-shirudo alone. Runtime and config files use dm-verity-and receive no seal writes.+shirudo alone. ``inode_copy_up`` refuses the overlay copy-up of a marked+inode, so a write through an overlay mounted over sealed state fails+rather than landing, label and all, in the upper directory. Runtime and+config files use dm-verity and receive no seal writes.Only an authorized caller may set the exact seal xattr, including on anunlabelled inode. A binary carrying a copied label gains no authority.A path the caller names for shirudo to write (``--report``, ``--out``,diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rstindex a25899c1..72c41f4f 100644--- a/Documentation/threat-model.rst+++ b/Documentation/threat-model.rst@@ -182,7 +182,13 @@ T3 Tampering with shirudo's files and processforeign entry rather than labelling, following, restoring orfailing on it, and refuses a state root without the label onceenforcement is live, since root could bind a tree of its own over- the path in a mount namespace and have it labelled. Directory+ the path in a mount namespace and have it labelled. An overlay+ root mounts over sealed state in a mount namespace of its own+ copies a lower inode up into root's upper directory before the+ first write reaches it, as the writer, so the copy carried the+ label and a write shirudo made through it landed in root's+ directory: the seal refuses the copy-up of a marked inode+ (``SealCopyUp``) and such a write fails instead. Directoryeviction remains subject to residual risk 15; existing writablereferences are not revoked.The authority the seal grants shirudo is not root's to aim: a pathdiff --git a/bpf/lib/shirudo_tamper.h b/bpf/lib/shirudo_tamper.hindex 148acbf9..d2d9ae8a 100644--- a/bpf/lib/shirudo_tamper.h+++ b/bpf/lib/shirudo_tamper.h@@ -43,6 +43,7 @@ enum shirudo_tamper_point {SHIRUDO_TAMPER_SEAL_ENTRY,SHIRUDO_TAMPER_PERF_ATTACH,SHIRUDO_TAMPER_PIN_ENTRY,+ SHIRUDO_TAMPER_SEAL_COPY_UP,SHIRUDO_TAMPER_MAX};diff --git a/bpf/seal.h b/bpf/seal.hindex f5a2dcb7..4269e07e 100644--- a/bpf/seal.h+++ b/bpf/seal.h@@ -48,7 +48,10 @@ 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. */+/*+ * The label of an inode as the last hook with a handle on it read it: a+ * directory at every open, a file at an open for writing.+ */struct seal_dir_mark {char label[LABEL_LEN];};@@ -230,6 +233,7 @@ int BPF_PROG(shirudo_seal_file_open, struct file *file, int ret)struct task_struct *task;struct seal_scratch *s;struct bpf_dynptr tdp;+ int tlen;if (ret)return ret;@@ -250,8 +254,32 @@ int BPF_PROG(shirudo_seal_file_open, struct file *file, int ret)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),- SHIRUDO_TAMPER_SEAL_WRITE);+ tlen = bpf_get_file_xattr(file, SHIRUDO_XATTR, &tdp);+ if (tlen == LABEL_LEN)+ seal_mark(inode, s->target, tlen);+ return seal_verdict(tlen, SHIRUDO_TAMPER_SEAL_WRITE);+}++/*+ * An overlay copies a lower inode up before the first write reaches it,+ * label and all, since the copy-up runs as the writer: a write shirudo+ * made through one mounted over sealed state landed in the upper+ * directory, root's, and the sealed inode stayed as it was. Sealed+ * state is never copied up. The hook cannot sleep, so the mark a write+ * open or a directory open leaves stands in for the label.+ */+SEC("lsm/inode_copy_up")+int BPF_PROG(shirudo_seal_inode_copy_up, struct dentry *src,+ struct cred **new, int ret)+{+ struct inode *inode = src->d_inode;++ (void)new;+ if (ret)+ return ret;+ if (!inode || !bpf_inode_storage_get(&seal_dir_marks, inode, NULL, 0))+ return 0;+ return shirudo_tamper_deny(SHIRUDO_TAMPER_SEAL_COPY_UP, -EPERM);}/* Refresh a directory's mark from its label after an xattr change. */diff --git a/util/image_guards.c b/util/image_guards.cindex bfa3e075..ef7b6bc5 100644--- a/util/image_guards.c+++ b/util/image_guards.c@@ -53,6 +53,7 @@ shirudo_seal_guards[SHIRUDO_SEAL_HOOK_COUNT] = {{ "shirudo_seal_inode_symlink", SHIRUDO_SEAL_SYMLINK_PIN },{ "shirudo_seal_inode_mknod", SHIRUDO_SEAL_MKNOD_PIN },{ "shirudo_seal_inode_link", SHIRUDO_SEAL_LINK_PIN },+ { "shirudo_seal_inode_copy_up", SHIRUDO_SEAL_COPY_UP_PIN },{ "shirudo_seal_ptrace_access_check",SHIRUDO_SEAL_PTRACE_PIN },{ "shirudo_seal_perf_event_alloc",diff --git a/util/image_guards.h b/util/image_guards.hindex 441eb4ba..f2de33e6 100644--- a/util/image_guards.h+++ b/util/image_guards.h@@ -8,7 +8,7 @@#include <stdint.h>-#define SHIRUDO_SEAL_HOOK_COUNT 19+#define SHIRUDO_SEAL_HOOK_COUNT 20#define SHIRUDO_IMAGE_SEAL_HOOK_COUNT 2#define SHIRUDO_MOUNT_HOOK_COUNT 5#define SHIRUDO_OBSERVER_HOOK_COUNT 7diff --git a/util/paths.h b/util/paths.hindex 44fcc983..d37511ea 100644--- a/util/paths.h+++ b/util/paths.h@@ -98,6 +98,7 @@#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_COPY_UP_PIN SHIRUDO_BPFFS_DIR "/seal_inode_copy_up_link"#define SHIRUDO_SEAL_INIT_SECURITY_PIN SHIRUDO_BPFFS_DIR "/seal_inode_init_security_link"#define SHIRUDO_SEAL_CREATE_PIN SHIRUDO_BPFFS_DIR "/seal_inode_create_link"#define SHIRUDO_SEAL_MKDIR_PIN SHIRUDO_BPFFS_DIR "/seal_inode_mkdir_link"diff --git a/util/tamper.c b/util/tamper.cindex 15963446..eef09d15 100644--- a/util/tamper.c+++ b/util/tamper.c@@ -84,6 +84,8 @@ static const struct {"program attached to a perf event by another executable" },[SHIRUDO_TAMPER_PIN_ENTRY] = { "PinEntry","directory or link made in the private bpffs by another executable" },+ [SHIRUDO_TAMPER_SEAL_COPY_UP] = { "SealCopyUp",+ "overlay copy-up of a sealed inode" },};const char *tamper_name(enum shirudo_tamper_point point)