eBPF-based security platform
xdp: require multi-buffer support on attached devices
John Fastabend02b790ef949e
diff --git a/Documentation/internals/kernel-interfaces.rst b/Documentation/internals/kernel-interfaces.rstindex 07fd2764..27e77cfe 100644--- a/Documentation/internals/kernel-interfaces.rst+++ b/Documentation/internals/kernel-interfaces.rst@@ -70,7 +70,12 @@ XDP uses ``BPF_LINK_CREATE`` with ``attach_type=BPF_XDP``,shirudo verifies ``BPF_LINK_TYPE_XDP`` and the returned ``xdp.ifindex``before pinning. Discovery uses kernel ``RTM_GETLINK`` replies,``ETHTOOL_GDRVINFO`` and devlink ``DEVLINK_CMD_PORT_GET``; it does not-read sysfs to classify devices. The ``lsm/bpf_prog`` guard refuses XDP+read sysfs to classify devices. Before attaching, shirudo reads+``NETDEV_A_DEV_XDP_FEATURES`` with ``NETDEV_CMD_DEV_GET`` on the ``netdev``+generic netlink family and refuses a device that reports+``NETDEV_XDP_ACT_BASIC`` without ``NETDEV_XDP_ACT_RX_SG``, as the kernel+never compares a program's multi-buffer flag with the device's. The+``lsm/bpf_prog`` guard refuses XDPdescriptors to unauthorized callers, including ``BPF_PROG_GET_FD_BY_ID``,because netlink accepts these descriptors outside ``bpf()``.diff --git a/Documentation/operate/troubleshoot/troubleshooting.rst b/Documentation/operate/troubleshoot/troubleshooting.rstindex 700c67f8..7f80e894 100644--- a/Documentation/operate/troubleshoot/troubleshooting.rst+++ b/Documentation/operate/troubleshoot/troubleshooting.rst@@ -518,7 +518,7 @@ Causescategory. For an XDP upatch, a line ``XDP: attach <device>`` beforeit names a device whose driver refused the program: a NIC added sincethe load, or a setting that survived the reboot, in the NIC's firmware- or the hypervisor, such as LRO.+ or the hypervisor, such as LRO or a device without multi-buffer XDP.ActionA reboot restores the same archive and fails the same way. For an XDPdiff --git a/Documentation/reference/cli/upatch-load.rst b/Documentation/reference/cli/upatch-load.rstindex 99bac1e8..00e62d38 100644--- a/Documentation/reference/cli/upatch-load.rst+++ b/Documentation/reference/cli/upatch-load.rst@@ -92,7 +92,8 @@ Each program can produce several ``link_<n>`` pins. Native XDP isrequired: unsupported devices are logged and skipped. An upatch with noattachable device yet still loads, and:doc:`/reference/cli/upatch-reconcile` attaches it when one appears. A-device that is occupied, and any other error, undoes all links the load+device that is occupied or does not report multi-buffer XDP, and any other+error, undoes all links the loadcreated. :doc:`/upatches/write/attach-kinds` defines the classes and limits.Options@@ -196,7 +197,8 @@ Exit status and failure categories``eventlog_full``, ``already_loaded``, ``parser_rejected``,``signature_rejected``, ``signature_unsupported``, ``lsm_denied``,``verifier_rejected``, ``attach_target_missing``, ``binary_mismatch``,-``attach_scope_mismatch``, ``tpm_error`` and ``unknown``.+``attach_scope_mismatch``, ``xdp_multibuf_unsupported``, ``tpm_error`` and+``unknown``.:doc:`/reference/failure-categories` gives the cause and the action foreach. Handle ``tampered`` as an incident(:doc:`/operate/troubleshoot/incident-tampered`).diff --git a/Documentation/reference/cli/upatch-reconcile.rst b/Documentation/reference/cli/upatch-reconcile.rstindex 8bf7f97a..4d1b64a6 100644--- a/Documentation/reference/cli/upatch-reconcile.rst+++ b/Documentation/reference/cli/upatch-reconcile.rst@@ -86,7 +86,8 @@ Exit status- 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 anddoes not change the status. Neither does a driver refusing the- program, but that is logged as a warning on every pass.+ program or a device without multi-buffer XDP, but those are logged+ as a warning on every pass.* - 1- The pass stopped: see the category in the report.* - 2diff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rstindex 382477b5..0ad3f8ff 100644--- a/Documentation/reference/failure-categories.rst+++ b/Documentation/reference/failure-categories.rst@@ -142,6 +142,14 @@ Categories``upatch load`` exits 2. ``bpm tree sync`` records the operation as``skipped: module not loaded`` and continues.- Load the module or install the binary, then load again.+ * - ``xdp_multibuf_unsupported``+ - ``link_create``+ - ``EMEDIUMTYPE``+ - A selected device has native XDP but does not report multi-buffer+ support (``NETDEV_XDP_ACT_RX_SG``). The load was undone.+ - Narrow the signed device-class mask, or configure or change the+ device so that it reports multi-buffer XDP. See+ :doc:`/upatches/write/attach-kinds`.* - ``binary_mismatch``- ``target_verify``, ``link_create``- ``ESTALE``diff --git a/builtin-upatch.c b/builtin-upatch.cindex 6f362dba..25ccb48b 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1359,11 +1359,18 @@ static int xdp_attach_one(int prog_fd, const struct prog_spec *spec,const struct xdp_device *dev, const char *pin){LIBBPF_OPTS(bpf_link_create_opts, opts, .flags = XDP_FLAGS_DRV_MODE);+ __u64 features = 0;int fd, ret;ret = xdp_device_verify(dev);if (ret)return ret;+ /* Before the attach: it can reconfigure a NIC, and reconcile retries. */+ ret = xdp_device_features(dev->ifindex, &features);+ if (ret)+ return ret;+ if (xdp_multibuf_refused(features))+ return XDP_ERR_NO_MULTIBUF;fd = bpf_link_create(prog_fd, (int)dev->ifindex, BPF_XDP, &opts);if (fd == -EOPNOTSUPP)return XDP_ATTACH_UNSUPPORTED;@@ -1404,12 +1411,12 @@ static int attach_xdp_devices(int prog_fd, const struct prog_spec *spec,}if (xdp_attach_skippable(ret, dev, restoring)) {pr_warn("XDP: skipping %s (ifindex %u): %s", dev->name,- dev->ifindex, strerror(-ret));+ dev->ifindex, xdp_attach_strerror(ret));continue;}if (ret < 0) {pr_err("XDP: attach %s (ifindex %u): %s", dev->name,- dev->ifindex, strerror(-ret));+ dev->ifindex, xdp_attach_strerror(ret));return ret;}(*pinned)++;@@ -2304,6 +2311,8 @@ static int __upatch_apply(const char *path, bool verify_only,cat = "attach_target_missing";else if (e == ESTALE)cat = "binary_mismatch";+ else if (e == EMEDIUMTYPE)+ cat = "xdp_multibuf_unsupported";else if (e == EXDEV)cat = "attach_scope_mismatch";else if (e == EOPNOTSUPP)@@ -4441,7 +4450,7 @@ static int reconcile_attach(int prog_fd, const char *name,} 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));+ dev->name, dev->ifindex, xdp_attach_strerror(ret));}/* All of this is about this device alone; the next pass looks again. */return 0;diff --git a/testing/unit/xdp.c b/testing/unit/xdp.cindex 91d7b3bb..29573cfe 100644--- a/testing/unit/xdp.c+++ b/testing/unit/xdp.c@@ -4,6 +4,8 @@#include <errno.h>#include <net/if.h>#include <linux/devlink.h>+#include <linux/genetlink.h>+#include <linux/netdev.h>#include <linux/rtnetlink.h>#include <shirudo/upatch.h>@@ -121,7 +123,9 @@ static void reconcile_never_touches_another_program(void)static void restore_fails_on_a_driver_refusal(void){static const int skipped[] = { -EBUSY, -EEXIST, -EXDEV, -ENODEV };- static const int refused[] = { -EINVAL, -ENOSPC, -ERANGE };+ static const int refused[] = {+ -EINVAL, -ENOSPC, -ERANGE, XDP_ERR_NO_MULTIBUF,+ };/* 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. */@@ -160,6 +164,21 @@ static void load_stops_on_an_occupied_device(void)EXPECT_FALSE(xdp_attach_skippable(0, &dev, false));}+static void multibuf_is_required_of_native_xdp_alone(void)+{+ const __u64 basic = NETDEV_XDP_ACT_BASIC;+ const __u64 sg = NETDEV_XDP_ACT_RX_SG;++ EXPECT_TRUE(xdp_multibuf_refused(basic));+ EXPECT_TRUE(xdp_multibuf_refused(basic | NETDEV_XDP_ACT_REDIRECT));+ EXPECT_FALSE(xdp_multibuf_refused(basic | sg));+ EXPECT_FALSE(xdp_multibuf_refused(0));+ EXPECT_FALSE(xdp_multibuf_refused(sg));+ EXPECT_STREQ(xdp_attach_strerror(XDP_ERR_NO_MULTIBUF),+ "the device does not report multi-buffer XDP support");+ EXPECT_STREQ(xdp_attach_strerror(-EINVAL), strerror(EINVAL));+}+static size_t add_attr(__u8 *buf, size_t len, __u16 type,const void *value, size_t size){@@ -206,6 +225,25 @@ static void link_messages_are_checked(void)EXPECT_EQ(xdp_device_parse(buf, len, &dev), -EINVAL);}+static void device_features_are_checked(void)+{+ __u64 value = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_RX_SG, out = 0;+ __u32 ifindex = 7, small = 1;+ __u8 buf[64] = {};+ size_t len;++ len = add_attr(buf, GENL_HDRLEN, NETDEV_A_DEV_IFINDEX, &ifindex,+ sizeof(ifindex));+ EXPECT_EQ(xdp_features_parse(buf, len, &out), -EINVAL);+ EXPECT_EQ(xdp_features_parse(buf, GENL_HDRLEN - 1, &out), -EINVAL);+ len = add_attr(buf, len, NETDEV_A_DEV_XDP_FEATURES, &value, sizeof(value));+ ASSERT_EQ(xdp_features_parse(buf, len, &out), 0);+ EXPECT_UEQ(out, value);+ len = add_attr(buf, GENL_HDRLEN, NETDEV_A_DEV_XDP_FEATURES, &small,+ sizeof(small));+ EXPECT_EQ(xdp_features_parse(buf, len, &out), -EINVAL);+}+static void kernel_discovery_revalidates_devices(void){struct xdp_device *devices = NULL;@@ -234,6 +272,8 @@ static const struct unit_case cases[] = {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(multibuf_is_required_of_native_xdp_alone),+ UNIT_CASE(device_features_are_checked),UNIT_CASE(link_messages_are_checked),UNIT_CASE(kernel_discovery_revalidates_devices),{}diff --git a/util/report.h b/util/report.hindex cdce91de..eae1c9e5 100644--- a/util/report.h+++ b/util/report.h@@ -22,7 +22,7 @@* Category strings: ok, tampered, keyring_missing, lsm_denied,* signature_rejected, verifier_rejected, parser_rejected,* payload_mismatch, already_loaded, already_unloaded, detach_failed,- * tpm_error, not_initialized, unknown.+ * tpm_error, not_initialized, xdp_multibuf_unsupported, unknown.*/struct op_report {bool ok;diff --git a/util/xdp.c b/util/xdp.cindex 81d56136..9d5c8da4 100644--- a/util/xdp.c+++ b/util/xdp.c@@ -13,6 +13,7 @@#include <linux/devlink.h>#include <linux/ethtool.h>#include <linux/genetlink.h>+#include <linux/netdev.h>#include <linux/netlink.h>#include <linux/rtnetlink.h>#include <linux/sockios.h>@@ -390,7 +391,7 @@ static int receive_family(const void *data, size_t len, void *ctx)CTRL_ATTR_FAMILY_ID, ctx, sizeof(__u16), NULL);}-static int devlink_family(__u16 *id)+static int genl_family(const char *name, __u16 *id){struct nl_request req = {.hdr.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN),@@ -401,8 +402,7 @@ static int devlink_family(__u16 *id)int ret;memcpy(req.data, &genl, sizeof(genl));- ret = attr_put(&req, CTRL_ATTR_FAMILY_NAME, DEVLINK_GENL_NAME,- sizeof(DEVLINK_GENL_NAME));+ ret = attr_put(&req, CTRL_ATTR_FAMILY_NAME, name, strlen(name) + 1);return ret ? ret : nl_exchange(NETLINK_GENERIC, &req, receive_family, id);}@@ -459,7 +459,7 @@ static int device_details(struct xdp_device *dev)};__u16 family = 0;- ret = devlink_family(&family);+ ret = genl_family(DEVLINK_GENL_NAME, &family);if (ret || !family)return ret ? ret : -EINVAL;req.hdr.nlmsg_type = family;@@ -480,6 +480,63 @@ static int device_details(struct xdp_device *dev)return 0;}+int xdp_features_parse(const void *data, size_t len, __u64 *features)+{+ bool present = false;+ int ret;++ if (len < GENL_HDRLEN)+ return -EINVAL;+ ret = attr_number((const __u8 *)data + GENL_HDRLEN, len - GENL_HDRLEN,+ NETDEV_A_DEV_XDP_FEATURES, features,+ sizeof(*features), &present);+ return ret ? ret : present ? 0 : -EINVAL;+}++static int receive_features(const void *data, size_t len, void *ctx)+{+ return xdp_features_parse(data, len, ctx);+}++int xdp_device_features(__u32 ifindex, __u64 *features)+{+ struct nl_request req = {+ .hdr.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN),+ .hdr.nlmsg_flags = NLM_F_REQUEST,+ };+ struct genlmsghdr genl = {+ .cmd = NETDEV_CMD_DEV_GET,+ .version = NETDEV_FAMILY_VERSION,+ };+ __u16 family = 0;+ int ret;++ /* -ENOENT would read as a missing attach target and be skipped. */+ ret = genl_family(NETDEV_FAMILY_NAME, &family);+ if (ret == -ENOENT)+ return -EOPNOTSUPP;+ if (ret || !family)+ return ret ? ret : -EINVAL;+ req.hdr.nlmsg_type = family;+ memcpy(req.data, &genl, sizeof(genl));+ ret = attr_put(&req, NETDEV_A_DEV_IFINDEX, &ifindex, sizeof(ifindex));+ return ret ? ret : nl_exchange(NETLINK_GENERIC, &req, receive_features,+ features);+}++bool xdp_multibuf_refused(__u64 features)+{+ return (features & NETDEV_XDP_ACT_BASIC) &&+ !(features & NETDEV_XDP_ACT_RX_SG);+}++const char *xdp_attach_strerror(int err)+{+ if (err == XDP_ERR_NO_MULTIBUF)+ return "the device does not report multi-buffer XDP support";+ return strerror(-err);+}+static int device_compare(const void *a, const void *b){const struct xdp_device *left = a, *right = b;diff --git a/util/xdp.h b/util/xdp.hindex da3d42e4..b15c249f 100644--- a/util/xdp.h+++ b/util/xdp.h@@ -4,6 +4,7 @@#ifndef SHIRUDO_XDP_H#define SHIRUDO_XDP_H+#include <errno.h>#include <stdbool.h>#include <stddef.h>@@ -11,6 +12,9 @@#define XDP_IFNAME_MAX 16+/* Every XDP program is loaded with frags, so the device must report them. */+#define XDP_ERR_NO_MULTIBUF (-EMEDIUMTYPE)+struct xdp_device {char devlink_device[128];char devlink_bus[128];@@ -49,5 +53,11 @@ bool xdp_device_selected(const struct xdp_device *devs, size_t count,size_t index, __u32 mask);int xdp_devices_read(struct xdp_device **out, 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);++/* Native XDP without multi-buffer; a device with neither fails on its own. */+bool xdp_multibuf_refused(__u64 features);+const char *xdp_attach_strerror(int err);#endif /* SHIRUDO_XDP_H */