From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Nicholas Carlini <npc@anthropic.com>,
Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Eduard Zingerman <eddyz87@gmail.com>,
Emil Tsalapatis <emil@etsalapatis.com>,
kkd@meta.com, kernel-team@meta.com
Subject: [PATCH bpf v2 4/7] bpf: Reject fall-through across subprogram boundaries
Date: Sat, 5 Sep 2026 10:34:12 +0200 [thread overview]
Message-ID: <20260905083418.3723623-5-memxor@gmail.com> (raw)
In-Reply-To: <20260905083418.3723623-1-memxor@gmail.com>
check_subprogs() verifies that each subprogram ends in an exit or an
unconditional jump, preventing control flow from falling through into
the next subprogram. However, this check runs before CO-RE relocations
are applied.
When a relocation cannot be resolved, bpf_core_patch_insn() poisons its
target by replacing it with an invalid BPF_CALL. A relocation targeting
the terminal instruction of a non-final subprogram can therefore create
a fall-through edge into the next subprogram after the invariant was
checked.
The per-subprogram DFS in bpf_compute_postorder() then visits the next
subprogram twice and writes past its prog->len-sized postorder array.
Stack liveness analysis relies on the same containment and can access its
per-subprogram arrays out of bounds as well.
Reject fall-through edges whose endpoints belong to different
subprograms in push_insn(). This reestablishes the invariant on the final
instruction stream at the common CFG edge insertion point. Cross-subprog
pseudo-call edges remain valid because they are represented as branch
edges.
Fixes: efcda22aa541 ("bpf: compute instructions postorder per subprogram")
Reported-by: Nicholas Carlini <npc@anthropic.com>
Suggested-by: Nicholas Carlini <npc@anthropic.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
kernel/bpf/cfg.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c
index 842c7d1eabcc..e3c904328ede 100644
--- a/kernel/bpf/cfg.c
+++ b/kernel/bpf/cfg.c
@@ -102,6 +102,7 @@ enum {
*/
static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
{
+ struct bpf_subprog_info *subprog;
int *insn_stack = env->cfg.insn_stack;
int *insn_state = env->cfg.insn_state;
@@ -121,6 +122,26 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
return -EINVAL;
}
+ /*
+ * check_subprogs() prevents control flow from falling through a
+ * subprogram boundary, but runs before CO-RE relocations can rewrite an
+ * instruction. Reestablish the invariant on the final instruction stream
+ * before constructing the CFG used by later per-subprogram passes.
+ */
+ if (e == FALLTHROUGH) {
+ subprog = bpf_find_containing_subprog(env, t);
+ if (w < subprog->start || w >= (subprog + 1)->start) {
+ verbose_linfo(env, t, "%d: ", t);
+ verbose(env, "fall-through out of subprog from insn %d to %d\n", t, w);
+ bpf_diag_program_structure(
+ env, t, "fall-through leaves subprogram",
+ "Keep fall-through control flow inside the current subprogram.",
+ "Instruction %d falls through to instruction %d outside its subprogram.",
+ t, w);
+ return -EINVAL;
+ }
+ }
+
if (e == BRANCH) {
/* mark branch target for state pruning */
mark_prune_point(env, w);
--
2.53.0
next prev parent reply other threads:[~2026-09-05 8:34 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-05 8:34 [PATCH bpf v2 0/7] Misc bug fixes - part 5 Kumar Kartikeya Dwivedi
2026-09-05 8:34 ` [PATCH bpf v2 1/7] bpf: Make post-verification instruction rewrites killable Kumar Kartikeya Dwivedi
2026-09-11 22:56 ` Eduard Zingerman
2026-09-05 8:34 ` [PATCH bpf v2 2/7] bpf: Preserve packet pointer class displacement in regsafe() Kumar Kartikeya Dwivedi
2026-09-05 9:25 ` bot+bpf-ci
2026-09-05 20:46 ` Alexei Starovoitov
2026-09-06 6:40 ` Eduard Zingerman
2026-09-06 7:04 ` Eduard Zingerman
2026-09-06 15:11 ` Alexei Starovoitov
2026-09-05 8:34 ` [PATCH bpf v2 3/7] selftests/bpf: Test packet pointer class displacement pruning Kumar Kartikeya Dwivedi
2026-09-05 9:10 ` bot+bpf-ci
2026-09-05 20:48 ` Alexei Starovoitov
2026-09-05 8:34 ` Kumar Kartikeya Dwivedi [this message]
2026-09-05 20:29 ` [PATCH bpf v2 4/7] bpf: Reject fall-through across subprogram boundaries Alexei Starovoitov
2026-09-05 8:34 ` [PATCH bpf v2 5/7] selftests/bpf: Test poisoned subprogram terminator Kumar Kartikeya Dwivedi
2026-09-05 8:34 ` [PATCH bpf v2 6/7] bpf: Assign lock identity to callback map values Kumar Kartikeya Dwivedi
2026-09-05 9:25 ` bot+bpf-ci
2026-09-12 0:23 ` Eduard Zingerman
2026-09-05 8:34 ` [PATCH bpf v2 7/7] selftests/bpf: Check callback map value lock identity Kumar Kartikeya Dwivedi
2026-09-05 9:10 ` bot+bpf-ci
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=20260905083418.3723623-5-memxor@gmail.com \
--to=memxor@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=kernel-team@meta.com \
--cc=kkd@meta.com \
--cc=npc@anthropic.com \
/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