Skip to content

eBPF-based security platform

image: default the runtime image to 15 MiB

Daniel Borkmann8e419c6754e8
diff --git a/.github/scripts/check-runtime-image.sh b/.github/scripts/check-runtime-image.sh
new file mode 100755
index 00000000..b9f55d9d
--- /dev/null
+++ b/.github/scripts/check-runtime-image.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# .github/scripts/check-runtime-image.sh -- build a runtime image of the
+# default size around a target binary, with throwaway keys, and report the
+# space left. It fails once the binary has outgrown the default of
+# shirudo-gen-runtime, which then needs raising.
+#
+# .github/scripts/check-runtime-image.sh <target-binary>
+#
+# SHIRUDO names the operator binary that makes the keys, SHIRUDO_BUILD the
+# signing helper and SHIRUDO_GEN_RUNTIME the generator. Each defaults to
+# the one in the checkout.
+
+set -eu
+
+if [ "$#" -ne 1 ]; then
+ printf 'usage: %s <target-binary>\n' "$0" >&2
+ exit 2
+fi
+
+root=$(cd "$(dirname "$0")/../.." && pwd)
+shirudo=${SHIRUDO:-$root/shirudo}
+builder=${SHIRUDO_BUILD:-$root/tools/shirudo-build/shirudo-build}
+generator=${SHIRUDO_GEN_RUNTIME:-$root/tools/gen-image/shirudo-gen-runtime}
+
+work=$(mktemp -d)
+trap 'rm -rf "$work"' EXIT HUP INT TERM
+
+if ! "$shirudo" genkey --keys-dir "$work/keys" >/dev/null 2>"$work/err"; then
+ cat "$work/err" >&2
+ exit 1
+fi
+printf '%s\n' \
+ 'config.verification_key = /run/shirudo/runtime/etc/shirudo/config-cert.der' \
+ 'config.pcr = 14' \
+ 'config.image = /var/lib/shirudo/images/config' >"$work/runtime.conf"
+
+if ! "$generator" --binary "$1" --bootstrap-conf "$work/runtime.conf" \
+ --cert "$work/keys/cert.der" --config-cert "$work/keys/cert.der" \
+ --output "$work/runtime" --keys-dir "$work/keys" \
+ --builder "$builder" >/dev/null 2>"$work/err"; then
+ cat "$work/err" >&2
+ if grep -q 'do not fit' "$work/err"; then
+ printf 'shirudo: the default runtime image no longer holds %s; raise size= in tools/gen-image/shirudo-gen-runtime\n' "$1" >&2
+ fi
+ exit 1
+fi
+
+size=$(stat -c %s "$work/runtime.img")
+free=$(debugfs -R stats "$work/runtime.img" 2>/dev/null |
+ awk '/^Free blocks:/ { print $3 * 4 }')
+printf 'shirudo: the default runtime image of %d MiB holds %s with %s KiB free\n' \
+ "$((size / 1048576))" "$1" "$free"
diff --git a/.github/workflows/deb.yml b/.github/workflows/deb.yml
index aeac966e..443421be 100644
--- a/.github/workflows/deb.yml
+++ b/.github/workflows/deb.yml
@@ -113,6 +113,12 @@ jobs:
/usr/bin/shirudo genkey
+ # The default runtime image size must hold the static target.
+ SHIRUDO=/usr/bin/shirudo \
+ SHIRUDO_BUILD=/usr/libexec/shirudo/shirudo-build \
+ SHIRUDO_GEN_RUNTIME=/usr/libexec/shirudo/gen-image/shirudo-gen-runtime \
+ sh .github/scripts/check-runtime-image.sh shirudo-target
+
# Verify that shirudo build works without a local copy of vmlinux.h
rm -rf /tmp/shirudo-smoke && mkdir -p /tmp/shirudo-smoke
cp testing/fixtures/basic.bpf.c /tmp/shirudo-smoke/
diff --git a/Documentation/contribute/developer-guide.rst b/Documentation/contribute/developer-guide.rst
index 2e5d7c0c..348f4e41 100644
--- a/Documentation/contribute/developer-guide.rst
+++ b/Documentation/contribute/developer-guide.rst
@@ -430,6 +430,11 @@ the commit at ``HEAD`` alone without them.
commit in the working tree.
* - ``check-static-target.sh <binary>``
- That a target binary is a static PIE.
+ * - ``check-runtime-image.sh <binary>``
+ - That a runtime image of the default size of
+ ``shirudo-gen-runtime`` holds the target binary, and how much
+ space it leaves. When it fails, raise ``size=`` in
+ ``tools/gen-image/shirudo-gen-runtime``.
* - ``tests.sh setup|run``
- Prepares a CI runner as the VM host and runs ``make tests``.
Meant for CI runners, which are discarded afterwards.
diff --git a/Documentation/contribute/test-coverage.rst b/Documentation/contribute/test-coverage.rst
index a65603c9..3792874e 100644
--- a/Documentation/contribute/test-coverage.rst
+++ b/Documentation/contribute/test-coverage.rst
@@ -65,8 +65,9 @@ signed config image.
- Removed fleet-management commands are absent and rejected
* - ``t1003-static-target``
- The target binary is a static PIE with OpenSSL 3.5 or newer and
- no shared TLS closure, and the operator binary is dynamic. Runs
- only where ``make target`` has built ``shirudo-target``
+ no shared TLS closure, fits the default runtime image, and the
+ operator binary is dynamic. Runs only where ``make target`` has
+ built ``shirudo-target``
* - ``t1004-target-system-libdir``
- Where ``make target-deps`` finds the system static archives
* - ``t1005-binrpm-pkg``
diff --git a/Documentation/reference/cli/gen-tools.rst b/Documentation/reference/cli/gen-tools.rst
index 8a0be5c8..c22fd6a8 100644
--- a/Documentation/reference/cli/gen-tools.rst
+++ b/Documentation/reference/cli/gen-tools.rst
@@ -58,8 +58,9 @@ root, no journal, 4096-byte blocks) and signs it with
It refuses a source that contains a file named ``signing_key.pem``, a
special file, a source that contains the keys directory, and for
-``--kind config`` any symbolic link. Requires ``mkfs.ext4`` and
-``veritysetup``.
+``--kind config`` any symbolic link. Files that do not fit ``--size``
+make it fail with ``<n> KiB of files do not fit an image of <size> MiB;
+raise --size``. Requires ``mkfs.ext4`` and ``veritysetup``.
.. list-table::
:header-rows: 1
@@ -147,8 +148,10 @@ trusted binaries. Requires ``readelf``, ``ldd`` and ``llvm-strip``.
- required
- Operator keys directory that signs the image.
* - ``--size``
- - 128
- - File system size in MiB, 8 to 1048576.
+ - 15
+ - File system size in MiB, 8 to 1048576. The default holds the
+ stripped static target binary and its certificates. A dynamic
+ binary with its libraries or an ``--extra-binary`` may need more.
* - ``--builder``
- ``$SHIRUDO_BUILD``, else ``shirudo-build``
- The signing helper.
diff --git a/testing/functional/build/t1003-static-target.sh b/testing/functional/build/t1003-static-target.sh
index 9b039533..f0b2d6e3 100644
--- a/testing/functional/build/t1003-static-target.sh
+++ b/testing/functional/build/t1003-static-target.sh
@@ -20,6 +20,18 @@ test_expect_success 'target is static PIE without an interpreter or shared depen
"$TARGET" version >"$WORK/version"
'
+if command -v veritysetup >/dev/null 2>&1 && command -v mkfs.ext4 >/dev/null 2>&1 &&
+ command -v debugfs >/dev/null 2>&1 && command -v llvm-strip >/dev/null 2>&1 &&
+ [ -x ../tools/shirudo-build/shirudo-build ]; then
+ test_expect_success 'the default runtime image holds the target binary' '
+ sh ../.github/scripts/check-runtime-image.sh "$TARGET"
+ '
+else
+ TAP_COUNT=$((TAP_COUNT + 1))
+ printf 'ok %d - the default runtime image holds the target binary # SKIP no image tools\n' \
+ "$TAP_COUNT"
+fi
+
test_expect_success 'the static ELF checker rejects the dynamic operator' '
! sh ../.github/scripts/check-static-target.sh "$OPERATOR"
'
diff --git a/testing/functional/image/t5001-config-image.sh b/testing/functional/image/t5001-config-image.sh
index 91591b16..a54b5358 100755
--- a/testing/functional/image/t5001-config-image.sh
+++ b/testing/functional/image/t5001-config-image.sh
@@ -310,6 +310,32 @@ if command -v mkfs.ext4 >/dev/null 2>&1 && command -v debugfs >/dev/null 2>&1; t
LC_ALL=C readelf -S "$W/static-probe" | grep -q "\.symtab" &&
! LC_ALL=C readelf -S "$W/extracted-static" | grep -q "\.symtab"
'
+ test_expect_success 'files that do not fit the image size are refused with their size' '
+ mkdir -p "$W/big-root" &&
+ head -c 9437184 /dev/urandom >"$W/big-root/big" &&
+ ! ../tools/gen-image/shirudo-gen-image --source "$W/big-root" \
+ --output "$W/big" --keys-dir "$W/keys" --builder "$B" --size 8 \
+ 2>"$W/big.err" &&
+ grep -Eq "[0-9]+ KiB of files do not fit an image of 8 MiB; raise --size" \
+ "$W/big.err" &&
+ ! test -e "$W/big.img"
+ '
+ test_expect_success 'check-runtime-image.sh reports the space left in the default image' '
+ SHIRUDO="$S" SHIRUDO_BUILD="$B" sh ../.github/scripts/check-runtime-image.sh \
+ "$W/static-probe" >"$W/check.out" &&
+ grep -Eq "^shirudo: the default runtime image of [0-9]+ MiB holds .* with [0-9]+ KiB free$" \
+ "$W/check.out"
+ '
+ test_expect_success 'check-runtime-image.sh fails once a binary outgrows the default' '
+ head -c 16777216 /dev/urandom >"$W/blob" &&
+ printf ".section .rodata\n.incbin \"%s\"\n.section .note.GNU-stack,\"\",%%progbits\n" \
+ "$W/blob" >"$W/blob.s" &&
+ printf "int main(void) { return 0; }\n" >"$W/big-probe.c" &&
+ ${CC:-cc} -static -o "$W/big-probe" "$W/big-probe.c" "$W/blob.s" &&
+ ! SHIRUDO="$S" SHIRUDO_BUILD="$B" sh ../.github/scripts/check-runtime-image.sh \
+ "$W/big-probe" 2>"$W/check.err" &&
+ grep -q "raise size= in tools/gen-image/shirudo-gen-runtime" "$W/check.err"
+ '
fi
diff --git a/testing/vm/bundle-images.sh b/testing/vm/bundle-images.sh
index 39f0b710..5d44d2b8 100755
--- a/testing/vm/bundle-images.sh
+++ b/testing/vm/bundle-images.sh
@@ -21,6 +21,7 @@ sed 's/@config_image@/config-boot-keys/g' "$templates/runtime.conf.in" \
--bootstrap-conf "$work/runtime.conf" --cert "$keys/cert.der" \
--config-cert "$keys/cert.der" --output "$bundle/verity/runtime" \
--keys-dir "$keys" --builder "$source_dir/tools/shirudo-build/shirudo-build" \
+ --size 64 \
--extra-binary "$source_dir/build/verity-tests/shirudo-policy-probe" \
--extra-binary "$source_dir/build/verity-tests/shirudo-image-fault" \
--unit-output "$bundle/verity/runtime-service.conf" \
diff --git a/tools/gen-image/shirudo-gen-image b/tools/gen-image/shirudo-gen-image
index 6515e6c9..b22afa87 100755
--- a/tools/gen-image/shirudo-gen-image
+++ b/tools/gen-image/shirudo-gen-image
@@ -65,9 +65,18 @@ check_source()
build_filesystem()
{
+ local files rc=0
+
truncate -s "${size}M" "$work/filesystem.img"
mkfs.ext4 -q -F -b 4096 -O '^has_journal' -E root_owner=0:0 \
- -d "$source_dir" "$work/filesystem.img"
+ -d "$source_dir" "$work/filesystem.img" 2>"$work/mkfs.err" || rc=$?
+ cat "$work/mkfs.err" >&2
+ [ "$rc" -eq 0 ] && return
+ if grep -q 'Could not allocate' "$work/mkfs.err"; then
+ files=$(du -s -k --apparent-size "$source_dir" | cut -f1)
+ die "$files KiB of files do not fit an image of $size MiB; raise --size"
+ fi
+ die 'mkfs.ext4 failed'
}
build_signed_image()
diff --git a/tools/gen-image/shirudo-gen-runtime b/tools/gen-image/shirudo-gen-runtime
index 036a4c68..025c7aff 100755
--- a/tools/gen-image/shirudo-gen-runtime
+++ b/tools/gen-image/shirudo-gen-runtime
@@ -178,7 +178,9 @@ cert=
config_cert=
output=
keys_dir=
-size=128
+# The stripped static target with its certificates, plus about 1 MiB.
+# .github/scripts/check-runtime-image.sh fails CI once it no longer fits.
+size=15
builder=${SHIRUDO_BUILD:-shirudo-build}
unit_output=
library_path=