Skip to content

eBPF-based security platform

bpf: guard attribute changes of labelled inodes in the seal

Puranjay Mohan30d077e09148
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rst
index 6f041971..9ad34ddc 100644
--- a/Documentation/architecture.rst
+++ b/Documentation/architecture.rst
@@ -288,19 +288,19 @@ Filesystem seal. ``bpf/seal.c`` relies on every inode under
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, since
any file system root mounts can present it. The ``file_open`` (for
-write), ``inode_unlink``, ``inode_rename``, ``inode_setxattr`` 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 in
-task-local storage, ``task_alloc`` inherits the mark, and
-``ptrace_access_check`` and ``perf_event_alloc`` deny against marked
-tasks, so a root debugger or profiler cannot attach to a running
-shirudo; a forged label can only mark its forger.
+write), ``inode_unlink``, ``inode_rename``, ``inode_setattr``,
+``inode_setxattr`` 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 in task-local storage, ``task_alloc`` inherits
+the mark, and ``ptrace_access_check`` and ``perf_event_alloc`` deny
+against marked tasks, so a root debugger or profiler cannot attach to a
+running shirudo; a forged label can only mark its forger.
Process hardening. ``util/harden.c`` runs at startup on the target:
``PR_SET_DUMPABLE`` off, ``PR_SET_NO_NEW_PRIVS``, ``PR_SET_PTRACER`` to
diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rst
index 75d2b72a..059670d3 100644
--- a/Documentation/threat-model.rst
+++ b/Documentation/threat-model.rst
@@ -144,11 +144,12 @@ T2 Detaching or replacing enforcement
T3 Tampering with shirudo's files and process
Every inode under ``/var/lib/shirudo`` carries the
``security.bpf.shirudo`` label; the seal program allows write open,
- unlink, rename and xattr changes only to the process that runs the
- executable ``init`` recorded, by device and inode and never by path,
- and only that process may set the label. The label marks what is
- protected and is public; it is not what identifies the caller, as a
- file system root mounts can present it on any binary.
+ attribute changes such as truncate, unlink, rename and xattr changes
+ only to the process that runs the executable ``init`` recorded, by
+ device and inode and never by path, and only that process may set
+ the label. The label marks what is protected and is public; it is
+ not what identifies the caller, as a file system root mounts can
+ present it on any binary.
Marked shirudo tasks cannot be ptraced or perf-sampled, and the
process drops dumpability, forbids new privileges and locks
sensitive buffers. The bpf keyring is sealed with
diff --git a/bpf/seal.c b/bpf/seal.c
index 74260999..0b2e9ac8 100644
--- a/bpf/seal.c
+++ b/bpf/seal.c
@@ -10,18 +10,18 @@
* shirudo binary carries the same label. The filesystem hooks read the
* target's label and, if present, require the calling task to run the
* executable shirudo init recorded in the identity map (the test bpf2
- * applies to the bpffs) -- so only shirudo may write, unlink, rename, or
- * relabel its own on-disk state. The label only marks a target, and only
- * shirudo may set it; its value is public and a mounted file system can
- * present it, so a binary carrying it proves nothing about the caller.
- * The exec hooks cache whether the successfully executed file carries a
- * Shirudo label in task storage for later access-control decisions. An
- * unlabelled filesystem target is not ours and is allowed through
- * untouched; a label that cannot be read, unless the caller may not read
- * the file, is taken to be there. No bpf_d_path(), no path strings: the
- * label rides the inode, immune to bind-mount / namespace / TOCTOU
- * games. Reads are not gated; only write-mode opens and the mutating
- * inode ops are.
+ * applies to the bpffs) -- so only shirudo may modify, unlink, rename,
+ * or relabel its own on-disk state. The label only marks a target, and
+ * only shirudo may set it; its value is public and a mounted file system
+ * can present it, so a binary carrying it proves nothing about the
+ * caller. The exec hooks cache whether the successfully executed file
+ * carries a Shirudo label in task storage for later access-control
+ * decisions. An unlabelled filesystem target is not ours and is allowed
+ * through untouched; a label that cannot be read, unless the caller may
+ * not read the file, is taken to be there. No bpf_d_path(), no path
+ * strings: the label rides the inode, immune to bind-mount / namespace /
+ * TOCTOU games. Reads are not gated; only write-mode opens and the
+ * mutating inode ops are.
*
* The filesystem and exec xattr hooks are sleepable (lsm.s/) because
* reading an xattr can sleep. The xattr-reading kfuncs write into a
@@ -307,6 +307,14 @@ int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,
return seal_dentry(new_dentry);
}
+/* Truncate, chmod, chown and utimes all come through here. */
+SEC("lsm.s/inode_setattr")
+int BPF_PROG(shirudo_seal_inode_setattr, struct mnt_idmap *idmap,
+ struct dentry *dentry, struct iattr *attr)
+{
+ return seal_dentry(dentry);
+}
+
SEC("lsm.s/inode_setxattr")
int BPF_PROG(shirudo_seal_inode_setxattr, struct mnt_idmap *idmap,
struct dentry *dentry, const char *name, const void *value,
diff --git a/builtin-init.c b/builtin-init.c
index 75ee0404..18ab3e8e 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -656,6 +656,7 @@ static int attach_seal(struct bpf_object **obj_out, struct bpf_link **links,
{ "shirudo_seal_file_open", SHIRUDO_SEAL_FILE_OPEN_PIN },
{ "shirudo_seal_inode_unlink", SHIRUDO_SEAL_UNLINK_PIN },
{ "shirudo_seal_inode_rename", SHIRUDO_SEAL_RENAME_PIN },
+ { "shirudo_seal_inode_setattr", SHIRUDO_SEAL_SETATTR_PIN },
{ "shirudo_seal_inode_setxattr", SHIRUDO_SEAL_SETXATTR_PIN },
{ "shirudo_seal_inode_removexattr", SHIRUDO_SEAL_RMXATTR_PIN },
};
@@ -774,7 +775,7 @@ static int init_lsm(bool attach_filesystem_seal)
struct bpf_link *link_unlink = NULL;
struct bpf_link *link_setx = NULL, *link_rmx = NULL, *link_pload = NULL;
struct bpf_link *link_keyperm = NULL;
- struct bpf_link *seal_links[8] = {};
+ struct bpf_link *seal_links[9] = {};
struct bpf_program *p_keyperm;
struct anchor_keys keys = {};
key_serial_t keyring;
diff --git a/testing/tamperproof/t1132-seal-scratch.sh b/testing/tamperproof/t1132-seal-scratch.sh
index 47cdecd0..f6ea711c 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 sealed5"
+SEALED="sealed1 sealed2 sealed3 sealed4 sealed5 sealed6 sealed7"
echo scratch >"$WORK/scratch"
{
echo "write $SH sh"
@@ -111,4 +111,12 @@ test_expect_success 'root cannot rename a file over a labelled file' '
[ "$(cat "$MNT/sealed5")" = scratch ]
'
+test_expect_success 'root cannot truncate, chmod or chown a labelled file' '
+ ! python3 -c "import os, sys; os.truncate(sys.argv[1], 0)" \
+ "$MNT/sealed6" 2>/dev/null &&
+ [ "$(cat "$MNT/sealed6")" = scratch ] &&
+ ! chmod 0200 "$MNT/sealed7" 2>/dev/null &&
+ ! chown 1:1 "$MNT/sealed7" 2>/dev/null
+'
+
test_done
diff --git a/testing/tamperproof/test.plan.md b/testing/tamperproof/test.plan.md
index 8cdfd420..8928631d 100644
--- a/testing/tamperproof/test.plan.md
+++ b/testing/tamperproof/test.plan.md
@@ -43,6 +43,7 @@ checks that Shirudo can still export the pre-existing Python exec record.
| 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. |
+| TP-24 | Root truncates, chmods or chowns a labelled file without opening it | Covered | `t1132-seal-scratch.sh` | `truncate(2)` changes the contents without a write open, and `chmod` can make a labelled file unreadable to a caller that may still write it; the seal guards attribute changes like writes. |
## Review notes
diff --git a/util/paths.h b/util/paths.h
index 9740e453..eb0fe61d 100644
--- a/util/paths.h
+++ b/util/paths.h
@@ -72,6 +72,7 @@
#define SHIRUDO_SEAL_TASK_ALLOC_PIN SHIRUDO_BPFFS_DIR "/seal_task_alloc_link"
#define SHIRUDO_SEAL_UNLINK_PIN SHIRUDO_BPFFS_DIR "/seal_inode_unlink_link"
#define SHIRUDO_SEAL_RENAME_PIN SHIRUDO_BPFFS_DIR "/seal_inode_rename_link"
+#define SHIRUDO_SEAL_SETATTR_PIN SHIRUDO_BPFFS_DIR "/seal_inode_setattr_link"
#define SHIRUDO_SEAL_SETXATTR_PIN SHIRUDO_BPFFS_DIR "/seal_inode_setxattr_link"
#define SHIRUDO_SEAL_RMXATTR_PIN SHIRUDO_BPFFS_DIR "/seal_inode_removexattr_link"
#define SHIRUDO_BPFFS_UPATCHES_DIR SHIRUDO_BPFFS_DIR "/upatches"