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

bpf: avoid UB in usages of the __imm_insn macro

[Changes from V2:
- no-strict-aliasing is only applied when building with GCC.
- cpumask_failure.c is excluded, as it doesn't use __imm_insn.]

The __imm_insn macro is defined in bpf_misc.h as:

#define __imm_insn(name, expr) [name]"i"(*(long *)&(expr))

This may lead to type-punning and strict aliasing rules violations in
it's typical usage where the address of a struct bpf_insn is passed as
expr, like in:

__imm_insn(st_mem,
BPF_ST_MEM(BPF_W, BPF_REG_1, offsetof(struct __sk_buff, mark), 42))

Where:

#define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
((struct bpf_insn) { \
.code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
.dst_reg = DST, \
.src_reg = 0, \
.off = OFF, \
.imm = IMM })

In all the actual instances of this in the BPF selftests the value is
fed to a volatile asm statement as soon as it gets read from memory,
and thus it is unlikely anti-aliasing rules breakage may lead to
misguided optimizations.

However, GCC detects the potential problem (indirectly) by issuing a
warning stating that a temporary <Uxxxxxx> is used uninitialized,
where the temporary corresponds to the memory read by *(long *).

This patch adds -fno-strict-aliasing to the compilation flags of the
particular selftests that do type punning via __imm_insn, only for
GCC.

Tested in master bpf-next.
No regressions.

Signed-off-by: Jose E. Marchesi <jose.marchesi@oracle.com>
Cc: david.faust@oracle.com
Cc: cupertino.miranda@oracle.com
Cc: Yonghong Song <yonghong.song@linux.dev>
Cc: Eduard Zingerman <eddyz87@gmail.com>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/r/20240508103551.14955-1-jose.marchesi@oracle.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Jose E. Marchesi and committed by
Alexei Starovoitov
1209a523 cd3fc3b9

+13
+13
tools/testing/selftests/bpf/Makefile
··· 86 86 progs/btf_dump_test_case_packing.c-bpf_gcc-CFLAGS := -Wno-error 87 87 progs/btf_dump_test_case_padding.c-bpf_gcc-CFLAGS := -Wno-error 88 88 progs/btf_dump_test_case_syntax.c-bpf_gcc-CFLAGS := -Wno-error 89 + 90 + # The following tests do type-punning, via the __imm_insn macro, from 91 + # `struct bpf_insn' to long and then uses the value. This triggers an 92 + # "is used uninitialized" warning in GCC due to strict-aliasing 93 + # rules. 94 + progs/verifier_ref_tracking.c-bpf_gcc-CFLAGS := -fno-strict-aliasing 95 + progs/verifier_unpriv.c-bpf_gcc-CFLAGS := -fno-strict-aliasing 96 + progs/verifier_cgroup_storage.c-bpf_gcc-CFLAGS := -fno-strict-aliasing 97 + progs/verifier_ld_ind.c-bpf_gcc-CFLAGS := -fno-strict-aliasing 98 + progs/verifier_map_ret_val.c-bpf_gcc-CFLAGS := -fno-strict-aliasing 99 + progs/verifier_spill_fill.c-bpf_gcc-CFLAGS := -fno-strict-aliasing 100 + progs/verifier_subprog_precision.c-bpf_gcc-CFLAGS := -fno-strict-aliasing 101 + progs/verifier_uninit.c-bpf_gcc-CFLAGS := -fno-strict-aliasing 89 102 endif 90 103 91 104 ifneq ($(CLANG_CPUV4),)