BPF List
 help / color / mirror / Atom feed
* [RFC PATCH] bpf: Prevent recursive deadlocks in BPF programs attached to spin lock helpers using fentry/ fexit
@ 2024-01-24 15:43 Siddharth Chintamaneni
  2024-01-30  9:25 ` Jiri Olsa
  0 siblings, 1 reply; 6+ messages in thread
From: Siddharth Chintamaneni @ 2024-01-24 15:43 UTC (permalink / raw)
  To: bpf
  Cc: alexei.starovoitov@gmail.com, daniel@iogearbox.net,
	olsajiri@gmail.com, andrii@kernel.org, Williams, Dan,
	Somaraju, Sai Roop, Sahu, Raj, Craun, Milo,
	sidchintamaneni@vt.edu

While we were working on some experiments with BPF trampoline, we came
across a deadlock scenario that could happen.

A deadlock happens when two nested BPF programs tries to acquire the
same lock i.e, If a BPF program is attached using fexit to
bpf_spin_lock or using a fentry to bpf_spin_unlock, and it then
attempts to acquire the same lock as the previous BPF program, a
deadlock situation arises.

Here is an example:

SEC(fentry/bpf_spin_unlock)
int fentry_2{
  bpf_spin_lock(&x->lock);
  bpf_spin_unlock(&x->lock);
}

SEC(fentry/xxx)
int fentry_1{
  bpf_spin_lock(&x->lock);
  bpf_spin_unlock(&x->lock);
}

To prevent these cases, a simple fix could be adding these helpers to
denylist in the verifier. This fix will prevent the BPF programs from
being loaded by the verifier.

previously, a similar solution was proposed to prevent recursion.
https://lore.kernel.org/lkml/20230417154737.12740-2-laoar.shao@gmail.com/

Signed-off-by: Siddharth Chintamaneni <sidchintamaneni@vt.edu>
---
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 65f598694d55..8f1834f27f81 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -20617,6 +20617,10 @@ BTF_ID(func, preempt_count_sub)
 BTF_ID(func, __rcu_read_lock)
 BTF_ID(func, __rcu_read_unlock)
 #endif
+#if defined(CONFIG_DYNAMIC_FTRACE)
+BTF_ID(func, bpf_spin_lock)
+BTF_ID(func, bpf_spin_unlock)
+#endif
 BTF_SET_END(btf_id_deny)

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-02-07  4:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-24 15:43 [RFC PATCH] bpf: Prevent recursive deadlocks in BPF programs attached to spin lock helpers using fentry/ fexit Siddharth Chintamaneni
2024-01-30  9:25 ` Jiri Olsa
2024-02-03  0:21   ` Siddharth Chintamaneni
2024-02-04 19:09     ` Yonghong Song
2024-02-07  0:21       ` Siddharth Chintamaneni
2024-02-07  4:25         ` Yonghong Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox