eBPF-based security platform
bpm: read and check fixes in a Beefile's tree table
John Fastabendfc142fc40c7c
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 27d6aa6b..2bd90951 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -479,12 +479,13 @@ signed config image.- The repository update unit and timer and their packagescriptlets, as text* - ``t7005-bpm-publish``- - ``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 and of an entry no- target would take, each before anything is uploaded, A-to-B tree- signing-key rotation with explicit predecessor verification+ - ``bpm build``, the ``fixes`` lists it takes and refuses, 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 and of an entry no target would take,+ each before anything is uploaded, A-to-B tree signing-key rotation+ with explicit predecessor verification* - ``t7006-policy-labels``- Beefile policy declarations are serialized into the signedupatch sectiondiff --git a/Documentation/upatches/publish/beefile.rst b/Documentation/upatches/publish/beefile.rstindex 5309f92c..6e0b1b26 100644--- a/Documentation/upatches/publish/beefile.rst+++ b/Documentation/upatches/publish/beefile.rst@@ -41,6 +41,8 @@ A Beefile is a small subset of TOML, read line by line.``bpm build`` reports every error in the file, of syntax or of content,as ``bpm build: <file> is not a valid Beefile``, without a line number.+For a ``fixes`` it refuses, a ``Beefile: fixes ...`` line before that one+says what is wrong with the list.Keys----@@ -277,6 +279,23 @@ tree operation.- no- The member that a ``delete`` publishes. Default:``unload_target``.+ * - ``fixes``+ - array of strings+ - no+ - The IDs of the advisories the upatch fixes, such as+ ``fixes = ["EXAMPLE-0101"]``: 1 to 16 IDs, each once and of 1 to+ 255 bytes of letters, digits, ``.``, ``_``, ``:`` and ``-``. The+ key may appear once.++``fixes`` is a convention of the keepalive backend, which reads it to say+which nodes are exposed to an advisory. Nodes ignore it: it selects+nothing, and no target reads, checks or measures it. ``bpm build``+checks the form of each ID, not that the advisory exists, and refuses a+list that breaks the rules above, an empty list and a value that is not+an array of strings. It keeps the Beefile as written; the order of the+IDs does not matter, because the list is published in byte order.+Publishing it comes with ``bpm tree append``, which does not write it+yet.``[upatches.tree.selector]``~~~~~~~~~~~~~~~~~~~~~~~~~~~~@@ -371,6 +390,8 @@ Limits and reserved names- 16* - values per selector key- 16+ * - IDs in ``fixes``+ - 16, of 255 bytes each* - keys in each of ``kconfig`` and ``labels``- 64* - length of a string in a selector or commanddiff --git a/testing/functional/bpm/t7005-bpm-publish.sh b/testing/functional/bpm/t7005-bpm-publish.shindex ed1c347d..0c2933c6 100644--- a/testing/functional/bpm/t7005-bpm-publish.sh+++ b/testing/functional/bpm/t7005-bpm-publish.sh@@ -194,6 +194,63 @@ test_expect_success 'bpm build refuses members a target could not unpack togethedone'+# $1 is the package directory under $WORK, $2 the value of the key.+fixes_package() {+ mkdir -p "$WORK/$1" &&+ cp "$WORK/pkg/t7005.bpf.c" "$WORK/$1/" &&+ ln -s "$REPO/vendor" "$WORK/$1/vendor" &&+ ln -s "$REPO/bpf" "$WORK/$1/bpf" &&+ printf '[package]\nname = "t7005"\n\n[[upatches]]\nsource = "t7005.bpf.c"\n\n[upatches.tree]\nid = "t7005"\nfixes = %s\n' \+ "$2" >"$WORK/$1/Beefile"+}+# $1 is the case, $2 the value, $3 what the refusal says.+fixes_refused() {+ fixes_package "fixes/$1" "$2" &&+ ( cd "$WORK" && ! "$S" bpm build "fixes/$1" --version 8.0.0 \+ --keys-dir "$WORK/keys" ) 2>"$WORK/fixes/$1.err" &&+ grep -q "Beefile: $3" "$WORK/fixes/$1.err" &&+ grep -q "is not a valid Beefile" "$WORK/fixes/$1.err" &&+ ! test -e "$WORK/t7005-8.0.0.bee"+}+# An array of $1 IDs, and one ID of $1 bytes.+fixes_ids() {+ python3 -c 'import sys; print("[" + ", ".join("\"ID-%d\"" % i for i in range(int(sys.argv[1]))) + "]")' "$1"+}+fixes_long() {+ python3 -c 'import sys; print("[\"" + "A" * int(sys.argv[1]) + "\"]")' "$1"+}++test_expect_success 'bpm build takes fixes of 1 to 16 IDs of 1 to 255 bytes' '+ fixes_package fixes/ok "[\"EXAMPLE-0101\", \"a.B_c:9-\"]" &&+ ( cd "$WORK" && "$S" bpm build fixes/ok --version 8.1.0 \+ --keys-dir "$WORK/keys" ) >&2 &&+ tar -xzOf "$WORK/t7005-8.1.0.bee" t7005-8.1.0/Beefile |+ cmp - "$WORK/fixes/ok/Beefile" &&+ fixes_package fixes/max "$(fixes_ids 16)" &&+ ( cd "$WORK" && "$S" bpm build fixes/max --version 8.2.0 \+ --keys-dir "$WORK/keys" ) >&2 &&+ fixes_package fixes/long "$(fixes_long 255)" &&+ ( cd "$WORK" && "$S" bpm build fixes/long --version 8.3.0 \+ --keys-dir "$WORK/keys" ) >&2 &&+ test -s "$WORK/t7005-8.2.0.bee" && test -s "$WORK/t7005-8.3.0.bee"+'++test_expect_success 'bpm build refuses a malformed fixes' '+ fixes_refused many "$(fixes_ids 17)" "fixes holds more than 16 IDs" &&+ fixes_refused dup "[\"B\", \"A\", \"B\"]" "fixes holds B twice" &&+ fixes_refused empty-id "[\"A\", \"\"]" "fixes holds an empty ID" &&+ fixes_refused char "[\"A\", \"CVE 1\"]" "ID 2 of fixes has a character outside" &&+ fixes_refused slash "[\"a/b\"]" "ID 1 of fixes has a character outside" &&+ fixes_refused escape "[\"A\\n\"]" "ID 1 of fixes has a character outside" &&+ fixes_refused empty "[]" "fixes is not an array of 1 to 16 strings" &&+ fixes_refused string "\"EXAMPLE-0101\"" "fixes is not an array of 1 to 16 strings" &&+ fixes_refused number "[1]" "fixes is not an array of 1 to 16 strings" &&+ fixes_refused bare "7" "fixes is not an array of 1 to 16 strings" &&+ fixes_refused toolong "$(fixes_long 256)" "fixes is not an array of 1 to 16 strings" &&+ fixes_refused twice "[\"A\"]+fixes = [\"B\"]" "fixes is given twice"+'+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/fuzz/corpus/beefile/fixes b/testing/fuzz/corpus/beefile/fixesnew file mode 100644index 00000000..04cc4b4f--- /dev/null+++ b/testing/fuzz/corpus/beefile/fixes@@ -0,0 +1,26 @@+[package]+name = "fixes"++[[upatches]]+source = "xfs-fix.bpf.c"++[upatches.tree]+id = "xfs-fix"+fixes = ["EXAMPLE-0101"]++[upatches.tree.selector]+architecture = ["x86_64"]++[[upatches]]+source = "ext4-fix.bpf.c"++[upatches.tree]+fixes = ['GHSA-xxxx-0000-zzzz', "CVE-2026-0002", "vendor:advisory_7.1"]+id = "ext4-fix"++[[upatches]]+source = "net-fix.bpf.c"++[upatches.tree]+id = "net-fix"+fixes = ["EXAMPLE-0116", "EXAMPLE-0115", "EXAMPLE-0114", "EXAMPLE-0113", "EXAMPLE-0112", "EXAMPLE-0111", "EXAMPLE-0110", "EXAMPLE-0109", "EXAMPLE-0108", "EXAMPLE-0107", "EXAMPLE-0106", "EXAMPLE-0105", "EXAMPLE-0104", "EXAMPLE-0103", "EXAMPLE-0102", "EXAMPLE-0101"]diff --git a/testing/fuzz/fuzz-beefile.c b/testing/fuzz/fuzz-beefile.cindex b6bdb820..4634ab7b 100644--- a/testing/fuzz/fuzz-beefile.c+++ b/testing/fuzz/fuzz-beefile.c@@ -7,6 +7,8 @@*/#include <errno.h>+#include <stdlib.h>+#include <string.h>#include "fuzz.h"#include "malloc.h"@@ -73,13 +75,34 @@ int build_package_verify(const char *input, const char *cert_path)return -ENOSYS;}+#define FIX_ID_BYTES \+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._:-"++/* The publisher emits fixes as parsed: valid IDs in byte order, each once. */+static void check_fixes(const struct bpm_fixes *fixes)+{+ if (fixes->n < 0 || fixes->n > BPM_BUILD_MAX_FIXES)+ abort();+ for (int i = 0; i < fixes->n; i++) {+ const char *id = fixes->ids[i];++ if (!id[0] || id[strspn(id, FIX_ID_BYTES)])+ abort();+ if (i && strcmp(fixes->ids[i - 1], id) >= 0)+ abort();+ }+}+int LLVMFuzzerTestOneInput(const __u8 *data, size_t size){struct bpm_build_spec *spec = xcalloc(1, sizeof(*spec));if (!spec)return 0;- bpm_build_parse_beefile(data, size, spec);+ if (!bpm_build_parse_beefile(data, size, spec)) {+ for (size_t i = 0; i < spec->n_upatches; i++)+ check_fixes(&spec->upatches[i].tree.fixes);+ }bpm_build_spec_free(spec);return 0;}diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex dcafc42c..297b76c1 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -302,6 +302,62 @@ static int build_parse_selector_kv(const char *key, const char *value,return 0;}+static bool build_fix_id_ok(const char *id)+{+ return id[0] && !id[strspn(id, BPM_FIX_ID_CHARS)];+}++static int strings_compare(const void *left, const void *right)+{+ return strcmp(left, right);+}++void bpm_strings_sort(char strings[][TREE_STRING_MAX], int n)+{+ qsort(strings, (size_t)n, TREE_STRING_MAX, strings_compare);+}++static int build_parse_fixes(const char *value, struct bpm_fixes *fixes)+{+ int ret;++ if (fixes->n) {+ pr_err("Beefile: fixes is given twice in one [upatches.tree]");+ return -EINVAL;+ }+ ret = build_parse_string_array(value, fixes->ids, BPM_BUILD_MAX_FIXES,+ &fixes->n);+ if (ret == -E2BIG) {+ pr_err("Beefile: fixes holds more than %d IDs",+ BPM_BUILD_MAX_FIXES);+ return ret;+ }+ if (ret) {+ pr_err("Beefile: fixes is not an array of 1 to %d strings of at most %d bytes",+ BPM_BUILD_MAX_FIXES, TREE_STRING_MAX - 1);+ return ret;+ }+ for (int i = 0; i < fixes->n; i++) {+ if (!fixes->ids[i][0]) {+ pr_err("Beefile: fixes holds an empty ID");+ return -EINVAL;+ }+ if (!build_fix_id_ok(fixes->ids[i])) {+ pr_err("Beefile: ID %d of fixes has a character outside [A-Za-z0-9._:-]",+ i + 1);+ return -EINVAL;+ }+ }+ bpm_strings_sort(fixes->ids, fixes->n);+ for (int i = 1; i < fixes->n; i++) {+ if (!strcmp(fixes->ids[i - 1], fixes->ids[i])) {+ pr_err("Beefile: fixes holds %s twice", fixes->ids[i]);+ return -EINVAL;+ }+ }+ return 0;+}+static bool build_name_ok(const char *s){const __u8 *p = (const __u8 *)s;@@ -604,6 +660,10 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)} else if (!strcmp(key, "unload_artifact")) {dst = t->unload_artifact;cap = sizeof(t->unload_artifact);+ } else if (!strcmp(key, "fixes")) {+ if (build_parse_fixes(value, &t->fixes))+ return -EINVAL;+ continue;} else {continue;}diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.cindex 11aa11d5..0db7c7eb 100644--- a/tools/shirudo-build/bpm-publish.c+++ b/tools/shirudo-build/bpm-publish.c@@ -528,26 +528,16 @@ out:return ret;}-static int tree_string_compare(const void *left, const void *right)-{- return strcmp(left, right);-}-/* Selector lists are sets: sort them so equal Beefiles digest equally. */static void tree_selector_canonicalize(struct tree_selector *s){- qsort(s->kernel_release_prefix, (size_t)s->n_kernel_release_prefix,- TREE_STRING_MAX, tree_string_compare);- qsort(s->kernel_build_id, (size_t)s->n_kernel_build_id,- TREE_STRING_MAX, tree_string_compare);- qsort(s->architecture, (size_t)s->n_architecture,- TREE_STRING_MAX, tree_string_compare);+ bpm_strings_sort(s->kernel_release_prefix, s->n_kernel_release_prefix);+ bpm_strings_sort(s->kernel_build_id, s->n_kernel_build_id);+ bpm_strings_sort(s->architecture, s->n_architecture);for (int i = 0; i < s->n_kconfig; i++)- qsort(s->kconfig[i].values, (size_t)s->kconfig[i].n_values,- TREE_STRING_MAX, tree_string_compare);+ bpm_strings_sort(s->kconfig[i].values, s->kconfig[i].n_values);for (int i = 0; i < s->n_labels; i++)- qsort(s->labels[i].values, (size_t)s->labels[i].n_values,- TREE_STRING_MAX, tree_string_compare);+ bpm_strings_sort(s->labels[i].values, s->labels[i].n_values);}/* An artifact that is described for its operation and not uploaded yet. */diff --git a/tools/shirudo-build/bpm.h b/tools/shirudo-build/bpm.hindex 53a6862e..f0efc315 100644--- a/tools/shirudo-build/bpm.h+++ b/tools/shirudo-build/bpm.h@@ -22,6 +22,9 @@#define BPM_BUILD_MAX_RODATA 16#define BPM_BUILD_MAX_COMMAND_ARGS 16#define BPM_BUILD_MAX_POLICIES 64+#define BPM_BUILD_MAX_FIXES 16+#define BPM_FIX_ID_CHARS \+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._:-"struct bpm_build_policy {char id[UPATCH_POLICY_NAME_MAX];@@ -49,6 +52,11 @@ struct bpm_build_file {char target[PATH_MAX];};+struct bpm_fixes {+ char ids[BPM_BUILD_MAX_FIXES][TREE_STRING_MAX];+ int n;+};+/** Optional [upatches.tree] table. Publication metadata read by `bpm tree* append` on the operator; the target consumes the signed publication and@@ -60,6 +68,7 @@ struct bpm_build_tree {char id[UPATCH_NAME_MAX];char load_artifact[PATH_MAX];char unload_artifact[PATH_MAX];+ struct bpm_fixes fixes;struct tree_selector *selector;};@@ -99,6 +108,7 @@ struct bpm_build_spec {int bpm_build_parse_beefile(const __u8 *body, size_t len,struct bpm_build_spec *spec);void bpm_build_spec_free(struct bpm_build_spec *spec);+void bpm_strings_sort(char strings[][TREE_STRING_MAX], int n);int build_bpm(int argc, char **argv);int build_bpm_push(int argc, char **argv);