KASAN: use-after-free Read in __queue_work (3)

24 views
Skip to first unread message

syzbot

unread,
Aug 8, 2020, 5:27:16 PM8/8/20
to da...@davemloft.net, johan....@gmail.com, ku...@kernel.org, linux-b...@vger.kernel.org, linux-...@vger.kernel.org, mar...@holtmann.org, net...@vger.kernel.org, syzkall...@googlegroups.com
Hello,

syzbot found the following issue on:

HEAD commit: c0842fbc random32: move the pseudo-random 32-bit definitio..
git tree: upstream
console output: https://syzkaller.appspot.com/x/log.txt?x=127a8d66900000
kernel config: https://syzkaller.appspot.com/x/.config?x=cf567e8c7428377e
dashboard link: https://syzkaller.appspot.com/bug?extid=77e5e02c6c81136cdaff
compiler: gcc (GCC) 10.1.0-syz 20200507
syz repro: https://syzkaller.appspot.com/x/repro.syz?x=140e36a4900000

IMPORTANT: if you fix the issue, please add the following tag to the commit:
Reported-by: syzbot+77e5e0...@syzkaller.appspotmail.com

==================================================================
BUG: KASAN: use-after-free in __queue_work+0xc6c/0xf20 kernel/workqueue.c:1412
Read of size 4 at addr ffff88809f1ab9c0 by task syz-executor.3/16144

CPU: 0 PID: 16144 Comm: syz-executor.3 Not tainted 5.8.0-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x18f/0x20d lib/dump_stack.c:118
print_address_description.constprop.0.cold+0xae/0x436 mm/kasan/report.c:383
__kasan_report mm/kasan/report.c:513 [inline]
kasan_report.cold+0x1f/0x37 mm/kasan/report.c:530
__queue_work+0xc6c/0xf20 kernel/workqueue.c:1412
queue_work_on+0x18b/0x200 kernel/workqueue.c:1518
queue_work include/linux/workqueue.h:507 [inline]
req_run+0x2c5/0x4a0 net/bluetooth/hci_request.c:90
hci_req_run_skb net/bluetooth/hci_request.c:102 [inline]
__hci_req_sync+0x1dd/0x830 net/bluetooth/hci_request.c:215
hci_req_sync+0x8a/0xc0 net/bluetooth/hci_request.c:282
hci_dev_cmd+0x5b3/0x950 net/bluetooth/hci_core.c:2011
hci_sock_ioctl+0x3fa/0x800 net/bluetooth/hci_sock.c:1053
sock_do_ioctl+0xcb/0x2d0 net/socket.c:1048
sock_ioctl+0x3b8/0x730 net/socket.c:1199
vfs_ioctl fs/ioctl.c:48 [inline]
ksys_ioctl+0x11a/0x180 fs/ioctl.c:753
__do_sys_ioctl fs/ioctl.c:762 [inline]
__se_sys_ioctl fs/ioctl.c:760 [inline]
__x64_sys_ioctl+0x6f/0xb0 fs/ioctl.c:760
do_syscall_64+0x60/0xe0 arch/x86/entry/common.c:384
entry_SYSCALL_64_after_hwframe+0x44/0xa9
RIP: 0033:0x45cce9
Code: 2d b6 fb ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 0f 83 fb b5 fb ff c3 66 2e 0f 1f 84 00 00 00 00
RSP: 002b:00007f18d49bfc78 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 000000000001d300 RCX: 000000000045cce9
RDX: 0000000020000000 RSI: 00000000400448de RDI: 0000000000000004
RBP: 000000000078c080 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 000000000078c04c
R13: 00007ffc84a6ab1f R14: 00007f18d49c09c0 R15: 000000000078c04c

Allocated by task 9187:
save_stack+0x1b/0x40 mm/kasan/common.c:48
set_track mm/kasan/common.c:56 [inline]
__kasan_kmalloc.constprop.0+0xc2/0xd0 mm/kasan/common.c:494
__do_kmalloc mm/slab.c:3656 [inline]
__kmalloc+0x17a/0x340 mm/slab.c:3665
kmalloc include/linux/slab.h:560 [inline]
kzalloc include/linux/slab.h:669 [inline]
alloc_workqueue+0x166/0xe50 kernel/workqueue.c:4265
hci_register_dev+0x1b5/0x930 net/bluetooth/hci_core.c:3509
__vhci_create_device+0x2ac/0x5b0 drivers/bluetooth/hci_vhci.c:124
vhci_create_device drivers/bluetooth/hci_vhci.c:148 [inline]
vhci_open_timeout+0x38/0x50 drivers/bluetooth/hci_vhci.c:305
process_one_work+0x94c/0x1670 kernel/workqueue.c:2269
worker_thread+0x64c/0x1120 kernel/workqueue.c:2415
kthread+0x3b5/0x4a0 kernel/kthread.c:292
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:293

Freed by task 16170:
save_stack+0x1b/0x40 mm/kasan/common.c:48
set_track mm/kasan/common.c:56 [inline]
kasan_set_free_info mm/kasan/common.c:316 [inline]
__kasan_slab_free+0xf5/0x140 mm/kasan/common.c:455
__cache_free mm/slab.c:3426 [inline]
kfree+0x103/0x2c0 mm/slab.c:3757
rcu_do_batch kernel/rcu/tree.c:2427 [inline]
rcu_core+0x5c7/0x1190 kernel/rcu/tree.c:2655
__do_softirq+0x2de/0xa24 kernel/softirq.c:298

