Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2705079pxv; Sun, 27 Jun 2021 06:21:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxJIk9zUc/PTZFHvhVRExD+EP7qd983W5CzcWPUcsz6duXN8fBNI8PYGMWXJFuO4bJ35Vys X-Received: by 2002:a05:6402:411:: with SMTP id q17mr27079204edv.313.1624800079191; Sun, 27 Jun 2021 06:21:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624800079; cv=none; d=google.com; s=arc-20160816; b=c9GTGAsAWREB77HHY6p0q8ZD13Am42+5MsnGTaaEymjTtFs1M5N+JwzidyboxWaZCp Q6QvPt6ENxYHgHsg1HnftSdg06Y3EyLJ1DPi0iR4Z6OE562QJvzqQ6vkJIIVWM6oaFPO E6KBZdbjXPTo9jXD1crJ3Ko3cfVhuGGVY99qpwzUJhlTffZvmWTpS7Jc8g5UWcZ0oUwx kP2fITr3LuRGKSldYLsfjPMeuip6IG+uV6moDYLbMiE1H9JFtGqUEwvLrsTlbEUz3UH6 ZquKJikrKE2ry2yyWP5+rP+hbsgk45eHGy18d6w5Nj17BccppTqC2Fx1gkAf3X/vT8eZ s95A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=k0n61o3odap5vfxppPxfCCX9IBXrUzL9bAI5+2bb2EA=; b=VIGOStdkVZunEX10hZ0y0bjAS52CBQcVS5sv5LOLWhgbbBAEjsiJvLDqbhjh5wrBFT 2OaOtrZrdB70XTh1XkJ/j0sbTmUbAGxILRKtJT+g6GtoGsb7xFx9kLIt6PtbQ8rVNV9a 0SJtun1530T7qG/BaMcQSGC59dcv3B85Hc5rri4miIu2TczfEae065CM1JLrQvv6Qke0 rNZH1rMd27Lk7Mln1WP6EeWRAiJ1Tb38+brrqd4782zjKS3NEobiS5fMu+YcEk9tGJSz vi0y3Crcw/wYO2AvoO7Ncn0VBiTwAUxjmySOt0c/lzLKtL85tDrxVHdmm7QyF4bMDpoE 9SVA== 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 jy15si11046499ejc.397.2021.06.27.06.20.36; Sun, 27 Jun 2021 06:21:19 -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 S230146AbhF0NOn (ORCPT + 99 others); Sun, 27 Jun 2021 09:14:43 -0400 Received: from www262.sakura.ne.jp ([202.181.97.72]:57553 "EHLO www262.sakura.ne.jp" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229817AbhF0NOm (ORCPT ); Sun, 27 Jun 2021 09:14:42 -0400 Received: from fsav115.sakura.ne.jp (fsav115.sakura.ne.jp [27.133.134.242]) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTP id 15RDBh50091179; Sun, 27 Jun 2021 22:11:43 +0900 (JST) (envelope-from penguin-kernel@I-love.SAKURA.ne.jp) Received: from www262.sakura.ne.jp (202.181.97.72) by fsav115.sakura.ne.jp (F-Secure/fsigk_smtp/550/fsav115.sakura.ne.jp); Sun, 27 Jun 2021 22:11:43 +0900 (JST) X-Virus-Status: clean(F-Secure/fsigk_smtp/550/fsav115.sakura.ne.jp) Received: from localhost.localdomain (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 15RDBcTs091155 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Sun, 27 Jun 2021 22:11:43 +0900 (JST) (envelope-from penguin-kernel@I-love.SAKURA.ne.jp) 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, Tetsuo Handa Subject: [PATCH] Bluetooth: call lock_sock() outside of spinlock section Date: Sun, 27 Jun 2021 22:11:34 +0900 Message-Id: <20210627131134.5434-1-penguin-kernel@I-love.SAKURA.ne.jp> X-Mailer: git-send-email 2.18.4 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") --- net/bluetooth/hci_sock.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c index eed0dd066e12..64e54ab0892f 100644 --- a/net/bluetooth/hci_sock.c +++ b/net/bluetooth/hci_sock.c @@ -759,19 +759,39 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event) if (event == HCI_DEV_UNREG) { struct sock *sk; +restart: /* 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. + */ + 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); } release_sock(sk); + sock_put(sk); + /* Restarting is safe, for hci_pi(sk)->hdev is now NULL + * if condition met and will "continue;" otherwise. + */ + goto restart; } read_unlock(&hci_sk_list.lock); } -- 2.18.4