Skip to content

eBPF-based security platform

xdp: fail on a link made for another device or namespace

John Fastabend646078638273
diff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rst
index fa2401b0..740bca36 100644
--- a/Documentation/reference/failure-categories.rst
+++ b/Documentation/reference/failure-categories.rst
@@ -197,12 +197,14 @@ Categories
:doc:`/upatches/test/debugging`.
* - ``attach_scope_mismatch``
- ``link_create``
- - ``EXDEV``
+ - ``EXDEV``, ``EREMOTE`` for XDP
- The link the kernel reports is not at the site the attach
metadata names: a cgroup link not at the root cgroup, a netfilter
link outside the initial network namespace or at another hook or
priority, a kprobe at another function or offset.
- For XDP, a load outside the initial network namespace.
+ For XDP, a load outside the initial network namespace, or a link
+ the kernel made for another device; that also fails boot restore,
+ and stops an ``upatch reconcile`` pass.
- Run the load in the host's initial cgroup and network namespaces
and with the host's sysfs.
* - ``already_loaded``
diff --git a/Documentation/upatches/write/attach-kinds.rst b/Documentation/upatches/write/attach-kinds.rst
index 653a5549..7e7e48f4 100644
--- a/Documentation/upatches/write/attach-kinds.rst
+++ b/Documentation/upatches/write/attach-kinds.rst
@@ -326,7 +326,9 @@ 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 XDP
programs are never replaced. Device classification and peer identity
are rechecked around attachment, and the returned link's ifindex is
-checked before pinning.
+checked before pinning. A link on another device or outside the initial
+namespace is ``attach_scope_mismatch`` and fails the load, the restore
+or the reconcile pass, never a skipped device.
Every XDP program is loaded with ``BPF_F_XDP_HAS_FRAGS``, so it can run on
a device whose MTU does not fit one buffer. A selected device with native
diff --git a/builtin-upatch.c b/builtin-upatch.c
index 8baf7c7a..e673e9b3 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -1379,6 +1379,9 @@ static int xdp_attach_one(int prog_fd, const struct prog_spec *spec,
if (fd < 0)
return fd;
ret = link_matches_spec(fd, spec, dev->ifindex);
+ /* Not to be read as xdp_device_verify()'s "the device changed". */
+ if (ret == -EXDEV)
+ ret = XDP_ERR_LINK_SCOPE;
if (!ret)
ret = xdp_device_verify(dev);
if (!ret)
@@ -2316,7 +2319,7 @@ static int __upatch_apply(const char *path, bool verify_only, bool restoring,
cat = "xdp_multibuf_unsupported";
else if (e == ENOPROTOOPT)
cat = "xdp_driver_refused";
- else if (e == EXDEV)
+ else if (e == EXDEV || e == EREMOTE)
cat = "attach_scope_mismatch";
else if (e == EOPNOTSUPP)
cat = "kernel_abi_unsupported";
@@ -4403,6 +4406,10 @@ 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 == XDP_ERR_LINK_SCOPE) {
+ rep_fail(rep, "link_create", "attach_scope_mismatch", ret,
+ xdp_attach_strerror(ret));
+ 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,
diff --git a/testing/unit/xdp.c b/testing/unit/xdp.c
index 9f59f35d..37c4b049 100644
--- a/testing/unit/xdp.c
+++ b/testing/unit/xdp.c
@@ -125,7 +125,7 @@ static void restore_fails_on_a_driver_refusal(void)
static const int skipped[] = { -EBUSY, -EEXIST, -EXDEV, -ENODEV };
static const int refused[] = {
-EINVAL, -ENOSPC, -ERANGE, XDP_ERR_NO_MULTIBUF,
- XDP_ERR_DRIVER_SETTINGS,
+ XDP_ERR_DRIVER_SETTINGS, XDP_ERR_LINK_SCOPE,
};
/* No device has this index, so the kernel answers ENODEV. */
struct xdp_device *devices = NULL, gone = { .ifindex = 0x7ffffff0 };
@@ -139,6 +139,8 @@ static void restore_fails_on_a_driver_refusal(void)
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));
@@ -190,6 +192,8 @@ static void only_a_device_without_xdp_lacks_it(void)
EXPECT_FALSE(xdp_attach_unsupported(0, 0));
EXPECT_STREQ(xdp_attach_strerror(XDP_ERR_DRIVER_SETTINGS),
"the driver refuses XDP in its current settings");
+ EXPECT_STREQ(xdp_attach_strerror(XDP_ERR_LINK_SCOPE),
+ "the link is not on the device and namespace it was made for");
}
static size_t add_attr(__u8 *buf, size_t len, __u16 type,
diff --git a/util/xdp.c b/util/xdp.c
index 4ec419a6..a7e45bb1 100644
--- a/util/xdp.c
+++ b/util/xdp.c
@@ -542,6 +542,8 @@ const char *xdp_attach_strerror(int err)
return "the device does not report multi-buffer XDP support";
if (err == XDP_ERR_DRIVER_SETTINGS)
return "the driver refuses XDP in its current settings";
+ if (err == XDP_ERR_LINK_SCOPE)
+ return "the link is not on the device and namespace it was made for";
return strerror(-err);
}
@@ -636,7 +638,8 @@ int xdp_devices_read(struct xdp_device **out, size_t *count)
bool xdp_attach_skippable(int err, const struct xdp_device *dev,
bool restoring)
{
- if (err >= 0 || err == -EACCES || err == -EPERM)
+ if (err >= 0 || err == -EACCES || err == -EPERM ||
+ err == XDP_ERR_LINK_SCOPE)
return false;
if (err == -EBUSY || err == -EEXIST)
return restoring;
diff --git a/util/xdp.h b/util/xdp.h
index 7d801859..9f175941 100644
--- a/util/xdp.h
+++ b/util/xdp.h
@@ -18,6 +18,9 @@
/* A driver with native XDP that rules it out in its current settings. */
#define XDP_ERR_DRIVER_SETTINGS (-ENOPROTOOPT)
+/* The kernel made the link for another device or namespace. */
+#define XDP_ERR_LINK_SCOPE (-EREMOTE)
+
struct xdp_device {
char devlink_device[128];
char devlink_bus[128];