Skip to content

Commit ba3ba53

Browse files
Vudentzgregkh
authored andcommitted
Bluetooth: hci_sync: Fix UAF in hci_disconnect_all_sync
[ Upstream commit 94d9ba9 ] Use-after-free can occur in hci_disconnect_all_sync if a connection is deleted by concurrent processing of a controller event. To prevent this the code now tries to iterate over the list backwards to ensure the links are cleanup before its parents, also it no longer relies on a cursor, instead it always uses the last element since hci_abort_conn_sync is guaranteed to call hci_conn_del. UAF crash log: ================================================================== BUG: KASAN: slab-use-after-free in hci_set_powered_sync (net/bluetooth/hci_sync.c:5424) [bluetooth] Read of size 8 at addr ffff888009d9c000 by task kworker/u9:0/124 CPU: 0 PID: 124 Comm: kworker/u9:0 Tainted: G W 6.5.0-rc1+ #10 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-1.fc38 04/01/2014 Workqueue: hci0 hci_cmd_sync_work [bluetooth] Call Trace: <TASK> dump_stack_lvl+0x5b/0x90 print_report+0xcf/0x670 ? __virt_addr_valid+0xdd/0x160 ? hci_set_powered_sync+0x2c9/0x4a0 [bluetooth] kasan_report+0xa6/0xe0 ? hci_set_powered_sync+0x2c9/0x4a0 [bluetooth] ? __pfx_set_powered_sync+0x10/0x10 [bluetooth] hci_set_powered_sync+0x2c9/0x4a0 [bluetooth] ? __pfx_hci_set_powered_sync+0x10/0x10 [bluetooth] ? __pfx_lock_release+0x10/0x10 ? __pfx_set_powered_sync+0x10/0x10 [bluetooth] hci_cmd_sync_work+0x137/0x220 [bluetooth] process_one_work+0x526/0x9d0 ? __pfx_process_one_work+0x10/0x10 ? __pfx_do_raw_spin_lock+0x10/0x10 ? mark_held_locks+0x1a/0x90 worker_thread+0x92/0x630 ? __pfx_worker_thread+0x10/0x10 kthread+0x196/0x1e0 ? __pfx_kthread+0x10/0x10 ret_from_fork+0x2c/0x50 </TASK> Allocated by task 1782: kasan_save_stack+0x33/0x60 kasan_set_track+0x25/0x30 __kasan_kmalloc+0x8f/0xa0 hci_conn_add+0xa5/0xa80 [bluetooth] hci_bind_cis+0x881/0x9b0 [bluetooth] iso_connect_cis+0x121/0x520 [bluetooth] iso_sock_connect+0x3f6/0x790 [bluetooth] __sys_connect+0x109/0x130 __x64_sys_connect+0x40/0x50 do_syscall_64+0x60/0x90 entry_SYSCALL_64_after_hwframe+0x6e/0xd8 Freed by task 695: kasan_save_stack+0x33/0x60 kasan_set_track+0x25/0x30 kasan_save_free_info+0x2b/0x50 __kasan_slab_free+0x10a/0x180 __kmem_cache_free+0x14d/0x2e0 device_release+0x5d/0xf0 kobject_put+0xdf/0x270 hci_disconn_complete_evt+0x274/0x3a0 [bluetooth] hci_event_packet+0x579/0x7e0 [bluetooth] hci_rx_work+0x287/0xaa0 [bluetooth] process_one_work+0x526/0x9d0 worker_thread+0x92/0x630 kthread+0x196/0x1e0 ret_from_fork+0x2c/0x50 ================================================================== Fixes: 182ee45 ("Bluetooth: hci_sync: Rework hci_suspend_notifier") Signed-off-by: Pauli Virtanen <pav@iki.fi> Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 80265dd commit ba3ba53

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

net/bluetooth/hci_sync.c

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5337,6 +5337,7 @@ int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
53375337
{
53385338
int err = 0;
53395339
u16 handle = conn->handle;
5340+
struct hci_conn *c;
53405341

53415342
switch (conn->state) {
53425343
case BT_CONNECTED:
@@ -5358,43 +5359,57 @@ int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
53585359
}
53595360
break;
53605361
default:
5362+
hci_dev_lock(hdev);
53615363
conn->state = BT_CLOSED;
5364+
hci_disconn_cfm(conn, reason);
5365+
hci_conn_del(conn);
5366+
hci_dev_unlock(hdev);
53625367
return 0;
53635368
}
53645369

5370+
hci_dev_lock(hdev);
5371+
5372+
/* Check if the connection hasn't been cleanup while waiting
5373+
* commands to complete.
5374+
*/
5375+
c = hci_conn_hash_lookup_handle(hdev, handle);
5376+
if (!c || c != conn) {
5377+
err = 0;
5378+
goto unlock;
5379+
}
5380+
53655381
/* Cleanup hci_conn object if it cannot be cancelled as it
53665382
* likelly means the controller and host stack are out of sync
53675383
* or in case of LE it was still scanning so it can be cleanup
53685384
* safely.
53695385
*/
5370-
if (err) {
5371-
struct hci_conn *c;
5372-
5373-
/* Check if the connection hasn't been cleanup while waiting
5374-
* commands to complete.
5375-
*/
5376-
c = hci_conn_hash_lookup_handle(hdev, handle);
5377-
if (!c || c != conn)
5378-
return 0;
5379-
5380-
hci_dev_lock(hdev);
5381-
hci_conn_failed(conn, err);
5382-
hci_dev_unlock(hdev);
5383-
}
5386+
hci_conn_failed(conn, reason);
53845387

5388+
unlock:
5389+
hci_dev_unlock(hdev);
53855390
return err;
53865391
}
53875392

53885393
static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
53895394
{
5390-
struct hci_conn *conn, *tmp;
5391-
int err;
5395+
struct list_head *head = &hdev->conn_hash.list;
5396+
struct hci_conn *conn;
53925397

5393-
list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) {
5394-
err = hci_abort_conn_sync(hdev, conn, reason);
5395-
if (err)
5396-
return err;
5398+
rcu_read_lock();
5399+
while ((conn = list_first_or_null_rcu(head, struct hci_conn, list))) {
5400+
/* Make sure the connection is not freed while unlocking */
5401+
conn = hci_conn_get(conn);
5402+
rcu_read_unlock();
5403+
/* Disregard possible errors since hci_conn_del shall have been
5404+
* called even in case of errors had occurred since it would
5405+
* then cause hci_conn_failed to be called which calls
5406+
* hci_conn_del internally.
5407+
*/
5408+
hci_abort_conn_sync(hdev, conn, reason);
5409+
hci_conn_put(conn);
5410+
rcu_read_lock();
53975411
}
5412+
rcu_read_unlock();
53985413

53995414
return 0;
54005415
}

0 commit comments

Comments
 (0)