eBPF-based security platform
bpm: complete a measured tree operation without measuring it again
Daniel Borkmann1ec13786fa3d
diff --git a/Documentation/reference/formats/bpm-formats.rst b/Documentation/reference/formats/bpm-formats.rstindex 5acd3558..c380d7cd 100644--- a/Documentation/reference/formats/bpm-formats.rst+++ b/Documentation/reference/formats/bpm-formats.rst@@ -528,7 +528,10 @@ not published.did not finish: the replay skips it and the next append drops it.* - ``pending.json``- JSON object with ``schema`` (1), ``seq``, ``id`` and- ``operation_digest`` while an operation is in progress.+ ``operation_digest`` while an operation is in progress. The next+ sync completes the operation without applying it again when the+ tree eventlog or this boot's eventlog already records it, and+ applies it again otherwise.* - ``artifacts/<hex>.upatch``- Fetched artifacts, named by their layer digest.* - ``status/<id>``diff --git a/builtin-bpm.c b/builtin-bpm.cindex 71499d4c..8093b02a 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -2053,7 +2053,9 @@ static int tree_measure_operation(const struct tree_operation *op,/** Complete the only non-idempotent window: the process may have- * appended the event before the sealed cursor was committed.+ * measured the operation, or appended its tree event too, before the+ * sealed cursor was committed. Applying it again would extend the+ * register with it a second time this boot.*/static int tree_recover_pending(const struct tree_publication *pub,struct tree_state *state)@@ -2103,9 +2105,15 @@ static int tree_recover_pending(const struct tree_publication *pub,} else if (ret != -ENOENT) {return -EKEYREJECTED;}- /* Still pre-operation; retry normally. */- if (!applied)- return 0;+ if (!applied) {+ ret = eventlog_tree_op_measured(op->seq, op->operation_digest);+ if (ret < 0)+ return -EKEYREJECTED;+ /* Still pre-operation; retry normally. */+ if (!ret)+ return 0;+ applied = true;+ }if (!event_present) {ret = tree_event_append(op->seq, op->operation_digest, next);if (ret)diff --git a/testing/unit/eventlog.c b/testing/unit/eventlog.cindex 2e0beaa8..561fe5a9 100644--- a/testing/unit/eventlog.c+++ b/testing/unit/eventlog.c@@ -122,9 +122,53 @@ static void nul_in_the_log_is_refused(void)drop_log();}+#define OPERATION "sha256:abababababababababababababababababababababababababababababababab"++static void tree_record(char *buf, size_t cap, __s64 seq, const char *op)+{+ struct json_emit e = { .buf = buf, .cap = cap };+ __u8 digest[32];++ memset(digest, 7, sizeof(digest));+ eventlog_record_open(&e, "tree_op", digest);+ json_obj_open_key(&e, "tree");+ json_kv_int(&e, "seq", seq);+ json_kv_string(&e, "operation_digest", op);+ json_obj_close(&e);+ json_obj_close(&e);+ ASSERT_EQ(e.err, 0);+}++/*+ * Tree recovery asks whether an operation was measured this boot before+ * it applies the operation again, which would measure it twice.+ */+static void tree_op_measured_this_boot(void)+{+ char init[1024], tree[1024], text[2048];+ int len;++ use_log();+ record(init, sizeof(init), "init", 1);+ tree_record(tree, sizeof(tree), 3, OPERATION);+ EXPECT_EQ(eventlog_tree_op_measured(3, OPERATION), 0);+ len = strsprintf(text, sizeof(text), "%s\n%s\n", init, tree);+ ASSERT_TRUE(len > 0);+ write_log(text, (size_t)len);+ EXPECT_EQ(eventlog_tree_op_measured(3, OPERATION), 1);+ EXPECT_EQ(eventlog_tree_op_measured(4, OPERATION), 0);+ EXPECT_EQ(eventlog_tree_op_measured(3, OPERATION "0"), 0);+ len = strsprintf(text, sizeof(text), "%s\n", tree);+ ASSERT_TRUE(len > 0);+ write_log(text, (size_t)len);+ EXPECT_EQ(eventlog_tree_op_measured(3, OPERATION), 0);+ drop_log();+}+static const struct unit_case cases[] = {UNIT_CASE(unfinished_append_is_no_record),UNIT_CASE(nul_in_the_log_is_refused),+ UNIT_CASE(tree_op_measured_this_boot),{}};diff --git a/util/eventlog.c b/util/eventlog.cindex 8a964091..b0d72903 100644--- a/util/eventlog.c+++ b/util/eventlog.c@@ -488,6 +488,52 @@ int eventlog_boot_lines(int (*cb)(const char *line, size_t len,return walk_boot(lines_line, &l);}+struct tree_op_ctx {+ const char *operation_digest;+ __s64 seq;+ bool found;+};++static int tree_op_line(const char *line, size_t len, bool is_init,+ void *ctx)+{+ struct tree_op_ctx *t = ctx;+ char type[32], digest[128];+ __s64 seq = 0;+ struct json_field tree[] = {+ { "seq", JSON_T_INT, &seq, 0, 0, NULL, 0 },+ { "operation_digest", JSON_T_STRING, digest, sizeof(digest),+ 0, NULL, 0 },+ };+ struct json_field f[] = {+ { "type", JSON_T_STRING, type, sizeof(type), 0, NULL, 0 },+ { "tree", JSON_T_OBJECT, NULL, 0, 0, tree, ARRAY_SIZE(tree) },+ };++ if (json_parse(line, len, f, ARRAY_SIZE(f)))+ return -EINVAL;+ if (f[0].seen && !strcmp(type, "tree_op") && tree[0].seen &&+ tree[1].seen && seq == t->seq &&+ !strcmp(digest, t->operation_digest))+ t->found = true;+ return 0;+}++int eventlog_tree_op_measured(__s64 seq, const char *operation_digest)+{+ struct tree_op_ctx t = {+ .operation_digest = operation_digest, .seq = seq,+ };+ int ret;++ ret = walk_boot(tree_op_line, &t);+ if (ret == -ENOENT)+ return 0;+ if (ret)+ return ret;+ return t.found;+}+struct upatches_ctx {int (*cb)(enum eventlog_upatch_event event, const char *name,const char *payload_hex, void *ctx);diff --git a/util/eventlog.h b/util/eventlog.hindex 764361f6..416ebf80 100644--- a/util/eventlog.h+++ b/util/eventlog.h@@ -78,6 +78,13 @@ int eventlog_boot_init(struct eventlog_init *out);int eventlog_boot_lines(int (*cb)(const char *line, size_t len,void *ctx), void *ctx);+/*+ * Whether this boot's records hold the tree_op of operation seq with+ * operation_digest: 1, 0, or -errno when the log cannot be read. A boot+ * without an init record holds none.+ */+int eventlog_tree_op_measured(__s64 seq, const char *operation_digest);+enum eventlog_upatch_event {EVENTLOG_UPATCH_INIT,EVENTLOG_UPATCH_LOAD,