The buggy address belongs to the object at ffff88809f1ab800
which belongs to the cache kmalloc-1k of size 1024
The buggy address is located 448 bytes inside of
1024-byte region [ffff88809f1ab800, ffff88809f1abc00)
The buggy address belongs to the page:
page:ffffea00027c6ac0 refcount:1 mapcount:0 mapping:0000000000000000 index:0x0
flags: 0xfffe0000000200(slab)
raw: 00fffe0000000200 ffffea00028becc8 ffffea00028ddbc8 ffff8880aa000c40
raw: 0000000000000000 ffff88809f1ab000 0000000100000002 0000000000000000
page dumped because: kasan: bad access detected

Memory state around the buggy address:
ffff88809f1ab880: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
ffff88809f1ab900: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff88809f1ab980: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
^
ffff88809f1aba00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
ffff88809f1aba80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
==================================================================


---
This report is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzk...@googlegroups.com.

syzbot will keep track of this issue. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.
syzbot can test patches for this issue, for details see:
https://goo.gl/tpsmEJ#testing-patches

Hillf Danton

unread,
Aug 9, 2020, 12:46:00 AM8/9/20
to syzbot, da...@davemloft.net, johan....@gmail.com, ku...@kernel.org, linux-b...@vger.kernel.org, linux-...@vger.kernel.org, mar...@holtmann.org, net...@vger.kernel.org, syzkall...@googlegroups.com, Markus Elfring, Hillf Danton

Sat, 08 Aug 2020 14:27:15 -0700
Add memory barrier in the ioctl path to detect if hdev is on the
unregister way home, and aquire hdev's lock as well to serialize
the two pathes.


--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -72,6 +72,13 @@ static int req_run(struct hci_request *r
if (skb_queue_empty(&req->cmd_q))
return -ENODATA;

+ if (hdev->workqueue == NULL)
+ return -ENODEV;
+ /* balance with the mb in hci_unregister_dev() */
+ smp_mb();
+ if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
+ return -ENODEV;
+
skb = skb_peek_tail(&req->cmd_q);
if (complete) {
bt_cb(skb)->hci.req_complete = complete;
@@ -84,7 +91,11 @@ static int req_run(struct hci_request *r
skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);

- queue_work(hdev->workqueue, &hdev->cmd_work);
+ hci_dev_lock(hdev);
+ /* lose the race for taking hdev's lock but nothing hurts */
+ if (hdev->workqueue != NULL)
+ queue_work(hdev->workqueue, &hdev->cmd_work);
+ hci_dev_unlock(hdev);

return 0;
}
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3387,10 +3387,13 @@ EXPORT_SYMBOL(hci_register_dev);
void hci_unregister_dev(struct hci_dev *hdev)
{
int id;
+ struct workqueue_struct *wq, *req_wq;

BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);

hci_dev_set_flag(hdev, HCI_UNREGISTER);
+ /* make HCI_UNREGISTER visible before reclaiming resources */
+ smp_mb();

id = hdev->id;

@@ -3427,10 +3430,12 @@ void hci_unregister_dev(struct hci_dev *
kfree_const(hdev->hw_info);
kfree_const(hdev->fw_info);

- destroy_workqueue(hdev->workqueue);
- destroy_workqueue(hdev->req_workqueue);
+ wq = hdev->workqueue;
+ req_wq = hdev->req_workqueue;

hci_dev_lock(hdev);
+ hdev->workqueue = NULL;
+ hdev->req_workqueue = NULL;
hci_bdaddr_list_clear(&hdev->blacklist);
hci_bdaddr_list_clear(&hdev->whitelist);
hci_uuids_clear(hdev);
@@ -3445,6 +3450,8 @@ void hci_unregister_dev(struct hci_dev *
hci_discovery_filter_clear(hdev);
hci_dev_unlock(hdev);

+ destroy_workqueue(wq);
+ destroy_workqueue(req_wq);
hci_dev_put(hdev);

ida_simple_remove(&hci_index_ida, id);

syzbot

unread,
May 13, 2021, 12:27:05 PM5/13/21
to Markus....@web.de, anant.th...@gmail.com, da...@davemloft.net, gre...@linuxfoundation.org, hda...@sina.com, johan....@gmail.com, ku...@kernel.org, li...@zju.edu.cn, linux-b...@vger.kernel.org, linux-...@vger.kernel.org, luiz....@gmail.com, mar...@holtmann.org, net...@vger.kernel.org, syzkall...@googlegroups.com
syzbot suspects this issue was fixed by commit:

commit e2cb6b891ad2b8caa9131e3be70f45243df82a80
Author: Lin Ma <li...@zju.edu.cn>
Date: Mon Apr 12 11:17:57 2021 +0000

bluetooth: eliminate the potential race condition when removing the HCI controller

bisection log: https://syzkaller.appspot.com/x/bisect.txt?x=127b3593d00000
start commit: c0842fbc random32: move the pseudo-random 32-bit definitio..
git tree: upstream
syz repro: https://syzkaller.appspot.com/x/repro.syz?x=140e36a4900000

If the result looks correct, please mark the issue as fixed by replying with:

#syz fix: bluetooth: eliminate the potential race condition when removing the HCI controller

For information about bisection process see: https://goo.gl/tpsmEJ#bisection

Dmitry Vyukov

unread,
May 14, 2021, 3:50:51 AM5/14/21
to syzbot, Markus Elfring, Anant Thazhemadam, David Miller, Greg Kroah-Hartman, Hillf Danton, Johan Hedberg, Jakub Kicinski, li...@zju.edu.cn, linux-bluetooth, LKML, luiz....@gmail.com, Marcel Holtmann, net...@vger.kernel.org, syzkall...@googlegroups.com
Looks reasonable based on the commit and bisection log.
Unfortunately I cannot easily send this as my email client will wrap
the commit title line (longer than 80 chars)...
Reply all
Reply to author
Forward
0 new messages