Skip to content

eBPF-based security platform

keepalive: emit the body a node's TPM quotes

John Fastabend5bd2af844c7d
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index bbfab7e1..7b040bfd 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -693,6 +693,18 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- Every sequence of up to 4 calls, every depth
- Valid JSON by a byte-level recognizer; a comma only after a
member; ``-E2BIG`` past the deepest level
+ * - ``keepalive-emit``
+ - ``keepalive_emit()``
+ - Three fixed bodies, without a tree, with a tree and nothing else
+ that may be null, and with every value; the last of these with a
+ last number of up to 4 digits in a buffer that holds 0 to 3 of
+ them; rooms of 0, 1 and 208 bytes; a missing string, a bad hash
+ and a byte that is not UTF-8
+ - Each body one JSON object of 22 members by a byte-level
+ recognizer, terminated in the buffer; another last number
+ changes only its digits, without a leading zero, or is
+ ``-ENOSPC`` exactly when it does not fit; ``-EINVAL`` and
+ ``-EILSEQ`` for the bad inputs, and no body then
* - ``otel-header-fields``
- The OTLP header checks and ``percent_decode()``
- Strings up to 32 bytes
diff --git a/testing/proofs/cbmc/keepalive-emit.c b/testing/proofs/cbmc/keepalive-emit.c
new file mode 100644
index 00000000..e64576e6
--- /dev/null
+++ b/testing/proofs/cbmc/keepalive-emit.c
@@ -0,0 +1,292 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * keepalive_emit() of util/keepalive.c: three bodies, each one JSON
+ * object of 22 members; a last number of up to LEN + 1 digits in a room
+ * for LEN changes only its digits, or is -ENOSPC exactly when too long;
+ * a bad input, a room cut short or a string not UTF-8 leaves no body.
+ */
+
+#include "proof.h"
+#include "../../../util/malloc.c"
+#include "../../../util/string.c"
+#include "../../../util/hex.c"
+#include "../../../util/json.c"
+#include "../../../util/keepalive.c"
+
+#define HASH \
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+#define NOT_A_HASH "sha256:0123456789abcdef"
+#define MEMBERS 22
+/* Past the longest of these bodies, and a room inside the shortest. */
+#define ROOM 832
+#define CUT_SHORT (ROOM / 4)
+
+enum state { START, KEY, STRING, COLON, VALUE, ELEMENT, ZERO, DIGITS, WORD,
+ NEXT, END };
+
+static const __u64 counters[TAMPER_MAX];
+static char reference[ROOM];
+
+/* keepalive_mask() is not reached: the mask comes in as text. */
+int base64_encode(const __u8 *data, size_t len, char *out, size_t out_sz)
+{
+ __CPROVER_assert(false, "no base64 in the emitter");
+ return -EINVAL;
+}
+
+static bool number_start(__u8 c, enum state *s)
+{
+ if (c < '0' || c > '9')
+ return false;
+ *s = c == '0' ? ZERO : DIGITS;
+ return true;
+}
+
+/*
+ * The members of one object of ECMA-404 without white space, whose values
+ * are ASCII strings without escapes, null, unsigned integers and arrays
+ * of those integers: a subset of the grammar, so what it takes is JSON.
+ * -1 for any other text.
+ */
+static int json_members(const char *t, size_t n)
+{
+ bool key = false, array = false;
+ enum state s = START;
+ size_t i, word = 0;
+ int members = 1;
+ __u8 c;
+
+ for (i = 0; i < n; i++) {
+ c = t[i];
+ switch (s) {
+ case START:
+ if (c != '{')
+ return -1;
+ s = KEY;
+ continue;
+ case KEY:
+ if (c != '"')
+ return -1;
+ key = true;
+ s = STRING;
+ continue;
+ case STRING:
+ if (c == '"')
+ s = key ? COLON : NEXT;
+ else if (c < 0x20 || c == '\\' || c >= 0x80)
+ return -1;
+ continue;
+ case COLON:
+ if (c != ':')
+ return -1;
+ key = false;
+ s = VALUE;
+ continue;
+ case WORD:
+ if (c != "null"[word++])
+ return -1;
+ if (word == 4)
+ s = NEXT;
+ continue;
+ case VALUE:
+ if (c == '"') {
+ s = STRING;
+ } else if (c == 'n') {
+ word = 1;
+ s = WORD;
+ } else if (c == '[') {
+ array = true;
+ s = ELEMENT;
+ } else if (!number_start(c, &s)) {
+ return -1;
+ }
+ continue;
+ case ELEMENT:
+ if (!number_start(c, &s))
+ return -1;
+ continue;
+ case DIGITS:
+ if (c >= '0' && c <= '9')
+ continue;
+ break;
+ case END:
+ return -1;
+ default:
+ break;
+ }
+ if (c == ',') {
+ s = array ? ELEMENT : KEY;
+ members += !array;
+ } else if (c == ']' && array) {
+ array = false;
+ s = NEXT;
+ } else if (c == '}' && !array) {
+ s = END;
+ } else {
+ return -1;
+ }
+ }
+ return s == END ? members : -1;
+}
+
+static struct keepalive_input without_a_tree(void)
+{
+ return (struct keepalive_input){
+ .boot_id = "b",
+ .version_id = HASH,
+ .kernel_release = "k",
+ .verdict = "init-pending",
+ .flags = 8,
+ .set_sha256 = HASH,
+ };
+}
+
+static struct keepalive_input with_a_tree_alone(void)
+{
+ struct keepalive_input in = without_a_tree();
+
+ in.config_root = "";
+ in.image_id = "";
+ in.tree = true;
+ in.tree_id = "t";
+ in.base_root = "";
+ in.projection_root = HASH;
+ in.mask = "";
+ return in;
+}
+
+static struct keepalive_input with_every_value(void)
+{
+ return (struct keepalive_input){
+ .boot_id = "b",
+ .version_id = HASH,
+ .config_root = HASH,
+ .kernel_release = "k",
+ .image_id = "i",
+ .image_version = "v",
+ .image_root = HASH,
+ .verdict = "ok",
+ .cond = 5461,
+ .flags = 63,
+ .tamper = counters,
+ .tree = true,
+ .tree_id = "t",
+ .generation = 42,
+ .last_seq = 12,
+ .base_root = HASH,
+ .projection_root = HASH,
+ .mask = "WdlZAA==",
+ .set_sha256 = HASH,
+ .count = 7,
+ };
+}
+
+static __u64 power_of_ten(size_t digits)
+{
+ __u64 power = 1;
+ size_t i;
+
+ for (i = 0; i < digits; i++)
+ power *= 10;
+ return power;
+}
+
+/*
+ * Each byte of these bodies is a constant: past a symbolic one the
+ * recognizer would branch at every byte to the end of its room.
+ */
+static void is_an_object(const struct keepalive_input *in)
+{
+ char *buf = any_bytes(ROOM);
+ size_t len;
+
+ __CPROVER_assert(!keepalive_emit(in, buf, ROOM, &len) && len < ROOM &&
+ !buf[len], "the body fits, terminated");
+ __CPROVER_assert(json_members(buf, len) == MEMBERS,
+ "one JSON object of 22 members");
+}
+
+/*
+ * The body whose last number is 0, an object as is_an_object() shows, is
+ * the reference: with any other number only the digits before its close
+ * differ, and they are that number's, so that the body stays an object.
+ * The room ends the buffer, so that a write past it fails the proof.
+ */
+static void ends_in_any_number(struct keepalive_input in)
+{
+ size_t prefix, cap, len, digits, i;
+ __u64 number, value = 0;
+ char *buf;
+ int ret;
+
+ in.not_ok = 0;
+ __CPROVER_assert(!keepalive_emit(&in, reference, ROOM, &len),
+ "the reference fits");
+ prefix = len - 2;
+ cap = len + LEN;
+ __CPROVER_assume(number < power_of_ten(LEN + 1));
+ for (digits = 1; number >= power_of_ten(digits); digits++)
+ ;
+ in.not_ok = number;
+ buf = any_bytes(cap);
+ ret = keepalive_emit(&in, buf, cap, &len);
+ __CPROVER_assert((ret == -ENOSPC) == (digits > LEN),
+ "-ENOSPC exactly for a number too long");
+ if (ret) {
+ __CPROVER_assert(ret == -ENOSPC && !len && !buf[0],
+ "a refusal leaves no body");
+ return;
+ }
+ __CPROVER_assert(len == prefix + digits + 1 && buf[len - 1] == '}' &&
+ !buf[len], "the digits, the close, the terminator");
+ for (i = 0; i < prefix; i++)
+ __CPROVER_assert(buf[i] == reference[i],
+ "the body before the number unchanged");
+ __CPROVER_assert(digits == 1 || buf[prefix] != '0', "no leading zero");
+ for (i = prefix; i < prefix + digits; i++) {
+ __CPROVER_assert(buf[i] >= '0' && buf[i] <= '9',
+ "decimal digits");
+ value = value * 10 + (__u64)(buf[i] - '0');
+ }
+ __CPROVER_assert(value == number, "the number's own digits");
+}
+
+static void refuses(const struct keepalive_input *in, size_t cap, int want)
+{
+ char *buf = cap ? any_bytes(cap) : NULL;
+ size_t len;
+ int ret;
+
+ ret = keepalive_emit(in, buf, cap, &len);
+ __CPROVER_assert(ret == want,
+ "-ENOSPC for the room, else for the input");
+ __CPROVER_assert(!len && (!cap || !buf[0]), "a refusal leaves no body");
+}
+
+void harness(void)
+{
+ struct keepalive_input in = without_a_tree();
+
+ ends_in_any_number(with_every_value());
+ if (LEN)
+ return;
+ refuses(&in, 0, -ENOSPC);
+ refuses(&in, 1, -ENOSPC);
+ refuses(&in, CUT_SHORT, -ENOSPC);
+ in.kernel_release = "\xff";
+ refuses(&in, ROOM, -EILSEQ);
+ in.boot_id = NULL;
+ refuses(&in, ROOM, -EINVAL);
+ in = with_a_tree_alone();
+ in.projection_root = NOT_A_HASH;
+ refuses(&in, ROOM, -EINVAL);
+
+ in = without_a_tree();
+ is_an_object(&in);
+ in = with_a_tree_alone();
+ is_an_object(&in);
+ in = with_every_value();
+ is_an_object(&in);
+}
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 86815566..6a0daec1 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -55,6 +55,7 @@ log-escape --unwind 7 LEN=0-5
json-emit-string --unwind 5 --unwindset json_string.0:27,put_string_n.0:5,put_raw.0:7,well_formed.0:27,replaced.0:5,memchr.0:9,memcmp.0:13 LEN=0-4
json-emit-buffer --unwind 1 --unwindset json_raw_value.0:5,harness.0:5 --malloc-may-fail --malloc-fail-null LEN=0-4
json-emit-nest --object-bits 12 --unwind 1 --unwindset json_text.0:57,sequences.0:4,sequences.1:5,any_depth.0:4,any_depth.1:4,strcmp.0:12,memchr.0:9,put_string_n.0:2,put_string_n.1:2,put_raw.0:8,json_kv_raw.0:3,json_raw_value.0:3,json_raw.0:5,strlen.0:3,vsnprintf.2:5,vsnprintf.0:2,format_number.0:3,format_number.2:5,format_number.3:3
+keepalive-emit -I ../../.. --object-bits 12 --unwind 1 --unwindset json_members.0:833,power_of_ten.0:6,put_string_n.0:5,put_string_n.1:66,put_raw.0:7,strlen.0:66,is_hash.0:66,kv_tamper.0:38,kv_no_tree.0:8,json_raw_value.0:6,vsnprintf.2:4,vsnprintf.0:2,format_number.0:6,format_number.2:5,format_number.3:6,ends_in_any_number.0:6,ends_in_any_number.1:833,ends_in_any_number.2:5 LEN=0-3
json-string --unwind 5 --unwindset json_string.0:7,parse_string.0:5,parse_string.1:7,memchr.0:9,memcmp.0:7,harness.0:8 LEN=0-6
json-escape --unwind 5 --unwindset parse_string.0:4,memcmp.0:9
json-roundtrip --unwind 1 --unwindset put_string_n.0:5,put_string_n.1:5,put_raw.0:7,vsnprintf.2:4,vsnprintf.0:2,format_number.0:3,format_number.2:5,format_number.3:3,parse_string.1:6,parse_string.0:4,hex4.0:5,utf8_width.0:4,memcmp.0:5 LEN=0-4
diff --git a/testing/unit/keepalive.c b/testing/unit/keepalive.c
index 4b14c894..6ba277b4 100644
--- a/testing/unit/keepalive.c
+++ b/testing/unit/keepalive.c
@@ -2,15 +2,50 @@
/* Copyright (c) 2026 Authors of shirudo */
#include <errno.h>
+#include <stdio.h>
#include <string.h>
+#include <shirudo/string.h>
+
+#include "json.h"
#include "keepalive.h"
+#include "tamper.h"
#include "unit.h"
#define APPLIED 1
#define PASSED_OVER 2
#define NOT_ATTACHED 3
+#define FIXTURES "fixtures/keepalive/"
+#define FIXTURE_BODY_MAX 1200
+#define ALL_OK 5461
+#define STRING_MAX 255
+
+#define VERSION_ID \
+ "d6d918ad22ff597579e6678bef75adca84d30f9661e0b7bbdbf534eebc138c1f"
+#define CONFIG_ROOT_A \
+ "1d8465c5b17838303fee92b3905420d294d2c22c576a0bdef0f69f190ed22807"
+#define CONFIG_ROOT_B \
+ "ff2cbeff5d0e9c41a7b36f28d14ce09b73a5182e6fc40d97b5e2a1683c0f7ff8"
+#define IMAGE_ROOT \
+ "a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c"
+#define ZEROS \
+ "0000000000000000000000000000000000000000000000000000000000000000"
+#define ONES \
+ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
+#define ROOT_AFTER_1 \
+ "04f83540c1727aefcf3c53449a99594c8fd1cc15e1466dbdbf3168cc8f9dc136"
+#define ROOT_AFTER_3 \
+ "154522480ff8884daa95589e80f26f20248bbf9c192dfbb3cee04e03b4f136a1"
+#define SET_1_0_0 \
+ "6c9e113d02cd14646f617d7bba4a092b91bc3802dfdaae50e2fef7a01e0580aa"
+#define SET_1_1_0 \
+ "74e5bd2c665109f6d8f926af5476c266ad70ab07293ff13b1a13d0b89aa5c1bf"
+#define SET_EMPTY \
+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
+
+static const __u64 no_tamper[TAMPER_MAX];
+
static void expect_mask(const __u8 *codes, size_t n, const char *want)
{
char b64[KEEPALIVE_MASK_B64_MAX];
@@ -202,6 +237,517 @@ static void cond_follows_the_order_conditions_returns(void)
EXPECT_EQ(cond & 3, 1);
}
+/* make -C testing runs from testing/, and by hand from the top of the tree. */
+static size_t read_fixture(const char *name, char *body, size_t cap)
+{
+ char path[256];
+ size_t len;
+ FILE *f;
+
+ ASSERT_TRUE(strsprintf(path, sizeof(path), FIXTURES "%s", name) > 0);
+ f = fopen(path, "r");
+ if (!f) {
+ ASSERT_TRUE(strsprintf(path, sizeof(path),
+ "testing/" FIXTURES "%s", name) > 0);
+ f = fopen(path, "r");
+ }
+ ASSERT_TRUE(f);
+ len = fread(body, 1, cap, f);
+ ASSERT_TRUE(feof(f));
+ fclose(f);
+ return len;
+}
+
+static void expect_fixture(const struct keepalive_input *in, const char *name)
+{
+ char want[KEEPALIVE_BODY_MAX], body[KEEPALIVE_BODY_MAX];
+ size_t want_len = read_fixture(name, want, sizeof(want)), len;
+
+ EXPECT_LT(want_len, FIXTURE_BODY_MAX + 1);
+ ASSERT_EQ(keepalive_emit(in, body, sizeof(body), &len), 0);
+ ASSERT_EQ(len, want_len);
+ EXPECT_MEMEQ(body, want, len);
+ EXPECT_EQ(body[len], '\0');
+}
+
+static void expect_body(const struct keepalive_input *in, const char *want)
+{
+ char body[KEEPALIVE_BODY_MAX];
+ size_t len;
+
+ ASSERT_EQ(keepalive_emit(in, body, sizeof(body), &len), 0);
+ EXPECT_STREQ(body, want);
+ EXPECT_EQ(len, strlen(want));
+}
+
+static void expect_member(const struct keepalive_input *in, const char *want)
+{
+ char body[KEEPALIVE_BODY_MAX];
+ size_t len;
+
+ ASSERT_EQ(keepalive_emit(in, body, sizeof(body), &len), 0);
+ UNIT_CHECK(0, strstr(body, want), "no %s in %s", want, body);
+}
+
+static void expect_refused(const struct keepalive_input *in, int ret)
+{
+ char body[KEEPALIVE_BODY_MAX] = "left";
+ size_t len = sizeof(body);
+
+ EXPECT_EQ(keepalive_emit(in, body, sizeof(body), &len), ret);
+ EXPECT_STREQ(body, "");
+ EXPECT_EQ(len, 0);
+}
+
+static struct keepalive_input node_a(void)
+{
+ return (struct keepalive_input){
+ .boot_id = "168d20a1-5011-4e1b-9924-d8b73c85aa4e",
+ .version_id = VERSION_ID,
+ .config_root = CONFIG_ROOT_A,
+ .kernel_release = "7.4.2",
+ .image_id = "fleet-os",
+ .image_version = "2026.11.1",
+ .image_root = IMAGE_ROOT,
+ .verdict = "ok",
+ .cond = ALL_OK,
+ .tamper = no_tamper,
+ .tree = true,
+ .tree_id = "fleet-base",
+ .base_root = ZEROS,
+ };
+}
+
+static struct keepalive_input node_b(void)
+{
+ struct keepalive_input in = node_a();
+
+ in.boot_id = "9a6f14ea-0cc2-45e1-8e23-7b887120cf54";
+ in.config_root = CONFIG_ROOT_B;
+ in.image_root = "";
+ in.projection_root = ZEROS;
+ in.set_sha256 = SET_EMPTY;
+ return in;
+}
+
+static void at_release(struct keepalive_input *in, __u64 generation,
+ __u8 outcome, char mask[KEEPALIVE_MASK_B64_MAX])
+{
+ __u8 codes[KEEPALIVE_MASK_OPS];
+
+ memset(codes, outcome, sizeof(codes));
+ ASSERT_EQ(keepalive_mask(codes, generation, mask,
+ KEEPALIVE_MASK_B64_MAX), 0);
+ in->generation = generation;
+ in->last_seq = generation;
+ in->mask = mask;
+}
+
+static void body_of_node_a_after_release_1_0_0(void)
+{
+ struct keepalive_input in = node_a();
+ char mask[KEEPALIVE_MASK_B64_MAX];
+
+ at_release(&in, 1, APPLIED, mask);
+ in.projection_root = ROOT_AFTER_1;
+ in.set_sha256 = SET_1_0_0;
+ in.count = 1;
+ expect_fixture(&in, "node-a-1.0.0.json");
+}
+
+static void body_of_node_a_after_release_1_1_0(void)
+{
+ struct keepalive_input in = node_a();
+ char mask[KEEPALIVE_MASK_B64_MAX];
+
+ at_release(&in, 3, APPLIED, mask);
+ in.projection_root = ROOT_AFTER_3;
+ in.set_sha256 = SET_1_1_0;
+ in.count = 1;
+ expect_fixture(&in, "node-a-1.1.0.json");
+}
+
+static void body_of_node_b_after_release_1_0_0(void)
+{
+ struct keepalive_input in = node_b();
+ char mask[KEEPALIVE_MASK_B64_MAX];
+
+ at_release(&in, 1, PASSED_OVER, mask);
+ expect_fixture(&in, "node-b-1.0.0.json");
+}
+
+static void body_of_node_b_after_release_1_1_0(void)
+{
+ struct keepalive_input in = node_b();
+ char mask[KEEPALIVE_MASK_B64_MAX];
+
+ at_release(&in, 3, PASSED_OVER, mask);
+ expect_fixture(&in, "node-b-1.1.0.json");
+}
+
+static void body_of_sixteen_operations(void)
+{
+ struct keepalive_input in = node_a();
+
+ in.boot_id = "d5140906-4eab-4983-a62c-29e49d449ea3";
+ in.config_root =
+ "1fbdff17ad8d50404772901df54d5aa2c698eddbb413e286fc654f80931c688a";
+ in.kernel_release = "7.4.0-shirudo";
+ in.generation = 42;
+ in.last_seq = 12;
+ in.projection_root =
+ "f59301708fc8e150c5d2abb1009b4f13fc0dcc1304b708f77dc14b1541679e28";
+ in.mask = "WdlZAA==";
+ in.set_sha256 =
+ "ac1af1d44b5c1fe3f616384db421138aa4d296595797b5864908a89276e17e6d";
+ in.count = 7;
+ expect_fixture(&in, "ops-16.json");
+}
+
+static struct keepalive_input all_null(void)
+{
+ return (struct keepalive_input){
+ .boot_id = "b",
+ .version_id = VERSION_ID,
+ .kernel_release = "k",
+ .verdict = "init-pending",
+ .flags = 8,
+ .set_sha256 = SET_EMPTY,
+ };
+}
+
+static void body_with_every_nullable_key_null(void)
+{
+ struct keepalive_input in = all_null();
+
+ expect_body(&in,
+ "{\"schema\":\"shirudo.keepalive/1\",\"boot_id\":\"b\","
+ "\"version_id\":\"" VERSION_ID "\",\"config_root\":null,"
+ "\"kernel_release\":\"k\",\"image_id\":null,"
+ "\"image_version\":null,\"image_root\":null,"
+ "\"verdict\":\"init-pending\",\"cond\":0,\"flags\":8,"
+ "\"tamper\":null,\"tree_id\":null,\"generation\":null,"
+ "\"base_seq\":null,\"last_seq\":null,\"base_root\":null,"
+ "\"projection_root\":null,\"mask\":null,"
+ "\"set_sha256\":\"" SET_EMPTY "\",\"count\":0,"
+ "\"not_ok\":0}");
+}
+
+static void body_without_a_tree_ignores_the_tree_values(void)
+{
+ char with_values[KEEPALIVE_BODY_MAX], without[KEEPALIVE_BODY_MAX];
+ struct keepalive_input in = node_a();
+ size_t len;
+
+ in.projection_root = ROOT_AFTER_1;
+ in.set_sha256 = SET_1_0_0;
+ in.mask = "QA==";
+ in.generation = 1;
+ in.last_seq = 1;
+ in.tree = false;
+ ASSERT_EQ(keepalive_emit(&in, with_values, sizeof(with_values), &len),
+ 0);
+ in.tree_id = NULL;
+ in.projection_root = "not a hash";
+ in.base_root = NULL;
+ in.mask = NULL;
+ ASSERT_EQ(keepalive_emit(&in, without, sizeof(without), &len), 0);
+ EXPECT_STREQ(with_values, without);
+ expect_member(&in, "\"tamper\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"
+ "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\"tree_id\":null,"
+ "\"generation\":null,\"base_seq\":null,\"last_seq\":null,"
+ "\"base_root\":null,\"projection_root\":null,"
+ "\"mask\":null,\"set_sha256\":");
+}
+
+static struct keepalive_input node_a_at_1_0_0(void)
+{
+ struct keepalive_input in = node_a();
+
+ in.generation = 1;
+ in.last_seq = 1;
+ in.projection_root = ROOT_AFTER_1;
+ in.mask = "QA==";
+ in.set_sha256 = SET_1_0_0;
+ in.count = 1;
+ return in;
+}
+
+static void body_gives_each_nullable_key_as_null(void)
+{
+ struct keepalive_input in = node_a_at_1_0_0();
+
+ in.config_root = NULL;
+ expect_member(&in, ",\"config_root\":null,\"kernel_release\":");
+ in.config_root = "";
+ expect_member(&in, ",\"config_root\":null,\"kernel_release\":");
+ in.image_id = NULL;
+ expect_member(&in, ",\"image_id\":null,\"image_version\":\"");
+ in.image_id = "";
+ expect_member(&in, ",\"image_id\":null,\"image_version\":\"");
+ in.image_version = NULL;
+ expect_member(&in, ",\"image_version\":null,\"image_root\":\"");
+ in.image_version = "";
+ expect_member(&in, ",\"image_version\":null,\"image_root\":\"");
+ in.image_root = NULL;
+ expect_member(&in, ",\"image_root\":null,\"verdict\":");
+ in.image_root = "";
+ expect_member(&in, ",\"image_root\":null,\"verdict\":");
+ in.tamper = NULL;
+ expect_member(&in, ",\"tamper\":null,\"tree_id\":\"fleet-base\",");
+ in.base_root = NULL;
+ expect_member(&in, ",\"base_root\":null,\"projection_root\":\"");
+ in.base_root = "";
+ expect_member(&in, ",\"base_root\":null,\"projection_root\":\"");
+ in.mask = NULL;
+ expect_member(&in, ",\"mask\":null,\"set_sha256\":");
+ expect_member(&in, ",\"tree_id\":\"fleet-base\",\"generation\":1,"
+ "\"base_seq\":0,\"last_seq\":1,\"base_root\":null,"
+ "\"projection_root\":\"" ROOT_AFTER_1 "\",\"mask\":null,");
+}
+
+static void body_tells_an_empty_mask_from_none(void)
+{
+ struct keepalive_input in = node_a_at_1_0_0();
+ char mask[KEEPALIVE_MASK_B64_MAX];
+
+ ASSERT_EQ(keepalive_mask(NULL, 0, mask, sizeof(mask)), 0);
+ in.mask = mask;
+ expect_member(&in, ",\"mask\":\"\",\"set_sha256\":");
+}
+
+struct key_walk {
+ size_t n;
+ size_t depth;
+ bool in_order;
+};
+
+static const char * const body_keys[] = {
+ "schema", "boot_id", "version_id", "config_root", "kernel_release",
+ "image_id", "image_version", "image_root", "verdict", "cond", "flags",
+ "tamper", "tree_id", "generation", "base_seq", "last_seq", "base_root",
+ "projection_root", "mask", "set_sha256", "count", "not_ok",
+};
+
+static int walk_open(void *ctx, bool array)
+{
+ struct key_walk *walk = ctx;
+
+ walk->depth++;
+ return 0;
+}
+
+static int walk_close(void *ctx, bool array)
+{
+ struct key_walk *walk = ctx;
+
+ walk->depth--;
+ return 0;
+}
+
+static int walk_key(void *ctx, const char *key)
+{
+ struct key_walk *walk = ctx;
+
+ if (walk->depth != 1 || walk->n >= ARRAY_SIZE(body_keys) ||
+ strcmp(key, body_keys[walk->n]))
+ walk->in_order = false;
+ walk->n++;
+ return 0;
+}
+
+static void expect_the_keys_in_order(const struct keepalive_input *in)
+{
+ static const struct json_visitor visitor = {
+ .open = walk_open,
+ .close = walk_close,
+ .key = walk_key,
+ };
+ struct key_walk walk = { .in_order = true };
+ char body[KEEPALIVE_BODY_MAX];
+ size_t len;
+
+ ASSERT_EQ(keepalive_emit(in, body, sizeof(body), &len), 0);
+ ASSERT_EQ(json_walk(body, len, &visitor, &walk), 0);
+ EXPECT_EQ(walk.n, 22);
+ EXPECT_EQ(walk.n, ARRAY_SIZE(body_keys));
+ EXPECT_TRUE(walk.in_order);
+}
+
+static void body_has_every_key_in_order(void)
+{
+ struct keepalive_input with_tree = node_a_at_1_0_0();
+ struct keepalive_input without = all_null();
+
+ expect_the_keys_in_order(&with_tree);
+ expect_the_keys_in_order(&without);
+}
+
+/*
+ * The backend reads a counter by its place in the array: a counter is
+ * appended to enum tamper_point and to this table, never put between two.
+ */
+static const char * const tamper_order[] = {
+ "LoadNotLoader", "LoadUnsigned", "LoadUnverified", "LoadKeyringKind",
+ "LoadKeyring", "MapCreate", "MapWrite", "MapFdWrite", "ProgRun",
+ "ProgAttach", "ObjPin", "BpfCommand", "ExeIdentity", "KeyringWrite",
+ "PinUnlink", "PinRename", "PinXattr", "BpffsUmount",
+ "BpffsUmountAbove", "BpffsMount", "SealWrite", "SealUnlink",
+ "SealRename", "SealSetattr", "SealXattr", "SealLabel", "SealPtrace",
+ "SealPerf", "ExecTraced", "SealEntry", "PerfAttach", "PinEntry",
+ "SealCopyUp", "Kexec", "TpmExtend", "ObjGet", "SealSignal",
+};
+
+static void tamper_counters_keep_their_places(void)
+{
+ size_t i;
+
+ ASSERT_EQ(ARRAY_SIZE(tamper_order), 37);
+ ASSERT_EQ(TAMPER_MAX, ARRAY_SIZE(tamper_order));
+ EXPECT_EQ(TAMPER_LOAD_NOT_LOADER, 0);
+ EXPECT_EQ(TAMPER_SEAL_SIGNAL, TAMPER_MAX - 1);
+ for (i = 0; i < ARRAY_SIZE(tamper_order); i++)
+ EXPECT_STREQ(tamper_name(i), tamper_order[i]);
+}
+
+static void body_lists_the_counters_in_enum_order(void)
+{
+ struct keepalive_input in = node_a_at_1_0_0();
+ __u64 tamper[TAMPER_MAX];
+ size_t i;
+
+ for (i = 0; i < TAMPER_MAX; i++)
+ tamper[i] = i + 1;
+ tamper[TAMPER_SEAL_SIGNAL] = ~0ULL;
+ in.tamper = tamper;
+ expect_member(&in, ",\"tamper\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,"
+ "16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,"
+ "34,35,36,18446744073709551615],\"tree_id\":");
+}
+
+static void body_refuses_a_missing_string(void)
+{
+ struct keepalive_input in = node_a_at_1_0_0();
+
+ in.boot_id = NULL;
+ expect_refused(&in, -EINVAL);
+ in = node_a_at_1_0_0();
+ in.kernel_release = NULL;
+ expect_refused(&in, -EINVAL);
+ in = node_a_at_1_0_0();
+ in.verdict = NULL;
+ expect_refused(&in, -EINVAL);
+ in = node_a_at_1_0_0();
+ in.tree_id = NULL;
+ expect_refused(&in, -EINVAL);
+}
+
+static void body_refuses_what_is_no_hash(void)
+{
+ struct keepalive_input in = node_a_at_1_0_0();
+
+ in.version_id = NULL;
+ expect_refused(&in, -EINVAL);
+ in.version_id = "";
+ expect_refused(&in, -EINVAL);
+ in.version_id = "sha256:" VERSION_ID;
+ expect_refused(&in, -EINVAL);
+ in.version_id = VERSION_ID "0";
+ expect_refused(&in, -EINVAL);
+ in.version_id =
+ "D6D918AD22FF597579E6678BEF75ADCA84D30F9661E0B7BBDBF534EEBC138C1F";
+ expect_refused(&in, -EINVAL);
+ in = node_a_at_1_0_0();
+ in.config_root = "1d8465c5";
+ expect_refused(&in, -EINVAL);
+ in = node_a_at_1_0_0();
+ in.image_root = "g" VERSION_ID;
+ expect_refused(&in, -EINVAL);
+ in = node_a_at_1_0_0();
+ in.base_root = "0";
+ expect_refused(&in, -EINVAL);
+ in = node_a_at_1_0_0();
+ in.projection_root = "";
+ expect_refused(&in, -EINVAL);
+ in.projection_root = NULL;
+ expect_refused(&in, -EINVAL);
+ in = node_a_at_1_0_0();
+ in.set_sha256 = NULL;
+ expect_refused(&in, -EINVAL);
+}
+
+static void body_refuses_a_string_that_is_not_utf8(void)
+{
+ struct keepalive_input in = node_a_at_1_0_0();
+
+ in.kernel_release = "7.4.2\xff";
+ expect_refused(&in, -EILSEQ);
+}
+
+static void body_refuses_a_buffer_one_byte_short(void)
+{
+ struct keepalive_input in = node_a_at_1_0_0();
+ char body[KEEPALIVE_BODY_MAX] = "left";
+ size_t len, whole;
+
+ ASSERT_EQ(keepalive_emit(&in, body, sizeof(body), &whole), 0);
+ EXPECT_EQ(keepalive_emit(&in, body, whole, &len), -ENOSPC);
+ EXPECT_STREQ(body, "");
+ EXPECT_EQ(len, 0);
+ EXPECT_EQ(keepalive_emit(&in, body, 1, &len), -ENOSPC);
+ EXPECT_EQ(keepalive_emit(&in, NULL, 0, &len), -ENOSPC);
+ ASSERT_EQ(keepalive_emit(&in, body, whole + 1, &len), 0);
+ EXPECT_EQ(len, whole);
+ EXPECT_EQ(strlen(body), whole);
+}
+
+static void body_of_the_largest_values_fits(void)
+{
+ char text[STRING_MAX + 1], mask[KEEPALIVE_MASK_B64_MAX];
+ char body[KEEPALIVE_BODY_MAX];
+ __u8 codes[KEEPALIVE_MASK_OPS];
+ struct keepalive_input in = {
+ .boot_id = text,
+ .version_id = ONES,
+ .config_root = ONES,
+ .kernel_release = text,
+ .image_id = text,
+ .image_version = text,
+ .image_root = ONES,
+ .verdict = text,
+ .cond = ~0,
+ .flags = ~0U,
+ .tree = true,
+ .tree_id = text,
+ .generation = ~0ULL,
+ .base_seq = ~0ULL,
+ .last_seq = ~0ULL,
+ .base_root = ONES,
+ .projection_root = ONES,
+ .mask = mask,
+ .set_sha256 = ONES,
+ .count = ~0ULL,
+ .not_ok = ~0ULL,
+ };
+ __u64 tamper[TAMPER_MAX];
+ size_t whole, len, i;
+
+ memset(text, 'x', STRING_MAX);
+ text[STRING_MAX] = '\0';
+ memset(codes, NOT_ATTACHED, sizeof(codes));
+ ASSERT_EQ(keepalive_mask(codes, KEEPALIVE_MASK_OPS, mask,
+ sizeof(mask)), 0);
+ for (i = 0; i < TAMPER_MAX; i++)
+ tamper[i] = ~0ULL;
+ in.tamper = tamper;
+
+ ASSERT_EQ(keepalive_emit(&in, body, sizeof(body), &whole), 0);
+ EXPECT_EQ(whole, 3298);
+ EXPECT_LT(whole, KEEPALIVE_BODY_MAX);
+ EXPECT_EQ(keepalive_emit(&in, body, whole, &len), -ENOSPC);
+ EXPECT_EQ(keepalive_emit(&in, body, whole + 1, &len), 0);
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(mask_of_one_operation),
UNIT_CASE(mask_of_the_release_example),
@@ -216,6 +762,23 @@ static const struct unit_case cases[] = {
UNIT_CASE(cond_leaves_a_missing_condition_at_zero),
UNIT_CASE(cond_places_each_status_by_name),
UNIT_CASE(cond_follows_the_order_conditions_returns),
+ UNIT_CASE(body_of_node_a_after_release_1_0_0),
+ UNIT_CASE(body_of_node_a_after_release_1_1_0),
+ UNIT_CASE(body_of_node_b_after_release_1_0_0),
+ UNIT_CASE(body_of_node_b_after_release_1_1_0),
+ UNIT_CASE(body_of_sixteen_operations),
+ UNIT_CASE(body_with_every_nullable_key_null),
+ UNIT_CASE(body_without_a_tree_ignores_the_tree_values),
+ UNIT_CASE(body_gives_each_nullable_key_as_null),
+ UNIT_CASE(body_tells_an_empty_mask_from_none),
+ UNIT_CASE(body_has_every_key_in_order),
+ UNIT_CASE(tamper_counters_keep_their_places),
+ UNIT_CASE(body_lists_the_counters_in_enum_order),
+ UNIT_CASE(body_refuses_a_missing_string),
+ UNIT_CASE(body_refuses_what_is_no_hash),
+ UNIT_CASE(body_refuses_a_string_that_is_not_utf8),
+ UNIT_CASE(body_refuses_a_buffer_one_byte_short),
+ UNIT_CASE(body_of_the_largest_values_fits),
{}
};
diff --git a/util/keepalive.c b/util/keepalive.c
index a26ddc3e..899fa56b 100644
--- a/util/keepalive.c
+++ b/util/keepalive.c
@@ -5,10 +5,15 @@
#include <string.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "crypto.h"
+#include "json.h"
#include "keepalive.h"
+#define SCHEMA "shirudo.keepalive/1"
+#define HASH_HEX_LEN (2 * CRYPTO_SHA256_LEN)
+
#define CODE_BITS 2
#define CODE_MAX 3
#define CODES_PER_BYTE 4
@@ -85,3 +90,154 @@ __u16 keepalive_cond(const struct condition *conds, size_t n)
(CODE_BITS * i);
return cond;
}
+
+static bool is_hash(const char *s)
+{
+ size_t i;
+
+ if (!s)
+ return false;
+ for (i = 0; i < HASH_HEX_LEN; i++)
+ if (!(s[i] >= '0' && s[i] <= '9') &&
+ !(s[i] >= 'a' && s[i] <= 'f'))
+ return false;
+ return !s[HASH_HEX_LEN];
+}
+
+static bool is_absent(const char *s)
+{
+ return !s || !s[0];
+}
+
+static bool is_hash_or_absent(const char *s)
+{
+ return is_absent(s) || is_hash(s);
+}
+
+static bool tree_is_valid(const struct keepalive_input *in)
+{
+ return !in->tree ||
+ (in->tree_id && is_hash_or_absent(in->base_root) &&
+ is_hash(in->projection_root));
+}
+
+static bool input_is_valid(const struct keepalive_input *in)
+{
+ return in->boot_id && is_hash(in->version_id) &&
+ is_hash_or_absent(in->config_root) && in->kernel_release &&
+ is_hash_or_absent(in->image_root) && in->verdict &&
+ tree_is_valid(in) && is_hash(in->set_sha256);
+}
+
+static void kv_string_or_null(struct json_emit *e, const char *key,
+ const char *val)
+{
+ if (is_absent(val))
+ json_kv_null(e, key);
+ else
+ json_kv_string(e, key, val);
+}
+
+/* json_kv_int() takes an __s64, which a tamper counter can pass. */
+static void u64_value(struct json_emit *e, __u64 val)
+{
+ char num[24];
+ int len = strsprintf(num, sizeof(num), "%llu", val);
+
+ if (len < 0)
+ e->err = -E2BIG;
+ else
+ json_raw_value(e, num, len);
+}
+
+static void kv_u64(struct json_emit *e, const char *key, __u64 val)
+{
+ json_key(e, key);
+ u64_value(e, val);
+}
+
+static void kv_tamper(struct json_emit *e, const __u64 *tamper)
+{
+ size_t i;
+
+ if (!tamper) {
+ json_kv_null(e, "tamper");
+ return;
+ }
+ json_arr_open(e, "tamper");
+ for (i = 0; i < TAMPER_MAX; i++)
+ u64_value(e, tamper[i]);
+ json_arr_close(e);
+}
+
+static void kv_no_tree(struct json_emit *e)
+{
+ static const char * const keys[] = {
+ "tree_id", "generation", "base_seq", "last_seq", "base_root",
+ "projection_root", "mask",
+ };
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(keys); i++)
+ json_kv_null(e, keys[i]);
+}
+
+static void kv_tree(struct json_emit *e, const struct keepalive_input *in)
+{
+ if (!in->tree) {
+ kv_no_tree(e);
+ return;
+ }
+ json_kv_string(e, "tree_id", in->tree_id);
+ kv_u64(e, "generation", in->generation);
+ kv_u64(e, "base_seq", in->base_seq);
+ kv_u64(e, "last_seq", in->last_seq);
+ kv_string_or_null(e, "base_root", in->base_root);
+ json_kv_string(e, "projection_root", in->projection_root);
+ if (in->mask)
+ json_kv_string(e, "mask", in->mask);
+ else
+ json_kv_null(e, "mask");
+}
+
+static void body(struct json_emit *e, const struct keepalive_input *in)
+{
+ json_obj_open(e);
+ json_kv_string(e, "schema", SCHEMA);
+ json_kv_string(e, "boot_id", in->boot_id);
+ json_kv_string(e, "version_id", in->version_id);
+ kv_string_or_null(e, "config_root", in->config_root);
+ json_kv_string(e, "kernel_release", in->kernel_release);
+ kv_string_or_null(e, "image_id", in->image_id);
+ kv_string_or_null(e, "image_version", in->image_version);
+ kv_string_or_null(e, "image_root", in->image_root);
+ json_kv_string(e, "verdict", in->verdict);
+ kv_u64(e, "cond", in->cond);
+ kv_u64(e, "flags", in->flags);
+ kv_tamper(e, in->tamper);
+ kv_tree(e, in);
+ json_kv_string(e, "set_sha256", in->set_sha256);
+ kv_u64(e, "count", in->count);
+ kv_u64(e, "not_ok", in->not_ok);
+ json_obj_close(e);
+}
+
+int keepalive_emit(const struct keepalive_input *in, char *buf, size_t cap,
+ size_t *len)
+{
+ struct json_emit e = { .buf = buf, .cap = cap };
+
+ *len = 0;
+ if (cap)
+ buf[0] = '\0';
+ if (!input_is_valid(in))
+ return -EINVAL;
+ body(&e, in);
+ if (e.err) {
+ if (cap)
+ buf[0] = '\0';
+ return e.err;
+ }
+ *len = e.len;
+ return 0;
+}
diff --git a/util/keepalive.h b/util/keepalive.h
index 6c730fda..492b32e4 100644
--- a/util/keepalive.h
+++ b/util/keepalive.h
@@ -2,23 +2,71 @@
/*
* Copyright (c) 2026 Authors of shirudo
*
- * The packed fields of the keepalive body: the per-operation outcome
- * mask and the conditions word.
+ * The keepalive body, the status record a node's TPM quotes, and its
+ * packed fields: the per-operation outcome mask and the conditions word.
*/
#ifndef SHIRUDO_KEEPALIVE_H
#define SHIRUDO_KEEPALIVE_H
+#include <stdbool.h>
#include <stddef.h>
#include <shirudo/misc.h>
+#include "bpf/lib/tamper.h"
#include "conditions.h"
#define KEEPALIVE_MASK_OPS 512
#define KEEPALIVE_MASK_BYTES (KEEPALIVE_MASK_OPS / 4)
#define KEEPALIVE_MASK_B64_MAX (4 * ((KEEPALIVE_MASK_BYTES + 2) / 3) + 1)
+/* Holds the largest body and its terminator. */
+#define KEEPALIVE_BODY_MAX 4096
+
+/*
+ * A hash is 64 lowercase hex digits. A string that may be null is so when
+ * it is NULL or, but for mask, empty, as sysinfo and the tree state leave
+ * what is absent.
+ */
+struct keepalive_input {
+ const char *boot_id;
+ const char *version_id; /* hash */
+ const char *config_root; /* hash or null */
+ const char *kernel_release;
+ const char *image_id; /* or null */
+ const char *image_version; /* or null */
+ const char *image_root; /* hash or null */
+ const char *verdict;
+ __u16 cond; /* keepalive_cond() */
+ __u32 flags;
+ const __u64 *tamper; /* TAMPER_MAX counters or null */
+ /* Not set: the node follows no tree and the seven tree keys are null. */
+ bool tree;
+ const char *tree_id;
+ __u64 generation;
+ __u64 base_seq;
+ __u64 last_seq;
+ const char *base_root; /* hash or null */
+ const char *projection_root; /* hash */
+ /* From keepalive_mask(): NULL is null, "" a window of no operation. */
+ const char *mask;
+ const char *set_sha256; /* hash */
+ __u64 count;
+ __u64 not_ok;
+};
+
+/*
+ * Writes the body as one compact JSON object with its terminator and no
+ * newline, and its length without the terminator in *len: the bytes to
+ * hash, send and store. -ENOSPC when cap does not hold them, -EINVAL for
+ * a string that is NULL where the body needs one or a hash that is not
+ * one, -EILSEQ for a string that is not UTF-8; buf is then empty and
+ * *len 0.
+ */
+int keepalive_emit(const struct keepalive_input *in, char *buf, size_t cap,
+ size_t *len) __must_check;
+
/*
* The first code in the highest bits, as the backend reads them; -EINVAL
* for a code past two bits or more than KEEPALIVE_MASK_OPS codes, -E2BIG