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

selftests/bpf: Fix misaligned accesses in xdp and xdp_bpf2bpf tests

Similar to previous patch, just copy over necessary struct into local
stack variable before checking its fields.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20211124002325.1737739-14-andrii@kernel.org

authored by

Andrii Nakryiko and committed by
Daniel Borkmann
8f6f41f3 43080b71

+9 -8
+6 -5
tools/testing/selftests/bpf/prog_tests/xdp.c
··· 11 11 const char *file = "./test_xdp.o"; 12 12 struct bpf_object *obj; 13 13 char buf[128]; 14 - struct ipv6hdr *iph6 = (void *)buf + sizeof(struct ethhdr); 15 - struct iphdr *iph = (void *)buf + sizeof(struct ethhdr); 14 + struct ipv6hdr iph6; 15 + struct iphdr iph; 16 16 __u32 duration, retval, size; 17 17 int err, prog_fd, map_fd; 18 18 ··· 28 28 29 29 err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4), 30 30 buf, &size, &retval, &duration); 31 - 31 + memcpy(&iph, buf + sizeof(struct ethhdr), sizeof(iph)); 32 32 CHECK(err || retval != XDP_TX || size != 74 || 33 - iph->protocol != IPPROTO_IPIP, "ipv4", 33 + iph.protocol != IPPROTO_IPIP, "ipv4", 34 34 "err %d errno %d retval %d size %d\n", 35 35 err, errno, retval, size); 36 36 37 37 err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6), 38 38 buf, &size, &retval, &duration); 39 + memcpy(&iph6, buf + sizeof(struct ethhdr), sizeof(iph6)); 39 40 CHECK(err || retval != XDP_TX || size != 114 || 40 - iph6->nexthdr != IPPROTO_IPV6, "ipv6", 41 + iph6.nexthdr != IPPROTO_IPV6, "ipv6", 41 42 "err %d errno %d retval %d size %d\n", 42 43 err, errno, retval, size); 43 44 out:
+3 -3
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c
··· 42 42 char buf[128]; 43 43 int err, pkt_fd, map_fd; 44 44 bool passed = false; 45 - struct iphdr *iph = (void *)buf + sizeof(struct ethhdr); 45 + struct iphdr iph; 46 46 struct iptnl_info value4 = {.family = AF_INET}; 47 47 struct test_xdp *pkt_skel = NULL; 48 48 struct test_xdp_bpf2bpf *ftrace_skel = NULL; ··· 93 93 /* Run test program */ 94 94 err = bpf_prog_test_run(pkt_fd, 1, &pkt_v4, sizeof(pkt_v4), 95 95 buf, &size, &retval, &duration); 96 - 96 + memcpy(&iph, buf + sizeof(struct ethhdr), sizeof(iph)); 97 97 if (CHECK(err || retval != XDP_TX || size != 74 || 98 - iph->protocol != IPPROTO_IPIP, "ipv4", 98 + iph.protocol != IPPROTO_IPIP, "ipv4", 99 99 "err %d errno %d retval %d size %d\n", 100 100 err, errno, retval, size)) 101 101 goto out;