The verifier assumes that map values are 8-byte aligned. Add a test that looks up an IPv4 lpm_trie value, a BPF_F_NO_PREALLOC hash value and a devmap value from an XDP program, stores the returned addresses in global variables and checks them in user space. The program does not execute any instruction that requires alignment, so on an affected kernel the test fails instead of crashing it. Without the previous patches the lpm_trie and devmap checks fail on 64-bit architectures, and the hash and devmap checks fail on 32-bit architectures. This patch was written with the help of an AI coding assistant (Claude Opus 5.5). Signed-off-by: Tobias Philipp --- .../bpf/prog_tests/map_value_align.c | 51 +++++++++++++++++++ .../selftests/bpf/progs/map_value_align.c | 48 +++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/map_value_align.c create mode 100644 tools/testing/selftests/bpf/progs/map_value_align.c diff --git a/tools/testing/selftests/bpf/prog_tests/map_value_align.c b/tools/testing/selftests/bpf/prog_tests/map_value_align.c new file mode 100644 index 000000000..91898a052 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/map_value_align.c @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include "map_value_align.skel.h" + +#define IFINDEX_LO 1 + +/* The verifier assumes that map values are 8-byte aligned */ +void test_map_value_align(void) +{ + LIBBPF_OPTS(bpf_test_run_opts, topts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + ); + struct bpf_devmap_val dval = { .ifindex = IFINDEX_LO }; + struct { __u32 prefixlen, data; } lkey = { .prefixlen = 32 }; + struct map_value_align *skel; + __u64 val = 0; + __u32 key = 0; + int err; + + skel = map_value_align__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + + err = bpf_map__update_elem(skel->maps.lpm, &lkey, sizeof(lkey), + &val, sizeof(val), BPF_ANY); + if (!ASSERT_OK(err, "lpm_update")) + goto out; + err = bpf_map__update_elem(skel->maps.hash, &key, sizeof(key), + &val, sizeof(val), BPF_ANY); + if (!ASSERT_OK(err, "hash_update")) + goto out; + err = bpf_map__update_elem(skel->maps.devmap, &key, sizeof(key), + &dval, sizeof(dval), BPF_ANY); + if (!ASSERT_OK(err, "devmap_update")) + goto out; + + err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.lookup), &topts); + if (!ASSERT_OK(err, "test_run")) + goto out; + + ASSERT_NEQ(skel->bss->lpm_addr, 0, "lpm_lookup"); + ASSERT_EQ(skel->bss->lpm_addr & 7, 0, "lpm_value_align"); + ASSERT_NEQ(skel->bss->hash_addr, 0, "hash_lookup"); + ASSERT_EQ(skel->bss->hash_addr & 7, 0, "hash_value_align"); + ASSERT_NEQ(skel->bss->devmap_addr, 0, "devmap_lookup"); + ASSERT_EQ(skel->bss->devmap_addr & 7, 0, "devmap_value_align"); +out: + map_value_align__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/map_value_align.c b/tools/testing/selftests/bpf/progs/map_value_align.c new file mode 100644 index 000000000..bd0663a13 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/map_value_align.c @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +struct lpm_key { + __u32 prefixlen; + __u32 data; +}; + +struct { + __uint(type, BPF_MAP_TYPE_LPM_TRIE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __uint(max_entries, 1); + __type(key, struct lpm_key); + __type(value, __u64); +} lpm SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(map_flags, BPF_F_NO_PREALLOC); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u64); +} hash SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_DEVMAP); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, struct bpf_devmap_val); +} devmap SEC(".maps"); + +/* Addresses of the map values, checked by user space */ +__u64 lpm_addr, hash_addr, devmap_addr; + +SEC("xdp") +int lookup(struct xdp_md *ctx) +{ + struct lpm_key lkey = { .prefixlen = 32 }; + __u32 key = 0; + + lpm_addr = (unsigned long)bpf_map_lookup_elem(&lpm, &lkey); + hash_addr = (unsigned long)bpf_map_lookup_elem(&hash, &key); + devmap_addr = (unsigned long)bpf_map_lookup_elem(&devmap, &key); + return XDP_PASS; +} + +char _license[] SEC("license") = "GPL"; -- 2.54.0 (Apple Git-157)