Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp813829pxb; Fri, 22 Apr 2022 11:43:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzNagp4QQiyw+VUvkUHquoqgkQF1wn4Sac5DijeinvGrz9k881/B1QMmf9THPfODIXIOsmF X-Received: by 2002:a05:6a00:10cc:b0:506:e0:d6c3 with SMTP id d12-20020a056a0010cc00b0050600e0d6c3mr6213745pfu.33.1650653033167; Fri, 22 Apr 2022 11:43:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650653033; cv=none; d=google.com; s=arc-20160816; b=Rs/n6RPCdEEt1vGadPiDFNNGmBXicm0eXY8feSYDWnPC/cCvT/lx3jiVv/UlDkLN/t 6CDlxwsaO91GWZyXa8zcmuvHzEJon2uCZP33xLEKdcG9mdDLzqV2srNdApmcTPbVHci1 DEYfmS03nbPlSBhExjwWT4bcycIbuqSWJmnbhcIl5nviP+V1544qOzwAWYaGLMCHex4B xPxg3bOsnISM48tKuHkKdoDi/PByEsHczDTAaGySP0+eohpnt9AClV8XlT2h5JqHQBFh Y90WpZ9FlHTLp2wkv2i48cJtca3rZwLuaiVpQIhzDuQvObwnYTHj3d4wgs7P+QXS+25R F4zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version; bh=SQIMlqVRYUdt8D92+AKHd5BgylfZxdpARJSOpRhLfFw=; b=VBDMeTK3KhkOmXmvDUusKW+KqXAG1mIMwQySfQdROALFtmgh35ub0dbCBsc4CiKTFU Lkb675f7ut32hZ1Cqu8wE+EyDyjrlZkda0Y9uWB3OB6RcwZLUoli3PuuvHz0A9g+1SDp 2xAIk88BUaz07EhD6TG5KHoJvSyYLscuHJyciswB/8ktsn2RX6w54Oj6hp4iHfL2AY2p KmYvuWC/tf2bEfasLxvILpo4YrdT4DDVvsfVaBuaRf6F5sihc9rQ1iSDpYpLOtbTk6Pq 3sapsfay73rQ96DyvUEjVIW//+B+6Zj2CJ3OusQTMcH7w6YwebLEE4A+ZSo41YoXjPST 8LDg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id q10-20020a170902edca00b00156abebc92esi3732611plk.544.2022.04.22.11.43.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 11:43:53 -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; 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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6743C12C41B; Fri, 22 Apr 2022 11:12:11 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356721AbiDUQAm convert rfc822-to-8bit (ORCPT + 99 others); Thu, 21 Apr 2022 12:00:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1390408AbiDUQAl (ORCPT ); Thu, 21 Apr 2022 12:00:41 -0400 Received: from mail.holtmann.org (coyote.holtmann.net [212.227.132.17]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7D9C0473A2 for ; Thu, 21 Apr 2022 08:57:49 -0700 (PDT) Received: from smtpclient.apple (p4fefc32f.dip0.t-ipconnect.de [79.239.195.47]) by mail.holtmann.org (Postfix) with ESMTPSA id 99AA6CED13; Thu, 21 Apr 2022 17:57:48 +0200 (CEST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.80.82.1.1\)) Subject: Re: [PATCH] Bluetooth: hci_event: Fix checking for invalid handle on error status From: Marcel Holtmann In-Reply-To: <20220420221433.2933868-1-luiz.dentz@gmail.com> Date: Thu, 21 Apr 2022 17:57:48 +0200 Cc: linux-bluetooth@vger.kernel.org Content-Transfer-Encoding: 8BIT Message-Id: <654FF692-95EB-459A-9144-62EA911C7BBB@holtmann.org> References: <20220420221433.2933868-1-luiz.dentz@gmail.com> To: Luiz Augusto von Dentz X-Mailer: Apple Mail (2.3696.80.82.1.1) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE 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 Hi Luiz, > Commit d5ebaa7c5f6f6 introduces checks for handle range > (e.g HCI_CONN_HANDLE_MAX) but controllers don't seem to respect the > valid range int case of error status: > >> HCI Event: Connect Complete (0x03) plen 11 > Status: Page Timeout (0x04) > Handle: 65535 > Address: 94:DB:56:XX:XX:XX (Sony Home Entertainment& > Sound Products Inc) > Link type: ACL (0x01) > Encryption: Disabled (0x00) > [1644965.827560] Bluetooth: hci0: Ignoring HCI_Connection_Complete for > invalid handle so the problem is that with BR/EDR the lookup is by BD_ADDR. I think the check for valid handle is wrong at the beginning of connect complete handler. The problem is really the fact the we trying a big hammer at the beginning. The hci_conn_add in case of auto-connect should validate status and handle. We are not even validating the status right now assuming we always get a status == 0 on auto-connect. The second handle validation should only occur if we have !status in the bottom half of that function. > Because of it is impossible to cleanup the connections properly since > the stack would attempt to cancel the connection which is no longer in > progress causing the following trace: > > < HCI Command: Create Connection Cancel (0x01|0x0008) plen 6 > Address: 94:DB:56:XX:XX:XX (Sony Home Entertainment& > Sound Products Inc) > = bluetoothd: src/profile.c:record_cb() Unable to get Hands-Free Voice > gateway SDP record: Connection timed out >> HCI Event: Command Complete (0x0e) plen 10 > Create Connection Cancel (0x01|0x0008) ncmd 1 > Status: Unknown Connection Identifier (0x02) > Address: 94:DB:56:XX:XX:XX (Sony Home Entertainment& > Sound Products Inc) > < HCI Command: Create Connection Cancel (0x01|0x0008) plen 6 > Address: 94:DB:56:XX:XX:XX (Sony Home Entertainment& > Sound Products Inc) Can we get details about which controller uses 0xffff instead of 0 for the handle? > > Fixes: d5ebaa7c5f6f6 ("Bluetooth: hci_event: Ignore multiple conn complete events") > Signed-off-by: Luiz Augusto von Dentz > --- > net/bluetooth/hci_event.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c > index abaabfae19cc..1cc5a712459e 100644 > --- a/net/bluetooth/hci_event.c > +++ b/net/bluetooth/hci_event.c > @@ -3068,7 +3068,7 @@ static void hci_conn_complete_evt(struct hci_dev *hdev, void *data, > struct hci_ev_conn_complete *ev = data; > struct hci_conn *conn; > > - if (__le16_to_cpu(ev->handle) > HCI_CONN_HANDLE_MAX) { > + if (!status && __le16_to_cpu(ev->handle) > HCI_CONN_HANDLE_MAX) { > bt_dev_err(hdev, "Ignoring HCI_Connection_Complete for invalid handle"); > return; > } See comments above. > @@ -4690,7 +4690,7 @@ static void hci_sync_conn_complete_evt(struct hci_dev *hdev, void *data, > return; > } > > - if (__le16_to_cpu(ev->handle) > HCI_CONN_HANDLE_MAX) { > + if (!status && __le16_to_cpu(ev->handle) > HCI_CONN_HANDLE_MAX) { > bt_dev_err(hdev, "Ignoring HCI_Sync_Conn_Complete for invalid handle"); > return; > } This is also in the wrong position. Fundamentally we need to check the validity of the handle before we assign it and not just globally. > @@ -5527,7 +5527,7 @@ static void le_conn_complete_evt(struct hci_dev *hdev, u8 status, > struct smp_irk *irk; > u8 addr_type; > > - if (handle > HCI_CONN_HANDLE_MAX) { > + if (!status && handle > HCI_CONN_HANDLE_MAX) { > bt_dev_err(hdev, "Ignoring HCI_LE_Connection_Complete for invalid handle"); > return; > } Same here. The global check is pointless. Check before using it. Regards Marcel