Skip to content

eBPF-based security platform

bpm: prepare to walk tree segments to support >512 ops

John Fastabend9e4f8a2d079f
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 81d19fa8..61e00105 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -224,17 +224,25 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- ``-E2BIG`` exactly at the limit with nothing changed; a copy owns
its arrays, a failed one holds nothing; text without controls
* - ``bpm-tree-reader``
- - ``tree_map_pair()``, ``tree_take_operation()``
- - Every pair and operation, into an empty or a full publication
+ - ``tree_map_pair()``, ``tree_take_operation()``,
+ ``tree_publication_check()``
+ - Every pair and operation, into an empty or a full publication;
+ every base with 0 or 1 operation and segment keys of every content
+ the parser leaves
- A key taken once, with a value, below its limit; an operation
- taken exactly when whole and with room
+ taken exactly when whole and with room; a base of 0 accepted
+ exactly without the segment keys, a later one exactly with an
+ operation, a ``sha256:`` digest and a root of 64 hex digits
* - ``bpm-tree-follows``
- - ``tree_publication_follows()``
+ - ``tree_publication_follows()``, ``tree_segment_precedes()``
- 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
+ - Refuses exactly a publication of another tree, of an earlier
+ generation unless the same manifest, naming another parent when
+ the next generation, or with its head before the node's cursor;
+ asks for a walk exactly when one it accepts starts past the
+ cursor, or past 0 without a state. A step of the walk accepts
+ exactly the segment the successor names, of its tree, ending at
+ its base and of a lower generation
* - ``bpm-tree-signers``
- ``tree_fetch_publication()``
- Active or inactive image config; every signing-set count through
diff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rst
index 5c93f51f..9629f6bf 100644
--- a/Documentation/reference/failure-categories.rst
+++ b/Documentation/reference/failure-categories.rst
@@ -612,6 +612,12 @@ rc=<rc> (<strerror>)`` and exits 1:
* - ``operation_validate``
- An operation of the publication is malformed or its digest does
not match.
+ * - ``segment``
+ - The publication is a segment after the first (``base.seq`` above
+ 0), which this release does not apply (``ENOTSUP``): it neither
+ walks back through the sealed segments nor checks a segment's base
+ against the node's history. A line before it names the segment's
+ base and the node's last applied ``seq``. Nothing is applied.
* - ``outcomes_check``
- The ``outcomes`` record does not cover the recorded ``last_seq``:
it is missing (``ENOENT``), has a gap or a malformed line, or ends
diff --git a/Documentation/reference/formats/bpm-formats.rst b/Documentation/reference/formats/bpm-formats.rst
index 5d1adda6..40ad450b 100644
--- a/Documentation/reference/formats/bpm-formats.rst
+++ b/Documentation/reference/formats/bpm-formats.rst
@@ -362,7 +362,9 @@ Publication document
"tree_id": "<tree-id>",
"generation": <n>,
"parent_manifest_digest": "sha256:<hex>",
- "base": {"seq": 0},
+ "base": {"seq": <b>,
+ "segment_manifest_digest": "sha256:<hex>",
+ "history_root": "<hex>"},
"operations": [<operation>, ...],
"head": {"seq": <n>}}
@@ -383,10 +385,19 @@ Publication document
- Digest of the publication manifest this one replaces. Absent in
the first publication.
* - ``base.seq``
- - Required. The target accepts only 0.
+ - Required. 0 for the first segment of the tree, otherwise the
+ ``head.seq`` of the sealed segment before it.
+ * - ``base.segment_manifest_digest``
+ - Required when ``base.seq`` is above 0 and refused at 0. Digest of
+ the sealed publication manifest of the segment before this one.
+ * - ``base.history_root``
+ - Required when ``base.seq`` is above 0 and refused at 0. 64 hex
+ digits: the ``history_root`` every node's ``outcomes`` record
+ holds at ``base.seq``, see `Node records`_.
* - ``operations``
- Required. At most 512. Each ``seq`` is one more than the previous
- one, and the first is ``base.seq + 1``.
+ one, and the first is ``base.seq + 1``. At least one when
+ ``base.seq`` is above 0.
* - ``head.seq``
- Required. The ``seq`` of the last operation, or ``base.seq`` when
there is none. Below 2\ :sup:`63` - 1.
@@ -519,13 +530,18 @@ Target acceptance
#. The signature verifies against the runtime image's initial
certificate.
#. The document parses with the rules above, ``tree_id`` is a package
- name, ``base.seq`` is 0, and every operation has a valid ``id``,
- ``op`` and digests and a correct ``operation_digest``.
+ name, and every operation has a valid ``id``, ``op`` and digests and a
+ correct ``operation_digest``.
#. ``tree_id`` equals the one the node recorded.
#. A publication other than the recorded one has a higher
``generation``. A direct successor that names a
``parent_manifest_digest`` names the recorded manifest.
#. ``head.seq`` is not below the node's last applied ``seq``.
+#. ``base.seq`` is 0. The target does not yet walk back through the
+ sealed segments to a node's last applied ``seq``, nor check a later
+ segment's base against the node's history, and refuses a later
+ segment with the ``segment`` phase of
+ :doc:`/reference/failure-categories`.
For each new operation whose selector matches, the target then fetches the
artifact manifest by ``manifest_digest``, takes the layer with
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 0c2861e4..6203abf6 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -2121,8 +2121,22 @@ static int cmd_bpm_tree_sync(int argc, const char **argv)
}
ret = tree_publication_follows(&pub, have_state ? &state : NULL,
manifest_digest);
- if (ret)
+ if (ret < 0)
+ goto out;
+ /*
+ * A later segment is applied only after its base is checked
+ * against the node's history root, and reached only by a walk
+ * back through the sealed segments when the node is behind it.
+ */
+ if (ret == TREE_FOLLOWS_WALK || pub.base_seq) {
+ pr_err("bpm tree sync: the publication is a segment that starts "
+ "after seq %lld and this node is at seq %lld; walking "
+ "back through sealed segments and checking a segment's "
+ "base are not supported", pub.base_seq, state.last_seq);
+ ret = -ENOTSUP;
+ tree_sync_report_error("segment", NULL, 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/fuzz/corpus/tree/segment b/testing/fuzz/corpus/tree/segment
new file mode 100644
index 00000000..911f0a1f
--- /dev/null
+++ b/testing/fuzz/corpus/tree/segment
@@ -0,0 +1 @@
+{"schema":1,"tree_id":"t","generation":4,"parent_manifest_digest":"sha256:d1a131e345e29aa6c2e01cb7edb6e8797804d87a0140f7372e9a0ca93fa62320","base":{"seq":2,"segment_manifest_digest":"sha256:d1a131e345e29aa6c2e01cb7edb6e8797804d87a0140f7372e9a0ca93fa62320","history_root":"5f1c0a4e2b7d9c3a8e6f1b0d2c4a7e9f3b5d8c1a6e0f2b4d7c9a3e5f8b1d0c2a"},"operations":[{"seq":3,"op":"add","id":"t","selector":{"architecture":["s390x"]},"artifact":{"repository":"oci://127.0.0.1:5532/ns/tree","manifest_digest":"sha256:cac08beec0a2d3554112aae593c63dfadda4e9192793351cf07f39c7f6706df4","layer_digest":"sha256:f8712d5eab7c627741d2b229c520156859e764641205d07a78a598a72a147c13","payload_sha256":"sha256:c5073b25dfb8f6b4136f62b9aa8e874bd9a38cbf8ce964a0a55cc86eee11a41f"},"operation_digest":"sha256:a87247aa5b91f0df7c23d09c17b85e65d0de675323f02fa2a2edf7c7f4751164"}],"head":{"seq":3}}
\ No newline at end of file
diff --git a/testing/fuzz/fuzz-tree.c b/testing/fuzz/fuzz-tree.c
index fa86dbb2..bf90698d 100644
--- a/testing/fuzz/fuzz-tree.c
+++ b/testing/fuzz/fuzz-tree.c
@@ -5,7 +5,9 @@
* tree_parse() on an upatch tree publication, the canonical
* digest of every operation it accepts and the publication emitted again,
* bare and with the publisher's fixes, which must read back with the same
- * operations and digests.
+ * base, operations and digests and pass tree_publication_check() exactly
+ * when the first did. A segment the check passes names its predecessor
+ * when it starts past 0, and never precedes itself.
*/
#include <stdlib.h>
@@ -42,7 +44,12 @@ static void read_back(const struct tree_publication *pub,
again.n_operations != pub->n_operations ||
strcmp(again.tree_id, pub->tree_id) ||
again.generation != pub->generation ||
- again.head_seq != pub->head_seq)
+ again.base_seq != pub->base_seq ||
+ strcmp(again.segment_manifest_digest,
+ pub->segment_manifest_digest) ||
+ strcmp(again.history_root, pub->history_root) ||
+ again.head_seq != pub->head_seq ||
+ !tree_publication_check(&again) != !tree_publication_check(pub))
abort();
for (int i = 0; i < pub->n_operations; i++) {
if (tree_operation_digest(&pub->operations[i], digest) !=
@@ -63,6 +70,12 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
if (tree_parse((const char *)data, size, &pub))
return 0;
+ if (!tree_publication_check(&pub) &&
+ (!pub.segment_manifest_digest[0] != !pub.base_seq ||
+ !pub.history_root[0] != !pub.base_seq ||
+ (pub.base_seq && !pub.n_operations) ||
+ !tree_segment_precedes(&pub, pub.segment_manifest_digest, &pub)))
+ abort();
read_back(&pub, NULL);
read_back(&pub, &extra);
tree_publication_free(&pub);
diff --git a/testing/proofs/cbmc/bpm-tree-follows.c b/testing/proofs/cbmc/bpm-tree-follows.c
index 4e86d652..9bcc545f 100644
--- a/testing/proofs/cbmc/bpm-tree-follows.c
+++ b/testing/proofs/cbmc/bpm-tree-follows.c
@@ -7,9 +7,12 @@
* 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.
+ * the next generation, with its head not before the node's cursor, and
+ * asks for a walk back exactly when such a publication starts past the
+ * cursor, or past 0 without a state. And tree_segment_precedes(), one
+ * step of that walk, which accepts exactly the segment the successor
+ * names, of its tree, ending at its base and of a lower generation.
+ * Strings are of up to two bytes.
*/
#include <stdbool.h>
@@ -28,7 +31,7 @@ static void follows_the_state(void)
struct tree_publication pub;
struct tree_state state;
char manifest[3];
- bool have, same, next;
+ bool have, same, next, refused;
int ret;
short_string(pub.tree_id);
@@ -38,36 +41,55 @@ static void follows_the_state(void)
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");
+ __CPROVER_assert(ret == (pub.base_seq ? TREE_FOLLOWS_WALK : 0),
+ "without a state, a walk unless it 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");
+ refused = 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;
+ __CPROVER_assert(ret == 0 || ret == TREE_FOLLOWS_WALK ||
+ ret == -EINVAL || ret == -ERANGE,
+ "0, a walk or a refusal");
+ __CPROVER_assert((ret < 0) == refused,
+ "refused exactly for another tree, an older generation, "
+ "another parent or a head before the cursor");
+ __CPROVER_assert(ret < 0 ||
+ (ret == TREE_FOLLOWS_WALK) ==
+ (pub.base_seq > state.last_seq),
+ "a walk exactly when it starts past the cursor");
+}
+
+static void precedes_the_successor(void)
+{
+ struct tree_publication pred, succ;
+ char digest[3];
+ bool named;
+ int ret;
+
+ short_string(pred.tree_id);
+ short_string(succ.tree_id);
+ short_string(succ.segment_manifest_digest);
+ short_string(digest);
+ ret = tree_segment_precedes(&pred, digest, &succ);
+ named = succ.base_seq > 0 &&
+ !strcmp(succ.segment_manifest_digest, digest) &&
+ !strcmp(pred.tree_id, succ.tree_id) &&
+ pred.head_seq == succ.base_seq &&
+ pred.generation < succ.generation;
+ __CPROVER_assert(ret == 0 || ret == -ERANGE, "0 or -ERANGE");
+ __CPROVER_assert(!ret == named,
+ "exactly the earlier segment of the tree that the "
+ "successor names, ending at its base");
}
void harness(void)
{
follows_the_state();
+ precedes_the_successor();
}
diff --git a/testing/proofs/cbmc/bpm-tree-reader.c b/testing/proofs/cbmc/bpm-tree-reader.c
index 4f35c459..a7657f6e 100644
--- a/testing/proofs/cbmc/bpm-tree-reader.c
+++ b/testing/proofs/cbmc/bpm-tree-reader.c
@@ -5,7 +5,10 @@
* What tree_parse() of util/bpm_tree.c keeps of a publication from a
* registry, as its parser hands it over: a kconfig or label key only once,
* with a value and below its limit, and an operation only whole, with a
- * positive seq, no empty selector list and room in the publication.
+ * positive seq, no empty selector list and room in the publication. And
+ * the base tree_publication_check() then accepts: at 0 without the
+ * segment keys, past 0 with an operation, a "sha256:" digest and a root
+ * of 64 hex digits each.
*/
/* util/malloc.c and util/string.c behind a file that includes their headers. */
@@ -13,6 +16,7 @@
#define STRING_INTERNAL
#include "proof.h"
+#include "../../../util/hex.c"
#include "../../../util/bpm_tree.c"
#include "../../../util/malloc.c"
#include "../../../util/string.c"
@@ -110,8 +114,50 @@ static void take_operation(void)
"the next operation read into a cleared one");
}
+static bool hex_digits(const char *s)
+{
+ for (int i = 0; i < 64; i++)
+ if (!((s[i] >= '0' && s[i] <= '9') ||
+ (s[i] >= 'a' && s[i] <= 'f') ||
+ (s[i] >= 'A' && s[i] <= 'F')))
+ return false;
+ return !s[64];
+}
+
+/* LEN operations, which run from the base to the head. */
+static void check_base(void)
+{
+ struct tree_operation *op = any_bytes(sizeof(*op));
+ struct tree_publication pub;
+ bool named, keys;
+ __s64 base;
+ int ret;
+
+ __CPROVER_assume(base >= 0 && base < INT64_MAX);
+ pub.base_seq = base;
+ pub.operations = op;
+ pub.n_operations = LEN;
+ pub.head_seq = base + LEN;
+ op->seq = base + 1;
+ pub.parent_manifest_digest[0] = '\0';
+ /* As the parser leaves them. */
+ pub.segment_manifest_digest[sizeof(pub.segment_manifest_digest) - 1] =
+ '\0';
+ pub.history_root[sizeof(pub.history_root) - 1] = '\0';
+ ret = tree_publication_check(&pub);
+ keys = pub.segment_manifest_digest[0] || pub.history_root[0];
+ named = !strncmp(pub.segment_manifest_digest, "sha256:", 7) &&
+ hex_digits(pub.segment_manifest_digest + 7) &&
+ hex_digits(pub.history_root);
+ __CPROVER_assert(ret == 0 || ret == -EINVAL, "0 or -EINVAL");
+ __CPROVER_assert(!ret == (base ? LEN && named : !keys),
+ "the first segment without the segment keys, a later "
+ "one with an operation and both keys well formed");
+}
+
void harness(void)
{
map_pair();
take_operation();
+ check_base();
}
diff --git a/testing/proofs/cbmc/bpm-tree-sync-model.c b/testing/proofs/cbmc/bpm-tree-sync-model.c
index ab3f5649..3ced2c08 100644
--- a/testing/proofs/cbmc/bpm-tree-sync-model.c
+++ b/testing/proofs/cbmc/bpm-tree-sync-model.c
@@ -754,9 +754,14 @@ static bool same_write(const struct write *a, int index)
}
}
+/*
+ * Checked, and the first segment naming no predecessor, which Checked
+ * leaves out as it does the digests: the sync refuses every later one.
+ */
static bool checked(void)
{
if (fetched.generation < 0 || fetched.base_seq != 0 ||
+ fetched.segment_manifest_digest[0] || fetched.history_root[0] ||
fetched.head_seq != fetched.n_operations)
return false;
for (int i = 0; i < fetched.n_operations; i++)
@@ -930,6 +935,8 @@ static void any_publication(void)
pub.n_operations = LEN;
any_name(pub.tree_id, false);
any_name(pub.parent_manifest_digest, true);
+ any_name(pub.segment_manifest_digest, true);
+ any_name(pub.history_root, true);
__CPROVER_assume(pub.generation >= 0 && pub.generation != INT64_MAX &&
pub.base_seq >= 0 && pub.head_seq >= pub.base_seq &&
pub.head_seq != INT64_MAX);
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 6a0daec1..979e6734 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -77,7 +77,7 @@ bpm-oci-header --unwind 22
bpm-oci-descriptor --unwind 130 LEN=0,1
bpm-names --unwind 131
bpm-tree-selector --unwind 10 --malloc-may-fail --malloc-fail-null LEN=0,1
-bpm-tree-reader --unwind 6 --malloc-may-fail --malloc-fail-null LEN=0,1
+bpm-tree-reader --unwind 6 --unwindset hex_digits.0:65,hex_decode.0:33,strlen.0:129,strncmp.0:8 --malloc-may-fail --malloc-fail-null LEN=0,1
bpm-source-config --unwind 14
bpm-oci-strings --unwind 9
bpm-semver-model --slice-formula --unwind 1 --unwindset strlen.0:10,strcmp.0:10,strcspn.0:2,strcspn.1:4,digits.0:10,parse_base.0:10,parse_base.1:10,semver_parse.0:4,semver_identifiers.0:4,semver_identifiers.1:3,prerelease_compare.0:3,memchr.0:4,memcmp.0:4,bpm_semver_compare.0:4,strnlen.0:10,any_string.0:10,break_at.0:10,split_on.0:10,next_ident.0:4,numeral.0:8,numeric.0:8,ident_ok.0:4,identifiers.0:3,lex_bytes.0:10,memcmp_model.0:4,lex_idents.0:3,semver_model_compare.0:4 LEN=9
diff --git a/testing/proofs/lean4/Shirudo/TreeSync.lean b/testing/proofs/lean4/Shirudo/TreeSync.lean
index 2c62ddc1..253a3bb7 100644
--- a/testing/proofs/lean4/Shirudo/TreeSync.lean
+++ b/testing/proofs/lean4/Shirudo/TreeSync.lean
@@ -76,7 +76,8 @@ applies it again, and that goes on to measure it: `upatch_install()` takes
the payload already loaded under the name as installed, and
`upatch_remove()` an upatch already gone as removed.
testing/proofs/cbmc/bpm-tree-follows.c proves
-`tree_publication_follows()` is `Follows`, and bpm-tree-sync-model.c checks
+`tree_publication_follows()` returns 0 exactly when `Follows` holds, the
+sync refusing the walk it asks for otherwise, and bpm-tree-sync-model.c checks
the writes of `cmd_bpm_tree_sync()` against `run` for publications of up to
two operations.
-/
@@ -199,8 +200,10 @@ def Checked (pub : Pub T M I D) : Prop :=
instance (pub : Pub T M I D) : Decidable (Checked pub) := by
unfold Checked; infer_instance
-/-- `tree_publication_follows()`, as `bpm-tree-follows.c` states it and
-proves the C meets for every `__s64`: without a state, a base of 0; with
+/-- What the sync takes of `tree_publication_follows()`, a return of 0,
+as `bpm-tree-follows.c` states it and proves the C meets for every
+`__s64`; it refuses the walk the function asks for past the cursor.
+Without a state, a base of 0; with
one, the same tree, a later generation unless it is the same manifest, the
recorded manifest as parent when it is the next generation and names one,
and the cursor between base and head. -/
diff --git a/testing/unit/bpm_tree.c b/testing/unit/bpm_tree.c
index 3e1e61bc..89ecd6f2 100644
--- a/testing/unit/bpm_tree.c
+++ b/testing/unit/bpm_tree.c
@@ -12,6 +12,8 @@
#include "unit.h"
#define DIGEST "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+#define SEALED "sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
+#define ROOT "0123456789abcdef0123456789abcdef0123456789ABCDEF0123456789abcdef"
static void set(char *dst, size_t cap, const char *src)
{
@@ -185,6 +187,64 @@ static void publication_reads_past_malformed_fixes(void)
xfree(op, sizeof(*op));
}
+/* A later segment names its sealed predecessor and the history before it. */
+static void segment_reads_back(void)
+{
+ struct tree_publication pub = { .schema = 1, .generation = 4,
+ .base_seq = 2, .head_seq = 3 };
+ struct tree_publication back = {};
+ struct tree_operation *op = operation();
+ char *doc = NULL;
+ size_t len = 0;
+
+ set(pub.tree_id, sizeof(pub.tree_id), "t");
+ set(pub.parent_manifest_digest, sizeof(pub.parent_manifest_digest),
+ SEALED);
+ set(pub.segment_manifest_digest, sizeof(pub.segment_manifest_digest),
+ SEALED);
+ set(pub.history_root, sizeof(pub.history_root), ROOT);
+ op->seq = 3;
+ ASSERT_EQ(tree_operation_digest(op, op->operation_digest), 0);
+ ASSERT_EQ(tree_publication_append(&pub, op), 0);
+ ASSERT_EQ(tree_emit_publication(&pub, NULL, &doc, &len), 0);
+ EXPECT_TRUE(strstr(doc, "\"base\":{\"seq\":2,"
+ "\"segment_manifest_digest\":\"" SEALED "\","
+ "\"history_root\":\"" ROOT "\"},"));
+ ASSERT_EQ(tree_parse(doc, len, &back), 0);
+ EXPECT_EQ(back.base_seq, 2);
+ EXPECT_STREQ(back.segment_manifest_digest, SEALED);
+ EXPECT_STREQ(back.history_root, ROOT);
+ EXPECT_EQ(tree_publication_check(&back), 0);
+ ASSERT_EQ(back.n_operations, 1);
+ EXPECT_EQ(tree_operation_validate(&back.operations[0]), 0);
+ xfree(doc, len);
+ tree_publication_free(&back);
+ pub.history_root[0] = '\0';
+ ASSERT_EQ(tree_emit_publication(&pub, NULL, &doc, &len), 0);
+ EXPECT_FALSE(strstr(doc, "history_root"));
+ xfree(doc, len);
+ tree_publication_free(&pub);
+ tree_selector_free(&op->selector);
+ xfree(op, sizeof(*op));
+}
+
+/* A root too long for its key, or a digest that is no string, does not parse. */
+static void segment_refuses_malformed_keys(void)
+{
+ static const char *const docs[] = {
+ "{\"schema\":1,\"tree_id\":\"t\",\"generation\":1,"
+ "\"base\":{\"seq\":0,\"history_root\":\"" ROOT "0\"},"
+ "\"operations\":[],\"head\":{\"seq\":0}}",
+ "{\"schema\":1,\"tree_id\":\"t\",\"generation\":1,"
+ "\"base\":{\"seq\":0,\"segment_manifest_digest\":3},"
+ "\"operations\":[],\"head\":{\"seq\":0}}",
+ };
+ struct tree_publication pub;
+
+ for (size_t i = 0; i < ARRAY_SIZE(docs); i++)
+ EXPECT_NE(tree_parse(docs[i], strlen(docs[i]), &pub), 0);
+}
+
/* What sync checks of an operation before it applies one. */
static void operation_validates_against_its_digest(void)
{
@@ -223,9 +283,6 @@ static void publication_runs_from_base_to_head(void)
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);
@@ -236,6 +293,64 @@ static void publication_runs_from_base_to_head(void)
EXPECT_EQ(tree_publication_check(&pub), -EINVAL);
pub.head_seq = 0;
EXPECT_EQ(tree_publication_check(&pub), 0);
+ pub.base_seq = -1;
+ pub.head_seq = -1;
+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);
+}
+
+/*
+ * A segment past the first names its sealed predecessor and the history
+ * root at its base and holds an operation; the first names neither.
+ */
+static void segment_names_its_base(void)
+{
+ struct tree_operation ops[2] = { { .seq = 3 }, { .seq = 4 } };
+ struct tree_publication pub = { .operations = ops, .n_operations = 2,
+ .base_seq = 2, .head_seq = 4 };
+
+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);
+ set(pub.segment_manifest_digest, sizeof(pub.segment_manifest_digest),
+ SEALED);
+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);
+ set(pub.history_root, sizeof(pub.history_root), ROOT);
+ EXPECT_EQ(tree_publication_check(&pub), 0);
+ pub.segment_manifest_digest[0] = '\0';
+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);
+ set(pub.segment_manifest_digest, sizeof(pub.segment_manifest_digest),
+ "sha256:bb");
+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);
+ set(pub.segment_manifest_digest, sizeof(pub.segment_manifest_digest),
+ "sha512:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);
+ set(pub.segment_manifest_digest, sizeof(pub.segment_manifest_digest),
+ SEALED);
+ set(pub.history_root, sizeof(pub.history_root),
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde");
+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);
+ set(pub.history_root, sizeof(pub.history_root),
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdeg");
+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);
+ set(pub.history_root, sizeof(pub.history_root), ROOT);
+ EXPECT_EQ(tree_publication_check(&pub), 0);
+ pub.n_operations = 0;
+ pub.head_seq = 2;
+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);
+
+ ops[0].seq = 1;
+ ops[1].seq = 2;
+ pub = (struct tree_publication){ .operations = ops, .n_operations = 2,
+ .head_seq = 2 };
+ set(pub.segment_manifest_digest, sizeof(pub.segment_manifest_digest),
+ SEALED);
+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);
+ pub.segment_manifest_digest[0] = '\0';
+ set(pub.history_root, sizeof(pub.history_root), ROOT);
+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);
+ set(pub.history_root, sizeof(pub.history_root),
+ "0000000000000000000000000000000000000000000000000000000000000000");
+ EXPECT_EQ(tree_publication_check(&pub), -EINVAL);
+ pub.history_root[0] = '\0';
+ EXPECT_EQ(tree_publication_check(&pub), 0);
}
/* A node never goes back to an older tree, or skips past its cursor. */
@@ -271,14 +386,97 @@ static void publication_follows_the_recorded_state(void)
EXPECT_EQ(tree_publication_follows(&pub, &state, "m7"), -ERANGE);
}
+/*
+ * A node behind a segment's base walks back to it, once the publication
+ * would otherwise follow; a node at or past the base does not.
+ */
+static void segment_asks_for_a_walk(void)
+{
+ struct tree_state state = { .generation = 5, .last_seq = 3,
+ .head_seq = 4 };
+ struct tree_publication pub = { .generation = 8, .base_seq = 4,
+ .head_seq = 6 };
+
+ 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, NULL, "m8"),
+ TREE_FOLLOWS_WALK);
+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m8"),
+ TREE_FOLLOWS_WALK);
+ state.last_seq = 4;
+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m8"), 0);
+ state.last_seq = 5;
+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m8"), 0);
+ state.last_seq = 6;
+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m8"), 0);
+ state.last_seq = 7;
+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m8"), -ERANGE);
+
+ state.last_seq = 3;
+ pub.generation = 6;
+ set(pub.parent_manifest_digest, sizeof(pub.parent_manifest_digest),
+ "m5");
+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m6"),
+ TREE_FOLLOWS_WALK);
+ set(pub.parent_manifest_digest, sizeof(pub.parent_manifest_digest),
+ "m4");
+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m6"), -ERANGE);
+ pub.generation = 5;
+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m6"), -ERANGE);
+ pub.generation = 8;
+ set(pub.tree_id, sizeof(pub.tree_id), "u");
+ EXPECT_EQ(tree_publication_follows(&pub, &state, "m8"), -EINVAL);
+}
+
+/* One step back: the sealed segment the successor names, right before it. */
+static void segment_precedes_its_successor(void)
+{
+ struct tree_publication pred = { .generation = 3, .base_seq = 0,
+ .head_seq = 2 };
+ struct tree_publication succ = { .generation = 4, .base_seq = 2,
+ .head_seq = 4 };
+
+ set(pred.tree_id, sizeof(pred.tree_id), "t");
+ set(succ.tree_id, sizeof(succ.tree_id), "t");
+ set(succ.segment_manifest_digest, sizeof(succ.segment_manifest_digest),
+ SEALED);
+ EXPECT_EQ(tree_segment_precedes(&pred, SEALED, &succ), 0);
+ EXPECT_EQ(tree_segment_precedes(&pred, DIGEST, &succ), -ERANGE);
+ set(pred.tree_id, sizeof(pred.tree_id), "u");
+ EXPECT_EQ(tree_segment_precedes(&pred, SEALED, &succ), -ERANGE);
+ set(pred.tree_id, sizeof(pred.tree_id), "t");
+ pred.head_seq = 1;
+ EXPECT_EQ(tree_segment_precedes(&pred, SEALED, &succ), -ERANGE);
+ pred.head_seq = 3;
+ EXPECT_EQ(tree_segment_precedes(&pred, SEALED, &succ), -ERANGE);
+ pred.head_seq = 2;
+ pred.generation = 4;
+ EXPECT_EQ(tree_segment_precedes(&pred, SEALED, &succ), -ERANGE);
+ pred.generation = 5;
+ EXPECT_EQ(tree_segment_precedes(&pred, SEALED, &succ), -ERANGE);
+ pred.generation = 1;
+ EXPECT_EQ(tree_segment_precedes(&pred, SEALED, &succ), 0);
+ succ.base_seq = 0;
+ pred.head_seq = 0;
+ EXPECT_EQ(tree_segment_precedes(&pred, SEALED, &succ), -ERANGE);
+ succ.segment_manifest_digest[0] = '\0';
+ EXPECT_EQ(tree_segment_precedes(&pred, "", &succ), -ERANGE);
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(operation_takes_no_control_character),
UNIT_CASE(publication_reads_back),
UNIT_CASE(publication_carries_keys_outside_the_digest),
UNIT_CASE(publication_reads_past_malformed_fixes),
+ UNIT_CASE(segment_reads_back),
+ UNIT_CASE(segment_refuses_malformed_keys),
UNIT_CASE(operation_validates_against_its_digest),
UNIT_CASE(publication_runs_from_base_to_head),
+ UNIT_CASE(segment_names_its_base),
UNIT_CASE(publication_follows_the_recorded_state),
+ UNIT_CASE(segment_asks_for_a_walk),
+ UNIT_CASE(segment_precedes_its_successor),
{}
};
diff --git a/util/bpm_tree.c b/util/bpm_tree.c
index 50a19ace..cf20ad02 100644
--- a/util/bpm_tree.c
+++ b/util/bpm_tree.c
@@ -306,6 +306,11 @@ int tree_parse(const char *body, size_t len,
};
struct json_field base[] = {
{ "seq", JSON_T_INT, &out->base_seq, 0, 0, NULL, 0 },
+ { "segment_manifest_digest", JSON_T_STRING,
+ out->segment_manifest_digest,
+ sizeof(out->segment_manifest_digest), 0, NULL, 0 },
+ { "history_root", JSON_T_STRING, out->history_root,
+ sizeof(out->history_root), 0, NULL, 0 },
};
struct json_field head[] = {
{ "seq", JSON_T_INT, &out->head_seq, 0, 0, NULL, 0 },
@@ -595,9 +600,9 @@ static size_t tree_operation_bound(const struct tree_operation *op)
/*
* Serialise a publication in the field order every publisher has used:
- * schema, tree_id, generation, parent_manifest_digest (when any), base,
- * operations, head. The bound covers the published form; the emitter
- * grows past it only for keys extra adds.
+ * schema, tree_id, generation, parent_manifest_digest (when any), base
+ * with its segment keys (when any), operations, head. The bound covers
+ * the published form; the emitter grows past it only for keys extra adds.
*/
int tree_emit_publication(const struct tree_publication *pub,
const struct tree_emit_extra *extra,
@@ -625,6 +630,11 @@ int tree_emit_publication(const struct tree_publication *pub,
pub->parent_manifest_digest);
json_obj_open_key(&e, "base");
json_kv_int(&e, "seq", pub->base_seq);
+ if (pub->segment_manifest_digest[0])
+ json_kv_string(&e, "segment_manifest_digest",
+ pub->segment_manifest_digest);
+ if (pub->history_root[0])
+ json_kv_string(&e, "history_root", pub->history_root);
json_obj_close(&e);
json_arr_open(&e, "operations");
for (int i = 0; !ret && i < pub->n_operations; i++)
@@ -666,6 +676,20 @@ int tree_operation_validate(const struct tree_operation *op)
return strcmp(op->operation_digest, expected) ? -EKEYREJECTED : 0;
}
+static bool tree_base_valid(const struct tree_publication *pub)
+{
+ __u8 digest[32];
+
+ if (pub->base_seq < 0)
+ return false;
+ if (!pub->base_seq)
+ return !pub->segment_manifest_digest[0] &&
+ !pub->history_root[0];
+ return pub->n_operations > 0 &&
+ !sha256_digest_decode(pub->segment_manifest_digest, digest) &&
+ !hex_decode(pub->history_root, digest, sizeof(digest));
+}
+
int tree_publication_check(const struct tree_publication *pub)
{
__u8 digest[32];
@@ -674,8 +698,8 @@ int tree_publication_check(const struct tree_publication *pub)
if (pub->parent_manifest_digest[0] &&
sha256_digest_decode(pub->parent_manifest_digest, digest))
return -EINVAL;
- if (pub->base_seq != 0)
- return -ENOTSUP;
+ if (!tree_base_valid(pub))
+ return -EINVAL;
if (n && (pub->operations[0].seq != pub->base_seq + 1 ||
pub->head_seq != pub->operations[n - 1].seq))
return -EINVAL;
@@ -691,7 +715,7 @@ int tree_publication_follows(const struct tree_publication *pub,
bool same;
if (!state)
- return pub->base_seq ? -ERANGE : 0;
+ return pub->base_seq ? TREE_FOLLOWS_WALK : 0;
if (strcmp(state->tree_id, pub->tree_id))
return -EINVAL;
same = !strcmp(state->manifest_digest, manifest_digest);
@@ -705,7 +729,20 @@ int tree_publication_follows(const struct tree_publication *pub,
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)
+ if (pub->head_seq < state->last_seq)
+ return -ERANGE;
+ return pub->base_seq > state->last_seq ? TREE_FOLLOWS_WALK : 0;
+}
+
+int tree_segment_precedes(const struct tree_publication *pred,
+ const char *pred_manifest_digest,
+ const struct tree_publication *succ)
+{
+ if (succ->base_seq <= 0 ||
+ strcmp(succ->segment_manifest_digest, pred_manifest_digest) ||
+ strcmp(pred->tree_id, succ->tree_id) ||
+ pred->head_seq != succ->base_seq ||
+ pred->generation >= succ->generation)
return -ERANGE;
return 0;
}
diff --git a/util/bpm_tree.h b/util/bpm_tree.h
index c0f1133f..49557491 100644
--- a/util/bpm_tree.h
+++ b/util/bpm_tree.h
@@ -80,6 +80,9 @@ struct tree_publication {
__s64 generation;
char parent_manifest_digest[128];
__s64 base_seq;
+ /* Past the first segment: its sealed predecessor, history at base. */
+ char segment_manifest_digest[128];
+ char history_root[65];
struct tree_operation *operations;
int operations_capacity;
int n_operations;
@@ -133,20 +136,34 @@ int tree_emit_publication(const struct tree_publication *pub,
* 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.
+ * base_seq + 1 to head_seq, or head_seq is base_seq without them. A
+ * segment past the first, base_seq above 0, holds an operation or more
+ * and names a well-formed sealed predecessor digest and history root; the
+ * first names neither: 0 or -EINVAL.
+ *
+ * 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, and no head before the node's
+ * cursor. 0, -EINVAL for another tree, -ERANGE, or TREE_FOLLOWS_WALK when
+ * its base lies past the cursor, 0 without a state, and the node must
+ * walk back through the sealed segments before it.
+ *
+ * tree_segment_precedes() is the check of one step of that walk: pred,
+ * whose manifest hashed to pred_manifest_digest, is the sealed segment
+ * succ names, of the same tree, ending where succ starts and of a lower
+ * generation. 0 or -ERANGE.
*/
+#define TREE_FOLLOWS_WALK 1
+
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);
+int tree_segment_precedes(const struct tree_publication *pred,
+ const char *pred_manifest_digest,
+ const struct tree_publication *succ) __must_check;
/*
* Evaluate a publication selector against the running node. An empty