After an lwt prog pushed an IPv6 header, bpf_lwt_xmit_reroute() hands skb->sk to ip6_dst_lookup_flow(). When the flow has no source address, the lookup reads inet6_sk(sk)->srcprefs, and inet6_sk() is NULL for example when: - skb->sk is a request_sock, e.g. for a SYN-ACK. - skb->sk is an IPv4 socket, the IPv6 header was pushed on an IPv4 packet. KASAN: null-ptr-deref in range [0x0000000000000040-0x0000000000000047] RIP: 0010:ip6_route_output_flags+0x279/0x4c0 Call Trace: ip6_dst_lookup_tail.constprop.0+0x26e/0x1050 ip6_dst_lookup_flow+0x101/0x1e0 bpf_lwt_xmit_reroute+0xc18/0x18b0 bpf_xmit+0x28f/0x400 lwtunnel_xmit+0x2a7/0x620 ip_finish_output2+0xa67/0x1f80 __ip_finish_output+0x510/0x7e0 ip_finish_output+0x2f/0x320 ip_output+0x171/0x3e0 ip_push_pending_frames+0x200/0x260 raw_sendmsg+0x114c/0x3680 inet_sendmsg+0xdf/0x100 __sys_sendto+0x3ef/0x500 __x64_sys_sendto+0xe0/0x1b0 x64_sys_call+0x1375/0x27e0 do_syscall_64+0xe7/0x630 entry_SYSCALL_64_after_hwframe+0x76/0x7e Pass the full socket, and no socket if it is not an IPv6 one while the flow has no source address. Fixes: 3bd0b15281af ("bpf: add handling of BPF_LWT_REROUTE to lwt_bpf.c") Signed-off-by: Jiayuan Chen --- net/core/lwt_bpf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/net/core/lwt_bpf.c b/net/core/lwt_bpf.c index 72c86ff2a847..33f710d92106 100644 --- a/net/core/lwt_bpf.c +++ b/net/core/lwt_bpf.c @@ -278,7 +278,11 @@ static int bpf_lwt_xmit_reroute(struct sk_buff *skb) fl6.daddr = iph6->daddr; fl6.saddr = iph6->saddr; - dst = ip6_dst_lookup_flow(net, skb->sk, &fl6, NULL); + if (ipv6_addr_any(&fl6.saddr) && + sk && sk->sk_family != AF_INET6) + sk = NULL; + + dst = ip6_dst_lookup_flow(net, sk, &fl6, NULL); if (IS_ERR(dst)) { err = PTR_ERR(dst); goto err; -- 2.43.0 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 --- .../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 sec "" dev link_err * - * Here 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 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 #include #include +#include #include +#include /* 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