eBPF-based security platform
xdp: report a driver refusal at reconcile, keep it fatal at restore
John Fastabend526a083ba7ac
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 4d2fea49..2487c384 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -210,7 +210,8 @@ signed config image.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 not- reported as tampering.+ reported as tampering, and a veth whose peer MTU is too large for+ XDP is warned about without stopping the pass.``tpm`` (4)~~~~~~~~~~~diff --git a/Documentation/operate/deploy/initialize.rst b/Documentation/operate/deploy/initialize.rstindex 4bef31f6..9d43a258 100644--- a/Documentation/operate/deploy/initialize.rst+++ b/Documentation/operate/deploy/initialize.rst@@ -263,7 +263,8 @@ one that fails, with ``boot-restore: failed to apply <file>`` andblocked``. The upatches it loaded before that one stay loaded, and noregister measures them. The archive is sealed, so the next boot triesthe same upatch again. The usual cause is a kernel on which the upatch-no longer loads. :ref:`boot-restore-lockout` describes the way out.+no longer loads, or a device that an XDP upatch selects whose driver+refuses it. :ref:`boot-restore-lockout` describes the way out.Register binding failures~~~~~~~~~~~~~~~~~~~~~~~~~diff --git a/Documentation/operate/troubleshoot/troubleshooting.rst b/Documentation/operate/troubleshoot/troubleshooting.rstindex 5f1aec27..700c67f8 100644--- a/Documentation/operate/troubleshoot/troubleshooting.rst+++ b/Documentation/operate/troubleshoot/troubleshooting.rst@@ -515,10 +515,16 @@ MeaningCausesThe archived upatch does not load on this kernel, typically after akernel upgrade. The load failure before this message carries its- category.+ category. For an XDP upatch, a line ``XDP: attach <device>`` before+ it names a device whose driver refused the program: a NIC added since+ the load, or a setting that survived the reboot, in the NIC's firmware+ or the hypervisor, such as LRO.Action- A reboot restores the same archive and fails the same way.+ A reboot restores the same archive and fails the same way. For an XDP+ device, undo the setting, or remove the device, and reboot. A device+ that cannot take XDP needs an upatch signed with a class mask that+ leaves it out.:doc:`/operate/maintain/backup-recovery` describes what works and what doesnot.diff --git a/Documentation/reference/cli/upatch-reconcile.rst b/Documentation/reference/cli/upatch-reconcile.rstindex 5bca7c83..8bf7f97a 100644--- a/Documentation/reference/cli/upatch-reconcile.rst+++ b/Documentation/reference/cli/upatch-reconcile.rst@@ -85,7 +85,8 @@ Exit status* - 0- The pass finished. A device that is occupied, has no native XDP, orchanged or went away during the pass is left for the next pass and- does not change the status.+ does not change the status. Neither does a driver refusing the+ program, but that is logged as a warning on every pass.* - 1- The pass stopped: see the category in the report.* - 2diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex 642060a1..88446b64 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -1554,8 +1554,15 @@ R21 upatches can be neutralized by changing their targetsunfiltered until then: normally seconds after its udev event, and up toone pass plus five seconds when udev gives no signal, as for a vethpeer moving namespace. A device stays uncovered while its XDP slot is- occupied, it has no native XDP, or root stops the timer and the udev- rule, and ``status`` does not list such devices. A veth end moved to another namespace+ occupied, it has no native XDP, its driver refuses the program, or+ root stops the timer and the udev rule, and ``status`` does not list+ such devices.+ At boot restore a device whose driver refuses fails ``init``: the+ register stays unbound, so attestation fails, and that upatch and every+ archive after it in name order stay unloaded that boot. A setting that+ survives a reboot, in the NIC's firmware or the hypervisor, therefore+ cannot take a device out of an upatch unseen, at the price of a host+ that boots without those upatches. 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 f62f5683..af5fc7ab 100644--- a/Documentation/upatches/write/attach-kinds.rst+++ b/Documentation/upatches/write/attach-kinds.rst@@ -302,8 +302,13 @@ Links use ``XDP_FLAGS_DRV_MODE``. Devices that refuse native XDP withchanges or goes away during attachment is skipped too. An XDP programwith no device to attach to still loads: a pin of its own, ``xdp_prog_<i>``,keeps it, and ``status`` shows it as ``xdp (waiting for a device)``. Other-errors undo the load's links, including an occupied XDP attachment point;-at boot restore an occupied device is skipped instead. Existing XDP+errors undo the load's links, including an occupied XDP attachment point+or a driver refusing the program. Boot restore fails the same way, and+``init`` stops with the register unbound. Only a device that went away+during the restore, or that an upatch restored before this one holds, is+skipped. That includes a device reconcile only warned about before the+reboot. A setting root makes after boot is the reconcile pass's to meet,+which leaves the device unattached and warns. Existing XDPprograms are never replaced. Device classification and peer identityare rechecked around attachment, and the returned link's ifindex ischecked before pinning.diff --git a/builtin-upatch.c b/builtin-upatch.cindex d9779b0f..6f362dba 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1378,23 +1378,6 @@ static int xdp_attach_one(int prog_fd, const struct prog_spec *spec,return ret;}-/*- * A device that changed or went away is not this upatch's failure. An- * occupied one is at load, where the operator can act on it, but not at- * boot, where two upatches that loaded with no device yet can meet on a- * device that has appeared since, and one failure would stop the rest of- * restore. The kernel reports a vanished ifindex as EINVAL, so ask.- */-static bool xdp_attach_skippable(int err, const struct xdp_device *dev,- bool restoring)-{- if (err >= 0)- return false;- if (err == -EBUSY || err == -EEXIST)- return restoring;- return err == -EXDEV || err == -ENODEV || xdp_device_verify(dev);-}-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, bool restoring)@@ -4455,11 +4438,12 @@ static int reconcile_attach(int prog_fd, const char *name,rep_fail(rep, "link_create", "lsm_denied", ret,"the guards refused the XDP link");return 1;+ } else if (ret < 0 && ret != -EBUSY && ret != -EEXIST &&+ !xdp_attach_skippable(ret, dev, false)) {+ pr_warn("XDP: upatch %s: attach %s (ifindex %u): %s", name,+ dev->name, dev->ifindex, strerror(-ret));}- /*- * Anything else is about this device alone: it is occupied, has no- * native XDP, or changed or went away. The next pass looks again.- */+ /* All of this is about this device alone; the next pass looks again. */return 0;}diff --git a/testing/functional/upatch/t3112-xdp-reconcile.sh b/testing/functional/upatch/t3112-xdp-reconcile.shindex 10cbbcf2..fca2f755 100644--- a/testing/functional/upatch/t3112-xdp-reconcile.sh+++ b/testing/functional/upatch/t3112-xdp-reconcile.sh@@ -128,6 +128,18 @@ test_xdp_success 'a veth end moved away keeps its link and the host end attaches[ "$("$H" query xdpb)" -gt 0 ]'+test_xdp_success 'a driver refusing one device warns and spares the others' '+ add_pair xdpa 30000 xdpb 30001 &&+ load "$NAME" &&+ add_pair xdpc 30010 xdpd 30011 &&+ ip link set xdpd mtu 9000 &&+ add_pair xdpe 30020 xdpf 30021 &&+ out=$(reconcile 2>&1) &&+ [ "$("$H" query xdpc)" = 0 ] &&+ [ "$("$H" query xdpe)" -gt 0 ] &&+ printf "%s\n" "$out" | grep -q "attach xdpc"+'+test_xdp_success 'an unload racing a pass is not tampering' 'add_pair xdpa 30000 xdpb 30001 &&(diff --git a/testing/unit/xdp.c b/testing/unit/xdp.cindex e5442572..0ac383e2 100644--- a/testing/unit/xdp.c+++ b/testing/unit/xdp.c@@ -106,6 +106,48 @@ static void reconcile_never_touches_another_program(void)EXPECT_EQ(xdp_reconcile_action(true, 9, 7), XDP_RECONCILE_OCCUPIED);}+static void restore_fails_on_a_driver_refusal(void)+{+ static const int skipped[] = { -EBUSY, -EEXIST, -EXDEV, -ENODEV };+ static const int refused[] = { -EINVAL, -ENOSPC, -ERANGE };+ /* No device has this index, so the kernel answers ENODEV. */+ struct xdp_device *devices = NULL, gone = { .ifindex = 0x7ffffff0 };+ /* Index 0 names no device, so the lookup itself fails with EINVAL. */+ struct xdp_device unaskable = {};+ size_t count = 0, i;+ int ret;++ for (i = 0; i < ARRAY_SIZE(skipped); i++)+ EXPECT_TRUE(xdp_attach_skippable(skipped[i], &gone, true));+ EXPECT_TRUE(xdp_attach_skippable(-EINVAL, &gone, true));+ EXPECT_FALSE(xdp_attach_skippable(-EINVAL, &unaskable, true));+ EXPECT_FALSE(xdp_attach_skippable(-EINVAL, &unaskable, false));+ EXPECT_FALSE(xdp_attach_skippable(-EACCES, &gone, true));+ EXPECT_FALSE(xdp_attach_skippable(-EPERM, &gone, true));+ EXPECT_FALSE(xdp_attach_skippable(0, &gone, true));+ EXPECT_FALSE(xdp_attach_skippable(1, &gone, true));++ ret = xdp_devices_read(&devices, &count);+ if (ret == -EPERM || ret == -EACCES || ret == -EAFNOSUPPORT)+ unit_skip("kernel network discovery unavailable");+ ASSERT_EQ(ret, 0);+ ASSERT_TRUE(count > 0);+ for (i = 0; i < ARRAY_SIZE(refused); i++)+ EXPECT_FALSE(xdp_attach_skippable(refused[i], &devices[0], true));+ xfree(devices, count * sizeof(*devices));+}++static void load_stops_on_an_occupied_device(void)+{+ struct xdp_device dev = {};++ EXPECT_FALSE(xdp_attach_skippable(-EBUSY, &dev, false));+ EXPECT_FALSE(xdp_attach_skippable(-EEXIST, &dev, false));+ EXPECT_TRUE(xdp_attach_skippable(-EXDEV, &dev, false));+ EXPECT_TRUE(xdp_attach_skippable(-ENODEV, &dev, false));+ EXPECT_FALSE(xdp_attach_skippable(0, &dev, false));+}+static size_t add_attr(__u8 *buf, size_t len, __u16 type,const void *value, size_t size){@@ -177,6 +219,8 @@ static const struct unit_case cases[] = {UNIT_CASE(veth_without_a_peer_is_not_selected),UNIT_CASE(unresolved_devices_sit_out),UNIT_CASE(reconcile_never_touches_another_program),+ UNIT_CASE(restore_fails_on_a_driver_refusal),+ UNIT_CASE(load_stops_on_an_occupied_device),UNIT_CASE(link_messages_are_checked),UNIT_CASE(kernel_discovery_revalidates_devices),{}diff --git a/util/xdp.c b/util/xdp.cindex 9cc7528c..83fcbc61 100644--- a/util/xdp.c+++ b/util/xdp.c@@ -545,6 +545,27 @@ int xdp_devices_read(struct xdp_device **out, size_t *count)return 0;}+/*+ * A driver refusal fails a restore as it fails a load: a setting that+ * survives a reboot must not take a device out of the upatch unreported.+ * Only an upatch restored before this one can hold the device then. The+ * kernel reports a vanished ifindex as EINVAL, so ask, and take only an+ * answer that the device is gone or changed: a lookup that itself fails+ * says nothing about the refusal.+ */+bool xdp_attach_skippable(int err, const struct xdp_device *dev,+ bool restoring)+{+ if (err >= 0 || err == -EACCES || err == -EPERM)+ return false;+ if (err == -EBUSY || err == -EEXIST)+ return restoring;+ if (err == -EXDEV || err == -ENODEV)+ return true;+ err = xdp_device_verify(dev);+ return err == -EXDEV || err == -ENODEV;+}+int xdp_device_verify(const struct xdp_device *expected){struct nl_request req = {diff --git a/util/xdp.h b/util/xdp.hindex 4ac7f66e..da3d42e4 100644--- a/util/xdp.h+++ b/util/xdp.h@@ -39,6 +39,10 @@ enum xdp_reconcile {enum xdp_reconcile xdp_reconcile_action(bool selected, __u32 attached,__u32 prog_id);+/* Whether a failed attach is left for a later pass rather than fatal. */+bool xdp_attach_skippable(int err, const struct xdp_device *dev,+ bool restoring);+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,