Skip to content

Commit 8f6f41f

Browse files
anakryikoborkmann
authored andcommitted
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
1 parent 43080b7 commit 8f6f41f

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

tools/testing/selftests/bpf/prog_tests/xdp.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ void test_xdp(void)
1111
const char *file = "./test_xdp.o";
1212
struct bpf_object *obj;
1313
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;
1616
__u32 duration, retval, size;
1717
int err, prog_fd, map_fd;
1818

@@ -28,16 +28,17 @@ void test_xdp(void)
2828

2929
err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
3030
buf, &size, &retval, &duration);
31-
31+
memcpy(&iph, buf + sizeof(struct ethhdr), sizeof(iph));
3232
CHECK(err || retval != XDP_TX || size != 74 ||
33-
iph->protocol != IPPROTO_IPIP, "ipv4",
33+
iph.protocol != IPPROTO_IPIP, "ipv4",
3434
"err %d errno %d retval %d size %d\n",
3535
err, errno, retval, size);
3636

3737
err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6),
3838
buf, &size, &retval, &duration);
39+
memcpy(&iph6, buf + sizeof(struct ethhdr), sizeof(iph6));
3940
CHECK(err || retval != XDP_TX || size != 114 ||
40-
iph6->nexthdr != IPPROTO_IPV6, "ipv6",
41+
iph6.nexthdr != IPPROTO_IPV6, "ipv6",
4142
"err %d errno %d retval %d size %d\n",
4243
err, errno, retval, size);
4344
out:

tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void test_xdp_bpf2bpf(void)
4242
char buf[128];
4343
int err, pkt_fd, map_fd;
4444
bool passed = false;
45-
struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
45+
struct iphdr iph;
4646
struct iptnl_info value4 = {.family = AF_INET};
4747
struct test_xdp *pkt_skel = NULL;
4848
struct test_xdp_bpf2bpf *ftrace_skel = NULL;
@@ -93,9 +93,9 @@ void test_xdp_bpf2bpf(void)
9393
/* Run test program */
9494
err = bpf_prog_test_run(pkt_fd, 1, &pkt_v4, sizeof(pkt_v4),
9595
buf, &size, &retval, &duration);
96-
96+
memcpy(&iph, buf + sizeof(struct ethhdr), sizeof(iph));
9797
if (CHECK(err || retval != XDP_TX || size != 74 ||
98-
iph->protocol != IPPROTO_IPIP, "ipv4",
98+
iph.protocol != IPPROTO_IPIP, "ipv4",
9999
"err %d errno %d retval %d size %d\n",
100100
err, errno, retval, size))
101101
goto out;

0 commit comments

Comments
 (0)