* [PATCH] kernel: bpf: syscall: fix a possible sleep-in-atomic bug in __bpf_prog_put()
@ 2023-05-29 15:53 starmiku1207184332
2023-05-30 4:54 ` Ratheesh Kannoth
0 siblings, 1 reply; 3+ messages in thread
From: starmiku1207184332 @ 2023-05-29 15:53 UTC (permalink / raw)
To: ast, daniel, john.fastabend, andrii, martin.lau, song, yhs,
kpsingh, sdf, haoluo, jolsa, davem, kuba, hawk
Cc: bpf, linux-kernel, netdev, Teng Qi
From: Teng Qi <starmiku1207184332@gmail.com>
__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().
Signed-off-by: Teng Qi <starmiku1207184332@gmail.com>
---
kernel/bpf/syscall.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 14f39c1e573e..48ff5d2e163a 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2099,10 +2099,12 @@ 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()) {
+ // safely calling vfree() under any context
INIT_WORK(&aux->work, bpf_prog_put_deferred);
schedule_work(&aux->work);
} else {
+ // depending on the vfree_atomic() branch in vfree()
bpf_prog_put_deferred(&aux->work);
}
}
--
2.25.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH] kernel: bpf: syscall: fix a possible sleep-in-atomic bug in __bpf_prog_put()
2023-05-29 15:53 [PATCH] kernel: bpf: syscall: fix a possible sleep-in-atomic bug in __bpf_prog_put() starmiku1207184332
@ 2023-05-30 4:54 ` Ratheesh Kannoth
0 siblings, 0 replies; 3+ messages in thread
From: Ratheesh Kannoth @ 2023-05-30 4:54 UTC (permalink / raw)
To: starmiku1207184332, ast, daniel, john.fastabend, andrii,
martin.lau, song, yhs, kpsingh, sdf, haoluo, jolsa, davem, kuba,
hawk
Cc: bpf, linux-kernel, netdev
From: starmiku1207184332@gmail.com
>
>+ // safely calling vfree() under any context
do we need this comment as code is self explanatory.
> INIT_WORK(&aux->work, bpf_prog_put_deferred);
> schedule_work(&aux->work);
> } else {
>+ // depending on the vfree_atomic() branch in vfree()
same as above.
> bpf_prog_put_deferred(&aux->work);
> }
> }
>--
>2.25.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH] kernel: bpf: syscall: fix a possible sleep-in-atomic bug in __bpf_prog_put()
@ 2023-05-29 15:48 starmiku1207184332
0 siblings, 0 replies; 3+ messages in thread
From: starmiku1207184332 @ 2023-05-29 15:48 UTC (permalink / raw)
To: ast, daniel, john.fastabend, andrii, martin.lau, song, yhs,
kpsingh, sdf, haoluo, jolsa, davem, kuba, hawk
Cc: bpf, linux-kernel, netdev, Teng Qi
From: Teng Qi <starmiku1207184332@gmail.com>
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().
Signed-off-by: Teng Qi <starmiku1207184332@gmail.com>
---
kernel/bpf/syscall.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 14f39c1e573e..48ff5d2e163a 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2099,10 +2099,12 @@ 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()) {
+ // safely calling vfree() under any context
INIT_WORK(&aux->work, bpf_prog_put_deferred);
schedule_work(&aux->work);
} else {
+ // depending on the vfree_atomic() branch in vfree()
bpf_prog_put_deferred(&aux->work);
}
}
--
2.25.1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-05-30 4:54 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-29 15:53 [PATCH] kernel: bpf: syscall: fix a possible sleep-in-atomic bug in __bpf_prog_put() starmiku1207184332
2023-05-30 4:54 ` Ratheesh Kannoth
-- strict thread matches above, loose matches on Subject: below --
2023-05-29 15:48 starmiku1207184332
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®