Skip to contentSkip to conversation

bpf: Fix a data-race around bpf_jit_limit.

PATCH Applied Kuniyuki Iwashima 2 participants 2 messages Aug 23, 2022

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

PATCH v3 1/1 bpf: Fix a data-race around bpf_jit_limit.

While reading bpf_jit_limit, it can be changed concurrently via sysctl, WRITE_ONCE() in __do_proc_doulongvec_minmax(). The size of bpf_jit_limit is long, so we need to add a paired READ_ONCE() to avoid load-tearing.

Fixes ede95a63b5e8 ("bpf: add bpf_jit_limit knob to restrict unpriv allocations")

---
v3:
* Update changelog to clarify paired WRITE_ONCE() and motivation as load-tearing.

v2: https://lore.kernel.org/netdev/[email protected]/
* Drop other 3 patches (No change for this patch)

v1: https://lore.kernel.org/bpf/[email protected]/

---
 kernel/bpf/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/core.c +1 -1
index c1e10d088dbb..3d9eb3ae334c 100644
@@ -971,7 +971,7 @@ pure_initcall(bpf_jit_charge_init);
 
 int bpf_jit_charge_modmem(u32 size)
 {
-	if (atomic_long_add_return(size, &bpf_jit_current) > bpf_jit_limit) {
+	if (atomic_long_add_return(size, &bpf_jit_current) > READ_ONCE(bpf_jit_limit)) {
 		if (!bpf_capable()) {
 			atomic_long_sub(size, &bpf_jit_current);
 			return -EPERM;
Signature · 1 line
2.30.2
#1 lore raw
This patch was applied to bpf/bpf.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This patch was applied to bpf/bpf.git (master)
by Daniel Borkmann <[email protected]>:

Quoted 8 lines · On Tue, 23 Aug 2022 14:58:04 -0700 you wrote:

While reading bpf_jit_limit, it can be changed concurrently via sysctl, WRITE_ONCE() in __do_proc_doulongvec_minmax(). The size of bpf_jit_limit is long, so we need to add a paired READ_ONCE() to avoid load-tearing.

Fixes ede95a63b5e8 ("bpf: add bpf_jit_limit knob to restrict unpriv allocations")

[...]

Here is the summary with links:
- [v3,bpf] bpf: Fix a data-race around bpf_jit_limit.

    https://git.kernel.org/bpf/bpf/c/0947ae112108

You are awesome, thank you!

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