Skip to content

eBPF-based security platform

bpm: require the package size in every index entry

Daniel Borkmann89e1ca64b8e5
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index cfe1e4ee..2ceed164 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 eleven of the modules in
-:src:`util/`: ``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 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.
.. list-table::
:header-rows: 1
@@ -621,7 +621,8 @@ Coverage by area
- ``t7001``, ``t7002``, ``t7003``, ``t7004``, ``t7005``,
``t7006``. Fuzz:
``beefile``, ``bpm-index``, ``bpm-package``, ``bpm-tar``,
- ``oci-http``, ``oci-manifest``, ``oci-ref``, ``registry-auth``
+ ``oci-http``, ``oci-manifest``, ``oci-ref``, ``registry-auth``. Unit:
+ ``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.rst
index 833d385a..51586502 100644
--- a/Documentation/reference/formats/bpm-formats.rst
+++ b/Documentation/reference/formats/bpm-formats.rst
@@ -281,8 +281,8 @@ The index document:
package layer has another digest, and a download that hashes to
another value.
* - ``package_size``
- - Size of the ``.bee`` layer in bytes. When positive, the target
- refuses a layer of another size.
+ - Size of the ``.bee`` layer in bytes, required and positive. The
+ target refuses a layer of another size.
* - ``signer_fingerprint``
- ``shirudo.spki_sha256_fingerprint`` of the package. Not checked by
the target.
diff --git a/testing/unit/bpm_package.c b/testing/unit/bpm_package.c
new file mode 100644
index 00000000..01567f49
--- /dev/null
+++ b/testing/unit/bpm_package.c
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+
+#include <shirudo/string.h>
+
+#include "bpm_oci.h"
+#include "bpm_package.h"
+#include "malloc.h"
+#include "unit.h"
+
+#define DIGEST "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+#define ENTRY(name, size) \
+ "{\"name\":\"" name "\",\"version\":\"1.0.0\"," \
+ "\"repository\":\"shirudo/pkg\"," \
+ "\"manifest_digest\":\"" DIGEST "\"," \
+ "\"package_digest\":\"" DIGEST "\"" size "}"
+
+static int index_of(const char *text, int *n)
+{
+ struct bpm_index_entry *entries;
+ int ret;
+
+ entries = xcalloc(BPM_MAX_ENTRIES, sizeof(*entries));
+ if (!entries)
+ return -ENOMEM;
+ *n = -1;
+ ret = bpm_index_parse((const __u8 *)text, strlen(text), entries, n);
+ xfree(entries, BPM_MAX_ENTRIES * sizeof(*entries));
+ return ret;
+}
+
+/*
+ * Every entry carries the size of its package, which the target holds
+ * the download to: the first one may not leave it out any more than the
+ * others.
+ */
+static void index_entry_needs_its_package_size(void)
+{
+ int n;
+
+ ASSERT_EQ(index_of("{\"schema\":1,\"packages\":["
+ ENTRY("a", ",\"package_size\":10") ","
+ ENTRY("b", ",\"package_size\":20") "]}", &n), 0);
+ EXPECT_EQ(n, 2);
+ EXPECT_EQ(index_of("{\"schema\":1,\"packages\":["
+ ENTRY("a", "") "]}", &n), -EINVAL);
+ EXPECT_EQ(index_of("{\"schema\":1,\"packages\":["
+ ENTRY("a", ",\"package_size\":10") ","
+ ENTRY("b", "") "]}", &n), -EINVAL);
+ EXPECT_EQ(index_of("{\"schema\":1,\"packages\":["
+ ENTRY("a", ",\"package_size\":null") "]}", &n),
+ -EINVAL);
+ EXPECT_EQ(index_of("{\"schema\":1,\"packages\":["
+ ENTRY("a", ",\"package_size\":0") "]}", &n),
+ -EINVAL);
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(index_entry_needs_its_package_size),
+ {}
+};
+
+UNIT_SUITE(bpm_package, cases);
diff --git a/util/bpm_package.c b/util/bpm_package.c
index 2b75b6d4..eccef66e 100644
--- a/util/bpm_package.c
+++ b/util/bpm_package.c
@@ -50,7 +50,7 @@ static int on_index_entry(void *ud)
return -E2BIG;
if (!bpm_name_ok(s->scratch.name) || !bpm_version_ok(s->scratch.version) ||
!s->scratch.repository[0] || !s->scratch.manifest_digest[0] ||
- !s->scratch.package_digest[0] || s->scratch.package_size < 0)
+ !s->scratch.package_digest[0] || s->scratch.package_size <= 0)
return -EINVAL;
s->items[s->n++] = s->scratch;
/* A field the next entry leaves out is not this entry's. */
@@ -67,6 +67,8 @@ int bpm_index_parse(const __u8 *body, size_t len,
struct json_field sub[7], root[2];
__s64 schema = 0;
+ /* Missing from the first entry as from any other, not 0. */
+ sink.scratch.package_size = -1;
memset(sub, 0, sizeof(sub));
sub[0] = (struct json_field){
.key = "name", .type = JSON_T_STRING,