bpf, sockmap: Prevent lock inversion deadlock in map delete elem
Subsystems (5)
Jakub Sitnicki
maintainerPATCH 1/1 bpf, sockmap: Prevent lock inversion deadlock in map delete elem
syzkaller started using corpuses where a BPF tracing program deletes elements from a sockmap/sockhash map. Because BPF tracing programs can be invoked from any interrupt context, locks taken during a map_delete_elem operation must be hardirq-safe. Otherwise a deadlock due to lock inversion is possible, as reported by lockdep:
CPU0 CPU1
---- ----
lock(&htab->buckets[i].lock);
local_irq_disable();
lock(&host->lock);
lock(&htab->buckets[i].lock);
<Interrupt>
lock(&host->lock);Locks in sockmap are hardirq-unsafe by design. We expects elements to be deleted from sockmap/sockhash only in task (normal) context with interrupts enabled, or in softirq context.
Detect when map_delete_elem operation is invoked from a context which is _not_ hardirq-unsafe, that is interrupts are disabled, and bail out with an error.
Note that map updates are not affected by this issue. BPF verifier does not allow updating sockmap/sockhash from a BPF tracing program today.
---
Cc: John Fastabend <[email protected]>
Cc: Edward Adam Davis <[email protected]>
Cc: Shung-Hsi Yu <[email protected]>
--- net/core/sock_map.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/core/sock_map.c +6 -0
@@ -411,6 +411,9 @@ static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,struct sock *sk;int err =0 ; +if (irqs_disabled ()) +return -EOPNOTSUPP ;/* locks here are hardirq-unsafe */ +spin_lock_bh (&stab->lock); sk = *psk;if (!sk_test || sk_test == sk)
@@ -933,6 +936,9 @@ static long sock_hash_delete_elem(struct bpf_map *map, void *key)struct bpf_shtab_elem *elem;int ret = -ENOENT ; +if (irqs_disabled ()) +return -EOPNOTSUPP ;/* locks here are hardirq-unsafe */ + hash =sock_hash_bucket_hash (key, key_size); bucket =sock_hash_select_bucket (htab, hash);
Signature · 1 line
2.44.0
Quoted 34 lines · Jakub Sitnicki
syzkaller started using corpuses where a BPF tracing program deletes elements from a sockmap/sockhash map. Because BPF tracing programs can be invoked from any interrupt context, locks taken during a
map_delete_elemoperation must be hardirq-safe. Otherwise a deadlock due to lock inversion is possible, as reported by lockdep:CPU0 CPU1 ---- ---- lock(&htab->buckets[i].lock); local_irq_disable(); lock(&host->lock); lock(&htab->buckets[i].lock); <Interrupt> lock(&host->lock);Locks in sockmap are hardirq-unsafe by design. We expects elements to be deleted from sockmap/sockhash only in task (normal) context with interrupts enabled, or in softirq context.
Detect when
map_delete_elemoperation is invoked from a context which is _not_ hardirq-unsafe, that is interrupts are disabled, and bail out with an error.Note that map updates are not affected by this issue. BPF verifier does not allow updating sockmap/sockhash from a BPF tracing program today.
Reported-by xingwei lee <[email protected]>Reported-by yue sun <[email protected]>Reported-by [email protected]Reported-and-tested-by [email protected]Fixes 604326b41a6f ("bpf, sockmap: convert to generic sk_msg interface")Signed-off-by Jakub Sitnicki <[email protected]>
Quoted 7 lines
---
Cc: John Fastabend <[email protected]>
Cc: Edward Adam Davis <[email protected]>
Cc: Shung-Hsi Yu <[email protected]>--- net/core/sock_map.c | 6 ++++++ 1 file changed, 6 insertions(+)
Agree.
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 9 lines · On Tue, 2 Apr 2024 12:46:21 +0200 you wrote:
syzkaller started using corpuses where a BPF tracing program deletes elements from a sockmap/sockhash map. Because BPF tracing programs can be invoked from any interrupt context, locks taken during a
map_delete_elemoperation must be hardirq-safe. Otherwise a deadlock due to lock inversion is possible, as reported by lockdep:CPU0 CPU1
[...]
Here is the summary with links:
- [bpf] bpf, sockmap: Prevent lock inversion deadlock in map delete elem
https://git.kernel.org/bpf/bpf/c/ff9105993240
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html