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

selftests/bpf: Fix misaligned memory accesses in xdp_bonding test

Construct packet buffer explicitly for each packet to avoid unaligned
memory accesses.

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

authored by

Andrii Nakryiko and committed by
Daniel Borkmann
43080b71 57428298

+20 -16
+20 -16
tools/testing/selftests/bpf/prog_tests/xdp_bonding.c
··· 218 218 .h_dest = BOND2_MAC, 219 219 .h_proto = htons(ETH_P_IP), 220 220 }; 221 - uint8_t buf[128] = {}; 222 - struct iphdr *iph = (struct iphdr *)(buf + sizeof(eh)); 223 - struct udphdr *uh = (struct udphdr *)(buf + sizeof(eh) + sizeof(*iph)); 221 + struct iphdr iph = {}; 222 + struct udphdr uh = {}; 223 + uint8_t buf[128]; 224 224 int i, s = -1; 225 225 int ifindex; 226 226 ··· 232 232 if (!ASSERT_GT(ifindex, 0, "get bond1 ifindex")) 233 233 goto err; 234 234 235 - memcpy(buf, &eh, sizeof(eh)); 236 - iph->ihl = 5; 237 - iph->version = 4; 238 - iph->tos = 16; 239 - iph->id = 1; 240 - iph->ttl = 64; 241 - iph->protocol = IPPROTO_UDP; 242 - iph->saddr = 1; 243 - iph->daddr = 2; 244 - iph->tot_len = htons(sizeof(buf) - ETH_HLEN); 245 - iph->check = 0; 235 + iph.ihl = 5; 236 + iph.version = 4; 237 + iph.tos = 16; 238 + iph.id = 1; 239 + iph.ttl = 64; 240 + iph.protocol = IPPROTO_UDP; 241 + iph.saddr = 1; 242 + iph.daddr = 2; 243 + iph.tot_len = htons(sizeof(buf) - ETH_HLEN); 244 + iph.check = 0; 246 245 247 246 for (i = 1; i <= NPACKETS; i++) { 248 247 int n; ··· 252 253 }; 253 254 254 255 /* vary the UDP destination port for even distribution with roundrobin/xor modes */ 255 - uh->dest++; 256 + uh.dest++; 256 257 257 258 if (vary_dst_ip) 258 - iph->daddr++; 259 + iph.daddr++; 260 + 261 + /* construct a packet */ 262 + memcpy(buf, &eh, sizeof(eh)); 263 + memcpy(buf + sizeof(eh), &iph, sizeof(iph)); 264 + memcpy(buf + sizeof(eh) + sizeof(iph), &uh, sizeof(uh)); 259 265 260 266 n = sendto(s, buf, sizeof(buf), 0, (struct sockaddr *)&saddr_ll, sizeof(saddr_ll)); 261 267 if (!ASSERT_EQ(n, sizeof(buf), "sendto"))