Skip to contentSkip to conversation

bpf: Fix a data-race around bpf_jit_limit.

PATCH Superseded Kuniyuki Iwashima 2 participants 3 messages Aug 23, 2022

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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

While reading bpf_jit_limit, it can be changed concurrently. Thus, we need to add READ_ONCE() to its reader.

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

---
v2:
* 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
maintainer
#1 lore raw
Quoted 2 lines · Kuniyuki Iwashima

While reading bpf_jit_limit, it can be changed concurrently. Thus, we need to add READ_ONCE() to its reader.

For sake of a better/clearer commit message, please also provide data about the WRITE_ONCE() pairing that this READ_ONCE() targets. This seems to be the case in __do_proc_doulongvec_minmax() as far as I can see. For your 2nd sentence above please also include load-tearing as main motivation for your fix.

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

---
v2:
* 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;
#2 lore raw
From:   Daniel Borkmann <[email protected]>
Date:   Tue, 23 Aug 2022 23:20:29 +0200
Quoted 8 lines · Kuniyuki Iwashima
Quoted 2 lines

While reading bpf_jit_limit, it can be changed concurrently. Thus, we need to add READ_ONCE() to its reader.

For sake of a better/clearer commit message, please also provide data about the WRITE_ONCE() pairing that this READ_ONCE() targets. This seems to be the case in __do_proc_doulongvec_minmax() as far as I can see. For your 2nd sentence above please also include load-tearing as main motivation for your fix.

I'll add better description.
Thank you!

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

---
v2:
* 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;