Skip to contentSkip to conversation

bpf: Don't attach to a subprog that is called in a recursion

PATCH Applied Alexei Starovoitov 2 participants 2 messages Nov 7

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Don't attach to a subprog that is called in a recursion

From: Alexei Starovoitov <[email protected]>

sum_to() of verifier_budget.c calls itself:

  sum_to:  if r1 != 0 goto 1f; r0 = 0; exit;
  1:       r6 = r1; r1 += -1; call sum_to; r0 += r6; exit;

It goes on while r10 is in the upper half of the kernel stack. The lower half is for what it calls. check_max_stack_depth() looks at that chain of calls when the prog is loaded. It cannot look at what is attached to a subprog of the chain later. For that reason an extension cannot replace such a subprog.

fentry and fexit progs can still be attached to it. The trampoline and the prog run below r10 of a subprog that passed its check. The frames of a tracing prog are in its private stack, but not all of them: not when it has tail calls or uses R12, not the frames of subprogs with stack arguments on x86-64, not the frames below 64 bytes. Such a prog takes up to 9K of the kernel stack, and nothing left room for it.

Refuse them like extensions, with one message for all:

Cannot attach to sum_to, which is called in a recursion

The main prog is never called in a recursion. fentry and fexit progs attach to it as before.

Fixes eaea73907052 ("bpf: Allow recursive subprogs")
---
 kernel/bpf/verifier.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +3 -3
index 09a44723bb2e..4a4d970627d1 100644
@@ -24047,9 +24047,9 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
 			bpf_log(log, "Only fentry can attach to %s, which can unwind\n", tname);
 			return -EINVAL;
 		}
-		/* It can have tail calls, which are not allowed there, see check_call_depth() */
-		if (aux->func && aux->func[subprog]->aux->in_recursion && prog_extension) {
-			bpf_log(log, "Cannot replace %s, which is called in a recursion\n", tname);
+		/* What it takes of the stack is not counted, see bpf_insert_recursion_checks() */
+		if (aux->func && aux->func[subprog]->aux->in_recursion) {
+			bpf_log(log, "Cannot attach to %s, which is called in a recursion\n", tname);
 			return -EINVAL;
 		}
 		conservative = aux->func_info_aux[subprog].unreliable;
Signature · 1 line
2.55.0
#1 lore raw
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 8 lines · On Sat, 7 Nov 2026 17:50:06 +0000 you wrote:

From: Alexei Starovoitov <[email protected]>

sum_to() of verifier_budget.c calls itself:

  sum_to:  if r1 != 0 goto 1f; r0 = 0; exit;
  1:       r6 = r1; r1 += -1; call sum_to; r0 += r6; exit;

[...]

Here is the summary with links:
- [bpf-next] bpf: Don't attach to a subprog that is called in a recursion

    https://git.kernel.org/bpf/bpf-next/c/dfc18dfc0da7

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html