Skip to content

eBPF-based security platform

image: check the config root on the descriptor shirudo reads it through

Daniel Borkmannd3d2f32469e0
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index a93c9702..872b4b62 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -488,6 +488,10 @@ signed config image.
* - ``t6148-read-once``
- A load and an unload stage nothing in sealed state, and the
archive holds the bytes the load read
+ * - ``t6149-config-root``
+ - A directory of the config image bound in place of its mount, in a
+ namespace with a ``/run`` of root's making, is refused as the
+ config
``bpm`` (7)
~~~~~~~~~~~
diff --git a/Documentation/internals/config-images.rst b/Documentation/internals/config-images.rst
index 1de520ff..61d1250a 100644
--- a/Documentation/internals/config-images.rst
+++ b/Documentation/internals/config-images.rst
@@ -209,9 +209,11 @@ then publishes it in the cache as ``<root hash>.img`` and its siblings.
It then activates it:
#. Map and check the candidate as above, require the device record,
- mount it at ``/run/shirudo/config-candidate``, validate it, and check
- that every certificate of its trust set is in the sealed ``.bpf``
- keyring.
+ mount it at ``/run/shirudo/config-candidate``, open its root once and
+ check on that descriptor that it is the image's device and the root
+ inode of its file system, validate it, and check that every
+ certificate of its trust set is in the sealed ``.bpf`` keyring.
+ Everything that follows reads the image through that descriptor.
#. Record the selection in the journal as pending.
#. Extend the config PCR with the update event.
#. Read the PCR and the TPM's reset and restart counts back; the PCR must
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index 03b04a4d..61d2e5b0 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -765,8 +765,13 @@ root swapped for a link would lead it to sealed state (``t6007``).
a private key and root cannot have shirudo write a key pair into sealed
state (``t6114``). The
contents of a config image are read from the descriptor of its verified
-root with ``openat2(2)`` and ``RESOLVE_BENEATH``, never through a
-``/proc/<pid>/fd`` path that root could mount over.
+root with ``openat2(2)`` and ``RESOLVE_BENEATH``, or one component at a
+time without following links, never through a ``/proc/<pid>/fd`` path
+that root could mount over. shirudo opens that descriptor once and
+checks it where it opened it, for the image's device and the root inode
+of its file system: a path checked and then opened is root's to swap in
+between, and a directory of the image bound in place of its mount has
+the image's device but leads every relative path elsewhere (``t6149``).
The ``.bpf`` and ``.dm-verity`` keyrings are sealed with
``KEYCTL_RESTRICT_KEYRING`` and no restriction type, so the kernel
@@ -878,7 +883,8 @@ An authorized process is protected against being taken over:
:Tests: ``t6001``, ``t6006``, ``t6007``, ``t6110``, ``t6111``,
``t6112``, ``t6113``, ``t6114``, ``t6115``, ``t6118``, ``t6123``,
``t6124``, ``t6127``, ``t6129``, ``t6131``, ``t6133``, ``t6134``,
- ``t6135``, ``t6136``, ``t6137``, ``t6144``, ``t6145``, ``t6146``
+ ``t6135``, ``t6136``, ``t6137``, ``t6144``, ``t6145``, ``t6146``,
+ ``t6149``
:Residual: R2, R19, R22
T4 Substituting artifacts
diff --git a/testing/functional/tamperproof/t6149-config-root.sh b/testing/functional/tamperproof/t6149-config-root.sh
new file mode 100755
index 00000000..c338f554
--- /dev/null
+++ b/testing/functional/tamperproof/t6149-config-root.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t6149 -- shirudo reads the config image only from the root of its
+# mount: a directory of the image bound in its place, in a namespace with
+# a /run of root's making, has the image's device but every relative path
+# leads elsewhere in the image, and a command there refuses the config.
+
+cd "$(dirname "$0")/../.."
+. ./integration-lib.sh
+target_guard
+command -v unshare >/dev/null 2>&1 || test_skip_all "no unshare"
+mountpoint -q /run/shirudo/config || test_skip_all "no config image mounted"
+SUB=$(find /run/shirudo/config -mindepth 1 -maxdepth 1 -type d \
+ ! -name lost+found | head -1)
+[ -n "$SUB" ] || test_skip_all "the config image has no directory"
+RUNTIME=$(runtime_binary)
+W=$(mktemp -d)
+trap 'rm -rf "$W"' EXIT
+mkdir -p "$W/run"
+cat >"$W/run.sh" <<EOF
+set -e
+mount -t tmpfs none "$W/run"
+mkdir -p "$W/run/shirudo/bpffs" "$W/run/shirudo/runtime" "$W/run/shirudo/config"
+mount --bind /run/shirudo/bpffs "$W/run/shirudo/bpffs"
+mount --bind /run/shirudo/runtime "$W/run/shirudo/runtime"
+mount --bind "\$1" "$W/run/shirudo/config"
+mount --rbind "$W/run" /run
+"$RUNTIME" upatch list >"$W/out" 2>&1
+EOF
+
+test_expect_success 'a command reads the config from the root of its mount' '
+ unshare -m sh "$W/run.sh" /run/shirudo/config
+'
+test_expect_success 'and refuses a directory of the image bound in its place' '
+ ! unshare -m sh "$W/run.sh" "$SUB" &&
+ grep -q "image state: Structure needs cleaning" "$W/out"
+'
+test_done
diff --git a/util/image_config.c b/util/image_config.c
index da40f885..cb603e85 100644
--- a/util/image_config.c
+++ b/util/image_config.c
@@ -152,10 +152,9 @@ static int validate_settings(int root_fd)
ssize_t length;
int fd, ret = 0;
- fd = openat(root_fd, "etc/shirudo/shirudo.conf",
- O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
+ fd = open_beneath(root_fd, "etc/shirudo/shirudo.conf", O_RDONLY);
if (fd < 0)
- return -errno;
+ return fd;
file = fdopen(fd, "r");
if (!file) {
ret = -errno;
diff --git a/util/image_runtime.c b/util/image_runtime.c
index f1c00f3f..5a3f726c 100644
--- a/util/image_runtime.c
+++ b/util/image_runtime.c
@@ -588,6 +588,31 @@ static noinline_for_stack int candidate_keys(int fd, bool enrolled)
return ret;
}
+#define EXT4_ROOT_INO 2
+
+/*
+ * The root of the config image on device as a descriptor, opened once
+ * and checked where it was opened: a path checked first and opened after
+ * is root's to swap between the two in a mount namespace of its own, and
+ * a directory of the image bound elsewhere has the image's device too,
+ * but every relative path read from it lands elsewhere in the image.
+ */
+static int config_root_open(const char *dir, dev_t device, int bad)
+{
+ struct stat st;
+ int fd;
+
+ fd = open(dir, O_PATH | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+ if (fstat(fd, &st))
+ bad = -errno;
+ else if (st.st_dev == device && st.st_ino == EXT4_ROOT_INO)
+ return fd;
+ close(fd);
+ return bad;
+}
+
static int mount_candidate(struct image_context *current)
{
struct dm_verity *device = &current->candidate_device;
@@ -597,8 +622,7 @@ static int mount_candidate(struct image_context *current)
if (!stat(CONFIG_IMAGE_DIR, &st) &&
st.st_dev == current->candidate_device.device) {
- current->candidate_fd = open(CONFIG_IMAGE_DIR,
- O_PATH | O_DIRECTORY | O_CLOEXEC);
+ mountpoint = CONFIG_IMAGE_DIR;
} else {
if (umount2(mountpoint, MNT_DETACH) &&
errno != EINVAL && errno != ENOENT)
@@ -608,10 +632,10 @@ static int mount_candidate(struct image_context *current)
ret = dm_verity_mount(device, mountpoint, false);
if (ret)
return ret;
- current->candidate_fd = open(mountpoint,
- O_PATH | O_DIRECTORY | O_CLOEXEC);
}
- return current->candidate_fd < 0 ? -errno : 0;
+ current->candidate_fd = config_root_open(mountpoint, device->device,
+ -ESTALE);
+ return min(current->candidate_fd, 0);
}
static int prepare_config(struct image_context *current,
@@ -699,15 +723,11 @@ static int activate_config(struct image_context *current, bool boot)
if (ret)
return ret;
}
- fd = open(CONFIG_IMAGE_DIR, O_PATH | O_DIRECTORY | O_CLOEXEC);
+ fd = config_root_open(CONFIG_IMAGE_DIR, current->candidate_device.device,
+ -ESTALE);
if (fd < 0)
- return -errno;
- if (fstat(fd, &st))
- ret = -errno;
- else if (st.st_dev != current->candidate_device.device)
- ret = -ESTALE;
- else
- ret = mount_flags(fd, false);
+ return fd;
+ ret = mount_flags(fd, false);
if (!ret)
ret = image_config_set(&current->bootstrap_config, fd);
close(fd);
@@ -986,17 +1006,12 @@ static int config_mount_check(const struct config_update_record *record)
DEVICE_CONFIG);
if (ret)
goto out;
- fd = open(CONFIG_IMAGE_DIR, O_PATH | O_DIRECTORY | O_CLOEXEC);
+ fd = config_root_open(CONFIG_IMAGE_DIR, device.device, -EUCLEAN);
if (fd < 0) {
- ret = -errno;
+ ret = fd;
goto out;
}
- if (fstat(fd, &st))
- ret = -errno;
- else if (st.st_dev != device.device)
- ret = -EUCLEAN;
- else
- ret = mount_flags(fd, false);
+ ret = mount_flags(fd, false);
if (!ret)
ret = image_config_set(&image.bootstrap_config, fd);
close(fd);