* [PATCH bpf-next V3] bpf: avoid UB in usages of the __imm_insn macro
@ 2024-05-08 10:35 Jose E. Marchesi
2024-05-08 15:38 ` Yonghong Song
2024-05-08 17:00 ` patchwork-bot+netdevbpf
0 siblings, 2 replies; 3+ messages in thread
From: Jose E. Marchesi @ 2024-05-08 10:35 UTC (permalink / raw)
To: bpf
Cc: Jose E . Marchesi, david.faust, cupertino.miranda, Yonghong Song,
Eduard Zingerman
[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>
---
tools/testing/selftests/bpf/Makefile | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 511ea84139a3..e962a0bd8a78 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -86,6 +86,19 @@ progs/btf_dump_test_case_namespacing.c-bpf_gcc-CFLAGS := -Wno-error
progs/btf_dump_test_case_packing.c-bpf_gcc-CFLAGS := -Wno-error
progs/btf_dump_test_case_padding.c-bpf_gcc-CFLAGS := -Wno-error
progs/btf_dump_test_case_syntax.c-bpf_gcc-CFLAGS := -Wno-error
+
+# The following tests do type-punning, via the __imm_insn macro, from
+# `struct bpf_insn' to long and then uses the value. This triggers an
+# "is used uninitialized" warning in GCC due to strict-aliasing
+# rules.
+progs/verifier_ref_tracking.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
+progs/verifier_unpriv.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
+progs/verifier_cgroup_storage.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
+progs/verifier_ld_ind.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
+progs/verifier_map_ret_val.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
+progs/verifier_spill_fill.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
+progs/verifier_subprog_precision.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
+progs/verifier_uninit.c-bpf_gcc-CFLAGS := -fno-strict-aliasing
endif
ifneq ($(CLANG_CPUV4),)
--
2.30.2
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH bpf-next V3] bpf: avoid UB in usages of the __imm_insn macro
2024-05-08 10:35 [PATCH bpf-next V3] bpf: avoid UB in usages of the __imm_insn macro Jose E. Marchesi
@ 2024-05-08 15:38 ` Yonghong Song
2024-05-08 17:00 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: Yonghong Song @ 2024-05-08 15:38 UTC (permalink / raw)
To: Jose E. Marchesi, bpf; +Cc: david.faust, cupertino.miranda, Eduard Zingerman
On 5/8/24 3:35 AM, Jose E. Marchesi wrote:
> [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>
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH bpf-next V3] bpf: avoid UB in usages of the __imm_insn macro
2024-05-08 10:35 [PATCH bpf-next V3] bpf: avoid UB in usages of the __imm_insn macro Jose E. Marchesi
2024-05-08 15:38 ` Yonghong Song
@ 2024-05-08 17:00 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-05-08 17:00 UTC (permalink / raw)
To: Jose E. Marchesi
Cc: bpf, david.faust, cupertino.miranda, yonghong.song, eddyz87
Hello:
This patch was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:
On Wed, 8 May 2024 12:35:51 +0200 you wrote:
> [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))
>
> [...]
Here is the summary with links:
- [bpf-next,V3] bpf: avoid UB in usages of the __imm_insn macro
https://git.kernel.org/bpf/bpf-next/c/1209a523f691
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-05-08 17:00 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-08 10:35 [PATCH bpf-next V3] bpf: avoid UB in usages of the __imm_insn macro Jose E. Marchesi
2024-05-08 15:38 ` Yonghong Song
2024-05-08 17:00 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox