Skip to content

eBPF-based security platform

bpm: hold archive paths to one rule on both sides

Daniel Borkmann38ba464e43ae
diff --git a/Documentation/reference/formats/bpm-formats.rst b/Documentation/reference/formats/bpm-formats.rst
index f7e8f3ac..c7989ed7 100644
--- a/Documentation/reference/formats/bpm-formats.rst
+++ b/Documentation/reference/formats/bpm-formats.rst
@@ -146,7 +146,9 @@ The target refuses an archive that breaks any of these:
that is NUL-terminated within its 100 bytes, an empty prefix field and a
size field of octal digits.
- Every member lies under the one top-level directory. No path is
- absolute or has an empty or ``..`` component.
+ absolute or has an empty, ``.`` or ``..`` component, so an archive
+ rooted at ``./`` is refused, as the member readers would find nothing
+ in it.
- Every member is a regular file or a directory.
- At most 4096 members and 256 MiB of content.
- No regular file appears twice.
diff --git a/testing/functional/bpm/t7005-bpm-publish.sh b/testing/functional/bpm/t7005-bpm-publish.sh
index aff25d82..8240e7e4 100644
--- a/testing/functional/bpm/t7005-bpm-publish.sh
+++ b/testing/functional/bpm/t7005-bpm-publish.sh
@@ -114,8 +114,19 @@ printf '[upatches.tree]\nid = "t7005"\n\n[upatches .tree.selector]\narchitecture
printf '[upatches.tree]\nid = "t7005"\n\n["upatches".tree.selector]\narchitecture = ["aarch64"]\n' \
>>"$WORK/spelled/4/Beefile"
-test_expect_success 'bpm build refuses a tree selector spelled another way' '
- for n in 1 2 3 4; do
+# Targets the archive reader refuses or reads as another member.
+n=5
+for target in ./Beefile a/./data.txt dir/; do
+ mkdir -p "$WORK/spelled/$n" &&
+ cp "$WORK/pkg/t7005.bpf.c" "$WORK/spelled/$n/" &&
+ echo data >"$WORK/spelled/$n/data.txt" &&
+ printf '[package]\nname = "t7005"\n\n[[files]]\nsource = "data.txt"\ntarget = "%s"\n\n[[upatches]]\nsource = "t7005.bpf.c"\n' \
+ "$target" >"$WORK/spelled/$n/Beefile"
+ n=$((n + 1))
+done
+
+test_expect_success 'bpm build refuses a tree selector or a target spelled another way' '
+ for n in 1 2 3 4 5 6 7; do
( cd "$WORK" && ! "$S" bpm build spelled/$n --version 9.0.$n \
--keys-dir "$WORK/keys" ) 2>"$WORK/spelled/$n.err" &&
grep -q "is not a valid Beefile" "$WORK/spelled/$n.err" &&
diff --git a/testing/unit/bpm_package.c b/testing/unit/bpm_package.c
index 01567f49..bf1f7fd1 100644
--- a/testing/unit/bpm_package.c
+++ b/testing/unit/bpm_package.c
@@ -57,8 +57,27 @@ static void index_entry_needs_its_package_size(void)
-EINVAL);
}
+/*
+ * One rule for every member: an archive rooted at ./ unpacked, while
+ * the member readers, which want <name>-<version>/<member>, found none.
+ */
+static void archive_path_has_no_dot_component(void)
+{
+ EXPECT_TRUE(bpm_archive_rel_ok("t-1.0.0/"));
+ EXPECT_TRUE(bpm_archive_rel_ok("t-1.0.0/Beefile"));
+ EXPECT_TRUE(bpm_archive_rel_ok("t-1.0.0/a/.b"));
+ EXPECT_FALSE(bpm_archive_rel_ok(""));
+ EXPECT_FALSE(bpm_archive_rel_ok("/t-1.0.0/Beefile"));
+ EXPECT_FALSE(bpm_archive_rel_ok("./"));
+ EXPECT_FALSE(bpm_archive_rel_ok("./Beefile"));
+ EXPECT_FALSE(bpm_archive_rel_ok("t-1.0.0/./Beefile"));
+ EXPECT_FALSE(bpm_archive_rel_ok("t-1.0.0/../Beefile"));
+ EXPECT_FALSE(bpm_archive_rel_ok("t-1.0.0//Beefile"));
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(index_entry_needs_its_package_size),
+ UNIT_CASE(archive_path_has_no_dot_component),
{}
};
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index c2ab75ad..2f0e478e 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -958,14 +958,19 @@ static int build_target_ok(const char *target)
{
const char *p = target;
- if (!target[0] || target[0] == '/' || !strcmp(target, ".") ||
- !strcmp(target, ".."))
+ if (!target[0] || target[0] == '/')
return -EINVAL;
+ /*
+ * As the target's archive reader takes a path, or the package
+ * would build and never install, or read one way and install
+ * another: ./Beefile is the reserved Beefile, and a/./b is a/b.
+ */
while (*p) {
const char *q = strchr(p, '/');
size_t n = q ? (size_t)(q - p) : strlen(p);
- if (!n || (n == 2 && !strncmp(p, "..", 2)))
+ if (!n || (n == 1 && *p == '.') ||
+ (n == 2 && !strncmp(p, "..", 2)) || (q && !q[1]))
return -EINVAL;
p = q ? q + 1 : p + n;
}
diff --git a/util/bpm_package.c b/util/bpm_package.c
index eccef66e..60dac27f 100644
--- a/util/bpm_package.c
+++ b/util/bpm_package.c
@@ -261,7 +261,8 @@ bool bpm_archive_rel_ok(const char *name)
q = strchr(p, '/');
if (!q)
q = p + strlen(p);
- if (q == p || (q - p == 2 && !strncmp(p, "..", 2)))
+ if (q == p || (q - p == 1 && *p == '.') ||
+ (q - p == 2 && !strncmp(p, "..", 2)))
return false;
p = *q ? q + 1 : q;
}