Skip to content

eBPF-based security platform

bpm: fetch a signed document and its signature in one place

Daniel Borkmannf38d18aad780
diff --git a/Documentation/reference/formats/bpm-formats.rst b/Documentation/reference/formats/bpm-formats.rst
index 23796677..b0738561 100644
--- a/Documentation/reference/formats/bpm-formats.rst
+++ b/Documentation/reference/formats/bpm-formats.rst
@@ -231,8 +231,8 @@ One index per registry, at ``<registry>/shirudo/bpm-index:stable``.
- Content
* - Index manifest
- OCI image manifest, ``schemaVersion`` 2, ``artifactType``
- ``application/vnd.shirudo.bpm.index.v1``, the empty config, and two
- layers.
+ ``application/vnd.shirudo.bpm.index.v1``, the empty config, and
+ exactly one document layer and one signature layer.
* - Document layer
- The index document, media type
``application/vnd.shirudo.bpm.index.v1+json``.
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 52638066..029a8c16 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -1038,52 +1038,15 @@ static int repo_update_one(const char *name)
rc = bpm_oci_url(&c, BPM_INDEX_REPOSITORY, "manifests", BPM_INDEX_TAG, url, sizeof(url));
if (rc)
return rc;
- rc = bpm_oci_get_memory(&c, url, "application/vnd.oci.image.manifest.v1+json", &manifest);
+ rc = bpm_oci_get_memory(&c, url, BPM_MANIFEST_MEDIA, &manifest);
if (rc)
goto out;
rc = bpm_oci_parse_manifest(manifest.data, manifest.len, layers,
ARRAY_SIZE(layers), &nl, NULL, 0);
if (rc)
goto out;
- if (nl < 2) {
- rc = -EINVAL;
- goto out;
- }
- for (int i = 0; i < nl; i++) {
- if (!strcmp(layers[i].media, BPM_INDEX_MEDIA)) {
- char layer_hex[65];
-
- rc = bpm_oci_url(&c, BPM_INDEX_REPOSITORY, "blobs",
- layers[i].digest, url, sizeof(url));
- if (rc)
- goto out;
- rc = bpm_oci_get_blob(&c, url, BPM_INDEX_MEDIA, &index);
- if (!rc && (bpm_sha256_hex(index.data, index.len, layer_hex,
- sizeof(layer_hex)) ||
- !bpm_digest_matches(layers[i].digest, layer_hex)))
- rc = -EKEYREJECTED;
- break;
- }
- }
- if (rc)
- goto out;
- for (int i = 0; i < nl; i++) {
- if (!strcmp(layers[i].media, BPM_INDEX_SIG_MEDIA)) {
- char sig_hex[65];
-
- rc = bpm_oci_url(&c, BPM_INDEX_REPOSITORY, "blobs",
- layers[i].digest, url, sizeof(url));
- if (rc)
- goto out;
- rc = bpm_oci_get_blob(&c, url, BPM_INDEX_SIG_MEDIA, &sig);
- if (!rc && (bpm_sha256_hex(sig.data, sig.len, sig_hex, sizeof(sig_hex)) ||
- !bpm_digest_matches(layers[i].digest, sig_hex)))
- rc = -EKEYREJECTED;
- break;
- }
- }
- if (!rc && !sig.data)
- rc = -EINVAL;
+ rc = bpm_oci_get_signed(&c, BPM_INDEX_REPOSITORY, layers, nl,
+ BPM_INDEX_MEDIA, BPM_INDEX_SIG_MEDIA, &index, &sig);
if (rc)
goto out;
if (verify_with_source_key(&s, index.data, index.len, sig.data,
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index 0ee532ae..6efd5673 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -107,7 +107,7 @@ static int fetch_index(struct bpm_oci_client *c, const char *cert,
{
struct bpm_buffer manifest = {}, payload = {}, sig = {};
struct bpm_descriptor *layers = NULL;
- char url[PATH_MAX], hex[65];
+ char url[PATH_MAX];
int nl = 0, rc;
*n = 0;
@@ -129,37 +129,11 @@ static int fetch_index(struct bpm_oci_client *c, const char *cert,
BPM_MAX_ENTRIES, &nl, NULL, 0);
if (rc)
goto out;
- for (int i = 0; i < nl; i++) {
- struct bpm_buffer *dst;
-
- if (!strcmp(layers[i].media, BPM_INDEX_MEDIA))
- dst = &payload;
- else if (!strcmp(layers[i].media, BPM_INDEX_SIG_MEDIA))
- dst = &sig;
- else
- continue;
- if (dst->data) {
- rc = -EINVAL;
- goto out;
- }
- rc = bpm_oci_url(c, BPM_INDEX_REPOSITORY, "blobs", layers[i].digest,
- url, sizeof(url));
- if (rc)
- goto out;
- rc = bpm_oci_get_blob(c, url, layers[i].media, dst);
- if (rc)
- goto out;
- if ((layers[i].size >= 0 && (__s64)dst->len != layers[i].size) ||
- bpm_sha256_hex(dst->data, dst->len, hex, sizeof(hex)) ||
- !bpm_digest_matches(layers[i].digest, hex)) {
- rc = -EKEYREJECTED;
- goto out;
- }
- }
- if (!payload.data || !sig.data) {
- rc = -EINVAL;
+ rc = bpm_oci_get_signed(c, BPM_INDEX_REPOSITORY, layers, nl,
+ BPM_INDEX_MEDIA, BPM_INDEX_SIG_MEDIA, &payload,
+ &sig);
+ if (rc)
goto out;
- }
if (bpm_verify_cms(payload.data, payload.len, sig.data, sig.len, cert)) {
pr_err("bpm push: the published repository index is not signed by %s",
cert);
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index 1ceb5fe2..ed8023bb 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -906,6 +906,44 @@ static int on_descriptor(void *ud)
return 0;
}
+int bpm_oci_get_signed(struct bpm_oci_client *c, const char *repository,
+ const struct bpm_descriptor *layers, int n_layers,
+ const char *payload_media, const char *sig_media,
+ struct bpm_buffer *payload, struct bpm_buffer *sig)
+{
+ int n_payload = 0, n_sig = 0, i, rc;
+ char url[PATH_MAX], hex[65];
+
+ for (i = 0; i < n_layers; i++) {
+ struct bpm_buffer *dst;
+ int *seen;
+
+ if (!strcmp(layers[i].media, payload_media)) {
+ dst = payload;
+ seen = &n_payload;
+ } else if (!strcmp(layers[i].media, sig_media)) {
+ dst = sig;
+ seen = &n_sig;
+ } else {
+ continue;
+ }
+ if ((*seen)++)
+ return -EINVAL;
+ rc = bpm_oci_url(c, repository, "blobs", layers[i].digest,
+ url, sizeof(url));
+ if (rc)
+ return rc;
+ rc = bpm_oci_get_blob(c, url, layers[i].media, dst);
+ if (rc)
+ return rc;
+ if ((layers[i].size >= 0 && (__s64)dst->len != layers[i].size) ||
+ bpm_sha256_hex(dst->data, dst->len, hex, sizeof(hex)) ||
+ !bpm_digest_matches(layers[i].digest, hex))
+ return -EKEYREJECTED;
+ }
+ return payload->data && sig->data ? 0 : -EINVAL;
+}
+
int bpm_oci_parse_manifest(const __u8 *body, size_t len,
struct bpm_descriptor *layers, int max_layers,
int *n_layers, char *package_name,
diff --git a/util/bpm_oci.h b/util/bpm_oci.h
index db8a1a73..a247d9d6 100644
--- a/util/bpm_oci.h
+++ b/util/bpm_oci.h
@@ -138,6 +138,16 @@ int bpm_oci_parse_manifest(const __u8 *body, size_t len,
struct bpm_descriptor *layers, int max_layers,
int *n_layers, char *package_name,
size_t package_name_sz);
+/*
+ * The one layer of payload_media and the one of sig_media among a
+ * manifest's layers, each matching its descriptor's digest and declared
+ * size: -EINVAL unless there is exactly one of each, -EKEYREJECTED on a
+ * mismatch. The caller frees both buffers, also on failure.
+ */
+int bpm_oci_get_signed(struct bpm_oci_client *c, const char *repository,
+ const struct bpm_descriptor *layers, int n_layers,
+ const char *payload_media, const char *sig_media,
+ struct bpm_buffer *payload, struct bpm_buffer *sig);
void bpm_oci_emit_descriptor(struct shirudo_json_emit *e, const char *key,
const struct bpm_descriptor *d, const char *title);
diff --git a/util/bpm_package.c b/util/bpm_package.c
index 445ed8f7..6bff16b9 100644
--- a/util/bpm_package.c
+++ b/util/bpm_package.c
@@ -796,8 +796,7 @@ int bpm_tree_fetch_publication(struct bpm_oci_client *c,
struct bpm_buffer manifest = {}, payload = {}, sig = {};
struct bpm_descriptor *layers = NULL;
char url[PATH_MAX], hex[65];
- int nl = 0, payload_layers = 0, sig_layers = 0;
- int rc;
+ int nl = 0, rc;
if (!reference[0] || strchr(reference, '/') || strchr(reference, '?') ||
strchr(reference, '#') || strstr(reference, ".."))
@@ -826,40 +825,10 @@ int bpm_tree_fetch_publication(struct bpm_oci_client *c,
BPM_MAX_ENTRIES, &nl, NULL, 0);
if (rc)
goto out;
- for (int i = 0; i < nl; i++) {
- struct bpm_buffer *dst;
-
- if (!strcmp(layers[i].media, BPM_TREE_MEDIA)) {
- dst = &payload;
- payload_layers++;
- } else if (!strcmp(layers[i].media, BPM_TREE_SIG_MEDIA)) {
- dst = &sig;
- sig_layers++;
- } else {
- continue;
- }
- if (dst->data) {
- rc = -EINVAL;
- goto out;
- }
- rc = bpm_oci_url(c, repository, "blobs", layers[i].digest,
- url, sizeof(url));
- if (rc)
- goto out;
- rc = bpm_oci_get_blob(c, url, layers[i].media, dst);
- if (rc)
- goto out;
- if ((layers[i].size >= 0 && (__s64)dst->len != layers[i].size) ||
- bpm_sha256_hex(dst->data, dst->len, hex, sizeof(hex)) ||
- !bpm_digest_matches(layers[i].digest, hex)) {
- rc = -EKEYREJECTED;
- goto out;
- }
- }
- if (payload_layers != 1 || sig_layers != 1 || !payload.data || !sig.data) {
- rc = -EINVAL;
+ rc = bpm_oci_get_signed(c, repository, layers, nl, BPM_TREE_MEDIA,
+ BPM_TREE_SIG_MEDIA, &payload, &sig);
+ if (rc)
goto out;
- }
if (bpm_verify_cms(payload.data, payload.len, sig.data, sig.len, cert_path)) {
rc = -EKEYREJECTED;
goto out;