Skip to content

eBPF-based security platform

bpm: bound the members of a package together as the target does

Daniel Borkmanna28e3f04867e
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 74dcb041..2c707f76 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -282,16 +282,18 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- ``tar_entry()``, ``tar_octal()``, ``tar_write()`` of
:src:`tools/shirudo-build/bpm-build.c`, against
``bpm_tar_header_ok()`` and ``bpm_tar_octal()``
- - Names of 1, 99 and 100 bytes, every size and mode, a file or a
- directory without content; the checksum for names that end in 16
- free bytes; every value into a field of 8 and 12 bytes;
- ``gzwrite()`` a stub that takes every write
- - Refused before a byte is written from 100 bytes of name or 8 GiB of
- content on; else one 512-byte header the reader takes, with its
- checksum, the name terminated in its field, no prefix, and name,
- size and type read back, uid, gid and mtime 0; content of up to
- 2\ :sup:`31` - 1 bytes padded to where the reader looks for the
- next header; a value wider than its field is refused
+ - Names of 1, 99 and 100 bytes, every size and mode, after members
+ of every total up to 256 MiB, a file or a directory without
+ content; the checksum for names that end in 16 free bytes; every
+ value into a field of 8 and 12 bytes; ``gzwrite()`` a stub that
+ takes every write
+ - Refused before a byte is written or counted from 100 bytes of name
+ on, and with ``EFBIG`` when the member takes the archive past the
+ 256 MiB the extractor unpacks; else counted, and one 512-byte
+ header the reader takes, with its checksum, the name terminated
+ in its field, no prefix, and name, size and type read back, uid,
+ gid and mtime 0, the content padded to where the reader looks for
+ the next header; a value wider than its field is refused
* - ``bpm-beefile-targets``
- ``build_target_ok()``, ``build_add_target()``, against
``bpm_archive_rel_ok()``
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 911501ad..ef47fd28 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -479,11 +479,11 @@ signed config image.
- The repository update unit and timer and their package
scriptlets, as text
* - ``t7005-bpm-publish``
- - ``bpm build`` and its refusal of a target below another member,
- ``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 signing-key rotation with explicit predecessor
- verification
+ - ``bpm build`` and its refusal of a target below another member
+ and 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
+ signing-key rotation with explicit predecessor verification
* - ``t7006-policy-labels``
- Beefile policy declarations are serialized into the signed
upatch section
diff --git a/Documentation/upatches/publish/beefile.rst b/Documentation/upatches/publish/beefile.rst
index 90037421..f38d5ff1 100644
--- a/Documentation/upatches/publish/beefile.rst
+++ b/Documentation/upatches/publish/beefile.rst
@@ -381,7 +381,7 @@ Limits and reserved names
* - member path including ``<name>-<version>/``
- 99 bytes, the limit of a ustar name
* - ``.bee`` file
- - 256 MiB per member file
+ - 256 MiB per member file, and for all members together
Every member path must be unique, and none may lie below another: a
name is a file or a directory, not both, so ``a`` and ``a/b`` do not go
@@ -389,7 +389,9 @@ into one package. ``Beefile``, ``README.md``, ``cert.der`` and
``build-environment.json`` are reserved for the members ``bpm build``
writes itself, and no member path lies below one of them either. A
member path longer than the ustar limit fails the build with ``bpm
-build: File name too long``.
+build: File name too long``, and members of more than 256 MiB together
+with ``bpm build: the members of the package are more than 256 MiB
+together, the most a target installs``: a target unpacks no more.
Example
-------
diff --git a/testing/functional/bpm/t7005-bpm-publish.sh b/testing/functional/bpm/t7005-bpm-publish.sh
index 6bd80d54..94303d05 100644
--- a/testing/functional/bpm/t7005-bpm-publish.sh
+++ b/testing/functional/bpm/t7005-bpm-publish.sh
@@ -141,6 +141,15 @@ for pair in "a a/b" "a/b a" "data.txt README.md/data.txt" "data.txt upatches"; d
n=$((n + 1))
done
+# Two members that each fit and together pass what a target unpacks.
+mkdir -p "$WORK/large" &&
+ ln -s "$REPO/vendor" "$WORK/large/vendor" &&
+ ln -s "$REPO/bpf" "$WORK/large/bpf" &&
+ cp "$WORK/pkg/t7005.bpf.c" "$WORK/large/" &&
+ truncate -s 134217729 "$WORK/large/one" "$WORK/large/two" &&
+ printf '[package]\nname = "t7005"\n\n[[files]]\nsource = "one"\n\n[[files]]\nsource = "two"\n\n[[upatches]]\nsource = "t7005.bpf.c"\n' \
+ >"$WORK/large/Beefile"
+
test_expect_success 'bpm build takes a SemVer version alone' '
for v in 1.0 01.0.0 1.0.0.1 1.0.0_rc; do
( cd "$WORK" && ! "$S" bpm build pkg --version $v \
@@ -168,6 +177,14 @@ test_expect_success 'bpm build refuses a target below another member' '
done
'
+test_expect_success 'bpm build refuses members a target could not unpack together' '
+ ( cd "$WORK" && ! "$S" bpm build large --version 9.2.0 \
+ --keys-dir "$WORK/keys" ) 2>"$WORK/large.err" &&
+ grep -q "more than 256 MiB together, the most a target installs" \
+ "$WORK/large.err" &&
+ ! test -e "$WORK/t7005-9.2.0.bee"
+'
+
PORT=$(python3 -c 'import socket; s = socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1])')
REG="$WORK/registry"
python3 ./helper/test-oci-registry.py --root "$REG" --port "$PORT" --writable \
diff --git a/testing/proofs/cbmc/bpm-tar-writer.c b/testing/proofs/cbmc/bpm-tar-writer.c
index 9baec451..c3a6817e 100644
--- a/testing/proofs/cbmc/bpm-tar-writer.c
+++ b/testing/proofs/cbmc/bpm-tar-writer.c
@@ -3,7 +3,8 @@
* Copyright (c) 2026 Authors of shirudo
*
* tar_entry() of tools/shirudo-build/bpm-build.c on every name of LEN
- * bytes, size and mode: a member is refused before a byte is written, or
+ * bytes, size and mode: a member is refused before a byte is written, as
+ * one is that takes the archive past what bpm_pkg_extract() unpacks, or
* its header is one block the readers of util/bpm_package.c take, with the
* same name, size and type, and the next header lies where they look.
*/
@@ -37,7 +38,6 @@
#define TAR_BLOCK 512
#define TAR_NAME 100
-#define TAR_SIZE_MAX 077777777777ULL
/* The last bytes of a name that are free where the checksum is proven. */
#define FREE 16
@@ -106,14 +106,17 @@ static void member_written(const char *name, bool checksum)
{
const size_t name_len = LEN;
struct bpm_tar tar;
+ __u64 before, pad;
mode_t mode;
size_t len;
- __u64 pad;
char type;
int ret;
/* build_archive() writes files, and one directory without content. */
__CPROVER_assume(type == '0' || (type == '5' && !len));
+ /* It starts from 0, and tar_entry() keeps the sum, as shown below. */
+ __CPROVER_assume(tar.total <= BPM_MAX_PACKAGE_BYTES);
+ before = tar.total;
member = name;
member_len = len;
member_mode = mode;
@@ -124,17 +127,17 @@ static void member_written(const char *name, bool checksum)
ret = tar_entry(&tar, name, NULL, len, mode, type);
if (checksum)
return;
- if (name_len >= TAR_NAME || len > TAR_SIZE_MAX) {
- __CPROVER_assert(ret && !headed,
- "refused before a byte is written");
+ if (name_len >= TAR_NAME || len > BPM_MAX_PACKAGE_BYTES - before) {
+ __CPROVER_assert(ret && !headed && tar.total == before,
+ "refused before a byte is written or counted");
+ __CPROVER_assert(name_len >= TAR_NAME || ret == -EFBIG,
+ "more than the extractor unpacks is named so");
return;
}
- __CPROVER_assert(headed, "the header written");
- /* One gzwrite() takes INT_MAX bytes; build_read_file() reads 256 MiB. */
- __CPROVER_assert(!ret == (len <= INT_MAX),
- "written whole, up to what one write takes");
- if (ret)
- return;
+ __CPROVER_assert(!ret && headed, "the header and the content written");
+ __CPROVER_assert(tar.total == before + len &&
+ tar.total <= BPM_MAX_PACKAGE_BYTES,
+ "counted, within what the extractor unpacks");
pad = (TAR_BLOCK - len % TAR_BLOCK) % TAR_BLOCK;
__CPROVER_assert(archived == TAR_BLOCK + len + pad,
"the next header where the readers look for it");
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index 4e94456f..2e0fe8e2 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -1443,6 +1443,7 @@ static int build_environment(const struct bpm_build_spec *spec,
struct bpm_tar {
gzFile gz;
+ __u64 total;
};
static int tar_write(struct bpm_tar *tar, const void *data, size_t len)
@@ -1467,8 +1468,15 @@ static int tar_entry(struct bpm_tar *tar, const char *name, const void *data,
__u32 sum = 0;
__u8 zeros[512] = {};
- if (strlen(name) >= 100 || (type == '0' && len > 077777777777ULL))
+ if (strlen(name) >= 100)
return -ENAMETOOLONG;
+ /*
+ * The sum the target's extractor takes: more would build and never
+ * install.
+ */
+ if (len > BPM_MAX_PACKAGE_BYTES - tar->total)
+ return -EFBIG;
+ tar->total += len;
memcpy(h, name, strlen(name));
if (tar_octal(h, 100, 8, mode & 07777) || tar_octal(h, 108, 8, 0) ||
tar_octal(h, 116, 8, 0) || tar_octal(h, 124, 12, len) ||
@@ -1508,14 +1516,14 @@ static int tar_path(struct bpm_tar *tar, const char *name, const char *path)
return ret;
}
-static int build_archive(const struct bpm_build_spec *spec, const char *version,
- const char *cert_path, const char *fingerprint,
- const char *out_dir)
+static int build_archive(const char *command, const struct bpm_build_spec *spec,
+ const char *version, const char *cert_path,
+ const char *fingerprint, const char *out_dir)
{
char output[PATH_MAX], top[PATH_MAX], name[PATH_MAX];
size_t environment_len = 0, cert_len = 0;
__u8 *environment = NULL, *cert = NULL;
- struct bpm_tar tar;
+ struct bpm_tar tar = {};
int ret;
if (strsprintf(top, sizeof(top), "%s-%s", spec->name, version) < 0 ||
@@ -1606,6 +1614,11 @@ static int build_archive(const struct bpm_build_spec *spec, const char *version,
ret = ret ? ret : -EIO;
xfree(environment, BPM_BUILD_MAX_JSON);
xfree(cert, cert_len + 1);
+ if (ret == -EFBIG) {
+ pr_err("%s: the members of the package are more than %u MiB together, the most a target installs",
+ command, BPM_MAX_PACKAGE_BYTES >> 20);
+ ret = -EINVAL;
+ }
if (ret)
unlink(output);
else
@@ -1800,7 +1813,8 @@ static int __build_bpm_build(int argc, char **argv,
ret = build_upatches(spec, pkey, cert_path, vmlinux_override,
shirudo_bpf_incdir, overrides, n_overrides, false);
if (!ret)
- ret = build_archive(spec, version, cert_path, fingerprint, out_dir);
+ ret = build_archive("bpm build", spec, version, cert_path,
+ fingerprint, out_dir);
build_stage_close(spec);
out_cert:
cert_free(&cert);
@@ -2242,7 +2256,8 @@ static int __build_bpm_config_update(int argc, char **argv,
pkey, cert_path);
}
if (!ret)
- ret = build_archive(spec, version, cert_path, fingerprint, out_dir);
+ ret = build_archive("bpm config update", spec, version,
+ cert_path, fingerprint, out_dir);
out_cert:
cert_free(&cert);
out: