eBPF-based security platform
bpm: compare the tree eventlog with the state in one place
John Fastabenda78281962b0d
diff --git a/Documentation/reference/formats/status-json.rst b/Documentation/reference/formats/status-json.rstindex 348a603a..da7d1685 100644--- a/Documentation/reference/formats/status-json.rst+++ b/Documentation/reference/formats/status-json.rst@@ -664,7 +664,9 @@ upatch_treereplayed.* - ``eventlog_matches_state``- boolean or ``null``- - The replay equals ``projection_root``.+ - The replay equals ``projection_root``; ``false`` when the tree+ state cannot be read, as the keepalive's ``tree_log_mismatch``+ has it, and ``null`` when the log cannot be replayed.:doc:`bpm-formats` defines the tree state.diff --git a/builtin-status.c b/builtin-status.cindex 048e82fa..786d3cc4 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -540,12 +540,12 @@ static void keepalive_window(struct keepalive_view *v,/* eventlog_matches_state of the status document, false only. */static bool keepalive_log_mismatch(const struct tree_state *s, int state_rc){- __u8 live[CHAIN_DIGEST_LEN], replay[CHAIN_DIGEST_LEN];+ __u8 replay[CHAIN_DIGEST_LEN];+ bool matches;- if (tree_event_replay(replay))+ if (tree_eventlog_matches(s, state_rc, replay, &matches))return false;- return state_rc || hex_decode(s->projection_root, live, sizeof(live)) ||- memneq_explicit(live, replay, sizeof(live));+ return !matches;}/* A tree status file says why the tree left the upatch not loaded. */diff --git a/testing/fuzz/fuzz-tree-state.c b/testing/fuzz/fuzz-tree-state.cindex 482b97bc..dc3e60da 100644--- a/testing/fuzz/fuzz-tree-state.c+++ b/testing/fuzz/fuzz-tree-state.c@@ -3,7 +3,8 @@* Copyright (c) 2026 Authors of shirudo** The upatch tree's state.json, its eventlog and its outcomes record, as- * a target reads them back before a sync.+ * a target reads them back before a sync. The eventlog matches a state+ * only when the state was read, and replays the same either way.*/#include <stdlib.h>@@ -44,17 +45,30 @@ static void outcomes_read_back_as_written(int fd, size_t size)}}+static void eventlog_matches_a_state_read(const struct tree_state *state,+ int state_rc)+{+ __u8 root[32], again[32];+ bool matches = false;+ int ret;++ ret = tree_event_replay(root);+ if (tree_eventlog_matches(state, state_rc, again, &matches) != ret ||+ (!ret && memcmp(root, again, sizeof(root))) ||+ (state_rc && matches))+ abort();+}+int LLVMFuzzerTestOneInput(const __u8 *data, size_t size){struct tree_state state;- __u8 root[32];- int fd;+ int fd, rc;fd = fuzz_file(data, size, fuzz_path, sizeof(fuzz_path));if (fd < 0)return 0;- tree_state_read(&state);- tree_event_replay(root);+ rc = tree_state_read(&state);+ eventlog_matches_a_state_read(&state, rc);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.cindex fb331abe..165166ab 100644--- a/testing/unit/bpm_tree_state.c+++ b/testing/unit/bpm_tree_state.c@@ -521,6 +521,40 @@ static void state_needs_both_projection_roots(void)drop_tree();}+/*+ * The tree eventlog matches a state that was read and holds its replay,+ * and no state that could not be read; a log that does not replay+ * leaves the answer unset.+ */+static void eventlog_matches_only_a_state_read(void)+{+ struct tree_state s = {};+ __u8 next[64] = {}, root[32], replay[32];+ bool matches = true;++ use_tree();+ EXPECT_EQ(tree_eventlog_matches(&s, 0, replay, &matches), -ENOENT);+ EXPECT_TRUE(matches);+ ASSERT_EQ(sha256_digest_decode(PENDING_DIGEST, next + 32), 0);+ ASSERT_EQ(sha256_calc(next, sizeof(next), root), 0);+ ASSERT_EQ(tree_event_append(1, PENDING_DIGEST, root), 0);+ hex_encode(root, sizeof(root), s.projection_root);+ ASSERT_EQ(tree_eventlog_matches(&s, 0, replay, &matches), 0);+ EXPECT_TRUE(matches);+ EXPECT_MEMEQ(replay, root, sizeof(root));+ ASSERT_EQ(tree_eventlog_matches(&s, -EINVAL, replay, &matches), 0);+ EXPECT_FALSE(matches);+ s.projection_root[0] = s.projection_root[0] == '0' ? '1' : '0';+ ASSERT_EQ(tree_eventlog_matches(&s, 0, replay, &matches), 0);+ EXPECT_FALSE(matches);+ s.projection_root[0] = '\0';+ matches = true;+ ASSERT_EQ(tree_eventlog_matches(&s, 0, replay, &matches), 0);+ EXPECT_FALSE(matches);+ unlink(events_path);+ drop_tree();+}+static const struct unit_case cases[] = {UNIT_CASE(long_line_is_one_line),UNIT_CASE(gzip_config_is_one_whole_member),@@ -534,6 +568,7 @@ static const struct unit_case cases[] = {UNIT_CASE(outcomes_cover_the_state),UNIT_CASE(state_names_its_publication_head_and_base),UNIT_CASE(state_needs_both_projection_roots),+ UNIT_CASE(eventlog_matches_only_a_state_read),{}};diff --git a/util/bpm_tree.h b/util/bpm_tree.hindex fbaac823..ef7064a0 100644--- a/util/bpm_tree.h+++ b/util/bpm_tree.h@@ -230,6 +230,14 @@ int tree_state_write(const struct tree_state *state);int tree_event_append(__s64 seq, const char *operation_digest,const __u8 projection_root[32]);int tree_event_replay(__u8 out[32]);+/*+ * Replays the tree eventlog into replay and sets *matches when it equals+ * the projection root of s, which tree_state_read() returned state_rc+ * for: a state that could not be read matches no replay. 0, or+ * tree_event_replay()'s error, leaving *matches as it was.+ */+int tree_eventlog_matches(const struct tree_state *s, int state_rc,+ __u8 replay[32], bool *matches) __must_check;enum tree_outcome {TREE_OUTCOME_APPLIED = 1,diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.cindex 96c7c5ba..611f84d1 100644--- a/util/bpm_tree_state.c+++ b/util/bpm_tree_state.c@@ -942,18 +942,35 @@ int tree_outcomes_resume(__s64 last_seq, __u8 history_root[32])return ret;}+int tree_eventlog_matches(const struct tree_state *s, int state_rc,+ __u8 replay[32], bool *matches)+{+ __u8 live[32];+ int ret;++ ret = tree_event_replay(replay);+ if (ret)+ return ret;+ *matches = !state_rc && !hex_decode(s->projection_root, live, 32) &&+ !memneq_explicit(live, replay, 32);+ return 0;+}+void tree_emit_status(struct json_emit *e){struct tree_state s = {};- __u8 live[32], replay[32];- char hex[65];bool configured = tree_configured();+ bool matches = false;+ __u8 replay[32];+ char hex[65];+ int state_rc;json_obj_open_key(e, "upatch_tree");json_kv_bool(e, "configured", configured);json_kv_bool(e, "pending", access(BPM_TREE_PENDING_PATH,R_OK) == 0);- if (tree_state_read(&s) == 0) {+ state_rc = tree_state_read(&s);+ if (!state_rc) {json_kv_string(e, "tree_id", s.tree_id);json_kv_int(e, "generation", s.generation);json_kv_int(e, "last_seq", s.last_seq);@@ -962,13 +979,10 @@ void tree_emit_status(struct json_emit *e)} else {json_kv_null(e, "state");}- if (tree_event_replay(replay) == 0) {+ if (!tree_eventlog_matches(&s, state_rc, replay, &matches)) {hex_encode(replay, 32, hex);json_kv_string(e, "eventlog_projection_root", hex);- json_kv_bool(e, "eventlog_matches_state",- hex_decode(s.projection_root, live,- 32) == 0 &&- !memneq_explicit(live, replay, 32));+ json_kv_bool(e, "eventlog_matches_state", matches);} else {json_kv_null(e, "eventlog_projection_root");json_kv_null(e, "eventlog_matches_state");