Skip to content

eBPF-based security platform

bpm: refuse a Beefile target below another member

Daniel Borkmann076282bbf482
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 157f53bc..74dcb041 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -298,8 +298,8 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- Targets of 0 to 8 bytes; up to 4 bytes into a table that holds one
target
- Takes exactly the paths the archive reader takes that have no
- final slash; a target joins the table once, and a refused one is
- not counted
+ final slash; a target joins the table once, none above or below
+ one that is in it, and a refused one is not counted
* - ``bpm-beefile-members``
- ``build_entry_name()``, against ``package_member_name_ok()`` and
``bpm_archive_rel_ok()``
@@ -349,7 +349,8 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
``[[upatches.policy]]`` are counted below 128, 128, 16 and 64 and
refused there; a table without its upatch is refused and counted
nowhere; ``Beefile``, ``README.md``, ``cert.der`` and
- ``build-environment.json`` are no targets
+ ``build-environment.json`` are no targets, nor is a path below
+ one of them
* - ``bpm-index-writer``
- ``push_index()`` of :src:`tools/shirudo-build/bpm-publish.c`,
against ``bpm_index_parse()``
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 46551321..911501ad 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -479,10 +479,11 @@ signed config image.
- The repository update unit and timer and their package
scriptlets, as text
* - ``t7005-bpm-publish``
- - ``bpm build``, ``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,
+ ``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 cc93afa2..90037421 100644
--- a/Documentation/upatches/publish/beefile.rst
+++ b/Documentation/upatches/publish/beefile.rst
@@ -383,10 +383,13 @@ Limits and reserved names
* - ``.bee`` file
- 256 MiB per member file
-Every member path must be unique. ``Beefile``, ``README.md``,
-``cert.der`` and ``build-environment.json`` are reserved for the members
-``bpm build`` writes itself. A member path longer than the ustar limit
-fails the build with ``bpm build: File name too long``.
+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
+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``.
Example
-------
diff --git a/testing/functional/bpm/t7005-bpm-publish.sh b/testing/functional/bpm/t7005-bpm-publish.sh
index 80496fa0..6bd80d54 100644
--- a/testing/functional/bpm/t7005-bpm-publish.sh
+++ b/testing/functional/bpm/t7005-bpm-publish.sh
@@ -125,6 +125,22 @@ for target in ./Beefile a/./data.txt dir/; do
n=$((n + 1))
done
+# A name that would be a file and a directory at once, in either order,
+# below a member bpm build writes itself, and above the load artifact.
+n=1
+for pair in "a a/b" "a/b a" "data.txt README.md/data.txt" "data.txt upatches"; do
+ mkdir -p "$WORK/nested/$n" &&
+ cp "$WORK/pkg/t7005.bpf.c" "$WORK/nested/$n/" &&
+ echo data >"$WORK/nested/$n/data.txt" &&
+ printf '[package]\nname = "t7005"\n' >"$WORK/nested/$n/Beefile" &&
+ for target in $pair; do
+ printf '\n[[files]]\nsource = "data.txt"\ntarget = "%s"\n' \
+ "$target" >>"$WORK/nested/$n/Beefile"
+ done &&
+ printf '\n[[upatches]]\nsource = "t7005.bpf.c"\n' >>"$WORK/nested/$n/Beefile"
+ n=$((n + 1))
+done
+
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 \
@@ -143,6 +159,15 @@ test_expect_success 'bpm build refuses a tree selector or a target spelled anoth
done
'
+test_expect_success 'bpm build refuses a target below another member' '
+ for n in 1 2 3 4; do
+ ( cd "$WORK" && ! "$S" bpm build nested/$n --version 9.1.$n \
+ --keys-dir "$WORK/keys" ) 2>"$WORK/nested/$n.err" &&
+ grep -q "is not a valid Beefile" "$WORK/nested/$n.err" &&
+ ! test -e "$WORK/t7005-9.1.$n.bee" || return 1
+ done
+'
+
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-beefile-tables.c b/testing/proofs/cbmc/bpm-beefile-tables.c
index 086a12b2..befb93d2 100644
--- a/testing/proofs/cbmc/bpm-beefile-tables.c
+++ b/testing/proofs/cbmc/bpm-beefile-tables.c
@@ -6,7 +6,7 @@
* of tables up to its limit: one more [[files]], [[upatches]], rodata or
* policy table is counted below the limit and refused at it, and a
* sub-table without its [[upatches]] or a target that is one of bpm
- * build's own members is refused. LEN picks the case.
+ * build's own members, or lies below one, is refused. LEN picks the case.
*/
/* As proof.h, before the system headers can include <ctype.h>. */
@@ -104,12 +104,17 @@ static void own_members_refused(void)
static const char *const own[] = {
"Beefile", "README.md", "cert.der", "build-environment.json",
};
+ static const char *const below[] = {
+ "Beefile/a", "README.md/a", "cert.der/a",
+ "build-environment.json/a",
+ };
char seen[1][PATH_MAX];
size_t n = 0, i;
for (i = 0; i < ARRAY_SIZE(own); i++)
__CPROVER_assert(build_add_target(seen, &n, own[i]) == -EEXIST &&
- !n, "no target is one of bpm build's own");
+ build_add_target(seen, &n, below[i]) == -EEXIST &&
+ !n, "no target is one of bpm build's own, or below one");
}
void harness(void)
diff --git a/testing/proofs/cbmc/bpm-beefile-targets.c b/testing/proofs/cbmc/bpm-beefile-targets.c
index 2468590c..0c4d6102 100644
--- a/testing/proofs/cbmc/bpm-beefile-targets.c
+++ b/testing/proofs/cbmc/bpm-beefile-targets.c
@@ -4,8 +4,9 @@
*
* The member paths bpm build takes from a Beefile, for every target of LEN
* bytes: tools/shirudo-build/bpm-build.c takes exactly the file paths the
- * archive reader of util/bpm_package.c takes, and each of them once;
- * bpm-beefile-tables has the members bpm build writes itself.
+ * archive reader of util/bpm_package.c takes, each of them once and none
+ * above or below one it took, which the extractor could not unpack beside
+ * it; bpm-beefile-tables has the members bpm build writes itself.
*/
/* As proof.h, before the system headers can include <ctype.h>. */
@@ -58,12 +59,20 @@ static bool target_taken(const char *target)
return taken;
}
+/* Whether path is dir, a slash and more: a file where dir is one. */
+static bool below(const char *dir, const char *path)
+{
+ size_t n = strlen(dir);
+
+ return strlen(path) > n && !memcmp(path, dir, n) && path[n] == '/';
+}
+
/* A target joins the one seen before it, any string or none. */
static void target_added(const char *target, bool taken)
{
char seen[2][PATH_MAX] = {};
size_t n = 1, i, j;
- bool twice;
+ bool twice, nested;
int ret;
for (j = 0; j <= LEN; j++) {
@@ -73,8 +82,10 @@ static void target_added(const char *target, bool taken)
}
__CPROVER_assume(i <= LEN);
twice = !strcmp(seen[0], target);
+ nested = below(seen[0], target) || below(target, seen[0]);
ret = build_add_target(seen, &n, target);
- __CPROVER_assert(!ret == (taken && !twice), "a path taken once");
+ __CPROVER_assert(!ret == (taken && !twice && !nested),
+ "a path taken once, none above or below one taken");
if (ret) {
__CPROVER_assert(n == 1, "a refused target is not counted");
return;
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 2d561e80..e1e11b07 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -115,7 +115,7 @@ bpm-beefile-lines --unwind 14 LEN=0-10
bpm-beefile-tables --unwind 40 LEN=0-7
bpm-beefile-names --unwind 131
bpm-beefile-version --unwind 13 LEN=5-9
-bpm-beefile-targets --unwind 11 LEN=0-8
+bpm-beefile-targets --unwind 11 --unwindset strlen.0:24,strncmp.0:24 LEN=0-8
bpm-beefile-members --unwind 11 LEN=0-6
bpm-index-writer --unwind 26 --unwindset strlen.0:64,strnlen.0:64,memcpy.0:64,put_string_n.1:64 --max-field-sensitivity-array-size 512 LEN=0-3
bpm-index-append --unwind 26 --unwindset strlen.0:260,strcmp.0:64,strnlen.0:64,memcpy.0:64,put_string_n.1:64 --max-field-sensitivity-array-size 512 LEN=0-2
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index 87f4a7d6..4e94456f 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -971,15 +971,34 @@ static int build_target_ok(const char *target)
return 0;
}
+static bool build_target_below(const char *dir, const char *path)
+{
+ size_t n = strlen(dir);
+
+ return !strncmp(path, dir, n) && path[n] == '/';
+}
+
+/*
+ * A name is a file or a directory, not both: a target below another, or
+ * below a member bpm build writes itself, would build and never install.
+ */
static int build_add_target(char seen[][PATH_MAX], size_t *n, const char *target)
{
+ static const char *const own[] = {
+ "Beefile", "README.md", "cert.der", "build-environment.json",
+ };
+
if (build_target_ok(target))
return -EINVAL;
- if (!strcmp(target, "Beefile") || !strcmp(target, "README.md") ||
- !strcmp(target, "cert.der") || !strcmp(target, "build-environment.json"))
- return -EEXIST;
+ for (size_t i = 0; i < ARRAY_SIZE(own); i++) {
+ if (!strcmp(target, own[i]) ||
+ build_target_below(own[i], target))
+ return -EEXIST;
+ }
for (size_t i = 0; i < *n; i++) {
- if (!strcmp(seen[i], target))
+ if (!strcmp(seen[i], target) ||
+ build_target_below(seen[i], target) ||
+ build_target_below(target, seen[i]))
return -EEXIST;
}
if (*n >= BPM_BUILD_MAX_ENTRIES * 3)