riscv, bpf: Adjust bpf_func to account for CFI offset in bpf_jit_free
Subsystems (4)
PATCH 1/1 riscv, bpf: Adjust bpf_func to account for CFI offset in bpf_jit_free
From: Pu Lehui <[email protected]>
When CFI is enabled, the actual BPF program entry point is shifted forward by a CFI preamble. During bpf_jit_free(), this shifted pointer causes the wrong ro_header to be fetched, leading to a potential invalid memory free.
Fix this by subtracting cfi_get_offset() from prog->bpf_func to correctly restore the original JITed allocation address before freeing.
---
- Separate from the patch series [0].
[0] https://lore.kernel.org/bpf/[email protected]
arch/riscv/net/bpf_jit_core.c | 1 + 1 file changed, 1 insertion(+)
arch/riscv/net/bpf_jit_core.c +1 -0
@@ -244,6 +244,7 @@ void bpf_jit_free(struct bpf_prog *prog)kvfree (jit_data->ctx.offset);kfree (jit_data); } + prog->bpf_func = (void *)prog->bpf_func -cfi_get_offset (); hdr =bpf_jit_binary_pack_hdr (prog);bpf_jit_binary_pack_free (hdr,NULL );WARN_ON_ONCE (!bpf_prog_kallsyms_verify_off (prog));
Signature · 1 line
2.34.1
Quoted 7 lines · Pu Lehui
From: Pu Lehui <[email protected]>
When CFI is enabled, the actual BPF program entry point is shifted forward by a CFI preamble. During
bpf_jit_free(), this shifted pointer causes the wrongro_headerto be fetched, leading to a potential invalid memory free.
Quoted 5 lines
Fix this by subtracting
cfi_get_offset()fromprog->bpf_functo correctly restore the original JITed allocation address before freeing.Fixes e63985ecd226 ("bpf, riscv64/cfi: Support kCFI + BPF on riscv64")Signed-off-by Pu Lehui <[email protected]>
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 Wed, 23 Sep 2026 14:42:16 +0000 you wrote:
From: Pu Lehui <[email protected]>
When CFI is enabled, the actual BPF program entry point is shifted forward by a CFI preamble. During
bpf_jit_free(), this shifted pointer causes the wrongro_headerto be fetched, leading to a potential invalid memory free.[...]
Here is the summary with links:
- [bpf-next] riscv, bpf: Adjust bpf_func to account for CFI offset in bpf_jit_free
https://git.kernel.org/bpf/bpf-next/c/93df8ae3267f
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html