eBPF-based security platform
seal: refuse renaming the directories above the state root
Daniel Borkmann77e4cd607376
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex c1800a9a..b6c8c481 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -492,6 +492,10 @@ signed config image.- A directory of the config image bound in place of its mount, in anamespace with a ``/run`` of root's making, is refused as theconfig+ * - ``t6150-ancestor-rename``+ - The directories above the state root carry the ancestor mark; a+ rename of one and the removal of the mark are refused and counted,+ and a directory beside the state root still renames``bpm`` (7)~~~~~~~~~~~diff --git a/Documentation/internals/enforcement.rst b/Documentation/internals/enforcement.rstindex 0f587d7f..ae52a1d7 100644--- a/Documentation/internals/enforcement.rst+++ b/Documentation/internals/enforcement.rst@@ -527,8 +527,8 @@ when the record it needs is missing.``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. Removing the- seal or the policy label is refused on any inode without- authority.+ seal or the policy label or the ancestor mark is refused on any+ inode without authority.- ``EACCES``- ``SealUnlink``, ``SealSetattr``, ``SealXattr``, ``SealLabel``- See the label verdict below.@@ -540,8 +540,9 @@ when the record it needs is missing.- ``SealXattr``, ``SealSetattr``- See the label verdict below.* - ``lsm.s/inode_setxattr`` (``seal``)- - Setting ``security.bpf.shirudo`` or- ``security.bpf.shirudo.policy`` on any inode, and any other name+ - Setting ``security.bpf.shirudo``,+ ``security.bpf.shirudo.policy`` or+ ``security.bpf.shirudo.ancestor`` on any inode, and any other nameon a labeled inode, to a task without authority. Policy valuesmust be exactly 8 bytes.- ``EACCES``@@ -549,7 +550,8 @@ when the record it needs is missing.- See the label verdict below.* - ``lsm.s/inode_rename`` (``seal``)- A labeled source, a labeled destination directory or a labeled- inode it would replace, to a task without authority.+ inode it would replace, or a source or replaced directory that+ carries the ancestor mark, to a task without authority.- ``EACCES``- ``SealRename``, ``SealEntry``- See the label verdict below.diff --git a/Documentation/internals/kernel-interfaces.rst b/Documentation/internals/kernel-interfaces.rstindex 7e3ad0b5..d38cc9ba 100644--- a/Documentation/internals/kernel-interfaces.rst+++ b/Documentation/internals/kernel-interfaces.rst@@ -327,7 +327,8 @@ Kfuncs and helpers* - Kfunc- Use* - ``bpf_get_file_xattr()``, ``bpf_get_dentry_xattr()``- - Read ``security.bpf.shirudo`` and ``security.bpf.shirudo.policy``.+ - Read ``security.bpf.shirudo``, ``security.bpf.shirudo.policy``+ and ``security.bpf.shirudo.ancestor``.Sleepable; upstream they check ``MAY_READ`` for the calling taskfirst, which the carried patch 0002 skips for ``security.bpf.``names.diff --git a/Documentation/internals/seal.rst b/Documentation/internals/seal.rstindex 223160f8..4cddd12b 100644--- a/Documentation/internals/seal.rst+++ b/Documentation/internals/seal.rst@@ -208,6 +208,14 @@ Setting or removing the attribute ``security.bpf.shirudo`` itself, orlabeled or not (``SealLabel``), so root cannot label an inode of its ownas shirudo's.+The directories above the state root are not labeled, since root makes+entries in ``/var/lib`` freely, but renaming one would move the sealed+tree aside, and the next boot's bootstrap run would label whatever root+put at ``/var/lib/shirudo``. That run marks each of them, up to the root+of the state's file system, with ``security.bpf.shirudo.ancestor``, and+a rename of a marked directory, or over one, needs runtime authority+(``SealRename``), as does setting or removing the mark (``SealLabel``).+A labeled directory takes new entries only from a task with runtimeauthority: ``inode_create``, ``inode_mkdir``, ``inode_symlink``,``inode_mknod``, ``inode_link`` and a rename into it (``SealEntry``).diff --git a/Documentation/reference/files.rst b/Documentation/reference/files.rstindex 477cf33a..f6246e76 100644--- a/Documentation/reference/files.rst+++ b/Documentation/reference/files.rst@@ -425,6 +425,14 @@ Extended attributesbitset. Exec ORs the original file and interpreter values intotask storage. Only a task with runtime authority may set, updateor remove it (``SealLabel``).+ * - ``security.bpf.shirudo.ancestor``+ - Each directory above ``/var/lib/shirudo`` on its file system,+ ``/var/lib`` and ``/var`` when the state is on the root file+ system.+ - ``1``, set by the run of ``init`` that brings enforcement up. The+ seal refuses a task without runtime authority a rename of or over+ a directory that carries it (``SealRename``), and the setting or+ removal of it on any inode (``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 28cdb0ce..674c706c 100644--- a/Documentation/security/tamper-counters.rst+++ b/Documentation/security/tamper-counters.rst@@ -136,8 +136,10 @@ counter.- ``lsm.s/inode_unlink``, ``lsm.s/inode_rmdir``- ``EACCES``* - ``SealRename``- - rename of or over a sealed file- - ``lsm.s/inode_rename``: a labeled source or victim+ - rename of or over a sealed file, or of or over a directory above+ the state root+ - ``lsm.s/inode_rename``: a labeled source or victim, or one that+ carries the ``security.bpf.shirudo.ancestor`` mark- ``EACCES``* - ``SealSetattr``- truncate, chmod, chown, utimes or chattr of a sealed file@@ -151,8 +153,8 @@ counter.* - ``SealLabel``- 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+ names ``security.bpf.shirudo``, ``security.bpf.shirudo.policy`` or+ ``security.bpf.shirudo.ancestor`` on any inode- ``EACCES``* - ``SealPtrace``- ptrace of a shirudo taskdiff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex e9b7e728..5263db9f 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -685,6 +685,15 @@ runtime authority (``SealWrite``, ``SealUnlink``, ``SealRename``,label. Only an authorized task sets or removes the exact seal label orchanges the policy label on any inode (``SealLabel``).+A rename of a directory above the state root, ``/var/lib`` say, would+move the sealed tree aside for the next boot's bootstrap run of+``init`` to label whatever root then puts at the path. That run marks+the directories above the state root on its file system with+``security.bpf.shirudo.ancestor``, and the seal refuses a task without+authority a rename of a marked directory or over it (``SealRename``)+and the setting or removal of the mark (``SealLabel``) (``t6150``).+What the next boot mounts at the path stays root's choice (R19).+The label is a public constant, the same on every build. It marks atarget and never grants authority: a file system root mounts can carryit, and a binary that carries it gains nothing. A label read that fails@@ -884,7 +893,7 @@ An authorized process is protected against being taken over:``t6112``, ``t6113``, ``t6114``, ``t6115``, ``t6118``, ``t6123``,``t6124``, ``t6127``, ``t6129``, ``t6131``, ``t6133``, ``t6134``,``t6135``, ``t6136``, ``t6137``, ``t6144``, ``t6145``, ``t6146``,- ``t6149``+ ``t6149``, ``t6150``:Residual: R2, R19, R22T4 Substituting artifacts@@ -1627,7 +1636,12 @@ R19 Raw device and offline writes bypass the sealThe seal guards file operations. A write to the block device under``/var/lib/shirudo`` while it is mounted (A10), or any change betweenboots, bypasses it. Removing the label from the state root offline- makes the next bootstrap run of ``init`` label everything under it.+ makes the next bootstrap run of ``init`` label everything under it,+ and so does another tree at ``/var/lib/shirudo`` on the next boot,+ which root chooses on the running system too: the root file system's+ ``fstab`` decides what the initrd mounts at ``/var`` and below+ (``x-initrd.mount``). Only the rename of a directory above the state+ root on its own file system is refused (T3).Across reboots only signatures and the register protect state:upatches are verified at load and measured in the init event, andthe package, tree and config selection state is not authenticated.diff --git a/bpf/lib/xattr.h b/bpf/lib/xattr.hindex cc372835..23218c02 100644--- a/bpf/lib/xattr.h+++ b/bpf/lib/xattr.h@@ -6,6 +6,7 @@#define XATTR_SEAL "security.bpf.shirudo"#define XATTR_POLICY "security.bpf.shirudo.policy"+#define XATTR_ANCESTOR "security.bpf.shirudo.ancestor"/* 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 be2089dc..d117c279 100644--- a/bpf/seal.c+++ b/bpf/seal.c@@ -130,6 +130,17 @@ static __always_inline bool seal_is_policy(const char *name)return !bpf_strncmp(buf, sizeof(XATTR_POLICY), XATTR_POLICY);}+static __always_inline bool seal_is_ancestor(const char *name)+{+ char buf[sizeof(XATTR_ANCESTOR) + 1];+ long n;++ n = bpf_probe_read_kernel_str(buf, sizeof(buf), name);+ if (n != sizeof(XATTR_ANCESTOR))+ return false;+ return !bpf_strncmp(buf, sizeof(XATTR_ANCESTOR), XATTR_ANCESTOR);+}+static __always_inline struct seal_scratch *seal_scratch_get(struct task_struct *task){@@ -515,6 +526,32 @@ int BPF_PROG(shirudo_seal_inode_link, const struct mnt_idmap *idmap,return seal_parent(new_dentry, TAMPER_SEAL_ENTRY);}+/*+ * A rename of a directory above the state root, /var/lib say, moves the+ * sealed tree aside, and the next boot's bootstrap init labels whatever+ * root then puts at the path as shirudo's. init marks those directories+ * with an xattr of their own, which only this check reads: the seal+ * label would refuse root every new entry there.+ */+static __always_inline int seal_ancestor(struct dentry *dentry)+{+ struct inode *inode = BPF_CORE_READ(dentry, d_inode);+ struct seal_scratch *s;+ struct bpf_dynptr tdp;+ int len;++ if (!inode || !seal_is_dir(inode))+ return 0;+ s = seal_scratch_get(bpf_get_current_task_btf());+ if (!s)+ return seal_caller(TAMPER_SEAL_RENAME);+ bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);+ len = bpf_get_dentry_xattr(dentry, XATTR_ANCESTOR, &tdp);+ if (len == -ENODATA || len == -EOPNOTSUPP)+ return 0;+ return seal_caller(TAMPER_SEAL_RENAME);+}+/* Protect the source, the destination directory and an existing target. */SEC("lsm.s/inode_rename")int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,@@ -527,6 +564,9 @@ int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,if (ret)return ret;err = seal_dentry(old_dir, old_dentry, TAMPER_SEAL_RENAME);+ if (err)+ return err;+ err = seal_ancestor(old_dentry);if (err)return err;err = seal_parent(new_dentry, TAMPER_SEAL_ENTRY);@@ -534,7 +574,8 @@ int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,return err;if (!target)return 0;- return seal_dentry(new_dir, new_dentry, TAMPER_SEAL_RENAME);+ err = seal_dentry(new_dir, new_dentry, TAMPER_SEAL_RENAME);+ return err ? err : seal_ancestor(new_dentry);}/* Truncate, chmod, chown and utimes all come through here. */@@ -552,7 +593,8 @@ int BPF_PROG(shirudo_seal_inode_setxattr, struct mnt_idmap *idmap,{if (ret)return ret;- if (seal_is_label(name) || seal_is_policy(name))+ if (seal_is_label(name) || seal_is_policy(name) ||+ seal_is_ancestor(name))return seal_caller(TAMPER_SEAL_LABEL);return seal_dentry(NULL, dentry, TAMPER_SEAL_XATTR);}@@ -568,7 +610,8 @@ int BPF_PROG(shirudo_seal_inode_removexattr, struct mnt_idmap *idmap,{if (ret)return ret;- if (seal_is_label(name) || seal_is_policy(name))+ if (seal_is_label(name) || seal_is_policy(name) ||+ seal_is_ancestor(name))return seal_caller(TAMPER_SEAL_LABEL);return seal_dentry(NULL, dentry, TAMPER_SEAL_XATTR);}diff --git a/builtin-init.c b/builtin-init.cindex f4eb0a28..32340bed 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -1345,6 +1345,14 @@ static void usage(void)fprintf(stderr, "usage: %s\n", init_help.usage);}+/* Where the state tree is on the root file system, which the initrd binds. */+static int state_source(char *buf, size_t cap)+{+ if (!initrd_mode)+ return strscpy(buf, VAR_LIB, cap);+ return strsprintf(buf, cap, "%s%s", sysroot(), VAR_LIB);+}+/** The initrd phase reads the root's state tree: the archived upatches* to restore, the eventlog to continue, the keys to compare. A@@ -1372,8 +1380,7 @@ static int initrd_enter(void)pr_err("initrd: the root file system is not an initramfs");return -ENOTBLK;}- if (strsprintf(src, sizeof(src), "%s%s", sysroot(),- VAR_LIB) < 0)+ if (state_source(src, sizeof(src)) < 0)return -ENAMETOOLONG;if (stat(src, &st) != 0 || !S_ISDIR(st.st_mode)) {pr_err("initrd: %s is not a directory; the root file system "@@ -1451,6 +1458,15 @@ static int run_init(void)ret = seal_label_tree(bootstrap);if (ret)return init_phase_error(ret, "sealing persistent state");+ if (bootstrap) {+ char src[PATH_MAX];++ ret = state_source(src, sizeof(src));+ if (ret >= 0)+ ret = seal_mark_ancestors(src);+ if (ret)+ return init_phase_error(ret, "marking the directories above the state root");+ }ret = eventlog_adopt_pending();if (ret)return init_phase_error(ret, "adopting the pending eventlog");diff --git a/testing/functional/tamperproof/t6150-ancestor-rename.sh b/testing/functional/tamperproof/t6150-ancestor-rename.shnew file mode 100755index 00000000..e581023e--- /dev/null+++ b/testing/functional/tamperproof/t6150-ancestor-rename.sh@@ -0,0 +1,67 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t6150 -- the directories above the state root carry the ancestor mark,+# and root can neither rename one, which would move the sealed tree aside+# for the next boot to label another in its place, nor remove the mark,+# while the directories beside the state root stay root's to rename.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"+MARK=security.bpf.shirudo.ancestor+W=$(mktemp -d)+cleanup() {+ [ ! -e /var/lib.t6150 ] || mv /var/lib.t6150 /var/lib || :+ rm -rf /var/lib/t6150-a /var/lib/t6150-b "$W"+}+trap cleanup EXIT+counter() {+ "$SHIRUDO_BIN" status --json | python3 -c '+import json, sys+print(json.load(sys.stdin)["tamper_attempts"]["counters"][sys.argv[1]])+' "$1"+}+# Run python $1 with the rest as arguments; prints the errno name it hit.+attempt() {+ python3 -c "+import errno, os, sys+try:+ $1+ print('done')+except OSError as e:+ print(errno.errorcode[e.errno])+" "$@" 2>&1+}+[ "$(stat -c %d /var/lib/shirudo)" = "$(stat -c %d /var/lib)" ] ||+ test_skip_all "the state root is a file system of its own"+rename_before=$(counter SealRename) || test_skip_all "no tamper counters"+label_before=$(counter SealLabel)++test_expect_success 'the directories above the state root carry the mark' '+ attempt "os.getxattr(\"/var/lib\", \"$MARK\")" >"$W/out" &&+ grep -qx done "$W/out" &&+ if [ "$(stat -c %d /var)" = "$(stat -c %d /var/lib)" ]; then+ attempt "os.getxattr(\"/var\", \"$MARK\")" >"$W/out" &&+ grep -qx done "$W/out"+ fi+'+test_expect_success 'a rename of one is refused and counted' '+ attempt "os.rename(\"/var/lib\", \"/var/lib.t6150\")" >"$W/out" &&+ grep -qx EACCES "$W/out" &&+ [ -d /var/lib/shirudo ] && [ ! -e /var/lib.t6150 ] &&+ [ "$(counter SealRename)" -gt "$rename_before" ]+'+test_expect_success 'and so is the removal of the mark' '+ attempt "os.removexattr(\"/var/lib\", \"$MARK\")" >"$W/out" &&+ grep -qx EACCES "$W/out" &&+ [ "$(counter SealLabel)" -gt "$label_before" ]+'+test_expect_success 'a directory beside the state root still renames' '+ mkdir /var/lib/t6150-a &&+ mv /var/lib/t6150-a /var/lib/t6150-b &&+ [ -d /var/lib/t6150-b ]+'+test_donediff --git a/util/paths.h b/util/paths.hindex 36a070c0..651e79e0 100644--- a/util/paths.h+++ b/util/paths.h@@ -127,6 +127,7 @@#define XATTR_SEAL "security.bpf.shirudo"#define XATTR_POLICY "security.bpf.shirudo.policy"+#define XATTR_ANCESTOR "security.bpf.shirudo.ancestor"/** Per-upatch payload_sha256 recorded on the pin directory in bpffs.diff --git a/util/seal.c b/util/seal.cindex 2d7e2b9a..93fe78b0 100644--- a/util/seal.c+++ b/util/seal.c@@ -34,6 +34,69 @@ int seal_label(const char *path)return 0;}+static int open_parent(int fd, struct stat *st)+{+ int parent;++ parent = openat(fd, "..", O_RDONLY | O_DIRECTORY | O_CLOEXEC);+ if (parent < 0)+ return -errno;+ if (fstat(parent, st)) {+ int err = -errno;++ close(parent);+ return err;+ }+ return parent;+}++/*+ * A directory whose parent is another file system, or itself, is the+ * root of its own: renaming it does not move the state tree off the+ * device the next boot mounts there.+ */+int seal_mark_ancestors(const char *root)+{+ struct stat st, cur, up;+ int fd, parent, ret;++ fd = open(root, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);+ if (fd < 0)+ return -errno;+ if (fstat(fd, &st)) {+ ret = -errno;+ goto out;+ }+ ret = open_parent(fd, &cur);+ if (ret < 0)+ goto out;+ close(fd);+ fd = ret;+ while (cur.st_dev == st.st_dev) {+ parent = open_parent(fd, &up);+ if (parent < 0) {+ ret = parent;+ goto out;+ }+ if (up.st_dev != cur.st_dev || up.st_ino == cur.st_ino) {+ close(parent);+ break;+ }+ if (fsetxattr(fd, XATTR_ANCESTOR, "1", 1, 0)) {+ ret = -errno;+ close(parent);+ goto out;+ }+ close(fd);+ fd = parent;+ cur = up;+ }+ ret = 0;+out:+ close(fd);+ return ret;+}+/** nftw is callback-only with no user pointer, so the root device is* passed via a file-scope variable -- shirudo init is single-threadeddiff --git a/util/seal.h b/util/seal.hindex 992f5f18..e0f09771 100644--- a/util/seal.h+++ b/util/seal.h@@ -32,6 +32,12 @@ int seal_label(const char *path);*/int seal_label_tree(bool allow_first);+/*+ * Mark the directories above the state root at root, up to the root of+ * its file system, so that the seal refuses to rename them.+ */+int seal_mark_ancestors(const char *root);+/** Creating under a sealed directory. The directory decides: an inode* created under a labelled directory carries that directory's label,diff --git a/util/tamper.c b/util/tamper.cindex 937b5165..85d06fb5 100644--- a/util/tamper.c+++ b/util/tamper.c@@ -65,7 +65,7 @@ static const struct {[TAMPER_SEAL_UNLINK] = { "SealUnlink","unlink of a sealed file or removal of a sealed directory" },[TAMPER_SEAL_RENAME] = { "SealRename",- "rename of or over a sealed file" },+ "rename of or over a sealed file or a directory above the state root" },[TAMPER_SEAL_SETATTR] = { "SealSetattr","truncate, chmod, chown, utimes or chattr of a sealed file" },[TAMPER_SEAL_XATTR] = { "SealXattr",