From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: bpf@vger.kernel.org
Cc: Jiayuan Chen <jiayuan.chen@linux.dev>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
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>,
Shuah Khan <shuah@kernel.org>,
Xuanqiang Luo <luoxuanqiang@kylinos.cn>,
Fernando Fernandez Mancera <fmancera@suse.de>,
Junseo Lim <zirajs7@gmail.com>, Leon Hwang <leon.hwang@linux.dev>,
Yuqiang Sun <izaiahsun@gmail.com>,
Peter Oskolkov <posk@google.com>,
netdev@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: [PATCH bpf 2/2] selftests/bpf: test lwt_xmit reroute of an IPv4-in-IPv6 encap
Date: Sun, 22 Nov 2026 12:13:46 +0800 [thread overview]
Message-ID: <20261122041401.189733-2-jiayuan.chen@linux.dev> (raw)
In-Reply-To: <20261122041401.189733-1-jiayuan.chen@linux.dev>
Add a test where an lwt_xmit prog pushes an IPv6 header with a ::
source on an IPv4 packet and reroutes. On an unfixed kernel this
dereferences inet6_sk() of the IPv4 socket and crashes; here reaching
the end is the pass.
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
.../selftests/bpf/prog_tests/lwt_reroute.c | 20 ++++++++++++++--
.../selftests/bpf/progs/test_lwt_reroute.c | 24 +++++++++++++++++++
2 files changed, 42 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/lwt_reroute.c b/tools/testing/selftests/bpf/prog_tests/lwt_reroute.c
index 6c50c0f63f43..01387c0f1b19 100644
--- a/tools/testing/selftests/bpf/prog_tests/lwt_reroute.c
+++ b/tools/testing/selftests/bpf/prog_tests/lwt_reroute.c
@@ -18,8 +18,8 @@
*
* ip route add 10.0.0.0/24 encap bpf xmit <obj> sec "<section_N>" dev link_err
*
- * Here <obj> is statically defined to test_lwt_reroute.bpf.o, and it contains
- * a single test program entry. This program sets packet mark by last byte of
+ * Here <obj> is statically defined to test_lwt_reroute.bpf.o.
+ * The lwt_xmit program sets packet mark by last byte of
* the IPv4 daddr. For example, a packet going to 1.2.3.4 will receive a skb
* mark 4. A packet will only be marked once, and IP x.x.x.0 will be skipped
* to avoid route loop. We didn't use generated BPF skeleton since the
@@ -42,6 +42,7 @@
* --------------------------------
* 1. Reroute to a running tun device
* 2. Reroute to a device where qdisc drop
+ * 3. Reroute an IPv4 packet with a pushed IPv6 header
*
* For case 1, ping packets should be received by the tun device.
*
@@ -241,11 +242,26 @@ static void test_lwt_reroute_qdisc_dropped(void)
close(tun_fd);
}
+static void test_lwt_reroute_push6(void)
+{
+ SYS(out, "ip link add link_err type dummy");
+ SYS(out, "ip link set lo up");
+ SYS(out, "ip addr add dev lo " LOCAL_SRC "/32");
+ SYS(out, "ip link set link_err up");
+ SYS(out, "ip route add %s dev link_err encap bpf xmit obj %s sec push6_zerosrc",
+ TEST_CIDR, BPF_OBJECT);
+
+ ping_once("10.0.0.2");
+out:
+ return;
+}
+
static void *test_lwt_reroute_run(void *arg)
{
netns_delete();
RUN_TEST(lwt_reroute_normal_xmit);
RUN_TEST(lwt_reroute_qdisc_dropped);
+ RUN_TEST(lwt_reroute_push6);
return NULL;
}
diff --git a/tools/testing/selftests/bpf/progs/test_lwt_reroute.c b/tools/testing/selftests/bpf/progs/test_lwt_reroute.c
index 1dc64351929c..c969962926e6 100644
--- a/tools/testing/selftests/bpf/progs/test_lwt_reroute.c
+++ b/tools/testing/selftests/bpf/progs/test_lwt_reroute.c
@@ -4,7 +4,9 @@
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>
#include <linux/if_ether.h>
+#include <linux/in.h>
#include <linux/ip.h>
+#include <linux/ipv6.h>
/* This function extracts the last byte of the daddr, and uses it
* as output dev index.
@@ -33,4 +35,26 @@ int test_lwt_reroute(struct __sk_buff *skb)
return BPF_LWT_REROUTE;
}
+SEC("push6_zerosrc")
+int test_lwt_reroute_push6(struct __sk_buff *skb)
+{
+ struct ipv6hdr h = {};
+
+ h.version = 6;
+ h.payload_len = bpf_htons(skb->len);
+ h.nexthdr = IPPROTO_IPIP;
+ h.hop_limit = 64;
+ /* 2001:db8::3, a fake address */
+ h.daddr.s6_addr[0] = 0x20;
+ h.daddr.s6_addr[1] = 0x01;
+ h.daddr.s6_addr[2] = 0x0d;
+ h.daddr.s6_addr[3] = 0xb8;
+ h.daddr.s6_addr[15] = 0x03;
+
+ if (bpf_lwt_push_encap(skb, BPF_LWT_ENCAP_IP, &h, sizeof(h)))
+ return BPF_DROP;
+
+ return BPF_LWT_REROUTE;
+}
+
char _license[] SEC("license") = "GPL";
--
2.43.0
prev parent reply other threads:[~2026-11-22 4:14 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-22 4:13 [PATCH bpf 1/2] bpf: lwt: fix NULL deref in IPv6 reroute Jiayuan Chen
2026-11-22 4:13 ` Jiayuan Chen [this message]
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=20261122041401.189733-2-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@kernel.org \
--cc=emil@etsalapatis.com \
--cc=fmancera@suse.de \
--cc=horms@kernel.org \
--cc=ihor.solodrai@linux.dev \
--cc=izaiahsun@gmail.com \
--cc=jolsa@kernel.org \
--cc=kuba@kernel.org \
--cc=leon.hwang@linux.dev \
--cc=linux-kselftest@vger.kernel.org \
--cc=luoxuanqiang@kylinos.cn \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=posk@google.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
--cc=zirajs7@gmail.com \
/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