Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1018873pxb; Wed, 6 Apr 2022 06:52:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwehsoBb94xnfBk2tE50OGQt60fvOZHA+ZS1RKOh7UtPRRk9DcsDIhyYd4gSK5U5WrXpFEx X-Received: by 2002:a17:902:e803:b0:154:2699:7f39 with SMTP id u3-20020a170902e80300b0015426997f39mr8522638plg.172.1649253150685; Wed, 06 Apr 2022 06:52:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649253150; cv=none; d=google.com; s=arc-20160816; b=xg56lhpEYC4CNAxKDhPDuqH3wq0CQOkSL2ueNMGL732q0uHyztK6MX8S2mnWxPVVl2 LfItD2TVd9gTkvslzTWi0+/buYgXiESp3RwHkksJpwGTv+wC6lrSSWXbfBBNOXck3CMW aVKIwwe+jNCGpLxbyEam/+ia1RysjVq3oJvBAhA/9RfgWMEv6CMAkT0NBMosSVdBmWR5 oX4Fm4asTGieWzl0alskxigxv6n7xltfm+OslPbIQGLgt49AI7KU5YgsIpF7cJsmFhA4 tHB6ddHMo8M94IDbU0mp1ZnOAJ10a4n+Qzya9nOnLCrJ77mU6JWxRAcCvNav2vOuOE+Z qVfg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=qWDLuBMv69l7smkyAs5PVU2OS4u5uUsp7PBl7TVy2v4=; b=QMOkowG205HSeBaufjz9TB/YjuL+6cmSzf4+1jbV7t3HsR7RMRbPT/vgWRQ5U1OOlp rHdXklBaD+xGfB8fguyfnOYUb8MNuHU1RdQN4TkOpukXKqiCgHYFrs7LC5JnjLI6UnZm dKryZmLK7Hl7/nJOaZeSxj+Gd1RejWHJm63eVlcuJb4by5sSkuMXeWBXNzCBb8UVHQRJ f4xdMabTHk+cywwT0kLDIM5Xcy5KvwjNvzO8QBc9xkjz1Mjds8KmeWAZLxzzmxjM+EXH 9rfwpa2Li19tMkXhfTZFgqmuOhIwJyTxhpQxFSlUn5pzV6L9HscOppqqjk0B0cp5stgb +ayw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=qON21d56; spf=softfail (google.com: domain of transitioning linux-bluetooth-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id m22-20020a170902bb9600b00153b2d16548si14237648pls.336.2022.04.06.06.52.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 06:52:30 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-bluetooth-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=qON21d56; spf=softfail (google.com: domain of transitioning linux-bluetooth-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 97B4E4B196B; Wed, 6 Apr 2022 04:46:56 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358052AbiDFAle (ORCPT + 99 others); Tue, 5 Apr 2022 20:41:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1572983AbiDERkJ (ORCPT ); Tue, 5 Apr 2022 13:40:09 -0400 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43B67B8202 for ; Tue, 5 Apr 2022 10:38:10 -0700 (PDT) Received: by mail-ej1-x62c.google.com with SMTP id bg10so28264567ejb.4 for ; Tue, 05 Apr 2022 10:38:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=qWDLuBMv69l7smkyAs5PVU2OS4u5uUsp7PBl7TVy2v4=; b=qON21d56MpM/bjOhhzQEfHJKs0ihdNmcrFgvwIuNLigXVdqEzo6ZoR0Ld23t3chaHH XlSq03hl7sOg//P/2hzVg6g6Of/vUeYj7GIQeJ73+YSCg5uSJkPzO1L6tlqWUa7fl74B z2QQNvA/+glkjWOXigxdvR+/hwIh5a/L/wokwf0eAlTRxC0NdoDcopb1Hr8iQYCHOtXW fFsWaZ4uWAKiq49hQqPn6eao6ceK5wHm8ElJydIe2dfPZN/jIHNi44cHj0YDf9DryL2e kqfnEOfKc1Xtfrol0XyxDMHhjWAesoKCe3uUDsp3IEkCtkGN7VH0YyBES76V4OwskLjo JeUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=qWDLuBMv69l7smkyAs5PVU2OS4u5uUsp7PBl7TVy2v4=; b=fqpAJ5kHfOGckZ5ibvhfoMPBYM/Qv4GAomZVStLxeir/Pke9QbNutcu2++1RyzZnO3 D0qedbK3IA1J46XRMd9QIQosX/vQoSTS8yh9AthKzPbskpqsVTXMLzUJiJeIcYSAJr8G E24ZEt/3td6ZyPt0Wuhqb3FAFG3yvpxh9AgHfttK9Bl56woxSMlxOTBiAoTa49efpjor +lqnUmRf99evgj1LYdcFIB5KjToHWkKy1G3nUcnAbAmmukwSX3AVw8y8lXtFeHjKKHjX HjPR4n3xWav3gBv8BW2eXHpRddMjxzax0AbIqCfs1Tvseg0120bIuL4YX4ws1C1/rJoC 5flQ== X-Gm-Message-State: AOAM531RX/se+Sl8FtxG5Y36qYvRuVNUZfPcWgkSYtmIXWVsjaKQdaYW Qrf7cb1BJ0ORCC1PkmbArCfj3ZDcVzQ= X-Received: by 2002:a17:906:730c:b0:6e6:c512:49c8 with SMTP id di12-20020a170906730c00b006e6c51249c8mr4686781ejc.405.1649180288841; Tue, 05 Apr 2022 10:38:08 -0700 (PDT) Received: from nlaptop.localdomain (ptr-dtfv0poj8u7zblqwbt6.18120a2.ip6.access.telenet.be. [2a02:1811:cc83:eef0:f2b6:6987:9238:41ca]) by smtp.gmail.com with ESMTPSA id x3-20020a50d9c3000000b0041c8ce4bcd7sm5646330edj.63.2022.04.05.10.38.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 10:38:08 -0700 (PDT) From: Niels Dossche To: linux-bluetooth@vger.kernel.org Cc: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , Niels Dossche Subject: [PATCH v2] Bluetooth: use hdev lock for accept_list and reject_list in conn req Date: Tue, 5 Apr 2022 19:37:52 +0200 Message-Id: <20220405173751.7269-1-dossche.niels@gmail.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 All accesses (both reads and modifications) to hdev->{accept,reject}_list are protected by hdev lock, except the ones in hci_conn_request_evt. This can cause a race condition in the form of a list corruption. The solution is to protect these lists in hci_conn_request_evt as well. I was unable to find the exact commit that introduced the issue for the reject list, I was only able to find it for the accept list. Fixes: a55bd29d5227 ("Bluetooth: Add white list lookup for incoming connection requests") Signed-off-by: Niels Dossche --- Changes in v2: - edited commit message to exclude note - used an exit goto label instead Note: I am currently working on a static analyser to detect missing locks using type-based static analysis as my master's thesis in order to obtain my master's degree. If you would like to have more details, please let me know. This was a reported case. I manually verified the report by looking at the code, so that I do not send wrong information or patches. After concluding that this seems to be a true positive, I created this patch. I have both compile-tested this patch and runtime-tested this patch on x86_64. The effect on a running system could be a potential race condition in exceptional cases. This issue was found on Linux v5.17.1. net/bluetooth/hci_event.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c index abaabfae19cc..02a77f676da4 100644 --- a/net/bluetooth/hci_event.c +++ b/net/bluetooth/hci_event.c @@ -3222,10 +3222,12 @@ static void hci_conn_request_evt(struct hci_dev *hdev, void *data, return; } + hci_dev_lock(hdev); + if (hci_bdaddr_list_lookup(&hdev->reject_list, &ev->bdaddr, BDADDR_BREDR)) { hci_reject_conn(hdev, &ev->bdaddr); - return; + goto unlock; } /* Require HCI_CONNECTABLE or an accept list entry to accept the @@ -3237,13 +3239,11 @@ static void hci_conn_request_evt(struct hci_dev *hdev, void *data, !hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, &ev->bdaddr, BDADDR_BREDR)) { hci_reject_conn(hdev, &ev->bdaddr); - return; + goto unlock; } /* Connection accepted */ - hci_dev_lock(hdev); - ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr); if (ie) memcpy(ie->data.dev_class, ev->dev_class, 3); @@ -3255,8 +3255,7 @@ static void hci_conn_request_evt(struct hci_dev *hdev, void *data, HCI_ROLE_SLAVE); if (!conn) { bt_dev_err(hdev, "no memory for new connection"); - hci_dev_unlock(hdev); - return; + goto unlock; } } @@ -3296,6 +3295,10 @@ static void hci_conn_request_evt(struct hci_dev *hdev, void *data, conn->state = BT_CONNECT2; hci_connect_cfm(conn, 0); } + + return; +unlock: + hci_dev_unlock(hdev); } static u8 hci_to_mgmt_reason(u8 err) -- 2.35.1