Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp6835894rwn; Tue, 13 Sep 2022 09:36:40 -0700 (PDT) X-Google-Smtp-Source: AA6agR6D4sp7/laBj867YVtDWGCaPlJnNAuI27I4ifUsk7aOL5zOksUw50nrVK9HO0qUS3Z8g3yu X-Received: by 2002:a17:90b:1d0e:b0:202:809c:2d70 with SMTP id on14-20020a17090b1d0e00b00202809c2d70mr170555pjb.14.1663087000641; Tue, 13 Sep 2022 09:36:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663087000; cv=none; d=google.com; s=arc-20160816; b=cijEgMLBj4/wANeoUfqcajmLLNl/gqV5nkqv7gNsJr9SO/o6u+372A1c/h1plB19se FP2K4Ep7K0IgrFprp1ZLzQVcD8YATY1joBoV9a9UciExSeY0DQGR0xFLjK7QzexNxVFe 5RcgBiWMmc+sOIpeW87W4n7Jz/ejNyud+d8HtDy0r3Q3M64S3RmP5N8EXAE+t7MkgVYh c7K/Rr2CrS5W20qfLbggRY97EL79VXv8lnHYE/AawJLCObYV7SZqpKpGB/8bmN+ulFnb gMmnH5OQj79B8YamAOsbCrKm9Uhg3vCNpbytRBnwkAKZoXmYbQMNfIgpViwPVnjqx9zv 7D9w== 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=xVJTuw3QuQuOeoFl+heMsboXqywN3ahaS8uuijHe0dI=; b=xRv1OQ2Er8kP+3pFQ0ZhKOQCwEkp6mloXoybQX6LOp+qz9wFHWBlTxERy3xtMITJql 8RDjLSZ049D6iTj9S8AblZGe3brINLX2F3uH2g7EjtxRsiw6m7rQe0Sc3nKQJhFrKwAZ jWcipSmXlklj7msLWC3ehgidODln9ReFkIzZpXdNK2gGy4gCn40L82sza2R4jhBzo513 WbQzcsEWtG7k7cMZrrME7VQcpf3ClN8y/m8/8SRmEYNNdTD2YFVXNZQuOteBsSaFYr31 4IZhIMzWnZUOSdRV3lEBRyRvP/EVIZfR2AVA79x15yeMCxZwPXPbGU0TnKHRFUYVkEj0 SP/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=pP2N9Tx5; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id az3-20020a056a02004300b004391c48220fsi5362470pgb.257.2022.09.13.09.36.09; Tue, 13 Sep 2022 09:36:40 -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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=pP2N9Tx5; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231296AbiIMQ1b (ORCPT + 99 others); Tue, 13 Sep 2022 12:27:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230371AbiIMQ1R (ORCPT ); Tue, 13 Sep 2022 12:27:17 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 179B3AC272 for ; Tue, 13 Sep 2022 08:22:25 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id y17so23276143ejo.6 for ; Tue, 13 Sep 2022 08:22:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date; bh=xVJTuw3QuQuOeoFl+heMsboXqywN3ahaS8uuijHe0dI=; b=pP2N9Tx570/xZ5sQDB7YbgtO+zKmGpeeEEKojeDT+4EECmVsWLXPivOE5X9w9jJase /249QKoyQKiAJhm9Jo78mWINt8Jf/js61StwBFkqy/NBnOyYalz9rSx1p6ODnES7cwCI sFp6PeO1cPlsT40Kqm+lC3xv2Zkbmag0ooBfOlA7WqfbEGch40FSXDkbd3Lh4o+80V1b n68ffMXuI5uEIHYPzSY5c79kG1PkC5AmiJ+e2gcGC78E4sy0+lOxwR5GRGQt2BWef9dE umLrkOmSAPk011FWBOxDiTJTDAlxXYb1mok7nQnT6jB3vSK1x6NnE0m6X+Qx/Vs21/e/ Q9bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=xVJTuw3QuQuOeoFl+heMsboXqywN3ahaS8uuijHe0dI=; b=kmrit5shCKB3OCgGT25r5Pb/tN7RFX9EDRK672vxWehmnJSHkN1cQciwgjVMa2LUif SPCoUPK5f52xBDB485Tx6A+yquAjTeEoJnyPOHA+wHydppG5nU9NJ/z6OMQKdRUd6R8i Pbp7g1ZbDgsE/9QyPkNHbtOYCPDc0dh4P1QyQ7bVdflQtMOfV8aroG9FaL40fTjGjMuu f19f94olb6Ul5ujFuhlzlY6jEJfBhObjm8G5TSLCCfPF9E4ZTrk90kCyqdq7q/TWmWrM crANtUpPQsx1+KQUS0SIYaMjPP81yl7PXpI4VMcQpe6sWn7ORIcDz1YhgjWCyLfAP6y4 GLPA== X-Gm-Message-State: ACgBeo3ZAuF0zZ2wpGttAKfwmbnwWinYmkK8DxfPy6baC4RbY4W8BnC0 C+VioSiQtaxukPd/KOUN8JKjwh+Xbsc= X-Received: by 2002:a17:907:3dab:b0:741:9a23:eb01 with SMTP id he43-20020a1709073dab00b007419a23eb01mr23204556ejc.26.1663082422146; Tue, 13 Sep 2022 08:20:22 -0700 (PDT) Received: from nlaptop.wireless.UGent.be (nata195.ugent.be. [157.193.240.195]) by smtp.gmail.com with ESMTPSA id gc33-20020a1709072b2100b00731803d4d04sm6134933ejc.82.2022.09.13.08.20.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Sep 2022 08:20:21 -0700 (PDT) From: Niels Dossche To: linux-bluetooth@vger.kernel.org Cc: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , Niels Dossche Subject: [PATCH] Bluetooth: protect remote oob data in build_pairing_cmd's callsites Date: Tue, 13 Sep 2022 17:20:16 +0200 Message-Id: <20220913152016.38691-1-dossche.niels@gmail.com> X-Mailer: git-send-email 2.37.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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 Accesses to hci_dev->remote_oob_data are protected by the hdev lock, except for the access in build_pairing_cmd via hci_find_remote_oob_data. Adding the lock around the access in build_pairing_cmd would cause a lock ordering problem: the l2cap_chan_lock is taken in the caller smp_conn_security, while the hdev lock should be taken before the chan lock. The solution is to add the hdev lock to the callsites of build_pairing_cmd. Fixes: 02b05bd8b0a6 ("Bluetooth: Set SMP OOB flag if OOB data is available") Signed-off-by: Niels Dossche --- Note: I am currently working on a static analyser to detect missing locks using type-based static analysis, which reported the missing lock on v6.0-rc5. 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 only managed to compile-test this patch on x86_64. After applying the patch, my analyser no longer reports the potential bug. net/bluetooth/smp.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c index 11f853d0500f..3efa99fa5062 100644 --- a/net/bluetooth/smp.c +++ b/net/bluetooth/smp.c @@ -1803,7 +1803,9 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) return 0; } + hci_dev_lock(dev); build_pairing_cmd(conn, req, &rsp, auth); + hci_dev_unlock(hdev); if (rsp.auth_req & SMP_AUTH_SC) { set_bit(SMP_FLAG_SC, &smp->flags); @@ -2335,7 +2337,9 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) skb_pull(skb, sizeof(*rp)); memset(&cp, 0, sizeof(cp)); + hci_dev_lock(hdev); build_pairing_cmd(conn, &cp, NULL, auth); + hci_dev_unlock(hdev); smp->preq[0] = SMP_CMD_PAIRING_REQ; memcpy(&smp->preq[1], &cp, sizeof(cp)); @@ -2380,6 +2384,7 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) return 1; } + hci_dev_lock(hcon->hdev); l2cap_chan_lock(chan); /* If SMP is already in progress ignore this request */ @@ -2435,6 +2440,7 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) unlock: l2cap_chan_unlock(chan); + hci_dev_unlock(hcon->hdev); return ret; } -- 2.37.3