eBPF-based security platform
bpm: check a tree publication in util/bpm_tree.c
Daniel Borkmann139dd10d2f92
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 1aaeddb9..cbe49fb3 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -91,6 +91,13 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- A name without ``~`` maps to a pin name without a dot and back toitself; a pin name without a dot that decodes is the encoding ofwhat it decodes to+ * - ``bpm-tree-follows``+ - ``tree_publication_follows()``+ - Every sequence number and generation; strings up to 2 bytes+ - Accepts exactly a publication of the same tree, of a later+ generation unless the same manifest, naming the recorded parent+ when the next generation, with the node's cursor between its base+ and head; without a state, one that starts at 0* - ``bpm-upatch-member``- ``bpm_upatch_member()``, ``bpm_upatch_pair()``- Lengths 0 to 18diff --git a/Makefile.shirudo b/Makefile.shirudoindex aa3c85fc..f2acfec2 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -988,7 +988,7 @@ FUZZ_CONFIG := util/config.c util/image_config.cFUZZ_SEAL := util/seal.c $(FUZZ_CONFIG)FUZZ_OCI := util/bpm_oci.c util/io.c $(FUZZ_SEAL) util/crypto_openssl.c \util/tls_openssl.c-FUZZ_PACKAGE := util/bpm_package.c util/bpm_tree.c $(FUZZ_OCI)+FUZZ_PACKAGE := util/bpm_package.c util/bpm_tree.c util/upatch.c $(FUZZ_OCI)FUZZ_TARGETS := $(patsubst %.c,%,$(wildcard testing/fuzz/fuzz-*.c))@@ -999,7 +999,7 @@ testing/fuzz/fuzz-bpm-index: testing/fuzz/fuzz-bpm-index.c \$(FUZZ_BASE) $(FUZZ_PACKAGE)testing/fuzz/fuzz-tree: testing/fuzz/fuzz-tree.c $(FUZZ_BASE) \util/bpm_tree.c util/crypto_openssl.c util/io.c \- $(FUZZ_SEAL)+ util/upatch.c $(FUZZ_SEAL)testing/fuzz/fuzz-upatch: testing/fuzz/fuzz-upatch.c $(FUZZ_BASE) \util/upatch.ctesting/fuzz/fuzz-prog-metadata: testing/fuzz/fuzz-prog-metadata.c \@@ -1074,7 +1074,8 @@ testing/fuzz/fuzz-config-update: testing/fuzz/fuzz-config-update.c \$(FUZZ_BASE) util/config_update.c \util/config_measure.c testing/fuzz/chain.c \util/crypto_openssl.c util/io.c $(FUZZ_SEAL)-FUZZ_TREE_STATE := util/bpm_tree_state.c util/bpm_tree.c util/io.c $(FUZZ_SEAL) \+FUZZ_TREE_STATE := util/bpm_tree_state.c util/bpm_tree.c util/upatch.c \+ util/io.c $(FUZZ_SEAL) \util/kernel_build_id.c util/kernel_release.c \util/crypto_openssl.c testing/fuzz/chain.ctesting/fuzz/fuzz-tree-state: testing/fuzz/fuzz-tree-state.c $(FUZZ_BASE) \diff --git a/builtin-bpm.c b/builtin-bpm.cindex f7fed959..e512b87c 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -2097,26 +2097,6 @@ static int tree_recover_pending(const struct tree_publication *pub,return 0;}-static int tree_validate_operation(const struct tree_operation *op)-{- __u8 digest[32];- char expected_digest[72];-- if (!op || op->seq <= 0 || !upatch_name_valid(op->id) ||- (strcmp(op->op, "add") && strcmp(op->op, "delete")))- return -EINVAL;- if (sha256_digest_decode(op->operation_digest, digest) ||- sha256_digest_decode(op->artifact.manifest_digest, digest) ||- sha256_digest_decode(op->artifact.layer_digest, digest) ||- sha256_digest_decode(op->artifact.payload_sha256, digest))- return -EINVAL;- if (tree_operation_digest(op, expected_digest))- return -EINVAL;- if (strcmp(op->operation_digest, expected_digest))- return -EKEYREJECTED;- return 0;-}-static void tree_sync_report_error(const char *phase,const struct tree_operation *op, int ret){@@ -2151,7 +2131,6 @@ static int cmd_bpm_tree_sync(int argc, const char **argv)char manifest_digest[BPM_MAX_DIGEST];char report[PATH_MAX] = {};bool dry_run = false, have_state = false;- bool same_manifest;int selected = 0, skipped = 0, applied = 0, module_skipped = 0;int tpm_available = 0;int ret;@@ -2198,11 +2177,6 @@ static int cmd_bpm_tree_sync(int argc, const char **argv)sizeof(manifest_digest));if (ret)goto out;- if (pub.parent_manifest_digest[0] &&- !tree_digest_valid(pub.parent_manifest_digest)) {- ret = -EINVAL;- goto out;- }/** The node records the tree ID in its state, which takes names* alone: one that is not would be applied, measured and logged@@ -2212,63 +2186,20 @@ static int cmd_bpm_tree_sync(int argc, const char **argv)ret = -EINVAL;goto out;}- /* Snapshot compaction belongs to a later schema. */- if (pub.base_seq != 0) {- ret = -ENOTSUP;- goto out;- }- if (pub.n_operations && pub.operations[0].seq != pub.base_seq + 1) {- ret = -EINVAL;- goto out;- }- if (pub.n_operations && pub.head_seq != pub.operations[pub.n_operations - 1].seq) {- ret = -EINVAL;- goto out;- }- if (!pub.n_operations && pub.head_seq != pub.base_seq) {- ret = -EINVAL;+ ret = tree_publication_check(&pub);+ if (ret)goto out;- }for (int i = 0; i < pub.n_operations; i++) {- ret = tree_validate_operation(&pub.operations[i]);+ ret = tree_operation_validate(&pub.operations[i]);if (ret) {tree_sync_report_error("operation_validate", &pub.operations[i], ret);goto out;}}- same_manifest = have_state && !strcmp(state.manifest_digest, manifest_digest);- if (have_state && strcmp(state.tree_id, pub.tree_id)) {- ret = -EINVAL;- goto out;- }- if (have_state && !same_manifest && pub.generation <= state.generation) {- ret = -ERANGE;- goto out;- }- /*- * Only a direct successor names the manifest this target recorded- * as its parent; one that missed a generation never saw the parent.- */- if (have_state && !same_manifest &&- pub.generation == state.generation + 1 &&- pub.parent_manifest_digest[0] &&- strcmp(pub.parent_manifest_digest, state.manifest_digest)) {- ret = -ERANGE;- goto out;- }- if (have_state && pub.head_seq < state.last_seq) {- ret = -ERANGE;- goto out;- }- /* The publication must contain the cursor base. */- if (have_state && pub.base_seq > state.last_seq) {- ret = -ERANGE;- goto out;- }- if (!have_state && pub.base_seq != 0) {- ret = -ERANGE;+ ret = tree_publication_follows(&pub, have_state ? &state : NULL,+ manifest_digest);+ if (ret)goto out;- }if (!have_state) {state.schema = 1;bpm_copy_string(state.tree_id, sizeof(state.tree_id), pub.tree_id);diff --git a/testing/proofs/cbmc/bpm-tree-follows.c b/testing/proofs/cbmc/bpm-tree-follows.cnew file mode 100644index 00000000..4e86d652--- /dev/null+++ b/testing/proofs/cbmc/bpm-tree-follows.c@@ -0,0 +1,73 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * tree_publication_follows() of util/bpm_tree.c, which decides whether+ * bpm tree sync may apply a signed publication over the state the node+ * recorded, for every sequence number and generation a __s64 holds: it+ * accepts exactly a publication of the same tree, of a later generation+ * unless it is the same manifest, naming the recorded parent when it is+ * the next generation, with the node's cursor between its base and head,+ * and without a state one that starts at 0. Strings are of up to two+ * bytes.+ */++#include <stdbool.h>++#include "proof.h"+#include "../../../util/hex.c"+#include "../../../util/bpm_tree.c"++static void short_string(const char *s)+{+ __CPROVER_assume(s[2] == '\0');+}++static void follows_the_state(void)+{+ struct tree_publication pub;+ struct tree_state state;+ char manifest[3];+ bool have, same, next;+ int ret;++ short_string(pub.tree_id);+ short_string(pub.parent_manifest_digest);+ short_string(state.tree_id);+ short_string(state.manifest_digest);+ short_string(manifest);+ ret = tree_publication_follows(&pub, have ? &state : NULL, manifest);+ if (!have) {+ __CPROVER_assert(!ret == (pub.base_seq == 0),+ "a first publication starts at 0");+ return;+ }+ same = !strcmp(state.manifest_digest, manifest);+ next = pub.generation > state.generation &&+ pub.generation - 1 == state.generation;+ __CPROVER_assert(ret || !strcmp(pub.tree_id, state.tree_id),+ "the same tree");+ __CPROVER_assert(ret || same || pub.generation > state.generation,+ "never an earlier or the same generation");+ __CPROVER_assert(ret || same || !next ||+ !pub.parent_manifest_digest[0] ||+ !strcmp(pub.parent_manifest_digest,+ state.manifest_digest),+ "the next generation names the recorded parent");+ __CPROVER_assert(ret || (pub.head_seq >= state.last_seq &&+ pub.base_seq <= state.last_seq),+ "the cursor between base and head");+ __CPROVER_assert(!ret || strcmp(pub.tree_id, state.tree_id) ||+ (!same && pub.generation <= state.generation) ||+ (!same && next && pub.parent_manifest_digest[0] &&+ strcmp(pub.parent_manifest_digest,+ state.manifest_digest)) ||+ pub.head_seq < state.last_seq ||+ pub.base_seq > state.last_seq,+ "and refused for nothing else");+}++void harness(void)+{+ follows_the_state();+}diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex 14a79fbd..4a4ce135 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -12,4 +12,5 @@ attest-name-digest --unwind 72 LEN=64-70paths-bpffs-name --unwind 18upatch-header --unwind 2 --unwindset memchr.0:257string-number-model --unwind 6+bpm-tree-follows --unwind 4bpm-upatch-member --unwind 30 LEN=0-18diff --git a/testing/unit/bpm_tree.c b/testing/unit/bpm_tree.cindex e77fd2b8..b4360dfc 100644--- a/testing/unit/bpm_tree.c+++ b/testing/unit/bpm_tree.c@@ -2,6 +2,7 @@/* Copyright (c) 2026 Authors of shirudo */#include <errno.h>+#include <stdint.h>#include <shirudo/string.h>@@ -92,9 +93,98 @@ static void publication_reads_back(void)xfree(op, sizeof(*op));}+/* What sync checks of an operation before it applies one. */+static void operation_validates_against_its_digest(void)+{+ struct tree_operation *op = operation();++ ASSERT_EQ(tree_operation_digest(op, op->operation_digest), 0);+ EXPECT_EQ(tree_operation_validate(op), 0);+ op->seq = 2;+ EXPECT_EQ(tree_operation_validate(op), -EKEYREJECTED);+ op->seq = 0;+ EXPECT_EQ(tree_operation_validate(op), -EINVAL);+ op->seq = 1;+ set(op->op, sizeof(op->op), "replace");+ EXPECT_EQ(tree_operation_validate(op), -EINVAL);+ set(op->op, sizeof(op->op), "add");+ set(op->id, sizeof(op->id), "../t");+ EXPECT_EQ(tree_operation_validate(op), -EINVAL);+ set(op->id, sizeof(op->id), "t");+ set(op->artifact.layer_digest, sizeof(op->artifact.layer_digest),+ "sha256:aa");+ EXPECT_EQ(tree_operation_validate(op), -EINVAL);+ tree_selector_free(&op->selector);+ xfree(op, sizeof(*op));+}++static void publication_runs_from_base_to_head(void)+{+ struct tree_operation ops[2] = { { .seq = 1 }, { .seq = 2 } };+ struct tree_publication pub = { .operations = ops, .n_operations = 2,+ .head_seq = 2 };++ EXPECT_EQ(tree_publication_check(&pub), 0);+ pub.head_seq = 3;+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);+ pub.head_seq = 2;+ ops[0].seq = 2;+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);+ ops[0].seq = 1;+ pub.base_seq = 1;+ EXPECT_EQ(tree_publication_check(&pub), -ENOTSUP);+ pub.base_seq = 0;+ set(pub.parent_manifest_digest, sizeof(pub.parent_manifest_digest),+ "sha256:aa");+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);+ set(pub.parent_manifest_digest, sizeof(pub.parent_manifest_digest),+ DIGEST);+ EXPECT_EQ(tree_publication_check(&pub), 0);+ pub.n_operations = 0;+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);+ pub.head_seq = 0;+ EXPECT_EQ(tree_publication_check(&pub), 0);+}++/* A node never goes back to an older tree, or skips past its cursor. */+static void publication_follows_the_recorded_state(void)+{+ struct tree_state state = { .generation = 5, .last_seq = 3 };+ struct tree_publication pub = { .generation = 6, .head_seq = 4 };++ set(state.tree_id, sizeof(state.tree_id), "t");+ set(state.manifest_digest, sizeof(state.manifest_digest), "m5");+ set(pub.tree_id, sizeof(pub.tree_id), "t");+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m6"), 0);+ EXPECT_EQ(tree_publication_follows(&pub, NULL, "m6"), 0);+ set(pub.parent_manifest_digest, sizeof(pub.parent_manifest_digest),+ "m5");+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m6"), 0);+ set(pub.parent_manifest_digest, sizeof(pub.parent_manifest_digest),+ "m4");+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m6"), -ERANGE);+ pub.generation = 7;+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m7"), 0);+ pub.generation = 5;+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m5"), 0);+ EXPECT_EQ(tree_publication_follows(&pub, &state, "other"), -ERANGE);+ pub.generation = 7;+ pub.head_seq = 2;+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m7"), -ERANGE);+ pub.head_seq = 4;+ set(pub.tree_id, sizeof(pub.tree_id), "u");+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m7"), -EINVAL);+ state.generation = INT64_MAX;+ set(pub.tree_id, sizeof(pub.tree_id), "t");+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m7"), -ERANGE);+}+static const struct unit_case cases[] = {UNIT_CASE(operation_takes_no_control_character),UNIT_CASE(publication_reads_back),+ UNIT_CASE(operation_validates_against_its_digest),+ UNIT_CASE(publication_runs_from_base_to_head),+ UNIT_CASE(publication_follows_the_recorded_state),{}};diff --git a/util/bpm_tree.c b/util/bpm_tree.cindex e28df74c..dde37892 100644--- a/util/bpm_tree.c+++ b/util/bpm_tree.c@@ -15,6 +15,7 @@#include "hex.h"#include "json.h"#include "malloc.h"+#include "upatch.h"int tree_selector_kv_add(struct tree_selector_kv **kvs, int *n, int max,struct tree_selector_kv **kv)@@ -624,3 +625,65 @@ int tree_emit_publication(const struct tree_publication *pub,*out_len = e.len;return 0;}++int tree_operation_validate(const struct tree_operation *op)+{+ char expected[72];+ __u8 digest[32];++ if (op->seq <= 0 || !upatch_name_valid(op->id) ||+ (strcmp(op->op, "add") && strcmp(op->op, "delete")))+ return -EINVAL;+ if (sha256_digest_decode(op->operation_digest, digest) ||+ sha256_digest_decode(op->artifact.manifest_digest, digest) ||+ sha256_digest_decode(op->artifact.layer_digest, digest) ||+ sha256_digest_decode(op->artifact.payload_sha256, digest))+ return -EINVAL;+ if (tree_operation_digest(op, expected))+ return -EINVAL;+ return strcmp(op->operation_digest, expected) ? -EKEYREJECTED : 0;+}++int tree_publication_check(const struct tree_publication *pub)+{+ __u8 digest[32];+ int n = pub->n_operations;++ if (pub->parent_manifest_digest[0] &&+ sha256_digest_decode(pub->parent_manifest_digest, digest))+ return -EINVAL;+ if (pub->base_seq != 0)+ return -ENOTSUP;+ if (n && (pub->operations[0].seq != pub->base_seq + 1 ||+ pub->head_seq != pub->operations[n - 1].seq))+ return -EINVAL;+ if (!n && pub->head_seq != pub->base_seq)+ return -EINVAL;+ return 0;+}++int tree_publication_follows(const struct tree_publication *pub,+ const struct tree_state *state,+ const char *manifest_digest)+{+ bool same;++ if (!state)+ return pub->base_seq ? -ERANGE : 0;+ if (strcmp(state->tree_id, pub->tree_id))+ return -EINVAL;+ same = !strcmp(state->manifest_digest, manifest_digest);+ if (!same && pub->generation <= state->generation)+ return -ERANGE;+ /*+ * Only a direct successor names the manifest this node recorded+ * as its parent; one that missed a generation never saw the parent.+ */+ if (!same && pub->generation - 1 == state->generation &&+ pub->parent_manifest_digest[0] &&+ strcmp(pub->parent_manifest_digest, state->manifest_digest))+ return -ERANGE;+ if (pub->head_seq < state->last_seq || pub->base_seq > state->last_seq)+ return -ERANGE;+ return 0;+}diff --git a/util/bpm_tree.h b/util/bpm_tree.hindex 0ae233d6..37245ae9 100644--- a/util/bpm_tree.h+++ b/util/bpm_tree.h@@ -115,6 +115,27 @@ int tree_operation_digest(const struct tree_operation *op,int tree_emit_publication(const struct tree_publication *pub,char **out, size_t *out_len);+/*+ * What tree sync checks before it applies anything. An operation has a+ * positive sequence number, an upatch name, add or delete, well-formed+ * digests and the operation digest its fields hash to: 0, -EINVAL, or+ * -EKEYREJECTED for a digest that does not match. A publication names a+ * well-formed parent digest if any, and its operations run from+ * base_seq + 1 to head_seq, or head_seq is base_seq without them: 0 or+ * -EINVAL, and -ENOTSUP for a base past 0, which snapshot compaction, a+ * later schema, would need. And it may follow the state the node+ * recorded, NULL for none, with manifest_digest its manifest's: the same+ * tree, a later generation unless it is the same manifest, the parent+ * the node recorded when it is the next generation and names one, no+ * head before the node's cursor and no base after it. 0, -EINVAL for+ * another tree, or -ERANGE.+ */+int tree_operation_validate(const struct tree_operation *op);+int tree_publication_check(const struct tree_publication *pub);+int tree_publication_follows(const struct tree_publication *pub,+ const struct tree_state *state,+ const char *manifest_digest);+/** Evaluate a publication selector against the running node. An empty* selector is an unconstrained wildcard and matches every node. 1 on a