Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp4331166rwn; Sun, 11 Sep 2022 09:38:29 -0700 (PDT) X-Google-Smtp-Source: AA6agR49ZQNwz8HB52xUz1kkv3RhamJeCOIIrkroPF3FleSPpXoozubGbhh1VDnZ43EcXbEE6TD0 X-Received: by 2002:a17:903:4054:b0:178:1451:3ca4 with SMTP id n20-20020a170903405400b0017814513ca4mr9850314pla.79.1662914309541; Sun, 11 Sep 2022 09:38:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662914309; cv=none; d=google.com; s=arc-20160816; b=hHHFd4jTZHixtxYNNNBmYB33gyxrlgwdDk3D0pXRVrYPR2isFAqSMCNE0j5GGeRWnO JhtYWycJs3dFHrLCuFtg6LlD44aqRLWZtwWiXTL7hwr+wemAuARvKjrVXGt8z9gtmeQn N2GTYA1qFTQ6LEGELIQyKbth0/V7tUbOKgEoRulQboBM5gQO8tDa6qLplCtwoIU5/eLF tXRhxNM4cGV4u7CKx55mdNliDiqUxv9UkyCxvbyWoPIhr5mCFJUNF4G8Nm701Gn7bXEU 8TzQV1Gb9Ri/7e4k1O4UhodXbvoY5cUanqSncukck4PiurT+sX55DU4mpuO+Hb3uUX28 OQwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:references :cc:to:from:content-language:subject:user-agent:mime-version:date :message-id; bh=Jl7CSjFoUkvMJ7Xw+cqzXoEFzQqU8f6iP2sldvjoF5U=; b=z4OVxga6CrW7s0gp9izL+wTwGjS0GEmfNqeKsk1mgPSqNC0nsnIluoZZxLJXk3HZGM 0RyfwOAzdqhVShqmGhLeIQb/XjJNvXkdnZ0iXV5NJ6nMID9RAO74hqbgDng5sIpyGvSH 0ulhJfLVc5dmPQQCl3W8mLwCBF9PmIVoQutLV/1L3omWoEZ8hvDvbLUtds8e1hfAfFB9 gvQSzCdg/7IpiSaMf/PkgzHaQaF2dIQcxwIVagdsLjj2SHxLtDyyQKEndf5SJxlb1Ust HmqZYFcwJA9EYVY4tlV1L2deO6gOrhC/fc1kYo8Jd/tctGqb6tJmNm7BIPfzrDn8y2BJ +GXw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p4-20020a63f444000000b0042b50f87762si6373033pgk.209.2022.09.11.09.38.05; Sun, 11 Sep 2022 09:38:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229777AbiIKQW1 (ORCPT + 99 others); Sun, 11 Sep 2022 12:22:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229816AbiIKQWU (ORCPT ); Sun, 11 Sep 2022 12:22:20 -0400 Received: from www262.sakura.ne.jp (www262.sakura.ne.jp [202.181.97.72]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58DD11573C for ; Sun, 11 Sep 2022 09:22:04 -0700 (PDT) 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 28BGLoL3041722; Mon, 12 Sep 2022 01:21:50 +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); Mon, 12 Sep 2022 01:21:50 +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 28BGLj2T041708 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NO); Mon, 12 Sep 2022 01:21:50 +0900 (JST) (envelope-from penguin-kernel@I-love.SAKURA.ne.jp) Message-ID: Date: Mon, 12 Sep 2022 01:21:42 +0900 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Subject: [PATCH] Bluetooth: avoid hci_dev_test_and_set_flag() in mgmt_init_hdev() Content-Language: en-US From: Tetsuo Handa To: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz Cc: syzbot , linux-bluetooth@vger.kernel.org, syzkaller-bugs@googlegroups.com References: <000000000000532e0e05e826413c@google.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org syzbot is again reporting attempt to cancel uninitialized work at mgmt_index_removed() [1], for setting of HCI_MGMT flag from mgmt_init_hdev() from hci_mgmt_cmd() from hci_sock_sendmsg() can race with testing of HCI_MGMT flag from mgmt_index_removed() from hci_sock_bind() due to lack of serialization via hci_dev_lock(). Since mgmt_init_hdev() is called with mgmt_chan_list_lock held, we can safely split hci_dev_test_and_set_flag() into hci_dev_test_flag() and hci_dev_set_flag(). Thus, in order to close this race, set HCI_MGMT flag after INIT_DELAYED_WORK() completed. This is a local fix based on mgmt_chan_list_lock. Lack of serialization via hci_dev_lock() might be causing different race conditions somewhere else. But a global fix based on hci_dev_lock() should deserve a future patch. Link: https://syzkaller.appspot.com/bug?extid=844c7bf1b1aa4119c5de Reported-by: syzbot+844c7bf1b1aa4119c5de@syzkaller.appspotmail.com Signed-off-by: Tetsuo Handa Fixes: 3f2893d3c142986a ("Bluetooth: don't try to cancel uninitialized works at mgmt_index_removed()") --- net/bluetooth/mgmt.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c index 72e6595a71cc..3d1cd0666968 100644 --- a/net/bluetooth/mgmt.c +++ b/net/bluetooth/mgmt.c @@ -1050,7 +1050,7 @@ static void discov_off(struct work_struct *work) static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) { - if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) + if (hci_dev_test_flag(hdev, HCI_MGMT)) return; BT_INFO("MGMT ver %d.%d", MGMT_VERSION, MGMT_REVISION); @@ -1065,6 +1065,8 @@ static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) * it */ hci_dev_clear_flag(hdev, HCI_BONDABLE); + + hci_dev_set_flag(hdev, HCI_MGMT); } static int read_controller_info(struct sock *sk, struct hci_dev *hdev, -- 2.18.4