Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp232891rwl; Thu, 30 Mar 2023 15:09:53 -0700 (PDT) X-Google-Smtp-Source: AKy350YHFLnavtXTsmwePh05PSNnHGyzDHw3FrTf8mIEjF/T+43zSW2lOyBttbL3Hw9NEIK6D6kd X-Received: by 2002:a17:90a:e7c4:b0:23b:4f4c:a8b with SMTP id kb4-20020a17090ae7c400b0023b4f4c0a8bmr27789833pjb.11.1680214192816; Thu, 30 Mar 2023 15:09:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680214192; cv=none; d=google.com; s=arc-20160816; b=jeED7bLgaoTo99P33oc5iH7jDGYVvJONaHdZoycavg+P6LiwekeB1Rf/V/zJmIhqbd 8V2MJdMtygTczvqj3JuBP1h6Lm2JN4JGAgwMiDFObRvGqBiTUUa0Z096gZ0QbbmzBGL0 C86X/9N7mgHhCzjsz3GVRX3IbEW3ZEFK4Y4HxhCOVhh0yLXfR1mMOe/ewEhIzIfXh1mm lWmFC/NLoqxoNT3e6AeTrGFAqKsBTaKImVNFGzFrFzYYdfdn0ezK1mrFfdn8/ax8ZgC0 TW4oVUhhy5/rrETuvSYnaih8iHksUQole1fSUK9vBFgi+87xrCSHrZikQtNgJpxzhIVh nq6A== 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:to:from:dkim-signature; bh=6F8FLEVXSII2jTBWg/LdI/5vXy8cfsxjGzqk1m7Dz5Y=; b=mpXmQZkq/3d1ImEo08YxlJzn6DM7C+6nfeGv4EcYLhQkRyGf7vcTIgVeSG3c0Nv0Zm lIh7yOuSwgSaoWRrwlQtKDK6BIrcYjxvnKZrjK4C+UqEwd87TC9FCnDmGatONxCUFki5 M9ILaQQ3Eayg//99XFhNthTBAtWsyMdIk1XctfR91mEygDaCkLWclWGXpQlq0A7Ehtn9 OSgDP0BPAtVYnaoLTQ9mbrX52LMsBidn5WSDqJIr0zxNnNTCU5LUvdxtvK50/rn+i8LF J/UuoAVQCsMBGpVzsjBSqoUZeMIditHLhdruviGmEPEQycLs1dBCu4z3UM6IC/3gGzy4 UFyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=oxQYhYMh; 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 d12-20020a170902cecc00b001a29ce664a1si275474plg.366.2023.03.30.15.09.40; Thu, 30 Mar 2023 15:09:52 -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=oxQYhYMh; 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 S229671AbjC3WHb (ORCPT + 99 others); Thu, 30 Mar 2023 18:07:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229992AbjC3WGW (ORCPT ); Thu, 30 Mar 2023 18:06:22 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4375C11668 for ; Thu, 30 Mar 2023 15:03:35 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id h14so12263765pgj.7 for ; Thu, 30 Mar 2023 15:03:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680213814; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=6F8FLEVXSII2jTBWg/LdI/5vXy8cfsxjGzqk1m7Dz5Y=; b=oxQYhYMhMaTmvXP8m+Cqy88oo/TiHLG08MdlUcgwcmOMq4A13iaC5i/6TPdc+J4FDl thXVdKDcdT54MgQ8Cn7wXthhbIvzELUQVKmjOAgXhNTe2sZSwtNtmBUUuuAvHcyEoh8/ 71WOF5Wjr7QqSoiTCK3I5HreJ0gu3j9Fj3GZ50Dqb4fz63esMzMGJ0YopyeiLReY16ry pkJdJtJWk+4IuAf+g/zxB0UimZBMN9/O9tbTpgMlA/rhG9j2VeoCbKpHIWMe6P9urjcI 9feiOjne0DyN39tB2bamld8amGMeTRCWZJ2HLwVUpxBDrkvZo+EDRJVUVyKa4J8d1ng6 yg3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680213814; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6F8FLEVXSII2jTBWg/LdI/5vXy8cfsxjGzqk1m7Dz5Y=; b=rDQlcBj+BQcHckokjn31ZVKtO3lfzvZwRPHh2BvHKs4dnIglQrVUibG66wgpGIn3Bd /9Tpi8w8+l3N1N5V+hOSrCWUcknRVs/nASd0DxdOFWGi1GKspSIqvHSjE0uowfSk4LYV T7yz+/z6lAEiemucNFLNQBlPvxOYfB/4Q7zQf8eALdBLw7DFZBLFKyzA7RQdncijuoUM bGwC6wCQQc+8aU6tfOBOgUmBhYwyAbLuG7W3ZGhlrOkr5JuxVDxv5mdvRNTJ+Ky8I1Xo lYopNUUttyqzX5mrOZXNzVD01Jm+32GSoAmQiNwLYE5kGM77S/ZczuUTycofiarOmBEW kO1g== X-Gm-Message-State: AAQBX9c/Y6iur93vZIq6njbMpbF2vwD7YVq0xah6YBnjPTJRCW+yVnkn VhatLfOg2rrZZoJeB0rRmbDvV2mLGu0= X-Received: by 2002:a62:1a53:0:b0:622:ec07:c6bc with SMTP id a80-20020a621a53000000b00622ec07c6bcmr22065788pfa.15.1680213814140; Thu, 30 Mar 2023 15:03:34 -0700 (PDT) Received: from lvondent-mobl4.. (c-71-59-129-171.hsd1.or.comcast.net. [71.59.129.171]) by smtp.gmail.com with ESMTPSA id z14-20020aa791ce000000b00571cdbd0771sm333790pfa.102.2023.03.30.15.03.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 15:03:33 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH] Bluetooth: Fix double free in hci_conn_cleanup Date: Thu, 30 Mar 2023 15:03:30 -0700 Message-Id: <20230330220332.1035910-1-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 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. Link: https://syzkaller.appspot.com/bug?id=1bb51491ca5df96a5f724899d1dbb87afda61419 [1] Signed-off-by: ZhengHan Wang Signed-off-by: Luiz Augusto von Dentz --- 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 e4aee5950c36..00d1e7201a44 100644 --- a/net/bluetooth/hci_conn.c +++ b/net/bluetooth/hci_conn.c @@ -168,13 +168,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 le_scan_cleanup(struct work_struct *work) diff --git a/net/bluetooth/hci_sysfs.c b/net/bluetooth/hci_sysfs.c index 08542dfc2dc5..633b82d54272 100644 --- a/net/bluetooth/hci_sysfs.c +++ b/net/bluetooth/hci_sysfs.c @@ -33,7 +33,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; @@ -46,27 +46,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; @@ -78,9 +81,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.39.2