Skip to content

eBPF-based security platform

build: refuse XDP programs of one upatch that share a device class

John Fastabendfe1875f987bd
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 79a3429b..e284c985 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -171,7 +171,9 @@ signed config image.
* - ``t3011-string-bounds``
- Names and the tree repository are refused rather than cut
* - ``t3012-xdp-metadata``
- - Signed XDP device-class masks, object inputs and invalid sections
+ - Signed XDP device-class masks, object inputs and invalid sections;
+ two XDP programs of one upatch build with disjoint classes and are
+ refused when they share one
* - ``t3101-target-fixtures``
- Prebuilt fixtures load and unload, foreign and own-keyring
fixtures are refused, a map fixture exports
diff --git a/Documentation/upatches/write/attach-kinds.rst b/Documentation/upatches/write/attach-kinds.rst
index 53e3aaa2..653a5549 100644
--- a/Documentation/upatches/write/attach-kinds.rst
+++ b/Documentation/upatches/write/attach-kinds.rst
@@ -252,6 +252,13 @@ mask is mandatory, nonzero and contains only the bits below. Bare
``xdp``, ``xdp.frags``, devmap and cpumap sections are refused;
``xdp/<decimal-mask>`` already means multi-buffer.
+A device runs one native XDP program at a time, so the XDP programs of
+one 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
+restore it is restored without that device, and ``status`` lists the
+device under ``XdpCovered`` as ``another XDP program attached``.
+
.. list-table::
:header-rows: 1
:widths: 12 22 66
diff --git a/testing/functional/upatch/t3012-xdp-metadata.sh b/testing/functional/upatch/t3012-xdp-metadata.sh
index 6ba94836..a182a0e3 100644
--- a/testing/functional/upatch/t3012-xdp-metadata.sh
+++ b/testing/functional/upatch/t3012-xdp-metadata.sh
@@ -30,6 +30,22 @@ build_xdp() {
--key "$WORK/k.pem" --cert "$WORK/c.der") >"$WORK/log" 2>&1
}
+build_pair() {
+ cat >"$WORK/pair.bpf.c" <<-EOF
+ // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
+ #include "vmlinux.h"
+ #include <bpf/bpf_helpers.h>
+ char _license[] SEC("license") = "Dual BSD/GPL";
+ SEC("$1")
+ int first(struct xdp_md *ctx) { return XDP_PASS; }
+ SEC("$2")
+ int second(struct xdp_md *ctx) { return XDP_PASS; }
+ EOF
+ (cd .. && "$SHB" "$WORK/pair.bpf.c" -o "$WORK/pair.upatch" \
+ --name pair --emit-artifacts --vmlinux vendor/linux/vmlinux.h \
+ --key "$WORK/k.pem" --cert "$WORK/c.der") >"$WORK/log" 2>&1
+}
+
check_masks() {
for mask in $(seq 1 31); do
if [ $((mask & 4)) -ne 0 ]; then
@@ -65,4 +81,13 @@ test_expect_success 'missing, malformed and unsupported masks and variants fail'
check_rejections
'
+test_expect_success 'programs of one upatch with disjoint classes build' '
+ build_pair xdp/1 xdp/2
+'
+
+test_expect_success 'programs of one upatch sharing a class are refused' '
+ ! build_pair xdp/3 xdp/2 &&
+ grep -q "selects a device class another XDP program" "$WORK/log"
+'
+
test_done
diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.c
index 5cf5dd75..e1ecf679 100644
--- a/tools/shirudo-build/package.c
+++ b/tools/shirudo-build/package.c
@@ -1159,7 +1159,7 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_maps,
char (*seen_maps)[UPATCH_PROG_MAP_MAX] = NULL;
struct bpf_program *p;
size_t len, base_len, md_alloc_len;
- __u32 i = 0, n_seen = 0, nr_uprobe_targets = 0;
+ __u32 i = 0, n_seen = 0, nr_uprobe_targets = 0, xdp_classes = 0;
bool has_serializer = false, has_test_run = false;
int err;
@@ -1333,6 +1333,14 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_maps,
err = -EINVAL;
goto err_free;
}
+ /* A device takes one native XDP program at a time. */
+ if (mask & xdp_classes) {
+ pr_err("XDP section '%s' selects a device class another XDP program of this upatch selects; give each class to one program",
+ sec);
+ err = -EINVAL;
+ goto err_free;
+ }
+ xdp_classes |= (__u32)mask;
md->progs[i].xdp.device_classes = cpu_to_le32((__u32)mask);
i++;
continue;