eBPF-based security platform
bpm: check a tree publication with one helper on node and operator
John Fastabende045483a58e9
diff --git a/Makefile.shirudo b/Makefile.shirudoindex 411fbe17..dc45f487 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -1144,8 +1144,7 @@ testing/fuzz/fuzz-bpm-index: testing/fuzz/fuzz-bpm-index.c \$(FUZZ_BASE) $(FUZZ_PACKAGE)testing/fuzz/fuzz-tree: testing/fuzz/fuzz-tree.c $(FUZZ_BASE) \util/bpm_tree.c util/chain_history.c \- util/crypto_openssl.c util/io.c util/upatch.c \- $(FUZZ_SEAL)+ util/upatch.c $(FUZZ_OCI)testing/fuzz/fuzz-upatch: testing/fuzz/fuzz-upatch.c $(FUZZ_BASE) \util/upatch.ctesting/fuzz/fuzz-prog-metadata: testing/fuzz/fuzz-prog-metadata.c \@@ -1224,10 +1223,9 @@ testing/fuzz/fuzz-config-update: testing/fuzz/fuzz-config-update.c \util/config_measure.c testing/fuzz/chain.c \util/crypto_openssl.c util/io.c $(FUZZ_SEAL)FUZZ_TREE_STATE := util/bpm_tree_state.c util/bpm_tree.c \- util/chain_history.c util/upatch.c \- util/io.c $(FUZZ_SEAL) \+ util/chain_history.c util/upatch.c $(FUZZ_OCI) \util/kernel_build_id.c util/kernel_release.c \- util/crypto_openssl.c testing/fuzz/chain.c+ testing/fuzz/chain.ctesting/fuzz/fuzz-tree-state: testing/fuzz/fuzz-tree-state.c $(FUZZ_BASE) \$(FUZZ_TREE_STATE)testing/fuzz/fuzz-kconfig: testing/fuzz/fuzz-kconfig.c $(FUZZ_BASE) \diff --git a/builtin-bpm.c b/builtin-bpm.cindex 8a55e95d..6d23cf78 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -2054,32 +2054,18 @@ static int tree_fetch_checked(struct tree_source *src, const char *reference,struct tree_publication *pub,char *manifest_digest, size_t digest_sz){- int ret;+ int bad_op, ret;ret = bpm_tree_fetch_publication(&src->c, src->repository, reference,get_cert_path(), src->trusted, pub,manifest_digest, digest_sz);if (ret)return ret;- /*- * The node records the tree ID in its state, which takes names- * alone: one that is not would be applied, measured and logged- * and then never recorded, failing every later sync.- */- if (!bpm_package_name_ok(pub->tree_id))- return -EINVAL;- ret = tree_publication_check(pub);- if (ret)- return ret;- for (int i = 0; i < pub->n_operations; i++) {- ret = tree_operation_validate(&pub->operations[i]);- if (ret) {- tree_sync_report_error("operation_validate",- &pub->operations[i], ret);- return ret;- }- }- return 0;+ ret = tree_publication_verify(pub, &bad_op);+ if (ret && bad_op >= 0)+ tree_sync_report_error("operation_validate",+ &pub->operations[bad_op], ret);+ return ret;}/*diff --git a/testing/fuzz/fuzz-tree.c b/testing/fuzz/fuzz-tree.cindex bf90698d..b90183ee 100644--- a/testing/fuzz/fuzz-tree.c+++ b/testing/fuzz/fuzz-tree.c@@ -7,7 +7,8 @@* bare and with the publisher's fixes, which must read back with the same* 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.+ * when it starts past 0, and never precedes itself. tree_publication_verify()+ * passes only what the check passes and names the operation that failed.*/#include <stdlib.h>@@ -63,6 +64,18 @@ static void read_back(const struct tree_publication *pub,xfree(out, len);}+static void verify_names_its_failure(const struct tree_publication *pub)+{+ int bad_op, ret;++ ret = tree_publication_verify(pub, &bad_op);+ if (bad_op < -1 || bad_op >= pub->n_operations ||+ (!ret && (bad_op != -1 || tree_publication_check(pub))) ||+ (bad_op >= 0 &&+ tree_operation_validate(&pub->operations[bad_op]) != ret))+ abort();+}+int LLVMFuzzerTestOneInput(const __u8 *data, size_t size){struct tree_publication pub;@@ -76,6 +89,7 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)(pub.base_seq && !pub.n_operations) ||!tree_segment_precedes(&pub, pub.segment_manifest_digest, &pub)))abort();+ verify_names_its_failure(&pub);read_back(&pub, NULL);read_back(&pub, &extra);tree_publication_free(&pub);diff --git a/testing/proofs/cbmc/bpm-tree-signers.c b/testing/proofs/cbmc/bpm-tree-signers.cindex de05bebd..5b8cc9ad 100644--- a/testing/proofs/cbmc/bpm-tree-signers.c+++ b/testing/proofs/cbmc/bpm-tree-signers.c@@ -112,18 +112,9 @@ int bpm_tree_fetch_publication(struct bpm_oci_client *c,}/* The checks of the publication, which other proofs cover. */-bool bpm_package_name_ok(const char *name)-{- return true;-}--int tree_publication_check(const struct tree_publication *pub)-{- return 0;-}--int tree_operation_validate(const struct tree_operation *op)+int tree_publication_verify(const struct tree_publication *pub, int *bad_op){+ *bad_op = -1;return 0;}diff --git a/testing/proofs/cbmc/bpm-tree-sync-model.c b/testing/proofs/cbmc/bpm-tree-sync-model.cindex f95d51af..0601f890 100644--- a/testing/proofs/cbmc/bpm-tree-sync-model.c+++ b/testing/proofs/cbmc/bpm-tree-sync-model.c@@ -37,11 +37,40 @@#include <openssl/x509.h>#include "proof.h"-/* The model's checks run as the C has them; the digest check is a stub. */+/*+ * The model's checks run as the C has them; the digest check is a stub.+ * The copy of tree_publication_verify() in util/bpm_tree.c would call the+ * real one, so it is given here as the C has it, calling the stub.+ */#define tree_operation_validate tree_operation_validate_unused+#define tree_publication_verify tree_publication_verify_unused#include "../../../util/bpm_tree.c"#undef tree_operation_validate+#undef tree_publication_verifyint tree_operation_validate(const struct tree_operation *op);++int tree_publication_verify(const struct tree_publication *pub, int *bad_op)+{+ int ret;++ if (bad_op)+ *bad_op = -1;+ if (!bpm_package_name_ok(pub->tree_id))+ return -EINVAL;+ ret = tree_publication_check(pub);+ if (ret)+ return ret;+ for (int i = 0; i < pub->n_operations; i++) {+ ret = tree_operation_validate(&pub->operations[i]);+ if (ret) {+ if (bad_op)+ *bad_op = i;+ return ret;+ }+ }+ return 0;+}+#include "../../../builtin-bpm.c"enum {diff --git a/testing/unit/bpm_tree.c b/testing/unit/bpm_tree.cindex fa7a88f7..cf8733d8 100644--- a/testing/unit/bpm_tree.c+++ b/testing/unit/bpm_tree.c@@ -301,6 +301,46 @@ static void publication_runs_from_base_to_head(void)EXPECT_EQ(tree_publication_check(&pub), -EINVAL);}+/*+ * What sync checks of a publication: a tree ID its state can record, the+ * numbers and each operation's digests, naming the operation that fails.+ */+static void publication_verifies_as_sync_does(void)+{+ struct tree_publication pub = { .schema = 1, .generation = 1,+ .head_seq = 2 };+ struct tree_operation *op = operation();+ int bad_op = 0;++ set(pub.tree_id, sizeof(pub.tree_id), "t");+ for (int seq = 1; seq <= 2; seq++) {+ op->seq = seq;+ ASSERT_EQ(tree_operation_digest(op, op->operation_digest), 0);+ ASSERT_EQ(tree_publication_append(&pub, op), 0);+ }+ EXPECT_EQ(tree_publication_verify(&pub, &bad_op), 0);+ EXPECT_EQ(bad_op, -1);+ set(pub.tree_id, sizeof(pub.tree_id), "../t");+ bad_op = 0;+ EXPECT_EQ(tree_publication_verify(&pub, &bad_op), -EINVAL);+ EXPECT_EQ(bad_op, -1);+ set(pub.tree_id, sizeof(pub.tree_id), "t");+ pub.head_seq = 3;+ bad_op = 0;+ EXPECT_EQ(tree_publication_verify(&pub, &bad_op), -EINVAL);+ EXPECT_EQ(bad_op, -1);+ pub.head_seq = 2;+ pub.operations[1].operation_digest[7] ^= 1;+ EXPECT_EQ(tree_publication_verify(&pub, &bad_op), -EKEYREJECTED);+ EXPECT_EQ(bad_op, 1);+ pub.operations[0].id[0] = '.';+ EXPECT_EQ(tree_publication_verify(&pub, &bad_op), -EINVAL);+ EXPECT_EQ(bad_op, 0);+ tree_publication_free(&pub);+ tree_selector_free(&op->selector);+ xfree(op, sizeof(*op));+}+/** A segment past the first names its sealed predecessor and the history* root at its base and holds an operation; the first names neither.@@ -549,6 +589,7 @@ static const struct unit_case cases[] = {UNIT_CASE(segment_refuses_malformed_keys),UNIT_CASE(operation_validates_against_its_digest),UNIT_CASE(publication_runs_from_base_to_head),+ UNIT_CASE(publication_verifies_as_sync_does),UNIT_CASE(segment_names_its_base),UNIT_CASE(publication_follows_the_recorded_state),UNIT_CASE(segment_asks_for_a_walk),diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.cindex 70502e30..0f2b06fd 100644--- a/tools/shirudo-build/bpm-publish.c+++ b/tools/shirudo-build/bpm-publish.c@@ -981,10 +981,11 @@ static int tree_refuse_sealed_xdp_class(struct bpm_oci_client *c,struct tree_later later = {};int next = 0, ret;- if (cur->base_seq > TREE_MAX_JOIN_OPS) {+ ret = tree_walk_within(cur->base_seq, 0);+ if (ret) {pr_err("bpm tree append: a joining node walks back %lld operations, past the limit of %d, so the XDP device classes of the sealed segments cannot be checked",(long long)cur->base_seq, TREE_MAX_JOIN_OPS);- return -E2BIG;+ return ret;}seen = xmalloc(sizeof(*seen));if (!seen)@@ -1322,7 +1323,7 @@ static int cmd_bpm_tree_append(int argc, char **argv)ret = -EINVAL;goto out;}- ret = build_tree_publication_verify(&pub);+ ret = tree_publication_verify(&pub, NULL);if (ret) {pr_err("bpm tree append: the publication at %s:%s does not verify",repo_ref, reference);diff --git a/tools/shirudo-build/bpm-show.c b/tools/shirudo-build/bpm-show.cindex cb626c21..5e638ca2 100644--- a/tools/shirudo-build/bpm-show.c+++ b/tools/shirudo-build/bpm-show.c@@ -40,22 +40,6 @@ static void tree_show_usage(void)"reference names.\n");}-int build_tree_publication_verify(const struct tree_publication *pub)-{- int ret;-- /*- * The node records the tree ID in its state, which takes names- * alone, so it refuses one that is not.- */- if (!bpm_package_name_ok(pub->tree_id))- return -EINVAL;- ret = tree_publication_check(pub);- for (int i = 0; !ret && i < pub->n_operations; i++)- ret = tree_operation_validate(&pub->operations[i]);- return ret;-}-int build_tree_fetch_checked(struct bpm_oci_client *c, const char *repository,const char *reference, const char *cert_path,const struct trusted_set *trusted,@@ -72,7 +56,7 @@ int build_tree_fetch_checked(struct bpm_oci_client *c, const char *repository,if (!ret)ret = tree_parse((const char *)bytes.data, bytes.len, pub);if (!ret)- ret = build_tree_publication_verify(pub);+ ret = tree_publication_verify(pub, NULL);if (ret || !document)bpm_buffer_free(&bytes);elsediff --git a/tools/shirudo-build/bpm.h b/tools/shirudo-build/bpm.hindex 352eabb7..d01148e6 100644--- a/tools/shirudo-build/bpm.h+++ b/tools/shirudo-build/bpm.h@@ -122,9 +122,6 @@ struct bpm_oci_client;struct trusted_set;/*- * What tree sync checks of a publication before it compares it with its- * state.- ** build_tree_fetch_checked() fetches the publication at reference, a tag* or a manifest digest, checks it against trusted, or cert_path when* trusted is NULL, as tree sync does and parses it into pub. document,@@ -134,7 +131,6 @@ struct trusted_set;* node walking back does: by digest, checked, and the one that ends where* succ starts.*/-int build_tree_publication_verify(const struct tree_publication *pub);int build_tree_fetch_checked(struct bpm_oci_client *c, const char *repository,const char *reference, const char *cert_path,const struct trusted_set *trusted,diff --git a/util/bpm_tree.c b/util/bpm_tree.cindex c5ffdff1..fe163aed 100644--- a/util/bpm_tree.c+++ b/util/bpm_tree.c@@ -10,6 +10,7 @@#include <shirudo/misc.h>#include <shirudo/string.h>+#include "bpm_oci.h"#include "bpm_tree.h"#include "crypto.h"#include "hex.h"@@ -708,6 +709,33 @@ int tree_publication_check(const struct tree_publication *pub)return 0;}+int tree_publication_verify(const struct tree_publication *pub, int *bad_op)+{+ int ret;++ if (bad_op)+ *bad_op = -1;+ /*+ * The node records the tree ID in its state, which takes names+ * alone: one that is not would be applied, measured and logged+ * and then never recorded, failing every later sync.+ */+ if (!bpm_package_name_ok(pub->tree_id))+ return -EINVAL;+ ret = tree_publication_check(pub);+ if (ret)+ return ret;+ for (int i = 0; i < pub->n_operations; i++) {+ ret = tree_operation_validate(&pub->operations[i]);+ if (ret) {+ if (bad_op)+ *bad_op = i;+ return ret;+ }+ }+ return 0;+}+int tree_publication_follows(const struct tree_publication *pub,const struct tree_state *state,const char *manifest_digest)diff --git a/util/bpm_tree.h b/util/bpm_tree.hindex a97c4e66..fbaac823 100644--- a/util/bpm_tree.h+++ b/util/bpm_tree.h@@ -155,6 +155,10 @@ int tree_emit_publication(const struct tree_publication *pub,* and names a well-formed sealed predecessor digest and history root; the* first names neither: 0 or -EINVAL.*+ * tree_publication_verify() makes those checks of a publication, and that+ * its tree ID is a name the node can record: 0 or the first failure, with+ * *bad_op, unless NULL, the index of the operation that failed or -1.+ ** 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@@ -172,6 +176,8 @@ int tree_emit_publication(const struct tree_publication *pub,int tree_operation_validate(const struct tree_operation *op);int tree_publication_check(const struct tree_publication *pub);+int tree_publication_verify(const struct tree_publication *pub,+ int *bad_op) __must_check;int tree_publication_follows(const struct tree_publication *pub,const struct tree_state *state,const char *manifest_digest);