Bluetooth: SMP: Serialize root channel updates
Subsystems (2)
v5 cover Bluetooth: SMP: Serialize root channel updates
SMP root changes must serialize with their users. Publish BT_LISTEN only after the root is initialized, under the global channel list lock, so another controller with the same address cannot select a partially initialized root (patch 1).
The force_bredr_smp writer takes hdev->lock and changes only its flag while the controller is down (patch 2). Power-on holds the request lock for its auto-off update (patch 3). Registration then runs on the ordered HCI workqueue under hdev->lock (patch 4), excluding both HCI event users and SMP receive callbacks from root replacement. Patch 4 depends on patch 3. Patch 2's exclusion from power-on also needs patch 4.
The old mgmt_powered() called smp_register() synchronously under the device mutex before submitting the HCI update. It did not run in the receive worker. SMP receive callbacks began using root-owned OOB data in March 2015, before the synchronous HCI update conversion that November. The shared root transform use was added in September 2018. Patch 4 corrects that history and tags the earlier root-data access as well.
The WARN_ON() for replacing an already registered root remains. The separate HCI_CONFIG auto-off shortcut issue mentioned in the review also predates this series and is outside these root lifetime changes.
Validation: GCC modular and Clang built-in Bluetooth builds, including Clang context analysis; every prefix and the combined tree with the six new Bluetooth series build successfully. No new kernel runtime test was run; the quoted reports are from the earlier instrumented test kernels.
I used Codex (OpenAI) to help write these patches.
Cen Zhang (4):
Bluetooth: SMP: Publish root channels after initialization
Bluetooth: SMP: Serialize forced BR/EDR channel changes
Bluetooth: hci_core: Hold the request lock for the power-on update
Bluetooth: SMP: Serialize root channel replacement
include/net/bluetooth/l2cap.h | 1 + net/bluetooth/hci_core.c | 3 +++ net/bluetooth/hci_debugfs.c | 2 ++ net/bluetooth/l2cap_core.c | 8 ++++++ net/bluetooth/smp.c | 46 ++++++++++++++++++++++++++++++++--- net/bluetooth/smp.h | 3 ++- 6 files changed, 59 insertions(+), 4 deletions(-)
---
Changes in v5:
- Move the AI assistance disclosure to plain text in the cover letter.
Changes in v4:
- Address the cross-controller root publication and powered-off debugfs concerns raised by Sashiko:
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/pm-bluetooth-objects-candidate-0184-v9-4094fa28af7bd56a283a-0%40gmail.com
- Publish fixed roots only after initialization under chan_list_lock, and change only the force flag while HCI_UP is clear.
- Correct the registration history and early root-data Fixes reference; state the dependency between debugfs and power-on serialization.
Changes in v3:
- Address the hci_power_on() path that reaches smp_register() without the request lock, raised by Sashiko: https://sashiko.dev/#/patchset/99158
- Add patch 2, which takes the request lock in hci_power_on() again, and correct the locking description of patch 3; the code of patches 1 and 3 is unchanged.
Changes in v2:
- Address the CheckPatch and GitLint long-line failures reported by the Bluetooth CI: https://patchwork.kernel.org/series/1197223/
- Trim the crash reports in both commit messages to the headline, call trace and allocation/free stacks; no code changes.
Link to v1: https://lore.kernel.org/r/pm-bluetooth-objects-candidate-0184-v7-c26e53b52c260063ab6a-0@gmail.com
Link to v2: https://lore.kernel.org/r/pm-bluetooth-objects-candidate-0184-v8-60106ad0c49170f5bfb4-0@gmail.com
Link to v3: https://lore.kernel.org/r/pm-bluetooth-objects-candidate-0184-v9-4094fa28af7bd56a283a-0@gmail.com
Link to v4: https://lore.kernel.org/r/pm-bluetooth-objects-candidate-0184-v11-900043ed2303656c76fe-0@gmail.com
PATCH v5 1/4 Bluetooth: SMP: Publish root channels after initialization
smp_add_cid() marks a channel as fixed before setting its operations, address and parent nesting level. Once it also sets BT_LISTEN, a global fixed-channel lookup can select it before the remaining stores finish:
Controller A Controller B with the same address
smp_add_cid()
l2cap_add_scid()
chan->state = BT_LISTEN
l2cap_global_fixed_chan()
select A's root
l2cap_new_connection()
call chan->ops->new_connection
chan->ops = &smp_root_chan_opsA per-controller mutex cannot exclude the second controller. Finish initializing the listener before setting BT_LISTEN, and serialize that state change with global lookups through chan_list_lock. Keep the fixed channel type assigned before publishing BT_LISTEN so connection-oriented PSM lookups cannot select an untyped channel in between.
--- include/net/bluetooth/l2cap.h | 1 + net/bluetooth/l2cap_core.c | 8 ++++++++ net/bluetooth/smp.c | 3 ++- 3 files changed, 11 insertions(+), 1 deletion(-)
include/net/bluetooth/l2cap.h +1 -0
@@ -982,6 +982,7 @@ int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm);int l2cap_add_scid (struct l2cap_chan *chan,__u16 scid);struct l2cap_chan *l2cap_chan_create (void ); +void l2cap_chan_listen (struct l2cap_chan *chan); void l2cap_chan_close_unlocked(struct l2cap_chan *chan, int reason) __must_not_hold(&chan->lock); int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
net/bluetooth/l2cap_core.c +8 -0
@@ -456,6 +456,14 @@ static void l2cap_chan_timeout(struct work_struct *work)l2cap_chan_put (chan); } +/* Publish a fully initialized listener to global channel lookups. */ +void l2cap_chan_listen (struct l2cap_chan *chan) +{ +write_lock (&chan_list_lock); + chan->state =BT_LISTEN ; +write_unlock (&chan_list_lock); +} +struct l2cap_chan *l2cap_chan_create (void ) {struct l2cap_chan *chan;
net/bluetooth/smp.c +2 -1
@@ -3400,7 +3400,6 @@ static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid) chan->src_type =BDADDR_BREDR ; } - chan->state =BT_LISTEN ; chan->mode =L2CAP_MODE_BASIC ; chan->imtu =L2CAP_DEFAULT_MTU ; chan->ops = &smp_root_chan_ops;
@@ -3408,6 +3407,8 @@ static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)/* Set correct nesting level for a parent/listening channel */ atomic_set (&chan->nesting,L2CAP_NESTING_PARENT ); +l2cap_chan_listen (chan); +return chan; }
PATCH v5 2/4 Bluetooth: SMP: Serialize forced BR/EDR channel changes
In reply to Cen Zhang Debugfs disable Power-off
load smp_bredr_data
clear the same slot release the channel
release the saved channel again
Take hdev->lock around the debugfs writer to serialize slot changes with power-off. When HCI_UP is clear, change only the force flag and let the next power-on register the root. Otherwise enabling the flag can leave a listening channel that an already-down close never removes. Skip deletion when power-off already cleared the slot.
The following registration change uses the same mutex to serialize this writer with power-on too. The preceding publication change protects lookups from another controller with the same address.
Oops report as below:
Oops: general protection fault, probably for non-canonical address
0xdffffc0000000001: 0000 [#1] SMP KASAN NOPTI
KASAN: null-ptr-deref in range [0x0000000000000008-0x000000000000000f]
Workqueue: hci1 hci_rx_work
RIP: 0010:l2cap_new_connection+0x16c/0x320
Call Trace:
<TASK>
l2cap_connect_cfm+0x46c/0xe60
hci_remote_ext_features_evt+0x67d/0xbd0
hci_event_packet+0x894/0xc70
hci_rx_work+0x367/0xf00
process_one_work+0x908/0x19c0
worker_thread+0x65c/0xe40
kthread+0x34f/0x460
ret_from_fork+0x659/0x940
ret_from_fork_asm+0x1a/0x30
</TASK>KASAN report as below:
==================================================================
BUG: KASAN: slab-use-after-free in smp_del_chan+0xd2/0xe0
Read of size 8 at addr ffff888112a2e4a0 by task sh/493
Call Trace:
<TASK>
dump_stack_lvl+0x93/0xd0
print_report+0xce/0x630
kasan_report+0xe0/0x110
smp_del_chan+0xd2/0xe0
smp_force_bredr+0xda/0x200
force_bredr_smp_write+0xc9/0x130
full_proxy_write+0x11f/0x180
vfs_write+0x25a/0x1010
ksys_write+0x111/0x200
do_syscall_64+0x115/0x6a0
entry_SYSCALL_64_after_hwframe+0x77/0x7f
</TASK>
Allocated by task 493:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
__kasan_kmalloc+0xaa/0xb0
__kmalloc_cache_noprof+0x251/0x610
l2cap_chan_create+0x47/0x930
smp_add_cid+0x309/0x4f0
smp_force_bredr+0x17d/0x200
force_bredr_smp_write+0xc9/0x130
full_proxy_write+0x11f/0x180
vfs_write+0x25a/0x1010
ksys_write+0x111/0x200
do_syscall_64+0x115/0x6a0
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Freed by task 496:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
kasan_save_free_info+0x3b/0x60
__kasan_slab_free+0x5f/0x80
kfree+0x307/0x580
l2cap_chan_put+0x273/0x3a0
smp_unregister+0x59/0xe0
hci_dev_close_sync+0x4c9/0x1150
hci_set_powered_sync+0x4a7/0x680
set_powered_sync+0xaa/0x150
hci_cmd_sync_work+0x1b3/0x450
process_one_work+0x908/0x19c0
worker_thread+0x65c/0xe40
kthread+0x34f/0x460
ret_from_fork+0x659/0x940
ret_from_fork_asm+0x1a/0x30
The buggy address belongs to the object at ffff888112a2e000
which belongs to the cache kmalloc-2k of size 2048
The buggy address is located 1184 bytes inside of
freed 2048-byte region [ffff888112a2e000, ffff888112a2e800)
==================================================================--- net/bluetooth/hci_debugfs.c | 2 ++ net/bluetooth/smp.c | 7 ++++++- net/bluetooth/smp.h | 3 ++- 3 files changed, 10 insertions(+), 2 deletions(-)
net/bluetooth/hci_debugfs.c +2 -0
@@ -525,7 +525,9 @@ static ssize_t force_bredr_smp_write(struct file *file,if (err)return err; +hci_dev_lock (hdev); err =smp_force_bredr (hdev, enable); +hci_dev_unlock (hdev);if (err)return err;
net/bluetooth/smp.c +6 -1
@@ -3433,6 +3433,10 @@ int smp_force_bredr(struct hci_dev *hdev, bool enable)if (enable ==hci_dev_test_flag (hdev,HCI_FORCE_BREDR_SMP ))return -EALREADY ; +/* A powered-off controller has no registered SMP roots. */ +if (!test_bit (HCI_UP , &hdev->flags)) + goto done; +if (enable) {struct l2cap_chan *chan;
@@ -3441,7 +3445,7 @@ int smp_force_bredr(struct hci_dev *hdev, bool enable)return PTR_ERR (chan); hdev->smp_bredr_data = chan; - }else { + }else if (hdev->smp_bredr_data) {struct l2cap_chan *chan; chan = hdev->smp_bredr_data;
@@ -3449,6 +3453,7 @@ int smp_force_bredr(struct hci_dev *hdev, bool enable)smp_del_chan (chan); } +done:hci_dev_change_flag (hdev,HCI_FORCE_BREDR_SMP );return 0 ;
net/bluetooth/smp.h +2 -1
@@ -195,7 +195,8 @@ bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],int smp_generate_rpa (struct hci_dev *hdev,const u8 irk[16 ],bdaddr_t *rpa);int smp_generate_oob (struct hci_dev *hdev,u8 hash[16 ],u8 rand[16 ]); -int smp_force_bredr (struct hci_dev *hdev,bool enable); +int smp_force_bredr(struct hci_dev *hdev, bool enable) + __must_hold(&hdev->lock);int smp_register (struct hci_dev *hdev);void smp_unregister (struct hci_dev *hdev);
PATCH v5 3/4 Bluetooth: hci_core: Hold the request lock for the power-on update
In reply to Cen ZhangWhen the controller is already up in the HCI_AUTO_OFF window, for example after set_public_address() or set_external_config() queued hdev->power_on, hci_power_on() runs hci_powered_update_sync() directly. The update registers the SMP channels and sends HCI commands through __hci_cmd_sync() and its variants, which require hdev->req_lock. Commit cf75ad8b41d2 ("Bluetooth: hci_sync: Convert MGMT_SET_POWERED") dropped the request lock that this path held around the update.
hdev->power_on runs on hdev->req_workqueue, which orders it with the other request works, but not with request lock users in process context such as the HCIDEVRESET ioctl or an rfkill block. Such a user can close the controller after the HCI_UP test or while the update runs, and the update then registers the SMP channels again after hci_dev_close_sync() unregistered them:
hdev->power_on HCIDEVRESET ioctl
test HCI_UP
clear HCI_AUTO_OFF
hci_req_sync_lock()
hci_dev_close_sync()
smp_unregister() hci_powered_update_sync()
smp_register()
send HCI commands
Take the request lock again before testing that the controller is up, as hci_power_on_sync() runs the same update with the lock held when management powers on the controller through the cmd_sync queue.
--- net/bluetooth/hci_core.c | 3 +++ 1 file changed, 3 insertions(+)
net/bluetooth/hci_core.c +3 -0
@@ -1006,14 +1006,17 @@ static void hci_power_on(struct work_struct *work)BT_DBG ("%s" , hdev->name); +hci_req_sync_lock (hdev);if (test_bit (HCI_UP , &hdev->flags) &&hci_dev_test_flag (hdev,HCI_MGMT ) &&hci_dev_test_and_clear_flag (hdev,HCI_AUTO_OFF )) {cancel_delayed_work (&hdev->power_off); err =hci_powered_update_sync (hdev); +hci_req_sync_unlock (hdev);mgmt_power_on (hdev, err);return ; } +hci_req_sync_unlock (hdev); err =hci_dev_do_open (hdev);if (err <0 ) {
PATCH v5 4/4 Bluetooth: SMP: Serialize root channel replacement
In reply to Cen Zhangsmp_register() replaces the SMP root channels when they are still registered from an earlier power-on. This happens when management powers on a controller that is already up with HCI_AUTO_OFF set:hci_power_on_sync() clears the flag and runs hci_powered_update_sync() again, which registers SMP so that the channels use the identity address chosen for this power-on. The WARN_ON() in smp_register() fires on this path, and the old channels and their SMP data are freed while two kinds of users can still hold them.
HCI event handlers read hdev->smp_data under hdev->lock, for example smp_irk_matches() when resolving the target of a directed advertising report:
HCI receive worker Power-on request
hci_dev_lock(hdev)
smp_irk_matches()
chan = hdev->smp_data
hci_powered_update_sync()
smp_register()
smp_del_chan(chan), chan freed
reads chan->dataSMP receive callbacks run on hdev->workqueue without hdev->lock and use the ECDH transform of the root, for example smp_cmd_public_key():
HCI receive worker (SMP) Power-on request
smp_cmd_public_key()
tfm = root's tfm_ecdh
smp_register()
smp_del_chan()
crypto_free_kpp(tfm)
compute_ecdh_secret(tfm, ...)Before commit 2ff13894cfb8 ("Bluetooth: Perform HCI update for power on synchronously"), mgmt_powered() called smp_register() synchronously under hdev->lock, before submitting the HCI update. That excluded HCI event users, but did not exclude SMP receive callbacks. Those callbacks already used root-owned OOB data after commit 33d0c030717b ("Bluetooth:
Use OOB key pair for LE SC pairing with OOB method"). The later commit 4ba5175f2c10 ("Bluetooth: Use correct tfm to generate OOB data") made the ECDH calculation use the root's transform too.
Queue registration on hdev->workqueue and hold hdev->lock while it replaces the channels. This adds exclusion from SMP receive callbacks and restores exclusion from HCI event users. smp_register() waits for the work and returns its result. Its callers hold the request lock but not hdev->lock, including hci_power_on() when it updates a controller in the HCI_AUTO_OFF window. No work on hdev->workqueue takes the request lock, and hci_dev_close_sync() drains this workqueue only with the request lock held, so the registration work cannot be queued while it drains. Users that run under the request lock, such as smp_generate_rpa(), stay serialized by that lock.
The reports below come from test kernels with scheduling hooks that widen these windows. The WARN_ON() in smp_register() was printed before each of them.
KASAN report as below:
==================================================================
BUG: KASAN: slab-use-after-free in smp_irk_matches+0x2fb/0x310
Read of size 8 at addr ffff88810c35f4a0 by task kworker/u17:0/520
Workqueue: hci0 hci_rx_work
Call Trace:
<TASK>
dump_stack_lvl+0x93/0xd0
print_report+0xce/0x630
kasan_report+0xe0/0x110
smp_irk_matches+0x2fb/0x310
process_adv_report.part.0+0x2ce/0x1680
hci_le_direct_adv_report_evt+0x2f8/0x4c0
hci_le_meta_evt+0x30c/0x5c0
hci_event_packet+0x4e8/0xc70
hci_rx_work+0x367/0xf00
process_one_work+0x908/0x19c0
worker_thread+0x65c/0xe40
kthread+0x34f/0x460
ret_from_fork+0x659/0x940
ret_from_fork_asm+0x1a/0x30
</TASK>
Allocated by task 522:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
__kasan_kmalloc+0xaa/0xb0
__kmalloc_cache_noprof+0x251/0x610
l2cap_chan_create+0x47/0x930
smp_add_cid+0x120/0x4f0
smp_register+0xa7/0x400
hci_powered_update_sync+0x88/0x720
hci_dev_open_sync+0xae0/0x2060
hci_set_powered_sync+0x2a9/0x720
set_powered_sync+0xaa/0x150
hci_cmd_sync_work+0x1b3/0x450
process_one_work+0x908/0x19c0
worker_thread+0x65c/0xe40
kthread+0x34f/0x460
ret_from_fork+0x659/0x940
ret_from_fork_asm+0x1a/0x30
Freed by task 522:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
kasan_save_free_info+0x3b/0x60
__kasan_slab_free+0x5f/0x80
kfree+0x307/0x580
l2cap_chan_put+0x273/0x3a0
smp_del_chan+0x9f/0x170
smp_register+0x2e8/0x400
hci_powered_update_sync+0x88/0x720
hci_set_powered_sync+0x5e5/0x720
set_powered_sync+0xaa/0x150
hci_cmd_sync_work+0x1b3/0x450
process_one_work+0x908/0x19c0
worker_thread+0x65c/0xe40
kthread+0x34f/0x460
ret_from_fork+0x659/0x940
ret_from_fork_asm+0x1a/0x30
The buggy address belongs to the object at ffff88810c35f000
which belongs to the cache kmalloc-2k of size 2048
The buggy address is located 1184 bytes inside of
freed 2048-byte region [ffff88810c35f000, ffff88810c35f800)
==================================================================KASAN report as below:
==================================================================
BUG: KASAN: slab-use-after-free in compute_ecdh_secret+0x856/0xa50
Read of size 4 at addr ffff8881071f4f00 by task kworker/u17:2/500
Workqueue: hci1 hci_rx_work
Call Trace:
<TASK>
dump_stack_lvl+0x93/0xd0
print_report+0xce/0x630
kasan_report+0xe0/0x110
compute_ecdh_secret+0x856/0xa50
smp_recv_cb+0x3acf/0x93b0
l2cap_recv_frame+0x13cc/0x8830
l2cap_recv_acldata+0xe9f/0x10e0
hci_rx_work+0x5a6/0xf00
process_one_work+0x908/0x19c0
worker_thread+0x65c/0xe40
kthread+0x34f/0x460
ret_from_fork+0x659/0x940
ret_from_fork_asm+0x1a/0x30
</TASK>
Allocated by task 497:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
__kasan_kmalloc+0xaa/0xb0
__kmalloc_node_noprof+0x2f2/0x7b0
crypto_create_tfm_node+0x92/0x390
crypto_alloc_tfm_node+0xf5/0x220
smp_add_cid+0xc1/0x4f0
smp_register+0x9d/0x430
hci_powered_update_sync+0x82/0x680
hci_set_powered_sync+0x4f4/0x750
set_powered_sync+0xb8/0x210
hci_cmd_sync_work+0x1b3/0x450
process_one_work+0x908/0x19c0
worker_thread+0x65c/0xe40
kthread+0x34f/0x460
ret_from_fork+0x659/0x940
ret_from_fork_asm+0x1a/0x30
Freed by task 497:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
kasan_save_free_info+0x3b/0x60
__kasan_slab_free+0x5f/0x80
kfree+0x307/0x580
crypto_destroy_tfm+0xdd/0x230
smp_del_chan+0x8b/0x180
smp_register+0x29d/0x430
hci_powered_update_sync+0x82/0x680
hci_set_powered_sync+0x4f4/0x750
set_powered_sync+0xb8/0x210
hci_cmd_sync_work+0x1b3/0x450
process_one_work+0x908/0x19c0
worker_thread+0x65c/0xe40
kthread+0x34f/0x460
ret_from_fork+0x659/0x940
ret_from_fork_asm+0x1a/0x30
The buggy address belongs to the object at ffff8881071f4f00
which belongs to the cache kmalloc-128 of size 128
The buggy address is located 0 bytes inside of
freed 128-byte region [ffff8881071f4f00, ffff8881071f4f80)
==================================================================--- net/bluetooth/smp.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-)
net/bluetooth/smp.c +35 -1
@@ -3459,7 +3459,8 @@ int smp_force_bredr(struct hci_dev *hdev, bool enable)return 0 ; } -int smp_register (struct hci_dev *hdev) +static int __smp_register (struct hci_dev *hdev) + __must_hold(&hdev->lock) {struct l2cap_chan *chan;
@@ -3509,6 +3510,39 @@ int smp_register(struct hci_dev *hdev)return 0 ; } +struct smp_register_work { +struct work_struct work; +struct hci_dev *hdev; +int err; +}; + +static void smp_register_func (struct work_struct *work) +{ +struct smp_register_work *reg = + container_of(work, struct smp_register_work, work); +struct hci_dev *hdev = reg->hdev; + +hci_dev_lock (hdev); + reg->err =__smp_register (hdev); +hci_dev_unlock (hdev); +} + +int smp_register (struct hci_dev *hdev) +{ +struct smp_register_work reg = { .hdev = hdev }; + +/* HCI event handlers use the root channels under hdev->lock, and +* SMP receive callbacks use them from hdev->workqueue. Replace the +* channels from that workqueue while holding hdev->lock. +*/ +INIT_WORK_ONSTACK (®.work, smp_register_func); +queue_work (hdev->workqueue, ®.work); +flush_work (®.work); +destroy_work_on_stack (®.work); + +return reg.err; +} +void smp_unregister (struct hci_dev *hdev) {struct l2cap_chan *chan;