Skip to contentSkip to conversation

bpf: cleanup aux->used_maps after jit

PATCH Applied Anton Protopopov 5 participants 6 messages Nov 24, 2025

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

PATCH 1/1 bpf: cleanup aux->used_maps after jit

In commit b4ce5923e780 ("bpf, x86: add new map type: instructions array") env->used_map was copied to func[i]->aux->used_maps before jitting. Clear these fields out after jitting such that pointer to freed memory (env->used_maps is freed later) are not kept in a live data structure.

The reason why the copies were initially added is explained in
https://lore.kernel.org/bpf/[email protected]

Fixes b4ce5923e780 ("bpf, x86: add new map type: instructions array")
---
 kernel/bpf/verifier.c | 9 +++++++++
 1 file changed, 9 insertions(+)
kernel/bpf/verifier.c +9 -0
index 2e170be647bd..766695491bc5 100644
@@ -22266,6 +22266,15 @@ static int jit_subprogs(struct bpf_verifier_env *env)
 		cond_resched();
 	}
 
+	/*
+	 * Cleanup func[i]->aux fields which aren't required
+	 * or can become invalid in future
+	 */
+	for (i = 0; i < env->subprog_cnt; i++) {
+		func[i]->aux->used_maps = NULL;
+		func[i]->aux->used_map_cnt = 0;
+	}
+
 	/* finally lock prog and jit images for all functions and
 	 * populate kallsysm. Begin at the first subprogram, since
 	 * bpf_prog_load will add the kallsyms for the main program.
Signature · 1 line
2.34.1
#1 lore raw
The patch correctly fixes the use-after-free issue. However, this isn't a bug, but should this have a Cc: [email protected] tag? The bug being fixed affects released kernels where…
Quoted 7 lines
kernel/bpf/verifier.c +0 -0
index 2e170be64..766695491 100644
@@ -22266,6 +22266,15 @@ static int jit_subprogs(struct bpf_verifier_env *env)
 		cond_resched();
 	}

kernel/bpf/verifier.c static int jit_subprogs(struct bpf_verifier_env *env) · line 22277

Quoted 10 lines
kernel/bpf/verifier.c +9 -0
 
+	/*
+	 * Cleanup func[i]->aux fields which aren't required
+	 * or can become invalid in future
+	 */
+	for (i = 0; i < env->subprog_cnt; i++) {
+		func[i]->aux->used_maps = NULL;
+		func[i]->aux->used_map_cnt = 0;
+	}
+

The patch correctly fixes the use-after-free issue. However, this isn't a bug, but should this have a Cc: [email protected] tag? The bug being fixed affects released kernels where bpf_prog_free_deferred() will call bpf_free_used_maps() on the dangling func[i]->aux->used_maps pointer, potentially causing kfree() to be called on already-freed or invalid memory.

kernel/bpf/verifier.c static int jit_subprogs(struct bpf_verifier_env *env) · line 22280

Quoted 3 lines
kernel/bpf/verifier.c +0 -0
 	/* finally lock prog and jit images for all functions and
 	 * populate kallsysm. Begin at the first subprogram, since
 	 * bpf_prog_load will add the kallsyms for the main program.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/19639286497

#2 lore raw
Quoted 8 lines · On 11/24/25 10:30 AM, [email protected] wrote:
Quoted 8 lines
kernel/bpf/verifier.c +0 -0
index 2e170be64..766695491 100644
@@ -22266,6 +22266,15 @@ static int jit_subprogs(struct bpf_verifier_env *env)
 		cond_resched();
 	}
 
Quoted 15 lines

kernel/bpf/verifier.c static int jit_subprogs(struct bpf_verifier_env *env) · line 22277

Quoted 9 lines
kernel/bpf/verifier.c +9 -0
+	/*
+	 * Cleanup func[i]->aux fields which aren't required
+	 * or can become invalid in future
+	 */
+	for (i = 0; i < env->subprog_cnt; i++) {
+		func[i]->aux->used_maps = NULL;
+		func[i]->aux->used_map_cnt = 0;
+	}
+

The patch correctly fixes the use-after-free issue. However, this isn't a bug, but should this have a Cc: [email protected] tag? The bug being fixed affects released kernels where bpf_prog_free_deferred() will call bpf_free_used_maps() on the dangling func[i]->aux->used_maps pointer, potentially causing kfree() to be called on already-freed or invalid memory.

I took a pull request for the review prompts this morning that adds
Fixes: suggestions and verification. If Alexei or others here would rather have this disabled for the BPF reviews, I'll make them default to off.

-chris

#3 lore raw
Quoted 26 lines · Chris Mason
Quoted 8 lines · On 11/24/25 10:30 AM, [email protected] wrote:
Quoted 8 lines
kernel/bpf/verifier.c +0 -0
index 2e170be64..766695491 100644
@@ -22266,6 +22266,15 @@ static int jit_subprogs(struct bpf_verifier_env *env)
             cond_resched();
     }
 
