Skip to content

eBPF-based security platform

bpm: record each tree operation's outcome and the publication's range

John Fastabendbdbd0898cfaa
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index f34e63ef..1a1529dc 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -638,7 +638,7 @@ the functional tests run.
* - ``tree``
- An upatch tree publication
* - ``tree-state``
- - The tree's ``state.json`` and eventlog
+ - The tree's ``state.json``, eventlog and outcome lines
* - ``upatch``
- A ``.upatch`` container and its decoder table
diff --git a/Documentation/reference/formats/bpm-formats.rst b/Documentation/reference/formats/bpm-formats.rst
index 813bf4ee..c20c34a6 100644
--- a/Documentation/reference/formats/bpm-formats.rst
+++ b/Documentation/reference/formats/bpm-formats.rst
@@ -536,7 +536,10 @@ not published.
- JSON object with ``schema`` (1), ``tree_id``, ``generation`` and
``manifest_digest`` of the last applied publication, ``last_seq``,
``projection_root`` and ``projection_root_tpm``. ``last_seq`` also
- moves past operations whose selector does not match.
+ moves past operations whose selector does not match. ``head_seq``
+ and ``segment_base_seq`` are the ``head.seq`` and ``base.seq`` of
+ the publication the node last synced to; a state without either
+ is refused.
* - ``tree-eventlog``
- One JSON object per line and applied operation, with ``schema``
(1), ``seq``, ``digest`` (the ``operation_digest``) and
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 476716c4..fa12b56e 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -2053,6 +2053,8 @@ static int cmd_bpm_tree_sync(int argc, const char **argv)
memset(state.projection_root_tpm, '0', 64);
state.projection_root_tpm[64] = '\0';
}
+ state.head_seq = pub.head_seq;
+ state.segment_base_seq = pub.base_seq;
ret = tree_recover_pending(&pub, &state);
if (ret) {
tree_sync_report_error("pending_recovery", NULL, ret);
diff --git a/testing/fuzz/corpus/tree-state/outcomes b/testing/fuzz/corpus/tree-state/outcomes
new file mode 100644
index 00000000..6ffd004a
--- /dev/null
+++ b/testing/fuzz/corpus/tree-state/outcomes
@@ -0,0 +1,8 @@
+{"schema":1,"seq":1,"outcome":2,"projection_root":"f64551fcd6f07823cb87971cfb91446425da18286b3ab1ef935e0cbd7a69f68a","history_root":"33112ee14ee469c3eb52fe90322ec81dd404a0093d565a6d71ce77cbc8124e3b"}
+{"schema":1,"seq":2,"outcome":3,"projection_root":"3946ca64ff78d93ca61090a437cbb6b3d2ca0d488f5f9ccf3059608368b27693","history_root":"f998fe06afa0cfbe73e0449dc2b1698309e1b5714960f027b2858312b152c275"}
+{"schema":1,"seq":3,"outcome":1,"projection_root":"43bb00d0ce7790a53b91256b370c887b24791a5539a6fbfb70c5870e8c91ae5d","history_root":"97fb5f8538b89f6c1accfd19836b65a73b61fbc2e0cbf84bb858a0fffa3f1592"}
+{"schema":1,"seq":4,"outcome":2,"projection_root":"ab71fc4c8a1c4d62b9202b36ee7c07dd398a0907a37037bd8c3959d6af573608","history_root":"e9590c04cea54beb769a96148583176605389b3a3809162f2fd6392b43fb8382"}
+{"schema":1,"seq":5,"outcome":3,"projection_root":"536c351ae15e5f5e3dc37bcc5dea8ab641e70fc0431a088a1d684aef2ebf3e5b","history_root":"acea77fd89c7fb586c8c7c612b53634c4571a40cf8b1bcecb002311210423f57"}
+{"schema":1,"seq":6,"outcome":1,"projection_root":"7d087a2e212c110e851c7b6fdc2853a41e7db1690beef7a9c11b3a26ee77e853","history_root":"fa37de9da4b13b3a18882ece23d6bee24a329b77d488b984d6f5587ce3f0f6cf"}
+{"schema":1,"seq":7,"outcome":2,"projection_root":"03fbd36c05856bca596b0bcb4466f4f30f0119a41be2b9fcebfa71d68178b116","history_root":"f254a1e7d56840a4f2faa65b43f82d21e65add6e7376169cb5a51da30007822a"}
+{"schema":1,"seq":8,"outcome":3,"projection_root":"9b85c0828dcfbc883726fc200c987baa9826da6ba6a358643a69a52d8698d72e","history_root":"9f095ecf60fdfca89c7c42f4f01bffd630fa8393a8ae2ce5a0d9445464c8d359"}
diff --git a/testing/fuzz/corpus/tree-state/outcomes-torn b/testing/fuzz/corpus/tree-state/outcomes-torn
new file mode 100644
index 00000000..2e805a89
--- /dev/null
+++ b/testing/fuzz/corpus/tree-state/outcomes-torn
@@ -0,0 +1,3 @@
+{"schema":1,"seq":1,"outcome":2,"projection_root":"f64551fcd6f07823cb87971cfb91446425da18286b3ab1ef935e0cbd7a69f68a","history_root":"33112ee14ee469c3eb52fe90322ec81dd404a0093d565a6d71ce77cbc8124e3b"}
+{"schema":1,"seq":2,"outcome":3,"projection_root":"3946ca64ff78d93ca61090a437cbb6b3d2ca0d488f5f9ccf3059608368b27693","history_root":"f998fe06afa0cfbe73e0449dc2b1698309e1b5714960f027b2858312b152c275"}
+{"schema":1,"seq":3,
\ No newline at end of file
diff --git a/testing/fuzz/corpus/tree-state/state.json b/testing/fuzz/corpus/tree-state/state.json
index 140c1fe5..9d253d92 100644
--- a/testing/fuzz/corpus/tree-state/state.json
+++ b/testing/fuzz/corpus/tree-state/state.json
@@ -5,5 +5,7 @@
"last_seq": 5,
"projection_root": "5c7761b120a9cf59e79e5a078227b450b7a39294db74bb3ba28e28c6ae196d27",
"projection_root_tpm": "5c7761b120a9cf59e79e5a078227b450b7a39294db74bb3ba28e28c6ae196d27",
- "manifest_digest": "sha256:05b3abf2579a5eb66403cd78be557fd860633a1fe2103c7642030defe32c657f"
+ "manifest_digest": "sha256:05b3abf2579a5eb66403cd78be557fd860633a1fe2103c7642030defe32c657f",
+ "head_seq": 5,
+ "segment_base_seq": 0
}
diff --git a/testing/fuzz/fuzz-tree-state.c b/testing/fuzz/fuzz-tree-state.c
index 99684144..482b97bc 100644
--- a/testing/fuzz/fuzz-tree-state.c
+++ b/testing/fuzz/fuzz-tree-state.c
@@ -2,10 +2,12 @@
/*
* Copyright (c) 2026 Authors of shirudo
*
- * The upatch tree's state.json and its eventlog, as a target reads them
- * back before a sync.
+ * The upatch tree's state.json, its eventlog and its outcomes record, as
+ * a target reads them back before a sync.
*/
+#include <stdlib.h>
+
#include "fuzz.h"
#include "paths.h"
@@ -18,6 +20,30 @@ static char fuzz_path[64];
#include "bpm_tree_state.c"
+/*
+ * Each line the reader takes lies within the record, follows the line
+ * before it and is written again as a line it takes the same.
+ */
+static void outcomes_read_back_as_written(int fd, size_t size)
+{
+ struct outcomes o = { .fd = fd };
+ struct outcome_line back;
+ char buf[OUTCOME_LINE_MAX];
+ __s64 seq = 0;
+ size_t len;
+
+ while (!outcomes_next(&o)) {
+ if ((size_t)o.end > size || o.line.seq != ++seq ||
+ !outcome_valid(o.line.outcome) ||
+ outcome_emit(&o.line, buf, sizeof(buf), &len) ||
+ outcome_parse(buf, len, &back) ||
+ back.seq != o.line.seq || back.outcome != o.line.outcome ||
+ memcmp(back.projection_root, o.line.projection_root, 32) ||
+ memcmp(back.history_root, o.line.history_root, 32))
+ abort();
+ }
+}
+
int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
{
struct tree_state state;
@@ -29,6 +55,7 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
return 0;
tree_state_read(&state);
tree_event_replay(root);
+ outcomes_read_back_as_written(fd, size);
close(fd);
return 0;
}
diff --git a/testing/unit/bpm_tree_state.c b/testing/unit/bpm_tree_state.c
index 30dd068c..f42b187d 100644
--- a/testing/unit/bpm_tree_state.c
+++ b/testing/unit/bpm_tree_state.c
@@ -1,6 +1,22 @@
// SPDX-License-Identifier: BSD-2-Clause
/* Copyright (c) 2026 Authors of shirudo */
+#include "../../util/paths.h"
+
+static char tree_dir[4200], state_path[4300], outcomes_path[4300];
+static char events_path[4300], pending_path[4300];
+
+#undef BPM_TREE_VAR_DIR
+#define BPM_TREE_VAR_DIR tree_dir
+#undef BPM_TREE_STATE_PATH
+#define BPM_TREE_STATE_PATH state_path
+#undef BPM_TREE_EVENTLOG_PATH
+#define BPM_TREE_EVENTLOG_PATH events_path
+#undef BPM_TREE_PENDING_PATH
+#define BPM_TREE_PENDING_PATH pending_path
+#undef BPM_TREE_OUTCOMES_PATH
+#define BPM_TREE_OUTCOMES_PATH outcomes_path
+
#include "../../util/bpm_tree_state.c"
#include "unit.h"
@@ -164,11 +180,336 @@ static void labels_are_cut_at_the_first_equals(void)
EXPECT_EQ(n, 0);
}
+static char top_dir[4096];
+
+static void use_tree(void)
+{
+ const char *tmp = getenv("TMPDIR");
+
+ ASSERT_TRUE(strsprintf(top_dir, sizeof(top_dir),
+ "%s/shirudo-unit-tree-XXXXXX",
+ tmp && tmp[0] ? tmp : "/tmp") > 0);
+ ASSERT_TRUE(mkdtemp(top_dir));
+ ASSERT_TRUE(strsprintf(tree_dir, sizeof(tree_dir), "%s/upatch-tree",
+ top_dir) > 0);
+ ASSERT_TRUE(strsprintf(state_path, sizeof(state_path),
+ "%s/state.json", tree_dir) > 0);
+ ASSERT_TRUE(strsprintf(outcomes_path, sizeof(outcomes_path),
+ "%s/outcomes", tree_dir) > 0);
+ ASSERT_TRUE(strsprintf(events_path, sizeof(events_path),
+ "%s/tree-eventlog", tree_dir) > 0);
+ ASSERT_TRUE(strsprintf(pending_path, sizeof(pending_path),
+ "%s/pending.json", tree_dir) > 0);
+}
+
+static void drop_tree(void)
+{
+ unlink(state_path);
+ unlink(outcomes_path);
+ rmdir(tree_dir);
+ rmdir(top_dir);
+}
+
+static void write_text(const char *file, const char *text, int flags)
+{
+ int fd;
+
+ ASSERT_TRUE(mkdir(tree_dir, 0700) == 0 || errno == EEXIST);
+ fd = open(file, O_WRONLY | O_CREAT | O_CLOEXEC | flags, 0600);
+ ASSERT_TRUE(fd >= 0);
+ ASSERT_EQ(write_complete(fd, text, strlen(text)), 0);
+ ASSERT_EQ(close(fd), 0);
+}
+
+static off_t outcomes_size(void)
+{
+ struct stat st;
+
+ ASSERT_EQ(stat(outcomes_path, &st), 0);
+ return st.st_size;
+}
+
+/* The roots a test gives the line of seq: every byte seq, or seq + 100. */
+static void append_outcome(__s64 seq, enum tree_outcome outcome)
+{
+ __u8 projection[32], history[32];
+
+ memset(projection, (int)seq, sizeof(projection));
+ memset(history, (int)seq + 100, sizeof(history));
+ ASSERT_EQ(tree_outcome_append(seq, outcome, projection, history), 0);
+}
+
+static void expect_roots(const __u8 projection[32], const __u8 history[32],
+ __s64 seq)
+{
+ __u8 want_projection[32] = {}, want_history[32] = {};
+
+ if (seq) {
+ memset(want_projection, (int)seq, sizeof(want_projection));
+ memset(want_history, (int)seq + 100, sizeof(want_history));
+ }
+ EXPECT_MEMEQ(projection, want_projection, 32);
+ EXPECT_MEMEQ(history, want_history, 32);
+}
+
+#define OUTCOME_LINE(seq, outcome) \
+ "{\"schema\":1,\"seq\":" #seq ",\"outcome\":" #outcome "," \
+ "\"projection_root\":\"" \
+ "0101010101010101010101010101010101010101010101010101010101010101" \
+ "\",\"history_root\":\"" \
+ "6565656565656565656565656565656565656565656565656565656565656565" \
+ "\"}\n"
+
+static void outcomes_append_and_read_back(void)
+{
+ __u8 codes[3] = {}, projection[32], history[32];
+ char *text;
+ size_t len;
+
+ use_tree();
+ append_outcome(1, TREE_OUTCOME_APPLIED);
+ ASSERT_EQ(read_file(outcomes_path, 4096, &text, &len), 0);
+ EXPECT_STREQ(text, OUTCOME_LINE(1, 1));
+ xfree(text, len + 1);
+ append_outcome(2, TREE_OUTCOME_UNMATCHED);
+ append_outcome(3, TREE_OUTCOME_MODULE_ABSENT);
+
+ ASSERT_EQ(tree_outcomes_read(0, 3, codes, projection, history), 0);
+ EXPECT_EQ(codes[0], 1);
+ EXPECT_EQ(codes[1], 2);
+ EXPECT_EQ(codes[2], 3);
+ expect_roots(projection, history, 0);
+
+ memset(codes, 0, sizeof(codes));
+ ASSERT_EQ(tree_outcomes_read(1, 3, codes, projection, history), 0);
+ EXPECT_EQ(codes[0], 2);
+ EXPECT_EQ(codes[1], 3);
+ EXPECT_EQ(codes[2], 0);
+ expect_roots(projection, history, 1);
+
+ ASSERT_EQ(tree_outcomes_read(3, 3, codes, projection, history), 0);
+ expect_roots(projection, history, 3);
+ ASSERT_EQ(tree_outcomes_read(0, 0, codes, projection, history), 0);
+ expect_roots(projection, history, 0);
+
+ EXPECT_EQ(tree_outcomes_read(2, 1, codes, projection, history),
+ -EINVAL);
+ EXPECT_EQ(tree_outcomes_read(-1, 1, codes, projection, history),
+ -EINVAL);
+ EXPECT_EQ(tree_outcome_append(0, TREE_OUTCOME_APPLIED, projection,
+ history), -EINVAL);
+ EXPECT_EQ(tree_outcome_append(4, 0, projection, history), -EINVAL);
+ EXPECT_EQ(tree_outcome_append(4, 4, projection, history), -EINVAL);
+ drop_tree();
+}
+
+static void outcomes_torn_last_line_is_dropped(void)
+{
+ __u8 codes[3], projection[32], history[32];
+ off_t whole;
+
+ use_tree();
+ append_outcome(1, TREE_OUTCOME_APPLIED);
+ append_outcome(2, TREE_OUTCOME_UNMATCHED);
+ whole = outcomes_size();
+ write_text(outcomes_path, "{\"schema\":1,\"seq\":3,\"outc", O_APPEND);
+
+ ASSERT_EQ(tree_outcomes_read(0, 2, codes, projection, history), 0);
+ EXPECT_EQ(tree_outcomes_read(0, 3, codes, projection, history),
+ -EBADMSG);
+ EXPECT_EQ(tree_outcomes_resume(3, history), -EBADMSG);
+ ASSERT_EQ(tree_outcomes_resume(2, history), 0);
+ EXPECT_EQ(outcomes_size(), whole);
+
+ write_text(outcomes_path, "{\"schema\":1,\"seq\":3,\"outc", O_APPEND);
+ append_outcome(3, TREE_OUTCOME_APPLIED);
+ ASSERT_EQ(tree_outcomes_read(0, 3, codes, projection, history), 0);
+ EXPECT_EQ(codes[2], 1);
+ drop_tree();
+}
+
+static void outcomes_resume_cuts_past_last_seq(void)
+{
+ __u8 codes[4], projection[32], history[32], want[32] = {};
+ off_t two;
+
+ use_tree();
+ memset(history, 0xff, sizeof(history));
+ ASSERT_EQ(tree_outcomes_resume(0, history), 0);
+ EXPECT_MEMEQ(history, want, sizeof(history));
+ EXPECT_EQ(access(outcomes_path, F_OK), -1);
+ EXPECT_EQ(tree_outcomes_resume(2, history), -ENOENT);
+ EXPECT_EQ(tree_outcomes_resume(-1, history), -EINVAL);
+
+ append_outcome(1, TREE_OUTCOME_APPLIED);
+ append_outcome(2, TREE_OUTCOME_UNMATCHED);
+ two = outcomes_size();
+ append_outcome(3, TREE_OUTCOME_MODULE_ABSENT);
+ append_outcome(4, TREE_OUTCOME_APPLIED);
+
+ ASSERT_EQ(tree_outcomes_resume(2, history), 0);
+ memset(want, 2 + 100, sizeof(want));
+ EXPECT_MEMEQ(history, want, sizeof(history));
+ EXPECT_EQ(outcomes_size(), two);
+ ASSERT_EQ(tree_outcomes_read(0, 2, codes, projection, history), 0);
+ EXPECT_EQ(codes[0], 1);
+ EXPECT_EQ(codes[1], 2);
+ EXPECT_EQ(tree_outcomes_read(0, 3, codes, projection, history),
+ -EBADMSG);
+
+ EXPECT_EQ(tree_outcomes_resume(5, history), -EBADMSG);
+ EXPECT_EQ(outcomes_size(), two);
+
+ /* What follows last_seq goes unread: it need not be a line. */
+ write_text(outcomes_path, "not a line\n" OUTCOME_LINE(9, 1),
+ O_APPEND);
+ ASSERT_EQ(tree_outcomes_resume(2, history), 0);
+ EXPECT_EQ(outcomes_size(), two);
+
+ ASSERT_EQ(tree_outcomes_resume(0, history), 0);
+ memset(want, 0, sizeof(want));
+ EXPECT_MEMEQ(history, want, sizeof(history));
+ EXPECT_EQ(outcomes_size(), 0);
+ drop_tree();
+}
+
+static int read_outcomes(const char *text, __s64 to)
+{
+ __u8 codes[8], projection[32], history[32];
+
+ write_text(outcomes_path, text, O_TRUNC);
+ return tree_outcomes_read(0, to, codes, projection, history);
+}
+
+static void outcomes_gap_and_missing_line(void)
+{
+ __u8 codes[2], projection[32], history[32];
+
+ use_tree();
+ EXPECT_EQ(tree_outcomes_read(0, 1, codes, projection, history),
+ -ENOENT);
+ EXPECT_EQ(read_outcomes(OUTCOME_LINE(1, 1), 1), 0);
+ EXPECT_EQ(read_outcomes(OUTCOME_LINE(1, 1), 2), -EBADMSG);
+ EXPECT_EQ(read_outcomes("", 1), -EBADMSG);
+
+ EXPECT_EQ(read_outcomes(OUTCOME_LINE(1, 1) OUTCOME_LINE(3, 1), 3),
+ -EBADMSG);
+ EXPECT_EQ(read_outcomes(OUTCOME_LINE(1, 1) OUTCOME_LINE(3, 1), 1), 0);
+ EXPECT_EQ(read_outcomes(OUTCOME_LINE(2, 1), 2), -EBADMSG);
+ EXPECT_EQ(read_outcomes(OUTCOME_LINE(1, 1) OUTCOME_LINE(1, 1), 2),
+ -EBADMSG);
+ EXPECT_EQ(tree_outcomes_resume(2, history), -EBADMSG);
+
+ EXPECT_EQ(read_outcomes(OUTCOME_LINE(1, 0), 1), -EBADMSG);
+ EXPECT_EQ(read_outcomes(OUTCOME_LINE(1, 4), 1), -EBADMSG);
+ EXPECT_EQ(read_outcomes(OUTCOME_LINE(0, 1), 1), -EBADMSG);
+ EXPECT_EQ(read_outcomes("\n" OUTCOME_LINE(1, 1), 1), -EBADMSG);
+ EXPECT_EQ(read_outcomes("not a line\n", 1), -EBADMSG);
+ EXPECT_EQ(read_outcomes("{\"schema\":2,\"seq\":1,\"outcome\":1,"
+ "\"projection_root\":\"\","
+ "\"history_root\":\"\"}\n", 1), -EBADMSG);
+ EXPECT_EQ(read_outcomes("{\"schema\":1,\"seq\":1,\"outcome\":1,"
+ "\"projection_root\":\"01\","
+ "\"history_root\":\"65\"}\n", 1), -EBADMSG);
+ EXPECT_EQ(read_outcomes("{\"schema\":1,\"seq\":1,\"outcome\":1}\n", 1),
+ -EBADMSG);
+ drop_tree();
+}
+
+static void outcomes_longer_line_is_malformed(void)
+{
+ char text[2 * OUTCOME_LINE_MAX];
+
+ use_tree();
+ memset(text, ' ', sizeof(text));
+ text[sizeof(text) - 2] = '\n';
+ text[sizeof(text) - 1] = '\0';
+ EXPECT_EQ(read_outcomes(text, 1), -EBADMSG);
+ text[sizeof(text) - 2] = ' ';
+ EXPECT_EQ(read_outcomes(text, 1), -EBADMSG);
+ drop_tree();
+}
+
+static void outcomes_cover_the_state(void)
+{
+ __u8 history[32], zero[32] = {};
+
+ use_tree();
+ EXPECT_EQ(tree_outcomes_resume(1, history), -ENOENT);
+
+ write_text(outcomes_path, OUTCOME_LINE(1, 1) OUTCOME_LINE(2, 2),
+ O_TRUNC);
+ EXPECT_EQ(tree_outcomes_resume(3, history), -EBADMSG);
+ ASSERT_EQ(tree_outcomes_resume(2, history), 0);
+ EXPECT_TRUE(memcmp(history, zero, sizeof(history)) != 0);
+
+ write_text(outcomes_path, OUTCOME_LINE(1, 1) OUTCOME_LINE(3, 2),
+ O_TRUNC);
+ EXPECT_EQ(tree_outcomes_resume(3, history), -EBADMSG);
+ ASSERT_EQ(tree_outcomes_resume(1, history), 0);
+
+ write_text(outcomes_path, "", O_TRUNC);
+ EXPECT_EQ(tree_outcomes_resume(1, history), -EBADMSG);
+ drop_tree();
+}
+
+#define STATE_ROOT \
+ "5c7761b120a9cf59e79e5a078227b450b7a39294db74bb3ba28e28c6ae196d27"
+#define STATE_HEAD \
+ "{\"schema\":1,\"tree_id\":\"prod\",\"generation\":3,\"last_seq\":5," \
+ "\"projection_root\":\"" STATE_ROOT "\"," \
+ "\"projection_root_tpm\":\"" STATE_ROOT "\"," \
+ "\"manifest_digest\":\"" PENDING_DIGEST "\""
+
+static int read_state(const char *text, struct tree_state *out)
+{
+ write_text(state_path, text, O_TRUNC);
+ return tree_state_read(out);
+}
+
+static void state_names_its_publication_head_and_base(void)
+{
+ struct tree_state s, back;
+
+ use_tree();
+ EXPECT_EQ(tree_state_read(&s), -ENOENT);
+ EXPECT_EQ(read_state(STATE_HEAD "}", &s), -EINVAL);
+ EXPECT_EQ(read_state(STATE_HEAD ",\"head_seq\":7}", &s), -EINVAL);
+ EXPECT_EQ(read_state(STATE_HEAD ",\"segment_base_seq\":0}", &s),
+ -EINVAL);
+ ASSERT_EQ(read_state(STATE_HEAD ",\"head_seq\":7,"
+ "\"segment_base_seq\":2}", &s), 0);
+ EXPECT_EQ(s.last_seq, 5);
+ EXPECT_EQ(s.head_seq, 7);
+ EXPECT_EQ(s.segment_base_seq, 2);
+
+ s.head_seq = 9;
+ s.segment_base_seq = 4;
+ ASSERT_EQ(tree_state_write(&s), 0);
+ ASSERT_EQ(tree_state_read(&back), 0);
+ EXPECT_STREQ(back.tree_id, "prod");
+ EXPECT_EQ(back.generation, 3);
+ EXPECT_EQ(back.last_seq, 5);
+ EXPECT_STREQ(back.projection_root, STATE_ROOT);
+ EXPECT_STREQ(back.projection_root_tpm, STATE_ROOT);
+ EXPECT_STREQ(back.manifest_digest, PENDING_DIGEST);
+ EXPECT_EQ(back.head_seq, 9);
+ EXPECT_EQ(back.segment_base_seq, 4);
+ drop_tree();
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(long_line_is_one_line),
UNIT_CASE(gzip_config_is_one_whole_member),
UNIT_CASE(pending_is_an_operation_in_flight),
UNIT_CASE(labels_are_cut_at_the_first_equals),
+ UNIT_CASE(outcomes_append_and_read_back),
+ UNIT_CASE(outcomes_torn_last_line_is_dropped),
+ UNIT_CASE(outcomes_resume_cuts_past_last_seq),
+ UNIT_CASE(outcomes_gap_and_missing_line),
+ UNIT_CASE(outcomes_longer_line_is_malformed),
+ UNIT_CASE(outcomes_cover_the_state),
+ UNIT_CASE(state_names_its_publication_head_and_base),
{}
};
diff --git a/util/bpm_tree.h b/util/bpm_tree.h
index c20cc8fc..a9e00691 100644
--- a/util/bpm_tree.h
+++ b/util/bpm_tree.h
@@ -95,6 +95,8 @@ struct tree_state {
char projection_root[65];
char projection_root_tpm[65];
char manifest_digest[128];
+ __s64 head_seq;
+ __s64 segment_base_seq;
};
int tree_parse(const char *body, size_t len,
@@ -167,6 +169,29 @@ int tree_event_append(__s64 seq, const char *operation_digest,
const __u8 projection_root[32]);
int tree_event_replay(__u8 out[32]);
+enum tree_outcome {
+ TREE_OUTCOME_APPLIED = 1,
+ TREE_OUTCOME_UNMATCHED = 2,
+ TREE_OUTCOME_MODULE_ABSENT = 3,
+};
+
+/*
+ * A writer holds the bpm lock. tree_outcomes_read() gives the outcomes
+ * of seq from + 1 through seq to and the roots at from;
+ * tree_outcomes_resume() gives the history root at last_seq and drops
+ * the lines past it, which a sync that stopped left. Seq 0 has zero
+ * roots. Both are -ENOENT without a record unless they ask for no line,
+ * and -EBADMSG for one that ends early or is out of order.
+ */
+int tree_outcome_append(__s64 seq, enum tree_outcome outcome,
+ const __u8 projection_root[32],
+ const __u8 history_root[32]) __must_check;
+int tree_outcomes_read(__s64 from, __s64 to, __u8 *codes,
+ __u8 projection_root[32],
+ __u8 history_root[32]) __must_check;
+int tree_outcomes_resume(__s64 last_seq,
+ __u8 history_root[32]) __must_check;
+
void tree_emit_status(struct json_emit *e);
/* Whether a tree is configured, in the config image when one is active. */
diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.c
index b490de11..6baae5f7 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -594,6 +594,9 @@ int tree_state_read(struct tree_state *out)
out->projection_root_tpm, sizeof(out->projection_root_tpm), 0, NULL, 0 },
{ "manifest_digest", JSON_T_STRING, out->manifest_digest,
sizeof(out->manifest_digest), 0, NULL, 0 },
+ { "head_seq", JSON_T_INT, &out->head_seq, 0, 0, NULL, 0 },
+ { "segment_base_seq", JSON_T_INT, &out->segment_base_seq, 0, 0,
+ NULL, 0 },
};
int ret;
@@ -602,7 +605,8 @@ int tree_state_read(struct tree_state *out)
if (ret)
return ret;
ret = json_parse((const char *)body, len, f, ARRAY_SIZE(f));
- if (!ret && (schema != 1 || !f[1].seen || !f[3].seen || !f[4].seen))
+ if (!ret && (schema != 1 || !f[1].seen || !f[3].seen || !f[4].seen ||
+ !f[7].seen || !f[8].seen))
ret = -EINVAL;
if (!ret && !f[5].seen) {
/* State written before the software projection field was added. */
@@ -654,6 +658,8 @@ int tree_state_write(const struct tree_state *s)
json_kv_string(&e, "projection_root", s->projection_root);
json_kv_string(&e, "projection_root_tpm", s->projection_root_tpm);
json_kv_string(&e, "manifest_digest", s->manifest_digest);
+ json_kv_int(&e, "head_seq", s->head_seq);
+ json_kv_int(&e, "segment_base_seq", s->segment_base_seq);
json_obj_close(&e);
if (e.err)
return e.err;
@@ -744,6 +750,206 @@ int tree_event_replay(__u8 out[32])
return ret;
}
+/* A line with the longest seq is 218 bytes, its newline included. */
+#define OUTCOME_LINE_MAX 256
+
+struct outcome_line {
+ __s64 seq;
+ __s64 outcome;
+ __u8 projection_root[32];
+ __u8 history_root[32];
+};
+
+static bool outcome_valid(__s64 outcome)
+{
+ return outcome == TREE_OUTCOME_APPLIED ||
+ outcome == TREE_OUTCOME_UNMATCHED ||
+ outcome == TREE_OUTCOME_MODULE_ABSENT;
+}
+
+static int outcome_emit(const struct outcome_line *line, char *buf,
+ size_t cap, size_t *len)
+{
+ struct json_emit e = { .buf = buf, .cap = cap };
+ char root[65];
+
+ json_obj_open(&e);
+ json_kv_int(&e, "schema", 1);
+ json_kv_int(&e, "seq", line->seq);
+ json_kv_int(&e, "outcome", line->outcome);
+ hex_encode(line->projection_root, 32, root);
+ json_kv_string(&e, "projection_root", root);
+ hex_encode(line->history_root, 32, root);
+ json_kv_string(&e, "history_root", root);
+ json_obj_close(&e);
+ *len = e.len;
+ return e.err;
+}
+
+static int outcome_parse(const char *text, size_t len,
+ struct outcome_line *out)
+{
+ char projection[65] = {}, history[65] = {};
+ __s64 schema = 0;
+ struct json_field f[] = {
+ { "schema", JSON_T_INT, &schema, 0, 0, NULL, 0 },
+ { "seq", JSON_T_INT, &out->seq, 0, 0, NULL, 0 },
+ { "outcome", JSON_T_INT, &out->outcome, 0, 0, NULL, 0 },
+ { "projection_root", JSON_T_STRING, projection,
+ sizeof(projection), 0, NULL, 0 },
+ { "history_root", JSON_T_STRING, history, sizeof(history),
+ 0, NULL, 0 },
+ };
+
+ *out = (struct outcome_line){};
+ if (json_parse(text, len, f, ARRAY_SIZE(f)) || schema != 1 ||
+ out->seq <= 0 || !outcome_valid(out->outcome) ||
+ hex_decode(projection, out->projection_root, 32) ||
+ hex_decode(history, out->history_root, 32))
+ return -EBADMSG;
+ return 0;
+}
+
+/* line starts as seq 0 with zero roots, where every tree starts. */
+struct outcomes {
+ int fd;
+ off_t end;
+ struct outcome_line line;
+};
+
+/*
+ * Every seq has its line, so the next one is a whole line of seq + 1 or
+ * the record is not one. A last line without its newline is an append
+ * that did not finish, and counts as no line.
+ */
+static int outcomes_next(struct outcomes *o)
+{
+ char buf[OUTCOME_LINE_MAX];
+ struct outcome_line next;
+ const char *newline;
+ size_t have = 0;
+ ssize_t got;
+
+ while (have < sizeof(buf)) {
+ got = pread(o->fd, buf + have, sizeof(buf) - have,
+ o->end + (off_t)have);
+ if (got < 0 && errno == EINTR)
+ continue;
+ if (got < 0)
+ return -errno;
+ if (!got)
+ break;
+ have += (size_t)got;
+ }
+ newline = memchr(buf, '\n', have);
+ if (!newline || outcome_parse(buf, (size_t)(newline - buf), &next) ||
+ next.seq != o->line.seq + 1)
+ return -EBADMSG;
+ o->line = next;
+ o->end += newline - buf + 1;
+ return 0;
+}
+
+static int outcomes_seek(struct outcomes *o, __s64 seq)
+{
+ int ret = 0;
+
+ while (!ret && o->line.seq < seq)
+ ret = outcomes_next(o);
+ return ret;
+}
+
+int tree_outcome_append(__s64 seq, enum tree_outcome outcome,
+ const __u8 projection_root[32],
+ const __u8 history_root[32])
+{
+ struct outcome_line line = { .seq = seq, .outcome = outcome };
+ char buf[OUTCOME_LINE_MAX];
+ size_t len;
+ int ret;
+
+ if (seq <= 0 || !outcome_valid(outcome))
+ return -EINVAL;
+ memcpy(line.projection_root, projection_root, 32);
+ memcpy(line.history_root, history_root, 32);
+ ret = outcome_emit(&line, buf, sizeof(buf), &len);
+ if (ret)
+ return ret;
+ ret = seal_mkdir_p(BPM_TREE_VAR_DIR, 0700);
+ if (ret)
+ return ret;
+ return seal_append_line(BPM_TREE_OUTCOMES_PATH, buf, len);
+}
+
+int tree_outcomes_read(__s64 from, __s64 to, __u8 *codes,
+ __u8 projection_root[32], __u8 history_root[32])
+{
+ struct outcomes o = {};
+ int ret;
+
+ if (from < 0 || to < from)
+ return -EINVAL;
+ o.fd = seal_open_read(BPM_TREE_OUTCOMES_PATH);
+ if (o.fd == -ENOENT && !to) {
+ memset(projection_root, 0, 32);
+ memset(history_root, 0, 32);
+ return 0;
+ }
+ if (o.fd < 0)
+ return o.fd;
+ ret = outcomes_seek(&o, from);
+ if (!ret) {
+ memcpy(projection_root, o.line.projection_root, 32);
+ memcpy(history_root, o.line.history_root, 32);
+ }
+ while (!ret && o.line.seq < to) {
+ ret = outcomes_next(&o);
+ if (!ret)
+ *codes++ = (__u8)o.line.outcome;
+ }
+ close(o.fd);
+ return ret;
+}
+
+static int outcomes_cut(struct outcomes *o)
+{
+ struct stat st;
+
+ if (fstat(o->fd, &st))
+ return -errno;
+ if (st.st_size == o->end)
+ return 0;
+ if (ftruncate(o->fd, o->end) || fsync(o->fd))
+ return -errno;
+ return 0;
+}
+
+int tree_outcomes_resume(__s64 last_seq, __u8 history_root[32])
+{
+ struct outcomes o = {};
+ int ret;
+
+ if (last_seq < 0)
+ return -EINVAL;
+ o.fd = seal_open(BPM_TREE_OUTCOMES_PATH, O_RDWR, 0);
+ if (o.fd == -ENOENT && !last_seq) {
+ memset(history_root, 0, 32);
+ return 0;
+ }
+ if (o.fd < 0)
+ return o.fd;
+ ret = lock_exclusive(o.fd);
+ if (!ret)
+ ret = outcomes_seek(&o, last_seq);
+ if (!ret)
+ ret = outcomes_cut(&o);
+ if (!ret)
+ memcpy(history_root, o.line.history_root, 32);
+ if (close(o.fd) && !ret)
+ ret = -errno;
+ return ret;
+}
+
void tree_emit_status(struct json_emit *e)
{
struct tree_state s = {};
diff --git a/util/paths.h b/util/paths.h
index 660cced5..0bc93526 100644
--- a/util/paths.h
+++ b/util/paths.h
@@ -57,6 +57,7 @@
#define BPM_TREE_VAR_DIR BPM_VAR_DIR "/upatch-tree"
#define BPM_TREE_STATE_PATH BPM_TREE_VAR_DIR "/state.json"
#define BPM_TREE_EVENTLOG_PATH BPM_TREE_VAR_DIR "/tree-eventlog"
+#define BPM_TREE_OUTCOMES_PATH BPM_TREE_VAR_DIR "/outcomes"
#define BPM_TREE_PENDING_PATH BPM_TREE_VAR_DIR "/pending.json"
#define BPM_TREE_ARTIFACTS_DIR BPM_TREE_VAR_DIR "/artifacts"
#define BPM_TREE_STATUS_DIR BPM_TREE_VAR_DIR "/status"