Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: bpf@vger.kernel.org
Cc: Jiayuan Chen <jiayuan.chen@linux.dev>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	John Fastabend <john.fastabend@gmail.com>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	Jesper Dangaard Brouer <hawk@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>,
	Shuah Khan <shuah@kernel.org>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	Hangbin Liu <liuhangbin@gmail.com>,
	Krishna Kumar <krikku@gmail.com>,
	Samiullah Khawaja <skhawaja@google.com>,
	Martin Karsten <mkarsten@uwaterloo.ca>,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Subject: [PATCH bpf-next v4 3/3] selftests/bpf: xdp: test dev_xdp_install() rejects device-bound program
Date: Mon, 10 Aug 2026 13:06:01 +0800	[thread overview]
Message-ID: <20260810050621.82035-4-jiayuan.chen@linux.dev> (raw)
In-Reply-To: <20260810050621.82035-1-jiayuan.chen@linux.dev>

bpf_xdp_link_update() used to skip the checks in dev_xdp_attach(). Add a
test that makes a generic XDP link with a normal program and then tries
to swap in a device-bound program, which must fail.

Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
 .../bpf/prog_tests/xdp_dev_bound_only.c       | 49 +++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_dev_bound_only.c b/tools/testing/selftests/bpf/prog_tests/xdp_dev_bound_only.c
index 7dd18c6d06c6..4a13f8ec4300 100644
--- a/tools/testing/selftests/bpf/prog_tests/xdp_dev_bound_only.c
+++ b/tools/testing/selftests/bpf/prog_tests/xdp_dev_bound_only.c
@@ -1,9 +1,11 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <net/if.h>
+#include <linux/if_link.h>
 #include <test_progs.h>
 #include <network_helpers.h>
 
 #define LOCAL_NETNS "xdp_dev_bound_only_netns"
+#define LINK_UPDATE_NETNS "xdp_dev_bound_only_lu_netns"
 
 static int load_dummy_prog(char *name, __u32 ifindex, __u32 flags)
 {
@@ -59,3 +61,50 @@ void test_xdp_dev_bound_only_offdev(void)
 	 */
 	SYS_NOFAIL("ip netns del " LOCAL_NETNS);
 }
+
+/* A device-bound program must not run on the XDP software path.
+ * dev_xdp_attach() rejected such programs, but bpf_xdp_link_update() reaches
+ * dev_xdp_install() directly and bypasses it, so the check has to live in
+ * dev_xdp_install(). Create a generic (SKB) XDP link with a normal program,
+ * then try to swap in a device-bound program via BPF_LINK_UPDATE.
+ */
+void test_xdp_dev_bound_only_link_update(void)
+{
+	LIBBPF_OPTS(bpf_link_create_opts, lopts, .flags = XDP_FLAGS_SKB_MODE);
+	int base_fd = -1, devbound_fd = -1, link_fd = -1;
+	struct nstoken *tok = NULL;
+	__u32 ifindex;
+	int err;
+
+	SYS(out, "ip netns add " LINK_UPDATE_NETNS);
+	tok = open_netns(LINK_UPDATE_NETNS);
+	if (!ASSERT_OK_PTR(tok, "open_netns"))
+		goto out;
+
+	SYS(out, "ip link add eth42 type veth");
+	ifindex = if_nametoindex("eth42");
+	if (!ASSERT_NEQ(ifindex, 0, "if_nametoindex"))
+		goto out;
+
+	devbound_fd = load_dummy_prog("devbound", ifindex, BPF_F_XDP_DEV_BOUND_ONLY);
+	if (!ASSERT_GE(devbound_fd, 0, "load_dummy_prog devbound"))
+		goto out;
+
+	base_fd = load_dummy_prog("base", 0, 0);
+	if (!ASSERT_GE(base_fd, 0, "load_dummy_prog base"))
+		goto out;
+
+	link_fd = bpf_link_create(base_fd, ifindex, BPF_XDP, &lopts);
+	if (!ASSERT_GE(link_fd, 0, "bpf_link_create"))
+		goto out;
+
+	err = bpf_link_update(link_fd, devbound_fd, NULL);
+	ASSERT_EQ(err, -EINVAL, "link_update device-bound rejected");
+
+out:
+	close(link_fd);
+	close(base_fd);
+	close(devbound_fd);
+	close_netns(tok);
+	SYS_NOFAIL("ip netns del " LINK_UPDATE_NETNS);
+}
-- 
2.43.0


  parent reply	other threads:[~2026-08-10  5:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-10  5:05 [PATCH bpf-next v4 0/3] bpf: prevent offloaded programs from running on host via tcx/netkit Jiayuan Chen
2026-08-10  5:05 ` [PATCH bpf-next v4 1/3] bpf, tcx, netkit: reject offloaded programs Jiayuan Chen
2026-08-10  5:06 ` [PATCH bpf-next v4 2/3] bpf, xdp: move offload check into dev_xdp_install() Jiayuan Chen
2026-08-10  5:06 ` Jiayuan Chen [this message]
2026-08-10  6:30   ` [PATCH bpf-next v4 3/3] selftests/bpf: xdp: test dev_xdp_install() rejects device-bound program bot+bpf-ci

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260810050621.82035-4-jiayuan.chen@linux.dev \
    --to=jiayuan.chen@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@google.com \
    --cc=emil@etsalapatis.com \
    --cc=hawk@kernel.org \
    --cc=horms@kernel.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=krikku@gmail.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=liuhangbin@gmail.com \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=mkarsten@uwaterloo.ca \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf@fomichev.me \
    --cc=shuah@kernel.org \
    --cc=skhawaja@google.com \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox