Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp6960396rwn; Tue, 13 Sep 2022 11:29:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM65mpLuzz/+5dq9HIU3rrHWuHWVV2OYWwt7Go9k0O1rp0Jq0TDncfQWKLDnKZEoI8VESsBc X-Received: by 2002:a17:90a:c70b:b0:200:4366:d047 with SMTP id o11-20020a17090ac70b00b002004366d047mr582283pjt.240.1663093754655; Tue, 13 Sep 2022 11:29:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663093754; cv=none; d=google.com; s=arc-20160816; b=Bb0sk5ofE5Xo8M6uTRvqViEolmxN06Chczjlr4coM5shKlEsBxAVXe3BbiUJaka5Uh ULXQRWzesxrnCIT4mmvsBOqGsPZk4rrH8+FqpOIwitpMNXLGXLnLfMYfyebXtyqbsq8/ C3oEUmUqX2w2fDRyVDHyggE2ZJUtIZaGF+ZfDj6jDrAoy+i4nq44CuEme+rGp8zavC70 RRMTuipMJpfJ7vdj2RPwfwusby2c/KwP6Ni2g2kmErlGAwF3ZdCwAhWUNWWlPO7HBG+s FDkk/E4o3RPLpChbB8alBNpSrvmjiBhsYy4eeQPzNeyGoUWE5yufUKCuY3mA3YYQV+5q MrlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=DSh1fcAi92GNDS/3Lu1DZwLxqUAdErIjYKBy1wxIczo=; b=I0WwpOzOb2JlIbGHwvj6Sa4mfO+nxf7WzpwMZNGQ19fHrF2hEQaUjNcP3BVPBmQAXc lMKMR8tElOQGOTzJqgVxpWc+cwal0JHDt52dfNlY+B2TLosBDvRI7pnidMEzB920c8Ua dCoxht4BFIQOvn3g+w2Bs9358KynKLoHiRDZUdrQaMwLTtGss/0gr3lwfC3lJmlnmbZs A1jemElbfO8rIAZPfPmvvDygvXSGsmUfA5k43a/8jqZDbPGGmUaR2jsyb4QIFzvzZlAx 7X/sIySlf0WBVGGcUoyds8bTm4YY7YmG0SlFrXe0flWEqEnBgeLS5ruXfPl+B7570krG qDYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=cwduGKBH; 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 m16-20020a170902bb9000b00172e131ab77si12421251pls.491.2022.09.13.11.29.02; Tue, 13 Sep 2022 11:29:14 -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=cwduGKBH; 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 S231686AbiIMSYi (ORCPT + 99 others); Tue, 13 Sep 2022 14:24:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231818AbiIMSYJ (ORCPT ); Tue, 13 Sep 2022 14:24:09 -0400 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10E10A00E9 for ; Tue, 13 Sep 2022 10:41:02 -0700 (PDT) Received: by mail-ej1-x62e.google.com with SMTP id gh9so29275089ejc.8 for ; Tue, 13 Sep 2022 10:41:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date; bh=DSh1fcAi92GNDS/3Lu1DZwLxqUAdErIjYKBy1wxIczo=; b=cwduGKBHK1M3FBFr19Lc8dW+XJikV82GhG5ZVkq5iWb8tu57WQAHUUSDC01MVCbTDB EUqXO4o8HpgmPunO8kzwpwSl7ymIwxjvwrTHnLRaXPyp5ZK3twbdrESYirEKNf/45kF9 cVfuwd9HsUOzk8H71E/93NUHym6MAFnkqpn0XD3Tz6/MvUxHqQMvlNjK1iN6H9E1St4C qbIYGEI6f2plReESre+DEQgXSEvuC2/MTtw8GB0CrLidLpYhkMk8y5A4FLLVT7tSBCyQ TgaHu3XkqL1i895Jd1Yt3XP3D57CYxWS4AOPKBB6lhk18TpMiabRGQOMlk9aI+29riII 3OvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date; bh=DSh1fcAi92GNDS/3Lu1DZwLxqUAdErIjYKBy1wxIczo=; b=HTo0WU48OxKUyomTIUbkAHwqNIGazcHWA5YGtCVfrpMMaXWxLUmIShTEyF944kzlH7 m/PAewOoFnUNYoCLBLaqWqYcYEYU32a+gwBDir3dnYaCMjUmN3IEkC9A2MoDIpK6Nvvo Uu1wfI7tkhF9pZD8RHyrtgIsulIbzyR8C9dHVnwTd3tC9U18y7MMKiZ85lqkusB4s5d8 VwC3jiXxkvqK8+DX3g1/7zvjxcRZ/k1iQJ967BzcP8MK468VL3Puj9a/o0t0uLW6XwTP Epi3QlnbbsNO+suoR54tvqOTs+jNV8MKLN2IdQ1E1Z9rbQHViO9WBCK1/p9+vGqHXIEz 32yg== X-Gm-Message-State: ACgBeo3Ya0QTqDcQordYSNjjl6yySH+IwKqCFNvsDb2yCp+PfhlTU9q1 p+aX3eP4spLX+Pg/6uzSOd6dOKbwyRo= X-Received: by 2002:a17:907:2bf4:b0:76f:1053:6e4 with SMTP id gv52-20020a1709072bf400b0076f105306e4mr22727681ejc.443.1663090860408; Tue, 13 Sep 2022 10:41:00 -0700 (PDT) Received: from ?IPV6:2a02:1811:cc83:eef0:f2b6:6987:9238:41ca? (ptr-dtfv0poj8u7zblqwbt6.18120a2.ip6.access.telenet.be. [2a02:1811:cc83:eef0:f2b6:6987:9238:41ca]) by smtp.gmail.com with ESMTPSA id h15-20020aa7c94f000000b00451ea135705sm3932139edt.92.2022.09.13.10.40.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 13 Sep 2022 10:40:59 -0700 (PDT) Message-ID: <34e66c3a-5966-6b08-aed0-36ec1517a76b@gmail.com> Date: Tue, 13 Sep 2022 19:40:58 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.0 Subject: Re: [PATCH] Bluetooth: protect remote oob data in build_pairing_cmd's callsites Content-Language: en-US To: linux-bluetooth@vger.kernel.org Cc: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz References: <20220913152016.38691-1-dossche.niels@gmail.com> From: Niels Dossche In-Reply-To: <20220913152016.38691-1-dossche.niels@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, 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 On 9/13/22 17:20, Niels Dossche wrote: > 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; > } > I'm sorry. The build failure is because I forgot to amend my commit locally. I've sent a v2 with all the changes.