Skip to content

eBPF-based security platform

keepalive: pack the outcome mask and the conditions word

John Fastabenddc0679a57402
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index e169aa91..bbfab7e1 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -931,6 +931,16 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
start with a fixed 832
- The whole text up to 191 bytes, else its first 188 and ``...``;
``?`` for a text of 1024 bytes
+ * - ``keepalive-pack``
+ - ``keepalive_mask()``, ``keepalive_cond()``
+ - Masks of 0 to 9, 512 and 513 codes, every code and buffer size;
+ 0 to 7 conditions of every known type, one unknown, and every
+ status. OpenSSL's encoder is a stub that keeps its input
+ - A window past 512 or a code past two bits is ``-EINVAL``, a
+ buffer too small ``-E2BIG``; else each code in its two bits, the
+ first highest, the last byte padded with zeros. Each condition's
+ pair at the place of its type name, from the first of that type,
+ ``00`` for a missing one or an unknown status, none past seven
Lean 4 proofs
-------------
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index be76cac3..fe3c09cc 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -588,12 +588,13 @@ signed config image.
Unit suites and fuzz targets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The unit suites in :src:`testing/unit/` cover 27 of the modules in
+The unit suites in :src:`testing/unit/` cover 28 of the modules in
:src:`util/`: ``attest``, ``bpm_oci``, ``bpm_package``, ``bpm_state``,
``bpm_tree``, ``bpm_tree_overlap``, ``bpm_tree_state``, ``chain``,
``conditions``, ``curl``,
``eventlog``, ``format``, ``hex``, ``io``, ``json``, ``kernel_abi``,
-``kernel_build_id``, ``kernel_release``, ``measured``, ``paths``,
+``keepalive``, ``kernel_build_id``, ``kernel_release``, ``measured``,
+``paths``,
``string``, ``upatch``, ``upatch_decode``, ``upatch_pins``,
``upatch_resolve``, ``upatch_verify`` and ``xdp``, and ``overflow`` the
arithmetic of
diff --git a/Makefile.shirudo b/Makefile.shirudo
index f00a006f..57929928 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -272,6 +272,7 @@ UTIL_SRCS := \
util/help.c \
util/timefmt.c \
util/conditions.c \
+ util/keepalive.c \
util/format.c \
util/seal.c \
util/serializer.c \
diff --git a/testing/proofs/cbmc/keepalive-pack.c b/testing/proofs/cbmc/keepalive-pack.c
new file mode 100644
index 00000000..e7215239
--- /dev/null
+++ b/testing/proofs/cbmc/keepalive-pack.c
@@ -0,0 +1,96 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The packed fields of util/keepalive.c that the backend reads bit by bit:
+ * the mask of LEN outcome codes and the conditions word.
+ */
+
+#include <stdbool.h>
+
+#include "proof.h"
+#include "../../../util/keepalive.c"
+
+static __u8 packed[KEEPALIVE_MASK_BYTES];
+static size_t packed_len;
+
+/* OpenSSL encodes; the proof keeps the bytes it is handed. */
+int base64_encode(const __u8 *data, size_t len, char *out, size_t out_sz)
+{
+ __CPROVER_assert(len <= sizeof(packed), "no more than a full mask");
+ memcpy(packed, data, len);
+ packed_len = len;
+ return 0;
+}
+
+static void mask(void)
+{
+ __u8 codes[LEN ? LEN : 1];
+ char b64[KEEPALIVE_MASK_B64_MAX];
+ size_t n = LEN, bytes = (n + 3) / 4, cap;
+ bool valid = true;
+ int ret;
+
+ __CPROVER_assume(cap <= sizeof(b64));
+ for (size_t i = 0; i < LEN; i++)
+ valid = valid && codes[i] <= 3;
+ ret = keepalive_mask(codes, LEN, b64, cap);
+ if (n > KEEPALIVE_MASK_OPS || !valid) {
+ __CPROVER_assert(ret == -EINVAL,
+ "a window too long or a code past two bits");
+ return;
+ }
+ if (cap < 4 * ((bytes + 2) / 3) + 1) {
+ __CPROVER_assert(ret == -E2BIG, "a buffer too small");
+ return;
+ }
+ __CPROVER_assert(!ret && packed_len == bytes, "every code packed");
+ for (size_t i = 0; i < LEN; i++)
+ __CPROVER_assert(((packed[i / 4] >> (6 - 2 * (i % 4))) & 3) ==
+ codes[i], "the first code in the highest bits");
+ if (LEN % 4)
+ __CPROVER_assert(!(packed[bytes - 1] & (0xff >> (2 * (LEN % 4)))),
+ "the last byte padded with zeros");
+}
+
+static const char *const types[] = {
+ "LsmAttached", "KeyringSealed", "ChainIntact", "UpatchesMeasured",
+ "TpmMeasured", "ConfigActive", "XdpCovered", "Unknown",
+};
+
+static const char *const statuses[] = { "", "OK", "WARN", "ALERT", "?" };
+
+static void cond(void)
+{
+ struct condition conds[CONDITIONS_MAX];
+ size_t type[CONDITIONS_MAX], status[CONDITIONS_MAX], n;
+ __u16 word;
+
+ __CPROVER_assume(n <= CONDITIONS_MAX);
+ for (size_t i = 0; i < n; i++) {
+ __CPROVER_assume(type[i] < ARRAY_SIZE(types));
+ __CPROVER_assume(status[i] < ARRAY_SIZE(statuses));
+ conds[i].type = types[type[i]];
+ conds[i].status = statuses[status[i]];
+ }
+ word = keepalive_cond(conds, n);
+ for (size_t k = 0; k < CONDITIONS_MAX; k++) {
+ size_t want = 0;
+
+ for (size_t i = n; i-- > 0;) {
+ if (type[i] == k)
+ want = status[i] <= 3 ? status[i] : 0;
+ }
+ __CPROVER_assert(((word >> (2 * k)) & 3) == want,
+ "each pair by its type name, the first given");
+ }
+ __CPROVER_assert(!(word >> (2 * CONDITIONS_MAX)),
+ "no pair past the conditions");
+}
+
+void harness(void)
+{
+ mask();
+ if (!LEN)
+ cond();
+}
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index b3536ee3..86815566 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -133,3 +133,4 @@ bpm-tree-state-replay-lines --unwind 8 --unwindset strlen.0:66,hex_decode.0:33,t
bpm-tree-state-pending --unwind 5 --unwindset upatch_name_valid.0:10,strnlen.0:74,memcpy.0:257,strlen.0:66,strcmp.0:18,hex_decode.0:33,digest_text.0:65,memcmp.0:8,strncmp.0:8
bpm-tree-state-match --unwind 4 --unwindset read_config_stream.0:7,read_config_stream.1:9,read_config_stream.2:9,read_config_stream.3:9,read_config_stream.4:9,read_text_line.0:9,strlen.0:9,strchr.0:9,strnlen.0:9,memcpy.0:9,memcmp.0:26 LEN=0-3
bpm-tree-state-options --unwind 4
+keepalive-pack --unwind 9 --unwindset strcmp.0:18,keepalive_mask.0:514,mask.0:514,mask.1:514 LEN=0-9,512,513
diff --git a/testing/unit/keepalive.c b/testing/unit/keepalive.c
new file mode 100644
index 00000000..4b14c894
--- /dev/null
+++ b/testing/unit/keepalive.c
@@ -0,0 +1,222 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+#include <string.h>
+
+#include "keepalive.h"
+#include "unit.h"
+
+#define APPLIED 1
+#define PASSED_OVER 2
+#define NOT_ATTACHED 3
+
+static void expect_mask(const __u8 *codes, size_t n, const char *want)
+{
+ char b64[KEEPALIVE_MASK_B64_MAX];
+
+ ASSERT_EQ(keepalive_mask(codes, n, b64, sizeof(b64)), 0);
+ EXPECT_STREQ(b64, want);
+}
+
+static void mask_of_one_operation(void)
+{
+ static const __u8 applied[] = { APPLIED };
+ static const __u8 passed_over[] = { PASSED_OVER };
+
+ expect_mask(applied, 1, "QA==");
+ expect_mask(passed_over, 1, "gA==");
+}
+
+static void mask_of_the_release_example(void)
+{
+ static const __u8 node_a[] = { APPLIED, APPLIED, APPLIED };
+ static const __u8 node_b[] = { PASSED_OVER, PASSED_OVER, PASSED_OVER };
+
+ expect_mask(node_a, ARRAY_SIZE(node_a), "VA==");
+ expect_mask(node_b, ARRAY_SIZE(node_b), "qA==");
+}
+
+static void mask_of_sixteen_operations(void)
+{
+ static const __u8 codes[] = {
+ APPLIED, APPLIED, PASSED_OVER, APPLIED,
+ NOT_ATTACHED, APPLIED, PASSED_OVER, APPLIED,
+ APPLIED, APPLIED, PASSED_OVER, APPLIED,
+ 0, 0, 0, 0,
+ };
+
+ expect_mask(codes, ARRAY_SIZE(codes), "WdlZAA==");
+}
+
+static void mask_of_no_operation_is_empty(void)
+{
+ static const __u8 codes[] = { APPLIED };
+
+ expect_mask(codes, 0, "");
+}
+
+static void mask_fills_a_byte_then_pads_the_next(void)
+{
+ static const __u8 codes[] = {
+ APPLIED, APPLIED, APPLIED, APPLIED, APPLIED,
+ };
+
+ expect_mask(codes, 4, "VQ==");
+ expect_mask(codes, 5, "VUA=");
+}
+
+static void mask_of_a_full_window(void)
+{
+ char want[KEEPALIVE_MASK_B64_MAX];
+ __u8 codes[KEEPALIVE_MASK_OPS + 1];
+ char b64[KEEPALIVE_MASK_B64_MAX];
+
+ memset(codes, APPLIED, sizeof(codes));
+ memset(want, 'V', sizeof(want) - 1);
+ memcpy(want + sizeof(want) - 5, "VVU=", 5);
+
+ EXPECT_EQ(sizeof(want), 173);
+ expect_mask(codes, KEEPALIVE_MASK_OPS, want);
+ EXPECT_EQ(keepalive_mask(codes, KEEPALIVE_MASK_OPS + 1, b64,
+ sizeof(b64)), -EINVAL);
+}
+
+static void mask_refuses_a_code_past_two_bits(void)
+{
+ static const __u8 codes[] = { APPLIED, 4 };
+ char b64[KEEPALIVE_MASK_B64_MAX];
+
+ EXPECT_EQ(keepalive_mask(codes, ARRAY_SIZE(codes), b64, sizeof(b64)),
+ -EINVAL);
+}
+
+static void mask_refuses_a_buffer_too_small(void)
+{
+ static const __u8 codes[] = {
+ APPLIED, APPLIED, APPLIED, APPLIED, APPLIED,
+ };
+ char b64[5];
+
+ EXPECT_EQ(keepalive_mask(codes, 0, b64, 0), -E2BIG);
+ EXPECT_EQ(keepalive_mask(codes, 0, b64, 1), 0);
+ EXPECT_STREQ(b64, "");
+ EXPECT_EQ(keepalive_mask(codes, 1, b64, 4), -E2BIG);
+ EXPECT_EQ(keepalive_mask(codes, 5, b64, 4), -E2BIG);
+ EXPECT_EQ(keepalive_mask(codes, 5, b64, sizeof(b64)), 0);
+ EXPECT_STREQ(b64, "VUA=");
+}
+
+static void cond_of_all_ok(void)
+{
+ static const struct condition conds[] = {
+ { .type = "LsmAttached", .status = "OK" },
+ { .type = "KeyringSealed", .status = "OK" },
+ { .type = "ChainIntact", .status = "OK" },
+ { .type = "UpatchesMeasured", .status = "OK" },
+ { .type = "TpmMeasured", .status = "OK" },
+ { .type = "ConfigActive", .status = "OK" },
+ { .type = "XdpCovered", .status = "OK" },
+ };
+ static const struct condition reordered[] = {
+ { .type = "XdpCovered", .status = "OK" },
+ { .type = "ChainIntact", .status = "OK" },
+ { .type = "ConfigActive", .status = "OK" },
+ { .type = "LsmAttached", .status = "OK" },
+ { .type = "TpmMeasured", .status = "OK" },
+ { .type = "KeyringSealed", .status = "OK" },
+ { .type = "UpatchesMeasured", .status = "OK" },
+ };
+
+ EXPECT_EQ(keepalive_cond(conds, ARRAY_SIZE(conds)), 5461);
+ EXPECT_EQ(keepalive_cond(reordered, ARRAY_SIZE(reordered)), 5461);
+}
+
+static void cond_of_a_config_warning(void)
+{
+ static const struct condition conds[] = {
+ { .type = "LsmAttached", .status = "OK" },
+ { .type = "KeyringSealed", .status = "OK" },
+ { .type = "ChainIntact", .status = "OK" },
+ { .type = "UpatchesMeasured", .status = "OK" },
+ { .type = "TpmMeasured", .status = "OK" },
+ { .type = "ConfigActive", .status = "WARN" },
+ { .type = "XdpCovered", .status = "OK" },
+ };
+ static const struct condition reordered[] = {
+ { .type = "ConfigActive", .status = "WARN" },
+ { .type = "XdpCovered", .status = "OK" },
+ { .type = "TpmMeasured", .status = "OK" },
+ { .type = "UpatchesMeasured", .status = "OK" },
+ { .type = "ChainIntact", .status = "OK" },
+ { .type = "KeyringSealed", .status = "OK" },
+ { .type = "LsmAttached", .status = "OK" },
+ };
+
+ EXPECT_EQ(keepalive_cond(conds, ARRAY_SIZE(conds)), 6485);
+ EXPECT_EQ(keepalive_cond(reordered, ARRAY_SIZE(reordered)), 6485);
+}
+
+static void cond_leaves_a_missing_condition_at_zero(void)
+{
+ static const struct condition conds[] = {
+ { .type = "LsmAttached", .status = "OK" },
+ { .type = "KeyringSealed", .status = "OK" },
+ { .type = "UpatchesMeasured", .status = "OK" },
+ { .type = "TpmMeasured", .status = "OK" },
+ { .type = "ConfigActive", .status = "OK" },
+ { .type = "XdpCovered", .status = "OK" },
+ };
+
+ EXPECT_EQ(keepalive_cond(conds, ARRAY_SIZE(conds)), 5461 & ~(3 << 4));
+ EXPECT_EQ(keepalive_cond(conds, 0), 0);
+}
+
+static void cond_places_each_status_by_name(void)
+{
+ static const struct condition conds[] = {
+ { .type = "XdpCovered", .status = "ALERT" },
+ { .type = "Unknown", .status = "ALERT" },
+ { .type = "LsmAttached", .status = "ALERT" },
+ { .type = "KeyringSealed", .status = "WARN" },
+ };
+
+ EXPECT_EQ(keepalive_cond(conds, ARRAY_SIZE(conds)),
+ (3 << 12) | (2 << 2) | 3);
+}
+
+static void cond_follows_the_order_conditions_returns(void)
+{
+ struct conditions_input in = {
+ .reachable = true,
+ .parsed = true,
+ .lsm_attached = 1,
+ };
+ struct condition conds[CONDITIONS_MAX];
+ size_t n = conditions(&in, conds, CONDITIONS_MAX), i;
+ __u16 cond = keepalive_cond(conds, n);
+
+ ASSERT_EQ(n, CONDITIONS_MAX);
+ for (i = 0; i < n; i++)
+ EXPECT_NE((cond >> (2 * i)) & 3, 0);
+ EXPECT_EQ(cond & 3, 1);
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(mask_of_one_operation),
+ UNIT_CASE(mask_of_the_release_example),
+ UNIT_CASE(mask_of_sixteen_operations),
+ UNIT_CASE(mask_of_no_operation_is_empty),
+ UNIT_CASE(mask_fills_a_byte_then_pads_the_next),
+ UNIT_CASE(mask_of_a_full_window),
+ UNIT_CASE(mask_refuses_a_code_past_two_bits),
+ UNIT_CASE(mask_refuses_a_buffer_too_small),
+ UNIT_CASE(cond_of_all_ok),
+ UNIT_CASE(cond_of_a_config_warning),
+ 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_SUITE(keepalive, cases);
diff --git a/util/keepalive.c b/util/keepalive.c
new file mode 100644
index 00000000..a26ddc3e
--- /dev/null
+++ b/util/keepalive.c
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+#include <string.h>
+
+#include <shirudo/misc.h>
+
+#include "crypto.h"
+#include "keepalive.h"
+
+#define CODE_BITS 2
+#define CODE_MAX 3
+#define CODES_PER_BYTE 4
+
+enum cond_code {
+ COND_NOT_EVALUATED,
+ COND_OK,
+ COND_WARN,
+ COND_ALERT,
+};
+
+/* The backend reads a pair by its place here: append, never reorder. */
+static const char * const cond_order[] = {
+ "LsmAttached",
+ "KeyringSealed",
+ "ChainIntact",
+ "UpatchesMeasured",
+ "TpmMeasured",
+ "ConfigActive",
+ "XdpCovered",
+};
+
+int keepalive_mask(const __u8 *codes, size_t n, char *b64, size_t cap)
+{
+ size_t len = (n + CODES_PER_BYTE - 1) / CODES_PER_BYTE, i;
+ __u8 raw[KEEPALIVE_MASK_BYTES] = {};
+
+ if (n > KEEPALIVE_MASK_OPS)
+ return -EINVAL;
+ for (i = 0; i < n; i++) {
+ size_t slot = CODES_PER_BYTE - 1 - i % CODES_PER_BYTE;
+
+ if (codes[i] > CODE_MAX)
+ return -EINVAL;
+ raw[i / CODES_PER_BYTE] |= codes[i] << (CODE_BITS * slot);
+ }
+ if (cap < 4 * ((len + 2) / 3) + 1)
+ return -E2BIG;
+ return base64_encode(raw, len, b64, cap);
+}
+
+static enum cond_code cond_code_of(const char *status)
+{
+ if (!strcmp(status, "OK"))
+ return COND_OK;
+ if (!strcmp(status, "WARN"))
+ return COND_WARN;
+ if (!strcmp(status, "ALERT"))
+ return COND_ALERT;
+ return COND_NOT_EVALUATED;
+}
+
+static enum cond_code cond_code_named(const struct condition *conds, size_t n,
+ const char *type)
+{
+ size_t i;
+
+ for (i = 0; i < n; i++)
+ if (!strcmp(conds[i].type, type))
+ return cond_code_of(conds[i].status);
+ return COND_NOT_EVALUATED;
+}
+
+__u16 keepalive_cond(const struct condition *conds, size_t n)
+{
+ __u16 cond = 0;
+ size_t i;
+
+ BUILD_BUG_ON(ARRAY_SIZE(cond_order) != CONDITIONS_MAX);
+ BUILD_BUG_ON(CODE_BITS * ARRAY_SIZE(cond_order) > 8 * sizeof(cond));
+
+ for (i = 0; i < ARRAY_SIZE(cond_order); i++)
+ cond |= cond_code_named(conds, n, cond_order[i]) <<
+ (CODE_BITS * i);
+ return cond;
+}
diff --git a/util/keepalive.h b/util/keepalive.h
new file mode 100644
index 00000000..6c730fda
--- /dev/null
+++ b/util/keepalive.h
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The packed fields of the keepalive body: the per-operation outcome
+ * mask and the conditions word.
+ */
+
+#ifndef SHIRUDO_KEEPALIVE_H
+#define SHIRUDO_KEEPALIVE_H
+
+#include <stddef.h>
+
+#include <shirudo/misc.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)
+
+/*
+ * 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
+ * for a buffer too small.
+ */
+int keepalive_mask(const __u8 *codes, size_t n, char *b64,
+ size_t cap) __must_check;
+
+/* Each pair by type name, so that a condition conditions() adds moves none. */
+__u16 keepalive_cond(const struct condition *conds, size_t n);
+
+#endif /* SHIRUDO_KEEPALIVE_H */