eBPF-based security platform
upatch: pin each XDP program by spec index
John Fastabend6e6a6a6e5f5c
diff --git a/Documentation/reference/files.rst b/Documentation/reference/files.rstindex b7f99f5a..2019adaa 100644--- a/Documentation/reference/files.rst+++ b/Documentation/reference/files.rst@@ -383,6 +383,9 @@ written as ``~d``.- Attachment ``<n>`` when there are several programs or the artifactcontains XDP. XDP can produce multiple links per program, orderedby program and then ifindex; skipped devices consume no pin number.+ * - ``xdp_prog_<i>``+ - XDP program ``<i>`` of the artifact's signed metadata. It holds the+ program loaded whatever devices it is attached to.* - ``test``- A program with the test-run role.* - ``serializers/<map>``diff --git a/builtin-upatch.c b/builtin-upatch.cindex 751fd8e1..2019ffbd 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1331,6 +1331,27 @@ static int prog_pin_path(char *pin, size_t sz, const char *dir,return n < 0 ? -ENAMETOOLONG : 0;}+/*+ * Links exist only per device, so with none attached the pin is what keeps+ * the program loaded, and its index is what ties it to its signed classes.+ */+static int pin_xdp_prog(int prog_fd, const char *dir, __u32 index)+{+ char pin[PATH_MAX + 32];+ struct stat st;+ int ret;++ if (strsprintf(pin, sizeof(pin), "%s/" XDP_PROG_PIN_PREFIX "%u", dir,+ index) < 0)+ return -ENAMETOOLONG;+ if (stat(pin, &st) == 0 && unlink(pin))+ return -errno;+ ret = obj_pin(prog_fd, pin);+ if (ret)+ pr_err("pin XDP program %s: %s", pin, strerror(-ret));+ return ret;+}+static int attach_xdp_devices(int prog_fd, const struct prog_spec *spec,const struct xdp_device *devices, size_t count,const char *dir, __u32 *pinned)@@ -1514,6 +1535,10 @@ static int attach_and_pin_progs(const struct loader_ctx *cx,}attach_seen++;if (spec->kind == UPATCH_ATTACH_XDP) {+ ret = pin_xdp_prog(inner_fd, dir, i);+ if (ret)+ goto out;+ made[i] = true;ret = attach_xdp_devices(inner_fd, spec, devices, nr_devices,dir, &pinned_links);if (ret)@@ -1597,6 +1622,10 @@ out:}attach_seen = 0;for (i = 0; i < nr_progs; i++) {+ if (made[i] && specs[i].kind == UPATCH_ATTACH_XDP &&+ strsprintf(pin, sizeof(pin),+ "%s/" XDP_PROG_PIN_PREFIX "%u", dir, i) >= 0)+ unlink(pin);if (made[i] &&!(has_xdp && specs[i].role == UPATCH_PROG_ROLE_ATTACH) &&!prog_pin_path(pin, sizeof(pin), dir,diff --git a/testing/functional/upatch/t3111-xdp.sh b/testing/functional/upatch/t3111-xdp.shindex 68ac638a..7803df41 100644--- a/testing/functional/upatch/t3111-xdp.sh+++ b/testing/functional/upatch/t3111-xdp.sh@@ -69,6 +69,7 @@ test_xdp_success 'one endpoint of a host veth pair receives native XDP' '[ "$("$H" query xdph1)" -gt 0 ] &&[ "$("$H" query xdph0)" = 0 ] &&[ -e "$B/$NAME/link_0" ] && [ ! -e "$B/$NAME/link_1" ] &&+ [ -e "$B/$NAME/xdp_prog_0" ] &&before=$(wc -l </var/lib/shirudo/eventlog) &&"$SHIRUDO_BIN" init &&[ "$(wc -l </var/lib/shirudo/eventlog)" = "$before" ] &&diff --git a/util/paths.h b/util/paths.hindex 13b862fe..a4eb1ed7 100644--- a/util/paths.h+++ b/util/paths.h@@ -40,6 +40,7 @@*/#define RUN_DIR "/run/shirudo"#define BPFFS_DIR RUN_DIR "/bpffs"+#define XDP_PROG_PIN_PREFIX "xdp_prog_"/* The init record written from the initrd, until the root adopts it. */#define EVENTLOG_PENDING_PATH RUN_DIR "/eventlog.pending"/* On a target with a config image these are the image's, opened from its root. */