eBPF-based security platform
bpf: check the destination of a rename in the seal
Puranjay Mohaneea0aa93298f
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex 712d0b82..6f041971 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -289,13 +289,13 @@ every build. The label says what is protected, not who may touch it:its value is public, and a binary that carries it proves nothing, sinceany file system root mounts can present it. The ``file_open`` (forwrite), ``inode_unlink``, ``inode_rename``, ``inode_setxattr`` and-``inode_removexattr`` hooks read the label of the target inode and, when-there is one, allow only the task that runs the executable ``init``-recorded in the frozen identity map, the same ``(dev, ino)`` test the-bpffs guards use. ``inode_setxattr`` also lets only that task set the-label itself, so root cannot mark files of its choosing. No-``bpf_d_path()``, no path strings, nothing a bind mount or a mount-namespace can confuse. ``bprm_check_security`` and+``inode_removexattr`` hooks read the label of the target inode, both+ends of a rename, and, when there is one, allow only the task that runs+the executable ``init`` recorded in the frozen identity map, the same+``(dev, ino)`` test the bpffs guards use. ``inode_setxattr`` also lets+only that task set the label itself, so root cannot mark files of its+choosing. No ``bpf_d_path()``, no path strings, nothing a bind mount or+a mount namespace can confuse. ``bprm_check_security`` and``bprm_committed_creds`` mark tasks that exec a labelled binary intask-local storage, ``task_alloc`` inherits the mark, and``ptrace_access_check`` and ``perf_event_alloc`` deny against markeddiff --git a/bpf/seal.c b/bpf/seal.cindex 8d5bfef6..74260999 100644--- a/bpf/seal.c+++ b/bpf/seal.c@@ -275,13 +275,36 @@ int BPF_PROG(shirudo_seal_inode_unlink, struct inode *dir,return seal_dentry(dentry);}-/* Protect the source of a rename -- moving a labelled inode out. */+/* Whether an inode is the executable recorded at init. */+static __always_inline bool seal_is_exe(struct inode *inode)+{+ struct shirudo_sb_id *id;+ __u32 zero = 0;++ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);+ return id && BPF_CORE_READ(inode, i_sb, s_dev) == id->exe_dev &&+ BPF_CORE_READ(inode, i_ino) == id->exe_ino;+}++/*+ * Protect both ends of a rename: moving a labelled inode out and+ * replacing one. The recorded executable may be replaced, as a package+ * upgrade does; the new binary is not shirudo until init records it.+ */SEC("lsm.s/inode_rename")int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,struct dentry *old_dentry, struct inode *new_dir,struct dentry *new_dentry){- return seal_dentry(old_dentry);+ struct inode *target = BPF_CORE_READ(new_dentry, d_inode);+ int err;++ err = seal_dentry(old_dentry);+ if (err)+ return err;+ if (!target || seal_is_exe(target))+ return 0;+ return seal_dentry(new_dentry);}SEC("lsm.s/inode_setxattr")diff --git a/testing/tamperproof/t1132-seal-scratch.sh b/testing/tamperproof/t1132-seal-scratch.shindex fc0a817a..47cdecd0 100644--- a/testing/tamperproof/t1132-seal-scratch.sh+++ b/testing/tamperproof/t1132-seal-scratch.sh@@ -33,7 +33,7 @@ sys.stdout.buffer.write(os.getxattr(sys.argv[1], "security.bpf.shirudo"))# Write the label into an image offline, as a mounted file system presents# it without any setxattr the seal could refuse.-SEALED="sealed1 sealed2 sealed3 sealed4"+SEALED="sealed1 sealed2 sealed3 sealed4 sealed5"echo scratch >"$WORK/scratch"{echo "write $SH sh"@@ -104,4 +104,11 @@ test_expect_success 'a binary its file system labels cannot rename, unlink or re"$MNT/py" "$WORK/ops.py" "$MNT"'+test_expect_success 'root cannot rename a file over a labelled file' '+ echo replacement >"$MNT/replacement" &&+ ! python3 -c "import os, sys; os.rename(sys.argv[1], sys.argv[2])" \+ "$MNT/replacement" "$MNT/sealed5" 2>/dev/null &&+ [ "$(cat "$MNT/sealed5")" = scratch ]+'+test_donediff --git a/testing/tamperproof/test.plan.md b/testing/tamperproof/test.plan.mdindex 73438858..8cdfd420 100644--- a/testing/tamperproof/test.plan.md+++ b/testing/tamperproof/test.plan.md@@ -42,6 +42,7 @@ checks that Shirudo can still export the pre-existing Python exec record.| TP-20 | Obtain or transfer Shirudo's open map FDs via `/proc/<pid>/fd`, `pidfd_getfd`, or `SCM_RIGHTS` | Not covered | None | Test each applicable acquisition/transfer route and verify a received FD cannot be used to mutate protected maps. || TP-21 | Concurrent or repeated serializer invocation changes/loses records | Not covered | None | Add after TP-14 policy fix; exercise repeated and concurrent export attempts and verify record integrity. || TP-22 | Root puts the public seal label on a file, or runs a binary a mounted file system labels, and then writes, renames, unlinks or relabels a labelled file | Covered | `t1132-seal-scratch.sh` | Only Shirudo may set the label, and the seal admits the executable `init` recorded, not one that carries the label. A failure is a bypass of the seal. |+| TP-23 | Root renames an unlabelled file over a labelled file and so replaces it | Covered | `t1132-seal-scratch.sh` | The seal checks the destination of a rename as well as its source; only the recorded executable may be replaced, as a package upgrade does. |## Review notes