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

selftests/bpf: Fix misaligned memory access in queue_stack_map test

Copy over iphdr into a local variable before accessing 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-11-andrii@kernel.org

authored by

Andrii Nakryiko and committed by
Daniel Borkmann
e2e0d90c 6c4dedb7

+7 -5
+7 -5
tools/testing/selftests/bpf/prog_tests/queue_stack_map.c
··· 14 14 int i, err, prog_fd, map_in_fd, map_out_fd; 15 15 char file[32], buf[128]; 16 16 struct bpf_object *obj; 17 - struct iphdr *iph = (void *)buf + sizeof(struct ethhdr); 17 + struct iphdr iph; 18 18 19 19 /* Fill test values to be used */ 20 20 for (i = 0; i < MAP_SIZE; i++) ··· 60 60 61 61 err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4), 62 62 buf, &size, &retval, &duration); 63 - if (err || retval || size != sizeof(pkt_v4) || 64 - iph->daddr != val) 63 + if (err || retval || size != sizeof(pkt_v4)) 64 + break; 65 + memcpy(&iph, buf + sizeof(struct ethhdr), sizeof(iph)); 66 + if (iph.daddr != val) 65 67 break; 66 68 } 67 69 68 - CHECK(err || retval || size != sizeof(pkt_v4) || iph->daddr != val, 70 + CHECK(err || retval || size != sizeof(pkt_v4) || iph.daddr != val, 69 71 "bpf_map_pop_elem", 70 72 "err %d errno %d retval %d size %d iph->daddr %u\n", 71 - err, errno, retval, size, iph->daddr); 73 + err, errno, retval, size, iph.daddr); 72 74 73 75 /* Queue is empty, program should return TC_ACT_SHOT */ 74 76 err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),