Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3697224rdg; Wed, 18 Oct 2023 03:33:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFb/pDp2vyThk83i5kmJxYiW746JmqmsHU3b8dCPD9geEhy50fER/k1KyJj9FAuIDVYFUrs X-Received: by 2002:a17:90b:892:b0:27d:1334:d266 with SMTP id bj18-20020a17090b089200b0027d1334d266mr4238327pjb.27.1697625179808; Wed, 18 Oct 2023 03:32:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697625179; cv=none; d=google.com; s=arc-20160816; b=ooVoud9wCAqIHgLAKr+aIL0/Nal9R9Jw7hO5TWB7g6JSeo5sXB1tRFBkM+nS6jqMT1 nJQpG1fRlis9vdqtDFjW3hW7jMNewHSPn8uzIk/TTljVLIssXV+kX29rkHlWqz00yTav Tv7FRyv0INDgzbR8ptoil7bO29+QJEr6uHshcF2rCYqmPDVbIyOEHUKZ2GMOHEqHa3FW LzyWjc+QQqfPG8R8yiO2ijRmb1CQJuOuA3pNb2D7El2Ikr+2bt3wfKsXhbjzSQBVOsRZ 2rAy0yCFVk8x48GvuDk9H3O5fJsl+kaVBN28SpNrxZ/HbEiMBH8cA0Z7QFyOGSxNXYyK boKQ== 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; bh=yy3zKBAFZX3KkcEfTDGyT2InNBRCVMUzdLJIyRUcdeE=; fh=t2883dhBSXK6a6ptLM6jNWu9EzglGg6C1Cn2Bh5hv1o=; b=rKGzkl4YN0FRm9n4UckV3wHPqT3Bm/kDYzATrh/OYkKwowLdtOOuGXpTkWoPwyh1Em rFjIiWCK8WHLnTBxX9iNvg539+KFPzVcs/s8f1XQv3IOJ3t/nXtIkls1R904FDE3n6sR z9Uwj6N82/Y7j3rR0KX9lMJvVprigesgZnniwXhXu/AP/p+VHAmksS8TuKzYWa5LC5Nu /9db7iXhcQD6rhp+AW3yaupHmRQWve1S81YMzcGBnXRMKI3alIhsm+sM6kFoSdsnQXNI AcVEe0jzWTmzBoDLh3urxyKMVWlfpdbIeUvp9UHxdFqlObar76R4dHN71AStaL2ET/jn ljlQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id w16-20020a17090a8a1000b002777ccd05bcsi1227157pjn.25.2023.10.18.03.32.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 03:32:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id EBE3A8131AB7; Wed, 18 Oct 2023 03:32:57 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230414AbjJRKcz (ORCPT + 99 others); Wed, 18 Oct 2023 06:32:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229844AbjJRKcy (ORCPT ); Wed, 18 Oct 2023 06:32:54 -0400 Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::226]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E74CEA for ; Wed, 18 Oct 2023 03:32:50 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id E802CC0007; Wed, 18 Oct 2023 10:32:48 +0000 (UTC) From: Bastien Nocera To: linux-bluetooth@vger.kernel.org Cc: ZhengHan Wang , Luiz Augusto von Dentz Subject: [PATCH v3] Bluetooth: Fix double free in hci_conn_cleanup Date: Wed, 18 Oct 2023 12:30:55 +0200 Message-ID: <20231018103248.294565-1-hadess@hadess.net> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-GND-Sasl: hadess@hadess.net X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_PASS,SPF_PASS 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 18 Oct 2023 03:32:58 -0700 (PDT) From: ZhengHan Wang syzbot reports a slab use-after-free in hci_conn_hash_flush [1]. After releasing an object using hci_conn_del_sysfs in the hci_conn_cleanup function, releasing the same object again using the hci_dev_put and hci_conn_put functions causes a double free. Here's a simplified flow: hci_conn_del_sysfs: hci_dev_put put_device kobject_put kref_put kobject_release kobject_cleanup kfree_const kfree(name) hci_dev_put: ... kfree(name) hci_conn_put: put_device ... kfree(name) This patch drop the hci_dev_put and hci_conn_put function call in hci_conn_cleanup function, because the object is freed in hci_conn_del_sysfs function. This patch also fixes the refcounting in hci_conn_add_sysfs() and hci_conn_del_sysfs() to take into account device_add() failures. This fixes CVE-2023-28464. Link: https://syzkaller.appspot.com/bug?id=1bb51491ca5df96a5f724899d1dbb87afda61419 [1] Signed-off-by: ZhengHan Wang Co-developed-by: Luiz Augusto von Dentz --- Re-sending Luiz' version of ZhengHan Wang's patch as a reply-to snafu caused v2 of the patch by Luiz to not be applied. I added the CVE reference, fixed the SoB and add mention of Luiz' changes in the commit message. net/bluetooth/hci_conn.c | 6 ++---- net/bluetooth/hci_sysfs.c | 23 ++++++++++++----------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c index 7a6f20338db8..236085451931 100644 --- a/net/bluetooth/hci_conn.c +++ b/net/bluetooth/hci_conn.c @@ -169,13 +169,11 @@ static void hci_conn_cleanup(struct hci_conn *conn) hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); } - hci_conn_del_sysfs(conn); - debugfs_remove_recursive(conn->debugfs); - hci_dev_put(hdev); + hci_conn_del_sysfs(conn); - hci_conn_put(conn); + hci_dev_put(hdev); } static void hci_acl_create_connection(struct hci_conn *conn) diff --git a/net/bluetooth/hci_sysfs.c b/net/bluetooth/hci_sysfs.c index 15b33579007c..367e32fe30eb 100644 --- a/net/bluetooth/hci_sysfs.c +++ b/net/bluetooth/hci_sysfs.c @@ -35,7 +35,7 @@ void hci_conn_init_sysfs(struct hci_conn *conn) { struct hci_dev *hdev = conn->hdev; - BT_DBG("conn %p", conn); + bt_dev_dbg(hdev, "conn %p", conn); conn->dev.type = &bt_link; conn->dev.class = &bt_class; @@ -48,27 +48,30 @@ void hci_conn_add_sysfs(struct hci_conn *conn) { struct hci_dev *hdev = conn->hdev; - BT_DBG("conn %p", conn); + bt_dev_dbg(hdev, "conn %p", conn); if (device_is_registered(&conn->dev)) return; dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle); - if (device_add(&conn->dev) < 0) { + if (device_add(&conn->dev) < 0) bt_dev_err(hdev, "failed to register connection device"); - return; - } - - hci_dev_hold(hdev); } void hci_conn_del_sysfs(struct hci_conn *conn) { struct hci_dev *hdev = conn->hdev; - if (!device_is_registered(&conn->dev)) + bt_dev_dbg(hdev, "conn %p", conn); + + if (!device_is_registered(&conn->dev)) { + /* If device_add() has *not* succeeded, use *only* put_device() + * to drop the reference count. + */ + put_device(&conn->dev); return; + } while (1) { struct device *dev; @@ -80,9 +83,7 @@ void hci_conn_del_sysfs(struct hci_conn *conn) put_device(dev); } - device_del(&conn->dev); - - hci_dev_put(hdev); + device_unregister(&conn->dev); } static void bt_host_release(struct device *dev) -- 2.41.0