eBPF-based security platform
testing: add make vm-sbom, which loads the VMs' SBOM into the stack
John Fastabend16ecc3c6fa24
diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex edf4b38f..09fe7af6 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -744,6 +744,37 @@ stopped but not removed with ``make vm-clean`` keeps its state directoryand is enrolled too. ``VM_NAME=<name>`` enrolls that VM alone, from itsown ``enroll.json``.+Each ``make vm`` runs the prepared runtime image, and names it in the+guest's ``/etc/os-release`` as the image ``test-vm``, at a version of the+first 12 hex digits of the SHA-256 of the runtime image key, which it+records in ``image`` in its state directory. ``make vm-sbom`` gives the+stack's backend the SBOM of every image the VMs run, so that xtop's SBOM+view lists what they install and the advisories against it.+:src:`testing/vm/runtime-sbom.sh` reads the dpkg database of the runtime+image with ``guestfish``, without booting it, has ``trivy rootfs`` write+a CycloneDX SBOM of it, and keeps it beside the image, so that every run+stores the same bytes. The target then stores each SBOM with+:doc:`/operate/backend/sbom-put` in ``build/test-vm/stack/sbom/``, loads+the store with :doc:`/operate/backend/sbom-scan`, and refreshes+``node_vulns``, which would otherwise show the findings only at its next+refresh, up to 5 minutes later, printing each command before it runs+it:++.. code-block:: console++ $ make vm-sbom+ $ tools/backend/jobs/sbom-put --store file://build/test-vm/stack/sbom --image-id test-vm --image-version 2db4c1808e8a build/test-vm/cache/runtime/rootfs-….cdx.json+ sbom-put: test-vm/2db4c1808e8a/-: stored, sha256 7596…, components: 692+ $ CLICKHOUSE_CLIENT='docker exec -i shirudo-dev-clickhouse clickhouse-client' tools/backend/jobs/sbom-scan --store file://build/test-vm/stack/sbom+ sbom-scan: test-vm/2db4c1808e8a/-: ok, components: 692, findings: 2680+ sbom-scan: loaded images: 1, findings: 2680+ $ docker exec -i shirudo-dev-clickhouse clickhouse-client --query 'SYSTEM REFRESH VIEW shirudo.node_vulns_mv; SYSTEM WAIT VIEW shirudo.node_vulns_mv'++It needs ``trivy`` and its advisory database on the host, which the scan+never updates: run ``trivy image --download-db-only`` first and whenever+you want newer advisories. Run it again to rescan; an image already+stored is left as it is. ``VM_NAME=<name>`` loads that VM's image alone.+Without the checker, ``make telemetry`` starts the telemetry stack alone and says so. Astack started before the checker was built keeps its collector: run``make stack-down``, which also removes the stack's data, beforediff --git a/Makefile b/Makefileindex 222513f4..6b662c9a 100644--- a/Makefile+++ b/Makefile@@ -89,7 +89,7 @@ help:'shirudo make targets:' \' Build: make, make target, make target-deps, make bpf-deps, make install' \' Operator tools: make shirudo-build, make quote-check' \- ' Local stack: make stack-up, make oci, make telemetry, make vm, make vm-enroll, make xtop, make stack-down' \+ ' Local stack: make stack-up, make oci, make telemetry, make vm, make vm-enroll, make vm-sbom, make xtop, make stack-down' \' Checks: make sparse' \' Source index: make tags (ctags), make cscope' \' Install roles: make install-target, make install-operator' \@@ -196,6 +196,6 @@ distclean: clean$(Q)rm -rf build/quote-check-deps "$(QUOTE_CHECK_OPENSSL_PREFIX)"$(Q)$(MAKE) -f Makefile.shirudo distclean-.PHONY: vm vm-console vm-ssh vm-enroll vm-stop vm-clean oci telemetry stack-up stack-down xtop-vm vm-console vm-ssh vm-enroll vm-stop vm-clean oci telemetry stack-up stack-down xtop:+.PHONY: vm vm-console vm-ssh vm-enroll vm-sbom vm-stop vm-clean oci telemetry stack-up stack-down xtop+vm vm-console vm-ssh vm-enroll vm-sbom vm-stop vm-clean oci telemetry stack-up stack-down xtop:+$(Q)$(MAKE) -C testing $@diff --git a/testing/Makefile b/testing/Makefileindex 24a0a983..2064766d 100644--- a/testing/Makefile+++ b/testing/Makefile@@ -29,7 +29,7 @@ test-vm-arch = $(shell TEST_VM_ARCH='$(TEST_VM_ARCH)'; \.DEFAULT_GOAL := all.PHONY: all help clean unit ci-checks functional end2end build-operator build-unit \test-helpers \- fuzz afl bundle test-vm-bundle require-vm-kernel vm vm-console vm-ssh vm-enroll vm-stop vm-clean \+ fuzz afl bundle test-vm-bundle require-vm-kernel vm vm-console vm-ssh vm-enroll vm-sbom vm-stop vm-clean \vm-operator-end2endPACKAGE_FORMAT ?= $(call test-vm-arch,TEST_VM_HOST_PKG_FORMAT)@@ -215,6 +215,10 @@ vm-enroll:$(Q)VM_NAME="$(VM_NAME)" \TEST_VM_WORK_DIR="$(TEST_VM_WORK_DIR)" $(ROOT)/testing/vm/run.sh --enroll+vm-sbom:+ $(Q)VM_NAME="$(VM_NAME)" \+ TEST_VM_WORK_DIR="$(TEST_VM_WORK_DIR)" $(ROOT)/testing/vm/run.sh --sbom+xtop:+$(Q)$(MAKE) -s -C $(ROOT)/tools/xtop$(Q)$(ROOT)/testing/vm/stack.sh xtop@@ -401,7 +405,7 @@ help:' make -j boots several at once.' \' make -C testing fuzz [FUZZ_SECONDS=60] [FUZZ_CORPUS=<dir>] [FUZZERS=<target>...]' \' make -C testing afl [AFL_SECONDS=600] [FUZZ_CORPUS=<dir>] [FUZZERS=<target>...]' \- ' make -C testing vm, vm-console, vm-ssh, vm-enroll, vm-stop, or vm-clean' \+ ' make -C testing vm, vm-console, vm-ssh, vm-enroll, vm-sbom, vm-stop, or vm-clean' \' VM_KERNEL=bpf-next (default) selects the shirudo-dev/kernel artifact' \' make -C testing bundle or test-helpers' \' make -C testing stack-up, oci, telemetry, xtop or stack-down' \diff --git a/testing/vm/guest.sh b/testing/vm/guest.shindex ef194ab6..cb9da018 100755--- a/testing/vm/guest.sh+++ b/testing/vm/guest.sh@@ -248,15 +248,24 @@ configure_map_export()>/etc/shirudo/shirudo.conf}-# The end-to-end nodes run an image that the keepalive backend knows by-# its os-release id and version. Their root is the stock cloud image, not a-# dm-verity image, so the command line names no roothash= and the backend-# binds them as declared.+# The end-to-end nodes and the interactive VMs run an image that the+# keepalive backend knows by its os-release id and version, the latter+# test-vm at the version the host gives for its runtime image. Their root+# is not a dm-verity image, so the command line names no roothash= and the+# backend binds them as declared.configure_image_identity(){- [[ "${TEST_VM_SUITE:-}" == end2end ]] || return 0- cat /usr/lib/os-release "$LOWER/testing/vm/files/end2end.os-release" \- >/etc/os-release.tmp+ if [[ "${TEST_VM_SUITE:-}" == end2end ]]; then+ cat /usr/lib/os-release "$LOWER/testing/vm/files/end2end.os-release" \+ >/etc/os-release.tmp+ elif [[ -n "${TEST_VM_IMAGE_VERSION:-}" ]]; then+ {+ cat /usr/lib/os-release+ printf 'IMAGE_ID=test-vm\nIMAGE_VERSION=%s\n' "$TEST_VM_IMAGE_VERSION"+ } >/etc/os-release.tmp+ else+ return 0+ fimv -f /etc/os-release.tmp /etc/os-release}diff --git a/testing/vm/run.sh b/testing/vm/run.shindex 4f3660d8..650baf64 100755--- a/testing/vm/run.sh+++ b/testing/vm/run.sh@@ -558,6 +558,59 @@ run_enroll()exec "$REPO_ROOT/testing/vm/stack.sh" enroll "$RUN_DIR/enroll.json" "${builds[@]}"}+# An interactive VM runs the runtime image, which it names in os-release+# as the image test-vm, so that the backend reads the SBOM make vm-sbom+# loads for it. The version stands for the runtime image key.+record_image()+{+ IMAGE_VERSION="$(printf '%s' "$RUNTIME_KEY" | sha256sum | cut -c1-12)"+ printf 'test-vm\t%s\t%s\n' "$IMAGE_VERSION" "$RUNTIME_KEY" >"$RUN_DIR/image"+}++# The images the VMs of directories $@ run, each once, a line of image_id,+# image_version and runtime image key apiece.+vm_images()+{+ local dir++ for dir; do+ [[ ! -s "$dir/image" ]] || cat "$dir/image"+ done | sort -u+}++run_sbom()+{+ local images image_id image_version runtime_key sbom line args=()++ if [[ -z "$VM_NAME_REQUEST" ]]; then+ mapfile -t images < <(vm_images "$VMS_DIR"/*/)+ [[ ${#images[@]} -gt 0 ]] ||+ die "no VM under $VMS_DIR names its image; start one with 'make -C testing vm'"+ else+ select_named_run_dir+ mapfile -t images < <(vm_images "$RUN_DIR")+ [[ ${#images[@]} -gt 0 ]] ||+ die "VM '$VM_NAME' names no image: only 'make vm' names one"+ fi+ if [[ -s "$CACHE_DIR/libguestfs/supermin.env" ]]; then+ # shellcheck source=/dev/null+ . "$CACHE_DIR/libguestfs/supermin.env"+ fi+ # The SBOM is kept beside its image, so that the bytes sbom-put+ # stores once are the same at every run.+ for line in "${images[@]}"; do+ IFS=$'\t' read -r image_id image_version runtime_key <<<"$line"+ sbom="$RUNTIME_CACHE_DIR/$runtime_key.cdx.json"+ if [[ ! -s "$sbom" ]]; then+ log "writing the SBOM of runtime image $runtime_key"+ "$REPO_ROOT/testing/vm/runtime-sbom.sh" \+ "$RUNTIME_CACHE_DIR/$runtime_key.qcow2" "$sbom"+ fi+ args+=("$image_id" "$image_version" "$sbom")+ done+ exec "$REPO_ROOT/testing/vm/stack.sh" sbom "${args[@]}"+}+start_swtpm(){local _@@ -1557,6 +1610,10 @@ while [[ "$#" -gt 0 ]]; do[[ "$#" -eq 1 ]] || die "--enroll does not accept additional arguments"run_enroll;;+ --sbom)+ [[ "$#" -eq 1 ]] || die "--sbom does not accept additional arguments"+ run_sbom+ ;;--verbose)VM_VERBOSE=1shift@@ -1618,6 +1675,8 @@ KERNEL="$(resolve_kernel)"RUNTIME_KEY="$(<"$TEST_VM_HOST_BUNDLE/runtime-image-key")"ROOTFS="$RUNTIME_CACHE_DIR/$RUNTIME_KEY.qcow2"+IMAGE_VERSION=+[[ "$VM_MODE" != interactive ]] || record_image[[ "$VM_ROLE" != operator ]] || prepare_operator_image[[ "$VM_ROLE" != operator ]] ||"$REPO_ROOT/testing/vm/list-source.sh" "$RUN_DIR/result/source-files"@@ -1751,6 +1810,7 @@ printf ' export TEST_VM_SKIP_SEAL=%q\n' "$SKIP_SEAL" >>"$USER_DATA"printf ' export TEST_VM_INITRD=%q\n' "$VM_INITRD" >>"$USER_DATA"printf ' export TEST_VM_SSH_PUBLIC_KEY=%q\n' "$SSH_PUBLIC_KEY" >>"$USER_DATA"printf ' export TEST_VM_ROLE=%q\n' "$VM_ROLE" >>"$USER_DATA"+printf ' export TEST_VM_IMAGE_VERSION=%q\n' "$IMAGE_VERSION" >>"$USER_DATA"if [[ -n "$OCI_CA_DIR" ]]; thenprintf ' export TEST_VM_OCI_CA=/mnt/shirudo-oci/ca.crt\n' >>"$USER_DATA"fidiff --git a/testing/vm/runtime-sbom.sh b/testing/vm/runtime-sbom.shnew file mode 100755index 00000000..03a3587e--- /dev/null+++ b/testing/vm/runtime-sbom.sh@@ -0,0 +1,46 @@+#!/usr/bin/env bash+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# Write the CycloneDX SBOM of a prepared runtime image: its dpkg database,+# read from the image without booting it, as trivy rootfs lists it.+#+# runtime-sbom.sh <runtime image> <sbom.cdx.json>++set -euo pipefail++# shellcheck source=testing/vm/arch.sh+. "$(dirname "${BASH_SOURCE[0]}")/arch.sh"+# shellcheck source=testing/vm/guestfs.sh+. "$(dirname "${BASH_SOURCE[0]}")/guestfs.sh"++die() { printf '[test-vm-sbom] error: %s\n' "$*" >&2; exit 1; }++[[ $# -eq 2 ]] || die "usage: runtime-sbom.sh <runtime image> <sbom.cdx.json>"+IMAGE=$1+SBOM=$2+[[ -s "$IMAGE" ]] || die "runtime image is missing: $IMAGE"+command -v guestfish >/dev/null 2>&1 ||+ die "guestfish is required; install it with: $(test_vm_install_cmd "$(test_vm_pkg libguestfs-tools guestfish)")"+command -v trivy >/dev/null 2>&1 ||+ die "trivy is required: https://trivy.dev/latest/getting-started/installation/"++WORK=$(mktemp -d)+TMP_SBOM="$SBOM.tmp.$$"+trap 'rm -rf -- "$WORK" "$TMP_SBOM"' EXIT+mkdir -p "$WORK/root/etc" "$WORK/root/var/lib/dpkg"++# Trivy tells Ubuntu from Debian by lsb-release and needs no more than the+# release files and dpkg's status to list the packages.+guestfs_setup+guestfs_run guestfish --ro -a "$IMAGE" -i <<EOF ||+download /etc/os-release $WORK/root/etc/os-release+download /etc/lsb-release $WORK/root/etc/lsb-release+download /etc/debian_version $WORK/root/etc/debian_version+download /var/lib/dpkg/status $WORK/root/var/lib/dpkg/status+EOF+ die "cannot read the dpkg database of $IMAGE"++trivy rootfs --format cyclonedx --quiet --output "$TMP_SBOM" \+ "$WORK/root" </dev/null || die "trivy rootfs failed"+mv -f -- "$TMP_SBOM" "$SBOM"diff --git a/testing/vm/stack.sh b/testing/vm/stack.shindex 73fd6e7d..cffe0853 100755--- a/testing/vm/stack.sh+++ b/testing/vm/stack.sh@@ -22,7 +22,7 @@ fiexec 9>"$W/.lock"flock 9case "$TLS_DIR/" in "$REPO/"*) echo 'stack private keys must be outside the checkout' >&2; exit 1 ;; esac-command=${1:?usage: stack.sh prepare|oci|telemetry|down|enroll|xtop|vm}+command=${1:?usage: stack.sh prepare|oci|telemetry|down|enroll|sbom|xtop|vm}shiftprepare(){@@ -83,6 +83,33 @@ enroll)printf "\$ CLICKHOUSE_CLIENT='%s' tools/put-shirudo-build --note 'test-vm build' %s\n" "$client" "$*"CLICKHOUSE_CLIENT=$client tools/put-shirudo-build --note 'test-vm build' "$@";;+sbom)+ # Shown before they run, as for enroll: each image's SBOM into the+ # store, then the scan that loads what Trivy finds in them, then a+ # refresh of node_vulns, which would show them only at its next+ # scheduled one, up to 5 minutes later.+ [ $# -gt 0 ] && [ $(($# % 3)) -eq 0 ] ||+ { echo 'usage: stack.sh sbom (<image_id> <image_version> <sbom>)...' >&2; exit 2; }+ [ "$(docker container inspect -f '{{.State.Running}}' "$STACK_NAME-clickhouse" 2>/dev/null)" = true ] ||+ { echo 'the local stack is not up: run make stack-up' >&2; exit 1; }+ mkdir -p "$W/sbom"+ cd "$REPO"+ client="docker exec -i $STACK_NAME-clickhouse clickhouse-client"+ store=file://$(realpath --relative-to="$REPO" "$W/sbom")+ while [ $# -gt 0 ]; do+ sbom=$(realpath --relative-to="$REPO" "$3")+ printf '$ tools/backend/jobs/sbom-put --store %s --image-id %s --image-version %s %s\n' \+ "$store" "$1" "$2" "$sbom"+ tools/backend/jobs/sbom-put --store "$store" --image-id "$1" \+ --image-version "$2" "$sbom"+ shift 3+ done+ printf "\$ CLICKHOUSE_CLIENT='%s' tools/backend/jobs/sbom-scan --store %s\n" "$client" "$store"+ CLICKHOUSE_CLIENT=$client tools/backend/jobs/sbom-scan --store "$store"+ refresh='SYSTEM REFRESH VIEW shirudo.node_vulns_mv; SYSTEM WAIT VIEW shirudo.node_vulns_mv'+ printf "\$ %s --query '%s'\n" "$client" "$refresh"+ $client --query "$refresh" </dev/null+ ;;vm)prepareexport TEST_VM_LOCAL_STACK=1