Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH] Bluetooth: hci_sync: Drop the command's sk reference
@ 2026-11-12  3:34 Weihong Guan
  2026-11-12  5:01 ` bluez.test.bot
  2026-11-12 20:20 ` [PATCH] " patchwork-bot+bluetooth
  0 siblings, 2 replies; 3+ messages in thread
From: Weihong Guan @ 2026-11-12  3:34 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Marcel Holtmann, Luiz Augusto von Dentz, Weihong Guan

hci_cmd_sync_alloc() takes a reference on the sk a command is sent for
(sock_hold()), and hci_cmd_sync_complete() means to drop it, but looks
for the sk in the response skb, which never carries one. The reference
is never dropped: every command sent with an sk leaks it, and with it
the sk and a reference on the bluetooth module.

The commands sent with an sk are LE Remove Advertising Set and LE
Clear Advertising Sets, for MGMT's Remove Advertising and the instance
removals, and Read Local OOB (Extended) Data. After each, the module's
refcnt stays one higher with no socket left open, and the module can't
be unloaded until a reboot.

Drop the reference from the request command, hdev->req_skb, before
freeing it.

Fixes: cba6b758711c ("Bluetooth: hci_sync: Make use of hci_cmd_sync_queue set 2")
Assisted-by: Claude:claude-opus-5.5
Signed-off-by: Weihong Guan <weihong.guan@gmail.com>
---
How it was found and tested: mgmt-tester on a Raspberry Pi 5 running
7.4-rc2 with net/bluetooth from bluetooth-next (hci_cmd_sync_alloc()
and hci_cmd_sync_complete() as in this tree), reading
/sys/module/bluetooth/refcnt before and after each test, alone, with
bluetoothd stopped.

Before, 11 of mgmt-tester's 515 tests left it one higher each: Read
Local OOB Data (SSP, SC), Read Local OOB Ext Data (SSP, SC), and every
one removing an extended advertising instance (Remove Ext Advertising -
Success 1, 2, Global Adv; Multi Ext Advertising - Success 4, 5; Add Ext
Advertising - Success 22; Ext Adv MGMT - AD Scan Resp - Off and On), as
did the removal case of the single-set series sent for BlueZ. A full run
left the module with stray references and no socket in
/proc/net/protocols.

After, each of them leaves it unchanged, a full run leaves it where it
started, the same tests pass and fail as before on that machine, and
dmesg has no refcount warning.

A command whose request never completes (a timeout) still has req_skb
freed elsewhere without the sk dropped; that path is left as it was.
"Bluetooth: hci_core: Fix leak of request command on tx timeout"
(Jiakai Xu, September; patchwork series 1168176) frees req_skb there,
and would need the same sock_put() for a command sent with an sk.

 net/bluetooth/hci_sync.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
index d5177e146..931494348 100644
--- a/net/bluetooth/hci_sync.c
+++ b/net/bluetooth/hci_sync.c
@@ -31,21 +31,23 @@ static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
 	hdev->req_result = result;
 	WRITE_ONCE(hdev->req_status, HCI_REQ_DONE);
 
-	/* Free the request command so it is not used as response */
+	/* Free the request command so it is not used as response, and drop
+	 * the reference hci_cmd_sync_alloc() took on its sk, if set: the
+	 * response skb doesn't carry it.
+	 */
 	hci_dev_lock(hdev);
-	kfree_skb(hdev->req_skb);
-	hdev->req_skb = NULL;
-	hci_dev_unlock(hdev);
-
-	if (skb) {
-		struct sock *sk = hci_skb_sk(skb);
+	if (hdev->req_skb) {
+		struct sock *sk = hci_skb_sk(hdev->req_skb);
 
-		/* Drop sk reference if set */
 		if (sk)
 			sock_put(sk);
+	}
+	kfree_skb(hdev->req_skb);
+	hdev->req_skb = NULL;
+	hci_dev_unlock(hdev);
 
+	if (skb)
 		hdev->req_rsp = skb_get(skb);
-	}
 
 	wake_up_interruptible(&hdev->req_wait_q);
 }
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-12 20:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12  3:34 [PATCH] Bluetooth: hci_sync: Drop the command's sk reference Weihong Guan
2026-11-12  5:01 ` bluez.test.bot
2026-11-12 20:20 ` [PATCH] " patchwork-bot+bluetooth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox