Skip to contentSkip to conversation

kernel: bpf: syscall: fix a possible sleep-in-atomic bug in __bpf_prog_put()

PATCH In review [email protected] 2 participants 4 messages May 30, 2023 – May 31, 2023

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

PATCH v2 1/1 kernel: bpf: syscall: fix a possible sleep-in-atomic bug in __bpf_prog_put()

From: Teng Qi <[email protected]>

__bpf_prog_put() indirectly calls kvfree() through bpf_prog_put_deferred() which is unsafe under atomic context. The current
condition ‘in_irq() || irqs_disabled()’ in __bpf_prog_put() to ensure safety does not cover cases involving the spin lock region and rcu read lock region. Since __bpf_prog_put() is called by various callers in kernel/, net/ and drivers/, and potentially more in future, it is necessary to handle those cases as well.

Although we haven`t found a proper way to identify the rcu read lock region, we have noticed that vfree() calls vfree_atomic() with the condition 'in_interrupt()' to ensure safety.

To make __bpf_prog_put() safe in practice, we propose calling bpf_prog_put_deferred() with the condition 'in_interrupt()' and using the work queue for any other context.

We also added a comment to indicate that the safety of __bpf_prog_put() relies implicitly on the implementation of vfree().

---
v2:
remove comments because of self explanatory of code.

Fixes d809e134be7a ("bpf: Prepare bpf_prog_put() to be called from irq context.")
---
 kernel/bpf/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/syscall.c +1 -1
index 14f39c1e573e..96658e5874be 100644
@@ -2099,7 +2099,7 @@ static void __bpf_prog_put(struct bpf_prog *prog)
 	struct bpf_prog_aux *aux = prog->aux;
 
 	if (atomic64_dec_and_test(&aux->refcnt)) {
-		if (in_irq() || irqs_disabled()) {
+		if (!in_interrupt()) {
 			INIT_WORK(&aux->work, bpf_prog_put_deferred);
 			schedule_work(&aux->work);
 		} else {
Signature · 1 line
2.25.1
#1 lore raw
Quoted 10 lines · On 5/30/23 12:06 AM, [email protected] wrote:

From: Teng Qi <[email protected]>

__bpf_prog_put() indirectly calls kvfree() through bpf_prog_put_deferred() which is unsafe under atomic context. The current
condition ‘in_irq() || irqs_disabled()’ in __bpf_prog_put() to ensure safety does not cover cases involving the spin lock region and rcu read lock region. Since __bpf_prog_put() is called by various callers in kernel/, net/ and drivers/, and potentially more in future, it is necessary to handle those cases as well.

Quoted 3 lines

Although we haven`t found a proper way to identify the rcu read lock region, we have noticed that vfree() calls vfree_atomic() with the condition 'in_interrupt()' to ensure safety.

I would really like you to create a test case
to demonstrate with a rcu or spin-lock warnings based on existing code base. With a test case, it would hard to see whether we need this patch or not.

Quoted 8 lines

To make __bpf_prog_put() safe in practice, we propose calling bpf_prog_put_deferred() with the condition 'in_interrupt()' and using the work queue for any other context.

We also added a comment to indicate that the safety of __bpf_prog_put() relies implicitly on the implementation of vfree().

Quoted 6 lines

---
v2:
remove comments because of self explanatory of code.

Fixes d809e134be7a ("bpf: Prepare bpf_prog_put() to be called from irq context.")

Please put 'Fixes' right before 'Signed-off-by' in the above.

Quoted 9 lines
---
  kernel/bpf/syscall.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/syscall.c +0 -0
index 14f39c1e573e..96658e5874be 100644

kernel/bpf/syscall.c static void __bpf_prog_put(struct bpf_prog *prog) · line 2102

Quoted 5 lines
kernel/bpf/syscall.c +1 -1
@@ -2099,7 +2099,7 @@ static void __bpf_prog_put(struct bpf_prog *prog)
  	struct bpf_prog_aux *aux = prog->aux;
 
  	if (atomic64_dec_and_test(&aux->refcnt)) {
-		if (in_irq() || irqs_disabled()) {
+		if (!in_interrupt()) {

Could we have cases where in software context we have irqs_disabled()?

Quoted 3 lines
kernel/bpf/syscall.c +0 -0
  			INIT_WORK(&aux->work, bpf_prog_put_deferred);
  			schedule_work(&aux->work);
  		} else {
author
#2 lore raw
Quoted 4 lines

I would really like you to create a test case
to demonstrate with a rcu or spin-lock warnings based on existing code base. With a test case, it would hard to see whether we need this patch or not.

Ok, I will try to construct a test case.

Quoted 1 line

Please put 'Fixes' right before 'Signed-off-by' in the above.

Ok.

Quoted 1 line

Could we have cases where in software context we have irqs_disabled()?

What do you mean about software context?

Quoted 60 lines · Yonghong Song
Quoted 10 lines · On 5/30/23 12:06 AM, [email protected] wrote:

From: Teng Qi <[email protected]>

__bpf_prog_put() indirectly calls kvfree() through bpf_prog_put_deferred() which is unsafe under atomic context. The current
condition ‘in_irq() || irqs_disabled()’ in __bpf_prog_put() to ensure safety does not cover cases involving the spin lock region and rcu read lock region. Since __bpf_prog_put() is called by various callers in kernel/, net/ and drivers/, and potentially more in future, it is necessary to handle those cases as well.

Quoted 3 lines

Although we haven`t found a proper way to identify the rcu read lock region, we have noticed that vfree() calls vfree_atomic() with the condition 'in_interrupt()' to ensure safety.

I would really like you to create a test case
to demonstrate with a rcu or spin-lock warnings based on existing code base. With a test case, it would hard to see whether we need this patch or not.

Quoted 8 lines

To make __bpf_prog_put() safe in practice, we propose calling bpf_prog_put_deferred() with the condition 'in_interrupt()' and using the work queue for any other context.

We also added a comment to indicate that the safety of __bpf_prog_put() relies implicitly on the implementation of vfree().

Quoted 6 lines

---
v2:
remove comments because of self explanatory of code.

Fixes d809e134be7a ("bpf: Prepare bpf_prog_put() to be called from irq context.")

Please put 'Fixes' right before 'Signed-off-by' in the above.

Quoted 9 lines
---
  kernel/bpf/syscall.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/syscall.c +0 -0
index 14f39c1e573e..96658e5874be 100644

kernel/bpf/syscall.c static void __bpf_prog_put(struct bpf_prog *prog) · line 2102

Quoted 5 lines
kernel/bpf/syscall.c +1 -1
@@ -2099,7 +2099,7 @@ static void __bpf_prog_put(struct bpf_prog *prog)
      struct bpf_prog_aux *aux = prog->aux;
 
      if (atomic64_dec_and_test(&aux->refcnt)) {
-             if (in_irq() || irqs_disabled()) {
+             if (!in_interrupt()) {

Could we have cases where in software context we have irqs_disabled()?

Quoted 3 lines
kernel/bpf/syscall.c +0 -0
                      INIT_WORK(&aux->work, bpf_prog_put_deferred);
                      schedule_work(&aux->work);
              } else {
#3 lore raw
Quoted 7 lines · Teng Qi
Quoted 4 lines

I would really like you to create a test case
to demonstrate with a rcu or spin-lock warnings based on existing code base. With a test case, it would hard to see whether we need this patch or not.

Ok, I will try to construct a test case.

Quoted 7 lines
Quoted 1 line

Please put 'Fixes' right before 'Signed-off-by' in the above.

Ok.

Quoted 1 line

Could we have cases where in software context we have irqs_disabled()?

What do you mean about software context?

sorry. i mean softirq context.

Quoted 62 lines · Yonghong Song
Quoted 60 lines · On 5/30/23 12:06 AM, [email protected] wrote:
Quoted 10 lines

From: Teng Qi <[email protected]>

__bpf_prog_put() indirectly calls kvfree() through bpf_prog_put_deferred() which is unsafe under atomic context. The current
condition ‘in_irq() || irqs_disabled()’ in __bpf_prog_put() to ensure safety does not cover cases involving the spin lock region and rcu read lock region. Since __bpf_prog_put() is called by various callers in kernel/, net/ and drivers/, and potentially more in future, it is necessary to handle those cases as well.

Quoted 3 lines

Although we haven`t found a proper way to identify the rcu read lock region, we have noticed that vfree() calls vfree_atomic() with the condition 'in_interrupt()' to ensure safety.

I would really like you to create a test case
to demonstrate with a rcu or spin-lock warnings based on existing code base. With a test case, it would hard to see whether we need this patch or not.

Quoted 8 lines

To make __bpf_prog_put() safe in practice, we propose calling bpf_prog_put_deferred() with the condition 'in_interrupt()' and using the work queue for any other context.

We also added a comment to indicate that the safety of __bpf_prog_put() relies implicitly on the implementation of vfree().

Quoted 6 lines

---
v2:
remove comments because of self explanatory of code.

Fixes d809e134be7a ("bpf: Prepare bpf_prog_put() to be called from irq context.")

Please put 'Fixes' right before 'Signed-off-by' in the above.

Quoted 9 lines
---
   kernel/bpf/syscall.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/syscall.c +0 -0
index 14f39c1e573e..96658e5874be 100644

kernel/bpf/syscall.c static void __bpf_prog_put(struct bpf_prog *prog) · line 2102

Quoted 5 lines
kernel/bpf/syscall.c +1 -1
@@ -2099,7 +2099,7 @@ static void __bpf_prog_put(struct bpf_prog *prog)
       struct bpf_prog_aux *aux = prog->aux;
 
       if (atomic64_dec_and_test(&aux->refcnt)) {
-             if (in_irq() || irqs_disabled()) {
+             if (!in_interrupt()) {

Could we have cases where in software context we have irqs_disabled()?

Quoted 3 lines
kernel/bpf/syscall.c +0 -0
                       INIT_WORK(&aux->work, bpf_prog_put_deferred);
                       schedule_work(&aux->work);
               } else {