Skip to content

eBPF-based security platform

init: tell a bound register and the initramfs apart from root's files

Daniel Borkmann1ae0fdfe93f1
diff --git a/builtin-init.c b/builtin-init.c
index 37618777..495f6125 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -16,12 +16,15 @@
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
+#include <sys/statfs.h>
#include <sys/statvfs.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <unistd.h>
+#include <linux/magic.h>
+
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
@@ -1209,10 +1212,22 @@ static void usage(void)
static int initrd_enter(void)
{
struct statvfs state;
+ struct statfs root;
char src[PATH_MAX];
struct stat st;
int rc;
+ /*
+ * /etc/initrd-release is root's to create on the root file system;
+ * the initramfs is not. Binding a tree of root's over the state
+ * directory is what a run from the root file system would do here.
+ */
+ if (statfs("/", &root) != 0)
+ return -errno;
+ if (root.f_type != TMPFS_MAGIC && root.f_type != RAMFS_MAGIC) {
+ pr_err("initrd: the root file system is not an initramfs");
+ return -ENOTBLK;
+ }
if (strsprintf(src, sizeof(src), "%s%s", sysroot(),
SHIRUDO_VAR_LIB) < 0)
return -ENAMETOOLONG;
@@ -1303,13 +1318,18 @@ static int run_init(void)
if (rc)
return init_phase_error(rc, "loading trusted keys");
+ /*
+ * The record is the eventlog's, which root can hide behind a mount;
+ * the register is the TPM's, written once this boot by whoever bound
+ * it. Either says the restore has happened.
+ */
rc = eventlog_boot_init(&rec);
if (rc && rc != -ENOENT) {
pr_err("init: eventlog %s unreadable: %s", eventlog_path(),
strerror(-rc));
return rc;
}
- recorded = rc == 0;
+ recorded = rc == 0 || chain_bound();
rc = boot_restore(recorded, &set, &n);
if (rc) {
pr_err("init: boot-restore failed; further changes "
diff --git a/testing/tamperproof/t1142-archive-restore.sh b/testing/tamperproof/t1142-archive-restore.sh
index 3af9932b..8d1c3b21 100755
--- a/testing/tamperproof/t1142-archive-restore.sh
+++ b/testing/tamperproof/t1142-archive-restore.sh
@@ -7,8 +7,11 @@
# one, so root can place any operator-signed .upatch there; a re-run of
# init used to restore it, attached and pinned, without a register event,
# since this boot's init event was already on record. init must now
-# refuse the restore and leave the artifact unloaded. Cleaned up through
-# the artifact's own unload directive, which removes the archive copy.
+# refuse the restore and leave the artifact unloaded, also when the
+# state directory is hidden behind a mount that carries no eventlog, and
+# must not take a root file system for the initramfs because root wrote
+# /etc/initrd-release. Cleaned up through the artifact's own unload
+# directive, which removes the archive copy.
cd "$(dirname "$0")/.."
. ./integration-lib.sh
@@ -21,18 +24,29 @@ UNLOAD="$FIXTURES/$NAME.unload.upatch"
ARCHIVE=/var/lib/shirudo/upatches
STAGE=/var/lib/t1142-stage
PIN="/run/shirudo/bpffs/upatches/$NAME/link"
+CERT=/etc/shirudo/keys/cert.der
[ -s "$LOAD" ] && [ -s "$UNLOAD" ] || test_skip_all "missing scope fixtures"
[ ! -e "$PIN" ] || test_skip_all "$NAME is loaded"
+[ ! -e /etc/initrd-release ] || test_skip_all "running in an initrd"
+# Under the library's errexit a failing step would end the cleanup.
restore() {
+ ! mountpoint -q /var/lib/shirudo || umount /var/lib/shirudo || true
+ ! mountpoint -q "$STAGE/shadow" || umount "$STAGE/shadow" || true
+ rm -f /etc/initrd-release
+ [ ! -e "$STAGE/cert-planted" ] || rm -f "$CERT" || true
+ rmdir -p /sysroot/var/lib/shirudo 2>/dev/null || true
"$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck >/dev/null 2>&1 || true
rm -rf "$STAGE"
"$SHIRUDO_BIN" init >/dev/null 2>&1 || true
}
trap restore EXIT
-CHAIN=$("$SHIRUDO_BIN" status --json 2>/dev/null |
- python3 -c "import json,sys; print(json.load(sys.stdin)[\"tpm\"][\"chain\"])")
+chain() {
+ "$SHIRUDO_BIN" status --json 2>/dev/null |
+ python3 -c "import json,sys; print(json.load(sys.stdin)[\"tpm\"][\"chain\"])"
+}
+CHAIN=$(chain)
test_expect_success 'root can rename a signed artifact into the archive' '
mkdir -p "$STAGE" && cp "$LOAD" "$STAGE/$NAME.upatch" &&
@@ -45,8 +59,7 @@ test_expect_success 'a re-run of init refuses to restore it unmeasured' '
grep -q "archived but not loaded after the register was bound" \
"$STAGE/init.err" &&
[ ! -e "$PIN" ] &&
- [ "$("$SHIRUDO_BIN" status --json 2>/dev/null |
- python3 -c "import json,sys; print(json.load(sys.stdin)[\"tpm\"][\"chain\"])")" = "$CHAIN" ]
+ [ "$(chain)" = "$CHAIN" ]
'
test_expect_success 'the unload directive removes the planted copy and init completes again' '
@@ -54,8 +67,39 @@ test_expect_success 'the unload directive removes the planted copy and init comp
report_field category | grep -qx already_unloaded &&
[ ! -e "$ARCHIVE/$NAME.upatch" ] &&
"$SHIRUDO_BIN" init >/dev/null 2>&1 &&
- [ "$("$SHIRUDO_BIN" status --json 2>/dev/null |
- python3 -c "import json,sys; print(json.load(sys.stdin)[\"tpm\"][\"chain\"])")" = "$CHAIN" ]
+ [ "$(chain)" = "$CHAIN" ]
+'
+
+# A copy of the state less its eventlog, bound over the real one: the
+# register says the restore happened whatever the log says.
+test_expect_success 'a state directory hidden behind a mount without its eventlog restores nothing' '
+ mkdir -p "$STAGE/shadow" && mount -t tmpfs tmpfs "$STAGE/shadow" &&
+ cp -r --preserve=mode,timestamps /var/lib/shirudo/. "$STAGE/shadow/" &&
+ rm -f "$STAGE/shadow/eventlog" &&
+ cp "$LOAD" "$STAGE/shadow/upatches/$NAME.upatch" &&
+ mount --bind "$STAGE/shadow" /var/lib/shirudo &&
+ rc=0; "$SHIRUDO_BIN" init >/dev/null 2>"$STAGE/shadow.err" || rc=$?;
+ umount /var/lib/shirudo &&
+ [ "$rc" != 0 ] &&
+ grep -q "archived but not loaded after the register was bound" \
+ "$STAGE/shadow.err" &&
+ [ ! -e "$PIN" ] &&
+ [ "$(chain)" = "$CHAIN" ]
+'
+
+test_expect_success 'a root file system is not an initramfs because of /etc/initrd-release' '
+ if [ ! -e "$CERT" ]; then
+ mkdir -p "$(dirname "$CERT")" &&
+ cp /run/shirudo/runtime/keys/cert.der "$CERT" &&
+ touch "$STAGE/cert-planted"
+ fi &&
+ mkdir -p /sysroot/var/lib/shirudo && touch /etc/initrd-release &&
+ rc=0; "$SHIRUDO_BIN" init >/dev/null 2>"$STAGE/initrd.err" || rc=$?;
+ rm -f /etc/initrd-release &&
+ [ "$rc" != 0 ] &&
+ grep -q "not an initramfs" "$STAGE/initrd.err" &&
+ ! mountpoint -q /var/lib/shirudo &&
+ [ "$(chain)" = "$CHAIN" ]
'
test_done