eBPF-based security platform
seal: read the directory's label in the entry hooks
Daniel Borkmann6a8cd6078653
diff --git a/AGENTS.md b/AGENTS.mdindex 553fc436..ea7ee884 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -311,11 +311,14 @@ one usually still compiles and passes tests.ACL, requires authority when the inode or the directory the operationchanges carries a mark; scratch allocation failure also requiresauthority. The seal marks what it labels at creation, what shirudo- opens and what anyone opens for writing or as a directory, and the- marks alone guard ACL and inode flag changes. Writers hold an open- parent directory to retain that mark. Mark eviction and foreign- link/rename entries remain limitations; existing writable descriptors- and mappings are not revoked.+ opens and what anyone opens for writing or as a directory, and+ refreshes a directory's mark in every entry hook from the label it+ reads there (carried kernel patches 0004 and 0005 make those hooks+ sleepable and `dentry->d_parent` trusted), so that+ `inode_init_security`, which has no dentry, labels the new inode from+ it: the marks are a cache, never the decision. A sealed inode cannot+ be hard-linked by a task without authority. Existing writable+ descriptors and mappings are not revoked.- **Single shirudo instance per host.** Keyring name, private bpffs,`/var/lib/shirudo`, the `security.bpf.shirudo` xattr namespace andthe NV index are single-tenant by design.diff --git a/Documentation/concepts/background/xattrs.rst b/Documentation/concepts/background/xattrs.rstindex 3d906872..9213d882 100644--- a/Documentation/concepts/background/xattrs.rst+++ b/Documentation/concepts/background/xattrs.rst@@ -228,15 +228,16 @@ The seal pairs the label with inode storage in the pattern above. Whenone of its sleepable programs reads a label, on every open of adirectory, on an open of a file for writing and after an xattr change,it records the label for that inode in the ``seal_dir_marks`` map, or-drops the record when there is no label. The hooks that cannot read the-label use the record. ``lsm/inode_init_security`` gives a new inode the-label of its directory's record and records a new directory in turn, so-a subtree created in one go needs no reopen. ``lsm/inode_copy_up``-refuses an overlay copy-up of a recorded inode (``SealCopyUp``). The-hooks that add an entry to a directory, by creation, link or rename,-require runtime authority when the directory is recorded-(``SealEntry``). shirudo's writers create files relative to an open-descriptor of the parent directory, which keeps its record in memory.+drops the record when there is no label. The hooks that add an entry to+a directory, by creation, link or rename, read the directory's label+from the new entry's parent dentry, require runtime authority when it+is labeled (``SealEntry``) and refresh the record on the way.+``lsm/inode_init_security``, which has no dentry to read from, gives a+new inode the label of its directory's record, refreshed a moment+before by the entry hook of the same operation, and records a new+directory in turn, so a subtree created in one go needs no reopen.+``lsm.s/inode_copy_up`` refuses an overlay copy-up of a labeled inode+(``SealCopyUp``).Like every map of shirudo's, ``seal_dir_marks`` is refused to taskswithout runtime authority, so userspace can neither read nor change arecord. See :doc:`/internals/seal`.@@ -261,8 +262,7 @@ file system's own permission checks then decide. The threat model liststhis under threats to resolve: :doc:`/security/threat-model`.The seal's records in inode storage are lost when an inode leaves the-inode cache. Until a sleepable program reads the label again, a file-created in that directory gets no label at creation, and creation, links-and renames into it are not checked. :doc:`/security/threat-model`-lists this as residual risk R15. A file system without ``security.`` xattrs cannot carry+inode cache, and nothing depends on them: the hooks read the label, and+the record a creation labels from is refreshed by the entry hook that+precedes it. A file system without ``security.`` xattrs cannot carrythe label at all.diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 1b12f99e..63b67c3d 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -378,8 +378,10 @@ signed config image.- A load stopped before its extend is counted, reported by statusand undone by the next upatch command* - ``t6144-returned-archive``- - An archive root brings back after its unload is reported, not- restored by init and removed by the next upatch command+ - The archive directory with its mark evicted takes nothing and+ lets no labeled archive out; an archive without a record of its+ load is reported, not restored by init and removed by the next+ upatch command``bpm`` (7)~~~~~~~~~~~diff --git a/Documentation/internals/enforcement.rst b/Documentation/internals/enforcement.rstindex 73336360..d5b9a40f 100644--- a/Documentation/internals/enforcement.rst+++ b/Documentation/internals/enforcement.rst@@ -501,13 +501,13 @@ when the record it needs is missing.- ``EACCES``- ``SealUnlink``, ``SealSetattr``, ``SealXattr``, ``SealLabel``- See the label verdict below.- * - ``lsm/inode_set_acl``, ``lsm/inode_remove_acl``,- ``lsm/inode_file_setattr`` (``seal``)+ * - ``lsm.s/inode_set_acl``, ``lsm.s/inode_remove_acl``,+ ``lsm.s/inode_file_setattr`` (``seal``)- A POSIX ACL change or a change of the inode flags (``chattr``) on- a marked inode to a task without authority.+ a labeled inode to a task without authority.- ``EACCES``- ``SealXattr``, ``SealSetattr``- - Open: an inode without a cached mark is not checked.+ - 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@@ -517,30 +517,33 @@ when the record it needs is missing.- ``SealLabel``, ``SealXattr``- See the label verdict below.* - ``lsm.s/inode_rename`` (``seal``)- - A labeled source, a marked destination directory or a labeled+ - A labeled source, a labeled destination directory or a labeledinode it would replace, to a task without authority.- ``EACCES``- ``SealRename``, ``SealEntry``- See the label verdict below.- * - ``lsm/inode_create``, ``lsm/inode_mkdir``, ``lsm/inode_symlink``,- ``lsm/inode_mknod``, ``lsm/inode_link`` (``seal``)- - A new entry in a marked directory to a task without authority.- The source inode of a hard link is not checked.+ * - ``lsm.s/inode_create``, ``lsm.s/inode_mkdir``,+ ``lsm.s/inode_symlink``, ``lsm.s/inode_mknod``,+ ``lsm.s/inode_link`` (``seal``)+ - A new entry in a labeled directory to a task without authority,+ read from the entry's parent dentry, and a hard link of a labeled+ inode anywhere. Each refreshes the directory's mark.- ``EACCES``- ``SealEntry``- - Open: a directory without a cached mark is not checked.+ - See the label verdict below.* - ``lsm/inode_init_security`` (``seal``)- Labels a new inode under a marked directory, inside the creation,and marks the new inode. Refuses the creation when it cannot addthe label.- ``EACCES``- Uncounted- - Open without a parent mark; closed when the label cannot be added.- * - ``lsm/inode_copy_up`` (``seal``)- - The overlay copy-up of a marked inode.+ - Cannot read: the entry hook of the same operation refreshed the+ parent's mark from its label just before.+ * - ``lsm.s/inode_copy_up`` (``seal``)+ - The overlay copy-up of a labeled inode, by anyone.- ``EPERM``- ``SealCopyUp``- - Open: an inode without a cached mark is not checked.+ - See the label verdict below.* - ``lsm.s/inode_post_setxattr``, ``lsm.s/inode_post_removexattr``(``seal``)- Refresh a directory's mark from its label after the change.diff --git a/Documentation/internals/seal.rst b/Documentation/internals/seal.rstindex a71f2610..346c9bd3 100644--- a/Documentation/internals/seal.rst+++ b/Documentation/internals/seal.rst@@ -243,23 +243,16 @@ LimitsThese are properties of the mechanism as it is. The:doc:`/security/threat-model` states their consequences.-Mark eviction- A mark is a cache. When the kernel evicts an inode, under memory- pressure or through ``vm.drop_caches``, its mark goes with it until- the next open that reads the label. In that window, creation, links- and renames into a directory are not checked,- ``inode_init_security`` adds no label, an ACL or inode flag change- is not checked, and a label read that fails for the caller counts- as unlabeled.--Hard links to labeled inodes- ``inode_link`` checks the destination directory's mark, not the- source inode. Root can hard-link a labeled file into an unsealed- directory on the same file system. shirudo replaces state by writing- a new file and renaming it over the old one, so the old inode stays- reachable through that link. While a directory's mark is absent, such- a link can be made inside a sealed directory, where it carries a- valid label.+Marks are a cache+ A mark is a cache of a label the seal read. When the kernel evicts+ an inode, under memory pressure or through ``vm.drop_caches``, its+ mark goes with it, and nothing opens: every hook that decides reads+ the label, and the entry hooks read the directory's from the new+ entry's parent dentry and refresh its mark on the way, which+ ``inode_init_security``, the one hook without a dentry, labels the+ new inode from in the same operation. On a kernel without the+ carried patches 0004 and 0005, the seal's entry hooks do not load+ and ``init`` fails.Label reads on an upstream kernelWithout the carried xattr patch, the kfuncs check that the calling@@ -269,13 +262,6 @@ Label reads on an upstream kernelwithout a mark look unlabeled to the seal; a marked one is refused,and the same failure on an open leaves the mark.-ACLs and inode flags- ``inode_set_acl``, ``inode_remove_acl`` and ``inode_file_setattr``- cannot sleep, so a POSIX ACL change and ``chattr`` on a marked inode- are refused by the mark alone, and an inode without a mark is not- checked. The removal of an empty labeled directory reads the label- as an unlink does.-Bind mountsNo mount guard covers the state file system, and shirudo's opensfollow mount points. A bind mount over a sealed path makes shirudodiff --git a/Documentation/security/security-model.rst b/Documentation/security/security-model.rstindex dffbf48a..aa99cf2b 100644--- a/Documentation/security/security-model.rst+++ b/Documentation/security/security-model.rst@@ -42,8 +42,8 @@ What shirudo provides* - Only shirudo changes its state- The seal labels ``/var/lib/shirudo`` and checks every change toa labeled inode; keyrings and authorized processes are guarded.- - Marks can be evicted; offline writes bypass it.- - :ref:`T3 <tm-t3>`, R15, R19+ - Offline writes bypass it.+ - :ref:`T3 <tm-t3>`, R19* - Every change is measured- Loads, unloads, revocations and tree operations extend a TPM NVregister whose writes ``lsm/tpm_command`` keeps to shirudo.diff --git a/Documentation/security/tamper-counters.rst b/Documentation/security/tamper-counters.rstindex 773e0564..926bc2fd 100644--- a/Documentation/security/tamper-counters.rst+++ b/Documentation/security/tamper-counters.rst@@ -141,14 +141,12 @@ counter.- ``EACCES``* - ``SealSetattr``- truncate, chmod, chown, utimes or chattr of a sealed file- - ``lsm.s/inode_setattr``; ``lsm/inode_file_setattr`` on a marked- inode+ - ``lsm.s/inode_setattr``, ``lsm.s/inode_file_setattr``- ``EACCES``* - ``SealXattr``- xattr or ACL change on a sealed file- - ``lsm.s/inode_setxattr``, ``lsm.s/inode_removexattr``;- ``lsm/inode_set_acl`` and ``lsm/inode_remove_acl`` on a marked- inode+ - ``lsm.s/inode_setxattr``, ``lsm.s/inode_removexattr``,+ ``lsm.s/inode_set_acl``, ``lsm.s/inode_remove_acl``- ``EACCES``* - ``SealLabel``- seal or policy label changed by another executable@@ -175,10 +173,11 @@ counter.``lsm/bprm_check_security`` in the ``bpf2`` object- ``EACCES``, or ``EPERM`` from the ``bpf2`` hook* - ``SealEntry``- - entry created, linked or renamed into a sealed directory- - ``lsm/inode_create``, ``lsm/inode_mkdir``,- ``lsm/inode_symlink``, ``lsm/inode_mknod``, ``lsm/inode_link`` in- the seal object, and ``lsm.s/inode_rename`` for the destination+ - entry into a sealed directory, or a sealed inode linked+ - ``lsm.s/inode_create``, ``lsm.s/inode_mkdir``,+ ``lsm.s/inode_symlink``, ``lsm.s/inode_mknod`` and+ ``lsm.s/inode_link`` in the seal object, which read the+ directory's label, and ``lsm.s/inode_rename`` for the destination- ``EACCES``* - ``PerfAttach``- program attached to a perf event by another executable@@ -192,7 +191,7 @@ counter.- ``EPERM``* - ``SealCopyUp``- overlay copy-up of a sealed inode- - ``lsm/inode_copy_up``+ - ``lsm.s/inode_copy_up``- ``EPERM``* - ``Kexec``- kexec image or initramfs loaddiff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex b5e2d84a..bdfea6b6 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -679,9 +679,10 @@ read. Where the read fails that way, the mark of the inode, or of thedirectory the operation changes, decides, and the operation needsauthority when either is marked (``t6111``). The seal marks every inodeit labels at creation, every file shirudo opens, every file anyone-opens for writing and every directory anyone opens; on a kernel-without the patch, a file shirudo has not touched since it came into-memory has no mark (R15). The labels+opens for writing, every directory anyone opens and the directory of+every new entry, from the label the entry hook reads; the marks are a+cache that only ``lsm/inode_init_security``, which has no dentry to+read from, decides by. The labelsthemselves are refused to such a caller whatever the read said: roottakes a file system uid that cannot read the inode with ``setfsuid()``and keeps the ``CAP_SYS_ADMIN`` that a ``security.*`` xattr change@@ -711,17 +712,20 @@ hook. Authors are expected to read these values; the verifier does notprevent signed programs from changing them. upatches without ``SHPL``receive no binding to these maps.-The seal keeps a mark in inode storage on every labeled directory it-has seen since the inode came into memory. A new inode under a marked-directory receives the label inside the transaction that creates it-(``lsm/inode_init_security``), and a marked directory takes new entries,-by create, mkdir, symlink, mknod, hard link or rename, only from-authorized tasks (``SealEntry``). shirudo's writers hold the parent-directory open while they create, which keeps the mark in memory. An-overlay copy-up of a marked inode is refused (``SealCopyUp``), so a-write through an overlay root mounts over sealed state fails instead of-landing in root's upper directory. Where the mark is absent, R15-applies.+A labeled directory takes new entries, by create, mkdir, symlink,+mknod, hard link or rename, only from authorized tasks (``SealEntry``):+each entry hook reads the directory's label from the new entry's parent+dentry, which the carried patches 0004 and 0005 (A3) make possible by+letting a sleepable program attach to ``inode_mkdir`` and ``inode_link``+and by trusting ``dentry->d_parent``, and refreshes the directory's+mark on the way, from which ``lsm/inode_init_security`` gives the new+inode its label inside the transaction that creates it. Evicting the+inode cache therefore opens nothing (``t6118``, ``t6144``). A labeled+inode is hard-linked nowhere by a task without authority, so its label+cannot be kept alive beside the state it left. An overlay copy-up of a+labeled inode is refused (``SealCopyUp``), so a write through an+overlay root mounts over sealed state fails instead of landing in+root's upper directory.shirudo opens sealed state without following symbolic links and takesno entry without the label for its own: ``init`` ignores such an entry@@ -788,11 +792,12 @@ An authorized process is protected against being taken over::Enforced at: ``lsm.s/file_open``, ``lsm.s/inode_unlink``,``lsm.s/inode_rmdir``, ``lsm.s/inode_rename``,``lsm.s/inode_setattr``, ``lsm.s/inode_setxattr``,- ``lsm.s/inode_removexattr``, ``lsm/inode_set_acl``,- ``lsm/inode_remove_acl``, ``lsm/inode_file_setattr``,- ``lsm/inode_create``, ``lsm/inode_mkdir``, ``lsm/inode_symlink``,- ``lsm/inode_mknod``, ``lsm/inode_link``, ``lsm/inode_init_security``,- ``lsm/inode_copy_up``, ``lsm/key_permission``,+ ``lsm.s/inode_removexattr``, ``lsm.s/inode_set_acl``,+ ``lsm.s/inode_remove_acl``, ``lsm.s/inode_file_setattr``,+ ``lsm.s/inode_create``, ``lsm.s/inode_mkdir``,+ ``lsm.s/inode_symlink``, ``lsm.s/inode_mknod``, ``lsm.s/inode_link``,+ ``lsm/inode_init_security``, ``lsm.s/inode_copy_up``,+ ``lsm/key_permission``,``lsm.s/bprm_check_security``, ``lsm.s/bprm_committed_creds``,``lsm/task_alloc``, ``lsm/bprm_check_security``,``lsm/ptrace_access_check``, ``lsm/perf_event_alloc``,@@ -808,7 +813,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``-:Residual: R2, R15, R19, R22+:Residual: R2, R19, R22T4 Substituting artifacts~~~~~~~~~~~~~~~~~~~~~~~~~@@ -1424,29 +1429,6 @@ R14 Configs roll backDetection: ``--expect-config-root`` with ``--expect-config-pcr``.Mitigation: D2.-R15 Marks can be evicted- The label at creation, the entry checks, the ACL and inode flag- checks and the verdict on a label read that fails for the caller- follow a mark that the seal keeps on an inode in memory. Root can- evict the inode, by dropping caches for instance, whenever shirudo- does not hold it open, and a file shirudo has not opened since it- came into memory has no mark. While a directory's mark is absent,- root can create entries in it and hard-link an already labeled inode- into it; a hard link is the same inode and carries the label. shirudo- replaces state by rename, so root can keep earlier labeled versions- alive through hard links in an unsealed directory on the same file- system and bring them back. An unlabeled entry stays inert: shirudo- neither follows nor restores it. A returned archive is not restored- either: ``init`` restores only an archive whose load the eventlog- records without a later unload, ``status`` reports it- (``UpatchesMeasured``) and the next upatch command removes it- (``t6144``). A returned labeled entry elsewhere in the state is- taken for shirudo's own. While a file's mark is absent, root can change- its ACL and inode flags, and on a kernel without the carried xattr- patch (A3) a label read it made fail counts as unlabeled (T3).- Detection: a returned archive shows in ``status``; other returned- entries only through signatures and measurement. Mitigation: D6.-R16 Any artifact ever signed loads at any timeThere is no version floor, no expiry and no per-targetauthorization. Neither shirudo nor the kernel checks a certificate's@@ -1769,13 +1751,6 @@ D5 In-kernel detachwhich :src:`bpf/unload.c` holds a placeholder for, once ``bpf_sys_bpf()``supports it (R1).-D6 A sleepable hook at dentry instantiation- A ``d_instantiate`` hook in which the BPF LSM can read the label,- marking a directory the moment its dentry is bound and closing the- eviction gap (R15). The `directory-entry investigation- <https://github.com/shirudo-dev/shirudo/issues/13>`__ records the- analysis.-D7 Pins without the mount treeKeep the enforcement links alive without depending on the bpffsmount (R3), tracked in `issue 22diff --git a/bpf/seal.c b/bpf/seal.cindex a8dd29cf..21a8d2f5 100644--- a/bpf/seal.c+++ b/bpf/seal.c@@ -322,18 +322,26 @@ int BPF_PROG(shirudo_seal_file_open, struct file *file, int ret)* 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.+ * state is never copied up, by anyone.*/-SEC("lsm/inode_copy_up")+SEC("lsm.s/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;+ struct task_struct *task = bpf_get_current_task_btf();+ struct seal_scratch *s = seal_scratch_get(task);+ struct bpf_dynptr tdp;+ int tlen;if (ret)return ret;- if (!inode || !bpf_inode_storage_get(&seal_dir_marks, inode, NULL, 0))+ if (!s)+ return shirudo_tamper_deny(TAMPER_SEAL_COPY_UP, -EPERM);+ bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);+ tlen = bpf_get_dentry_xattr(src, XATTR_SEAL, &tdp);+ if (tlen == -ENODATA || tlen == -EOPNOTSUPP)+ return 0;+ if ((tlen == -EACCES || tlen == -EPERM) && !seal_marked(src->d_inode))return 0;return shirudo_tamper_deny(TAMPER_SEAL_COPY_UP, -EPERM);}@@ -374,12 +382,14 @@ void BPF_PROG(shirudo_seal_inode_post_removexattr, struct dentry *dentry,* parent directory is marked, with the parent's label, and mark the new* inode in turn: a subtree created in one go needs no reopen, and a* file carries its mark from the start.- * 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.+ * The hook cannot sleep and has no dentry, so the label comes from the+ * mark, which the entry hook of the same operation refreshed from the+ * directory's label just before. 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,@@ -430,52 +440,72 @@ int BPF_PROG(shirudo_seal_inode_unlink, struct inode *dir,* A sealed directory takes new entries from shirudo alone. The label a* creation inherits stops writes to the new inode, but a rename or a* link brings an inode in with its contents, and a symbolic link sends- * whoever follows it elsewhere. The mark stands in for the label these- * hooks cannot read.+ * whoever follows it elsewhere. The directory's label is read from the+ * new entry's parent dentry, and its mark refreshed on the way, which+ * inode_init_security labels the new inode from a moment later.*/-static __always_inline int seal_dir_entry(struct inode *dir)+static __always_inline int seal_parent(struct dentry *dentry,+ enum tamper_point point){- if (!bpf_inode_storage_get(&seal_dir_marks, dir, NULL, 0))- return 0;- return seal_caller(TAMPER_SEAL_ENTRY);+ struct task_struct *task = bpf_get_current_task_btf();+ struct seal_scratch *s = seal_scratch_get(task);+ struct dentry *parent = dentry->d_parent;+ struct bpf_dynptr tdp;+ int tlen;++ if (!s || !parent)+ return seal_caller(point);+ bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);+ tlen = bpf_get_dentry_xattr(parent, XATTR_SEAL, &tdp);+ if (parent->d_inode)+ seal_mark(parent->d_inode, s->target, tlen);+ return seal_verdict(tlen, NULL, parent->d_inode, point);}-SEC("lsm/inode_create")+SEC("lsm.s/inode_create")int BPF_PROG(shirudo_seal_inode_create, const struct mnt_idmap *idmap,struct inode *dir, struct dentry *dentry, umode_t mode, int ret){- return ret ? ret : seal_dir_entry(dir);+ return ret ? ret : seal_parent(dentry, TAMPER_SEAL_ENTRY);}-SEC("lsm/inode_mkdir")+SEC("lsm.s/inode_mkdir")int BPF_PROG(shirudo_seal_inode_mkdir, const struct mnt_idmap *idmap,struct inode *dir, struct dentry *dentry, umode_t mode, int ret){- return ret ? ret : seal_dir_entry(dir);+ return ret ? ret : seal_parent(dentry, TAMPER_SEAL_ENTRY);}-SEC("lsm/inode_symlink")+SEC("lsm.s/inode_symlink")int BPF_PROG(shirudo_seal_inode_symlink, const struct mnt_idmap *idmap,struct inode *dir, struct dentry *dentry, const char *old_name,int ret){- return ret ? ret : seal_dir_entry(dir);+ return ret ? ret : seal_parent(dentry, TAMPER_SEAL_ENTRY);}-SEC("lsm/inode_mknod")+SEC("lsm.s/inode_mknod")int BPF_PROG(shirudo_seal_inode_mknod, const struct mnt_idmap *idmap,struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev,int ret){- return ret ? ret : seal_dir_entry(dir);+ return ret ? ret : seal_parent(dentry, TAMPER_SEAL_ENTRY);}-SEC("lsm/inode_link")+/* A sealed inode is linked nowhere by anyone else: it keeps its label. */+SEC("lsm.s/inode_link")int BPF_PROG(shirudo_seal_inode_link, const struct mnt_idmap *idmap,struct dentry *old_dentry, struct inode *dir,struct dentry *new_dentry, int ret){- return ret ? ret : seal_dir_entry(dir);+ int err;++ if (ret)+ return ret;+ err = seal_dentry(NULL, old_dentry, TAMPER_SEAL_ENTRY);+ if (err)+ return err;+ return seal_parent(new_dentry, TAMPER_SEAL_ENTRY);}/* Protect the source, the destination directory and an existing target. */@@ -492,7 +522,7 @@ int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,err = seal_dentry(old_dir, old_dentry, TAMPER_SEAL_RENAME);if (err)return err;- err = seal_dir_entry(new_dir);+ err = seal_parent(new_dentry, TAMPER_SEAL_ENTRY);if (err)return err;if (!target)@@ -537,38 +567,30 @@ int BPF_PROG(shirudo_seal_inode_removexattr, struct mnt_idmap *idmap,}/*- * ACL changes and the inode flags of chattr come through hooks that- * cannot sleep, so the mark stands in for the label. An ACL that grants- * another id a write without a read would otherwise open a marked inode- * to that id, whose label read then fails (seal_verdict).+ * An ACL that grants another id a write without a read would open a+ * sealed inode to that id, whose label read then fails (seal_verdict),+ * and the inode flags of chattr make a file immutable or append-only.*/-SEC("lsm/inode_set_acl")+SEC("lsm.s/inode_set_acl")int BPF_PROG(shirudo_seal_inode_set_acl, const struct mnt_idmap *idmap,struct dentry *dentry, const char *acl_name,struct posix_acl *kacl, int ret){- if (ret)- return ret;- return seal_marked(dentry->d_inode) ? seal_caller(TAMPER_SEAL_XATTR) : 0;+ return ret ? ret : seal_dentry(NULL, dentry, TAMPER_SEAL_XATTR);}-SEC("lsm/inode_remove_acl")+SEC("lsm.s/inode_remove_acl")int BPF_PROG(shirudo_seal_inode_remove_acl, const struct mnt_idmap *idmap,struct dentry *dentry, const char *acl_name, int ret){- if (ret)- return ret;- return seal_marked(dentry->d_inode) ? seal_caller(TAMPER_SEAL_XATTR) : 0;+ return ret ? ret : seal_dentry(NULL, dentry, TAMPER_SEAL_XATTR);}-SEC("lsm/inode_file_setattr")+SEC("lsm.s/inode_file_setattr")int BPF_PROG(shirudo_seal_inode_file_setattr, struct dentry *dentry,struct file_kattr *fa, int ret){- if (ret)- return ret;- return seal_marked(dentry->d_inode) ?- seal_caller(TAMPER_SEAL_SETATTR) : 0;+ return ret ? ret : seal_dentry(NULL, dentry, TAMPER_SEAL_SETATTR);}SEC("lsm.s/inode_rmdir")diff --git a/testing/functional/tamperproof/t6115-archive-restore.sh b/testing/functional/tamperproof/t6115-archive-restore.shindex 7dc3faab..075d296e 100755--- a/testing/functional/tamperproof/t6115-archive-restore.sh+++ b/testing/functional/tamperproof/t6115-archive-restore.sh@@ -6,9 +6,9 @@# bound. Rename brings an inode into a sealed directory without creating# one, so root used to place any operator-signed .upatch there and a# re-run of init restored it, attached and pinned, without a register-# event. The seal now refuses the rename; one that slips in while the-# directory's mark is evicted carries no label, and init leaves it alone-# instead of labelling, following or restoring it. The restore also+# event. The seal refuses the rename by the directory's label, which it+# reads in the hook, so evicting the directory's mark opens nothing, and+# a re-run of init restores nothing new. The restore also# must not take a state directory hidden behind a mount that carries no# eventlog for the real one, nor a root file system for the initramfs# because root wrote /etc/initrd-release.@@ -54,8 +54,8 @@ counter() {python3 -c "import json,sys; print(json.load(sys.stdin)[\"tamper_attempts\"][\"counters\"][\"SealEntry\"])"}-# Rename while the directory's mark is evicted, the gap the seal keeps-# (residual risk 15); the entry comes in unlabelled.+# Rename while the directory's mark is evicted: the seal reads the+# label, so every try is refused.plant() {i=0while [ "$i" -lt 8 ]; do@@ -82,21 +82,14 @@ test_expect_success 'root cannot rename a signed artifact into the archive' '[ "$(counter)" -gt "$before" ]'-test_expect_success 'one that slips in while the mark is evicted is ignored by init' '- plant "$STAGE/$NAME.upatch" "$ARCHIVE/$NAME.upatch" &&- ! labelled "$ARCHIVE/$NAME.upatch" &&- "$SHIRUDO_BIN" init >/dev/null 2>"$STAGE/init.err" &&- grep -q "$NAME.upatch is not shirudo.s own (foreign entry); ignored" \- "$STAGE/init.err" &&- [ ! -e "$PIN" ] &&- ! labelled "$ARCHIVE/$NAME.upatch" &&- [ "$(chain)" = "$CHAIN" ]-'--test_expect_success 'the foreign entry is root'"'"'s to remove' '- rm "$ARCHIVE/$NAME.upatch" &&+test_expect_success 'nothing slips in while the directory mark is evicted' '+ before=$(counter) &&+ ! plant "$STAGE/$NAME.upatch" "$ARCHIVE/$NAME.upatch" &&[ ! -e "$ARCHIVE/$NAME.upatch" ] &&+ [ -s "$STAGE/$NAME.upatch" ] &&+ [ "$(counter)" -gt "$before" ] &&"$SHIRUDO_BIN" init >/dev/null 2>&1 &&+ [ ! -e "$PIN" ] &&[ "$(chain)" = "$CHAIN" ]'diff --git a/testing/functional/tamperproof/t6118-foreign-entries.sh b/testing/functional/tamperproof/t6118-foreign-entries.shindex e094a44b..f9aa7bcf 100755--- a/testing/functional/tamperproof/t6118-foreign-entries.sh+++ b/testing/functional/tamperproof/t6118-foreign-entries.sh@@ -5,10 +5,10 @@# t6118 -- entries root brings into sealed state. A sealed directory# takes new entries from shirudo alone: a file, directory, symbolic link# or device root creates there is refused, and so is a rename or hard-# link, which would bring an inode in with its contents. One that slips-# in while the directory's mark is evicted carries no label: shirudo-# never follows it, adopts it or reads it as its own, and a re-run of-# init completes with it in place. Non-destructive.+# link, which would bring an inode in with its contents. The seal reads+# the directory's label in the hook, so evicting the directory's mark+# opens nothing either, and a re-run of init completes unchanged.+# Non-destructive.cd "$(dirname "$0")/../..". ./integration-lib.sh@@ -53,8 +53,8 @@ except OSError:sys.exit(1)" "$1"}-# Rename while the directory's mark is evicted, the gap the seal keeps-# (residual risk 15); the entry comes in unlabelled.+# Rename while the directory's mark is evicted: the seal reads the+# label, so every try is refused.plant() {i=0while [ "$i" -lt 8 ]; do@@ -88,20 +88,18 @@ test_expect_success 'root cannot rename or hard-link content into sealed state'[ "$(counter)" -ge $((before + 2)) ]'-# The archive entry is a link to a signed artifact root keeps elsewhere:-# init must neither follow it nor label it.-test_expect_success 'a link that slipped in while the mark was evicted is not followed' '+# A link to a signed artifact root keeps elsewhere cannot slip in even+# while the directory's mark is evicted.+test_expect_success 'a link cannot slip in while the directory mark is evicted' '+ before=$(counter) &&mkdir -p "$STAGE" && cp "$LOAD" "$STAGE/$NAME.upatch" &&ln -s "$STAGE/$NAME.upatch" "$STAGE/t6118-link.upatch" &&- plant "$STAGE/t6118-link.upatch" "$ARCHIVE/t6118-link.upatch" &&- [ -L "$ARCHIVE/t6118-link.upatch" ] &&- "$SHIRUDO_BIN" init >/dev/null 2>"$STAGE/init.err" &&- grep -q "t6118-link.upatch is not shirudo.s own (foreign entry); ignored" \- "$STAGE/init.err" &&+ ! plant "$STAGE/t6118-link.upatch" "$ARCHIVE/t6118-link.upatch" &&+ [ ! -L "$ARCHIVE/t6118-link.upatch" ] &&+ [ "$(counter)" -gt "$before" ] &&+ "$SHIRUDO_BIN" init >/dev/null 2>&1 &&[ ! -e "$PIN" ] &&- ! labelled "$ARCHIVE/t6118-link.upatch" &&- [ "$(chain)" = "$CHAIN" ] &&- rm "$ARCHIVE/t6118-link.upatch"+ [ "$(chain)" = "$CHAIN" ]'test_expect_success 'root cannot plant a link where shirudo stages' 'diff --git a/testing/functional/tamperproof/t6144-returned-archive.sh b/testing/functional/tamperproof/t6144-returned-archive.shindex d53f2a3e..d398ef4a 100755--- a/testing/functional/tamperproof/t6144-returned-archive.sh+++ b/testing/functional/tamperproof/t6144-returned-archive.sh@@ -2,11 +2,15 @@# SPDX-License-Identifier: BSD-2-Clause# Copyright (c) 2026 Authors of shirudo#-# t6144 -- an archive root brings back is not shirudo's to restore. A-# labeled archive hard-linked out before its unload and back into the-# archive directory while the directory's mark is evicted (R15) carries-# the label, but the eventlog records its unload: status reports it,-# init does not restore it and the next upatch command removes it.+# t6144 -- the archive directory after its mark is evicted, and an+# archive the eventlog does not cover. The seal reads the directory's+# label in the entry hooks, so evicting the cache opens nothing: a hard+# link of a labeled archive out of the directory, and a file, a link or+# a rename into it, are refused with the mark gone, while shirudo's own+# load still labels what it archives. An archive without a record of+# its load, which a load stopped between its archive copy and its+# extend leaves, is reported by status, ignored by init and removed by+# the next upatch command.cd "$(dirname "$0")/../..". ./integration-lib.sh@@ -19,9 +23,10 @@ LOAD="$FIXTURES/$NAME.upatch"UNLOAD="$FIXTURES/$NAME.unload.upatch"PINDIR="/run/shirudo/bpffs/upatches/$NAME"OTHERDIR="/run/shirudo/bpffs/upatches/$OTHER"-ARCHIVE="/var/lib/shirudo/upatches/$NAME.upatch"+UPDIR=/var/lib/shirudo/upatches+ARCHIVE="$UPDIR/$NAME.upatch"+EVENTLOG=/var/lib/shirudo/eventlogKEEPDIR=/var/lib/t6144-KEEP="$KEEPDIR/keep.upatch"W=$(mktemp -d)cleanup() {[ ! -e "$OTHERDIR" ] ||@@ -29,8 +34,6 @@ cleanup() {>/dev/null 2>&1 || :[ ! -e "$PINDIR" ] ||"$SHIRUDO_BIN" upatch unload "$UNLOAD" >/dev/null 2>&1 || :- # The kept link carries the label, so root cannot unlink it: the- # directory stays behind with it.rm -rf "$KEEPDIR" "$W" 2>/dev/null || :}trap cleanup EXIT@@ -48,10 +51,46 @@ done[ ! -e "$ARCHIVE" ] || test_skip_all "$NAME is archived already"mkdir -p "$KEEPDIR"+counter() {+ "$SHIRUDO_BIN" status --json | python3 -c '+import json, sys+print(json.load(sys.stdin)["tamper_attempts"]["counters"][sys.argv[1]])+' "$1"+}label_len() {python3 -c 'import os, sysprint(len(os.getxattr(sys.argv[1], "security.bpf.shirudo")))' "$1"}+evict() {+ sync && echo 2 >/proc/sys/vm/drop_caches+}++# Start a load and stop it once its archive copy appears, before the+# register moves. A load that got as far as its record, which the+# eventlog shows as one more line, is unloaded and tried again. The+# poll is builtins only and bounded, as a load that fails stays a+# zombie until it is waited for.+stopped_load() {+ for try in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do+ lines=$(wc -l <"$EVENTLOG")+ "$SHIRUDO_BIN" upatch load "$LOAD" >/dev/null 2>&1 &+ pid=$!+ i=0+ while kill -0 "$pid" 2>/dev/null && [ ! -e "$ARCHIVE" ] &&+ [ "$i" -lt 2000000 ]; do+ i=$((i + 1))+ done+ kill -9 "$pid" 2>/dev/null || :+ wait "$pid" 2>/dev/null || :+ if [ -e "$ARCHIVE" ] &&+ [ "$(wc -l <"$EVENTLOG")" = "$lines" ]; then+ return 0+ fi+ [ ! -e "$PINDIR" ] ||+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" >/dev/null 2>&1 || :+ done+ return 1+}# The verdict and the UpatchesMeasured condition of a status document.measured_condition() {@@ -63,22 +102,39 @@ print(d["status"], c[0]["status"] if c else "-", c[0]["detail"] if c else "-")PY}-test_expect_success 'a labeled archive can be hard-linked out of the archive directory' '+test_expect_success 'a labeled archive cannot be hard-linked out of the archive directory' '+ before=$(counter SealEntry) &&"$SHIRUDO_BIN" upatch load "$LOAD" >/dev/null 2>&1 &&- [ -s "$ARCHIVE" ] &&- ln "$ARCHIVE" "$KEEP" &&- [ "$(label_len "$KEEP")" = 64 ]+ [ "$(label_len "$ARCHIVE")" = 64 ] &&+ ! ln "$ARCHIVE" "$KEEPDIR/keep.upatch" 2>/dev/null &&+ [ ! -e "$KEEPDIR/keep.upatch" ] &&+ [ "$(counter SealEntry)" -ge $((before + 1)) ]+'+test_expect_success 'nothing enters the archive directory while its mark is evicted' '+ before=$(counter SealEntry) &&+ cp "$LOAD" "$KEEPDIR/plain.upatch" &&+ evict && ! touch "$UPDIR/t6144.upatch" 2>/dev/null &&+ evict && ! ln "$KEEPDIR/plain.upatch" "$UPDIR/t6144-link.upatch" 2>/dev/null &&+ evict && ! mv "$KEEPDIR/plain.upatch" "$UPDIR/t6144-moved.upatch" 2>/dev/null &&+ [ ! -e "$UPDIR/t6144.upatch" ] && [ ! -e "$UPDIR/t6144-link.upatch" ] &&+ [ ! -e "$UPDIR/t6144-moved.upatch" ] && [ -s "$KEEPDIR/plain.upatch" ] &&+ [ "$(counter SealEntry)" -ge $((before + 3)) ]'-test_expect_success 'after its unload the archive returns through the link once the mark is evicted' '+test_expect_success 'shirudo still labels what it archives after the eviction' '+ evict &&+ "$SHIRUDO_BIN" upatch load "$FIXTURES/$OTHER.upatch" >/dev/null 2>&1 &&+ [ "$(label_len "$UPDIR/$OTHER.upatch")" = 64 ] &&+ "$SHIRUDO_BIN" upatch unload "$FIXTURES/$OTHER.unload.upatch" \+ >/dev/null 2>&1 &&"$SHIRUDO_BIN" upatch unload "$UNLOAD" >/dev/null 2>&1 &&- [ ! -e "$ARCHIVE" ] &&- ! ln "$KEEP" "$ARCHIVE" 2>/dev/null &&- sync &&- echo 2 >/proc/sys/vm/drop_caches &&- ln "$KEEP" "$ARCHIVE" &&- [ "$(label_len "$ARCHIVE")" = 64 ]+ [ ! -e "$ARCHIVE" ]'-test_expect_success 'status reports the returned archive as tampered' '+test_expect_success 'a load stopped after its archive copy leaves an archive without a record' '+ stopped_load &&+ [ -e "$ARCHIVE" ] &&+ [ -e "$PINDIR/link" ]+'+test_expect_success 'status reports the archive without a record as tampered' '"$SHIRUDO_BIN" status --json >"$W/status.json" 2>/dev/null &&measured_condition "$W/status.json" >"$W/cond" &&read -r verdict cond detail <"$W/cond" &&@@ -86,16 +142,16 @@ test_expect_success 'status reports the returned archive as tampered' '[ "$cond" = ALERT ] &&case "$detail" in *"$NAME"*) true ;; *) false ;; esac'-test_expect_success 'init does not restore the returned archive' '+test_expect_success 'init does not restore an archive without a record' '"$SHIRUDO_BIN" init >/dev/null 2>"$W/init.err" &&grep -q "$NAME.upatch is archived without a record of its load" \"$W/init.err" &&- [ ! -e "$PINDIR" ] &&[ -e "$ARCHIVE" ]'-test_expect_success 'the next upatch command removes the returned archive' '+test_expect_success 'the next upatch command removes the archive and the pins' '"$SHIRUDO_BIN" upatch load "$FIXTURES/$OTHER.upatch" >/dev/null 2>&1 &&[ ! -e "$ARCHIVE" ] &&+ [ ! -e "$PINDIR" ] &&[ -e "$OTHERDIR/link" ] &&"$SHIRUDO_BIN" status --json >"$W/status.json" 2>/dev/null &&measured_condition "$W/status.json" >"$W/cond" &&diff --git a/util/tamper.c b/util/tamper.cindex 41a237cc..b5ba37aa 100644--- a/util/tamper.c+++ b/util/tamper.c@@ -79,7 +79,7 @@ static const struct {[TAMPER_EXEC_TRACED] = { "ExecTraced","exec of shirudo by a traced task" },[TAMPER_SEAL_ENTRY] = { "SealEntry",- "entry created, linked or renamed into a sealed directory" },+ "entry into a sealed directory, or a sealed inode linked" },[TAMPER_PERF_ATTACH] = { "PerfAttach","program attached to a perf event by another executable" },[TAMPER_PIN_ENTRY] = { "PinEntry",