mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bharath Reddy <kbreddy.rpbc@gmail.com>
To: syzbot+535ecc844591e50588a5@syzkaller.appspotmail.com
Cc: kartikey406@gmail.com, linux-kernel@vger.kernel.org,
	syzkaller-bugs@googlegroups.com,
	Bharath Reddy <kbreddy.rpbc@gmail.com>
Subject: [PATCH] Bluetooth: fix memory leaks in error path of hci_alloc_dev()
Date: Sun, 31 May 2026 19:55:29 +0530	[thread overview]
Message-ID: <20260531142529.97657-1-kbreddy.rpbc@gmail.com> (raw)
In-Reply-To: <6a1b8c87.fbc46276.d3ed.0668.GAE@google.com>

#syz test: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master

Syzbot caught a percpu memory leak in the SRCU struct when Bluetooth
HCI UART configuration fails early.

The problem boils down to a bypassed destructor. If device setup fails
before hci_register_dev() completes, the HCI_UNREGISTER flag is never
set. Later, when the device is freed, bt_host_release() sees this flag
is missing, skips hci_release_dev(), and jumps straight to kfree(hdev).
This leaks the SRCU memory, which was also incorrectly being cleaned up
in hci_unregister_dev() which is also skipped during an early abort.

To fix this and properly balance the allocation and teardown paths:

1. Move cleanup_srcu_struct() into hci_release_dev() so it directly
   mirrors the allocation in hci_alloc_dev().
2. Unconditionally call hci_release_dev() from bt_host_release() by
   dropping the HCI_UNREGISTER check. This is safe for unregistered
   devices since it only cleans up resources initialized early on in
   hci_alloc_dev().

Reported-by: syzbot+535ecc844591e50588a5@syzkaller.appspotmail.com

Signed-off-by: Bharath Reddy <kbreddy.rpbc@gmail.com>
---
 net/bluetooth/hci_core.c  | 3 ++-
 net/bluetooth/hci_sysfs.c | 6 ++----
 2 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index c46c1236ebfa..f6bc7cb3e55c 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -2701,7 +2701,6 @@ void hci_unregister_dev(struct hci_dev *hdev)
 	write_unlock(&hci_dev_list_lock);
 
 	synchronize_srcu(&hdev->srcu);
-	cleanup_srcu_struct(&hdev->srcu);
 
 	disable_work_sync(&hdev->rx_work);
 	disable_work_sync(&hdev->cmd_work);
@@ -2774,6 +2773,8 @@ void hci_release_dev(struct hci_dev *hdev)
 	kfree_skb(hdev->sent_cmd);
 	kfree_skb(hdev->req_skb);
 	kfree_skb(hdev->recv_event);
+
+	cleanup_srcu_struct(&hdev->srcu);
 	kfree(hdev);
 }
 EXPORT_SYMBOL(hci_release_dev);
diff --git a/net/bluetooth/hci_sysfs.c b/net/bluetooth/hci_sysfs.c
index 041ce9adc378..261b28a25b02 100644
--- a/net/bluetooth/hci_sysfs.c
+++ b/net/bluetooth/hci_sysfs.c
@@ -83,10 +83,8 @@ static void bt_host_release(struct device *dev)
 {
 	struct hci_dev *hdev = to_hci_dev(dev);
 
-	if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
-		hci_release_dev(hdev);
-	else
-		kfree(hdev);
+	hci_release_dev(hdev);
+
 	module_put(THIS_MODULE);
 }
 
-- 
2.34.1


  reply	other threads:[~2026-05-31 14:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260531001321.98695-1-kartikey406@gmail.com>
2026-05-31  1:19 ` [syzbot] [bluetooth?] memory leak in init_srcu_struct_fields syzbot
2026-05-31 14:25   ` Bharath Reddy [this message]
2026-05-31 14:59     ` syzbot
2026-05-31 14:41   ` [PATCH] Bluetooth: fix memory leaks in error path of hci_alloc_dev() Bharath Reddy
2026-05-31 15:27     ` [syzbot] [bluetooth?] memory leak in init_srcu_struct_fields syzbot
2026-05-31 15:27   ` [PATCH v2] Bluetooth: fix memory leak in error path of hci_alloc_dev() Bharath Reddy
2026-05-31 16:11     ` [syzbot] [bluetooth?] memory leak in init_srcu_struct_fields syzbot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260531142529.97657-1-kbreddy.rpbc@gmail.com \
    --to=kbreddy.rpbc@gmail.com \
    --cc=kartikey406@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=syzbot+535ecc844591e50588a5@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®