eBPF-based security platform
status: check XDP coverage only in the initial network namespace
John Fastabend7f799b02f424
diff --git a/Documentation/operate/run/read-status.rst b/Documentation/operate/run/read-status.rstindex 6666c634..9434d7e7 100644--- a/Documentation/operate/run/read-status.rst+++ b/Documentation/operate/run/read-status.rst@@ -268,7 +268,10 @@ verdict is derived from them.refused the program, or a setting such as LRO turned its XDP off,or the reconcile units are stopped. ``WARN`` ``XDP coverage notchecked: <error>`` means the check could not run, or could not ask- a device what it runs; such a device may be unfiltered.+ a device what it runs; such a device may be unfiltered. ``XDP+ coverage not checked: not in the initial network namespace`` is a+ ``status`` run in a container or another namespace, whose devices+ are not the host's: run it in the host's namespace.* - ``ConfigActive``- The config PCR, the activation records and the mounted configimage agree. ``WARN`` ``the activation of config <root>.. isdiff --git a/builtin-upatch.c b/builtin-upatch.cindex 619cacfa..146afaae 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1176,31 +1176,8 @@ static int prog_attach_for_spec(int prog_fd,}}-/* The initial network namespace's cookie, NET_NS_INIT_ID in linux/nsfs.h. */-#define NET_NS_INIT_COOKIE 7ULL/* The root of the unified cgroup hierarchy. */#define ROOT_CGROUP_ID 1ULL-#ifndef SO_NETNS_COOKIE-#define SO_NETNS_COOKIE 71-#endif--/* A netfilter link hooks the caller's network namespace. */-static int netns_initial(void)-{- socklen_t len = sizeof(__u64);- __u64 cookie = 0;- int fd, ret;-- fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);- if (fd < 0)- return -errno;- ret = getsockopt(fd, SOL_SOCKET, SO_NETNS_COOKIE, &cookie, &len) ?- -errno : 0;- close(fd);- if (ret)- return ret;- return cookie == NET_NS_INIT_COOKIE ? 0 : -EXDEV;-}/** The site a kprobe, uprobe, cgroup or netfilter program attaches at isdiff --git a/testing/unit/conditions.c b/testing/unit/conditions.cindex 4f79257b..9dce2912 100644--- a/testing/unit/conditions.c+++ b/testing/unit/conditions.c@@ -155,6 +155,11 @@ static void unchecked_coverage_warns_without_degrading(void)EXPECT_STREQ(row->status, "WARN");EXPECT_TRUE(strstr(row->detail, "not checked"));EXPECT_STREQ(status_verdict(&in), "ok");+ in.xdp_coverage_rc = -EXDEV;+ row = condition_of(&in, c, "XdpCovered");+ ASSERT_TRUE(row);+ EXPECT_STREQ(row->detail, "XDP coverage not checked: "+ "not in the initial network namespace");}static void covered_devices_are_ok(void)diff --git a/testing/unit/xdp.c b/testing/unit/xdp.cindex 82d3b6b0..26843b7d 100644--- a/testing/unit/xdp.c+++ b/testing/unit/xdp.c@@ -310,6 +310,18 @@ static void device_features_are_checked(void)EXPECT_EQ(xdp_features_parse(buf, len, &out), -EINVAL);}+static void the_network_namespace_is_known(void)+{+ int ret;++ /* CI runs riscv64 under QEMU user emulation, which lacks the option. */+#ifdef __riscv+ unit_skip("SO_NETNS_COOKIE unavailable under emulation");+#endif+ ret = netns_initial();+ EXPECT_TRUE(ret == 0 || ret == -EXDEV);+}+static void kernel_discovery_revalidates_devices(void){struct xdp_device *devices = NULL;@@ -346,6 +358,7 @@ static const struct unit_case cases[] = {UNIT_CASE(attach_errors_never_alias_an_errno),UNIT_CASE(device_features_are_checked),UNIT_CASE(link_messages_are_checked),+ UNIT_CASE(the_network_namespace_is_known),UNIT_CASE(kernel_discovery_revalidates_devices),{}};diff --git a/util/conditions.c b/util/conditions.cindex 243513bf..ddd13555 100644--- a/util/conditions.c+++ b/util/conditions.c@@ -231,6 +231,9 @@ size_t conditions(const struct conditions_input *in,in->xdp_uncovered,in->xdp_uncovered_detail ?in->xdp_uncovered_detail : "?");+ else if (in->xdp_coverage_rc == -EXDEV)+ set_detail(&c[n], "WARN", "XDP coverage not checked: %s",+ "not in the initial network namespace");else if (in->xdp_coverage_rc)set_detail(&c[n], "WARN", "XDP coverage not checked: %s",strerror(-in->xdp_coverage_rc));diff --git a/util/upatch_verify.c b/util/upatch_verify.cindex 6876b818..d881a43b 100644--- a/util/upatch_verify.c+++ b/util/upatch_verify.c@@ -496,6 +496,13 @@ int upatch_xdp_uncovered(const struct measured_set *s,struct uncovered_walk w = { .cb = cb, .ctx = ctx };int ret;+ /* Elsewhere the devices and their indexes are another namespace's. */+ ret = netns_initial();+ if (ret) {+ if (!*check_err)+ *check_err = ret;+ return 0;+ }ret = walk_pinned(s, xdp_uncovered, NULL, &w, check_err);xfree(w.devices, size_mul(w.count, sizeof(*w.devices)));return ret < 0 ? ret : w.found;diff --git a/util/xdp.c b/util/xdp.cindex 0ac2367d..47c1f3eb 100644--- a/util/xdp.c+++ b/util/xdp.c@@ -596,6 +596,30 @@ const char *xdp_uncovered_reason(__u32 attached, int features_err,return "not attached";}+/* The initial network namespace's cookie, NET_NS_INIT_ID in linux/nsfs.h. */+#define NET_NS_INIT_COOKIE 7ULL++#ifndef SO_NETNS_COOKIE+#define SO_NETNS_COOKIE 71+#endif++int netns_initial(void)+{+ socklen_t len = sizeof(__u64);+ __u64 cookie = 0;+ int fd, ret;++ fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);+ if (fd < 0)+ return -errno;+ ret = getsockopt(fd, SOL_SOCKET, SO_NETNS_COOKIE, &cookie, &len) ?+ -errno : 0;+ close(fd);+ if (ret)+ return ret;+ return cookie == NET_NS_INIT_COOKIE ? 0 : -EXDEV;+}+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 4bdb10f7..61e81e6e 100644--- a/util/xdp.h+++ b/util/xdp.h@@ -66,6 +66,12 @@ 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);+/*+ * 0 in the initial network namespace, -EXDEV in another. Devices, and the+ * hook of a netfilter link, are the caller's namespace's.+ */+int netns_initial(void);+/** A device whose facts cannot be read sits out, unresolved, rather than* failing the read: it need not be one any upatch selects.