eBPF-based security platform
bpm: refuse a tree add that shares an XDP device class
John Fastabendde7a60d75652
diff --git a/.agents/skills/upatch/SKILL.md b/.agents/skills/upatch/SKILL.mdindex 3f321230..04fec2b3 100644--- a/.agents/skills/upatch/SKILL.md+++ b/.agents/skills/upatch/SKILL.md@@ -140,6 +140,9 @@ shirudo bpm tree append <name>-<version>.bee oci://<registry>/<repository> [<app`BPM_OCI_SCHEME=http` is for a registry on the loopback only. `treeappend` adds a load or delete operation to a signed upatch tree thattargets sync; `Documentation/reference/cli/bpm.rst` lists its options.+It refuses an `add` whose XDP programs select a device class another+upatch of the tree selects on the same nodes: give each class to one+upatch, or the two entries selectors that exclude each other.## Load it on a targetdiff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex e7838464..420804d5 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -465,7 +465,8 @@ signed config image.scriptlets, as text* - ``t7005-bpm-publish``- ``bpm build``, ``bpm push`` with a signed index, ``bpm tree- append``, A-to-B tree signing-key rotation with explicit+ append`` and its refusal of an XDP device class two upatches of+ a tree select, A-to-B tree signing-key rotation with explicitpredecessor verification* - ``t7006-policy-labels``- Beefile policy declarations are serialized into the signed@@ -549,9 +550,10 @@ signed config image.Unit suites and fuzz targets~~~~~~~~~~~~~~~~~~~~~~~~~~~~-The unit suites in :src:`testing/unit/` cover 25 of the modules in+The unit suites in :src:`testing/unit/` cover 26 of the modules in:src:`util/`: ``attest``, ``bpm_oci``, ``bpm_package``, ``bpm_state``,-``bpm_tree``, ``bpm_tree_state``, ``chain``, ``conditions``,+``bpm_tree``, ``bpm_tree_overlap``, ``bpm_tree_state``, ``chain``,+``conditions``,``eventlog``, ``format``, ``hex``, ``io``, ``json``, ``kernel_abi``,``kernel_build_id``, ``kernel_release``, ``measured``, ``paths``,``string``, ``upatch``, ``upatch_decode``, ``upatch_pins``,@@ -696,8 +698,9 @@ Coverage by area* - upatch tree- ``t3011``, ``t5004``, ``t7005``, ``t7101``, ``t7102``,``t7103``, ``t7104``, ``t7105``, ``t9105``. Fuzz: ``tree``,- ``tree-state``, ``kconfig``. Unit: ``bpm_tree``, ``bpm_tree_state``,- ``kernel_build_id``, ``kernel_release``+ ``tree-state``, ``kconfig``. Unit: ``bpm_tree``,+ ``bpm_tree_overlap``, ``bpm_tree_state``, ``kernel_build_id``,+ ``kernel_release``* - Policy labels- upatch labels and Beefile serialization: ``t3109``, ``t7006``.Unauthorized policy-xattr changes: ``t6111``. Task and socketdiff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rstindex d45a686d..b26f538d 100644--- a/Documentation/reference/cli/bpm.rst+++ b/Documentation/reference/cli/bpm.rst@@ -226,6 +226,26 @@ package as the next operations of the signed tree at- ``add``- ``add`` or ``delete``.+An ``add`` is refused when an XDP program of the upatch selects a device+class that an XDP program of another upatch in the tree selects, and the+selectors of the two operations do not exclude each other. A device runs+one native XDP program, so the second upatch would fail its load on every+node with such a device and ``tree sync`` would stop there. The message+names both upatches, the sequence number of the earlier operation and the+shared classes, and nothing is published.++- Only an ``add`` that no later ``delete`` withdrew counts. A ``delete``+ withdraws it when its selector reaches every node the ``add`` reaches:+ it is empty, or it constrains nothing the ``add`` leaves open and+ allows every value the ``add`` allows.+- Two selectors exclude each other when both name architectures, kernel+ build IDs or kernel release prefixes and have none in common, or when+ both name the same label or kernel option and share no value for it.+ Selectors that name different labels do not: one node can carry both.+- The classes of the earlier upatches are read from their artifacts in+ the registry, so the append fails when one of them cannot be fetched.+ Two upatches of the package being appended are compared as well.+key---diff --git a/Documentation/upatches/publish/publishing.rst b/Documentation/upatches/publish/publishing.rstindex cb511d6d..26beabde 100644--- a/Documentation/upatches/publish/publishing.rst+++ b/Documentation/upatches/publish/publishing.rst@@ -140,6 +140,14 @@ Append it to the tree``add`` of the new one after it, since a target cannot load a secondupatch under a name that is already loaded.+``tree append`` refuses an ``add`` whose XDP programs select a device+class that another upatch of the tree selects on the same nodes, since a+device runs one native XDP program and the second load would stop every+such node's ``tree sync``. Give each class to one upatch, give the two+entries selectors that exclude each other, such as different values of+one label, or append the ``delete`` of the earlier upatch first.+:doc:`/reference/cli/bpm` has the exact rule.+What a target does with them----------------------------diff --git a/Documentation/upatches/write/attach-kinds.rst b/Documentation/upatches/write/attach-kinds.rstindex 7e7e48f4..417343ae 100644--- a/Documentation/upatches/write/attach-kinds.rst+++ b/Documentation/upatches/write/attach-kinds.rst@@ -254,8 +254,10 @@ mask is mandatory, nonzero and contains only the bits below. BareA device runs one native XDP program at a time, so the XDP programs ofone upatch must select disjoint classes: ``shirudo build`` refuses-``SEC("xdp/3")`` beside ``SEC("xdp/2")``. Two upatches can still select-the same device. The second one's ``upatch load`` then fails, at boot+``SEC("xdp/3")`` beside ``SEC("xdp/2")``. ``bpm tree append`` refuses an+upatch that selects a class another upatch of the tree selects on the+same nodes. Two upatches loaded another way can still select the same+device. The second one's ``upatch load`` then fails, at bootrestore it is restored without that device, and ``status`` lists thedevice under ``XdpCovered`` as ``another XDP program attached``.diff --git a/Makefile.shirudo b/Makefile.shirudoindex d6d4a981..f00a006f 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -242,6 +242,7 @@ UTIL_SRCS := \util/hex.c \util/string.c \util/bpm_tree.c \+ util/bpm_tree_overlap.c \util/bpm_tree_state.c \util/bpm_oci.c \util/curl.c \@@ -494,7 +495,8 @@ tools/shirudo-build/shirudo-build: $(BUILD_OBJS) util/sign.o util/config_image.outil/crypto_openssl.o util/tls_openssl.o \util/bpm_oci.o util/curl.o util/bpm_package.o \util/upatch_rodata.o \- util/bpm_tree.o $(VERSION_OBJ) \+ util/bpm_tree.o util/bpm_tree_overlap.o \+ util/kernel_release.o $(VERSION_OBJ) \$(LIBBPF_A)@printf ' LD $@\n'$(Q)$(LD) $(CFLAGS) $(HARDEN_LDFLAGS) $(LDFLAGS) $(RPATH_LDFLAGS) -o $@ $^ \diff --git a/testing/functional/bpm/t7005-bpm-publish.sh b/testing/functional/bpm/t7005-bpm-publish.shindex b7607ba2..80496fa0 100644--- a/testing/functional/bpm/t7005-bpm-publish.sh+++ b/testing/functional/bpm/t7005-bpm-publish.sh@@ -369,6 +369,76 @@ PYgrep -q "no \[upatches.tree\] entries" "$WORK/plain.err"'+# A package of one XDP upatch named $1 for the device classes $2, with $3+# as the rest of its tree entry. A second name $4 adds an upatch for the+# same classes.+xdp_package() {+ mkdir -p "$WORK/$1" &&+ ln -s "$REPO/vendor" "$WORK/$1/vendor" &&+ ln -s "$REPO/bpf" "$WORK/$1/bpf" &&+ printf '[package]\nname = "%s"\n' "$1" >"$WORK/$1/Beefile" &&+ for name in $1 ${4:-}; do+ printf '// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause\n#include "vmlinux.h"\n#include <bpf/bpf_helpers.h>\n\nchar _license[] SEC("license") = "Dual BSD/GPL";\n\nSEC("xdp/%s")\nint xdp_pass(struct xdp_md *ctx)\n{\n\treturn XDP_PASS;\n}\n' \+ "$2" >"$WORK/$1/$name.bpf.c" &&+ printf '\n[[upatches]]\nsource = "%s.bpf.c"\n\n[upatches.tree]\nid = "%s"\n%b' \+ "$name" "$name" "$3" >>"$WORK/$1/Beefile" || return 1+ done &&+ ( cd "$WORK" && "$S" bpm build "$1" --version 1.0.0 \+ --keys-dir "$WORK/keys" ) >&2+}+site() {+ printf '\\n[upatches.tree.selector.labels]\\nsite = [%s]\\n' "$1"+}+xdp_append() {+ "$S" bpm tree append "$WORK/$1-1.0.0.bee" "oci://$REGISTRY/ns/xdp" \+ --keys-dir "$WORK/keys" --tree-id xdp "${2:---op=add}" \+ >/dev/null 2>"$WORK/$1.err"+}+xdp_refused() {+ if xdp_append "$1"; then+ return 1+ fi+ grep -q "$1 and $2 both select XDP device classes $3 " "$WORK/$1.err"+}++# A device runs one native XDP program, so the second of two upatches that+# select a class fails its load wherever such a device is, and tree sync+# stops there on every node the two reach.+test_expect_success 'tree append refuses an XDP device class another upatch of the tree selects' '+ xdp_package t7005xa 3 "$(site \"a\")" &&+ xdp_package t7005xb 2 "$(site "\"a\", \"b\"")" &&+ xdp_append t7005xa &&+ cp "$REG/ns/xdp/manifests/stable" "$WORK/xdp1.manifest" &&+ xdp_refused t7005xb "t7005xa (seq 1)" 2 &&+ cmp -s "$WORK/xdp1.manifest" "$REG/ns/xdp/manifests/stable" &&+ ! ls "$REG/ns/xdp/manifests" | grep -q "upatch-t7005xb"+'++test_expect_success 'tree append takes the class once the upatch that held it is deleted' '+ xdp_append t7005xa --op=delete &&+ xdp_append t7005xb &&+ xdp_package t7005xc 1 "$(site \"a\")" &&+ xdp_append t7005xc+'++test_expect_success 'tree append takes a shared class on selectors that exclude each other, and another class' '+ xdp_package t7005xd 2 "$(site \"c\")" &&+ xdp_append t7005xd &&+ xdp_package t7005xe 8 "" &&+ xdp_append t7005xe &&+ xdp_package t7005xf 2 "" &&+ xdp_refused t7005xf "t7005xb (seq 3)" 2+'++test_expect_success 'tree append refuses two upatches of one package that select a class' '+ xdp_package t7005xg 16 "" t7005xh &&+ ! "$S" bpm tree append "$WORK/t7005xg-1.0.0.bee" "oci://$REGISTRY/ns/xdp2" \+ --keys-dir "$WORK/keys" >/dev/null 2>"$WORK/t7005xg.err" &&+ grep -q "t7005xh and t7005xg (seq 1) both select XDP device classes 16 " \+ "$WORK/t7005xg.err" &&+ ! test -e "$REG/ns/xdp2/manifests/stable"+'+test_expect_success 'the operator commands are reachable directly in shirudo-build' '"$B" bpm --help | grep -q "tree append <package-file> <repository>" &&"$B" bpm tree append --help 2>&1 | grep -q "\-\-op add|delete" &&diff --git a/testing/unit/bpm_tree_overlap.c b/testing/unit/bpm_tree_overlap.cnew file mode 100644index 00000000..7ed9858f--- /dev/null+++ b/testing/unit/bpm_tree_overlap.c@@ -0,0 +1,157 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <shirudo/string.h>++#include "bpm_tree.h"+#include "bpm_tree_overlap.h"+#include "unit.h"++static void set(char *dst, size_t cap, const char *src)+{+ ASSERT_TRUE(strscpy(dst, src, cap) >= 0);+}++static void pair(struct tree_selector_kv **kvs, int *n, const char *key,+ const char *first, const char *second)+{+ struct tree_selector_kv *kv;++ ASSERT_EQ(tree_selector_kv_add(kvs, n, TREE_MAX_LABELS, &kv), 0);+ set(kv->key, sizeof(kv->key), key);+ set(kv->values[kv->n_values++], TREE_STRING_MAX, first);+ if (second)+ set(kv->values[kv->n_values++], TREE_STRING_MAX, second);+}++static void label(struct tree_selector *s, const char *key, const char *first,+ const char *second)+{+ pair(&s->labels, &s->n_labels, key, first, second);+}++static void an_unconstrained_selector_shares_every_node(void)+{+ static struct tree_selector any, some;++ set(some.architecture[some.n_architecture++], TREE_STRING_MAX, "x86_64");+ label(&some, "site", "a", NULL);+ EXPECT_FALSE(tree_selectors_disjoint(&any, &any));+ EXPECT_FALSE(tree_selectors_disjoint(&any, &some));+ EXPECT_FALSE(tree_selectors_disjoint(&some, &any));+ tree_selector_free(&some);+}++static void lists_without_a_common_value_are_disjoint(void)+{+ static struct tree_selector a, b;++ set(a.architecture[a.n_architecture++], TREE_STRING_MAX, "x86_64");+ set(b.architecture[b.n_architecture++], TREE_STRING_MAX, "aarch64");+ EXPECT_TRUE(tree_selectors_disjoint(&a, &b));+ set(b.architecture[b.n_architecture++], TREE_STRING_MAX, "x86_64");+ EXPECT_FALSE(tree_selectors_disjoint(&a, &b));+ set(a.kernel_build_id[a.n_kernel_build_id++], TREE_STRING_MAX, "aa");+ EXPECT_FALSE(tree_selectors_disjoint(&a, &b));+ set(b.kernel_build_id[b.n_kernel_build_id++], TREE_STRING_MAX, "bb");+ EXPECT_TRUE(tree_selectors_disjoint(&a, &b));+}++/* 7.1 takes 7.1.3 and not 7.10.0, as the node reads a prefix. */+static void release_prefixes_meet_where_one_continues_the_other(void)+{+ static struct tree_selector a, b;++ set(a.kernel_release_prefix[a.n_kernel_release_prefix++],+ TREE_STRING_MAX, "7.1");+ set(b.kernel_release_prefix[b.n_kernel_release_prefix++],+ TREE_STRING_MAX, "7.10");+ EXPECT_TRUE(tree_selectors_disjoint(&a, &b));+ EXPECT_TRUE(tree_selectors_disjoint(&b, &a));+ set(b.kernel_release_prefix[0], TREE_STRING_MAX, "7.1.3");+ EXPECT_FALSE(tree_selectors_disjoint(&a, &b));+ EXPECT_FALSE(tree_selectors_disjoint(&b, &a));+ set(b.kernel_release_prefix[0], TREE_STRING_MAX, "7");+ EXPECT_FALSE(tree_selectors_disjoint(&a, &b));+}++static void a_key_with_other_values_is_disjoint(void)+{+ static struct tree_selector a, b, other_key, both;++ label(&a, "site", "a", NULL);+ label(&b, "site", "b", NULL);+ label(&other_key, "env", "b", NULL);+ label(&both, "site", "b", "a");+ EXPECT_TRUE(tree_selectors_disjoint(&a, &b));+ EXPECT_FALSE(tree_selectors_disjoint(&a, &other_key));+ EXPECT_FALSE(tree_selectors_disjoint(&a, &both));+ pair(&a.kconfig, &a.n_kconfig, "CONFIG_PREEMPT", "y", NULL);+ pair(&both.kconfig, &both.n_kconfig, "CONFIG_PREEMPT", "n", NULL);+ EXPECT_TRUE(tree_selectors_disjoint(&a, &both));+ tree_selector_free(&a);+ tree_selector_free(&b);+ tree_selector_free(&other_key);+ tree_selector_free(&both);+}++static struct tree_operation *operation(struct tree_publication *pub,+ const char *op, const char *id)+{+ static struct tree_operation blank;++ set(blank.op, sizeof(blank.op), op);+ set(blank.id, sizeof(blank.id), id);+ ASSERT_EQ(tree_publication_append(pub, &blank), 0);+ return &pub->operations[pub->n_operations - 1];+}++/* A delete that leaves the upatch on some node the add reached keeps it. */+static void an_add_lives_until_a_delete_reaches_all_its_nodes(void)+{+ struct tree_publication pub = {};+ struct tree_operation *op;++ operation(&pub, "delete", "x");+ op = operation(&pub, "add", "x");+ label(&op->selector, "site", "a", "b");+ EXPECT_TRUE(tree_add_live(&pub, 1));++ operation(&pub, "delete", "y");+ EXPECT_TRUE(tree_add_live(&pub, 1));+ operation(&pub, "add", "x");+ EXPECT_TRUE(tree_add_live(&pub, 1));++ op = operation(&pub, "delete", "x");+ label(&op->selector, "site", "a", NULL);+ EXPECT_TRUE(tree_add_live(&pub, 1));++ op = operation(&pub, "delete", "x");+ label(&op->selector, "site", "a", "b");+ set(op->selector.architecture[op->selector.n_architecture++],+ TREE_STRING_MAX, "x86_64");+ EXPECT_TRUE(tree_add_live(&pub, 1));++ op = operation(&pub, "delete", "x");+ label(&op->selector, "site", "b", "a");+ EXPECT_FALSE(tree_add_live(&pub, 1));+ EXPECT_TRUE(tree_add_live(&pub, 3));+ pub.n_operations--;+ EXPECT_TRUE(tree_add_live(&pub, 1));++ operation(&pub, "delete", "x");+ EXPECT_FALSE(tree_add_live(&pub, 1));+ EXPECT_FALSE(tree_add_live(&pub, 3));+ tree_publication_free(&pub);+}++static const struct unit_case cases[] = {+ UNIT_CASE(an_unconstrained_selector_shares_every_node),+ UNIT_CASE(lists_without_a_common_value_are_disjoint),+ UNIT_CASE(release_prefixes_meet_where_one_continues_the_other),+ UNIT_CASE(a_key_with_other_values_is_disjoint),+ UNIT_CASE(an_add_lives_until_a_delete_reaches_all_its_nodes),+ {}+};++UNIT_SUITE(bpm_tree_overlap, cases);diff --git a/testing/unit/upatch.c b/testing/unit/upatch.cindex 61f5fac3..44726c19 100644--- a/testing/unit/upatch.c+++ b/testing/unit/upatch.c@@ -125,6 +125,19 @@ static void name_fits_its_pin_directory(void)EXPECT_FALSE(upatch_name_valid(name));}+static void xdp_classes_come_from_xdp_programs_alone(void)+{+ struct prog_spec specs[] = {+ { .kind = UPATCH_ATTACH_XDP, .device_classes = 1 },+ { .kind = UPATCH_ATTACH_RAW_TRACEPOINT, .device_classes = 16 },+ { .kind = UPATCH_ATTACH_XDP, .device_classes = 8 },+ };++ EXPECT_UEQ(prog_specs_xdp_classes(specs, 3), 9);+ EXPECT_UEQ(prog_specs_xdp_classes(specs + 1, 1), 0);+ EXPECT_UEQ(prog_specs_xdp_classes(specs, 0), 0);+}+static void xdp_masks_are_signed_and_bounded(void){struct upatch_prog_entry prog = {};@@ -180,6 +193,7 @@ static const struct unit_case cases[] = {UNIT_CASE(name_signed_is_the_header_name),UNIT_CASE(member_is_a_name_and_a_payload),UNIT_CASE(name_fits_its_pin_directory),+ UNIT_CASE(xdp_classes_come_from_xdp_programs_alone),UNIT_CASE(xdp_masks_are_signed_and_bounded),{}};diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.cindex 63710c28..a3845c8c 100644--- a/tools/shirudo-build/bpm-publish.c+++ b/tools/shirudo-build/bpm-publish.c@@ -25,6 +25,7 @@#include "bpm_oci.h"#include "bpm_package.h"#include "bpm_tree.h"+#include "bpm_tree_overlap.h"#include "crypto.h"#include "hex.h"#include "io.h"@@ -640,6 +641,111 @@ static int tree_push_artifact(struct bpm_oci_client *c, const char *repository,return 0;}+/* The classes of a load upatch, none for one without an XDP program. */+static int upatch_xdp_classes(const void *body, size_t len, __u32 *classes)+{+ struct prog_spec *specs = NULL;+ const void *attach;+ __u32 attach_len;+ struct upatch u;+ int ret;++ ret = upatch_open_memory(body, len, &u);+ if (ret)+ return ret;+ attach = upatch_signed_attach(&u, &attach_len);+ ret = attach ? prog_specs_parse(attach, attach_len, u.hdr.nr_maps,+ u.hdr.nr_progs, &specs) : -EINVAL;+ if (!ret) {+ *classes = prog_specs_xdp_classes(specs, u.hdr.nr_progs);+ prog_specs_free(specs, u.hdr.nr_progs);+ }+ upatch_close(&u);+ return ret;+}++static int tree_fetch_xdp_classes(struct bpm_oci_client *c,+ const char *repository, const char *repo_ref,+ const struct tree_operation *op,+ __u32 *classes)+{+ struct bpm_oci_client elsewhere __cleanup(bpm_oci_client_clear) = {};+ char other_repository[BPM_MAX_ENDPOINT], url[PATH_MAX], hex[65];+ struct bpm_buffer blob = {};+ __u8 digest[32];+ int ret;++ if (sha256_digest_decode(op->artifact.layer_digest, digest))+ return -EINVAL;+ if (strcmp(op->artifact.repository, repo_ref)) {+ ret = bpm_oci_parse_tree_repo(op->artifact.repository,+ &elsewhere, other_repository,+ sizeof(other_repository));+ if (ret)+ return ret;+ c = &elsewhere;+ repository = other_repository;+ }+ ret = bpm_oci_url(c, repository, "blobs", op->artifact.layer_digest,+ url, sizeof(url));+ if (!ret)+ ret = bpm_oci_get_blob(c, url, BPM_UPATCH_MEDIA, &blob);+ if (!ret && (bpm_sha256_hex(blob.data, blob.len, hex, sizeof(hex)) ||+ !bpm_digest_matches(op->artifact.layer_digest, hex)))+ ret = -EKEYREJECTED;+ if (!ret)+ ret = upatch_xdp_classes(blob.data, blob.len, classes);+ bpm_buffer_free(&blob);+ return ret;+}++/* The classes of the publication's operations, read once each. */+struct tree_xdp_classes {+ __u32 classes[TREE_MAX_OPS];+ bool known[TREE_MAX_OPS];+};++/*+ * A device runs one native XDP program. The second of two upatches that+ * select a class fails its load on every node with such a device, and+ * tree sync stops at the first operation that fails.+ */+static int tree_refuse_shared_xdp_class(struct bpm_oci_client *c,+ const char *repository,+ const char *repo_ref,+ const struct tree_publication *pub,+ struct tree_xdp_classes *seen,+ const struct tree_operation *op,+ __u32 classes)+{+ for (int i = 0; classes && i < pub->n_operations; i++) {+ const struct tree_operation *other = &pub->operations[i];+ int ret;++ if (strcmp(other->op, "add") || !strcmp(other->id, op->id) ||+ !tree_add_live(pub, i) ||+ tree_selectors_disjoint(&other->selector, &op->selector))+ continue;+ if (!seen->known[i]) {+ ret = tree_fetch_xdp_classes(c, repository, repo_ref,+ other, &seen->classes[i]);+ if (ret) {+ pr_err("bpm tree append: cannot read the artifact of %s (seq %lld) to compare its XDP device classes",+ other->id, (long long)other->seq);+ return ret;+ }+ seen->known[i] = true;+ }+ if (!(seen->classes[i] & classes))+ continue;+ pr_err("bpm tree append: %s and %s (seq %lld) both select XDP device classes %u on nodes their selectors share; a device runs one XDP program, so give each class to one upatch or the two selectors that exclude each other",+ op->id, other->id, (long long)other->seq,+ seen->classes[i] & classes);+ return -EBUSY;+ }+ return 0;+}+static int cmd_bpm_tree_append(int argc, char **argv){const char *package = NULL, *raw_repository = NULL, *keys_dir_arg = NULL;@@ -650,6 +756,7 @@ static int cmd_bpm_tree_append(int argc, char **argv)const char *reference = "stable", *operation = "add";char key_path[PATH_MAX], cert_path[PATH_MAX];char parent[BPM_MAX_DIGEST] = {}, digest[72];+ struct tree_xdp_classes *seen = NULL;struct tree_publication pub = {};struct tree_operation *op = NULL;size_t len, payload_len = 0, sig_len = 0;@@ -801,7 +908,8 @@ static int cmd_bpm_tree_append(int argc, char **argv)sizeof(pub.parent_manifest_digest), parent);op = xmalloc(sizeof(*op));- if (!op) {+ seen = xcalloc(1, sizeof(*seen));+ if (!op || !seen) {ret = -ENOMEM;goto out;}@@ -809,6 +917,7 @@ static int cmd_bpm_tree_append(int argc, char **argv)const struct bpm_build_upatch *u = &spec->upatches[i];const char *member;__u8 *body = NULL;+ __u32 classes = 0;size_t body_len;__s64 seq;@@ -839,8 +948,21 @@ static int cmd_bpm_tree_append(int argc, char **argv)}}tree_selector_canonicalize(&op->selector);- ret = tree_push_artifact(&c, repository, repo_ref, body, body_len,- u->tree.id, unload, &op->artifact);+ if (!unload) {+ ret = upatch_xdp_classes(body, body_len, &classes);+ if (ret)+ pr_err("bpm tree append: %s is not a valid upatch",+ u->tree.id);+ else+ ret = tree_refuse_shared_xdp_class(&c, repository,+ repo_ref, &pub,+ seen, op,+ classes);+ }+ if (!ret)+ ret = tree_push_artifact(&c, repository, repo_ref, body,+ body_len, u->tree.id, unload,+ &op->artifact);xfree(body, body_len + 1);if (ret)goto out;@@ -849,6 +971,8 @@ static int cmd_bpm_tree_append(int argc, char **argv)ret = tree_publication_append(&pub, op);if (ret)goto out;+ seen->classes[pub.n_operations - 1] = classes;+ seen->known[pub.n_operations - 1] = true;pub.head_seq = op->seq;appended++;}@@ -907,6 +1031,7 @@ out:if (op)tree_selector_free(&op->selector);xfree(op, sizeof(*op));+ xfree(seen, sizeof(*seen));tree_publication_free(&pub);bpm_build_spec_free(spec);xfree(beefile, len + 1);diff --git a/util/bpm_tree_overlap.c b/util/bpm_tree_overlap.cnew file mode 100644index 00000000..db6d2813--- /dev/null+++ b/util/bpm_tree_overlap.c@@ -0,0 +1,163 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * Which operations of an upatch tree can meet on one node, for the+ * checks `bpm tree append` makes before it signs a publication.+ */++#include <stdbool.h>+#include <string.h>++#include "bpm_tree.h"+#include "bpm_tree_overlap.h"+#include "kernel_release.h"++static bool list_holds(const char (*list)[TREE_STRING_MAX], int n,+ const char *value)+{+ for (int i = 0; i < n; i++)+ if (!strcmp(list[i], value))+ return true;+ return false;+}++static bool lists_share(const char (*a)[TREE_STRING_MAX], int na,+ const char (*b)[TREE_STRING_MAX], int nb)+{+ for (int i = 0; i < na; i++)+ if (list_holds(b, nb, a[i]))+ return true;+ return false;+}++static bool list_within(const char (*inner)[TREE_STRING_MAX], int n_inner,+ const char (*outer)[TREE_STRING_MAX], int n_outer)+{+ for (int i = 0; i < n_inner; i++)+ if (!list_holds(outer, n_outer, inner[i]))+ return false;+ return true;+}++/*+ * Two prefixes take a common release exactly when one takes the other+ * read as a release: every release the longer takes continues it.+ */+static bool prefixes_share(const char (*a)[TREE_STRING_MAX], int na,+ const char (*b)[TREE_STRING_MAX], int nb)+{+ for (int i = 0; i < na; i++)+ for (int j = 0; j < nb; j++)+ if (kernel_release_prefix_matches(a[i], b[j]) ||+ kernel_release_prefix_matches(b[j], a[i]))+ return true;+ return false;+}++static bool prefixes_within(const char (*inner)[TREE_STRING_MAX], int n_inner,+ const char (*outer)[TREE_STRING_MAX], int n_outer)+{+ for (int i = 0; i < n_inner; i++) {+ bool taken = false;++ for (int j = 0; j < n_outer && !taken; j++)+ taken = kernel_release_prefix_matches(inner[i],+ outer[j]);+ if (!taken)+ return false;+ }+ return true;+}++/* A node has one value for a key. */+static bool kvs_exclude(const struct tree_selector_kv *a, int na,+ const struct tree_selector_kv *b, int nb)+{+ for (int i = 0; i < na; i++)+ for (int j = 0; j < nb; j++)+ if (!strcmp(a[i].key, b[j].key) &&+ !lists_share(a[i].values, a[i].n_values,+ b[j].values, b[j].n_values))+ return true;+ return false;+}++static bool kvs_within(const struct tree_selector_kv *inner, int n_inner,+ const struct tree_selector_kv *outer, int n_outer)+{+ for (int j = 0; j < n_outer; j++) {+ bool held = false;++ for (int i = 0; i < n_inner && !held; i++)+ held = !strcmp(inner[i].key, outer[j].key) &&+ list_within(inner[i].values, inner[i].n_values,+ outer[j].values, outer[j].n_values);+ if (!held)+ return false;+ }+ return true;+}++bool tree_selectors_disjoint(const struct tree_selector *a,+ const struct tree_selector *b)+{+ if (a->n_architecture && b->n_architecture &&+ !lists_share(a->architecture, a->n_architecture,+ b->architecture, b->n_architecture))+ return true;+ if (a->n_kernel_build_id && b->n_kernel_build_id &&+ !lists_share(a->kernel_build_id, a->n_kernel_build_id,+ b->kernel_build_id, b->n_kernel_build_id))+ return true;+ if (a->n_kernel_release_prefix && b->n_kernel_release_prefix &&+ !prefixes_share(a->kernel_release_prefix,+ a->n_kernel_release_prefix,+ b->kernel_release_prefix,+ b->n_kernel_release_prefix))+ return true;+ return kvs_exclude(a->kconfig, a->n_kconfig, b->kconfig, b->n_kconfig) ||+ kvs_exclude(a->labels, a->n_labels, b->labels, b->n_labels);+}++/* An empty list of outer constrains nothing, one of inner everything. */+static bool selector_covers(const struct tree_selector *outer,+ const struct tree_selector *inner)+{+ if (outer->n_architecture &&+ (!inner->n_architecture ||+ !list_within(inner->architecture, inner->n_architecture,+ outer->architecture, outer->n_architecture)))+ return false;+ if (outer->n_kernel_build_id &&+ (!inner->n_kernel_build_id ||+ !list_within(inner->kernel_build_id, inner->n_kernel_build_id,+ outer->kernel_build_id, outer->n_kernel_build_id)))+ return false;+ if (outer->n_kernel_release_prefix &&+ (!inner->n_kernel_release_prefix ||+ !prefixes_within(inner->kernel_release_prefix,+ inner->n_kernel_release_prefix,+ outer->kernel_release_prefix,+ outer->n_kernel_release_prefix)))+ return false;+ return kvs_within(inner->kconfig, inner->n_kconfig,+ outer->kconfig, outer->n_kconfig) &&+ kvs_within(inner->labels, inner->n_labels,+ outer->labels, outer->n_labels);+}++bool tree_add_live(const struct tree_publication *pub, int index)+{+ const struct tree_operation *add = &pub->operations[index];++ for (int i = index + 1; i < pub->n_operations; i++) {+ const struct tree_operation *later = &pub->operations[i];++ if (!strcmp(later->op, "delete") &&+ !strcmp(later->id, add->id) &&+ selector_covers(&later->selector, &add->selector))+ return false;+ }+ return true;+}diff --git a/util/bpm_tree_overlap.h b/util/bpm_tree_overlap.hnew file mode 100644index 00000000..863f6962--- /dev/null+++ b/util/bpm_tree_overlap.h@@ -0,0 +1,31 @@+/* SPDX-License-Identifier: BSD-2-Clause */+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * What a publisher can tell about the nodes two operations of an upatch+ * tree reach, from their selectors alone.+ */++#ifndef SHIRUDO_BPM_TREE_OVERLAP_H+#define SHIRUDO_BPM_TREE_OVERLAP_H++#include <stdbool.h>++struct tree_publication;+struct tree_selector;++/*+ * Whether no node matches both selectors: they name different+ * architectures, kernel builds or releases, or different values for one+ * label or kernel option. False is no proof of a shared node.+ */+bool tree_selectors_disjoint(const struct tree_selector *a,+ const struct tree_selector *b);++/*+ * Whether the add at index may still be loaded somewhere: no later+ * delete of its upatch reaches every node the add reaches.+ */+bool tree_add_live(const struct tree_publication *pub, int index);++#endif /* SHIRUDO_BPM_TREE_OVERLAP_H */diff --git a/util/upatch.c b/util/upatch.cindex 8d2c3ca3..ac54fa33 100644--- a/util/upatch.c+++ b/util/upatch.c@@ -590,6 +590,16 @@ void prog_specs_free(struct prog_spec *specs, __u32 nr_progs)xfree(specs, (size_t)nr_progs * sizeof(*specs));}+__u32 prog_specs_xdp_classes(const struct prog_spec *specs, __u32 nr_progs)+{+ __u32 i, classes = 0;++ for (i = 0; i < nr_progs; i++)+ if (specs[i].kind == UPATCH_ATTACH_XDP)+ classes |= specs[i].device_classes;+ return classes;+}+int prog_specs_parse(const void *metadata, __u32 metadata_len,__u32 nr_maps, __u32 nr_progs, struct prog_spec **out){diff --git a/util/upatch.h b/util/upatch.hindex aa0f1203..011e0087 100644--- a/util/upatch.h+++ b/util/upatch.h@@ -169,6 +169,8 @@ struct prog_spec {int prog_specs_parse(const void *metadata, __u32 metadata_len,__u32 nr_maps, __u32 nr_progs, struct prog_spec **out);void prog_specs_free(struct prog_spec *specs, __u32 nr_progs);+/* Every device class an XDP program among the specs selects. */+__u32 prog_specs_xdp_classes(const struct prog_spec *specs, __u32 nr_progs);/** The target's upatch lifecycle, which builtin-upatch.c implements for