Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4706705pxj; Wed, 12 May 2021 11:20:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxLcHk7wp5OUVeJa5+evvzoW1XzJ0Bh7/Ow/WsBKSQt/bl/MCigYyGGElwLETIV3kOhSYKE X-Received: by 2002:adf:8bd4:: with SMTP id w20mr11092069wra.192.1620843618239; Wed, 12 May 2021 11:20:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620843618; cv=none; d=google.com; s=arc-20160816; b=jUncUFf5+uEsws/Urthof05N3WErnMCjPV5OP+Gs+wXcCX/W3cDKP0YQLpjHpYgoCT zn4SsfbrAe5bR3TAOnbWUa+hF0PBrlEdWkDdPXtwil7FkZOLzvWtLwNUX4LGN8zamfZq RRxgdCHi7LEgK/OTONLn3HmLA6jC1CbuplFGqYoYpqUDco2MBgw1Y6k405dm5y8EJFVS RjlPpT+ZaFQjSk2qKbY9PQgxnHyGEB/Dlh7PJucSNBG9MeTMEhEORO9Zo/vc6Al0vw5L ttMZvELeEvQIRKlsMQSgd0N6ra7UCY73Gq+CE4Pdi/tD2HE9lWCUWz9x7etzKVI+gbuz IrLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=r+AUlVym7EUODBgvM1XIFRqc3F/3aXQDmfvdA9o70/Q=; b=r8XPaq3TOdYhlYlO2fgXg2mNSq1d5tjwTBAWQQ+NIbh/C28/QqikOiEeM8yHVzPXLt qkicKSRZpNCZLvzVyM4KpBHM4fNd2Z9ljGhRxyMnTBMgZMlj0i/kSmQs1HantaJYq3fD j/zPadKMxcTjSpCVvsr/HAl0QZrRQmChKehwaflaQhqp1nHX3PDRNC8heyHSCYl8omCw MDRR/0lsfwLtmntHS/Nh9hFr7vCGPmLgcmckr3EB0KxiANJvXoSAYPYLRgFoDcZ/JlG+ Dv9SVu8En4DLE/wOtBqXrg7dKhOM5/5ScEWHw05MVUjcdNEh5zFgjfI38zEPzIxgVlaw NFhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WvpOqOVF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s6si409250wmc.20.2021.05.12.11.19.53; Wed, 12 May 2021 11:20:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WvpOqOVF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353953AbhELSP5 (ORCPT + 99 others); Wed, 12 May 2021 14:15:57 -0400 Received: from mail.kernel.org ([198.145.29.99]:42878 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241197AbhELQ0v (ORCPT ); Wed, 12 May 2021 12:26:51 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 824BE61DC9; Wed, 12 May 2021 15:50:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620834627; bh=u/vCJMWqveRKayYgIkYTkKMVbM/LUpDQ7arthnLfzpk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WvpOqOVFgKxlqaSd8LWGhMoPF9Vf06SVJAnDtZP55MGVBtJMOUlFDxluXVU57aO/v SYhSrMYyb8+2ivbkYHWHLCUuqhjOkilfMb2gELo5vuxdeMdn9yBvQn6QIx0njNokc3 04W3Q5FjnHtAq8XvIsAbYUsUrbTwLEdQxnNV3Eo8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lin Ma , Marcel Holtmann Subject: [PATCH 5.12 002/677] bluetooth: eliminate the potential race condition when removing the HCI controller Date: Wed, 12 May 2021 16:40:48 +0200 Message-Id: <20210512144837.290499032@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144837.204217980@linuxfoundation.org> References: <20210512144837.204217980@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Lin Ma commit e2cb6b891ad2b8caa9131e3be70f45243df82a80 upstream. There is a possible race condition vulnerability between issuing a HCI command and removing the cont. Specifically, functions hci_req_sync() and hci_dev_do_close() can race each other like below: thread-A in hci_req_sync() | thread-B in hci_dev_do_close() | hci_req_sync_lock(hdev); test_bit(HCI_UP, &hdev->flags); | ... | test_and_clear_bit(HCI_UP, &hdev->flags) hci_req_sync_lock(hdev); | | In this commit we alter the sequence in function hci_req_sync(). Hence, the thread-A cannot issue th. Signed-off-by: Lin Ma Cc: Marcel Holtmann Fixes: 7c6a329e4447 ("[Bluetooth] Fix regression from using default link policy") Signed-off-by: Greg Kroah-Hartman --- net/bluetooth/hci_request.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) --- a/net/bluetooth/hci_request.c +++ b/net/bluetooth/hci_request.c @@ -272,12 +272,16 @@ int hci_req_sync(struct hci_dev *hdev, i { int ret; - if (!test_bit(HCI_UP, &hdev->flags)) - return -ENETDOWN; - /* Serialize all requests */ hci_req_sync_lock(hdev); - ret = __hci_req_sync(hdev, req, opt, timeout, hci_status); + /* check the state after obtaing the lock to protect the HCI_UP + * against any races from hci_dev_do_close when the controller + * gets removed. + */ + if (test_bit(HCI_UP, &hdev->flags)) + ret = __hci_req_sync(hdev, req, opt, timeout, hci_status); + else + ret = -ENETDOWN; hci_req_sync_unlock(hdev); return ret;