Skip to content

eBPF-based security platform

bpm: build packages with SemVer versions alone

Daniel Borkmann975bda134821
diff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rst
index a63cc671..e5854a75 100644
--- a/Documentation/reference/cli/bpm.rst
+++ b/Documentation/reference/cli/bpm.rst
@@ -151,7 +151,7 @@ the Beefile's ``update`` command for each input to produce the new
``.rodata`` (``--rodata-input`` replaces an input file), signs the upatch
again and signs a new unload directive for it. The base package must be
signed by the same key and declare ``.rodata`` inputs, and ``--version``
-must be greater than its version. Without ``--file``, the Beefile and the
+must be a SemVer 2.0.0 version greater than its version. Without ``--file``, the Beefile and the
commands it names come from the base package and run on the operator
host: take base packages only from sources you trust. The output is
``<name>-<version>.bee`` in the current directory. Options take their
@@ -286,6 +286,8 @@ build
- ``--rodata-input <name>=<path>`` replaces a ``.rodata`` input file of
the Beefile. ``<name>`` is the file's base name up to the first dot
(``watch`` for ``watch.list``). It is repeatable, once per name.
+- ``--version`` is a SemVer 2.0.0 version, such as ``1.2.0`` or
+ ``1.2.0-rc.1``.
- Every upatch is compiled in the Beefile's directory and signed.
- The output is ``<name>-<version>.bee`` in the current directory, and
``build`` prints its path.
diff --git a/Documentation/reference/formats/bpm-formats.rst b/Documentation/reference/formats/bpm-formats.rst
index 927ca764..5acd3558 100644
--- a/Documentation/reference/formats/bpm-formats.rst
+++ b/Documentation/reference/formats/bpm-formats.rst
@@ -80,13 +80,13 @@ Names
128 characters.
* - Package version
- ``bpm build --version``
- - Starts with a letter or digit, then ``[A-Za-z0-9._+-]``, fewer
- than 128 characters. The target also accepts a version that
- starts with another character than ``.``. The newest version is
- chosen by SemVer 2.0.0 precedence, build metadata ignored. A
- version that is not SemVer 2.0.0, such as ``1.2`` or
- ``01.0.0``, is older than every one that is, and such versions
- order by their bytes.
+ - A SemVer 2.0.0 version such as ``1.2.0``, ``1.2.0-rc.1`` or
+ ``1.2.0+build.5``, fewer than 128 characters. The newest version is
+ chosen by SemVer 2.0.0 precedence, build metadata ignored. The
+ target also accepts other versions of ``[A-Za-z0-9._+-]`` that do
+ not start with ``.``, from packages built before this rule: such
+ a version is older than every SemVer one, and they order by
+ their bytes.
* - upatch name
- The source file's basename without ``.bpf.c``, ``.bpf.o`` or ``.o``
- The Beefile cannot override it. 1 to 255 characters of
diff --git a/testing/functional/bpm/t7005-bpm-publish.sh b/testing/functional/bpm/t7005-bpm-publish.sh
index 8240e7e4..ad8355ab 100644
--- a/testing/functional/bpm/t7005-bpm-publish.sh
+++ b/testing/functional/bpm/t7005-bpm-publish.sh
@@ -125,6 +125,15 @@ for target in ./Beefile a/./data.txt dir/; do
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 \
+ --keys-dir "$WORK/keys" ) 2>"$WORK/version.err" &&
+ grep -q "is not a SemVer 2.0.0 version" "$WORK/version.err" &&
+ ! test -e "$WORK/t7005-$v.bee" || return 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 \
diff --git a/testing/unit/bpm_package.c b/testing/unit/bpm_package.c
index b7ba6921..d689552e 100644
--- a/testing/unit/bpm_package.c
+++ b/testing/unit/bpm_package.c
@@ -130,8 +130,28 @@ static void version_order_is_total(void)
}
}
+static void semver_valid_is_semver_alone(void)
+{
+ static const char *const good[] = {
+ "0.0.0", "1.2.3", "1.0.0-rc.1", "1.0.0-0.3.7", "1.0.0-x-y.z",
+ "1.0.0+build.5", "1.0.0-alpha+001", "18446744073709551615.0.0",
+ };
+ static const char *const bad[] = {
+ "", "1", "1.2", "1.2.3.4", "01.0.0", "1.02.0", "1.0.0foo",
+ "1.0.0-", "1.0.0-rc..1", "1.0.0-rc.01", "1.0.0+", "1.0.0+a..b",
+ "1.0.0_rc", "v1.0.0", "1.0.0-rc.1+b_1", "18446744073709551616.0.0",
+ };
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(good); i++)
+ EXPECT_TRUE(bpm_semver_valid(good[i]));
+ for (i = 0; i < ARRAY_SIZE(bad); i++)
+ EXPECT_FALSE(bpm_semver_valid(bad[i]));
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(index_entry_needs_its_package_size),
+ UNIT_CASE(semver_valid_is_semver_alone),
UNIT_CASE(semver_follows_its_precedence),
UNIT_CASE(version_order_is_total),
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 2f0e478e..710e8d4b 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -311,17 +311,10 @@ static bool build_name_ok(const char *s)
return true;
}
+/* A version the target orders as the operator means it. */
static bool build_version_ok(const char *s)
{
- const __u8 *p = (const __u8 *)s;
-
- if (!s || !*s || !isalnum((__u8)s[0]) || strlen(s) >= 128)
- return false;
- for (; *p; p++) {
- if (!(isalnum(*p) || *p == '.' || *p == '_' || *p == '-' || *p == '+'))
- return false;
- }
- return true;
+ return s && strlen(s) < 128 && bpm_semver_valid(s);
}
static int build_default_targets(struct bpm_build_upatch *u)
@@ -1705,10 +1698,15 @@ static int __build_bpm_build(int argc, char **argv,
goto usage;
}
}
- if (!version || !build_version_ok(version)) {
+ if (!version) {
pr_err("bpm build: --version <version> is required");
goto usage;
}
+ if (!build_version_ok(version)) {
+ pr_err("bpm build: --version %s is not a SemVer 2.0.0 version such as 1.2.0, 1.2.0-rc.1 or 1.2.0+build.5",
+ version);
+ return -EINVAL;
+ }
if (!build_resolve_keys_dir(keys_arg, keys_dir, sizeof(keys_dir)) ||
!realpath(keys_dir, keys_abs)) {
pr_err("bpm build: keys directory %s is not usable", keys_dir);
@@ -2008,8 +2006,13 @@ static int __build_bpm_config_update(int argc, char **argv,
goto usage;
}
}
- if (!base_arg || !version || !build_version_ok(version))
+ if (!base_arg || !version)
goto usage;
+ if (!build_version_ok(version)) {
+ pr_err("bpm config update: --version %s is not a SemVer 2.0.0 version such as 1.2.0, 1.2.0-rc.1 or 1.2.0+build.5",
+ version);
+ return -EINVAL;
+ }
if (!strncmp(base_arg, "oci://", 6)) {
ret = build_download_package(base_arg, base_package_tmp);
if (ret)
diff --git a/util/bpm_package.c b/util/bpm_package.c
index 364d6865..8c2e3cd9 100644
--- a/util/bpm_package.c
+++ b/util/bpm_package.c
@@ -221,6 +221,13 @@ static int prerelease_compare(const char *a, size_t la, const char *b,
return la ? 1 : lb ? -1 : 0;
}
+bool bpm_semver_valid(const char *s)
+{
+ struct semver v;
+
+ return s && semver_parse(s, &v);
+}
+
/*
* SemVer 2.0.0 precedence, build metadata ignored. A version it does not
* describe sorts below every one it does, and such versions sort among
diff --git a/util/bpm_package.h b/util/bpm_package.h
index 32aee94c..b7c57fa6 100644
--- a/util/bpm_package.h
+++ b/util/bpm_package.h
@@ -38,6 +38,8 @@ int bpm_index_parse(const __u8 *body, size_t len,
struct bpm_index_entry *entries, int *n_entries);
/* The order of two package versions: below 0, 0 or above 0. */
int bpm_semver_compare(const char *a, const char *b);
+/* Whether s is a SemVer 2.0.0 version, as bpm build takes alone. */
+bool bpm_semver_valid(const char *s);
int bpm_verify_cms(const __u8 *payload, size_t payload_len,
const __u8 *sig, size_t sig_len,
const char *cert_path);