Received: by 2002:aa6:da0e:0:b029:115:a171:fe4c with SMTP id z14csp1258959lkb; Wed, 7 Jul 2021 02:46:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwMc+hf4wtl++P9CV+jEM05kNzNdEK6+u36ADcF9dqj0OdzNH8g58WpsJp0+LwPOHLYYCjC X-Received: by 2002:a17:906:3402:: with SMTP id c2mr23034904ejb.213.1625651163857; Wed, 07 Jul 2021 02:46:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625651163; cv=none; d=google.com; s=arc-20160816; b=u3z77BH4EEoj/HD+wMLTC0F2hLWA6GSjplCv9emqNa08KjgyltrFymJrFVDBYQKT7l 8KDFVcrJGesECstAct5AmOTRho08BQEEfWOVxaDPZ0OVtwvmho2EsQuodRJQlZdZXfsB 82YsIMiZF1A6BNJi1wGeLVERjOX5vft8AYzhsUwI246l3A9CcfgNVL4Fq80o3VKN3lqI DSLQvC3B799kM4/EQ/KuTk/lOXO282DrBFPY19pUsf60OruKBLJKD6VxesWuMS4eJ0C+ n2OtWTWA+GxXJbQCfIUA8LmjHrsdCJI8PBO1tyJ8gnPwJvxGxEbNq7iFf4cgHMv7r+LK naCw== 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:from:references :cc:to:subject; bh=u0KMpUen2WfoRiq1KOXrlDQYYtjgIHEuoTRaPcxZLgE=; b=LqxrKb3fp14aGRzqqNWdjP9socSaQEqWHkcHmPfR+ZmgWJX0qESqR+N0ZHJWiyOjT5 T4Cn3aL+QwD5lCwaJXculXcnmc6KzUjDkQtJFtlNwutGKQe+1kSPeXzJHtUH/q6g6sc1 xtqknoDRl/qYHLZ3T2M7mBx8nh6vP7yg1+7lZWJrcQQ8PbF+9xoTzhal8jDI/nALWsPp FFwofM3yzq8w0nue/ax26uh1SisHMbkxov4ZlSZPOEOSiu4Fn5vQcP8wyFtfOk8xTl9z 9n2Zn2UWl4OQ2x0AKUecZyYjasiPn0j2ih3VwU2oiGgym0GvdXMbyceFpyDsojtOrcAi VulA== 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 dn2si12614878ejc.136.2021.07.07.02.45.20; Wed, 07 Jul 2021 02:46:03 -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 S230489AbhGGJqe (ORCPT + 99 others); Wed, 7 Jul 2021 05:46:34 -0400 Received: from www262.sakura.ne.jp ([202.181.97.72]:55757 "EHLO www262.sakura.ne.jp" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230498AbhGGJqd (ORCPT ); Wed, 7 Jul 2021 05:46:33 -0400 Received: from fsav411.sakura.ne.jp (fsav411.sakura.ne.jp [133.242.250.110]) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTP id 1679hawo045341; Wed, 7 Jul 2021 18:43:36 +0900 (JST) (envelope-from penguin-kernel@i-love.sakura.ne.jp) Received: from www262.sakura.ne.jp (202.181.97.72) by fsav411.sakura.ne.jp (F-Secure/fsigk_smtp/550/fsav411.sakura.ne.jp); Wed, 07 Jul 2021 18:43:36 +0900 (JST) X-Virus-Status: clean(F-Secure/fsigk_smtp/550/fsav411.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 1679haQ0045338 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NO); Wed, 7 Jul 2021 18:43:36 +0900 (JST) (envelope-from penguin-kernel@i-love.sakura.ne.jp) Subject: [PATCH v2] Bluetooth: call lock_sock() outside of spinlock section 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> From: Tetsuo Handa Message-ID: <9deece33-5d7f-9dcb-9aaa-94c60d28fc9a@i-love.sakura.ne.jp> Date: Wed, 7 Jul 2021 18:43:36 +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: <20210627131134.5434-1-penguin-kernel@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]. Defer calling lock_sock() via sock_hold(). Link: https://syzkaller.appspot.com/bug?extid=a5df189917e79d5e59c9 [1] 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 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 | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c index b04a5a02ecf3..d8e1ac1ae10d 100644 --- a/net/bluetooth/hci_sock.c +++ b/net/bluetooth/hci_sock.c @@ -758,20 +758,46 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event) if (event == HCI_DEV_UNREG) { struct sock *sk; + bool put_dev; +restart: + put_dev = false; /* Detach sockets from device */ read_lock(&hci_sk_list.lock); sk_for_each(sk, &hci_sk_list.head) { + /* hci_sk_list.lock is preventing hci_sock_release() + * from calling bt_sock_unlink(). + */ + if (hci_pi(sk)->hdev != hdev || sk_unhashed(sk)) + continue; + /* Take a ref because we can't call lock_sock() with + * hci_sk_list.lock held. + */ + sock_hold(sk); + read_unlock(&hci_sk_list.lock); lock_sock(sk); - if (hci_pi(sk)->hdev == hdev) { + /* Since hci_sock_release() might have already called + * bt_sock_unlink() while waiting for lock_sock(), + * use sk_hashed(sk) for checking that bt_sock_unlink() + * is not yet called. + */ + write_lock(&hci_sk_list.lock); + if (sk_hashed(sk) && hci_pi(sk)->hdev == hdev) { hci_pi(sk)->hdev = NULL; sk->sk_err = EPIPE; sk->sk_state = BT_OPEN; sk->sk_state_change(sk); - - hci_dev_put(hdev); + put_dev = true; } + write_unlock(&hci_sk_list.lock); release_sock(sk); + sock_put(sk); + if (put_dev) + hci_dev_put(hdev); + /* Restarting is safe, for hci_pi(sk)->hdev != hdev if + * condition met and sk_unhashed(sk) == true otherwise. + */ + goto restart; } read_unlock(&hci_sk_list.lock); } -- 2.18.4