This is a note to let you know that I've just added the patch titled bpf, sockmap: Prevent lock inversion deadlock in map delete elem to the 5.4-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: bpf-sockmap-prevent-lock-inversion-deadlock-in-map-delete-elem.patch and it can be found in the queue-5.4 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From ff91059932401894e6c86341915615c5eb0eca48 Mon Sep 17 00:00:00 2001 From: Jakub Sitnicki <jakub@xxxxxxxxxxxxxx> Date: Tue, 2 Apr 2024 12:46:21 +0200 Subject: bpf, sockmap: Prevent lock inversion deadlock in map delete elem From: Jakub Sitnicki <jakub@xxxxxxxxxxxxxx> commit ff91059932401894e6c86341915615c5eb0eca48 upstream. 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: 604326b41a6f ("bpf, sockmap: convert to generic sk_msg interface") Reported-by: xingwei lee <xrivendell7@xxxxxxxxx> Reported-by: yue sun <samsun1006219@xxxxxxxxx> Reported-by: syzbot+bc922f476bd65abbd466@xxxxxxxxxxxxxxxxxxxxxxxxx Reported-by: syzbot+d4066896495db380182e@xxxxxxxxxxxxxxxxxxxxxxxxx Signed-off-by: Jakub Sitnicki <jakub@xxxxxxxxxxxxxx> Signed-off-by: Daniel Borkmann <daniel@xxxxxxxxxxxxx> Tested-by: syzbot+d4066896495db380182e@xxxxxxxxxxxxxxxxxxxxxxxxx Acked-by: John Fastabend <john.fastabend@xxxxxxxxx> Closes: https://syzkaller.appspot.com/bug?extid=d4066896495db380182e Closes: https://syzkaller.appspot.com/bug?extid=bc922f476bd65abbd466 Link: https://lore.kernel.org/bpf/20240402104621.1050319-1-jakub@xxxxxxxxxxxxxx Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- net/core/sock_map.c | 6 ++++++ 1 file changed, 6 insertions(+) --- a/net/core/sock_map.c +++ b/net/core/sock_map.c @@ -321,6 +321,9 @@ static int __sock_map_delete(struct bpf_ struct sock *sk; int err = 0; + if (irqs_disabled()) + return -EOPNOTSUPP; /* locks here are hardirq-unsafe */ + raw_spin_lock_bh(&stab->lock); sk = *psk; if (!sk_test || sk_test == sk) @@ -654,6 +657,9 @@ static int sock_hash_delete_elem(struct struct bpf_htab_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); Patches currently in stable-queue which might be from jakub@xxxxxxxxxxxxxx are queue-5.4/bpf-sockmap-prevent-lock-inversion-deadlock-in-map-delete-elem.patch