Bluetooth: 6lowpan: Protect adapter use and notification lifetime
Subsystems (2)
cover Bluetooth: 6lowpan: Protect adapter use and notification lifetime
An adapter remains discoverable while asynchronous netdevice deletion is pending. Protect lookup and ready-callback use with RTNL, and close the delayed-notification producer before deleting its embedded storage.
Cen Zhang (2):
Bluetooth: 6lowpan: Hold RTNL across adapter lookup and use
Bluetooth: 6lowpan: Disable notify_peers before netdev deletion
net/bluetooth/6lowpan.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
---
PATCH 1/2 Bluetooth: 6lowpan: Hold RTNL across adapter lookup and use
lookup_dev() returns a borrowed adapter after dropping RCU. The last peer's asynchronous deletion can unregister and free its netdevice while a later ready callback uses that pointer:
Ready callback Deletion worker
lookup_dev()
leave RCU
lock RTNL
unregister the netdevice remove the adapter from the list
ifup(): wait for RTNL
unlock RTNL and release storage
open the stale netdevice
Take RTNL before lookup and retain it through peer publication and device open. Use lowpan_register_netdevice() because registration now runs with RTNL held, and remove the nested lock from ifup(). Deletion cannot revoke the adapter between its lookup and last ready-callback use.
The excerpt below retains the headline and stack traces.
KASAN report as below:
Oops: Oops: 0000 [#1] SMP KASAN NOPTI
Workqueue: hci1 hci_rx_work
RIP: 0010:dev_addr_check+0x4e/0x260
Call Trace:
<TASK>
__dev_open+0xa8/0x6d0
? __pfx___dev_open+0x10/0x10
netif_open+0xde/0x140
? __pfx_netif_open+0x10/0x10
dev_open+0xb9/0x180
chan_ready_cb+0x420/0xf90
? __pfx_chan_ready_cb+0x10/0x10
? lockdep_init_map_type+0x5c/0x230
? srso_alias_return_thunk+0x5/0xfbef5
? l2cap_chan_put+0x48/0x3a0
l2cap_le_connect_req+0xccd/0x11a0
? __pfx_l2cap_le_connect_req+0x10/0x10
l2cap_recv_frame+0x467f/0x8830
? kfree_skbmem+0x15e/0x1d0
? srso_alias_return_thunk+0x5/0xfbef5
? srso_alias_return_thunk+0x5/0xfbef5
? __mutex_lock+0x182c/0x2590
? srso_alias_return_thunk+0x5/0xfbef5
? __pfx_l2cap_recv_frame+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? find_held_lock+0x2b/0x80
? l2cap_recv_acldata+0x1eb/0x10e0
? __pfx___mutex_lock+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? __mutex_unlock_slowpath+0x147/0x920
? __pfx_l2cap_conn_hold_unless_zero+0x10/0x10
? __pfx_hci_conn_enter_active_mode+0x10/0x10
? l2cap_recv_acldata+0x13d/0x10e0
? srso_alias_return_thunk+0x5/0xfbef5
l2cap_recv_acldata+0xe9f/0x10e0
? srso_alias_return_thunk+0x5/0xfbef5
hci_rx_work+0x5a6/0xf00
? srso_alias_return_thunk+0x5/0xfbef5
process_one_work+0x908/0x19c0
? __pfx_process_one_work+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? lock_is_held_type+0x8f/0x100
? srso_alias_return_thunk+0x5/0xfbef5
worker_thread+0x65c/0xe40
? __pfx_worker_thread+0x10/0x10
kthread+0x34f/0x460
? srso_alias_return_thunk+0x5/0xfbef5
? __pfx_kthread+0x10/0x10
ret_from_fork+0x659/0x940
? __pfx_ret_from_fork+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? __switch_to+0x74f/0xf80
? __pfx_kthread+0x10/0x10
ret_from_fork_asm+0x1a/0x30
</TASK>
RIP: 0010:dev_addr_check+0x4e/0x260---
This patch was written with assistance from Codex (OpenAI).
net/bluetooth/6lowpan.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
net/bluetooth/6lowpan.c +12 -5
@@ -597,11 +597,11 @@ static void ifup(struct net_device *netdev) {int err; -rtnl_lock (); +ASSERT_RTNL (); + err =dev_open (netdev,NULL );if (err <0 )BT_INFO ("iface %s cannot be opened (%d)" , netdev->name, err); -rtnl_unlock (); }static void ifdown (struct net_device *netdev)
@@ -706,7 +706,7 @@ static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev)list_add_rcu (&(*dev)->list, &bt_6lowpan_devices);spin_unlock (&devices_lock); - err =lowpan_register_netdev (netdev,LOWPAN_LLTYPE_BTLE ); + err =lowpan_register_netdevice (netdev,LOWPAN_LLTYPE_BTLE );if (err <0 ) {BT_INFO ("register_netdev failed %d" , err);spin_lock (&devices_lock);
@@ -735,12 +735,16 @@ static inline void chan_ready_cb(struct l2cap_chan *chan)struct lowpan_btle_dev *dev;bool new_netdev = false; +/* Protect the device lookup and use against unregister. */ +rtnl_lock (); + dev =lookup_dev (chan->conn);BT_DBG ("chan %p conn %p dev %p" , chan, chan->conn, dev);if (!dev) {if (setup_netdev (chan, &dev) <0 ) { +rtnl_unlock ();l2cap_chan_del (chan, -ENOENT );return ; }
@@ -748,10 +752,13 @@ static inline void chan_ready_cb(struct l2cap_chan *chan) }if (!try_module_get (THIS_MODULE )) -return ; + goto out;add_peer_chan (chan, dev, new_netdev);ifup (dev->netdev); + +out: +rtnl_unlock (); }static void unregister_dev (struct lowpan_btle_dev *dev)
@@ -763,7 +770,7 @@ static void unregister_dev(struct lowpan_btle_dev *dev)* * rtnl_lock -> put_device(parent) -> hci_release_dev -> * destroy_workqueue -> hci_rx_work -> l2cap_recv_acldata -> -* chan_ready_cb -> ifup -> rtnl_lock +* chan_ready_cb -> rtnl_lock * * However, hci_rx_work is disabled in hci_unregister_dev, so this * should not occur. Make lockdep happy by postponing hdev release after
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/series/1217950/
---Test result---
Test Summary:
CheckPatch PASS 1.14 seconds VerifyFixes PASS 0.11 seconds VerifySignedoff PASS 0.10 seconds GitLint PASS 0.53 seconds SubjectPrefix PASS 0.20 seconds BuildKernel PASS 21.46 seconds CheckAllWarning PASS 24.32 seconds CheckSparse PASS 25.75 seconds BuildKernel32 PASS 20.53 seconds CheckKernelLLVM PASS 21.41 seconds TestRunnerSetup PASS 455.03 seconds TestRunner_6lowpan-tester PASS 6.76 seconds IncrementalBuild PASS 21.32 seconds
https://github.com/bluez/bluetooth-next/pull/1174
---
Regards,
Linux Bluetooth
PATCH 2/2 Bluetooth: 6lowpan: Disable notify_peers before netdev deletion
In reply to Cen ZhangThe last-peer close path cancels notify_peers and queues adapter deletion. Before deletion removes the adapter from the list, another ready callback can find it and queue the embedded delayed work again:
Last-peer close Later ready / deletion work
cancel notify_peers
queue delete_netdev
add_peer_chan()
requeue notify_peers
unregister and free the adapter notification timer uses freed work
Disable the delayed work synchronously instead of only cancelling it. This both drains an existing callback and rejects later queue attempts for the adapter which is already being deleted.
The excerpt below retains the headline and stack traces.
Use disable_delayed_work_sync() before teardown, as commit d236517c264e4 ("Bluetooth: coredump: Quiesce dump work on unregister") does before freeing the Bluetooth coredump state.
KASAN report as below:
BUG: KASAN: slab-use-after-free in do_notify_peers+0x63/0x70
Read of size 8 at addr ffff888117711030 by task kworker/3:1/50
Workqueue: events do_notify_peers
Call Trace:
<TASK>
dump_stack_lvl+0x93/0xd0
print_report+0xce/0x630
? do_notify_peers+0x63/0x70
? srso_alias_return_thunk+0x5/0xfbef5
? __virt_addr_valid+0x20d/0x410
? do_notify_peers+0x63/0x70
kasan_report+0xe0/0x110
? do_notify_peers+0x63/0x70
do_notify_peers+0x63/0x70
process_one_work+0x908/0x19c0
? __pfx_process_one_work+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
worker_thread+0x65c/0xe40
? srso_alias_return_thunk+0x5/0xfbef5
? __kthread_parkme+0x175/0x220
? srso_alias_return_thunk+0x5/0xfbef5
? __pfx_worker_thread+0x10/0x10
kthread+0x34f/0x460
? srso_alias_return_thunk+0x5/0xfbef5
? __pfx_kthread+0x10/0x10
ret_from_fork+0x659/0x940
? __pfx_ret_from_fork+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? __switch_to+0x74f/0xf80
? __pfx_kthread+0x10/0x10
ret_from_fork_asm+0x1a/0x30
</TASK>
Allocated by task 500:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
__kasan_kmalloc+0xaa/0xb0
__kvmalloc_node_noprof+0x353/0x930
alloc_netdev_mqs+0xa0/0x1250
chan_ready_cb+0x4ec/0xeb0
l2cap_le_connect_req+0xccd/0x11a0
l2cap_recv_frame+0x467f/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
Freed by task 11:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
kasan_save_free_info+0x3b/0x60
__kasan_slab_free+0x5f/0x80
kfree+0x307/0x580
device_release+0xd7/0x280
kobject_put+0x1ad/0x610
netdev_run_todo+0x91c/0x13a0
unregister_dev+0x82/0x280
process_one_work+0x908/0x19c0
worker_thread+0x65c/0xe40
kthread+0x34f/0x460
ret_from_fork+0x659/0x940
ret_from_fork_asm+0x1a/0x30---
This patch was written with assistance from Codex (OpenAI).
net/bluetooth/6lowpan.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/bluetooth/6lowpan.c +2 -1
@@ -824,7 +824,8 @@ static void chan_close_cb(struct l2cap_chan *chan)if (!err && last && dev && !atomic_read (&dev->peer_count)) {spin_unlock (&devices_lock); -cancel_delayed_work_sync (&dev->notify_peers); +/* Prevent a later ready callback from requeueing work. */ +disable_delayed_work_sync (&dev->notify_peers);ifdown (dev->netdev);