Skip to content

eBPF-based security platform

testing: build the test VM initrd without root or host changes

John Fastabend3526cf63a5cb
diff --git a/.agents/skills/testing/SKILL.md b/.agents/skills/testing/SKILL.md
index b17c3382..141fd64c 100644
--- a/.agents/skills/testing/SKILL.md
+++ b/.agents/skills/testing/SKILL.md
@@ -25,7 +25,8 @@ make -C testing help
```
Host tests run as they are. A target test, `t?1??`, runs in a fresh VM
-per family, which needs root, KVM, the test kernel and the bundle; see
+per family, which needs KVM, an unlimited `ulimit -l`, the test kernel
+and the bundle, but not root; see
below. The VM always boots the dm-verity runtime and the signed config
from a dracut initrd. The runner caches the initrd by kernel, bundle and
initrd inputs, while each family keeps its own VM and guest state. CI
@@ -119,7 +120,7 @@ anyway. Never stop, replace or clean a VM without that answer.
## Commands
-Run each as root, as the testing guide does.
+Run each as yourself; root is not needed.
```sh
make -C testing vm VM_NAME=<name> [KERNEL=<kernel>]
diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rst
index 791e4482..15c25546 100644
--- a/Documentation/contribute/testing/testing.rst
+++ b/Documentation/contribute/testing/testing.rst
@@ -27,8 +27,9 @@ Before you begin
- For the ``ci-checks`` tests: ``git``, ``jq``, ``pkg-config``,
``readelf`` and ``spatch`` from Coccinelle. A test whose tool is
missing skips. The runner of the ``CI suite`` workflow has them all.
-- For the tests that boot a VM: root, a readable and writable
- ``/dev/kvm``, an x86-64 or arm64 host and what ``make target`` needs,
+- For the tests that boot a VM: a readable and writable ``/dev/kvm``,
+ a locked-memory limit (``ulimit -l``) of ``unlimited``, which the
+ shirudo binaries the runner calls require, an x86-64 or arm64 host and what ``make target`` needs,
since the bundle a VM boots holds the static target binary. On Ubuntu,
``.github/scripts/tests.sh setup`` installs ``cloud-image-utils``,
``dracut-core``, ``file``, ``isc-dhcp-client``, ``libguestfs-tools``,
@@ -173,7 +174,7 @@ Run everything, as CI does:
.. code-block:: console
- # make tests
+ $ make tests
``make -C testing help`` lists the unit suites and the families. Run
one tier, one unit suite or one family:
@@ -182,7 +183,7 @@ one tier, one unit suite or one family:
$ make -C testing unit
$ make -C testing unit/<suite>
- # make -C testing functional/<family>
+ $ make -C testing functional/<family>
``<suite>`` is a file name under :src:`testing/unit/` without ``.c``, such
as ``json``. ``<family>`` is a directory under :src:`testing/functional/`,
@@ -190,7 +191,7 @@ such as ``upatch``. A family runs its operator tests in number order,
then boots its VM and runs its target tests there. ``make -j`` boots the
VMs of several families side by side.
-Leave out every test that boots a VM, for a host without KVM or root:
+Leave out every test that boots a VM, for a host without KVM:
.. code-block:: console
@@ -563,7 +564,7 @@ From the checkout root, run the following commands in any order:
$ make oci
$ make telemetry
- # make vm
+ $ make vm
``make oci`` starts a persistent TLS OCI registry on
``127.0.0.1:15444``. ``make telemetry`` starts the OTLP collector on
@@ -627,7 +628,7 @@ tests.
.. code-block:: console
- # make -C testing end2end
+ $ make -C testing end2end
The host side writes to ``build/test-vm/end2end/``. The guest reaches
the registry and collector through QEMU's address for the host. ClickHouse
@@ -758,7 +759,7 @@ Write a shell test
.. code-block:: console
- # make -C testing functional/<family>
+ $ make -C testing functional/<family>
A target test finds loaded only what its family loaded, because each
family boots a VM of its own. ``require_loaded <fixture>`` loads a
diff --git a/Documentation/reference/cheat-sheet.rst b/Documentation/reference/cheat-sheet.rst
index 67748a16..21583fac 100644
--- a/Documentation/reference/cheat-sheet.rst
+++ b/Documentation/reference/cheat-sheet.rst
@@ -456,13 +456,13 @@ List the suites and families:
$ make -C testing help
-Boot the interactive test VM, log in and stop it. Booting needs root:
+Boot the interactive test VM, log in and stop it:
.. code-block:: console
- # make -C testing vm
- # make -C testing vm-ssh
- # make -C testing vm-stop
+ $ make -C testing vm
+ $ make -C testing vm-ssh
+ $ make -C testing vm-stop
Run the fuzz targets for a minute each:
diff --git a/Documentation/reference/cli/gen-tools.rst b/Documentation/reference/cli/gen-tools.rst
index 62134907..f72f1b5e 100644
--- a/Documentation/reference/cli/gen-tools.rst
+++ b/Documentation/reference/cli/gen-tools.rst
@@ -223,15 +223,20 @@ shirudo-gen-initrd
shirudo-gen-initrd --print-cmdline
Runs ``dracut --force --add shirudo`` for a kernel release, then checks
-the result. It runs as root and requires ``dracut``. The initrd holds the
+the result. It requires ``dracut``, and root unless ``--output`` names
+where to write: without it the initrd replaces the one the host boots.
+Run as another user, dracut writes every member of the archive as owned
+by root. The initrd holds the
binary at its root file system path, the primary certificate as
``/etc/shirudo/keys/cert.der``, the trusted certificates and their vouches
under ``/etc/shirudo/keys/trusted/``, the bootstrap
``/etc/shirudo/shirudo.conf`` and ``shirudo-initrd.service``, enabled.
-When the script's own directory holds ``module-setup.sh`` that differs
-from ``/usr/lib/dracut/modules.d/90shirudo/module-setup.sh``, it installs
-its copy there. Paths given on the command line reach the module through a
+When ``--module-dir`` or the script's own directory holds
+``module-setup.sh``, dracut runs on a private copy of its base directory
+whose ``modules.d/90shirudo`` is that module, and the host's
+``/usr/lib/dracut`` is left as it is. Otherwise the module comes from
+``/usr/lib/dracut/modules.d/90shirudo``. Paths given on the command line reach the module through a
generated ``dracut.conf`` that also leaves out the host's drop-ins.
The check lists the archive with ``lsinitrd`` and fails unless it holds
@@ -290,7 +295,7 @@ kernel's command line lacks ``bpf.keyring_unsealed=1`` or
- Embed no kernel modules.
* - ``--module-dir``
- the script's directory
- - Where to install the dracut module from.
+ - The dracut module to build with.
* - ``--``
-
- Pass the remaining arguments to ``dracut``.
diff --git a/Documentation/start/quickstart.rst b/Documentation/start/quickstart.rst
index 6622d25c..d0828f5d 100644
--- a/Documentation/start/quickstart.rst
+++ b/Documentation/start/quickstart.rst
@@ -21,9 +21,10 @@ Before you begin
- A Linux host on x86-64 or arm64 with KVM. ``/dev/kvm`` must be readable
and writable.
-- Root on that host. The test VM builds its initrd with dracut, which needs
- root, and the signing key it generates for the VM belongs to root. Run
- every host command on this page as root.
+- A locked-memory limit of ``unlimited`` (``ulimit -l``): the shirudo
+ binaries lock their memory and refuse to run without it. Root is not
+ needed; the test VM builds its initrd with dracut as you, without
+ changing the host's dracut.
- Network access. The first boot clones the bpf-next kernel tree and
downloads an Ubuntu cloud image.
- To build shirudo: a C toolchain with ``make`` and ``pkg-config``, a
diff --git a/testing/functional/build/t1006-gen-initrd-cli.sh b/testing/functional/build/t1006-gen-initrd-cli.sh
index 437d4d91..17c5b82d 100755
--- a/testing/functional/build/t1006-gen-initrd-cli.sh
+++ b/testing/functional/build/t1006-gen-initrd-cli.sh
@@ -27,6 +27,13 @@ test_expect_success 'the generator check refuses a missing initrd' '
! bash $G --check "$W/none.img" >/dev/null 2>&1
'
+if [ "$(id -u)" != 0 ]; then
+ test_expect_success 'the generator needs root only to replace the host initrd' '
+ ! bash $G --kver 0 >/dev/null 2>"$W/err" &&
+ grep -q "run as root, or name an --output" "$W/err"
+ '
+fi
+
test_expect_success 'the installer plans the package, the certificate and the initrd' '
bash $I --dry-run --package "$W/shirudo-target_0_amd64.deb" \
--cert "$W/cert.der" --no-cmdline --no-initrd >"$W/plan" &&
diff --git a/testing/vm/run.sh b/testing/vm/run.sh
index 43d17bf5..cdb68739 100755
--- a/testing/vm/run.sh
+++ b/testing/vm/run.sh
@@ -1333,7 +1333,6 @@ if [[ "$VM_INITRD" == 1 ]]; then
fi
check_host_dependencies
validate_host_bundle
-[[ "$EUID" == 0 ]] || die "building the initrd with dracut needs root"
[[ "$CPUS" =~ ^[1-9][0-9]*$ ]] || die "TEST_VM_CPUS must be a positive integer"
[[ "$VM_TIMEOUT" =~ ^[1-9][0-9]*$ ]] || die "TEST_VM_TIMEOUT must be a positive integer"
diff --git a/tools/gen-initrd/module-setup.sh b/tools/gen-initrd/module-setup.sh
index 2532899b..73ad4b9a 100755
--- a/tools/gen-initrd/module-setup.sh
+++ b/tools/gen-initrd/module-setup.sh
@@ -98,9 +98,12 @@ install() {
[[ -r $conf ]] || return 1
inst_simple "$conf" /etc/shirudo/shirudo.conf || return 1
inst_multiple mount || return 1
- # dracut keeps the owner of what it copies, and init refuses keys
- # and configuration another user could change.
- chown -R 0:0 "$initdir/etc/shirudo" || return 1
+ # dracut run as root keeps the owner of what it copies, and init
+ # refuses keys and configuration another user could change. Run as
+ # anyone else, it writes every member of the archive as 0:0.
+ if [[ $EUID == 0 ]]; then
+ chown -R 0:0 "$initdir/etc/shirudo" || return 1
+ fi
inst_simple "$unit" "$systemdsystemunitdir/shirudo-initrd.service" ||
return 1
$SYSTEMCTL -q --root "$initdir" enable shirudo-initrd.service ||
diff --git a/tools/gen-initrd/shirudo-gen-initrd b/tools/gen-initrd/shirudo-gen-initrd
index 4188dc6d..8aaf690e 100755
--- a/tools/gen-initrd/shirudo-gen-initrd
+++ b/tools/gen-initrd/shirudo-gen-initrd
@@ -29,7 +29,7 @@ Regenerate the initrd of a kernel with shirudo inside, through dracut.
--bootstrap-conf <file> authenticated bootstrap shirudo.conf to embed
--kmoddir <dir> kernel modules to take from <dir>/<release>
--no-kernel embed no kernel modules (all-built-in kernels)
- --module-dir <dir> dracut module to install from (default: this
+ --module-dir <dir> dracut module to build with (default: this
script's directory, when it holds module-setup.sh)
-h, --help this text
@@ -148,12 +148,11 @@ if [ "$check_only" = 1 ]; then
exit 0
fi
-[ "$(id -u)" = 0 ] || die "run as root"
+# Without --output the initrd replaces the one this host boots.
+[ -n "$output" ] || [ "$(id -u)" = 0 ] || die "run as root, or name an --output"
command -v dracut >/dev/null 2>&1 ||
die "dracut not found; the production configuration needs a systemd initrd built by dracut"
-# The module comes from the package or from this checkout; dracut only
-# takes modules from its modules.d, so a checkout's copy is installed.
if [ -z "$module_dir" ]; then
here=$(cd "$(dirname "$0")" && pwd)
if [ -f "$here/module-setup.sh" ]; then
@@ -162,14 +161,11 @@ if [ -z "$module_dir" ]; then
fi
if [ -n "$module_dir" ] && [ "$module_dir" != "$MODULE_DIR_INSTALLED" ]; then
[ -f "$module_dir/module-setup.sh" ] || die "no module-setup.sh in $module_dir"
- if ! cmp -s "$module_dir/module-setup.sh" "$MODULE_DIR_INSTALLED/module-setup.sh"; then
- install -d "$MODULE_DIR_INSTALLED"
- install -m 0755 "$module_dir/module-setup.sh" "$MODULE_DIR_INSTALLED/module-setup.sh"
- say "installed the dracut module into $MODULE_DIR_INSTALLED"
- fi
+else
+ [ -f "$MODULE_DIR_INSTALLED/module-setup.sh" ] ||
+ die "no dracut module at $MODULE_DIR_INSTALLED; install the shirudo-target package"
+ module_dir=
fi
-[ -f "$MODULE_DIR_INSTALLED/module-setup.sh" ] ||
- die "no dracut module at $MODULE_DIR_INSTALLED; install the shirudo-target package"
if [ -z "$unit" ] && [ ! -f "$UNIT_INSTALLED" ]; then
here=${here:-$(cd "$(dirname "$0")" && pwd)}
@@ -194,6 +190,24 @@ fi
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
+# dracut takes modules only from the modules.d of its base directory. A
+# module from elsewhere goes into a private base that links the host's,
+# so that building an initrd never changes the host's dracut.
+dracut_base=${dracutbasedir:-/usr/lib/dracut}
+if [ -n "$module_dir" ]; then
+ [ -d "$dracut_base/modules.d" ] || die "no dracut modules at $dracut_base/modules.d"
+ mkdir "$tmp/dracut" "$tmp/dracut/modules.d" "$tmp/dracut/modules.d/90shirudo"
+ for entry in "$dracut_base"/*; do
+ [ "${entry##*/}" = modules.d ] || ln -s "$entry" "$tmp/dracut/"
+ done
+ for entry in "$dracut_base"/modules.d/*; do
+ [ "${entry##*/}" = 90shirudo ] || ln -s "$entry" "$tmp/dracut/modules.d/"
+ done
+ install -m 0755 "$module_dir/module-setup.sh" \
+ "$tmp/dracut/modules.d/90shirudo/module-setup.sh"
+ dracut_base=$tmp/dracut
+fi
+
# Paths that differ from the installed ones reach the module through a
# dracut.conf(5) file of their own, which also shuts out the host's
# drop-ins so that a test initrd depends on nothing but what was asked.
@@ -222,7 +236,7 @@ out=${output:-$(default_output)}
[ -z "$output" ] || args+=("$output")
say "running dracut for $kver"
-dracut "${args[@]}"
+dracutbasedir=$dracut_base dracut "${args[@]}"
check_initrd "$out"
for parameter in $CMDLINE_PARAM; do