eBPF-based security platform
keepalive: move the quote into util
John Fastabend7515642d51a6
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex e9845c94..8f44fc34 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -1219,8 +1219,8 @@ a proof covers.* - :src:`util/crypto_openssl.c`, :src:`util/curl.c`,:src:`util/elf_target.c`, :src:`util/kernel_abi.c`,:src:`util/nv_writer.c`, :src:`util/sign.c`,- :src:`util/tls_openssl.c`, :src:`util/tpm.c`,- :src:`util/upatch_decode.c`+ :src:`util/keepalive_quote.c`, :src:`util/tls_openssl.c`,+ :src:`util/tpm.c`, :src:`util/upatch_decode.c`- Mostly calls into a library a harness would have to model whole:OpenSSL, libcurl, libelf, the BTF reader of libbpf, tpm2-tss andLuaJITdiff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 53340b0b..84d9b4d6 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -610,7 +610,8 @@ The unit suites in :src:`testing/unit/` cover 29 of the modules in``bpm_tree``, ``bpm_tree_overlap``, ``bpm_tree_state``, ``chain``,``conditions``, ``curl``,``eventlog``, ``format``, ``hex``, ``io``, ``json``, ``kernel_abi``,-``keepalive``, ``kernel_build_id``, ``kernel_release``, ``measured``,+``keepalive``, ``keepalive_quote``, ``kernel_build_id``,+``kernel_release``, ``measured``,``paths``, ``string``, ``sysinfo``, ``upatch``, ``upatch_decode``,``upatch_pins``, ``upatch_resolve``, ``upatch_verify`` and ``xdp``, and``overflow`` the arithmetic of@@ -772,7 +773,7 @@ Coverage by area- ``t4104``, ``t6139``, ``t7109``, ``t8001`` to ``t8007``,``t8101`` to ``t8106``, ``t9101`` to ``t9105``. Fuzz: ``otel``,``quote-check``, ``serializer``. Unit: ``keepalive``,- ``sysinfo``+ ``keepalive_quote``, ``sysinfo``* - Decoders- ``t3002``, ``t8102``, ``t9101``. Fuzz: ``decoder``* - Tamper counters@@ -1011,7 +1012,7 @@ Parsers and units- The dm-verity superblock check in :src:`util/config_image.c` has nofuzz target. ``t5001`` covers truncation and unsupported fields.-- 40 of the 68 modules in :src:`util/` have no unit suite of their own.+- 40 of the 69 modules in :src:`util/` have no unit suite of their own.Related-------diff --git a/Makefile.shirudo b/Makefile.shirudoindex 737978bd..a645a782 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -273,6 +273,7 @@ UTIL_SRCS := \util/timefmt.c \util/conditions.c \util/keepalive.c \+ util/keepalive_quote.c \util/format.c \util/seal.c \util/serializer.c \diff --git a/builtin-status.c b/builtin-status.cindex 474423d5..b6a17f06 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -42,6 +42,7 @@#include "util/json.h"#include "util/identity.h"#include "util/keepalive.h"+#include "util/keepalive_quote.h"#include "util/image_bootstrap.h"#include "util/image_runtime.h"#include "util/keyring.h"@@ -687,66 +688,6 @@ keepalive_body(const struct conditions_input *cin,return ret;}-#define BASE64_SIZE(n) (4 * (((n) + 2) / 3) + 1)-#define KEEPALIVE_QUOTE_MAX (sizeof("attest=;sig=;ak=") + \- BASE64_SIZE(TPM_ATTEST_MAX) + \- BASE64_SIZE(TPM_SIGNATURE_MAX) + \- BASE64_SIZE(TPM_PUBLIC_MAX))--/* attest=<TPMS_ATTEST>;sig=<TPMT_SIGNATURE>;ak=<TPMT_PUBLIC>, in base64. */-static noinline_for_stack int-keepalive_quote_format(const struct tpm_attestation *att,- const struct tpm_key *ak, char *out, size_t cap)-{- char attest[BASE64_SIZE(TPM_ATTEST_MAX)];- char sig[BASE64_SIZE(TPM_SIGNATURE_MAX)];- char public[BASE64_SIZE(TPM_PUBLIC_MAX)];- int ret;-- ret = base64_encode(att->attest, att->attest_len, attest,- sizeof(attest));- if (!ret)- ret = base64_encode(att->signature, att->signature_len, sig,- sizeof(sig));- if (!ret)- ret = base64_encode(ak->public, ak->public_len, public,- sizeof(public));- if (!ret)- ret = strsprintf(out, cap, "attest=%s;sig=%s;ak=%s", attest,- sig, public);- return min(ret, 0);-}--/*- * The attestation key quotes the SHA-256 of the body over no PCR, which- * tells a keepalive quote from one of attest. The key is created for- * this quote and flushed after it.- */-static noinline_for_stack int-keepalive_quote(const char *body, size_t len, char *quote, size_t cap)-{- struct tpm_key ak = { .handle = 0 };- struct tpm_attestation att = {};- __u8 digest[CRYPTO_SHA256_LEN];- struct tpm *tpm = NULL;- int ret;-- ret = sha256_buffer(body, len, digest);- if (ret)- return ret;- ret = tpm_open(&tpm);- if (ret)- return -ENXIO;- ret = tpm_ak_create(tpm, &ak);- if (!ret)- ret = tpm_quote(tpm, &ak, digest, sizeof(digest), 0, &att);- if (!ret)- ret = keepalive_quote_format(&att, &ak, quote, cap);- tpm_key_flush(tpm, &ak);- tpm_close(tpm);- return ret;-}-/** One OTLP log: the body as a string and its quote as the one attribute.* Without a quote nothing is sent: the backend has no use for adiff --git a/testing/unit/keepalive_quote.c b/testing/unit/keepalive_quote.cnew file mode 100644index 00000000..32fa833c--- /dev/null+++ b/testing/unit/keepalive_quote.c@@ -0,0 +1,70 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <errno.h>+#include <string.h>++#include "keepalive_quote.h"+#include "unit.h"++static void quote_is_the_three_fields_in_base64(void)+{+ struct tpm_attestation att = {+ .attest = { 0xff, 0x54, 0x43 },+ .attest_len = 3,+ .signature = { 0x00, 0x18 },+ .signature_len = 2,+ };+ struct tpm_key ak = {+ .public = { 0x00, 0x23, 0x00, 0x0b },+ .public_len = 4,+ };+ char out[KEEPALIVE_QUOTE_MAX];++ ASSERT_EQ(keepalive_quote_format(&att, &ak, out, sizeof(out)), 0);+ EXPECT_STREQ(out, "attest=/1RD;sig=ABg=;ak=ACMACw==");+}++static void quote_of_the_largest_fields_fits(void)+{+ struct tpm_attestation att = {+ .attest_len = TPM_ATTEST_MAX,+ .signature_len = TPM_SIGNATURE_MAX,+ };+ struct tpm_key ak = { .public_len = TPM_PUBLIC_MAX };+ char out[KEEPALIVE_QUOTE_MAX];++ ASSERT_EQ(keepalive_quote_format(&att, &ak, out, sizeof(out)), 0);+ EXPECT_UEQ(strlen(out) + 1, KEEPALIVE_QUOTE_MAX - 3);+}++static void quote_refuses_a_buffer_one_byte_short(void)+{+ struct tpm_attestation att = {+ .attest = { 0xff, 0x54, 0x43 },+ .attest_len = 3,+ .signature = { 0x00, 0x18 },+ .signature_len = 2,+ };+ struct tpm_key ak = {+ .public = { 0x00, 0x23, 0x00, 0x0b },+ .public_len = 4,+ };+ const char *want = "attest=/1RD;sig=ABg=;ak=ACMACw==";+ char out[64];++ EXPECT_EQ(keepalive_quote_format(&att, &ak, out, strlen(want)),+ -E2BIG);+ EXPECT_EQ(keepalive_quote_format(&att, &ak, out, strlen(want) + 1),+ 0);+ EXPECT_STREQ(out, want);+}++static const struct unit_case cases[] = {+ UNIT_CASE(quote_is_the_three_fields_in_base64),+ UNIT_CASE(quote_of_the_largest_fields_fits),+ UNIT_CASE(quote_refuses_a_buffer_one_byte_short),+ {}+};++UNIT_SUITE(keepalive_quote, cases);diff --git a/util/keepalive_quote.c b/util/keepalive_quote.cnew file mode 100644index 00000000..eb3e4223--- /dev/null+++ b/util/keepalive_quote.c@@ -0,0 +1,61 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <shirudo/misc.h>+#include <shirudo/string.h>++#include "crypto.h"+#include "keepalive_quote.h"+#include "tpm.h"++noinline_for_stack int+keepalive_quote_format(const struct tpm_attestation *att,+ const struct tpm_key *ak, char *out, size_t cap)+{+ char attest[KEEPALIVE_B64_SIZE(TPM_ATTEST_MAX)];+ char sig[KEEPALIVE_B64_SIZE(TPM_SIGNATURE_MAX)];+ char public[KEEPALIVE_B64_SIZE(TPM_PUBLIC_MAX)];+ int ret;++ ret = base64_encode(att->attest, att->attest_len, attest,+ sizeof(attest));+ if (!ret)+ ret = base64_encode(att->signature, att->signature_len, sig,+ sizeof(sig));+ if (!ret)+ ret = base64_encode(ak->public, ak->public_len, public,+ sizeof(public));+ if (!ret)+ ret = strsprintf(out, cap, "attest=%s;sig=%s;ak=%s", attest,+ sig, public);+ return min(ret, 0);+}++/*+ * The quote is over no PCR, which tells a keepalive quote from one of+ * attest. The key is created for this quote and flushed after it.+ */+noinline_for_stack int+keepalive_quote(const char *body, size_t len, char *quote, size_t cap)+{+ struct tpm_key ak = {};+ struct tpm_attestation att = {};+ __u8 digest[CRYPTO_SHA256_LEN];+ struct tpm *tpm = NULL;+ int ret;++ ret = sha256_buffer(body, len, digest);+ if (ret)+ return ret;+ ret = tpm_open(&tpm);+ if (ret)+ return ret;+ ret = tpm_ak_create(tpm, &ak);+ if (!ret)+ ret = tpm_quote(tpm, &ak, digest, sizeof(digest), 0, &att);+ if (!ret)+ ret = keepalive_quote_format(&att, &ak, quote, cap);+ tpm_key_flush(tpm, &ak);+ tpm_close(tpm);+ return ret;+}diff --git a/util/keepalive_quote.h b/util/keepalive_quote.hnew file mode 100644index 00000000..c7bb6761--- /dev/null+++ b/util/keepalive_quote.h@@ -0,0 +1,42 @@+/* SPDX-License-Identifier: BSD-2-Clause */+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The shirudo.quote of a keepalive: the attestation key's quote of the+ * SHA-256 of the body, as the attest, sig and ak fields in base64.+ */++#ifndef SHIRUDO_KEEPALIVE_QUOTE_H+#define SHIRUDO_KEEPALIVE_QUOTE_H++#include <stddef.h>++#include <shirudo/misc.h>++#include "tpm.h"++#define KEEPALIVE_B64_SIZE(n) (4 * (((n) + 2) / 3) + 1)++/* Holds the longest quote and its terminator. */+#define KEEPALIVE_QUOTE_MAX (sizeof("attest=;sig=;ak=") + \+ KEEPALIVE_B64_SIZE(TPM_ATTEST_MAX) + \+ KEEPALIVE_B64_SIZE(TPM_SIGNATURE_MAX) + \+ KEEPALIVE_B64_SIZE(TPM_PUBLIC_MAX))++/*+ * Writes attest=<TPMS_ATTEST>;sig=<TPMT_SIGNATURE>;ak=<TPMT_PUBLIC> in+ * padded base64 with its terminator. -E2BIG when cap does not hold it.+ */+int keepalive_quote_format(const struct tpm_attestation *att,+ const struct tpm_key *ak, char *out,+ size_t cap) __must_check;++/*+ * Quotes the SHA-256 of the body with the attestation key of+ * tpm_ak_create() over no PCR and writes the quote as+ * keepalive_quote_format() does.+ */+int keepalive_quote(const char *body, size_t len, char *quote,+ size_t cap) __must_check;++#endif /* SHIRUDO_KEEPALIVE_QUOTE_H */