Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp310059pxj; Thu, 20 May 2021 09:49:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwQmlS6ytx+yVw/xyzV+3uqqboEntf/H5nywJJ1NxP4uBKQIsXFDqkxqE6vgAlcqERLwk/Q X-Received: by 2002:a17:906:9bf3:: with SMTP id de51mr5485671ejc.394.1621529372452; Thu, 20 May 2021 09:49:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621529372; cv=none; d=google.com; s=arc-20160816; b=v/raN2DGLBf3YG8zsizsDTC16DRMDnNjchWVmiBeneX2BHOi6uNWZhCuCnXV9z03YJ DNaCYfWqEju+jc03JIfIBM+JFjEkMM3EHXzYrfsD+HKkofJdzAHY1We/HhZy9gAlF/aW F3N2tQCY0ulzYDrH7Rhs//HCOYlcs7bBdjd/St6xqtGlzBXB31ZI3sjnEWjLpU29k9XA Y8Jhu9Rbti7c+rCzIgUBJ9kmo/VViVlcrcSzAq9i2wdiJVx7kS5K7Bs+6ULRMdGKANFK QIoaGCeQQHtmbuh4JHvTTsqf5Sf+1KdBfCM6tpieDLQa/l6LNI3Cwk6wiwZyLym0B1x4 30+w== 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=ri9A+8Hl7SRfMZo2Gmr4TwudkdZCdfcrnuMbiYakpao=; b=yheIRzV/3BnrNEXJyTuHlgvQtNXCvRK745sCTQYhF5bOmzcD8rEIUsWtcjxg9fZ0WX 2FhsnsdefgvhiYLK+cHD5vBEs35C7iw5SWw2Iij369j3ZE3khMAu4DOBwSi+KXTPpsaZ mbRI/S7xm0AV5KvThAJvKHg4nhmItkmsJov/Kitd762/7PDiokVMuV0aTw4M4Ht86oNk okVzkp1fU9ofRFWfng2rAw+1Y1FkA3qrgAC0VSgLu9Eo3bNJyrBfzGmhcvbNjsei1eOb n7oG7eX6YOvFlKhc1OocCeEGUHZAv7BA4w54dZqxaEypWGCIffCTQmBtTaDD39fIlH2Q Mhxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=w+WyTYTi; 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 y15si693576ejw.484.2021.05.20.09.49.06; Thu, 20 May 2021 09:49:32 -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=w+WyTYTi; 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 S234184AbhETJsu (ORCPT + 99 others); Thu, 20 May 2021 05:48:50 -0400 Received: from mail.kernel.org ([198.145.29.99]:47674 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233727AbhETJqS (ORCPT ); Thu, 20 May 2021 05:46:18 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 508A2613F2; Thu, 20 May 2021 09:33:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621503234; bh=U3fyrcHCCNg6Ujw55OfZ638vS2vjwcPYCyTNs7gGx2I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=w+WyTYTikx0RE1JNsLs5lBFjRz9hudpdIpeMVQA2g22qgCfhVUJmCpaoJo/RQsGyI 8DdcwPon0MXJoFwjjIpJng19Y4lPUT0Z3MmYh6tTAnPMUrkkf8fG1cV3GUjDKQsVtB F5m0Tq4GdC1WXkqhufywnFcSdjaUeKczmqkCW/ak= 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.19 129/425] bluetooth: eliminate the potential race condition when removing the HCI controller Date: Thu, 20 May 2021 11:18:18 +0200 Message-Id: <20210520092135.675573809@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092131.308959589@linuxfoundation.org> References: <20210520092131.308959589@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 @@ -271,12 +271,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;