Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3811986pxv; Tue, 13 Jul 2021 04:29:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzpRA3i6ViJ+yh0H5jGdwq7OlT4dMmoZxlbbRv7bZI+nd0gmvoZ8cDbGt5XFU0+NRwh+8Tf X-Received: by 2002:a92:8707:: with SMTP id m7mr2590102ild.177.1626175771157; Tue, 13 Jul 2021 04:29:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626175771; cv=none; d=google.com; s=arc-20160816; b=cbLL517EUBBxM6vkjK64P7tN08fzXY3cq68wTnEGEYhcpa1tcLrKhpzxi+jmIwOqIA EDZAKZXATAHVTetrPtg57I3vsMqn4V6QC7/tIzjZWwCnnGxYCN/8sNFqfhci4hqmhQdo fEU3cvW2kmPHmeXx7hN3NtQ+MCwQ0mc2kn+Dng6ZiuqsEZiLEx1twpiPxG82ZxUXovqS 5xnsKpBYcP0Sy+Yx2SwrchCqchJFAmJ9Cl89H4JN9p7iFWPlKSMhAqvTr04BqCXE4u6r gQLDhwOwzvPHeTMBtGac0pmriTuDYOrLq4YvbhfSkhIYcgOrHK8Mn0D8TpRfam8IsAao GSsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:references:cc :to:from:subject; bh=iBJSbyIJGvkbXzA+jjPVBcZYMddiqge09jpT/qM01AE=; b=vmlazKzR7VACGpDoXcvjnhyqHXxj7NXxtBmdqO7aPJulrckelKZdTYUOpXXV9NR/cc Ewpet+tLNN5JBwcJbq/m7+rzf0ati3b/rAPNvJWaWXy3Q7DQhotLshy0g/L8ZroxT4hO IX01jTgYPBaaPQbWu9Q1MORochxJPn2B0lYo4zhpU8Q1Pio00kbLSofMge4qmaQicsMt HbF/l/Rs2zwW65wl2KNPrbx6D8uOCjRwrX3zzrMg6RuYdAYC23FfrK4SP5wcDTCqanyN Ii6giPZFGyDCZhlV2c2HT/yu7hS0l8NQ3XpbOKCSgZkUP2AVyT8iTCyk0QakMdogK821 iA8g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w4si23924063iov.14.2021.07.13.04.29.02; Tue, 13 Jul 2021 04:29:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235709AbhGMLbC (ORCPT + 99 others); Tue, 13 Jul 2021 07:31:02 -0400 Received: from www262.sakura.ne.jp ([202.181.97.72]:56390 "EHLO www262.sakura.ne.jp" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235574AbhGMLbC (ORCPT ); Tue, 13 Jul 2021 07:31:02 -0400 Received: from fsav313.sakura.ne.jp (fsav313.sakura.ne.jp [153.120.85.144]) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTP id 16DBRpRX036255; Tue, 13 Jul 2021 20:27:51 +0900 (JST) (envelope-from penguin-kernel@I-love.SAKURA.ne.jp) Received: from www262.sakura.ne.jp (202.181.97.72) by fsav313.sakura.ne.jp (F-Secure/fsigk_smtp/550/fsav313.sakura.ne.jp); Tue, 13 Jul 2021 20:27:51 +0900 (JST) X-Virus-Status: clean(F-Secure/fsigk_smtp/550/fsav313.sakura.ne.jp) Received: from [192.168.1.9] (M106072142033.v4.enabler.ne.jp [106.72.142.33]) (authenticated bits=0) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTPSA id 16DBRp4q036252 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NO); Tue, 13 Jul 2021 20:27:51 +0900 (JST) (envelope-from penguin-kernel@I-love.SAKURA.ne.jp) Subject: [PATCH v3] Bluetooth: call lock_sock() outside of spinlock section From: Tetsuo Handa To: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , linux-bluetooth@vger.kernel.org Cc: "David S. Miller" , Jakub Kicinski , Lin Ma , netdev@vger.kernel.org References: <20210627131134.5434-1-penguin-kernel@I-love.SAKURA.ne.jp> <9deece33-5d7f-9dcb-9aaa-94c60d28fc9a@i-love.sakura.ne.jp> Message-ID: <48d66166-4d39-4fe2-3392-7e0c84b9bdb3@i-love.sakura.ne.jp> Date: Tue, 13 Jul 2021 20:27:49 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <9deece33-5d7f-9dcb-9aaa-94c60d28fc9a@i-love.sakura.ne.jp> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org 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@i-love.sakura.ne.jp [2] Reported-by: syzbot Signed-off-by: Tetsuo Handa Tested-by: syzbot 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. 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