public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Bluetooth: fix race during h4 bluetooth initialization
@ 2026-03-26  9:59 Jonathan Rissanen
  2026-03-26  9:59 ` [PATCH v2 1/2] Bluetooth: hci_ldisc: Clear HCI_UART_PROTO_INIT on error Jonathan Rissanen
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Jonathan Rissanen @ 2026-03-26  9:59 UTC (permalink / raw)
  To: Marcel Holtmann, Luiz Augusto von Dentz
  Cc: linux-bluetooth, linux-kernel, Jonathan Rissanen, kernel

Hi,

This series fixes a race condition that causes hci events to sometimes
be ignored during h4 initialization.

The first patch clears the HCI_UART_PROTO_INIT bit if device
registration fails. This is needed to prevent the second patch from
introducing a possible null pointer dereference.

The second patch contains the fix for the race condition.

Signed-off-by: Jonathan Rissanen <jonathan.rissanen@axis.com>
---
Changes in v2:
- Added a patch before the fix that prevents possible null pointer dereference
  from being introduced.
- Link to v1: https://lore.kernel.org/r/20260320-hci-init-fix-v1-1-e1960a41baf2@axis.com

---
Jonathan Rissanen (2):
      Bluetooth: hci_ldisc: Clear HCI_UART_PROTO_INIT on error
      Bluetooth: hci_h4: Fix race during initialization

 drivers/bluetooth/hci_h4.c    | 3 ---
 drivers/bluetooth/hci_ldisc.c | 3 +++
 2 files changed, 3 insertions(+), 3 deletions(-)
---
base-commit: 05f7e89ab9731565d8a62e3b5d1ec206485eeb0b
change-id: 20260303-hci-init-fix-9657128a0104

Best regards,
-- 
Jonathan Rissanen <jonathan.rissanen@axis.com>


^ permalink raw reply	[flat|nested] 6+ messages in thread
* [PATCH v3 1/2] Bluetooth: hci_ldisc: Clear HCI_UART_PROTO_INIT on error
@ 2026-03-27 10:47 Jonathan Rissanen
  2026-03-27 11:32 ` Bluetooth: fix race during h4 bluetooth initialization bluez.test.bot
  0 siblings, 1 reply; 6+ messages in thread
From: Jonathan Rissanen @ 2026-03-27 10:47 UTC (permalink / raw)
  To: Marcel Holtmann, Luiz Augusto von Dentz
  Cc: linux-bluetooth, linux-kernel, Jonathan Rissanen, kernel

When hci_register_dev() fails in hci_uart_register_dev()
HCI_UART_PROTO_INIT is not cleared before calling hu->proto->close(hu)
and setting hu->hdev to NULL. This means incoming UART data will reach
the protocol-specific recv handler in hci_uart_tty_receive() after
resources are freed.

Clear HCI_UART_PROTO_INIT with a write lock before calling
hu->proto->close() and setting hu->hdev to NULL. The write lock ensures
all active readers have completed and no new reader can enter the
protocol recv path before resources are freed.

This allows the protocol-specific recv functions to remove the
"HCI_UART_REGISTERED" guard without risking a null pointer dereference
if hci_register_dev() fails.

Fixes: 5df5dafc171b ("Bluetooth: hci_uart: Fix another race during initialization")
Signed-off-by: Jonathan Rissanen <jonathan.rissanen@axis.com>
---
 drivers/bluetooth/hci_ldisc.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/bluetooth/hci_ldisc.c b/drivers/bluetooth/hci_ldisc.c
index 2b28515de92c..5455990ab211 100644
--- a/drivers/bluetooth/hci_ldisc.c
+++ b/drivers/bluetooth/hci_ldisc.c
@@ -692,6 +692,9 @@ static int hci_uart_register_dev(struct hci_uart *hu)
 
 	if (hci_register_dev(hdev) < 0) {
 		BT_ERR("Can't register HCI device");
+		percpu_down_write(&hu->proto_lock);
+		clear_bit(HCI_UART_PROTO_INIT, &hu->flags);
+		percpu_up_write(&hu->proto_lock);
 		hu->proto->close(hu);
 		hu->hdev = NULL;
 		hci_free_dev(hdev);

-- 
2.39.5


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

end of thread, other threads:[~2026-03-27 11:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-26  9:59 [PATCH v2 0/2] Bluetooth: fix race during h4 bluetooth initialization Jonathan Rissanen
2026-03-26  9:59 ` [PATCH v2 1/2] Bluetooth: hci_ldisc: Clear HCI_UART_PROTO_INIT on error Jonathan Rissanen
2026-03-26 11:08   ` Bluetooth: fix race during h4 bluetooth initialization bluez.test.bot
2026-03-26  9:59 ` [PATCH v2 2/2] Bluetooth: hci_h4: Fix race during initialization Jonathan Rissanen
2026-03-26 16:38 ` [PATCH v2 0/2] Bluetooth: fix race during h4 bluetooth initialization Luiz Augusto von Dentz
  -- strict thread matches above, loose matches on Subject: below --
2026-03-27 10:47 [PATCH v3 1/2] Bluetooth: hci_ldisc: Clear HCI_UART_PROTO_INIT on error Jonathan Rissanen
2026-03-27 11:32 ` Bluetooth: fix race during h4 bluetooth initialization bluez.test.bot

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