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 v2 1/2] Bluetooth: hci_ldisc: Clear HCI_UART_PROTO_INIT on error
  2026-03-26  9:59 [PATCH v2 0/2] Bluetooth: fix race during h4 bluetooth initialization Jonathan Rissanen
@ 2026-03-26  9:59 ` 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
  2 siblings, 1 reply; 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

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.

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

* [PATCH v2 2/2] Bluetooth: hci_h4: Fix race during initialization
  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  9:59 ` Jonathan Rissanen
  2026-03-26 16:38 ` [PATCH v2 0/2] Bluetooth: fix race during h4 bluetooth initialization Luiz Augusto von Dentz
  2 siblings, 0 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

Commit 5df5dafc171b ("Bluetooth: hci_uart: Fix another race during
initialization") fixed a race for hci commands sent during initialization.
However, there is still a race that happens if an hci event from one of
these commands is received before HCI_UART_REGISTERED has been set at
the end of hci_uart_register_dev(). The event will be ignored which
causes the command to fail with a timeout in the log:

"Bluetooth: hci0: command 0x1003 tx timeout"

This is because the hci event receive path (hci_uart_tty_receive ->
h4_recv) requires HCI_UART_REGISTERED to be set in h4_recv(), while the
hci command transmit path (hci_uart_send_frame -> h4_enqueue) only
requires HCI_UART_PROTO_INIT to be set in hci_uart_send_frame().

The check for HCI_UART_REGISTERED was originally added in commit
c2578202919a ("Bluetooth: Fix H4 crash from incoming UART packets")
to fix a crash caused by hu->hdev being null dereferenced. That can no
longer happen: once HCI_UART_PROTO_INIT is set in hci_uart_register_dev()
all pointers (hu, hu->priv and hu->hdev) are valid, and
hci_uart_tty_receive() already calls h4_recv() on HCI_UART_PROTO_INIT
or HCI_UART_PROTO_READY.

Remove the check for HCI_UART_REGISTERED in h4_recv() to fix the race
condition.

Signed-off-by: Jonathan Rissanen <jonathan.rissanen@axis.com>
---
 drivers/bluetooth/hci_h4.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/drivers/bluetooth/hci_h4.c b/drivers/bluetooth/hci_h4.c
index ec017df8572c..1e9e2cad9ddf 100644
--- a/drivers/bluetooth/hci_h4.c
+++ b/drivers/bluetooth/hci_h4.c
@@ -109,9 +109,6 @@ static int h4_recv(struct hci_uart *hu, const void *data, int count)
 {
 	struct h4_struct *h4 = hu->priv;
 
-	if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
-		return -EUNATCH;
-
 	h4->rx_skb = h4_recv_buf(hu, h4->rx_skb, data, count,
 				 h4_recv_pkts, ARRAY_SIZE(h4_recv_pkts));
 	if (IS_ERR(h4->rx_skb)) {

-- 
2.39.5


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

* RE: Bluetooth: fix race during h4 bluetooth initialization
  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   ` bluez.test.bot
  0 siblings, 0 replies; 6+ messages in thread
From: bluez.test.bot @ 2026-03-26 11:08 UTC (permalink / raw)
  To: linux-bluetooth, jonathan.rissanen

