eBPF-based security platform
testing: quote the keepalive fixture bodies
John Fastabendb75c158fbfc7
diff --git a/.gitignore b/.gitignoreindex eb635f20..bb538fb6 100644--- a/.gitignore+++ b/.gitignore@@ -65,6 +65,7 @@!/testing/helper/test-bpm-key.py!/testing/helper/test-bpm-info.py!/testing/helper/test-seal-seccomp.py+!/testing/helper/capture-keepalive-quotes.sh# libFuzzer targets and the inputs that crashed them/testing/fuzz/fuzz-*diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 84d9b4d6..cb5aacb1 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -265,6 +265,14 @@ signed config image.An unsalted negative control accepts a forged acknowledgmentwith an empty or SHA-256 response HMAC; malformed lengths are refused.Salted sessions reject it and leave the real measured value unchanged+ * - ``t4006-keepalive-quotes``+ - The quotes of :src:`testing/fixtures/keepalive/quotes.tsv`: each+ row's ``body_sha256`` is SHA-256 of its body file, and fails on+ a body altered by one byte; every row verifies under+ ``shirudo-quote-check`` without a TPM, node A's rows under one+ key and node B's under another; a quote over an altered body is+ ``body_mismatch``; with the libtpms simulator built, the capture+ script rebuilds a file that verifies* - ``t4101-initrd``- The production configuration: ``init`` from the initrd, adoption,PCR 11 after the binding, pins under ``/run``@@ -770,9 +778,9 @@ Coverage by areaUnauthorized policy-xattr changes: ``t6111``. Task and socketinheritance: ``t6141``, ``t6142``* - Telemetry- - ``t4104``, ``t6139``, ``t7109``, ``t8001`` to ``t8007``,- ``t8101`` to ``t8106``, ``t9101`` to ``t9105``. Fuzz: ``otel``,- ``quote-check``, ``serializer``. Unit: ``keepalive``,+ - ``t4006``, ``t4104``, ``t6139``, ``t7109``, ``t8001`` to+ ``t8007``, ``t8101`` to ``t8106``, ``t9101`` to ``t9105``. Fuzz:+ ``otel``, ``quote-check``, ``serializer``. Unit: ``keepalive``,``keepalive_quote``, ``sysinfo``* - Decoders- ``t3002``, ``t8102``, ``t9101``. Fuzz: ``decoder``diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex a50d7e6c..c9126427 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -788,6 +788,19 @@ Add a fixture or a helperhelper that a target test runs also goes in the list of helpers that:src:`testing/vm/build-bundle.sh` copies into the bundle. A Pythonhelper needs none of this.+- The keepalive bodies in :src:`testing/fixtures/keepalive/` carry real+ quotes in ``quotes.tsv``: per row the body's file name, then the+ ``attest``, ``sig``, ``ak`` and ``body_sha256`` columns of+ ``shirudo-quote-check``, so ``cut -f2-`` feeds the checker. A change+ to a node body fails ``t4006`` until+ :src:`testing/helper/capture-keepalive-quotes.sh` rebuilds the file.+ It quotes each body with ``test-keepalive-quote`` under the libtpms+ simulator, as ``status --otel`` quotes its own, with one TPM state+ for node A and another for node B; it needs no root and no VM, only+ ``make -C testing test-helpers`` with libtpms installed. Each run+ starts from fresh TPM states, so both the quotes and the two+ attestation keys change; a state directory named as its second+ argument keeps the keys.- A fuzz target goes in ``testing/fuzz/fuzz-<name>.c`` with a seedcorpus in ``testing/fuzz/corpus/<name>/`` and a rule in:src:`Makefile.shirudo` that names the sources it links.diff --git a/Makefile.shirudo b/Makefile.shirudoindex a645a782..32e21928 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -736,6 +736,7 @@ TEST_HELPERS := $(VERITY_TEST_HELPERS) \TEST_HELPERS += testing/helper/test-nv-writerTEST_HELPERS += testing/helper/test-keyring-sealTEST_HELPERS += testing/helper/test-tpm-preopened+TEST_HELPERS += testing/helper/test-keepalive-quotetesting/helper/test-conditions: testing/helper/test-conditions.c util/conditions.o \util/string.o util/malloc.o@@ -818,6 +819,11 @@ testing/helper/test-register-cli: testing/helper/test-register-cli.c \@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^ $(STATIC_LIBS) $(DYNAMIC_LIBS)+testing/helper/test-keepalive-quote: testing/helper/test-keepalive-quote.c \+ util/keepalive_quote.o $(REGISTER_OBJS)+ @printf ' CC $@\n'+ $(Q)$(CC) $(CFLAGS) -o $@ $^ $(REGISTER_LIBS)+testing/helper/tpmsim-cmd: testing/helper/tpmsim-cmd.c@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS_BASE) -Iinclude $(TPMSIM_CFLAGS) -o $@ $< $(TPMSIM_LIBS) -lcryptodiff --git a/testing/fixtures/keepalive/quotes.tsv b/testing/fixtures/keepalive/quotes.tsvnew file mode 100644index 00000000..83312772--- /dev/null+++ b/testing/fixtures/keepalive/quotes.tsv@@ -0,0 +1,4 @@+node-a-1.0.0.json /1RDR4AYACIAC8yPm8NCFaXsGeNm/K5lH8ACW0CXacA/nNWfo6e7qMeyACAhsWqUASTRY7vfejfm43Jxg617fCsCuX5mbvbT2NBw/QAAAAAAAAALAAAAAAAAAAABICQBJQASAAAAAAABAAsDAAAAACDjsMRCmPwcFJr79MiZb7kkJ65B5GSbk0yklZkbeFK4VQ== ABgACwAgWR618d+eEkNNFV2EJULBd96BPL4MvGAtbOkW0bQESfQAIGGdJ6DbYzm1g+NM34GF5tlHdnPHHpq4am/5Pp35Zdc+ ACMACwAFAHIAAAAQABgACwADABAAINjwQ/NaFNgV6q2W2e3yjYbJ4ABIMXzcXhBi61m8A45VACAOFYuhumBSuWOm+gTbLci3cOAv6kMMeeDtSgNruFzoYg== 21b16a940124d163bbdf7a37e6e3727183ad7b7c2b02b97e666ef6d3d8d070fd+node-a-1.1.0.json /1RDR4AYACIAC8yPm8NCFaXsGeNm/K5lH8ACW0CXacA/nNWfo6e7qMeyACAMwOvKIObRbmJce9O0Bma1qO/9eZPQ9EiVg5APy1pHuwAAAAAAAAApAAAAAAAAAAABICQBJQASAAAAAAABAAsDAAAAACDjsMRCmPwcFJr79MiZb7kkJ65B5GSbk0yklZkbeFK4VQ== ABgACwAgHoyYivLSNxJT1wiYTwNdiOxKo43aqHAg4K3ZTp++KlQAIAIO8BClMsmcUAGpG6NgKILeKiW6UP9M+GJFAEGO4rdL ACMACwAFAHIAAAAQABgACwADABAAINjwQ/NaFNgV6q2W2e3yjYbJ4ABIMXzcXhBi61m8A45VACAOFYuhumBSuWOm+gTbLci3cOAv6kMMeeDtSgNruFzoYg== 0cc0ebca20e6d16e625c7bd3b40666b5a8effd7993d0f4489583900fcb5a47bb+node-b-1.0.0.json /1RDR4AYACIAC3zsC1hrNOKNJtq/X6oDmJsgGRhVvRnRL51e1PJIMu1QACDZVIahNp2Vs7w3ISLr02EEwednQxrbkTmQifWaQTM0qAAAAAAAAAAOAAAAAAAAAAABICQBJQASAAAAAAABAAsDAAAAACDjsMRCmPwcFJr79MiZb7kkJ65B5GSbk0yklZkbeFK4VQ== ABgACwAgKQu59K2fbLLKYUcSkNifr2Na8mme6TtOVfNRP4l1umsAILuNXd676wWyywtVez0OBoFnZKQIlCmPMLFYCLjFGmwI ACMACwAFAHIAAAAQABgACwADABAAILc+BViAIEdI2pt2dzBSRwdUc00zfA/B8vjrJMlWp39JACCDBaDtUnqWWQX/jPv467uHo5IM1Uwf8TL7pkmlCaFlgg== d95486a1369d95b3bc372122ebd36104c1e767431adb91399089f59a413334a8+node-b-1.1.0.json /1RDR4AYACIAC3zsC1hrNOKNJtq/X6oDmJsgGRhVvRnRL51e1PJIMu1QACCKGBX9S8UMrDh/Y7313vAX93CsHbII4n/1OkFWnoXIvAAAAAAAAAAqAAAAAAAAAAABICQBJQASAAAAAAABAAsDAAAAACDjsMRCmPwcFJr79MiZb7kkJ65B5GSbk0yklZkbeFK4VQ== ABgACwAgd+ReAli4/55LlvN5tBABjypR6HNjH+sV4WVFQHI/IzUAILADwcKlvrIF4otoRR26MxAIrgHkY0nBVcw5ywxq6Vpr ACMACwAFAHIAAAAQABgACwADABAAILc+BViAIEdI2pt2dzBSRwdUc00zfA/B8vjrJMlWp39JACCDBaDtUnqWWQX/jPv467uHo5IM1Uwf8TL7pkmlCaFlgg== 8a1815fd4bc50cac387f63bdf5def017f770ac1db208e27ff53a41569e85c8bcdiff --git a/testing/functional/tpm/t4006-keepalive-quotes.sh b/testing/functional/tpm/t4006-keepalive-quotes.shnew file mode 100755index 00000000..fc0ee0c0--- /dev/null+++ b/testing/functional/tpm/t4006-keepalive-quotes.sh@@ -0,0 +1,79 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t4006 -- the quotes of testing/fixtures/keepalive/quotes.tsv: each row+# covers its body file as it is now, verifies under shirudo-quote-check+# without a TPM, node A and node B each sign with a key of their own, and+# the capture script rebuilds a file that verifies as well.++cd "$(dirname "$0")/../.."+. ./test-lib.sh++QUOTE_CHECK=${QUOTE_CHECK:-../tools/quote-check/shirudo-quote-check}+[ -x "$QUOTE_CHECK" ] || test_skip_all "shirudo-quote-check is not built"+FIXTURES=fixtures/keepalive+QUOTES=$FIXTURES/quotes.tsv+BODIES="node-a-1.0.0.json node-a-1.1.0.json node-b-1.0.0.json node-b-1.1.0.json"+W=$(mktemp -d) || exit 1+trap 'rm -rf "$W"' EXIT++# Fails when a row of quotes file $1 names a body that is not in+# directory $2 or a body_sha256 that is not SHA-256 of that file.+rows_match_bodies() {+ [ "$(cut -f1 "$1" | tr '\n' ' ')" = "$BODIES " ] || return 1+ while IFS=' ' read -r name _ _ _ hash; do+ [ "$(sha256sum <"$2/$name" | cut -d' ' -f1)" = "$hash" ] ||+ return 1+ done <"$1"+}++# Checks quotes file $1 with the checker, its lines in $2: every row+# passes, node A's two rows under one key and node B's under another.+check_quotes() {+ cut -f2- "$1" | "$QUOTE_CHECK" >"$2" &&+ [ "$(wc -l <"$2")" -eq 4 ] &&+ ! grep -v '^{"ok":1,' "$2" &&+ sed -n 's/.*"ak_name":"\([0-9a-f]*\)".*/\1/p' "$2" >"$2.names" &&+ [ "$(sed -n 1p "$2.names")" = "$(sed -n 2p "$2.names")" ] &&+ [ "$(sed -n 3p "$2.names")" = "$(sed -n 4p "$2.names")" ] &&+ [ "$(sed -n 1p "$2.names")" != "$(sed -n 3p "$2.names")" ] &&+ [ "$(sort -u "$2.names" | wc -l)" -eq 2 ]+}++test_expect_success 'every row covers its body file as it is now' '+ rows_match_bodies "$QUOTES" "$FIXTURES"+'++test_expect_success 'a body altered by one byte fails the staleness check' '+ mkdir "$W/stale" &&+ for b in $BODIES; do cp "$FIXTURES/$b" "$W/stale/" || exit 1; done &&+ sed -i "s/\"verdict\":\"ok\"/\"verdict\":\"oK\"/" \+ "$W/stale/node-b-1.1.0.json" &&+ ! cmp -s "$FIXTURES/node-b-1.1.0.json" "$W/stale/node-b-1.1.0.json" &&+ ! rows_match_bodies "$QUOTES" "$W/stale"+'++test_expect_success 'every row verifies, with one key per node' '+ check_quotes "$QUOTES" "$W/checked"+'++test_expect_success 'a body altered by one byte does not match its quote' '+ sha=$(sed "s/\"verdict\":\"ok\"/\"verdict\":\"oK\"/" \+ "$FIXTURES/node-a-1.0.0.json" | sha256sum | cut -d" " -f1) &&+ [ "$sha" != "$(sed -n 1p "$QUOTES" | cut -f5)" ] &&+ sed -n 1p "$QUOTES" | cut -f2-4 >"$W/altered.row" &&+ sed -i "s/\$/ $sha/" "$W/altered.row" &&+ "$QUOTE_CHECK" <"$W/altered.row" >"$W/altered.out" &&+ [ "$(cat "$W/altered.out")" = "{\"ok\":0,\"reason\":\"body_mismatch\"}" ]+'++if [ -x helper/tpmsim-cmd ] && [ -x helper/test-keepalive-quote ]; then+ test_expect_success 'the capture script rebuilds quotes that verify' '+ helper/capture-keepalive-quotes.sh "$W/recaptured.tsv" &&+ rows_match_bodies "$W/recaptured.tsv" "$FIXTURES" &&+ check_quotes "$W/recaptured.tsv" "$W/rechecked"+ '+fi++test_donediff --git a/testing/helper/capture-keepalive-quotes.sh b/testing/helper/capture-keepalive-quotes.shnew file mode 100755index 00000000..04f6b8c9--- /dev/null+++ b/testing/helper/capture-keepalive-quotes.sh@@ -0,0 +1,45 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# Rebuilds testing/fixtures/keepalive/quotes.tsv, or the file named first,+# by quoting each node body under the libtpms simulator: one TPM state+# for node A and another for node B, under the directory named second or+# a fresh one, so the two nodes have different attestation keys.++set -eu++HELPER=$(cd "$(dirname "$0")" && pwd)+FIXTURES=$HELPER/../fixtures/keepalive+OUT=${1:-$FIXTURES/quotes.tsv}+STATE=${2:-}+BODIES="node-a-1.0.0 node-a-1.1.0 node-b-1.0.0 node-b-1.1.0"++for tool in "$HELPER/tpmsim-cmd" "$HELPER/test-keepalive-quote"; do+ if [ ! -x "$tool" ]; then+ echo "$tool is not built: make -C testing test-helpers" \+ "(needs libtpms)" >&2+ exit 1+ fi+done++FRESH=+if [ -z "$STATE" ]; then+ STATE=$(mktemp -d)+ FRESH=$STATE+fi+ROWS=$(mktemp "$OUT.XXXXXX")+cleanup() {+ rm -f "$ROWS"+ [ -z "$FRESH" ] || rm -rf "$FRESH"+}+trap cleanup EXIT++for body in $BODIES; do+ node=${body#node-}+ node=${node%%-*}+ mkdir -p "$STATE/node-$node"+ SHIRUDO_TPM_TCTI="cmd:exec $HELPER/tpmsim-cmd $STATE/node-$node" \+ "$HELPER/test-keepalive-quote" "$FIXTURES/$body.json" >>"$ROWS"+done+mv "$ROWS" "$OUT"diff --git a/testing/helper/test-keepalive-quote.c b/testing/helper/test-keepalive-quote.cnew file mode 100644index 00000000..ee91847e--- /dev/null+++ b/testing/helper/test-keepalive-quote.c@@ -0,0 +1,101 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * Quotes a keepalive body file with the TPM as status --otel quotes its+ * own body, and prints the row of testing/fixtures/keepalive/quotes.tsv:+ * the file name, attest, sig, ak and body_sha256, tab-separated.+ */++#include <errno.h>+#include <stdio.h>+#include <string.h>++#include <shirudo/misc.h>++#include "crypto.h"+#include "hex.h"+#include "io.h"+#include "keepalive.h"+#include "keepalive_quote.h"+#include "malloc.h"++#define QUOTE_FIELDS 3++/* Turns the fields of attest=...;sig=...;ak=... into their values. */+static int quote_values(char *quote, const char *values[QUOTE_FIELDS])+{+ static const char *const keys[QUOTE_FIELDS] = {+ "attest=", "sig=", "ak=",+ };+ char *field = quote;+ size_t i;++ for (i = 0; i < QUOTE_FIELDS; i++) {+ size_t n = strlen(keys[i]);+ char *end;++ if (strncmp(field, keys[i], n))+ return -EINVAL;+ values[i] = field + n;+ end = strchr(field + n, ';');+ if (i + 1 == QUOTE_FIELDS)+ return end ? -EINVAL : 0;+ if (!end)+ return -EINVAL;+ *end = '\0';+ field = end + 1;+ }+ return -EINVAL;+}++static int print_row(const char *path, const char *body, size_t len)+{+ char hash[2 * CRYPTO_SHA256_LEN + 1];+ char quote[KEEPALIVE_QUOTE_MAX];+ const char *values[QUOTE_FIELDS];+ __u8 digest[CRYPTO_SHA256_LEN];+ const char *name;+ int ret;++ ret = keepalive_quote(body, len, quote, sizeof(quote));+ if (ret) {+ fprintf(stderr, "quote %s: %s\n", path, strerror(-ret));+ return ret;+ }+ ret = quote_values(quote, values);+ if (ret) {+ fprintf(stderr, "quote %s: not attest=;sig=;ak=\n", path);+ return ret;+ }+ ret = sha256_buffer(body, len, digest);+ if (ret)+ return ret;+ hex_encode(digest, sizeof(digest), hash);+ name = strrchr(path, '/');+ name = name ? name + 1 : path;+ if (printf("%s\t%s\t%s\t%s\t%s\n", name, values[0], values[1],+ values[2], hash) < 0 || fflush(stdout))+ return -EIO;+ return 0;+}++int main(int argc, char **argv)+{+ char *body = NULL;+ size_t len = 0;+ int ret;++ if (argc != 2) {+ fprintf(stderr, "usage: %s <body>\n", argv[0]);+ return 2;+ }+ ret = read_file(argv[1], KEEPALIVE_BODY_MAX - 1, &body, &len);+ if (ret) {+ fprintf(stderr, "read %s: %s\n", argv[1], strerror(-ret));+ return 1;+ }+ ret = print_row(argv[1], body, len);+ xfree(body, len + 1);+ return ret ? 1 : 0;+}