Quoted 15 lines

kernel/bpf/verifier.c static int jit_subprogs(struct bpf_verifier_env *env) · line 22277

Quoted 9 lines
kernel/bpf/verifier.c +9 -0
+    /*
+     * Cleanup func[i]->aux fields which aren't required
+     * or can become invalid in future
+     */
+    for (i = 0; i < env->subprog_cnt; i++) {
+            func[i]->aux->used_maps = NULL;
+            func[i]->aux->used_map_cnt = 0;
+    }
+

The patch correctly fixes the use-after-free issue. However, this isn't a bug, but should this have a Cc: [email protected] tag? The bug being fixed affects released kernels where bpf_prog_free_deferred() will call bpf_free_used_maps() on the dangling func[i]->aux->used_maps pointer, potentially causing kfree() to be called on already-freed or invalid memory.

Quoted 4 lines

I took a pull request for the review prompts this morning that adds
Fixes: suggestions and verification. If Alexei or others here would rather have this disabled for the BPF reviews, I'll make them default to off.

Disable it pls.
In this case Fixes tag point to a commit in bpf-next, so nothing to backport and, in general, we pretty much never do "cc: stable" unless it's a critical fix.
I believe Greg and Sasha don't rely on cc: stable much. Automation will figure things out based on Fixes tag.
cc: stable is an additional signal, but not mandatory, and we prefer to avoid extra lines in the commit log when they are not necessary.

Fixes tag is a different story. We definitely want it for fixes and related changes even when they're not necessarily a fix. But a generic AI comment "should there be a Fixes tag?" will not be useful. If it can say "Should there be Fixes: sha ("bpf: ..")" that would be nice.

#4 lore raw
Quoted 42 lines · Alexei Starovoitov
Quoted 26 lines · Chris Mason
Quoted 8 lines · On 11/24/25 10:30 AM, [email protected] wrote:
Quoted 8 lines
kernel/bpf/verifier.c +0 -0
index 2e170be64..766695491 100644
@@ -22266,6 +22266,15 @@ static int jit_subprogs(struct bpf_verifier_env *env)
             cond_resched();
     }
 
Quoted 15 lines

kernel/bpf/verifier.c static int jit_subprogs(struct bpf_verifier_env *env) · line 22277

Quoted 9 lines
kernel/bpf/verifier.c +9 -0
+    /*
+     * Cleanup func[i]->aux fields which aren't required
+     * or can become invalid in future
+     */
+    for (i = 0; i < env->subprog_cnt; i++) {
+            func[i]->aux->used_maps = NULL;
+            func[i]->aux->used_map_cnt = 0;
+    }
+

The patch correctly fixes the use-after-free issue. However, this isn't a bug, but should this have a Cc: [email protected] tag? The bug being fixed affects released kernels where bpf_prog_free_deferred() will call bpf_free_used_maps() on the dangling func[i]->aux->used_maps pointer, potentially causing kfree() to be called on already-freed or invalid memory.

Quoted 4 lines

I took a pull request for the review prompts this morning that adds
Fixes: suggestions and verification. If Alexei or others here would rather have this disabled for the BPF reviews, I'll make them default to off.

Disable it pls.
In this case Fixes tag point to a commit in bpf-next, so nothing to backport and, in general, we pretty much never do "cc: stable" unless it's a critical fix.
I believe Greg and Sasha don't rely on cc: stable much. Automation will figure things out based on Fixes tag.
cc: stable is an additional signal, but not mandatory, and we prefer to avoid extra lines in the commit log when they are not necessary.

Quoted 5 lines

Fixes tag is a different story. We definitely want it for fixes and related changes even when they're not necessarily a fix. But a generic AI comment "should there be a Fixes tag?" will not be useful. If it can say "Should there be Fixes: sha ("bpf: ..")" that would be nice.

Agreed, it shouldn't be hard for AI to suggest the Fixes: tag and the commit being fixed. I'll work on this.

I've disabled the Fixes: prompt by default, looks like one more review got in before I turned it off.

-chris

#5 lore raw
In reply to Anton Protopopov
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 9 lines · On Mon, 24 Nov 2025 15:15:15 +0000 you wrote:

In commit b4ce5923e780 ("bpf, x86: add new map type: instructions array") env->used_map was copied to func[i]->aux->used_maps before jitting. Clear these fields out after jitting such that pointer to freed memory (env->used_maps is freed later) are not kept in a live data structure.

The reason why the copies were initially added is explained in
https://lore.kernel.org/bpf/[email protected]

[...]

Here is the summary with links:
- [bpf-next] bpf: cleanup aux->used_maps after jit

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

You are awesome, thank you!

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