eBPF-based security platform
bpm: require a whole descriptor for every manifest layer
Daniel Borkmanna79a4da5b0fb
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 2ceed164..498c7ae5 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -491,13 +491,13 @@ signed config image.Unit suites and fuzz targets~~~~~~~~~~~~~~~~~~~~~~~~~~~~-The unit suites in :src:`testing/unit/` cover twelve of the modules in-:src:`util/`: ``bpm_package``, ``chain``, ``eventlog``, ``hex``, ``io``,-``json``, ``kernel_abi``, ``kernel_build_id``, ``kernel_release``,-``string``, ``upatch`` and ``upatch_decode``, and ``overflow`` the-arithmetic of :src:`include/shirudo/overflow.h`. The other modules are-reached through the test helpers, the fuzz targets and the binaries the-functional tests run.+The unit suites in :src:`testing/unit/` cover thirteen of the modules in+:src:`util/`: ``bpm_oci``, ``bpm_package``, ``chain``, ``eventlog``,+``hex``, ``io``, ``json``, ``kernel_abi``, ``kernel_build_id``,+``kernel_release``, ``string``, ``upatch`` and ``upatch_decode``, and+``overflow`` the arithmetic of :src:`include/shirudo/overflow.h`. The+other modules are reached through the test helpers, the fuzz targets and+the binaries the functional tests run... list-table:::header-rows: 1@@ -622,7 +622,7 @@ Coverage by area``t7006``. Fuzz:``beefile``, ``bpm-index``, ``bpm-package``, ``bpm-tar``,``oci-http``, ``oci-manifest``, ``oci-ref``, ``registry-auth``. Unit:- ``bpm_package``+ ``bpm_oci``, ``bpm_package``* - upatch tree- ``t3011``, ``t5004``, ``t7005``, ``t7101``, ``t7102``,``t7103``, ``t7104``, ``t9105``. Fuzz: ``tree``, ``tree-state``,diff --git a/Documentation/reference/formats/bpm-formats.rst b/Documentation/reference/formats/bpm-formats.rstindex 51586502..f7e8f3ac 100644--- a/Documentation/reference/formats/bpm-formats.rst+++ b/Documentation/reference/formats/bpm-formats.rst@@ -219,6 +219,10 @@ Package in the registryThe last path component of ``<repository>`` follows the package name rule.+A manifest shirudo reads, for a package, an index, a tree publication or+an artifact, is refused unless each of its layers is an object with a+``mediaType``, a ``sha256:`` digest and a ``size``.+Repository index----------------diff --git a/testing/unit/bpm_oci.c b/testing/unit/bpm_oci.cnew file mode 100644index 00000000..8fb1c18c--- /dev/null+++ b/testing/unit/bpm_oci.c@@ -0,0 +1,58 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <errno.h>++#include <shirudo/string.h>++#include "bpm_oci.h"+#include "unit.h"++#define DIGEST "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+#define MEDIA "\"mediaType\":\"application/vnd.shirudo.upatch.v1\""++static int manifest_of(const char *layers, int *n)+{+ struct bpm_descriptor descs[4];+ char text[1024];++ ASSERT_TRUE(strsprintf(text, sizeof(text),+ "{\"schemaVersion\":2,\"layers\":[%s]}",+ layers) > 0);+ *n = -1;+ return bpm_oci_parse_manifest((const __u8 *)text, strlen(text), descs,+ ARRAY_SIZE(descs), n, NULL, 0);+}++/*+ * The registry writes the manifest before anything is verified: a layer+ * without its digest or with a negative size would be fetched from an+ * empty URL or skip the size check.+ */+static void manifest_layer_is_a_whole_descriptor(void)+{+ int n;++ ASSERT_EQ(manifest_of("{" MEDIA ",\"digest\":\"" DIGEST "\","+ "\"size\":5}", &n), 0);+ EXPECT_EQ(n, 1);+ EXPECT_EQ(manifest_of("{" MEDIA ",\"size\":5}", &n), -EINVAL);+ EXPECT_EQ(manifest_of("{" MEDIA ",\"digest\":\"sha512:aa\","+ "\"size\":5}", &n), -EINVAL);+ EXPECT_EQ(manifest_of("{" MEDIA ",\"digest\":\"" DIGEST "\"}", &n),+ -EINVAL);+ EXPECT_EQ(manifest_of("{" MEDIA ",\"digest\":\"" DIGEST "\","+ "\"size\":-5}", &n), -EINVAL);+ EXPECT_EQ(manifest_of("{\"digest\":\"" DIGEST "\",\"size\":5}", &n),+ -EINVAL);+ EXPECT_EQ(manifest_of("{" MEDIA ",\"digest\":\"" DIGEST "\","+ "\"size\":5},{" MEDIA ",\"size\":5}", &n),+ -EINVAL);+}++static const struct unit_case cases[] = {+ UNIT_CASE(manifest_layer_is_a_whole_descriptor),+ {}+};++UNIT_SUITE(bpm_oci, cases);diff --git a/util/bpm_oci.c b/util/bpm_oci.cindex c0220dbc..37a065a8 100644--- a/util/bpm_oci.c+++ b/util/bpm_oci.c@@ -916,11 +916,18 @@ struct desc_sink {static int on_descriptor(void *ud){struct desc_sink *s = ud;+ __u8 digest[32];if (s->n >= s->max)return -E2BIG;+ /* The registry wrote this: a layer is all three or none. */+ if (!s->scratch.media[0] || s->scratch.size < 0 ||+ strncmp(s->scratch.digest, "sha256:", 7) ||+ hex_decode(s->scratch.digest + 7, digest, sizeof(digest)))+ return -EINVAL;s->items[s->n++] = s->scratch;memset(&s->scratch, 0, sizeof(s->scratch));+ s->scratch.size = -1;return 0;}@@ -973,6 +980,7 @@ int bpm_oci_parse_manifest(const __u8 *body, size_t len,struct json_field sub[4], root[3];__s64 schema = 0;+ sink.scratch.size = -1;memset(sub, 0, sizeof(sub));sub[0] = (struct json_field){ .key = "mediaType", .type = JSON_T_STRING,.dst = sink.scratch.media, .dst_cap = sizeof(sink.scratch.media) };