Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: fix null pointer deref in bpf_prog_test_run_xdp

syzkaller was able to generate the following XDP program ...

(18) r0 = 0x0
(61) r5 = *(u32 *)(r1 +12)
(04) (u32) r0 += (u32) 0
(95) exit

... and trigger a NULL pointer dereference in ___bpf_prog_run()
via bpf_prog_test_run_xdp() where this was attempted to run.

Reason is that recent xdp_rxq_info addition to XDP programs
updated all drivers, but not bpf_prog_test_run_xdp(), where
xdp_buff is set up. Thus when context rewriter does the deref
on the netdev it's NULL at runtime. Fix it by using xdp_rxq
from loopback dev. __netif_get_rx_queue() helper can also be
reused in various other locations later on.

Fixes: 02dd3291b2f0 ("bpf: finally expose xdp_rxq_info to XDP bpf-programs")
Reported-by: syzbot+1eb094057b338eb1fc00@syzkaller.appspotmail.com
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Cc: Jesper Dangaard Brouer <brouer@redhat.com>
Acked-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Daniel Borkmann and committed by
Alexei Starovoitov
65073a67 62a06994

+24
+6
include/linux/netdevice.h
··· 3228 3228 } 3229 3229 #endif 3230 3230 3231 + static inline struct netdev_rx_queue * 3232 + __netif_get_rx_queue(struct net_device *dev, unsigned int rxq) 3233 + { 3234 + return dev->_rx + rxq; 3235 + } 3236 + 3231 3237 #ifdef CONFIG_SYSFS 3232 3238 static inline unsigned int get_netdev_rx_queue_index( 3233 3239 struct netdev_rx_queue *queue)
+4
net/bpf/test_run.c
··· 151 151 { 152 152 u32 size = kattr->test.data_size_in; 153 153 u32 repeat = kattr->test.repeat; 154 + struct netdev_rx_queue *rxqueue; 154 155 struct xdp_buff xdp = {}; 155 156 u32 retval, duration; 156 157 void *data; ··· 165 164 xdp.data = data + XDP_PACKET_HEADROOM + NET_IP_ALIGN; 166 165 xdp.data_meta = xdp.data; 167 166 xdp.data_end = xdp.data + size; 167 + 168 + rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0); 169 + xdp.rxq = &rxqueue->xdp_rxq; 168 170 169 171 retval = bpf_test_run(prog, &xdp, repeat, &duration); 170 172 if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN)
+14
tools/testing/selftests/bpf/test_verifier.c
··· 7780 7780 .result = REJECT, 7781 7781 }, 7782 7782 { 7783 + "XDP, using ifindex from netdev", 7784 + .insns = { 7785 + BPF_MOV64_IMM(BPF_REG_0, 0), 7786 + BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 7787 + offsetof(struct xdp_md, ingress_ifindex)), 7788 + BPF_JMP_IMM(BPF_JLT, BPF_REG_2, 1, 1), 7789 + BPF_MOV64_IMM(BPF_REG_0, 1), 7790 + BPF_EXIT_INSN(), 7791 + }, 7792 + .result = ACCEPT, 7793 + .prog_type = BPF_PROG_TYPE_XDP, 7794 + .retval = 1, 7795 + }, 7796 + { 7783 7797 "meta access, test1", 7784 7798 .insns = { 7785 7799 BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,