Skip to content

eBPF-based security platform

seal: let the marks decide where the label read fails for the caller

Daniel Borkmann74483f78ffa0
diff --git a/AGENTS.md b/AGENTS.md
index 5c4ab28d..cf85301a 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -299,12 +299,16 @@ one usually still compiles and passes tests.
an authorized caller may set or remove the exact seal xattr. Both rename
endpoints, write open, unlink, setattr and xattr changes are checked. There
is no executable replacement exception.
- Label read errors require authority except ENODATA, EOPNOTSUPP, EACCES
- and EPERM; scratch allocation failure also requires authority. The seal
- labels new inodes during creation when the parent has a cached directory
- mark. Writers hold an open parent directory to retain that mark. Directory
- eviction and foreign link/rename entries remain limitations; existing
- writable descriptors and mappings are not revoked.
+ Label read errors require authority except ENODATA and EOPNOTSUPP; an
+ EACCES or EPERM, which root brings about with an idmapped mount or an
+ ACL, requires authority when the inode or the directory the operation
+ changes carries a mark; scratch allocation failure also requires
+ authority. 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.
- **Single shirudo instance per host.** Keyring name, private bpffs,
`/var/lib/shirudo`, the `security.bpf.shirudo` xattr namespace and
the NV index are single-tenant by design.
diff --git a/Documentation/internals/enforcement.rst b/Documentation/internals/enforcement.rst
index b0a4336c..4cb34b2a 100644
--- a/Documentation/internals/enforcement.rst
+++ b/Documentation/internals/enforcement.rst
@@ -207,7 +207,7 @@ order:
``sb_remount`` and the ``file_open`` recorder, open the bpffs root so
the recorder notes the mount, and freeze that record.
#. Load ``seal`` on the shared maps, freeze its scratch map, and attach
- its twenty-two hooks, then ``mmap_file`` and ``file_mprotect``.
+ its twenty-six hooks, then ``mmap_file`` and ``file_mprotect``.
#. Attach ``lsm/bpf_map``, then ``lsm/bpf_prog_load``, then ``lsm/bpf``.
``init`` pins every link as it attaches it, before the next fallible
@@ -236,8 +236,9 @@ in which they attached. The kernel's BPF trampoline puts a new program at
the head of the list, and for an LSM hook that returns a value, a
non-zero return ends the list. Where ``bpf2`` and ``seal`` share a hook
(``bprm_check_security``, ``file_open``, ``inode_unlink``,
-``inode_rename``, ``inode_setxattr``, ``inode_removexattr``,
-``inode_mkdir``, ``inode_symlink``, ``inode_link``), the ``seal``
+``inode_rmdir``, ``inode_rename``, ``inode_setxattr``,
+``inode_removexattr``, ``inode_mkdir``, ``inode_symlink``,
+``inode_link``), the ``seal``
program runs first, and when it refuses, its errno and counter are the
ones the caller sees. A traced exec of a runtime file, for example, fails with
``EACCES`` from ``seal`` rather than ``EPERM`` from ``bpf2``.
@@ -476,19 +477,27 @@ when the record it needs is missing.
- Unconditional.
* - ``lsm.s/file_open`` (``seal``)
- A write open of a labeled file to a task without authority. On a
- directory open, records or clears the directory mark from its
- label.
+ directory open, and on any open by an authorized task, records or
+ clears the mark from the label; a write open records it.
- ``EACCES``
- ``SealWrite``
- See the label verdict below.
- * - ``lsm.s/inode_unlink``, ``lsm.s/inode_setattr``,
- ``lsm.s/inode_removexattr`` (``seal``)
+ * - ``lsm.s/inode_unlink``, ``lsm.s/inode_rmdir``,
+ ``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
- policy xattr is refused on any inode without authority.
+ seal or the policy label is refused on any inode without
+ authority.
- ``EACCES``
- - ``SealUnlink``, ``SealSetattr``, ``SealXattr``
+ - ``SealUnlink``, ``SealSetattr``, ``SealXattr``, ``SealLabel``
- See the label verdict below.
+ * - ``lsm/inode_set_acl``, ``lsm/inode_remove_acl``,
+ ``lsm/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.
+ - ``EACCES``
+ - ``SealXattr``, ``SealSetattr``
+ - Open: an inode without a cached mark is not checked.
* - ``lsm.s/inode_setxattr`` (``seal``)
- Setting ``security.bpf.shirudo`` or
``security.bpf.shirudo.policy`` on any inode, and any other name
@@ -512,7 +521,7 @@ when the record it needs is missing.
- Open: a directory without a cached mark is not checked.
* - ``lsm/inode_init_security`` (``seal``)
- Labels a new inode under a marked directory, inside the creation,
- and marks a new directory. Refuses the creation when it cannot add
+ and marks the new inode. Refuses the creation when it cannot add
the label.
- ``EACCES``
- Uncounted
@@ -530,10 +539,12 @@ when the record it needs is missing.
- A failed read removes the mark.
The label verdict of the ``seal`` hooks reads ``security.bpf.shirudo``
-with the caller's permission. No label (``ENODATA``), no security
-extended attributes on the file system (``EOPNOTSUPP``) and a label the
-caller may not read (``EACCES``, ``EPERM``) count as unlabeled, and the
-operation goes ahead. A label of any length, any other read error and a
+with the caller's permission. No label (``ENODATA``) and no security
+extended attributes on the file system (``EOPNOTSUPP``) count as
+unlabeled, and the operation goes ahead. A label the caller may not
+read (``EACCES``, ``EPERM``) counts as unlabeled unless the inode, or
+the directory the operation changes, carries a mark, which requires
+runtime authority. A label of any length, any other read error and a
failure to allocate the scratch buffer require runtime authority.
:doc:`seal` explains why and what follows from it.
diff --git a/Documentation/internals/seal.rst b/Documentation/internals/seal.rst
index 5bfc94cd..cfb9fe5f 100644
--- a/Documentation/internals/seal.rst
+++ b/Documentation/internals/seal.rst
@@ -184,14 +184,19 @@ Reading the label
The seal reads the label with the ``bpf_get_file_xattr()`` and
``bpf_get_dentry_xattr()`` kfuncs. Both first check that the calling
-task may read the inode. If the seal refused every operation whose label
-read failed, it would refuse every write-only open and every unlink of a
-file the caller cannot read, on every file system. So these results count
-as unlabeled and the operation goes ahead: no label (``ENODATA``), no
-security extended attributes on the file system (``EOPNOTSUPP``), and a
-label the caller may not read (``EACCES``, ``EPERM``). A label of any
-length, any other error, and a failure to allocate the scratch buffer
-the kfunc writes into require runtime authority.
+task may read the inode, against its raw ids. If the seal refused every
+operation whose label read failed, it would refuse every write-only
+open and every unlink of a file the caller cannot read, on every file
+system. So no label (``ENODATA``) and no security extended attributes
+on the file system (``EOPNOTSUPP``) count as unlabeled and the operation
+goes ahead. A label the caller may not read (``EACCES``, ``EPERM``) is
+also what root makes of any label by granting a write without a read,
+through an idmapped bind mount or an ACL, and then acting at a file
+system uid that cannot read. There the mark of the inode, or of the
+directory the operation changes, decides, and a marked one requires
+runtime authority. A label of any length, any other error, and a
+failure to allocate the scratch buffer the kfunc writes into require
+runtime authority.
Labeling at creation
---------------------
@@ -205,11 +210,12 @@ creation: no inode is better than one of shirudo's without its label.
That hook cannot sleep, so it cannot read the parent's extended
attribute. It uses a cached mark instead: ``seal_dir_marks``, inode
-storage that records a directory's label. A directory gets its mark when
-any task opens it and its label is read, when an extended attribute on it
-changes, and when it is created under a marked parent. A directory open
-whose label read finds nothing removes the mark; a read that fails, for
-a caller who may not read the directory, leaves it.
+storage that records an inode's label. A directory gets its mark when
+any task opens it and its label is read, a file when an authorized task
+opens it or any task opens it for writing, and either when an extended
+attribute on it changes and when it is created under a marked parent.
+An open whose label read finds nothing removes the mark; a read that
+fails, for a caller who may not read the inode, leaves it.
shirudo's writers keep the mark in place. They open the parent directory
and create files relative to that descriptor, which holds the inode in
@@ -231,12 +237,14 @@ Limits
These are properties of the mechanism as it is. The
:doc:`/security/threat-model` states their consequences.
-Directory eviction
- A mark is a cache. When the kernel evicts a directory's inode,
- under memory pressure or through ``vm.drop_caches``, its mark goes
- with it until the next open of that directory. In that window,
- creation, links and renames into the directory are not checked, and
- ``inode_init_security`` adds no label.
+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
@@ -251,14 +259,15 @@ Label reads under another LSM
The kfuncs' read permission check runs every LSM's
``inode_permission`` hook for the calling task. A policy of another
LSM that lets a task write or unlink a file but not read it makes a
- labeled inode look unlabeled to the seal, and the same failure on
- a directory open removes the directory's mark.
-
-Operations without a check
- The seal does not attach ``inode_rmdir``, ``inode_file_setattr`` or
- the ACL hooks ``inode_set_acl`` and ``inode_remove_acl``. Root can
- remove an empty labeled directory, set the immutable or append-only
- flag on sealed files with ``chattr``, and change their POSIX ACLs.
+ labeled inode without 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 mounts
No mount guard covers the state file system, and shirudo's opens
diff --git a/Documentation/security/security-model.rst b/Documentation/security/security-model.rst
index 9e48e493..57ffd9c1 100644
--- a/Documentation/security/security-model.rst
+++ b/Documentation/security/security-model.rst
@@ -42,9 +42,9 @@ What shirudo provides
* - Only shirudo changes its state
- The seal labels ``/var/lib/shirudo`` and checks every change to
a labeled inode; keyrings and authorized processes are guarded.
- - Directory marks can be evicted; some operations and other LSMs
- are not covered; offline writes bypass it.
- - :ref:`T3 <tm-t3>`, R15, R19, U5, U12
+ - Marks can be evicted; another LSM can refuse the label read;
+ offline writes bypass it.
+ - :ref:`T3 <tm-t3>`, R15, R19, U12
* - Every change is measured
- Loads, unloads, revocations and tree operations extend a TPM NV
register whose writes ``lsm/tpm_command`` keeps to shirudo.
diff --git a/Documentation/security/tamper-counters.rst b/Documentation/security/tamper-counters.rst
index 1af7eb69..bb79b412 100644
--- a/Documentation/security/tamper-counters.rst
+++ b/Documentation/security/tamper-counters.rst
@@ -130,20 +130,23 @@ counter.
- ``lsm.s/file_open``
- ``EACCES``
* - ``SealUnlink``
- - unlink of a sealed file
- - ``lsm.s/inode_unlink``
+ - unlink of a sealed file or removal of a sealed directory
+ - ``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
- ``EACCES``
* - ``SealSetattr``
- - truncate, chmod, chown or utimes of a sealed file
- - ``lsm.s/inode_setattr``
+ - truncate, chmod, chown, utimes or chattr of a sealed file
+ - ``lsm.s/inode_setattr``; ``lsm/inode_file_setattr`` on a marked
+ inode
- ``EACCES``
* - ``SealXattr``
- - xattr change on a sealed file
- - ``lsm.s/inode_setxattr``, ``lsm.s/inode_removexattr``
+ - 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
- ``EACCES``
* - ``SealLabel``
- seal or policy label changed by another executable
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index 4d3b621a..d3a451aa 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -646,9 +646,12 @@ labels the existing tree on the run that brings enforcement up, before
the seal attaches. On every later run a state root without the label is
refused, since it is a tree bound over the path rather than the one
``init`` sealed (``t6123``). For a labeled inode, a write open, an
-unlink, either end of a rename, a setattr (truncate, chmod, chown,
-utimes) and an xattr change need runtime authority (``SealWrite``,
-``SealUnlink``, ``SealRename``, ``SealSetattr``, ``SealXattr``). Only an
+unlink or the removal of an empty directory, either end of a rename, a
+setattr (truncate, chmod, chown, utimes) and an xattr change need
+runtime authority (``SealWrite``, ``SealUnlink``, ``SealRename``,
+``SealSetattr``, ``SealXattr``), and for a marked one so do a change of
+its POSIX ACL (``SealXattr``) and of its inode flags with ``chattr``
+(``SealSetattr``), whose hooks cannot read the label. Only an
authorized task sets or removes the exact seal label or changes the
policy label on any inode (``SealLabel``).
@@ -657,13 +660,22 @@ target and never grants authority: a file system root mounts can carry
it, and a binary that carries it gains nothing. A label read that fails
with an error other than ``ENODATA``, ``EOPNOTSUPP``, ``EACCES`` or
``EPERM`` requires authority, and so does a failure to allocate the
-seal's scratch memory. The two permission errors count as unlabeled
-because the kernel checks read permission for the caller before it
-returns the label (U12). The labels themselves are refused to such a
-caller whatever the read said: root takes a file system uid that cannot
-read the inode with ``setfsuid()`` and keeps the ``CAP_SYS_ADMIN`` that
-a ``security.*`` xattr change needs, so the read fails where the
-removal would pass (``t6111``).
+seal's scratch memory. The kernel checks read permission for the caller
+before it returns the label, against the raw ids of the inode (U12), so
+the two permission errors mean either a caller that may do nothing or
+one that root gave a write without a read: through an idmapped bind
+mount, which shows uid 0's files as another id's to the permission
+check, or through an ACL, and then at a file system uid that cannot
+read. Where the read fails that way, the mark of the inode, or of the
+directory the operation changes, decides, and the operation needs
+authority when either is marked (``t6111``). The seal marks every inode
+it labels at creation, every file shirudo opens, every file anyone
+opens for writing and every directory anyone opens; a file shirudo has
+not touched since it came into memory has no mark (R15). The labels
+themselves are refused to such a caller whatever the read said: root
+takes a file system uid that cannot read the inode with ``setfsuid()``
+and keeps the ``CAP_SYS_ADMIN`` that a ``security.*`` xattr change
+needs, so the read fails where the removal would pass (``t6111``).
Executables may carry ``security.bpf.shirudo.policy``, an 8-byte
target-native ``__u64`` bitset. During exec, the seal ORs the value from
@@ -761,8 +773,10 @@ An authorized process is protected against being taken over:
:Adversary: ADV-B and ADV-C
:Enforced at: ``lsm.s/file_open``, ``lsm.s/inode_unlink``,
- ``lsm.s/inode_rename``, ``lsm.s/inode_setattr``,
- ``lsm.s/inode_setxattr``, ``lsm.s/inode_removexattr``,
+ ``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``,
@@ -781,7 +795,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``
-:Residual: R2, R15, R19, R22, U5, U12
+:Residual: R2, R15, R19, R22, U12
T4 Substituting artifacts
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1386,18 +1400,22 @@ R14 Configs roll back
Detection: ``--expect-config-root`` with ``--expect-config-pcr``.
Mitigation: D2.
-R15 Directory marks can be evicted
- The label at creation and the entry checks follow a mark that the
- seal keeps on a directory inode in memory. Root can evict the inode,
- by dropping caches for instance, whenever shirudo does not hold the
- directory open. While the mark is absent, root can create entries in
- a labeled directory 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 labeled entry is taken for
- shirudo's own. Detection: only signatures and measurement; an
+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 labeled entry is taken
+ for shirudo's own. While a file's mark is absent, root can change
+ its ACL and inode flags, and a label read it made fail counts as
+ unlabeled (T3). Detection: only signatures and measurement; an
archived upatch restored at the next boot appears in the init event's
set. Mitigation: D6.
@@ -1527,19 +1545,6 @@ U4 Signatures other than ML-DSA-87 are accepted in places
:Meanwhile: Use only certificates that ``shirudo genkey`` made, for
repositories too.
-U5 The seal does not cover every operation
- :Affects: Writable state (T3).
- :Consequence: ``inode_rmdir``, ``inode_file_setattr`` (``chattr``)
- and the POSIX ACL hooks are not attached, so root can remove an
- empty labeled directory, set the immutable or append-only flag
- on sealed files, and change their ACLs. The seal's link check
- looks at the destination directory only, so a hard link keeps a
- labeled inode alive in an unsealed directory (R15).
- :Status: The missing hooks are confirmed in code; the consequences
- are a code-path reading, not reproduced.
- :Meanwhile: Nothing on the target. A changed upatch archive still
- fails at load or shows in the init event.
-
U6 The eventlog becomes unreadable at 8 MiB
:Affects: Every reader of the eventlog: replay, ``shirudo status``,
``shirudo attest`` and ``init``.
@@ -1629,9 +1634,9 @@ U12 Another LSM can switch the seal off
:Consequence: The kernel's label read checks the caller's read
permission, and every LSM's ``inode_permission`` hook takes part.
A policy that grants write or unlink but refuses read makes a
- labeled file look unlabeled, and the operation goes through
- without a counter. The same refusal on a directory open leaves
- its mark as it was.
+ labeled inode without a mark look unlabeled, and the operation
+ goes through without a counter; a marked one needs authority
+ (T3), and the same refusal on an open leaves the mark as it was.
:Status: Confirmed in code and in the kernel; the attack is a
code-path reading, not reproduced.
:Meanwhile: Run no other LSM whose policy root can change at
diff --git a/bpf/seal.c b/bpf/seal.c
index f14f7870..3a67141c 100644
--- a/bpf/seal.c
+++ b/bpf/seal.c
@@ -22,7 +22,10 @@ struct {
/*
* 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.
+ * directory at every open, a file at an open for writing by anyone, at
+ * any open by an authorized task and at its creation under a marked
+ * directory. Where a hook cannot read the label, or the read fails for
+ * the caller, the mark stands in for it.
*/
struct seal_dir_mark {
char label[LABEL_LEN];
@@ -59,23 +62,45 @@ static __always_inline int seal_caller(enum tamper_point point)
shirudo_tamper_deny(point, -EACCES);
}
+static __always_inline bool seal_task_authorized(void)
+{
+ struct sb_id *id;
+ __u32 zero = 0;
+
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ return id && shirudo_task_is_self(id);
+}
+
+static __always_inline bool seal_marked(struct inode *inode)
+{
+ return inode && bpf_inode_storage_get(&seal_dir_marks, inode, NULL, 0);
+}
+
/*
* Decide on the result of reading the target's label (tlen). No label
* (-ENODATA) or no security xattrs on the file system (-EOPNOTSUPP) is
- * not shirudo's inode. Neither is one the caller may not read (-EACCES,
- * -EPERM): the kfunc checks read permission for the caller, so failing
- * those closed would refuse every write-only open and every unlink of an
- * unreadable file. A label of any size, or any other error, requires the
- * caller to be shirudo. 0 = allow, -EACCES = deny.
+ * not shirudo's inode. The kfunc checks read permission for the caller
+ * against the raw ids of the inode, so the read fails with -EACCES or
+ * -EPERM for a caller that may neither read nor write, and for one that
+ * an ACL or an idmapped mount lets write what it may not read. Failing
+ * every such read closed would refuse every write-only open and every
+ * unlink of an unreadable file, so the mark of the inode, or of the
+ * directory the operation changes, decides there. A label of any size,
+ * or any other error, requires the caller to be shirudo. 0 = allow,
+ * -EACCES = deny.
*/
-static __always_inline int seal_verdict(int tlen,
+static __always_inline int seal_verdict(int tlen, struct inode *inode,
+ struct inode *dir,
enum tamper_point point)
{
switch (tlen) {
case -ENODATA:
case -EOPNOTSUPP:
+ return 0;
case -EACCES:
case -EPERM:
+ if (seal_marked(inode) || seal_marked(dir))
+ return seal_caller(point);
return 0;
default:
return seal_caller(point);
@@ -251,8 +276,18 @@ int BPF_PROG(shirudo_seal_file_open, struct file *file, int ret)
bpf_get_file_xattr(file, XATTR_SEAL, &tdp));
return 0;
}
- if (!(BPF_CORE_READ(file, f_mode) & FMODE_WRITE))
+ if (!(BPF_CORE_READ(file, f_mode) & FMODE_WRITE)) {
+ /* A read by shirudo marks what it reads; others cost nothing. */
+ if (!seal_task_authorized())
+ return 0;
+ 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, XATTR_SEAL, &tdp));
return 0;
+ }
s = seal_scratch_get(task);
if (!s) /* cannot read the label */
return seal_caller(TAMPER_SEAL_WRITE);
@@ -260,7 +295,7 @@ int BPF_PROG(shirudo_seal_file_open, struct file *file, int ret)
tlen = bpf_get_file_xattr(file, XATTR_SEAL, &tdp);
if (tlen == LABEL_LEN)
seal_mark(inode, s->target, tlen);
- return seal_verdict(tlen, TAMPER_SEAL_WRITE);
+ return seal_verdict(tlen, inode, NULL, TAMPER_SEAL_WRITE);
}
/*
@@ -284,14 +319,14 @@ int BPF_PROG(shirudo_seal_inode_copy_up, struct dentry *src,
return shirudo_tamper_deny(TAMPER_SEAL_COPY_UP, -EPERM);
}
-/* Refresh a directory's mark from its label after an xattr change. */
+/* Refresh an inode'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))
+ if (!inode)
return;
s = seal_scratch_get(bpf_get_current_task_btf());
if (!s)
@@ -317,8 +352,9 @@ void BPF_PROG(shirudo_seal_inode_post_removexattr, struct 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.
+ * 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,
@@ -342,8 +378,6 @@ int BPF_PROG(shirudo_seal_inode_init_security, struct inode *inode,
bpf_dynptr_from_mem(m->label, LABEL_LEN, 0, &dp);
if (bpf_inode_init_xattr(xattrs, xattr_count, XATTR_SEAL, &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)
@@ -351,7 +385,8 @@ int BPF_PROG(shirudo_seal_inode_init_security, struct inode *inode,
return 0;
}
-static __always_inline int seal_dentry(struct dentry *dentry,
+static __always_inline int seal_dentry(struct inode *dir,
+ struct dentry *dentry,
enum tamper_point point)
{
struct task_struct *task = bpf_get_current_task_btf();
@@ -362,14 +397,14 @@ static __always_inline int seal_dentry(struct dentry *dentry,
return seal_caller(point);
bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);
return seal_verdict(bpf_get_dentry_xattr(dentry, XATTR_SEAL, &tdp),
- point);
+ dentry->d_inode, dir, point);
}
SEC("lsm.s/inode_unlink")
int BPF_PROG(shirudo_seal_inode_unlink, struct inode *dir,
struct dentry *dentry, int ret)
{
- return ret ? ret : seal_dentry(dentry, TAMPER_SEAL_UNLINK);
+ return ret ? ret : seal_dentry(dir, dentry, TAMPER_SEAL_UNLINK);
}
/*
@@ -435,7 +470,7 @@ int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,
if (ret)
return ret;
- err = seal_dentry(old_dentry, TAMPER_SEAL_RENAME);
+ err = seal_dentry(old_dir, old_dentry, TAMPER_SEAL_RENAME);
if (err)
return err;
err = seal_dir_entry(new_dir);
@@ -443,7 +478,7 @@ int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,
return err;
if (!target)
return 0;
- return seal_dentry(new_dentry, TAMPER_SEAL_RENAME);
+ return seal_dentry(new_dir, new_dentry, TAMPER_SEAL_RENAME);
}
/* Truncate, chmod, chown and utimes all come through here. */
@@ -451,7 +486,7 @@ SEC("lsm.s/inode_setattr")
int BPF_PROG(shirudo_seal_inode_setattr, struct mnt_idmap *idmap,
struct dentry *dentry, struct iattr *attr, int ret)
{
- return ret ? ret : seal_dentry(dentry, TAMPER_SEAL_SETATTR);
+ return ret ? ret : seal_dentry(NULL, dentry, TAMPER_SEAL_SETATTR);
}
SEC("lsm.s/inode_setxattr")
@@ -463,7 +498,7 @@ int BPF_PROG(shirudo_seal_inode_setxattr, struct mnt_idmap *idmap,
return ret;
if (seal_is_label(name) || seal_is_policy(name))
return seal_caller(TAMPER_SEAL_LABEL);
- return seal_dentry(dentry, TAMPER_SEAL_XATTR);
+ return seal_dentry(NULL, dentry, TAMPER_SEAL_XATTR);
}
/*
@@ -479,7 +514,49 @@ int BPF_PROG(shirudo_seal_inode_removexattr, struct mnt_idmap *idmap,
return ret;
if (seal_is_label(name) || seal_is_policy(name))
return seal_caller(TAMPER_SEAL_LABEL);
- return seal_dentry(dentry, TAMPER_SEAL_XATTR);
+ return seal_dentry(NULL, dentry, TAMPER_SEAL_XATTR);
+}
+
+/*
+ * 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).
+ */
+SEC("lsm/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;
+}
+
+SEC("lsm/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;
+}
+
+SEC("lsm/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;
+}
+
+SEC("lsm.s/inode_rmdir")
+int BPF_PROG(shirudo_seal_inode_rmdir, struct inode *dir,
+ struct dentry *dentry, int ret)
+{
+ return ret ? ret : seal_dentry(dir, dentry, TAMPER_SEAL_UNLINK);
}
char _license[] SEC("license") = "Dual BSD/GPL";
diff --git a/util/image_guards.c b/util/image_guards.c
index a648f167..ad92a77c 100644
--- a/util/image_guards.c
+++ b/util/image_guards.c
@@ -36,10 +36,15 @@ shirudo_seal_guards[SEAL_HOOK_COUNT] = {
{ "shirudo_seal_task_alloc", SEAL_TASK_ALLOC_PIN },
{ "shirudo_seal_file_open", SEAL_FILE_OPEN_PIN },
{ "shirudo_seal_inode_unlink", SEAL_UNLINK_PIN },
+ { "shirudo_seal_inode_rmdir", SEAL_RMDIR_PIN },
{ "shirudo_seal_inode_rename", SEAL_RENAME_PIN },
{ "shirudo_seal_inode_setattr", SEAL_SETATTR_PIN },
+ { "shirudo_seal_inode_file_setattr",
+ SEAL_FILE_SETATTR_PIN },
{ "shirudo_seal_inode_setxattr", SEAL_SETXATTR_PIN },
{ "shirudo_seal_inode_removexattr", SEAL_RMXATTR_PIN },
+ { "shirudo_seal_inode_set_acl", SEAL_SET_ACL_PIN },
+ { "shirudo_seal_inode_remove_acl", SEAL_REMOVE_ACL_PIN },
{ "shirudo_seal_inode_post_setxattr",
SEAL_POST_SETXATTR_PIN },
{ "shirudo_seal_inode_post_removexattr",
diff --git a/util/image_guards.h b/util/image_guards.h
index 2b5872f0..3ba30be7 100644
--- a/util/image_guards.h
+++ b/util/image_guards.h
@@ -8,7 +8,7 @@
#include <stdint.h>
-#define SEAL_HOOK_COUNT 22
+#define SEAL_HOOK_COUNT 26
#define IMAGE_SEAL_HOOK_COUNT 2
#define MOUNT_HOOK_COUNT 5
#define OBSERVER_HOOK_COUNT 9
diff --git a/util/paths.h b/util/paths.h
index 2aac1dc3..e2933fdc 100644
--- a/util/paths.h
+++ b/util/paths.h
@@ -96,6 +96,10 @@
#define SEAL_SYMLINK_PIN BPFFS_DIR "/seal_inode_symlink_link"
#define SEAL_MKNOD_PIN BPFFS_DIR "/seal_inode_mknod_link"
#define SEAL_LINK_PIN BPFFS_DIR "/seal_inode_link_link"
+#define SEAL_RMDIR_PIN BPFFS_DIR "/seal_inode_rmdir_link"
+#define SEAL_SET_ACL_PIN BPFFS_DIR "/seal_inode_set_acl_link"
+#define SEAL_REMOVE_ACL_PIN BPFFS_DIR "/seal_inode_remove_acl_link"
+#define SEAL_FILE_SETATTR_PIN BPFFS_DIR "/seal_inode_file_setattr_link"
#define BPFFS_UPATCHES_DIR BPFFS_DIR "/upatches"
#define IDENTITY_PIN BPFFS_DIR "/identity"
#define TAMPER_PIN BPFFS_DIR "/tamper"
diff --git a/util/tamper.c b/util/tamper.c
index b8bef963..48546519 100644
--- a/util/tamper.c
+++ b/util/tamper.c
@@ -63,13 +63,13 @@ static const struct {
[TAMPER_SEAL_WRITE] = { "SealWrite",
"write open of a sealed file" },
[TAMPER_SEAL_UNLINK] = { "SealUnlink",
- "unlink of a sealed file" },
+ "unlink of a sealed file or removal of a sealed directory" },
[TAMPER_SEAL_RENAME] = { "SealRename",
"rename of or over a sealed file" },
[TAMPER_SEAL_SETATTR] = { "SealSetattr",
- "truncate, chmod, chown or utimes of a sealed file" },
+ "truncate, chmod, chown, utimes or chattr of a sealed file" },
[TAMPER_SEAL_XATTR] = { "SealXattr",
- "xattr change on a sealed file" },
+ "xattr or ACL change on a sealed file" },
[TAMPER_SEAL_LABEL] = { "SealLabel",
"seal or policy label changed by another executable" },
[TAMPER_SEAL_PTRACE] = { "SealPtrace",