Re: [PATCH v3] Bluetooth: call lock_sock() outside of spinlock section

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Tetsuo,

On Tue, Jul 13, 2021 at 4:28 AM Tetsuo Handa
<penguin-kernel@xxxxxxxxxxxxxxxxxxx> wrote:
>
> syzbot is hitting might_sleep() warning at hci_sock_dev_event() due to
> calling lock_sock() with rw spinlock held [1]. Among three possible
> approaches [2], this patch chose holding a refcount via sock_hold() and
> revalidating the element via sk_hashed().
>
> Link: https://syzkaller.appspot.com/bug?extid=a5df189917e79d5e59c9 [1]
> Link: https://lkml.kernel.org/r/05535d35-30d6-28b6-067e-272d01679d24@xxxxxxxxxxxxxxxxxxx [2]
> Reported-by: syzbot <syzbot+a5df189917e79d5e59c9@xxxxxxxxxxxxxxxxxxxxxxxxx>
> Signed-off-by: Tetsuo Handa <penguin-kernel@xxxxxxxxxxxxxxxxxxx>
> Tested-by: syzbot <syzbot+a5df189917e79d5e59c9@xxxxxxxxxxxxxxxxxxxxxxxxx>
> Fixes: e305509e678b3a4a ("Bluetooth: use correct lock to prevent UAF of hdev object")
> ---
> Changes in v3:
>   Don't use unlocked hci_pi(sk)->hdev != hdev test, for it is racy.
>   No need to defer hci_dev_put(hdev), for it can't be the last reference.
>
> Changes in v2:
>   Take hci_sk_list.lock for write in case bt_sock_unlink() is called after
>   sk_hashed(sk) test, and defer hci_dev_put(hdev) till schedulable context.

How about we revert back to use bh_lock_sock_nested but use
local_bh_disable like the following patch:

https://patchwork.kernel.org/project/bluetooth/patch/20210713162838.693266-1-desmondcheongzx@xxxxxxxxx/

>  net/bluetooth/hci_sock.c | 30 +++++++++++++++++++++++++++++-
>  1 file changed, 29 insertions(+), 1 deletion(-)
>
> diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c
> index b04a5a02ecf3..786a06a232fd 100644
> --- a/net/bluetooth/hci_sock.c
> +++ b/net/bluetooth/hci_sock.c
> @@ -760,10 +760,18 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event)
>                 struct sock *sk;
>
>                 /* Detach sockets from device */
> +restart:
>                 read_lock(&hci_sk_list.lock);
>                 sk_for_each(sk, &hci_sk_list.head) {
> +                       /* This sock_hold(sk) is safe, for bt_sock_unlink(sk)
> +                        * is not called yet.
> +                        */
> +                       sock_hold(sk);
> +                       read_unlock(&hci_sk_list.lock);
>                         lock_sock(sk);
> -                       if (hci_pi(sk)->hdev == hdev) {
> +                       write_lock(&hci_sk_list.lock);
> +                       /* Check that bt_sock_unlink(sk) is not called yet. */
> +                       if (sk_hashed(sk) && hci_pi(sk)->hdev == hdev) {
>                                 hci_pi(sk)->hdev = NULL;
>                                 sk->sk_err = EPIPE;
>                                 sk->sk_state = BT_OPEN;
> @@ -771,7 +779,27 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event)
>
>                                 hci_dev_put(hdev);
>                         }
> +                       write_unlock(&hci_sk_list.lock);
>                         release_sock(sk);
> +                       read_lock(&hci_sk_list.lock);
> +                       /* If bt_sock_unlink(sk) is not called yet, we can
> +                        * continue iteration. We can use __sock_put(sk) here
> +                        * because hci_sock_release() will call sock_put(sk)
> +                        * after bt_sock_unlink(sk).
> +                        */
> +                       if (sk_hashed(sk)) {
> +                               __sock_put(sk);
> +                               continue;
> +                       }
> +                       /* Otherwise, we need to restart iteration, for the
> +                        * next socket pointed by sk->next might be already
> +                        * gone. We can't use __sock_put(sk) here because
> +                        * hci_sock_release() might have already called
> +                        * sock_put(sk) after bt_sock_unlink(sk).
> +                        */
> +                       read_unlock(&hci_sk_list.lock);
> +                       sock_put(sk);
> +                       goto restart;
>                 }
>                 read_unlock(&hci_sk_list.lock);
>         }
> --
> 2.18.4
>


-- 
Luiz Augusto von Dentz



[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux