eBPF-based security platform
bpm: keep tree sync's pending record in util/bpm_tree_state.c
Daniel Borkmannc53e5732e248
diff --git a/builtin-bpm.c b/builtin-bpm.cindex 776fc5be..0690a581 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -1768,27 +1768,6 @@ static int tree_write_report(const char *path, const struct tree_state *s,return 0;}-static int tree_write_pending(const struct tree_operation *op)-{- char buf[1024];- struct json_emit e = { .buf = buf, .cap = sizeof(buf) };- int ret;-- json_obj_open(&e);- json_kv_int(&e, "schema", 1);- json_kv_int(&e, "seq", op->seq);- json_kv_string(&e, "id", op->id);- json_kv_string(&e, "operation_digest", op->operation_digest);- json_obj_close(&e);- if (e.err)- return e.err;- ret = seal_mkdir_p(BPM_TREE_VAR_DIR, 0700);- if (ret)- return ret;- return seal_write_file(BPM_TREE_PENDING_PATH, buf,- e.len, 0600);-}-static int tree_status_write(const char *id, const char *status){char path[PATH_MAX];@@ -1816,39 +1795,6 @@ static void tree_status_clear(const char *id)unlink(path);}-struct bpm_tree_pending {- __s64 seq;- char id[TREE_STRING_MAX];- char operation_digest[128];-};--static int tree_read_pending(struct bpm_tree_pending *pending)-{- __u8 *body;- size_t len;- __s64 schema = 0;- struct json_field fields[] = {- { "schema", JSON_T_INT, &schema, 0, 0, NULL, 0 },- { "seq", JSON_T_INT, &pending->seq, 0, 0, NULL, 0 },- { "id", JSON_T_STRING, pending->id,- sizeof(pending->id), 0, NULL, 0 },- { "operation_digest", JSON_T_STRING,- pending->operation_digest, sizeof(pending->operation_digest),- 0, NULL, 0 },- };- int ret = read_file(BPM_TREE_PENDING_PATH, 64u * 1024u,- &body, &len);- if (ret)- return ret;- ret = json_parse((const char *)body, len, fields, ARRAY_SIZE(fields));- if (!ret && (schema != 1 || pending->seq <= 0 ||- !upatch_name_valid(pending->id) ||- !tree_digest_valid(pending->operation_digest)))- ret = -EINVAL;- xfree(body, len + 1);- return ret;-}-/** event = SHA256("shirudo-tree\0" || seq || operation_digest). The* tree's own projection stays a file chain; the register is what an@@ -1906,13 +1852,13 @@ static int tree_measure_operation(const struct tree_operation *op,static int tree_recover_pending(const struct tree_publication *pub,struct tree_state *state){- struct bpm_tree_pending pending = {};+ struct tree_pending pending;const struct tree_operation *op = NULL;__u8 digest[32], previous[32], next[32], replay[32];bool applied = false, event_present = false;int ret;- ret = tree_read_pending(&pending);+ ret = tree_pending_read(&pending);if (ret == -ENOENT)return 0;if (ret)@@ -2120,7 +2066,7 @@ static int cmd_bpm_tree_sync(int argc, const char **argv)ret = -EINVAL;goto out;}- ret = tree_write_pending(op);+ ret = tree_pending_write(op);if (ret) {tree_sync_report_error("pending_write", op, ret);goto out;diff --git a/testing/unit/bpm_tree_state.c b/testing/unit/bpm_tree_state.cindex 4335b26e..e219ff47 100644--- a/testing/unit/bpm_tree_state.c+++ b/testing/unit/bpm_tree_state.c@@ -113,9 +113,39 @@ static void gzip_config_is_one_whole_member(void)EXPECT_EQ(read_gz(false, 0, NULL), -EINVAL);}+#define PENDING_DIGEST \+ "sha256:00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"++static int pending(const char *doc, struct tree_pending *out)+{+ return tree_pending_parse(doc, strlen(doc), out);+}++static void pending_is_an_operation_in_flight(void)+{+ struct tree_pending p;++ ASSERT_EQ(pending("{\"schema\":1,\"seq\":3,\"id\":\"fix\","+ "\"operation_digest\":\"" PENDING_DIGEST "\"}", &p), 0);+ EXPECT_EQ(p.seq, 3);+ EXPECT_STREQ(p.id, "fix");+ EXPECT_EQ(pending("{\"schema\":2,\"seq\":3,\"id\":\"fix\","+ "\"operation_digest\":\"" PENDING_DIGEST "\"}", &p),+ -EINVAL);+ EXPECT_EQ(pending("{\"schema\":1,\"seq\":0,\"id\":\"fix\","+ "\"operation_digest\":\"" PENDING_DIGEST "\"}", &p),+ -EINVAL);+ EXPECT_EQ(pending("{\"schema\":1,\"seq\":3,\"id\":\"../fix\","+ "\"operation_digest\":\"" PENDING_DIGEST "\"}", &p),+ -EINVAL);+ EXPECT_EQ(pending("{\"schema\":1,\"seq\":3,\"id\":\"fix\","+ "\"operation_digest\":\"sha256:00\"}", &p), -EINVAL);+}+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),{}};diff --git a/util/bpm_tree.h b/util/bpm_tree.hindex 37245ae9..c20cc8fc 100644--- a/util/bpm_tree.h+++ b/util/bpm_tree.h@@ -144,6 +144,23 @@ int tree_publication_follows(const struct tree_publication *pub,*/int tree_selector_matches(const struct tree_selector *selector);+/*+ * The operation tree sync is applying, recorded before it starts so that+ * a sync after a crash reconciles it with the tree eventlog: its+ * sequence number, upatch name and operation digest. Parsing refuses a+ * record that does not have those; reading is -ENOENT for none.+ */+struct tree_pending {+ __s64 seq;+ char id[TREE_STRING_MAX];+ char operation_digest[128];+};++int tree_pending_parse(const char *body, size_t len,+ struct tree_pending *out);+int tree_pending_read(struct tree_pending *out);+int tree_pending_write(const struct tree_operation *op);+int tree_state_read(struct tree_state *out);int tree_state_write(const struct tree_state *state);int tree_event_append(__s64 seq, const char *operation_digest,diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.cindex 5a0236df..aec7dde9 100644--- a/util/bpm_tree_state.c+++ b/util/bpm_tree_state.c@@ -569,6 +569,63 @@ static bool hex32_valid(const char *hex)return !hex_decode(hex, raw, sizeof(raw));}+int tree_pending_parse(const char *body, size_t len,+ struct tree_pending *out)+{+ __s64 schema = 0;+ struct json_field fields[] = {+ { "schema", JSON_T_INT, &schema, 0, 0, NULL, 0 },+ { "seq", JSON_T_INT, &out->seq, 0, 0, NULL, 0 },+ { "id", JSON_T_STRING, out->id, sizeof(out->id), 0, NULL, 0 },+ { "operation_digest", JSON_T_STRING, out->operation_digest,+ sizeof(out->operation_digest), 0, NULL, 0 },+ };+ __u8 digest[32];+ int ret;++ *out = (struct tree_pending){};+ ret = json_parse(body, len, fields, ARRAY_SIZE(fields));+ if (!ret && (schema != 1 || out->seq <= 0 ||+ !upatch_name_valid(out->id) ||+ sha256_digest_decode(out->operation_digest, digest)))+ ret = -EINVAL;+ return ret;+}++int tree_pending_read(struct tree_pending *out)+{+ size_t len;+ char *body;+ int ret;++ ret = read_file(BPM_TREE_PENDING_PATH, 64u * 1024u, &body, &len);+ if (ret)+ return ret;+ ret = tree_pending_parse(body, len, out);+ xfree(body, len + 1);+ return ret;+}++int tree_pending_write(const struct tree_operation *op)+{+ char buf[1024];+ struct json_emit e = { .buf = buf, .cap = sizeof(buf) };+ int ret;++ json_obj_open(&e);+ json_kv_int(&e, "schema", 1);+ json_kv_int(&e, "seq", op->seq);+ json_kv_string(&e, "id", op->id);+ json_kv_string(&e, "operation_digest", op->operation_digest);+ json_obj_close(&e);+ if (e.err)+ return e.err;+ ret = seal_mkdir_p(BPM_TREE_VAR_DIR, 0700);+ if (ret)+ return ret;+ return seal_write_file(BPM_TREE_PENDING_PATH, buf, e.len, 0600);+}+int tree_state_read(struct tree_state *out){__u8 *body, digest[32];