Skip to content

eBPF-based security platform

testing: prove tree sync with two operations

KP Singheb16fc72ef5c
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index aa50a51d..a2820318 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -209,13 +209,33 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
* - ``bpm-tree-sync-model``
- ``cmd_bpm_tree_sync()``, ``tree_recover_pending()``,
``tree_publication_check()``, ``tree_publication_follows()``
- - Every state, pending record, tree eventlog replay and record of the
- pending operation, publications of 0 to 2 operations that
- ``tree_parse()`` lets through, every selector answer and every
- failure below sync, including loading and checking the active signing
- set; strings of one byte
- - The writes of the Lean model ``Shirudo.TreeSync``, in its order,
- none after a failure, all of them when nothing fails
+ - The full command with 0 or 1 operation, every pending state,
+ selector answer and first failing I/O call, or no failure.
+ Names have one byte and the previous root has 32 symbolic bytes.
+ - Writes match the Lean model ``Shirudo.TreeSync`` in order;
+ nothing is written after a failure and every expected write
+ happens when nothing fails
+ * - ``bpm-tree-sync-application``
+ - ``cmd_bpm_tree_sync()``
+ - Two operations with no pending record; every selector answer
+ and first failing I/O call, or no failure. The previous root
+ has 32 symbolic bytes.
+ - The same ordered-write properties as ``bpm-tree-sync-model``
+ * - ``bpm-tree-sync-pending``
+ - ``cmd_bpm_tree_sync()``, ``tree_recover_pending()``
+ - Two operations with a pending record; every selector answer,
+ measured state and first failing I/O call, or no failure. TPM
+ replay equals the previous root, the next root, or neither;
+ the previous root has 32 symbolic bytes.
+ - The same ordered-write properties as ``bpm-tree-sync-model``
+ * - ``bpm-tree-recovery-model``
+ - ``tree_recover_pending()``
+ - Publications with 0 to 2 operations, a matching or unrelated
+ pending record, each TPM replay class, the measured flag and
+ every first failing I/O call. The previous root has all 32
+ symbolic bytes.
+ - The return decision, ordered writes, cursor, tree ID, manifest
+ and both projection roots match the independent recovery model
* - ``bpm-upatch-member``
- ``bpm_upatch_member()``, ``bpm_upatch_pair()``
- Lengths 0 to 18
@@ -709,8 +729,9 @@ the model follows the C.
each write whole, one sync at a time; the unlink of ``pending.json``
succeeds; an extend and its eventlog record are one write
- CBMC: ``bpm-tree-follows`` for ``tree_publication_follows()``,
- ``bpm-tree-sync-model`` for the writes of a sync of up to two
- operations; ``tree_parse()`` by review
+ ``bpm-tree-sync-model`` and its two-operation variants for
+ sync writes, ``bpm-tree-recovery-model`` for pending recovery;
+ ``tree_parse()`` by review
Not covered yet
---------------
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index b353dca4..6de46bc9 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -1041,7 +1041,9 @@ and tree signatures accept algorithms other than ML-DSA (U4).
``bpm-oci-descriptor``, ``bpm-semver-model``, ``Shirudo.SemVer``,
``Shirudo.ArchivePath``, ``bpm-archive-path-model``,
``Shirudo.JsonString``, ``Shirudo.TreeSync``, ``bpm-tree-follows``,
- ``bpm-tree-sync-model``, ``bpm-tree-reader``, ``json-string``,
+ ``bpm-tree-sync-model``, ``bpm-tree-sync-application``,
+ ``bpm-tree-sync-pending``, ``bpm-tree-recovery-model``,
+ ``bpm-tree-reader``, ``json-string``,
``json-limits``
:Residual: R8, R9, U3, U4, U15
diff --git a/testing/proofs/cbmc/bpm-tree-recovery-model.c b/testing/proofs/cbmc/bpm-tree-recovery-model.c
new file mode 100644
index 00000000..93fca53f
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-tree-recovery-model.c
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * Check tree_recover_pending() against the independent recovery model,
+ * without also expanding the later tree operations.
+ */
+
+#define harness whole_tree_sync_harness
+#include "bpm-tree-sync-model.c"
+#undef harness
+
+static void recovery_start(struct tree_state *actual, struct mstate *model)
+{
+ if (have_state) {
+ *actual = disk_state;
+ } else {
+ actual->schema = 1;
+ bpm_copy_string(actual->tree_id, sizeof(actual->tree_id),
+ fetched.tree_id);
+ memset(actual->projection_root, '0', 64);
+ actual->projection_root[64] = '\0';
+ memset(actual->projection_root_tpm, '0', 64);
+ actual->projection_root_tpm[64] = '\0';
+ }
+ model->tree_id[0] = have_state ? disk_state.tree_id[0] :
+ fetched.tree_id[0];
+ model->tree_id[1] = '\0';
+ model->generation = have_state ? disk_state.generation : 0;
+ model->last_seq = have_state ? disk_state.last_seq : 0;
+ model->manifest[0] = have_state ? disk_state.manifest_digest[0] : '\0';
+ model->manifest[1] = '\0';
+ for (int i = 0; i < 32; i++)
+ model->root[i] = have_state ?
+ (__u8)disk_state.projection_root[i] : '0';
+}
+
+static void recovery_state_matches(const struct tree_state *actual,
+ const struct mstate *model)
+{
+ bool tpm_root_set = !have_state ||
+ actual->last_seq != disk_state.last_seq;
+
+ __CPROVER_assert(actual->generation == model->generation,
+ "recovery keeps the generation");
+ __CPROVER_assert(actual->last_seq == model->last_seq,
+ "recovery advances the expected cursor");
+ __CPROVER_assert(actual->tree_id[0] == model->tree_id[0] &&
+ actual->tree_id[1] == model->tree_id[1],
+ "recovery keeps the tree ID");
+ __CPROVER_assert(actual->manifest_digest[0] == model->manifest[0] &&
+ actual->manifest_digest[1] == model->manifest[1],
+ "recovery keeps the manifest");
+ for (int i = 0; i < 32; i++) {
+ __CPROVER_assert((__u8)actual->projection_root[i] ==
+ model->root[i],
+ "recovery records the expected root bytes");
+ if (tpm_root_set)
+ __CPROVER_assert((__u8)actual->projection_root_tpm[i] ==
+ model->root[i],
+ "recovery records the expected TPM root bytes");
+ }
+}
+
+void harness(void)
+{
+ struct tree_state actual = {};
+ struct mstate model = {};
+ __u32 first_failure;
+ bool model_ok;
+ int ret;
+
+ fail_at = first_failure;
+ any_node();
+ any_publication();
+ __CPROVER_assume(checked());
+ any_replay_root();
+ recovery_start(&actual, &model);
+ model_ok = recover(&model);
+ ret = tree_recover_pending(&fetched, &actual);
+ __CPROVER_assert(failed || (ret == 0) == model_ok,
+ "recovery accepts the same pending state as the model");
+ __CPROVER_assert(failed || n_got == n_want,
+ "recovery makes every expected write");
+ if (!failed && model_ok)
+ recovery_state_matches(&actual, &model);
+}
diff --git a/testing/proofs/cbmc/bpm-tree-sync-application.c b/testing/proofs/cbmc/bpm-tree-sync-application.c
new file mode 100644
index 00000000..40699fc6
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-tree-sync-application.c
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+/* Check the operation path when no pending record exists. */
+#define TREE_SYNC_APPLICATION_ONLY 1
+#include "bpm-tree-sync-model.c"
diff --git a/testing/proofs/cbmc/bpm-tree-sync-model.c b/testing/proofs/cbmc/bpm-tree-sync-model.c
index ffa3625b..78b3c1b5 100644
--- a/testing/proofs/cbmc/bpm-tree-sync-model.c
+++ b/testing/proofs/cbmc/bpm-tree-sync-model.c
@@ -55,6 +55,7 @@ struct write {
static struct write want[MAX_WRITES];
static int n_got, n_want;
+static __u32 fail_at, fail_call;
static bool failed;
/* What the node holds, what the registry serves and what the checks say. */
@@ -70,10 +71,14 @@ static bool sel_match[LEN ? LEN : 1], sel_fail[LEN ? LEN : 1];
static __s64 measure_seq;
static __u8 measure_d[32];
+/* A failed I/O stops sync before another modeled I/O call. */
static bool fail_here(void)
{
bool fail;
+ __CPROVER_assert(fail_call <= fail_at,
+ "no I/O call after a failed I/O");
+ fail = fail_call++ == fail_at;
if (fail)
failed = true;
return fail;
@@ -85,7 +90,7 @@ static void copy_short(char dst[2], const char *src)
dst[1] = '\0';
}
-static bool same_write(const struct write *a, const struct write *b);
+static bool same_write(const struct write *a, int index);
/* A write sync makes, checked against the model's at its place. */
static void record(const struct write *w)
@@ -93,7 +98,7 @@ static void record(const struct write *w)
__CPROVER_assert(!failed, "nothing written after a failure");
__CPROVER_assert(n_got < n_want, "no write the model does not make");
if (n_got < n_want)
- __CPROVER_assert(same_write(w, &want[n_got]),
+ __CPROVER_assert(same_write(w, n_got),
"the model's write at its place");
n_got++;
}
@@ -587,40 +592,55 @@ struct mstate {
static void want_write(int kind, int op, __s64 seq, const __u8 *d,
const struct mstate *st)
{
- struct write *w;
+ int index;
- __CPROVER_assume(n_want < MAX_WRITES);
- w = &want[n_want++];
- w->kind = kind;
- w->op = op;
- w->seq = st ? st->last_seq : seq;
+ __CPROVER_assert(n_want < MAX_WRITES, "expected write log fits");
+ index = n_want++;
+ want[index].kind = kind;
+ want[index].op = op;
+ want[index].seq = st ? st->last_seq : seq;
if (d)
- memcpy(w->d, d, 32);
+ for (int i = 0; i < 32; i++)
+ want[index].d[i] = d[i];
if (st) {
- memcpy(w->d, st->root, 32);
- w->generation = st->generation;
- copy_short(w->manifest, st->manifest);
- copy_short(w->tree_id, st->tree_id);
+ for (int i = 0; i < 32; i++)
+ want[index].d[i] = st->root[i];
+ want[index].generation = st->generation;
+ want[index].manifest[0] = st->manifest[0];
+ want[index].manifest[1] = '\0';
+ want[index].tree_id[0] = st->tree_id[0];
+ want[index].tree_id[1] = '\0';
}
}
-static bool same_write(const struct write *a, const struct write *b)
+static bool same_digest(const struct write *actual, int index)
{
- if (a->kind != b->kind)
+ for (int i = 0; i < 32; i++)
+ if (actual->d[i] != want[index].d[i])
+ return false;
+ return true;
+}
+
+static bool same_write(const struct write *a, int index)
+{
+ if (a->kind != want[index].kind)
return false;
switch (a->kind) {
case W_PENDING:
- return a->op == b->op;
+ return a->op == want[index].op;
case W_MEASURE:
- return a->seq == b->seq && !memcmp(a->d, b->d, 32);
+ return a->seq == want[index].seq && same_digest(a, index);
case W_APPEND:
- return a->op == b->op && a->seq == b->seq &&
- !memcmp(a->d, b->d, 32);
+ return a->op == want[index].op &&
+ a->seq == want[index].seq && same_digest(a, index);
case W_STATE:
- return a->seq == b->seq && a->generation == b->generation &&
- !memcmp(a->d, b->d, 32) &&
- !strcmp(a->manifest, b->manifest) &&
- !strcmp(a->tree_id, b->tree_id);
+ return a->seq == want[index].seq &&
+ a->generation == want[index].generation &&
+ same_digest(a, index) &&
+ a->manifest[0] == want[index].manifest[0] &&
+ a->manifest[1] == want[index].manifest[1] &&
+ a->tree_id[0] == want[index].tree_id[0] &&
+ a->tree_id[1] == want[index].tree_id[1];
default:
return true;
}
@@ -804,10 +824,9 @@ static void any_node(void)
disk_state.generation = state_there ? generation : 0;
disk_state.last_seq = state_there ? last_seq : 0;
for (int i = 0; i < 32; i++) {
- __u8 b, r;
+ __u8 b;
disk_state.projection_root[i] = state_there ? (char)b : '\0';
- replay_root[i] = r;
}
__CPROVER_assume(seq > 0 && id && digest);
disk_pending.seq = pending_there ? seq : 0;
@@ -817,14 +836,52 @@ static void any_node(void)
replay_kind = replay;
}
+/* Recovery only asks whether replay equals the previous or next root. */
+static void any_replay_root(void)
+{
+ __u8 previous[32], digest[32], next[32], other = 0;
+ int relation;
+
+ if (!have_pending || replay_kind != 2)
+ return;
+ if (have_state)
+ memcpy(previous, disk_state.projection_root, sizeof(previous));
+ else
+ memset(previous, '0', sizeof(previous));
+ digest_of(disk_pending.operation_digest, digest);
+ model_hash(previous, digest, next);
+ __CPROVER_assume(relation >= 0 && relation <= 2);
+ if (relation == 1)
+ memcpy(replay_root, next, sizeof(replay_root));
+ else
+ memcpy(replay_root, previous, sizeof(replay_root));
+ if (relation != 2)
+ return;
+ if (other == previous[0] || other == next[0])
+ other = 1;
+ if (other == previous[0] || other == next[0])
+ other = 2;
+ __CPROVER_assert(other != previous[0] && other != next[0],
+ "the third replay class differs from both roots");
+ replay_root[0] = other;
+}
+
void harness(void)
{
const char *argv[2];
+ __u32 first_failure;
+ fail_at = first_failure;
argv[0] = "tree";
argv[1] = "sync";
any_node();
+#ifdef TREE_SYNC_APPLICATION_ONLY
+ __CPROVER_assume(!have_pending);
+#elif defined(TREE_SYNC_PENDING_ONLY)
+ __CPROVER_assume(have_pending);
+#endif
any_publication();
+ any_replay_root();
run();
cmd_bpm_tree_sync(2, argv);
__CPROVER_assert(failed || n_got == n_want,
diff --git a/testing/proofs/cbmc/bpm-tree-sync-pending.c b/testing/proofs/cbmc/bpm-tree-sync-pending.c
new file mode 100644
index 00000000..acc0c7d9
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-tree-sync-pending.c
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+/* Check recovery and later operations in the same sync. */
+#define TREE_SYNC_PENDING_ONLY 1
+#include "bpm-tree-sync-model.c"
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 2877317f..ba6ba4cb 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -26,7 +26,10 @@ upatch-rodata -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf
string-number-model --unwind 6
measured-fold-model --unwind 7
bpm-tree-follows --unwind 4
-bpm-tree-sync-model --object-bits 10 --unwind 60 --max-field-sensitivity-array-size 257 LEN=0-2
+bpm-tree-sync-model --object-bits 10 --unwind 8 --unwindset memcmp.0:33,memneq_explicit.0:33,hex_encode.0:33,hex_decode.0:33,model_hash.0:33,any_node.0:33,run.0:33,same_digest.0:33,want_write.0:33,want_write.1:33,strcmp.0:66,strlen.0:66 --max-field-sensitivity-array-size 257 --slice-formula LEN=0-1
+bpm-tree-sync-application --object-bits 10 --unwind 8 --unwindset memcmp.0:33,memneq_explicit.0:33,hex_encode.0:33,hex_decode.0:33,model_hash.0:33,any_node.0:33,run.0:33,same_digest.0:33,want_write.0:33,want_write.1:33,strcmp.0:66,strlen.0:66 --max-field-sensitivity-array-size 257 --slice-formula LEN=2
+bpm-tree-sync-pending --object-bits 10 --unwind 8 --unwindset memcmp.0:33,memneq_explicit.0:33,hex_encode.0:33,hex_decode.0:33,model_hash.0:33,any_node.0:33,run.0:33,same_digest.0:33,want_write.0:33,want_write.1:33,strcmp.0:66,strlen.0:66 --max-field-sensitivity-array-size 257 --slice-formula LEN=2
+bpm-tree-recovery-model --object-bits 10 --unwind 8 --unwindset memcmp.0:33,memneq_explicit.0:33,hex_encode.0:33,hex_decode.0:33,model_hash.0:33,any_node.0:33,same_digest.0:33,want_write.0:33,want_write.1:33,strcmp.0:66,strlen.0:66,recovery_start.0:33,recovery_state_matches.0:33 --max-field-sensitivity-array-size 257 --slice-formula LEN=0-2
bpm-tree-signers --unwind 8
bpm-upatch-member --unwind 30 LEN=0-18
tpm-reader --unwind 1