Skip to content

eBPF-based security platform

bpm: let tree append take an oci:// package by its manifest digest

John Fastabend03be81d69f46
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 28dd796d..5c106521 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -619,7 +619,9 @@ signed config image.
not trip, and of an entry no target would take,
each before anything is uploaded, its refusal to extend a tree
without ``--tree-id`` or with another tree's id, A-to-B tree
- signing-key rotation with explicit predecessor verification
+ signing-key rotation with explicit predecessor verification, and
+ an ``oci://`` package taken by its tag and ``--package-digest``,
+ refused when the tag serves another manifest
* - ``t7006-policy-labels``
- Beefile policy declarations are serialized into the signed
upatch section
diff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rst
index ea9eb1b3..0ef23f7f 100644
--- a/Documentation/reference/cli/bpm.rst
+++ b/Documentation/reference/cli/bpm.rst
@@ -265,6 +265,20 @@ package as the next operations of the signed tree at
- Operations after which a segment is sealed, 1 to 512. Any other
value is refused with ``bpm tree append: --segment-ops must be 1
to 512``. It may differ from one append to the next.
+ * - ``--package-digest``
+ - none
+ - ``sha256:`` and 64 lowercase hex digits, the manifest digest of an
+ ``oci://`` package, as ``bpm push`` prints it and the repository
+ index lists it. Required with an ``oci://`` package and refused
+ with a package file.
+
+``<package>`` is a ``.bee`` file, or ``oci://<registry>/<repository>:<tag>``
+with ``--package-digest``. The append downloads that package to a file
+under ``/tmp``, which it removes when it ends, and refuses it with ``bpm
+tree append: <package> serves manifest sha256:<hex>, not <digest>`` when
+the tag names another manifest: a registry can point a tag at any package
+the key ever signed, an older one included. Either form without the
+other is a usage error.
The existing publication must pass the checks ``tree sync`` makes before
it applies anything: the sequence numbers, the ``base`` keys and every
diff --git a/testing/functional/bpm/t7005-bpm-publish.sh b/testing/functional/bpm/t7005-bpm-publish.sh
index 09a08dd7..a6726c79 100644
--- a/testing/functional/bpm/t7005-bpm-publish.sh
+++ b/testing/functional/bpm/t7005-bpm-publish.sh
@@ -639,4 +639,51 @@ test_expect_success 'a wrong explicit predecessor certificate leaves the tree un
cmp "$REG/ns/tree/manifests/stable" "$WORK/rotated.manifest"
'
+digest_of() {
+ printf 'sha256:%s' "$(sha256sum <"$REG/ns/t7005/manifests/$1" | cut -d" " -f1)"
+}
+
+test_expect_success 'bpm tree append takes a pushed package by its tag and manifest digest' '
+ "$S" bpm tree append "oci://$REGISTRY/ns/t7005:1.0.0" "oci://$REGISTRY/ns/tree-oci" \
+ --package-digest "$(digest_of 1.0.0)" --keys-dir "$WORK/keys" \
+ >"$WORK/append-oci.out" 2>&1 &&
+ grep -q "^oci://$REGISTRY/ns/tree-oci:stable sha256:" "$WORK/append-oci.out" &&
+ split_layers "$REG/ns/tree-oci/manifests/stable" "$REG/ns/tree-oci" "$WORK/tree-oci" &&
+ cms_verify "$WORK/tree-oci.sig" "$WORK/tree-oci.payload" &&
+ python3 - "$WORK/tree-oci.payload" "$WORK/load.json" <<"PYTEST"
+import json, pathlib, sys
+pub = json.loads(pathlib.Path(sys.argv[1]).read_bytes())
+load = json.load(open(sys.argv[2]))
+op = pub["operations"][0]
+assert pub["tree_id"] == "t7005" and len(pub["operations"]) == 1, pub
+assert op["op"] == "add" and op["id"] == "t7005", op
+assert op["artifact"]["payload_sha256"] == "sha256:" + load["payload_sha256"], op
+PYTEST
+'
+
+# A tag is the registry's to move to any package the key signed, an older
+# one included: the digest the operator names decides which is appended.
+test_expect_success 'bpm tree append refuses a tag that serves another manifest than the digest' '
+ ! "$S" bpm tree append "oci://$REGISTRY/ns/t7005:1.0.0" "oci://$REGISTRY/ns/tree-moved" \
+ --package-digest "$(digest_of 0.9.0)" --keys-dir "$WORK/keys" \
+ >/dev/null 2>"$WORK/moved.err" &&
+ grep -q "serves manifest $(digest_of 1.0.0), not $(digest_of 0.9.0)" "$WORK/moved.err" &&
+ ! test -e "$REG/ns/tree-moved"
+'
+
+usage_status() {
+ "$S" bpm tree append "$@" --keys-dir "$WORK/keys" >/dev/null 2>"$WORK/usage.err"
+ test $? -eq 2
+}
+
+test_expect_success 'bpm tree append takes --package-digest with an oci:// package alone' '
+ usage_status "oci://$REGISTRY/ns/t7005:1.0.0" "oci://$REGISTRY/ns/tree-oci" &&
+ grep -q "package-digest names the manifest of an oci:// package" "$WORK/usage.err" &&
+ usage_status "$WORK/t7005-1.0.0.bee" "oci://$REGISTRY/ns/tree-oci" \
+ --package-digest "$(digest_of 1.0.0)" &&
+ usage_status "oci://$REGISTRY/ns/t7005:1.0.0" "oci://$REGISTRY/ns/tree-oci" \
+ --package-digest sha256:ABC &&
+ grep -q "is not sha256:<64 lowercase hex>" "$WORK/usage.err"
+'
+
test_done
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index 8c62c44b..a015f16c 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -14,6 +14,7 @@
#include <string.h>
#include <sys/stat.h>
#include <time.h>
+#include <unistd.h>
#include <curl/curl.h>
#include <shirudo/misc.h>
@@ -54,6 +55,7 @@ enum tree_append_opt {
TREE_APPEND_REFERENCE,
TREE_APPEND_OP,
TREE_APPEND_SEGMENT_OPS,
+ TREE_APPEND_PACKAGE_DIGEST,
};
static const struct opt_spec tree_append_opts[] = {
@@ -75,6 +77,8 @@ static const struct opt_spec tree_append_opts[] = {
"seal a segment holding n operations and start the next, 1 to "
__stringify(TREE_MAX_OPS) " (default "
__stringify(TREE_SEGMENT_OPS) ")" },
+ [TREE_APPEND_PACKAGE_DIGEST] = { "--package-digest", 0, 0,
+ "<sha256:hex>", "the manifest digest of an oci:// package" },
};
static const struct help_section tree_append_sections[] = {
@@ -82,12 +86,13 @@ static const struct help_section tree_append_sections[] = {
};
static const struct help tree_append_help = {
- .usage = "shirudo bpm tree append <package-file> <repository> "
- "[options]",
+ .usage = "shirudo bpm tree append <package-file|oci://host/repo:tag> "
+ "<repository> [options]",
.summary = "Publish the [upatches.tree] entries of a built package "
"as the next\noperations of the signed tree at "
"<repository>:<ref>. The key defaults to\n--keys-dir, "
- KEYS_DIR_ENV " or " KEYS_DIR ".",
+ KEYS_DIR_ENV " or " KEYS_DIR ". An oci:// package needs "
+ "--package-digest,\nthe digest of its manifest.",
.sections = tree_append_sections,
.n_sections = ARRAY_SIZE(tree_append_sections),
};
@@ -1204,9 +1209,15 @@ static int tree_new_id(const char *tree_id, const char *package,
return 0;
}
+#define TREE_APPEND_DOWNLOAD "/tmp/shirudo-bpm-append-XXXXXX"
+
/* One bpm tree append, filled in by its steps in turn. */
struct tree_append {
const char *package;
+ const char *package_digest;
+ const char *package_file;
+ char download[sizeof(TREE_APPEND_DOWNLOAD)];
+ bool downloaded;
const char *raw_repository;
const char *keys_dir_arg;
const char *key;
@@ -1243,6 +1254,17 @@ struct tree_append {
size_t sig_len;
};
+static bool package_digest_ok(const char *digest)
+{
+ char canonical[SHA256_DIGEST_STR_SIZE];
+ __u8 raw[CRYPTO_SHA256_LEN];
+
+ if (sha256_digest_decode(digest, raw))
+ return false;
+ sha256_digest_encode(raw, canonical);
+ return !strcmp(digest, canonical);
+}
+
/*
* 0, or EXIT_USAGE for a usage error, reported, or OPT_DONE where help
* or the option list was printed.
@@ -1289,6 +1311,14 @@ static int tree_append_parse(struct tree_append *ta, int argc,
return EXIT_USAGE;
ta->segment_ops = (int)n_ops;
break;
+ case TREE_APPEND_PACKAGE_DIGEST:
+ if (!package_digest_ok(p.value)) {
+ opt_usage(&p, "--package-digest %s is not sha256:<64 lowercase hex>",
+ p.value);
+ return EXIT_USAGE;
+ }
+ ta->package_digest = p.value;
+ break;
case OPT_ARG:
if (ta->raw_repository) {
opt_usage(&p, "unexpected argument '%s'",
@@ -1310,6 +1340,10 @@ static int tree_append_parse(struct tree_append *ta, int argc,
opt_usage(&p, "takes <package-file> and <repository>");
return EXIT_USAGE;
}
+ if (!!str_has_prefix(ta->package, "oci://") != !!ta->package_digest) {
+ opt_usage(&p, "--package-digest names the manifest of an oci:// package, and one needs it");
+ return EXIT_USAGE;
+ }
ta->unload = op_delete;
return 0;
}
@@ -1350,14 +1384,31 @@ static int tree_append_trim_repository(struct tree_append *ta)
return 0;
}
+static int tree_append_fetch_package(struct tree_append *ta)
+{
+ int ret;
+
+ ta->package_file = ta->package;
+ if (!ta->package_digest)
+ return 0;
+ memcpy(ta->download, TREE_APPEND_DOWNLOAD, sizeof(ta->download));
+ ret = build_download_package("bpm tree append", ta->package,
+ ta->package_digest, ta->download);
+ if (ret)
+ return ret;
+ ta->downloaded = true;
+ ta->package_file = ta->download;
+ return 0;
+}
+
static int tree_append_read_package(struct tree_append *ta)
{
int ret;
- ret = bpm_pkg_load_info(ta->package, &ta->info);
+ ret = bpm_pkg_load_info(ta->package_file, &ta->info);
if (ret)
return ret;
- ret = bpm_pkg_read_member_in(ta->package, "Beefile", NULL,
+ ret = bpm_pkg_read_member_in(ta->package_file, "Beefile", NULL,
ta->package_dir, sizeof(ta->package_dir),
&ta->beefile, &ta->beefile_len,
4u * 1024u * 1024u);
@@ -1571,7 +1622,7 @@ static int tree_append_entry(struct tree_append *ta,
return -EOVERFLOW;
}
member = ta->unload ? u->tree.unload_artifact : u->tree.load_artifact;
- ret = bpm_pkg_read_member_in(ta->package, member, ta->package_dir,
+ ret = bpm_pkg_read_member_in(ta->package_file, member, ta->package_dir,
NULL, 0, &body, &body_len,
UPATCH_FILE_MAX);
if (ret) {
@@ -1706,6 +1757,8 @@ static int tree_append_run(struct tree_append *ta)
ret = tree_append_signing_keys(ta);
if (!ret)
ret = tree_append_trim_repository(ta);
+ if (!ret)
+ ret = tree_append_fetch_package(ta);
if (!ret)
ret = tree_append_read_package(ta);
if (!ret)
@@ -1741,6 +1794,8 @@ static void tree_append_free(struct tree_append *ta)
xfree(ta->beefile, ta->beefile_len + 1);
xfree(ta->info.cert, ta->info.cert_len + 1);
bpm_oci_client_clear(&ta->c);
+ if (ta->downloaded)
+ unlink(ta->download);
}
static int cmd_bpm_tree_append(int argc, const char **argv)