eBPF-based security platform
xdp: let a device that cannot be classified sit out, not fail discovery
John Fastabendee33acd45614
diff --git a/Documentation/operate/run/read-status.rst b/Documentation/operate/run/read-status.rstindex e76ef47e..6666c634 100644--- a/Documentation/operate/run/read-status.rst+++ b/Documentation/operate/run/read-status.rst@@ -258,7 +258,10 @@ verdict is derived from them.their XDP upatch: <upatch> on <device> (<why>)`` lists eachdevice that does not, whatever the reason, with what the devicereports: ``no native XDP``, ``no multi-buffer XDP``, ``another- XDP program attached`` or ``not attached``. A device added in the+ XDP program attached`` or ``not attached``. ``not classified:+ <error>`` is a device whose driver or devlink port could not be+ read, so no upatch selects it; it is listed under every XDP+ upatch it does not run, since any of them might select it. A device added in thelast seconds shows ``not attached`` until ``upatch reconcile``attaches it, and one udev does not announce, such as a veth peermoving namespace, for up to a minute. One that stays listed is unfiltered. Its driverdiff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex d79b0349..9a57cb7e 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -1565,7 +1565,12 @@ R21 upatches can be neutralized by changing their targetsorder stay unloaded that boot. Neither a setting that survives areboot, in the NIC's firmware or the hypervisor, nor a second upatchthat selects the device can therefore take it out of an upatch- unseen, at the price of a host that boots without those upatches. A veth end moved to another namespace+ unseen, at the price of a host that boots without those upatches. A+ device whose driver name or devlink port cannot be read is the+ exception: no class can be given to it, and any device's failing+ lookup would otherwise stop ``init``, so it sits out with a warning+ and ``status`` lists it as ``not classified`` under every XDP+ upatch. A veth end moved to another namespacekeeps its link, so a pair can hold two attachments, one of them runningin that namespace. Reconcile adds no register event: TPM evidence coversthe signed class mask and loaded artifact, not the device inventory ordiff --git a/Documentation/upatches/write/attach-kinds.rst b/Documentation/upatches/write/attach-kinds.rstindex bcc200ed..7e53f0f8 100644--- a/Documentation/upatches/write/attach-kinds.rst+++ b/Documentation/upatches/write/attach-kinds.rst@@ -312,7 +312,14 @@ Pairs entirely outside the initial namespace are excluded. Discoverytolerates interfaces changing under it: a snapshot the kernel reportsas interrupted is retried, and an interface renamed or removed duringthe snapshot, or a veth whose peer is not in it, is left out rather than-failing the load. A kernel that does not answer a netlink request within+failing the load. So is a device whose driver name or devlink port+cannot be read, with a warning naming it: it need not be one the upatch+selects, so it does not fail the load or the boot restore, and+``status`` lists it under ``XdpCovered`` as ``not classified`` until a+later read succeeds, unless it runs the upatch. A lookup that cannot be+made at all, out of memory or descriptors, timed out, refused with+``EPERM`` or ``EACCES``, still fails the load: it would leave every+device out. A kernel that does not answer a netlink request within5 seconds fails the load with ``ETIMEDOUT``, and is not asked again.Links use ``XDP_FLAGS_DRV_MODE``. A device without native XDP, whichdiff --git a/builtin-upatch.c b/builtin-upatch.cindex e43d22eb..619cacfa 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1497,6 +1497,7 @@ static int attach_and_pin_progs(const struct loader_ctx *cx,ret = xdp_devices_read(&devices, &nr_devices);if (ret)goto out;+ xdp_devices_warn_unclassified(devices, nr_devices);}ret = mkdir_if_absent(dir, 0700, &made_dir);if (ret) {@@ -4599,6 +4600,7 @@ static int cmd_upatch_reconcile(int argc, const char **argv)"cannot read the interfaces");goto done;}+ xdp_devices_warn_unclassified(devices, nr_devices);}for (n = 0; n < nr_work; n++)if (reconcile_upatch(&work[n], devices, nr_devices, &rep))diff --git a/util/upatch_verify.c b/util/upatch_verify.cindex cfc8b6fb..6876b818 100644--- a/util/upatch_verify.c+++ b/util/upatch_verify.c@@ -379,6 +379,7 @@ struct uncovered_walk {size_t count;bool devices_read;int found;+ char why[128];};static int note_uncovered(int prog_fd, const struct prog_spec *spec,@@ -401,6 +402,56 @@ static int note_uncovered(int prog_fd, const struct prog_spec *spec,xdp_uncovered_reason(attached, err, features), w->ctx);}+static bool runs_program_of(const char *name, const struct prog_spec *specs,+ __u32 nr_progs, __u32 attached)+{+ __u32 i, id;+ int fd;++ for (i = 0; attached && i < nr_progs; i++) {+ if (specs[i].kind != UPATCH_ATTACH_XDP)+ continue;+ fd = upatch_xdp_prog_open(name, i, &id);+ if (fd < 0)+ continue;+ close(fd);+ if (id == attached)+ return true;+ }+ return false;+}++/*+ * Whether name selects it is not known, so unless it runs name's program+ * it may run without it.+ */+static int note_unclassified(struct uncovered_walk *w, const char *name,+ const struct prog_spec *specs, __u32 nr_progs)+{+ size_t i;+ int ret;++ for (i = 0; i < w->count; i++) {+ const struct xdp_device *dev = &w->devices[i];+ __u32 attached = 0;++ if (!dev->unclassified_err)+ continue;+ if (!bpf_xdp_query_id((int)dev->ifindex, XDP_FLAGS_DRV_MODE,+ &attached) &&+ runs_program_of(name, specs, nr_progs, attached))+ continue;+ if (strsprintf(w->why, sizeof(w->why), "not classified: %s",+ strerror(-dev->unclassified_err)) < 0)+ return -E2BIG;+ w->found++;+ ret = w->cb(name, dev->name, w->why, w->ctx);+ if (ret)+ return ret;+ }+ return 0;+}+/* An archive that changed is upatch_xdp_archives_changed()'s to report. */static int xdp_uncovered(const struct measured_set *s, const char *name,void *ctx)@@ -427,6 +478,9 @@ static int xdp_uncovered(const struct measured_set *s, const char *name,ret = upatch_xdp_each_uncovered(name, specs, u.hdr.nr_progs,w->devices, w->count,note_uncovered, w, &query_err);+ if (!ret)+ ret = note_unclassified(w, name, specs,+ u.hdr.nr_progs);}prog_specs_free(specs, u.hdr.nr_progs);upatch_close(&u);diff --git a/util/xdp.c b/util/xdp.cindex 757ebc8c..0ac2367d 100644--- a/util/xdp.c+++ b/util/xdp.c@@ -22,6 +22,7 @@#include <shirudo/string.h>#include <shirudo/upatch.h>+#include "log.h"#include "malloc.h"#include "xdp.h"@@ -603,6 +604,13 @@ static int device_compare(const void *a, const void *b)(left->ifindex < right->ifindex);}+static bool lookup_failed(int err)+{+ return err == -ENOMEM || err == -ENOBUFS || err == -EMFILE ||+ err == -ENFILE || err == -ETIMEDOUT || err == -EPERM ||+ err == -EACCES || err == -EBADMSG;+}+static int devices_read_once(struct device_list *list){struct nl_request req = {@@ -624,19 +632,37 @@ static int devices_read_once(struct device_list *list)if (i && list->devices[i - 1].ifindex == dev->ifindex)return -EAGAIN;/*- * A rename or removal racing the dump says nothing about- * the other devices; without its facts this one can't be- * classified, so it sits out this discovery.+ * A rename or removal racing the dump, or a driver or devlink+ * answer about this device alone, says nothing about the+ * others; without its facts this one can't be classified, so+ * it sits out this discovery. Only a failure is shown to the+ * operator. A lookup that could not be made at all would leave+ * every device out, so it fails the discovery.*/ret = device_details(dev);- if (ret == -EXDEV || ret == -ENODEV)- dev->unresolved = true;- else if (ret)+ if (ret && lookup_failed(ret))return ret;+ if (ret) {+ dev->unresolved = true;+ if (ret != -EXDEV && ret != -ENODEV)+ dev->unclassified_err = ret;+ }}return 0;}+void xdp_devices_warn_unclassified(const struct xdp_device *devs,+ size_t count)+{+ size_t i;++ for (i = 0; i < count; i++)+ if (devs[i].unclassified_err)+ pr_warn("XDP: cannot classify %s (ifindex %u), it sits out: %s",+ devs[i].name, devs[i].ifindex,+ strerror(-devs[i].unclassified_err));+}+int xdp_devices_read(struct xdp_device **out, size_t *count){struct device_list list = {};diff --git a/util/xdp.h b/util/xdp.hindex d40e5dbd..4bdb10f7 100644--- a/util/xdp.h+++ b/util/xdp.h@@ -38,6 +38,8 @@ struct xdp_device {char name[XDP_IFNAME_MAX];__u32 ifindex;__u32 peer_ifindex;+ /* Why the driver or devlink facts could not be read, or 0. */+ int unclassified_err;__u32 flags;__u32 port_index;__u16 port_flavour;@@ -64,7 +66,13 @@ int xdp_device_parse(const void *data, size_t len, struct xdp_device *out);__u32 xdp_device_class(const struct xdp_device *dev);bool xdp_device_selected(const struct xdp_device *devs, size_t count,size_t index, __u32 mask);+/*+ * A device whose facts cannot be read sits out, unresolved, rather than+ * failing the read: it need not be one any upatch selects.+ */int xdp_devices_read(struct xdp_device **out, size_t *count);+void xdp_devices_warn_unclassified(const struct xdp_device *devs,+ size_t count);int xdp_device_verify(const struct xdp_device *expected);int xdp_features_parse(const void *data, size_t len, __u64 *features);int xdp_device_features(__u32 ifindex, __u64 *features);