Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp4626321rwr; Sun, 30 Apr 2023 11:14:37 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5dEvWx0sgKS17Jvj0DZKezgXx7TTDkVLEHyXhPK0cmtTMmLt3Xa2TJwyJvMAAdgdsCzrTw X-Received: by 2002:a17:90a:3c8b:b0:247:8b61:a41 with SMTP id g11-20020a17090a3c8b00b002478b610a41mr11234404pjc.25.1682878477317; Sun, 30 Apr 2023 11:14:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682878477; cv=none; d=google.com; s=arc-20160816; b=OHi6Kr86IulevjSMpV8oWzbXVz4TyfrsjlzAdp8U3LcYDO9IwxBPco0L8AqwbeLu3o iBGuxIGNi1tnFnfOcv/8BbuM+pJCZhUf5gSYijHvBok1WXkrOmXuq3Qg7USbuJ5KhvVj vfNEAy+i/2gOEJ3/6DbAkMLVDdd9poXCfpw+vOYtbxjUj1V6KMiirTgxEM8149zLBGjK iBGqnBs7+gpb1Z7E2u9xWPij74TF63d9Y8AM3C2YdJH78346NSg6G1Qz+e9BVZNtoVfA k3PXQfps0ztUbymgXxVJPYNdJUfMtkv0nixvCRQs7qpeV+nvSlHSNbq/21B59axVWhhF eI8A== 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=SG+O2Nuecmo3S1C5gW1OXRfwUABkZXk/+etEKv1A1Wo=; b=Nl+9Jb/y9eP1k9BUKj6x78p2alhgGzb6vyk5zZaLYLYDlFieZDOa9DgDsOhb9OpWuq ANuFYgjCezDc4v4BUYPoFhviDhiw9ueX1sYWtSvqPg8hwOUriGJoWhmZlslWuAF+9sD8 K6HUneL4zE0r90lSSb7wJEmXJbUnqPNQH8quorWOXZRZwgmUnkzI1TYR5AMDOCBjQ4S+ KcVX1KV5He/tNwBS1MEi2HUKFISXHd3GicAUEzqKefKqZWAAt3Pg8q2Uv4AwvmRRwj8k Lpee/RdZDWuLrXI68R5bf8YpTw2Ek5o6E0Y42k6FGU9fTLrPvqhRXLkEdH/JnfGS1sYr lwyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@pku.edu.cn header.s=dkim header.b=lJYPCkdS; 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=NONE dis=NONE) header.from=pku.edu.cn Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k23-20020a6568d7000000b00503a029385asi22389585pgt.849.2023.04.30.11.14.21; Sun, 30 Apr 2023 11:14:37 -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 (test mode) header.i=@pku.edu.cn header.s=dkim header.b=lJYPCkdS; 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=NONE dis=NONE) header.from=pku.edu.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229909AbjD3SFz (ORCPT + 99 others); Sun, 30 Apr 2023 14:05:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229568AbjD3SFz (ORCPT ); Sun, 30 Apr 2023 14:05:55 -0400 Received: from pku.edu.cn (mx19.pku.edu.cn [162.105.129.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B5ED8E7B for ; Sun, 30 Apr 2023 11:05:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pku.edu.cn; s=dkim; h=Received:From:To:Cc:Subject:Date: Message-Id:MIME-Version:Content-Transfer-Encoding; bh=SG+O2Nuecm o3S1C5gW1OXRfwUABkZXk/+etEKv1A1Wo=; b=lJYPCkdSBpS0jLLYMHxp8CsLto yh77CSpQXJ5JUSnNA2fqBJnrPgJ+wP/lPYybxwet8TQrXPOroglJS44jhNIJF+Gu iDYeJbEiMATbzRJLQpUv3HEiLOzaQKfKsLuSSRAxP4OMmKK+eeduSZIIHxjWCO4Q bc5PQLxdOtckID/XU= Received: from localhost.localdomain (unknown [10.7.101.92]) by front01 (Coremail) with SMTP id 5oFpogBXX2f2rU5kiWv2AA--.57652S2; Mon, 01 May 2023 02:05:47 +0800 (CST) From: Ruihan Li To: linux-bluetooth@vger.kernel.org Cc: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , Ruihan Li Subject: [PATCH v2] Bluetooth: Fix potential double free caused by hci_conn_unlink Date: Mon, 1 May 2023 02:05:35 +0800 Message-Id: <20230430180535.168270-1-lrh2000@pku.edu.cn> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: 5oFpogBXX2f2rU5kiWv2AA--.57652S2 X-Coremail-Antispam: 1UD129KBjvJXoWxJrWkCrWrXrW8JryrKr1xXwb_yoW8tFWUpa y3WayaqF4kJrn3WF4jyw4kWrsYvw1kZFy7Kr1rtryrAws0qry8Aw4FkryUKrW5ZrWkWF4Y vF4Utr1UKF4DC37anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9j1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s1l1IIY67AE w4v_Jr0_Jr4l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2 IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwA2z4x0Y4vEx4A2 jsIE14v26F4UJVW0owA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Cr1j6rxdM2vYz4IE04k24V AvwVAKI4IrM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xf McIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7 v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7MxkIecxEwVCm -wCF04k20xvY0x0EwIxGrwCF04k20xvE74AGY7Cv6cx26w4UJr1UMxC20s026xCaFVCjc4 AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE 17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMI IF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4l IxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvf C2KfnxnUUI43ZEXa7VUbHa0DUUUUU== X-CM-SenderInfo: yssqiiarrvmko6sn3hxhgxhubq/1tbiAgEPBVPy77wh+AAKso X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, 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 The hci_conn_unlink function is being called by hci_conn_del, which means it should not call hci_conn_del with the input parameter conn again. If it does, conn may have already been released when hci_conn_unlink returns, leading to potential UAF and double-free issues. This patch resolves the problem by modifying hci_conn_unlink to release only conn's child links when necessary, but never release conn itself. Fixes: 06149746e720 ("Bluetooth: hci_conn: Add support for linking multiple hcon") Signed-off-by: Ruihan Li --- Changes since v1: * CI complains that there are some merge conflicts. This is because void hci_conn_del(struct hci_conn *conn) this completely unrelated line makes the patch dependent on another fix: https://lore.kernel.org/linux-bluetooth/20230430171847.156825-1-lrh2000@pku.edu.cn/ But actually, deleting that line makes the two patches independent of each other, and everything still works. net/bluetooth/hci_conn.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c index 85c34c837..5f388202f 100644 --- a/net/bluetooth/hci_conn.c +++ b/net/bluetooth/hci_conn.c @@ -1083,8 +1083,18 @@ static void hci_conn_unlink(struct hci_conn *conn) if (!conn->parent) { struct hci_link *link, *t; - list_for_each_entry_safe(link, t, &conn->link_list, list) - hci_conn_unlink(link->conn); + list_for_each_entry_safe(link, t, &conn->link_list, list) { + struct hci_conn *child = link->conn; + + hci_conn_unlink(child); + + /* Due to race, SCO connection might be not established + * yet at this point. Delete it now, otherwise it is + * possible for it to be stuck and can't be deleted. + */ + if (child->handle == HCI_CONN_HANDLE_UNSET) + hci_conn_del(child); + } return; } @@ -1100,12 +1110,5 @@ static void hci_conn_unlink(struct hci_conn *conn) kfree(conn->link); conn->link = NULL; - - /* Due to race, SCO connection might be not established - * yet at this point. Delete it now, otherwise it is - * possible for it to be stuck and can't be deleted. - */ - if (conn->handle == HCI_CONN_HANDLE_UNSET) - hci_conn_del(conn); } -- 2.40.0