Skip to content

Commit ff91059

Browse files
jsitnickiborkmann
authored andcommitted
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. Fixes: 604326b ("bpf, sockmap: convert to generic sk_msg interface") Reported-by: xingwei lee <[email protected]> Reported-by: yue sun <[email protected]> Reported-by: [email protected] Reported-by: [email protected] Signed-off-by: Jakub Sitnicki <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Tested-by: [email protected] Acked-by: John Fastabend <[email protected]> Closes: https://syzkaller.appspot.com/bug?extid=d4066896495db380182e Closes: https://syzkaller.appspot.com/bug?extid=bc922f476bd65abbd466 Link: https://lore.kernel.org/bpf/[email protected]
1 parent 8c3fe02 commit ff91059

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

net/core/sock_map.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
411411
struct sock *sk;
412412
int err = 0;
413413

414+
if (irqs_disabled())
415+
return -EOPNOTSUPP; /* locks here are hardirq-unsafe */
416+
414417
spin_lock_bh(&stab->lock);
415418
sk = *psk;
416419
if (!sk_test || sk_test == sk)
@@ -933,6 +936,9 @@ static long sock_hash_delete_elem(struct bpf_map *map, void *key)
933936
struct bpf_shtab_elem *elem;
934937
int ret = -ENOENT;
935938

939+
if (irqs_disabled())
940+
return -EOPNOTSUPP; /* locks here are hardirq-unsafe */
941+
936942
hash = sock_hash_bucket_hash(key, key_size);
937943
bucket = sock_hash_select_bucket(htab, hash);
938944

0 commit comments

Comments
 (0)