Skip to content

eBPF-based security platform

init: label the state tree on the bootstrap run alone

Daniel Borkmann89e6e14b82cd
diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rst
index ce282542..a25899c1 100644
--- a/Documentation/threat-model.rst
+++ b/Documentation/threat-model.rst
@@ -178,8 +178,11 @@ T3 Tampering with shirudo's files and process
writers hold the parent directory open to populate and retain that
mark, open nothing in sealed state through a symbolic link, and take
no entry without the label for shirudo's own: ``init`` labels the
- tree on its first run and afterwards ignores a foreign entry rather
- than labelling, following, restoring or failing on it. Directory
+ tree on the run that brings enforcement up and afterwards ignores a
+ foreign entry rather than labelling, following, restoring or
+ failing on it, and refuses a state root without the label once
+ enforcement is live, since root could bind a tree of its own over
+ the path in a mount namespace and have it labelled. Directory
eviction remains subject to residual risk 15; existing writable
references are not revoked.
The authority the seal grants shirudo is not root's to aim: a path
diff --git a/builtin-init.c b/builtin-init.c
index c4236e01..7e56a71b 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -1335,10 +1335,15 @@ static int run_init(void)
{
struct shirudo_measured_upatch *set = NULL;
struct shirudo_eventlog_init rec;
- bool recorded;
+ bool recorded, bootstrap;
size_t n = 0;
int rc;
+ /* Only the run that brings enforcement up may label an unlabelled tree. */
+ rc = shirudo_enforcement_active();
+ if (rc < 0)
+ return init_phase_error(rc, "reading the enforcement state");
+ bootstrap = rc == 0;
rc = init_bpffs();
if (rc)
return init_phase_error(rc, "private bpffs setup");
@@ -1351,7 +1356,7 @@ static int run_init(void)
return init_phase_error(rc, "closing the boot key image");
/* Seal writable state before the hooks attach, then adopt the initrd log. */
- rc = seal_label_tree();
+ rc = seal_label_tree(bootstrap);
if (rc)
return init_phase_error(rc, "sealing persistent state");
rc = eventlog_adopt_pending();
@@ -1362,7 +1367,7 @@ static int run_init(void)
if (rc)
return init_phase_error(rc, "loading enforcement");
- rc = seal_label_tree();
+ rc = seal_label_tree(bootstrap);
if (rc)
return init_phase_error(rc, "sealing initialized state");
rc = shirudo_image_runtime_mount();
diff --git a/util/seal.c b/util/seal.c
index 4958cce0..a1c93389 100644
--- a/util/seal.c
+++ b/util/seal.c
@@ -87,7 +87,14 @@ static int seal_label_cb(const char *path, const struct stat *sb,
return 0;
}
-int seal_label_tree(void)
+/*
+ * The first labelling is the bootstrap's alone, before enforcement is
+ * live: with the hooks attached, a state root without the label is not
+ * the one init sealed but a tree bound over its path, in a mount
+ * namespace of root's say, and labelling it would hand root inodes the
+ * seal takes for shirudo's own.
+ */
+int seal_label_tree(bool allow_first)
{
char label[SEAL_LABEL_LEN];
struct stat st;
@@ -97,6 +104,11 @@ int seal_label_tree(void)
seal_root_dev = st.st_dev;
seal_walk_error = 0;
seal_first_init = seal_lget(SHIRUDO_VAR_LIB, label) <= 0;
+ if (seal_first_init && !allow_first) {
+ pr_err("seal: %s carries no label with enforcement live "
+ "(foreign state tree); refusing", SHIRUDO_VAR_LIB);
+ return -EPERM;
+ }
if (nftw(SHIRUDO_VAR_LIB, seal_label_cb, 16, FTW_PHYS | FTW_MOUNT) != 0)
return seal_walk_error ? seal_walk_error : -errno;
return 0;
diff --git a/util/seal.h b/util/seal.h
index 02214a41..c2572948 100644
--- a/util/seal.h
+++ b/util/seal.h
@@ -30,7 +30,7 @@ int seal_label(const char *path);
* a kind shirudo never makes is someone else's and is ignored. Stop on
* the first failed label; partial sealing is an init failure.
*/
-int seal_label_tree(void);
+int seal_label_tree(bool allow_first);
/*
* Creating under a sealed directory. The directory decides: an inode