eBPF-based security platform
xdp: fail a boot restore on an occupied device
John Fastabendf9bd89f5353c
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 420804d5..af31d9e1 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -210,7 +210,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, does not read a gone device as tampering, removes its+ occupies, without a warning, while ``status`` lists it under+ ``XdpCovered``, and attaches it once that program is unloaded, does not read a gone device as tampering, removes itspin 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@@ -844,7 +845,9 @@ Most of these attacks are open threats inwith ``CONFIG_LSM="bpf"`` alone.- ``bpm config update --base-package`` running commands from a packagefetched from a registry.-- A boot restore that fails part way.+- A boot restore that fails part way, among them one that finds a+ device another XDP program holds: only a reboot runs the restore.+ ``t3111`` covers the load, which attaches through the same code.- Loading an older artifact signed by the same key, and an unloaddirective signed by another trusted key than the load.- Stopping a running shirudo with ``SIGSTOP``, ``mount_setattr(2)``,diff --git a/Documentation/operate/run/read-status.rst b/Documentation/operate/run/read-status.rstindex 9873bf9d..e76ef47e 100644--- a/Documentation/operate/run/read-status.rst+++ b/Documentation/operate/run/read-status.rst@@ -672,6 +672,12 @@ program, and the next ``upatch reconcile`` attaches it. Where thedevice cannot run XDP at all, sign the upatch with a class mask thatleaves the device out.+A device listed as ``another XDP program attached`` because a second+loaded upatch holds it does not wait for a reconcile pass alone: the+next boot restore fails on it, ``init`` stops with the register unbound+and the host comes up ``reboot-pending``. Unload one of the two upatches+before the next reboot.+A device that stays listed also costs the target work on everyreconcile pass. The pass takes the register lock, checks the archiveagain, retries the attach and logs a warning, every 60 seconds and ondiff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex 60448365..d79b0349 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -1559,12 +1559,13 @@ R21 upatches can be neutralized by changing their targetsdevice under ``XdpCovered`` with the reason the device reports andgives the verdict ``degraded``, which the keepalive carries; rootcan stop the keepalive as well, which its collector sees as silence.- 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 namespace+ At boot restore a device whose driver refuses, or that an upatch+ restored before holds, fails ``init``: the register stays unbound, so+ attestation fails, and that upatch and every archive after it in name+ order stay unloaded that boot. Neither a setting that survives a+ reboot, in the NIC's firmware or the hypervisor, nor a second upatch+ that 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 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 417343ae..dd0ca937 100644--- a/Documentation/upatches/write/attach-kinds.rst+++ b/Documentation/upatches/write/attach-kinds.rst@@ -257,9 +257,11 @@ one upatch must select disjoint classes: ``shirudo build`` refuses``SEC("xdp/3")`` beside ``SEC("xdp/2")``. ``bpm tree append`` refuses anupatch that selects a class another upatch of the tree selects on thesame nodes. Two upatches loaded another way 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``.+device. While such a device is there, the second one's ``upatch load``+fails. When the device appears after both loaded, reconcile attaches the+first, ``status`` lists the device for the second under ``XdpCovered``+as ``another XDP program attached``, and the next boot restore fails on+it... list-table:::header-rows: 1@@ -322,9 +324,9 @@ keeps it, and ``status`` shows it as ``xdp (waiting for a device)``. Othererrors undo the load's links, including an occupied XDP attachment pointor 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,+during the restore is skipped. A device that an upatch restored before+this one holds fails the restore like any other, also one that 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, warns, and lets ``status`` list it. Existing XDPprograms are never replaced. Device classification and peer identityare rechecked around attachment, and the returned link's ifindex isdiff --git a/builtin-upatch.c b/builtin-upatch.cindex e6f0a0d2..cd371e40 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1394,7 +1394,7 @@ static int xdp_attach_one(int prog_fd, const struct prog_spec *spec,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)+ const char *dir, __u32 *pinned){__u32 attached = 0;size_t i;@@ -1416,7 +1416,7 @@ static int attach_xdp_devices(int prog_fd, const struct prog_spec *spec,dev->name, dev->ifindex, xdp_device_class(dev));continue;}- if (xdp_attach_skippable(ret, dev, restoring)) {+ if (xdp_attach_skippable(ret, dev)) {pr_warn("XDP: skipping %s (ifindex %u): %s", dev->name,dev->ifindex, xdp_attach_strerror(ret));continue;@@ -1445,7 +1445,7 @@ static int attach_xdp_devices(int prog_fd, const struct prog_spec *spec,static int attach_and_pin_progs(const struct loader_ctx *cx,size_t prog_desc_off, __u32 nr_progs,const char *name,- const struct prog_spec *specs, bool restoring)+ const struct prog_spec *specs){char dir[PATH_MAX], serializers_dir[PATH_MAX + 16];bool made_dir = false, made_serializers = false;@@ -1571,7 +1571,7 @@ static int attach_and_pin_progs(const struct loader_ctx *cx,goto out;made[i] = true;ret = attach_xdp_devices(inner_fd, spec, devices, nr_devices,- dir, &pinned_links, restoring);+ dir, &pinned_links);if (ret)goto out;continue;@@ -1920,7 +1920,7 @@ static void apply_policy_labels(const struct upatch *u)}/* Shared by `upatch load` and boot restore: one signature-verified path. */-static int __upatch_apply(const char *path, bool verify_only, bool restoring,+static int __upatch_apply(const char *path, bool verify_only,struct op_report *rep){struct upatch u;@@ -2308,7 +2308,7 @@ static int __upatch_apply(const char *path, bool verify_only, bool restoring,* exits.*/ret = attach_and_pin_progs(cx, prog_desc_off, u.hdr.nr_progs,- upatch_name, prog_specs, restoring);+ upatch_name, prog_specs);if (ret) {int e = -ret;const char *cat;@@ -2369,7 +2369,7 @@ out:int upatch_restore(const char *path){- return __upatch_apply(path, false, true, NULL);+ return __upatch_apply(path, false, NULL);}/*@@ -3053,7 +3053,7 @@ static int upatch_load(const struct load_args *args)goto done;}- if (__upatch_apply(file, false, false, &rep) != 0)+ if (__upatch_apply(file, false, &rep) != 0)goto done;if (!have_id) { /* loaded but couldn't read identity */ret = 0;@@ -3242,7 +3242,7 @@ static int cmd_upatch_unload(int argc, const char **argv)if (undo_unmeasured(&rep, "unload"))goto done;/* A bad signature fails here, before anything is removed. */- if (__upatch_apply(file, true, false, &rep) != 0) {+ if (__upatch_apply(file, true, &rep) != 0) {pr_err("upatch unload: signature verification failed; refusing");goto done;}@@ -4413,7 +4413,7 @@ static int reconcile_attach(int prog_fd, const char *name,xdp_attach_strerror(ret));return 1;} else if (ret < 0 && ret != XDP_ERR_OCCUPIED &&- !xdp_attach_skippable(ret, dev, false)) {+ !xdp_attach_skippable(ret, dev)) {pr_warn("XDP: upatch %s: attach %s (ifindex %u): %s", name,dev->name, dev->ifindex, xdp_attach_strerror(ret));}diff --git a/testing/functional/upatch/t3112-xdp-reconcile.sh b/testing/functional/upatch/t3112-xdp-reconcile.shindex 5b5916ac..2db991b2 100644--- a/testing/functional/upatch/t3112-xdp-reconcile.sh+++ b/testing/functional/upatch/t3112-xdp-reconcile.sh@@ -90,8 +90,17 @@ test_xdp_success 'a device another program occupies is left alone' 'add_pair xdpc 30010 xdpd 30011 &&load "$CONFLICT" &&id=$("$H" query xdpc) && [ "$id" -gt 0 ] &&+ out=$(reconcile 2>&1) &&+ [ "$("$H" query xdpc)" = "$id" ] &&+ ! printf "%s\n" "$out" | grep -q "attach xdpc" &&+ ! ls "$B/$NAME" | grep -q "^link_" &&+ "$SHIRUDO_BIN" status |+ grep -q "XdpCovered *WARN .*$NAME on xdpc (another XDP program attached)" &&+ unload "$CONFLICT" &&reconcile &&- [ "$("$H" query xdpc)" = "$id" ]+ [ -e "$B/$NAME/link_0" ] &&+ [ "$("$H" query xdpc)" -gt 0 ] &&+ ! "$SHIRUDO_BIN" status | grep -q "XdpCovered *WARN"'test_xdp_success 'a gone device loses its pin and no live index is reused' 'diff --git a/testing/unit/xdp.c b/testing/unit/xdp.cindex 97e51927..5ea733b6 100644--- a/testing/unit/xdp.c+++ b/testing/unit/xdp.c@@ -120,12 +120,13 @@ 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 void attach_fails_on_a_refusal_of_a_device_still_there(void){- static const int skipped[] = { XDP_ERR_OCCUPIED, -EXDEV, -ENODEV };+ static const int skipped[] = { -EXDEV, -ENODEV };static const int refused[] = {-EINVAL, -ENOSPC, -ERANGE, XDP_ERR_NO_MULTIBUF,XDP_ERR_DRIVER_SETTINGS, XDP_ERR_LINK_SCOPE, -EEXIST,+ XDP_ERR_OCCUPIED,};/* No device has this index, so the kernel answers ENODEV. */struct xdp_device *devices = NULL, gone = { .ifindex = 0x7ffffff0 };@@ -135,16 +136,14 @@ static void restore_fails_on_a_driver_refusal(void)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(XDP_ERR_LINK_SCOPE, &gone, true));- EXPECT_FALSE(xdp_attach_skippable(XDP_ERR_LINK_SCOPE, &gone, 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));+ EXPECT_TRUE(xdp_attach_skippable(skipped[i], &gone));+ EXPECT_TRUE(xdp_attach_skippable(-EINVAL, &gone));+ EXPECT_FALSE(xdp_attach_skippable(-EINVAL, &unaskable));+ EXPECT_FALSE(xdp_attach_skippable(XDP_ERR_LINK_SCOPE, &gone));+ EXPECT_FALSE(xdp_attach_skippable(-EACCES, &gone));+ EXPECT_FALSE(xdp_attach_skippable(-EPERM, &gone));+ EXPECT_FALSE(xdp_attach_skippable(0, &gone));+ EXPECT_FALSE(xdp_attach_skippable(1, &gone));ret = xdp_devices_read(&devices, &count);if (ret == -EPERM || ret == -EACCES || ret == -EAFNOSUPPORT)@@ -152,19 +151,21 @@ static void restore_fails_on_a_driver_refusal(void)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));+ EXPECT_FALSE(xdp_attach_skippable(refused[i], &devices[0]));xfree(devices, count * sizeof(*devices));}-static void load_stops_on_an_occupied_device(void)+/* Gone or not, a device that answers busy runs another program. */+static void attach_stops_on_an_occupied_device(void){- struct xdp_device dev = {};-- EXPECT_FALSE(xdp_attach_skippable(XDP_ERR_OCCUPIED, &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));+ struct xdp_device dev = {}, gone = { .ifindex = 0x7ffffff0 };++ EXPECT_FALSE(xdp_attach_skippable(XDP_ERR_OCCUPIED, &dev));+ EXPECT_FALSE(xdp_attach_skippable(XDP_ERR_OCCUPIED, &gone));+ EXPECT_FALSE(xdp_attach_skippable(-EEXIST, &dev));+ EXPECT_TRUE(xdp_attach_skippable(-EXDEV, &dev));+ EXPECT_TRUE(xdp_attach_skippable(-ENODEV, &dev));+ EXPECT_FALSE(xdp_attach_skippable(0, &dev));}static void multibuf_is_required_of_native_xdp_alone(void)@@ -299,8 +300,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(attach_fails_on_a_refusal_of_a_device_still_there),+ UNIT_CASE(attach_stops_on_an_occupied_device),UNIT_CASE(multibuf_is_required_of_native_xdp_alone),UNIT_CASE(uncovered_devices_say_why),UNIT_CASE(only_a_device_without_xdp_lacks_it),diff --git a/util/upatch.h b/util/upatch.hindex 011e0087..979cda28 100644--- a/util/upatch.h+++ b/util/upatch.h@@ -174,8 +174,7 @@ __u32 prog_specs_xdp_classes(const struct prog_spec *specs, __u32 nr_progs);/** The target's upatch lifecycle, which builtin-upatch.c implements for- * init, bpm and keys: boot restore, install, unload. A restore leaves a- * device that an upatch restored before it holds, where a load fails.+ * init, bpm and keys: boot restore, install, unload.*/int upatch_restore(const char *path);int upatch_install(const char *path);diff --git a/util/xdp.c b/util/xdp.cindex 4e2497dd..eaca3ed8 100644--- a/util/xdp.c+++ b/util/xdp.c@@ -628,21 +628,18 @@ int xdp_devices_read(struct xdp_device **out, size_t *count)}/*- * 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.+ * A driver refusal or an occupied device fails a restore as it fails a+ * load: neither a setting that survives a reboot nor another program+ * may take a device out of the upatch unreported. 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)+bool xdp_attach_skippable(int err, const struct xdp_device *dev){if (err >= 0 || err == -EACCES || err == -EPERM ||- err == XDP_ERR_LINK_SCOPE)+ err == XDP_ERR_LINK_SCOPE || err == XDP_ERR_OCCUPIED)return false;- if (err == XDP_ERR_OCCUPIED)- return restoring;if (err == -EXDEV || err == -ENODEV)return true;err = xdp_device_verify(dev);diff --git a/util/xdp.h b/util/xdp.hindex ac2f30fd..8f723104 100644--- a/util/xdp.h+++ b/util/xdp.h@@ -56,8 +56,7 @@ 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);+bool xdp_attach_skippable(int err, const struct xdp_device *dev);int xdp_device_parse(const void *data, size_t len, struct xdp_device *out);__u32 xdp_device_class(const struct xdp_device *dev);