Received: by 2002:a05:6520:4211:b029:f4:110d:56bc with SMTP id o17csp2110200lkv; Thu, 20 May 2021 03:39:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwgC/mKRY6gCQFzVPzR5BlHOPltWydWj0kxJ2aHdvwdRMFErRqsHkXaWAS/6EUwtoylFCEU X-Received: by 2002:a05:6602:2b0a:: with SMTP id p10mr4373387iov.129.1621507147721; Thu, 20 May 2021 03:39:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621507147; cv=none; d=google.com; s=arc-20160816; b=WFsr7q8mtyWzeSUu38A1VkkurytirL9oMeJIQqB2FyRWI017A0UQidKao6pTlrJ8IY wR6KgPZhy6JX6Cnu8IbPhwETZ4nuUda5nPLyRkzECZXPp/uQrR21+pC4oDF2If5qxAHX IXKdBbejpM12GpXYpnLyQNe8pYN0E4A1XQ+sBf4t27JCHZV/fm9g+2TuEtkl84jxgti4 38QUpEoimjWyJwER19QTTbe46+E4OM0oW+oyjJs39YnxW2lYOsZPOT5UIcgs6DVVtv8A 0E7CA0OcNYZjx1CoxKKnpjpcBGdFCxsec7o5YA04a37y3klQr98CxOkk5frp0GIUHuBI cg/A== 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=BPXDxiHlT2iFbZ8LGIsAgeNDmtusLd5jOBAoxurITAY=; b=LcbS6BeaF06ZG3LyTTHLZoFNQCHPp69QUM/haWgMqV4Izk+j7ii1C0BVEHlIQXA+o3 iJT4rf6HHE+E5Qe2SV40cxSBcKdrtO2fjnaoZXdkHdzt7IDGegsSdggsgiHkM+fsdghP Df+WGRT53ENb3Qk3JKry+Y+r3+vu82/Fj3kmdZFrD4Jnkh5XHpxlcYtDNsLGA66ftugW CoOzcsb8qq3ia4ym1G7hXuORkxORHlkDAdsWHNSoirb9+zetWBMssDglRF0OITrjD0zU n39t600j3rcQDZi+alB2XFDTxZOJDN9VGrgGTcXSU4Bt5yuFzV1Q97Mf5UcNzIw+F+z5 1Mdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SivyFesK; 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 y3si1833854iom.73.2021.05.20.03.38.54; Thu, 20 May 2021 03:39:07 -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=SivyFesK; 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 S234342AbhETKjD (ORCPT + 99 others); Thu, 20 May 2021 06:39:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:55680 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236604AbhETKYV (ORCPT ); Thu, 20 May 2021 06:24:21 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A05F161AC0; Thu, 20 May 2021 09:49:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621504147; bh=5I85wcDe0+tu8kRv34Vd5ogkqy9/kT8uNV8rcXe5NBc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SivyFesKDnhE+jEaHT5OF3PA6vgnjpGO43xUTg7fGoYW/m76jzEeJF+jpnBNINFRh NzSpeekP3F4buS9sPI4Ry+Y7Vg7/l2rHHrTyBbyqoV6ybhe0T5/6vuRz9FFdbUs4BP 3pAGJgMvoj8HNtTZRwXGGB5JF5gQspjFeAqwI4ZY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lin Ma , Marcel Holtmann Subject: [PATCH 4.14 113/323] bluetooth: eliminate the potential race condition when removing the HCI controller Date: Thu, 20 May 2021 11:20:05 +0200 Message-Id: <20210520092123.971935784@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092120.115153432@linuxfoundation.org> References: <20210520092120.115153432@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 @@ -277,12 +277,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;