eBPF-based security platform
bpm: upload nothing of a tree publication before it is checked
Daniel Borkmanna7caa4b53154
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex ef47fd28..f34e63ef 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -482,7 +482,8 @@ signed config image.- ``bpm build`` and its refusal of a target below another memberand of members a target could not unpack together, ``bpm push``with a signed index, ``bpm tree append`` and its refusal of an- XDP device class two upatches of a tree select, A-to-B tree+ XDP device class two upatches of a tree select and of an entry no+ target would take, each before anything is uploaded, A-to-B treesigning-key rotation with explicit predecessor verification* - ``t7006-policy-labels``- Beefile policy declarations are serialized into the signeddiff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rstindex b26f538d..31e08833 100644--- a/Documentation/reference/cli/bpm.rst+++ b/Documentation/reference/cli/bpm.rst@@ -226,6 +226,13 @@ package as the next operations of the signed tree at- ``add``- ``add`` or ``delete``.+``tree append`` works out every operation of the package, checks each+as a target will and signs the publication before it uploads anything:+when it refuses, the registry is as it was. An entry no target could+take, one with a control character in its id or in a selector value,+fails with ``bpm tree append: the tree entry of <id> makes an operation+no target takes``.+An ``add`` is refused when an XDP program of the upatch selects a deviceclass that an XDP program of another upatch in the tree selects, and theselectors of the two operations do not exclude each other. A device runsdiff --git a/testing/functional/bpm/t7005-bpm-publish.sh b/testing/functional/bpm/t7005-bpm-publish.shindex 224471f0..ed1c347d 100644--- a/testing/functional/bpm/t7005-bpm-publish.sh+++ b/testing/functional/bpm/t7005-bpm-publish.sh@@ -487,7 +487,19 @@ test_expect_success 'tree append refuses two upatches of one package that select--keys-dir "$WORK/keys" >/dev/null 2>"$WORK/t7005xg.err" &&grep -q "t7005xh and t7005xg (seq 1) both select XDP device classes 16 " \"$WORK/t7005xg.err" &&- ! test -e "$REG/ns/xdp2/manifests/stable"+ ! test -e "$REG/ns/xdp2"+'++# No node can digest an operation with a control character in it, so the+# entry is refused, and before its artifact reaches the registry.+test_expect_success 'tree append uploads nothing for an entry no target would take' '+ xdp_package t7005ct 16 "$(site "\"a\\\\tb\"")" &&+ grep -q "^site = \[\"a.tb\"\]\$" "$WORK/t7005ct/Beefile" &&+ ! "$S" bpm tree append "$WORK/t7005ct-1.0.0.bee" "oci://$REGISTRY/ns/ctl" \+ --keys-dir "$WORK/keys" >/dev/null 2>"$WORK/t7005ct.err" &&+ grep -q "the tree entry of t7005ct makes an operation no target takes" \+ "$WORK/t7005ct.err" &&+ ! test -e "$REG/ns/ctl"'test_expect_success 'the operator commands are reachable directly in shirudo-build' 'diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.cindex a3845c8c..11aa11d5 100644--- a/tools/shirudo-build/bpm-publish.c+++ b/tools/shirudo-build/bpm-publish.c@@ -550,26 +550,46 @@ static void tree_selector_canonicalize(struct tree_selector *s)TREE_STRING_MAX, tree_string_compare);}+/* An artifact that is described for its operation and not uploaded yet. */+struct tree_upload {+ __u8 *body;+ size_t body_len;+ char tag[160];+ char *manifest;+ size_t manifest_len;+};++static int tree_describe_blob(const void *body, size_t len, const char *media,+ struct bpm_descriptor *out)+{+ char hex[65];++ if (bpm_sha256_hex(body, len, hex, sizeof(hex)) ||+ strsprintf(out->digest, sizeof(out->digest), "sha256:%s", hex) < 0)+ return -EINVAL;+ bpm_copy_string(out->media, sizeof(out->media), media);+ out->size = (__s64)len;+ return 0;+}+/*- * Publish one immutable upatch artifact and describe it for the tree.- * The bytes are checked to be the directive the operation claims (same- * name, load versus unload) before anything is uploaded, so a target+ * Describe one immutable upatch artifact for the tree, with the manifest+ * its upload will put. The bytes are checked to be the directive the+ * operation claims, same name and load versus unload, so that a target* never meets an operation its own tree_verify_upatch() would reject.*/-static int tree_push_artifact(struct bpm_oci_client *c, const char *repository,- const char *raw_repository,- const __u8 *body, size_t len,- const char *id, bool unload,- struct tree_artifact *out)+static int tree_describe_artifact(const char *raw_repository,+ const __u8 *body, size_t len,+ const char *id, bool unload,+ struct tree_artifact *out,+ struct tree_upload *up){char name[UPATCH_NAME_MAX + 1], title[UPATCH_NAME_MAX + 16];char temporary[] = "/tmp/shirudo-bpm-tree-XXXXXX";- struct bpm_descriptor layer, config;- char empty_config[] = "{}";+ struct bpm_descriptor layer = {}, config = {};+ char hex[65], manifest_hex[65];struct json_emit e;struct upatch u;- char hex[65], tag[160];- char *manifest;int fd, ret;fd = mkstemp(temporary);@@ -595,23 +615,21 @@ static int tree_push_artifact(struct bpm_oci_client *c, const char *repository,unload ? "load" : "unload", name);return ret;}- if (strsprintf(tag, sizeof(tag), "upatch-%s-%.16s", id, hex) < 0 || strlen(tag) > 128)+ if (strsprintf(up->tag, sizeof(up->tag), "upatch-%s-%.16s", id, hex) < 0 ||+ strlen(up->tag) > 128)return -ENAMETOOLONG;if (strsprintf(title, sizeof(title), "%s.upatch", id) < 0)return -ENAMETOOLONG;- ret = bpm_oci_push_blob(c, repository, body, len, BPM_UPATCH_MEDIA,- &layer);- if (ret)- return ret;- ret = bpm_oci_push_blob(c, repository, empty_config,- strlen(empty_config), BPM_EMPTY_CONFIG_MEDIA,- &config);+ ret = tree_describe_blob(body, len, BPM_UPATCH_MEDIA, &layer);+ if (!ret)+ ret = tree_describe_blob("{}", 2, BPM_EMPTY_CONFIG_MEDIA,+ &config);if (ret)return ret;- manifest = xmalloc(BPM_MANIFEST_CAP);- if (!manifest)+ up->manifest = xmalloc(BPM_MANIFEST_CAP);+ if (!up->manifest)return -ENOMEM;- e = (struct json_emit){ .buf = manifest, .cap = BPM_MANIFEST_CAP };+ e = (struct json_emit){ .buf = up->manifest, .cap = BPM_MANIFEST_CAP };json_obj_open(&e);json_kv_int(&e, "schemaVersion", 2);json_kv_string(&e, "mediaType", BPM_MANIFEST_MEDIA);@@ -624,23 +642,43 @@ static int tree_push_artifact(struct bpm_oci_client *c, const char *repository,json_kv_string(&e, "org.opencontainers.image.title", title);json_obj_close(&e);json_obj_close(&e);- ret = e.err;- if (!ret)- ret = bpm_oci_put_manifest(c, repository, tag,- (const __u8 *)manifest, e.len,- out->manifest_digest,- sizeof(out->manifest_digest));- xfree(manifest, BPM_MANIFEST_CAP);- if (ret)- return ret;- bpm_copy_string(out->repository, sizeof(out->repository), raw_repository);- bpm_copy_string(out->layer_digest, sizeof(out->layer_digest), layer.digest);- if (strsprintf(out->payload_sha256, sizeof(out->payload_sha256),+ if (e.err)+ return e.err;+ up->manifest_len = e.len;+ if (bpm_sha256_hex(up->manifest, e.len, manifest_hex,+ sizeof(manifest_hex)) ||+ strsprintf(out->manifest_digest, sizeof(out->manifest_digest),+ "sha256:%s", manifest_hex) < 0 ||+ strsprintf(out->payload_sha256, sizeof(out->payload_sha256),"sha256:%s", hex) < 0)return -E2BIG;+ bpm_copy_string(out->repository, sizeof(out->repository), raw_repository);+ bpm_copy_string(out->layer_digest, sizeof(out->layer_digest), layer.digest);return 0;}+static int tree_upload_artifact(struct bpm_oci_client *c, const char *repository,+ const struct tree_upload *up)+{+ struct bpm_descriptor layer, config;+ char empty_config[] = "{}";+ char digest[72];+ int ret;++ ret = bpm_oci_push_blob(c, repository, up->body, up->body_len,+ BPM_UPATCH_MEDIA, &layer);+ if (!ret)+ ret = bpm_oci_push_blob(c, repository, empty_config,+ strlen(empty_config),+ BPM_EMPTY_CONFIG_MEDIA, &config);+ if (!ret)+ ret = bpm_oci_put_manifest(c, repository, up->tag,+ (const __u8 *)up->manifest,+ up->manifest_len, digest,+ sizeof(digest));+ return ret;+}+/* The classes of a load upatch, none for one without an XDP program. */static int upatch_xdp_classes(const void *body, size_t len, __u32 *classes){@@ -757,6 +795,7 @@ static int cmd_bpm_tree_append(int argc, char **argv)char key_path[PATH_MAX], cert_path[PATH_MAX];char parent[BPM_MAX_DIGEST] = {}, digest[72];struct tree_xdp_classes *seen = NULL;+ struct tree_upload *uploads = NULL;struct tree_publication pub = {};struct tree_operation *op = NULL;size_t len, payload_len = 0, sig_len = 0;@@ -909,7 +948,8 @@ static int cmd_bpm_tree_append(int argc, char **argv)op = xmalloc(sizeof(*op));seen = xcalloc(1, sizeof(*seen));- if (!op || !seen) {+ uploads = xcalloc(BPM_BUILD_MAX_ENTRIES, sizeof(*uploads));+ if (!op || !seen || !uploads) {ret = -ENOMEM;goto out;}@@ -960,15 +1000,25 @@ static int cmd_bpm_tree_append(int argc, char **argv)classes);}if (!ret)- ret = tree_push_artifact(&c, repository, repo_ref, body,- body_len, u->tree.id, unload,- &op->artifact);- xfree(body, body_len + 1);- if (ret)+ ret = tree_describe_artifact(repo_ref, body, body_len,+ u->tree.id, unload,+ &op->artifact,+ &uploads[appended]);+ if (ret) {+ xfree(body, body_len + 1);goto out;+ }+ uploads[appended].body = body;+ uploads[appended].body_len = body_len;ret = tree_operation_digest(op, op->operation_digest);if (!ret)- ret = tree_publication_append(&pub, op);+ ret = tree_operation_validate(op);+ if (ret) {+ pr_err("bpm tree append: the tree entry of %s makes an operation no target takes",+ u->tree.id);+ goto out;+ }+ ret = tree_publication_append(&pub, op);if (ret)goto out;seen->classes[pub.n_operations - 1] = classes;@@ -987,6 +1037,12 @@ static int cmd_bpm_tree_append(int argc, char **argv)ret = sign_detached(payload, payload_len, key, cert, &sig, &sig_len);if (ret)goto out;+ /* Nothing is uploaded before all of it is known to be publishable. */+ for (int i = 0; i < appended; i++) {+ ret = tree_upload_artifact(&c, repository, &uploads[i]);+ if (ret)+ goto out;+ }ret = bpm_oci_push_blob(&c, repository, empty_config,strlen(empty_config), BPM_EMPTY_CONFIG_MEDIA,&config);@@ -1032,6 +1088,11 @@ out:tree_selector_free(&op->selector);xfree(op, sizeof(*op));xfree(seen, sizeof(*seen));+ for (size_t i = 0; uploads && i < BPM_BUILD_MAX_ENTRIES; i++) {+ xfree(uploads[i].manifest, BPM_MANIFEST_CAP);+ xfree(uploads[i].body, uploads[i].body_len + 1);+ }+ xfree(uploads, BPM_BUILD_MAX_ENTRIES * sizeof(*uploads));tree_publication_free(&pub);bpm_build_spec_free(spec);xfree(beefile, len + 1);