[-- Attachment #1: Type: text/plain, Size: 2834 bytes --]

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1072851

---Test result---

Test Summary:
CheckPatch                    PENDING   0.36 seconds
GitLint                       PENDING   0.27 seconds
SubjectPrefix                 PASS      0.24 seconds
BuildKernel                   PASS      26.06 seconds
CheckAllWarning               PASS      28.32 seconds
CheckSparse                   PASS      27.19 seconds
BuildKernel32                 PASS      25.19 seconds
TestRunnerSetup               PASS      566.64 seconds
TestRunner_l2cap-tester       PASS      27.65 seconds
TestRunner_iso-tester         FAIL      30.48 seconds
TestRunner_bnep-tester        PASS      6.38 seconds
TestRunner_mgmt-tester        FAIL      115.27 seconds
TestRunner_rfcomm-tester      PASS      11.22 seconds
TestRunner_sco-tester         FAIL      14.34 seconds
TestRunner_ioctl-tester       PASS      10.34 seconds
TestRunner_mesh-tester        FAIL      11.47 seconds
TestRunner_smp-tester         PASS      8.58 seconds
TestRunner_userchan-tester    PASS      6.96 seconds
IncrementalBuild              PENDING   0.51 seconds

Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script
Output:

##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:

##############################
Test: TestRunner_iso-tester - FAIL
Desc: Run iso-tester with test-runner
Output:
BUG: KASAN: slab-use-after-free in le_read_features_complete+0x7e/0x2b0
Total: 141, Passed: 141 (100.0%), Failed: 0, Not Run: 0
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner
Output:
Total: 494, Passed: 489 (99.0%), Failed: 1, Not Run: 4

Failed Test Cases
Read Exp Feature - Success                           Failed       0.110 seconds
##############################
Test: TestRunner_sco-tester - FAIL
Desc: Run sco-tester with test-runner
Output:
WARNING: possible circular locking dependency detected
BUG: sleeping function called from invalid context at net/core/sock.c:3782
Total: 30, Passed: 30 (100.0%), Failed: 0, Not Run: 0
##############################
Test: TestRunner_mesh-tester - FAIL
Desc: Run mesh-tester with test-runner
Output:
Total: 10, Passed: 8 (80.0%), Failed: 2, Not Run: 0

Failed Test Cases
Mesh - Send cancel - 1                               Timed out    1.796 seconds
Mesh - Send cancel - 2                               Timed out    1.999 seconds
##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series
Output:



---
Regards,
Linux Bluetooth


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

* Re: [PATCH v2 0/2] Bluetooth: fix race during h4 bluetooth initialization
  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  9:59 ` [PATCH v2 2/2] Bluetooth: hci_h4: Fix race during initialization Jonathan Rissanen
@ 2026-03-26 16:38 ` Luiz Augusto von Dentz
  2 siblings, 0 replies; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2026-03-26 16:38 UTC (permalink / raw)
  To: Jonathan Rissanen; +Cc: Marcel Holtmann, linux-bluetooth, linux-kernel, kernel

Hi Jonathan,

On Thu, Mar 26, 2026 at 5:59 AM Jonathan Rissanen
<jonathan.rissanen@axis.com> wrote:
>
> 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

These should have Fixes: tag included as well before we merge;
otherwise, they look good to me.

>  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>
>


-- 
Luiz Augusto von Dentz

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

* RE: Bluetooth: fix race during h4 bluetooth initialization
  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 ` bluez.test.bot
  0 siblings, 0 replies; 6+ messages in thread
From: bluez.test.bot @ 2026-03-27 11:32 UTC (permalink / raw)
  To: linux-bluetooth, jonathan.rissanen

[-- Attachment #1: Type: text/plain, Size: 2833 bytes --]

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1073500

---Test result---

Test Summary:
CheckPatch                    PENDING   0.39 seconds
GitLint                       PENDING   0.23 seconds
SubjectPrefix                 PASS      0.23 seconds
BuildKernel                   PASS      25.13 seconds
CheckAllWarning               PASS      30.16 seconds
CheckSparse                   PASS      26.52 seconds
BuildKernel32                 PASS      24.27 seconds
TestRunnerSetup               PASS      519.71 seconds
TestRunner_l2cap-tester       PASS      27.44 seconds
TestRunner_iso-tester         FAIL      33.49 seconds
TestRunner_bnep-tester        PASS      6.40 seconds
TestRunner_mgmt-tester        FAIL      110.54 seconds
TestRunner_rfcomm-tester      PASS      9.24 seconds
TestRunner_sco-tester         FAIL      14.16 seconds
TestRunner_ioctl-tester       PASS      10.00 seconds
TestRunner_mesh-tester        FAIL      11.47 seconds
TestRunner_smp-tester         PASS      8.54 seconds
TestRunner_userchan-tester    PASS      6.61 seconds
IncrementalBuild              PENDING   1.04 seconds

Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script
Output:

##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:

##############################
Test: TestRunner_iso-tester - FAIL
Desc: Run iso-tester with test-runner
Output:
BUG: KASAN: slab-use-after-free in le_read_features_complete+0x7e/0x2b0
Total: 141, Passed: 141 (100.0%), Failed: 0, Not Run: 0
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner
Output:
Total: 494, Passed: 489 (99.0%), Failed: 1, Not Run: 4

Failed Test Cases
Read Exp Feature - Success                           Failed       0.102 seconds
##############################
Test: TestRunner_sco-tester - FAIL
Desc: Run sco-tester with test-runner
Output:
WARNING: possible circular locking dependency detected
BUG: sleeping function called from invalid context at net/core/sock.c:3782
Total: 30, Passed: 30 (100.0%), Failed: 0, Not Run: 0
##############################
Test: TestRunner_mesh-tester - FAIL
Desc: Run mesh-tester with test-runner
Output:
Total: 10, Passed: 8 (80.0%), Failed: 2, Not Run: 0

Failed Test Cases
Mesh - Send cancel - 1                               Timed out    1.927 seconds
Mesh - Send cancel - 2                               Timed out    1.996 seconds
##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series
Output:



---
Regards,
Linux Bluetooth


^ permalink raw reply	[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