From: Yonghong Song <yonghong.song@linux.dev>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
kernel-team@fb.com, Martin KaFai Lau <martin.lau@kernel.org>
Subject: [PATCH bpf-next v5 0/3] bpf: Warn with __bpf_trap() kfunc maybe due to uninitialized variable
Date: Fri, 23 May 2025 13:53:16 -0700 [thread overview]
Message-ID: <20250523205316.1291136-1-yonghong.song@linux.dev> (raw)
Marc Suñé (Isovalent, part of Cisco) reported an issue where an
uninitialized variable caused generating bpf prog binary code not
working as expected. The reproducer is in [1] where the flags
“-Wall -Werror” are enabled, but there is no warning as the compiler
takes advantage of uninitialized variable to do aggressive optimization.
Such optimization results in a verification log:
last insn is not an exit or jmp
User still needs to take quite some time to figure out what is
the root cause.
To give a better hint to user, __bpf_trap() kfunc is introduced
in kernel and the compiler ([2]) will encode __bpf_trap()
as needed. For example, compiler may generate 'unreachable' IR
after do optimizaiton by taking advantage of uninitialized variable,
and later bpf backend will translate such 'unreachable' IR to
__bpf_trap() func in final binary. When kernel detects
__bpf_trap(), it is able to issue much better verifier log, e.g.
unexpected __bpf_trap() due to uninitialized variable?
[1] https://github.com/msune/clang_bpf/blob/main/Makefile#L3
[2] https://github.com/llvm/llvm-project/pull/131731
Changelogs:
v4 -> v5:
- v4: https://lore.kernel.org/bpf/20250521032047.1015381-1-yonghong.song@linux.dev/
- Change original kfunc bpf_unreachable() to __bpf_trap().
- Better codes for function check_special_kfunc().
v3 -> v4:
- v3: https://lore.kernel.org/bpf/20250519203339.2060080-1-yonghong.song@linux.dev/
- Remove special_kfunc_set in verifier.
v2 -> v3:
- v2: https://lore.kernel.org/bpf/CAADnVQL9A8vB-yRjnZn8bgMrfDSO17FFBtS_xOs5w-LSq+p74g@mail.gmail.com/
- The newer llvm patch (above [2]) added 'exit' insn if the last insn
in the function is bpf_unreachable(). This way, check_subprogs()
handling is unnecessary and removed.
- Remove the big C test (above [1]) and add a simple C test and three
inline asm tests.
v1 -> v2:
- v1: https://lore.kernel.org/bpf/20250511182744.1806792-1-yonghong.song@linux.dev/
- If bpf_unreachable() is hit during check_kfunc_call(), report the
verification failure.
- Add three inline asm test cases.
Yonghong Song (3):
bpf: Remove special_kfunc_set from verifier
bpf: Warn with __bpf_trap() kfunc maybe due to uninitialized variable
selftests/bpf: Add unit tests with __bpf_trap() kfunc
kernel/bpf/helpers.c | 5 +
kernel/bpf/verifier.c | 379 +++++++++---------
.../selftests/bpf/prog_tests/verifier.c | 2 +
.../selftests/bpf/progs/verifier_bpf_trap.c | 71 ++++
4 files changed, 260 insertions(+), 197 deletions(-)
create mode 100644 tools/testing/selftests/bpf/progs/verifier_bpf_trap.c
--
2.47.1
next reply other threads:[~2025-05-23 20:53 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-23 20:53 Yonghong Song [this message]
2025-05-23 20:53 ` [PATCH bpf-next v5 1/3] bpf: Remove special_kfunc_set from verifier Yonghong Song
2025-05-23 20:53 ` [PATCH bpf-next v5 2/3] bpf: Warn with __bpf_trap() kfunc maybe due to uninitialized variable Yonghong Song
2025-05-23 20:53 ` [PATCH bpf-next v5 3/3] selftests/bpf: Add unit tests with __bpf_trap() kfunc Yonghong Song
2025-05-27 17:30 ` Alexei Starovoitov
2025-05-26 21:49 ` [PATCH bpf-next v5 0/3] bpf: Warn with __bpf_trap() kfunc maybe due to uninitialized variable Eduard Zingerman
2025-05-27 17:40 ` patchwork-bot+netdevbpf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250523205316.1291136-1-yonghong.song@linux.dev \
--to=yonghong.song@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
--cc=martin.lau@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox