eBPF-based security platform
upatch: check the stable pins of an XDP upatch
John Fastabendc41cd0c1f1b9
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 2c1c843e..4d2fea49 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -206,8 +206,8 @@ signed config image.* - ``t3112-xdp-reconcile``- ``upatch reconcile`` attaches a device added after load, adds nopin or event on a second pass, leaves a device another program- occupies, removes a gone device's pin without reusing a live- index, does nothing after unload, and leaves the link on a veth+ occupies, does not read a gone device as tampering, removes its+ pin without reusing a live index, does nothing after unload, and leaves the link on a vethend moved to another namespace. A changed archive is covered bythe ``upatch_verify`` unit cases. An unload racing a pass is notreported as tampering.diff --git a/Documentation/operate/run/read-status.rst b/Documentation/operate/run/read-status.rstindex 6ca6337a..0ade6114 100644--- a/Documentation/operate/run/read-status.rst+++ b/Documentation/operate/run/read-status.rst@@ -240,7 +240,9 @@ verdict is derived from them.:doc:`/operate/troubleshoot/incident-tampered`. ``ALERT`` ``<n>upatches are missing a link: <names>``: a loaded upatch has fewerlink pins than its signed metadata implies, so part of it is- detached. Handle it the same way. ``WARN`` ``no+ detached. For an upatch with XDP that means an ``xdp_prog_<i>`` pin+ or the link of another attach program; a device going away is not+ a missing link. Handle it the same way. ``WARN`` ``norecord of this boot to check the pins against`` before ``init``, or``pins not checked against the records: <error>``.* - ``TpmMeasured``diff --git a/Documentation/upatches/write/attach-kinds.rst b/Documentation/upatches/write/attach-kinds.rstindex 987d1198..f62f5683 100644--- a/Documentation/upatches/write/attach-kinds.rst+++ b/Documentation/upatches/write/attach-kinds.rst@@ -312,7 +312,10 @@ Load and restore each discover devices once.:doc:`/reference/cli/upatch-reconcile` repeats that discovery later: itattaches a loaded upatch to any selected device it does not run on yetand removes the pin of a link whose device has gone. Removing a device-detaches its XDP link. The signed artifact and TPM measurement+detaches its XDP link, so ``init`` and ``status`` do not count XDP link+pins as they do other links: they require the ``xdp_prog_<i>`` pin of each+XDP program and a link for each other attach program of the upatch.+The signed artifact and TPM measurementcover the class policy, not a fixed device inventory. The location``status`` and ``upatch get`` report names the first device stillattached and how many more there are, as in ``xdp/eth0 (+1)``.diff --git a/testing/functional/upatch/t3112-xdp-reconcile.sh b/testing/functional/upatch/t3112-xdp-reconcile.shindex 297b8dd0..10cbbcf2 100644--- a/testing/functional/upatch/t3112-xdp-reconcile.sh+++ b/testing/functional/upatch/t3112-xdp-reconcile.sh@@ -98,6 +98,8 @@ test_xdp_success 'a gone device loses its pin and no live index is reused' 'load "$NAME" &&[ -e "$B/$NAME/link_0" ] && [ -e "$B/$NAME/link_1" ] &&ip link del xdpa &&+ "$SHIRUDO_BIN" status --check &&+ "$SHIRUDO_BIN" init &&add_pair xdpe 30020 xdpf 30021 &&reconcile &&[ ! -e "$B/$NAME/link_0" ] &&diff --git a/util/upatch_verify.c b/util/upatch_verify.cindex 6a42ef90..d5079437 100644--- a/util/upatch_verify.c+++ b/util/upatch_verify.c@@ -9,9 +9,13 @@#include <errno.h>#include <limits.h>#include <string.h>+#include <sys/stat.h>#include <sys/xattr.h>#include <unistd.h>+#include <bpf/bpf.h>+#include <linux/bpf.h>+#include <shirudo/string.h>#include "chain.h"@@ -74,6 +78,58 @@ int upatch_archive_verify(const char *name,return 0;}+static int count_non_xdp_link(__u32 index, const char *pin, void *ctx)+{+ struct bpf_link_info info = {};+ __u32 len = sizeof(info);+ __u32 *found = ctx;+ int fd, ret;++ fd = bpf_obj_get(pin);+ if (fd < 0)+ return -errno;+ ret = bpf_link_get_info_by_fd(fd, &info, &len);+ close(fd);+ if (ret)+ return ret;+ if (info.type != BPF_LINK_TYPE_XDP)+ (*found)++;+ return 0;+}++/*+ * XDP links come and go with devices, so their number says nothing. What+ * cannot change is a program pin for each XDP program and a link for each+ * other attach program. Link numbers are shared and shift with the devices,+ * so the kernel says which links are which.+ */+static int xdp_upatch_complete(const char *name, const struct prog_spec *specs,+ __u32 nr_progs, __u32 nr_attach)+{+ __u32 i, nr_xdp = 0, found = 0;+ char pin[PATH_MAX];+ char entry[32];+ struct stat st;+ int ret;++ for (i = 0; i < nr_progs; i++) {+ if (specs[i].kind != UPATCH_ATTACH_XDP)+ continue;+ nr_xdp++;+ if (strsprintf(entry, sizeof(entry), XDP_PROG_PIN_PREFIX "%u", i) < 0 ||+ upatch_pin_path(pin, sizeof(pin), name, entry, NULL))+ return -ENAMETOOLONG;+ if (stat(pin, &st))+ return errno == ENOENT ? 0 : -errno;+ }+ if (nr_attach == nr_xdp)+ return 1;+ ret = upatch_each_link(name, count_non_xdp_link, &found);+ if (ret)+ return ret;+ return found == nr_attach - nr_xdp;+}+int upatch_links_complete(const char *name, const char *path){struct prog_spec *specs = NULL;@@ -98,9 +154,13 @@ int upatch_links_complete(const char *name, const char *path)if (specs[i].kind == UPATCH_ATTACH_XDP)has_xdp = true;}- ret = upatch_pins_read(name, &pins);- if (!ret)- ret = has_xdp || upatch_pins_complete(&pins, nr_attach);+ if (has_xdp) {+ ret = xdp_upatch_complete(name, specs, u.hdr.nr_progs, nr_attach);+ } else {+ ret = upatch_pins_read(name, &pins);+ if (!ret)+ ret = upatch_pins_complete(&pins, nr_attach);+ }prog_specs_free(specs, u.hdr.nr_progs);out:upatch_close(&u);