bpf: potential double-free of env->insn_aux_data
Eduard Zingerman
maintainerPATCH 1/1 bpf: potential double-free of env->insn_aux_data
Function bpf_patch_insn_data() has the following structure:
static struct bpf_prog *bpf_patch_insn_data(... env ...)
{
struct bpf_prog *new_prog;
struct bpf_insn_aux_data *new_data = NULL; if (len > 1) {
new_data = vrealloc(...); // <--------- (1)
if (!new_data)
return NULL;
env->insn_aux_data = new_data; // <---- (2)
}
new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
if (IS_ERR(new_prog)) {
...
vfree(new_data); // <----------------- (3)
return NULL;
}
... happy path ...}
In case if bpf_patch_insn_single() returns an error the new_data allocated at (1) will be freed at (3). However, at (2) this pointer is stored in env->insn_aux_data. Which is freed unconditionally by verifier.c:bpf_check() on both happy and error paths. Thus, leading to double-free.
Fix this by removing vfree() call at (3), ownership over new_data is already passed to env->insn_aux_data at this point.
--- kernel/bpf/verifier.c | 1 - 1 file changed, 1 deletion(-)
kernel/bpf/verifier.c +0 -1
@@ -20781,7 +20781,6 @@ static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 ofverbose (env,"insn %d cannot be patched due to 16-bit range\n" , env->insn_aux_data[off].orig_idx); -vfree (new_data);return NULL ; }adjust_insn_aux_data (env, new_data, new_prog, off, len);
---
base-commit: 22f20375f5b71f30c0d6896583b93b6e4bba7279
change-id: 20250912-patch-insn-data-double-free-f7be62b9c4a9
Daniel Borkmann
maintainerQuoted 45 lines · Eduard Zingerman
Function
bpf_patch_insn_data()has the following structure:static struct bpf_prog *bpf_patch_insn_data(... env ...) { struct bpf_prog *new_prog; struct bpf_insn_aux_data *new_data = NULL;if (len > 1) { new_data = vrealloc(...); // <--------- (1) if (!new_data) return NULL; env->insn_aux_data = new_data; // <---- (2) } new_prog = bpf_patch_insn_single(env->prog, off, patch, len); if (IS_ERR(new_prog)) { ... vfree(new_data); // <----------------- (3) return NULL; } ... happy path ...}
In case if
bpf_patch_insn_single()returns an error thenew_dataallocated at (1) will be freed at (3). However, at (2) this pointer is stored inenv->insn_aux_data. Which is freed unconditionally by verifier.c:bpf_check() on both happy and error paths. Thus, leading to double-free.Fix this by removing
vfree()call at (3), ownership overnew_datais already passed toenv->insn_aux_dataat this point.Fixes 77620d126739 ("bpf: use realloc in bpf_patch_insn_data")Reported-by Chris Mason <[email protected]>Signed-off-by Eduard Zingerman <[email protected]>--- kernel/bpf/verifier.c | 1 - 1 file changed, 1 deletion(-)kernel/bpf/verifier.c +0
-0
kernel/bpf/verifier.c static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 of · line 20784
Quoted 4 lines
kernel/bpf/verifier.c +0
-1@@ -20781,7 +20781,6 @@ static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 ofverbose (env,"insn %d cannot be patched due to 16-bit range\n" , env->insn_aux_data[off].orig_idx); -vfree (new_data);
I presume you mean bpf-next tree, otherwise we'd be adding a memory leak into bpf tree?
kernel/bpf/verifier.c static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 of · line 20786
Quoted 3 lines
kernel/bpf/verifier.c +0
-0return NULL ; }adjust_insn_aux_data (env, new_data, new_prog, off, len);
Thanks,
Daniel
Eduard Zingerman
maintainerOn Mon, 2025-09-15 at 13:42 +0200, Daniel Borkmann wrote:
[...]
Quoted 11 lines
kernel/bpf/verifier.c static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 of · line 20784
Quoted 9 lines
kernel/bpf/verifier.c +0
-1@@ -20781,7 +20781,6 @@ static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 ofverbose (env,"insn %d cannot be patched due to 16-bit range\n" , env->insn_aux_data[off].orig_idx); -vfree (new_data);I presume you mean bpf-next tree, otherwise we'd be adding a memory leak into bpf tree?
Hi Daniel,
Yes, the tag should be bpf-next.
Will resend with the correct tag.
I apologize, this was sloppy on my side.
[...]
Quoted 17 lines · Eduard Zingerman
On Mon, 2025-09-15 at 13:42 +0200, Daniel Borkmann wrote:
[...]
Quoted 11 lines
kernel/bpf/verifier.c static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 of · line 20784
Quoted 9 lines
kernel/bpf/verifier.c +0
-1@@ -20781,7 +20781,6 @@ static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 ofverbose (env,"insn %d cannot be patched due to 16-bit range\n" , env->insn_aux_data[off].orig_idx); -vfree (new_data);I presume you mean bpf-next tree, otherwise we'd be adding a memory leak into bpf tree?
Quoted 4 lines
Hi Daniel,
Yes, the tag should be bpf-next.
Will resend with the correct tag.
No need to resend.