Skip to contentSkip to conversation

bpf: bpf_prog_pack: set proper size before freeing ro_header

PATCH Applied Song Liu 2 participants 2 messages Feb 17, 2022

Subsystems (4)
lore
Share & tools
lore

Song Liu

author reviewer
#0 lore raw

PATCH 1/1 bpf: bpf_prog_pack: set proper size before freeing ro_header

bpf_prog_pack_free() uses header->size to decide whether the header should be freed with module_memfree() or the bpf_prog_pack logic. However, in kvmalloc() failure path of bpf_jit_binary_pack_alloc(), header->size is not set yet. As a result, bpf_prog_pack_free() may treat a slice of a pack as a standalone kvmalloc'd header and call module_memfree() on the whole pack. This in turn causes use-after-free by other users of the pack.

Fix this by setting ro_header->size before freeing ro_header.

Fixes 33c9805860e5 ("bpf: Introduce bpf_jit_binary_pack_[alloc|finalize|free]")
Reported-by [email protected]
Reported-by [email protected]
Reported-by [email protected]
---
 kernel/bpf/core.c | 1 +
 1 file changed, 1 insertion(+)
kernel/bpf/core.c +1 -0
index 44623c9b5bb1..ebb0193d07f0 100644
@@ -1069,6 +1069,7 @@ bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **image_ptr,
 
 	*rw_header = kvmalloc(size, GFP_KERNEL);
 	if (!*rw_header) {
+		bpf_arch_text_copy(&ro_header->size, &size, sizeof(size));
 		bpf_prog_pack_free(ro_header);
 		bpf_jit_uncharge_modmem(size);
 		return NULL;
Signature · 1 line
2.30.2
#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 9 lines · On Thu, 17 Feb 2022 10:30:01 -0800 you wrote:

bpf_prog_pack_free() uses header->size to decide whether the header should be freed with module_memfree() or the bpf_prog_pack logic. However, in kvmalloc() failure path of bpf_jit_binary_pack_alloc(), header->size is not set yet. As a result, bpf_prog_pack_free() may treat a slice of a pack as a standalone kvmalloc'd header and call module_memfree() on the whole pack. This in turn causes use-after-free by other users of the pack.

[...]

Here is the summary with links:
- [bpf-next] bpf: bpf_prog_pack: set proper size before freeing ro_header

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

You are awesome, thank you!

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