Skip to content

dwc_otg: Probable use-after-free in handle_hc_intr resulting in Oops or SLUB errors #217

Closed

Description

@P33M

My Model A Pi oopses in a reproducible way after a while under the following conditions:

  1. Core SOF interrupts are disabled (due to a single device plugged in with no periodic endpoints)
  2. Occasional USB access - with either control or bulk messages.

This is with latest default kernel.

The problem is most reproducible with my RT5370 wifi dongle whose rt2800_usb driver will periodically do radio link maintenance as dictated by its link quality management (setting gain/speed etc). The problem also occurs at infrequent intervals during a wifi data transfer.

If the device is plugged in via a hub (including the smsc one on a Pi model B) the system no longer Oopses.

First OOPS:

<1>[23717.211305] Unable to handle kernel NULL pointer dereference at virtual address 00000000
<1>[23717.224500] pgd = c0004000
<1>[23717.229450] [00000000] *pgd=00000000
<0>[23717.236017] Internal error: Oops: 17 [#1] PREEMPT ARM
kdb> btc
btc: cpu status: Currently on cpu 0
Available cpus: 0
Stack traceback for pid 2376
0xca996200     2376        2  1    0   R  0xca9964e0 *kworker/u:2
[<c0013a7c>] (unwind_backtrace+0x0/0xf0) from [<c0010f64>] (show_stack+0x10/0x14)
[<c0010f64>] (show_stack+0x10/0x14) from [<c0076700>] (kdb_show_stack+0x3c/0x58)
[<c0076700>] (kdb_show_stack+0x3c/0x58) from [<c00767a4>] (kdb_bt1.isra.0+0x88/0xd0)
[<c00767a4>] (kdb_bt1.isra.0+0x88/0xd0) from [<c0076960>] (kdb_bt+0x174/0x360)
[<c0076960>] (kdb_bt+0x174/0x360) from [<c0074334>] (kdb_parse+0x2b4/0x630)
[<c0074334>] (kdb_parse+0x2b4/0x630) from [<c00769dc>] (kdb_bt+0x1f0/0x360)
[<c00769dc>] (kdb_bt+0x1f0/0x360) from [<c0074334>] (kdb_parse+0x2b4/0x630)
[<c0074334>] (kdb_parse+0x2b4/0x630) from [<c0074f4c>] (kdb_main_loop+0x4d8/0x6fc)
[<c0074f4c>] (kdb_main_loop+0x4d8/0x6fc) from [<c0077500>] (kdb_stub+0x154/0x380)
[<c0077500>] (kdb_stub+0x154/0x380) from [<c006e634>] (kgdb_handle_exception+0x1f8/0x668)
[<c006e634>] (kgdb_handle_exception+0x1f8/0x668) from [<c00131fc>] (kgdb_notify+0x24/0x40)
[<c00131fc>] (kgdb_notify+0x24/0x40) from [<c039b4d4>] (notifier_call_chain+0x44/0x84)
[<c039b4d4>] (notifier_call_chain+0x44/0x84) from [<c039b54c>] (__atomic_notifier_call_chain+0x38/0x4c)
[<c039b54c>] (__atomic_notifier_call_chain+0x38/0x4c) from [<c039b578>] (atomic_notifier_call_chain+0x18/0x20)
[<c039b578>] (atomic_notifier_call_chain+0x18/0x20) from [<c039b5b8>] (notify_die+0x38/0x44)
[<c039b5b8>] (notify_die+0x38/0x44) from [<c001102c>] (die+0xc4/0x39c)
[<c001102c>] (die+0xc4/0x39c) from [<c039430c>] (__do_kernel_fault.part.9+0x54/0x74)
[<c039430c>] (__do_kernel_fault.part.9+0x54/0x74) from [<c039b1e4>] (do_page_fault+0x1e8/0x3ec)
[<c039b1e4>] (do_page_fault+0x1e8/0x3ec) from [<c000832c>] (do_DataAbort+0x34/0x98)
[<c000832c>] (do_DataAbort+0x34/0x98) from [<c03999d8>] (__dabt_svc+0x38/0x60)
Exception stack(0xca99bc88 to 0xca99bcd0)
bc80:                   f3000806 a0000013 00000000 00000000 ca964980 00000000
bca0: ca982850 caa565a0 ca964a80 ca982838 00000000 ca9837c0 00000000 ca99bcd0
bcc0: c029824c c0298260 60000013 ffffffff
[<c03999d8>] (__dabt_svc+0x38/0x60) from [<c0298260>] (dwc_otg_hcd_urb_enqueue+0x70/0x184)
[<c0298260>] (dwc_otg_hcd_urb_enqueue+0x70/0x184) from [<c0298e7c>] (dwc_otg_urb_enqueue+0x21c/0x2f0)
[<c0298e7c>] (dwc_otg_urb_enqueue+0x21c/0x2f0) from [<c0273344>] (usb_hcd_submit_urb+0xbc/0x77c)
[<c0273344>] (usb_hcd_submit_urb+0xbc/0x77c) from [<c0275828>] (usb_start_wait_urb+0x44/0xbc)
[<c0275828>] (usb_start_wait_urb+0x44/0xbc) from [<c0275a54>] (usb_control_msg+0xb4/0xe8)
[<c0275a54>] (usb_control_msg+0xb4/0xe8) from [<bf0bce48>] (rt2x00usb_vendor_request+0x98/0x128 [rt2x00usb])
[<bf0bce48>] (rt2x00usb_vendor_request+0x98/0x128 [rt2x00usb]) from [<bf0bcf6c>] (rt2x00usb_vendor_req_buff_lock+0x60/0x108 [rt2x00usb])
[<bf0bcf6c>] (rt2x00usb_vendor_req_buff_lock+0x60/0x108 [rt2x00usb]) from [<bf0bd13c>] (rt2x00usb_regbusy_read+0x78/0x104 [rt2x00usb])
[<bf0bd13c>] (rt2x00usb_regbusy_read+0x78/0x104 [rt2x00usb]) from [<bf0c4d98>] (rt2800_bbp_read+0x58/0xd4 [rt2800lib])
[<bf0c4d98>] (rt2800_bbp_read+0x58/0xd4 [rt2800lib]) from [<bf0c8234>] (rt2800_config_txpower+0x1ac/0x460 [rt2800lib])
[<bf0c8234>] (rt2800_config_txpower+0x1ac/0x460 [rt2800lib]) from [<bf0ae718>] (rt2x00link_agc+0x28/0x54 [rt2x00lib])
[<bf0ae718>] (rt2x00link_agc+0x28/0x54 [rt2x00lib]) from [<c0035024>] (process_one_work+0x150/0x40c)
[<c0035024>] (process_one_work+0x150/0x40c) from [<c0035604>] (worker_thread+0x150/0x498)
[<c0035604>] (worker_thread+0x150/0x498) from [<c003a7c8>] (kthread+0x88/0x94)
[<c003a7c8>] (kthread+0x88/0x94) from [<c000e9fc>] (kernel_thread_exit+0x0/0x8)
kdb>

I recompiled the kernel with debug info, reproduced the OOPS and traced the exception to line 507 in dwc_otg_hcd.c -

if ((qtd->qh->ep_type == UE_BULK)

For some reason qh (or qtd) ends up null.

This line is only called if SOF interrupts are disabled and the qtd allocation was successful, i.e. if there are no periodic endpoints enabled.

As usual I have reconfigured the kernel to do SLUB debugging with poison/redzone/user checking enabled along with more general debug options. SLUB is reporting a possible use-after-free (which reliably happens on boot):

[  335.227016] =============================================================================
[  335.227053] BUG kmalloc-64 (Not tainted): Poison overwritten
[  335.227063] -----------------------------------------------------------------------------
[  335.227063]
[  335.227082] INFO: 0xc96d8610-0xc96d8610. First byte 0x0 instead of 0x6b
[  335.227131] INFO: Allocated in dwc_otg_hcd_qtd_create+0x1c/0x50 age=184 cpu=0 pid=2480
[  335.227160]  __slab_alloc.isra.46.constprop.48+0x454/0x4b4
[  335.227188]  __kmalloc+0x64/0x100
[  335.227206]  dwc_otg_hcd_qtd_create+0x1c/0x50
[  335.227220]  dwc_otg_hcd_urb_enqueue+0x58/0x18c
[  335.227233]  dwc_otg_urb_enqueue+0x24c/0x2e8
[  335.227264]  usb_hcd_submit_urb+0x69c/0x7d8
[  335.227302]  rt2x00usb_kick_rx_entry+0xa0/0xf4 [rt2x00usb]
[  335.227378]  rt2x00queue_init_queues+0x4c/0x80 [rt2x00lib]
[  335.227413]  rt2x00lib_enable_radio+0x1c/0xc0 [rt2x00lib]
[  335.227442]  rt2x00lib_start+0x98/0xc0 [rt2x00lib]
[  335.227698]  ieee80211_do_open+0x118/0x7d0 [mac80211]
[  335.227718]  __dev_open+0x9c/0x104
[  335.227732]  __dev_change_flags+0xa4/0x130
[  335.227745]  dev_change_flags+0x10/0x44
[  335.227760]  devinet_ioctl+0x304/0x75c
[  335.227784]  sock_ioctl+0x22c/0x284
[  335.227809] INFO: Freed in release_channel+0x124/0x1ec age=59 cpu=0 pid=0
[  335.227828]  __slab_free+0x2c/0x33c
[  335.227843]  release_channel+0x124/0x1ec
[  335.227858]  handle_hc_xfercomp_intr+0x2e0/0x3b4
[  335.227872]  dwc_otg_hcd_handle_hc_n_intr+0x1c4/0x620
[  335.227886]  dwc_otg_hcd_handle_hc_intr+0x54/0x78
[  335.227900]  dwc_otg_hcd_handle_intr+0x100/0x1f4
[  335.227912]  dwc_otg_hcd_irq+0xc/0x18
[  335.227932]  usb_hcd_irq+0x30/0x40
[  335.227958]  handle_irq_event_percpu+0x34/0x190
[  335.227973]  handle_irq_event+0x60/0x80
[  335.227992]  handle_level_irq+0xd4/0x13c
[  335.228006]  generic_handle_irq+0x30/0x48
[  335.228028]  handle_IRQ+0x60/0x80
[  335.228051]  __irq_svc+0x34/0xc8
[  335.228067]  default_idle+0x24/0x2c
[  335.228080]  cpu_idle+0x68/0xb4
[  335.228094] INFO: Slab 0xc0712b00 objects=16 used=16 fp=0x  (null) flags=0x0080
[  335.228106] INFO: Object 0xc96d8600 @offset=1536 fp=0xc96d8400
[  335.228106]
[  335.228127] Bytes b4 c96d85f0: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a  ZZZZZZZZZZZZZZZZ
[  335.228142] Object c96d8600: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b  kkkkkkkkkkkkkkkk
[  335.228154] Object c96d8610: 00 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b  .kkkkkkkkkkkkkkk
[  335.228167] Object c96d8620: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b  kkkkkkkkkkkkkkkk
[  335.228180] Object c96d8630: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b a5  kkkkkkkkkkkkkkk.
[  335.228192] Redzone c96d8640: bb bb bb bb                                      ....
[  335.228205] Padding c96d86e8: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a  ZZZZZZZZZZZZZZZZ
[  335.228217] Padding c96d86f8: 5a 5a 5a 5a 5a 5a 5a 5a                          ZZZZZZZZ
[  335.228262] [<c00138c0>] (unwind_backtrace+0x0/0xf0) from [<c00bbba4>] (check_bytes_and_report+0xb4/0xdc)
[  335.228292] [<c00bbba4>] (check_bytes_and_report+0xb4/0xdc) from [<c00bbc98>] (check_object+0xcc/0x214)
[  335.228321] [<c00bbc98>] (check_object+0xcc/0x214) from [<c0393048>] (alloc_debug_processing+0x88/0x134)
[  335.228351] [<c0393048>] (alloc_debug_processing+0x88/0x134) from [<c0393b4c>] (__slab_alloc.isra.46.constprop.48+0x454/0x4b4)
[  335.228381] [<c0393b4c>] (__slab_alloc.isra.46.constprop.48+0x454/0x4b4) from [<c00bd5b0>] (__kmalloc+0x64/0x100)
[  335.228415] [<c00bd5b0>] (__kmalloc+0x64/0x100) from [<c029b798>] (dwc_otg_hcd_qtd_create+0x30/0x50)
[  335.228443] [<c029b798>] (dwc_otg_hcd_qtd_create+0x30/0x50) from [<c02973cc>] (dwc_otg_hcd_urb_enqueue+0x58/0x18c)
[  335.228468] [<c02973cc>] (dwc_otg_hcd_urb_enqueue+0x58/0x18c) from [<c0298014>] (dwc_otg_urb_enqueue+0x24c/0x2e8)
[  335.228501] [<c0298014>] (dwc_otg_urb_enqueue+0x24c/0x2e8) from [<c0271c84>] (usb_hcd_submit_urb+0x69c/0x7d8)
[  335.228534] [<c0271c84>] (usb_hcd_submit_urb+0x69c/0x7d8) from [<c0273b7c>] (usb_start_wait_urb+0x44/0xb4)
[  335.228557] [<c0273b7c>] (usb_start_wait_urb+0x44/0xb4) from [<c0273db0>] (usb_control_msg+0xc8/0xf4)
[  335.228599] [<c0273db0>] (usb_control_msg+0xc8/0xf4) from [<bf145e58>] (rt2x00usb_vendor_request+0x8c/0x11c [rt2x00usb])
[  335.228647] [<bf145e58>] (rt2x00usb_vendor_request+0x8c/0x11c [rt2x00usb]) from [<bf145fec>] (rt2x00usb_vendor_req_buff_lock+0xcc/0x100 [rt2x00usb])
[  335.228690] [<bf145fec>] (rt2x00usb_vendor_req_buff_lock+0xcc/0x100 [rt2x00usb]) from [<bf146180>] (rt2x00usb_vendor_request_buff+0x64/0xa8 [rt2x00usb])
[  335.228738] [<bf146180>] (rt2x00usb_vendor_request_buff+0x64/0xa8 [rt2x00usb]) from [<bf164030>] (rt2x00usb_register_read+0x30/0x40 [rt2800usb])
[  335.228812] [<bf164030>] (rt2x00usb_register_read+0x30/0x40 [rt2800usb]) from [<bf151274>] (rt2800_config_intf+0xd0/0x21c [rt2800lib])
[  335.228902] [<bf151274>] (rt2800_config_intf+0xd0/0x21c [rt2800lib]) from [<bf1354a4>] (rt2x00lib_config_intf+0x120/0x128 [rt2x00lib])
[  335.228970] [<bf1354a4>] (rt2x00lib_config_intf+0x120/0x128 [rt2x00lib]) from [<bf134a84>] (rt2x00mac_remove_interface+0x84/0x8c [rt2x00lib])
[  335.229196] [<bf134a84>] (rt2x00mac_remove_interface+0x84/0x8c [rt2x00lib]) from [<bf05cc00>] (ieee80211_do_stop+0x3ac/0x5e0 [mac80211])
[  335.229473] [<bf05cc00>] (ieee80211_do_stop+0x3ac/0x5e0 [mac80211]) from [<bf05ce44>] (ieee80211_stop+0x10/0x18 [mac80211])
[  335.229635] [<bf05ce44>] (ieee80211_stop+0x10/0x18 [mac80211]) from [<c02ef784>] (__dev_close_many+0x9c/0xcc)
[  335.229662] [<c02ef784>] (__dev_close_many+0x9c/0xcc) from [<c02ef7dc>] (__dev_close+0x28/0x3c)
[  335.229689] [<c02ef7dc>] (__dev_close+0x28/0x3c) from [<c02f231c>] (__dev_change_flags+0xa4/0x130)
[  335.229715] [<c02f231c>] (__dev_change_flags+0xa4/0x130) from [<c02f2414>] (dev_change_flags+0x10/0x44)
[  335.229741] [<c02f2414>] (dev_change_flags+0x10/0x44) from [<c0346a48>] (devinet_ioctl+0x304/0x75c)
[  335.229774] [<c0346a48>] (devinet_ioctl+0x304/0x75c) from [<c02dd12c>] (sock_ioctl+0x22c/0x284)
[  335.229807] [<c02dd12c>] (sock_ioctl+0x22c/0x284) from [<c00d31a0>] (do_vfs_ioctl+0x578/0x600)
[  335.229833] [<c00d31a0>] (do_vfs_ioctl+0x578/0x600) from [<c00d3260>] (sys_ioctl+0x38/0x5c)
[  335.229862] [<c00d3260>] (sys_ioctl+0x38/0x5c) from [<c000d8a0>] (ret_fast_syscall+0x0/0x30)
[  335.229881] FIX kmalloc-64: Restoring 0xc96d8610-0xc96d8610=0x6b
[  335.229881]
[  335.229897] FIX kmalloc-64: Marking all objects used

I'm somewhat stuck at this point as I've drudged through the transfer complete interrupt handling with no avail.

I got a similar oops when I unplugged a zd1211b device; possibly related:

[   35.172381] Unable to handle kernel paging request at virtual address 6b6b6b9f
[   35.181055] pgd = c0004000
[   35.185035] [6b6b6b9f] *pgd=00000000
[   35.189902] Internal error: Oops: 5 [#1] PREEMPT ARM

Entering kdb (current=0xc05131c8, pid 0) Oops: (null)
due to oops @ 0xc0299bc8

Pid: 0, comm:              swapper
CPU: 0    Not tainted  (3.6.11+ #3)
PC is at handle_hc_nak_intr+0x14/0x14c
LR is at dwc_otg_hcd_handle_hc_n_intr+0x4b4/0x620
pc : [<c0299bc8>]    lr : [<c029a47c>]    psr: 20000193
sp : c0509e48  ip : 00000000  fp : f29805ec
r10: 00000012  r9 : c8946300  r8 : f29805e0
r7 : cabc0380  r6 : cabc0380  r5 : 00000001  r4 : c8946300
r3 : 6b6b6b6b  r2 : f29805e0  r1 : cabdfc60  r0 : cabc0380
Flags: nzCv  IRQs off  FIQs on  Mode SVC_32  ISA ARM  Segment kernel
Control: 00c5387d  Table: 0970c008  DAC: 00000017
[<c00138c0>] (unwind_backtrace+0x0/0xf0) from [<c007223c>] (kdb_dumpregs+0x28/0x50)
[<c007223c>] (kdb_dumpregs+0x28/0x50) from [<c0074424>] (kdb_main_loop+0x250/0x750)
[<c0074424>] (kdb_main_loop+0x250/0x750) from [<c0076d60>] (kdb_stub+0x278/0x3d8)
[<c0076d60>] (kdb_stub+0x278/0x3d8) from [<c006e120>] (kgdb_handle_exception+0x41c/0x644)
[<c006e120>] (kgdb_handle_exception+0x41c/0x644) from [<c0013028>] (kgdb_notify+0x28/0x44)
[<c0013028>] (kgdb_notify+0x28/0x44) from [<c03999ec>] (notifier_call_chain+0x44/0x84)
[<c03999ec>] (notifier_call_chain+0x44/0x84) from [<c0399a64>] (__atomic_notifier_call_chain+0x38/0x4c)
[<c0399a64>] (__atomic_notifier_call_chain+0x38/0x4c) from [<c0399a90>] (atomic_notifier_call_chain+0x18/0x20)
[<c0399a90>] (atomic_notifier_call_chain+0x18/0x20) from [<c0399ad0>] (notify_die+0x38/0x44)
[<c0399ad0>] (notify_die+0x38/0x44) from [<c0010e88>] (die+0xe0/0x3ac)
[<c0010e88>] (die+0xe0/0x3ac) from [<c0016d04>] (__do_kernel_fault+0x64/0x84)
[<c0016d04>] (__do_kernel_fault+0x64/0x84) from [<c03998d4>] (do_page_fault+0x37c/0x3ac)
[<c03998d4>] (do_page_fault+0x37c/0x3ac) from [<c0008310>] (do_DataAbort+0x34/0x98)
[<c0008310>] (do_DataAbort+0x34/0x98) from [<c0397ef8>] (__dabt_svc+0x38/0x60)
Exception stack(0xc0509e00 to 0xc0509e48)
9e00: cabc0380 cabdfc60 f29805e0 6b6b6b6b c8946300 00000001 cabc0380 cabc0380
9e20: f29805e0 c8946300 00000012 f29805ec 00000000 c0509e48 c029a47c c0299bc8
9e40: 20000193 ffffffff
[<c0397ef8>] (__dabt_svc+0x38/0x60) from [<c0299bc8>] (handle_hc_nak_intr+0x14/0x14c)
[<c0299bc8>] (handle_hc_nak_intr+0x14/0x14c) from [<c029a47c>] (dwc_otg_hcd_handle_hc_n_intr+0x4b4/0x620)
[<c029a47c>] (dwc_otg_hcd_handle_hc_n_intr+0x4b4/0x620) from [<c029a63c>] (dwc_otg_hcd_handle_hc_intr+0x54/0x78)
[<c029a63c>] (dwc_otg_hcd_handle_hc_intr+0x54/0x78) from [<c029a760>] (dwc_otg_hcd_handle_intr+0x100/0x1f4)
[<c029a760>] (dwc_otg_hcd_handle_intr+0x100/0x1f4) from [<c02980c4>] (dwc_otg_hcd_irq+0xc/0x18)
[<c02980c4>] (dwc_otg_hcd_irq+0xc/0x18) from [<c026fd6c>] (usb_hcd_irq+0x30/0x40)
[<c026fd6c>] (usb_hcd_irq+0x30/0x40) from [<c0077c60>] (handle_irq_event_percpu+0x34/0x190)
[<c0077c60>] (handle_irq_event_percpu+0x34/0x190) from [<c0077e1c>] (handle_irq_event+0x60/0x80)
[<c0077e1c>] (handle_irq_event+0x60/0x80) from [<c0079ee0>] (handle_level_irq+0xd4/0x13c)
[<c0079ee0>] (handle_level_irq+0xd4/0x13c) from [<c0077594>] (generic_handle_irq+0x30/0x48)
[<c0077594>] (generic_handle_irq+0x30/0x48) from [<c000e78c>] (handle_IRQ+0x60/0x80)
[<c000e78c>] (handle_IRQ+0x60/0x80) from [<c0397f54>] (__irq_svc+0x34/0xc8)
[<c0397f54>] (__irq_svc+0x34/0xc8) from [<c000e898>] (default_idle+0x24/0x2c)
[<c000e898>] (default_idle+0x24/0x2c) from [<c000ea24>] (cpu_idle+0x68/0xb4)
[<c000ea24>] (cpu_idle+0x68/0xb4) from [<c04e6730>] (start_kernel+0x290/0x2d8)
more>
kdb>

Activity

  1. P33M commented on Feb 10, 2013

    @P33M
    ContributorAuthor

    The OOPS is almost certainly a continuation of #190 - this case being brought out into the open on the model A because of the lack of SOF interrupts. The QTD is being freed immediately after interrupts are re-enabled in dwc_otg_qtd_add() which results in either null pointer dereference without SLUB debugging turned on, or a dereference of 0x6b6b6b6b with SLUB debugging enabled.

    There is still a use-after-free going on as evidenced by SLUB detecting corruption. Either the qtd or qh structs are being tampered with after freeing - offset 0x10 in each case is:
    qh->channel (uint8_t)
    qtd->error_count (uint8_t)

    I also note the offset 0x3F also has an overwritten byte to A5 - no idea what this is.

    Edit: I patched the offending function into a more sane check carried out prior to firing the QTD off into the driver. I will leave the Pi online overnight and see if it falls over.

    gist: https://gist.github.com/P33M/b85973e60405bdb1db0d

  2. popcornmix commented on Feb 11, 2013

    @popcornmix
    Collaborator

    Interesting. I hope you find a fix.

  3. P33M commented on Feb 12, 2013

    @P33M
    ContributorAuthor

    This is what I've gotten so far:

    • Fix incorrect error handling in dwc_otg_hcd_alloc_urb leading to OOPS
    • Fix unsafe access of qtd in dwc_otg_hcd_urb_enqueue leading to potential OOPS
    • Hack: clobber pointers after freeing QTDs and QHs to hide obscure memory corruption bug

    This completely solves the first OOPS experienced. It doesn't remove the SLUB poison overwritten though. The hack is just there to narrow the causes down (i.e. eliminate double free)

    One clue is that kill_urbs_in_qh will predictably OOPS due to dereferencing freed memory in the non_periodic_sched_active qh list. This is replicated by pulling the USB stick out of the port at just the right time.

    gist: https://gist.github.com/P33M/1629b113edcf101aa3f6

  4. P33M commented on Feb 14, 2013

    @P33M
    ContributorAuthor

    I've found the cause of the memory corruption (I think).

    In dwc_otg_hcd_handle_hc_n_intr, the various halt conditions of a host channel are processed. If a NYET occurs on an non-periodic OUT, the channel is halted with xfercomp and nyet bits set - the driver commentary says:

     * Handles a host channel NYET interrupt. This interrupt should only occur on
     * Bulk and Control OUT endpoints and for complete split transactions. If a
     * NYET occurs at the same time as a Transfer Complete interrupt, it is
     * handled in the xfercomp interrupt handler, not here. This handler may be
     * called in either DMA mode or Slave mode.
     */
    

    but the NYET interrupt isn't cleared by xfercomp. I think this results in the IRQ being re-entrant but with xfercomp cleared and nyet active - which leads to accessing a qtd that doesn't exist because xfercomp removed it. I need to check to see that this is actually happening.

    This correlates with the stacktrace associated with the corruption - in most cases it's a control URB that results in the use-after-free.

  5. popcornmix commented on Feb 15, 2013

    @popcornmix
    Collaborator

    @P33M
    Sounds promising.

  6. P33M commented on Feb 21, 2013

    @P33M
    ContributorAuthor

    Found the incorrect QTD access.

    In handle_hc_n_intr the execution flow hands off to each interrupt type handler in turn with xfercomp at the top of the queue. The incorrect access happens around line 1589 in dwc_otg_hcd_intr.c (exact lines incorrect as I have added debug statements).

    This is presumably because the ACK interrupt fires after Xfercomp as Xfercomp can take up to 500us to complete. This behaviour I have only seen on CONTROL type endpoints where the device spends time processing data - e.g. in my case it is when my RTL5370 chipset finishes loading firmware. In this case I doubt the use-after-free will result in memory corruption as we are in and IRQ and there are no further allocations before handle_ack_intr, however if the ACK interrupt fires after the handler completes...

    Issue #205 may be related. If the xfercomp completes and the ACK interrupt then fires with the hc not expecting it, you will get this result. In this case it is certain that kernel memory is corrupt because the QTD circleq will be empty and no check is made before accessing the QTD.

  7. P33M commented on Feb 21, 2013

    @P33M
    ContributorAuthor

    Note that #217 (comment) is hogwash - NYET is handled correctly as far as I can tell.

    As for a bugfix - there seems to be a few of these arbitrary qtd->error_count = 0 statements scattered throughout the code. In general the entire file is an exercise in how not to write an interrupt handler due to the extremely scattered and convoluted call graph, overall SLOC, calls to freeing memory etc, etc. Therefore just bludgeoning in for a fix for laggy hardware would be fraught with danger.

    I will be back from my current field job at the weekend and will have more time to think on a solution.

    Suggestions, anyone?

  8. ghollingworth commented on Feb 21, 2013

    @ghollingworth

    I've found another terrible bit of code that also means that it's possible to end up with a null qtr pointer when you go to schedule a transaction… Turns out the driver does the following:

    add_qtd(qh, qtd)
    {
    if(schedule_periodic(qh))
    {
    // Interrupt occurs here and tries to access qh->qtr before we've set it!
    qh->qtd = qtd;
    }
    }

    Unfortunately schedule periodic also enables the start of frame interrupt which then means the SOF interrupt occurs and tries to actually access the qh->qtd structure before we've filled it in!

    I think the changes I've got so far for my FIQ implementation means that now I'm just doing it all a lot quicker which in turn means I'm more likely to hit the problem!

    More hacking ensues….

    Gordon

    Gordon Hollingworth, Director of Engineering

    On 21 Feb 2013, at 11:39, P33M notifications@github.com wrote:

    Note that #217 is hogwash - NYET is handled correctly as far as I can tell.

    As for a bugfix - there seems to be a few of these arbitrary qtd->error_count = 0 statements scattered throughout the code. In general the entire file is an exercise in how not to write an interrupt handler due to the extremely scattered and convoluted call graph, overall SLOC, calls to freeing memory etc, etc. Therefore just bludgeoning in for a fix for laggy hardware would be fraught with danger.

    I will be back from my current field job at the weekend and will have more time to think on a solution.

    Suggestions, anyone?

    —
    Reply to this email directly or view it on GitHub.

  9. P33M commented on Feb 21, 2013

    @P33M
    ContributorAuthor

    OK... I'm getting these with IN endpoints (both BULK and CONTROL) on my RTL5370 - why on earth does the hardware generate an interrupt on an IN endpoint after it's already interrupted that the transaction is complete?

  10. P33M commented on Feb 23, 2013

    @P33M
    ContributorAuthor

    Eurgh. I've uncovered yet more bugs while attempting to pin down this one.

    Those issues aside, I've determined the following:

    • dwc_otg: Probable use-after-free in handle_hc_intr resulting in Oops or SLUB errors #217 (comment) is bollocks
    • the incorrect QTD access happens if and only if the ack and chhltd interrupts fire at the same time
    • chhltd is cleared by default in the driver unless it is the only interrupt to have fired. Presumably something to do with DMA mode.
    • in several of the cases I have seen, the qtd list is not empty when the incorrect access occurs. Just the first qtd in the circleq references freed memory.
    • The IRQ is not being re-entrant as the xfercomp handler takes the time to clear and disable all other pending interrupts.
    • The ack is not spurious - I added register writes to clear all pending interrupts, masked or not, on allocation of a hc to a channel (i.e. re-use a channel) and got the same result.

    What I am unable to determine is the phase in the transaction upon which this odd bug occurs. It's most definitely not after the transfer is completed and the hc is disabled.

  11. P33M commented on Feb 23, 2013

    @P33M
    ContributorAuthor

    It would be possible to elastoplast this one like so:

    diff --git a/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c b/drivers/usb/host/dwc_otg_hcd_intr.c
    index e8c91e7..b911995 100644
    --- a/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
    +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
    @@ -1571,7 +1571,8 @@ static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
                            halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
                    }
            } else {
    -               qtd->error_count = 0;
    +               if(!hc->ep_is_in || !dwc_qh_is_non_per(hc->qh))
    +                       qtd->error_count = 0;
    
                    if (hc->qh->ping_state) {
                            hc->qh->ping_state = 0;
    

    This at least solves the specific case of memory corruption picked up by SLUB in my case.

  12. P33M commented on Feb 25, 2013

    @P33M
    ContributorAuthor

    Apologies for the steadily decreasing signal to noise ratio of this thread but yet more info:

    This is hardware wierdness. After tracing the actual interrupt servicing it appears that the ACK interrupt is being raised after a transfer complete interrupt despite the hc's interrupts being cleared and masked!

    This only occurs on (non-split) transfers which have previously halted in an error state, in my case I get a reproducible xacterr interrupt with my RTL5370 on cold boot for 2 IN transactions. If a transaction has previously bombed out, when requeued the ACK interrupt is unmasked and in theory used to reset the error count. If the xfercomp is subsequently triggered on the last packet in a transaction the QTD ends up being freed by xfercomp and the resulting re-entry into the IRQ will result in a freed byte being overwritten to 0x00. If the interrupts were happening around the time where something else was allocating memory this could result in corruption.

    Basically, by the time we actually get to the ACK interrupt handler both the interrupt and mask reg do in fact read 0x0000000, but during the top-level re-entry it looks like the register write hasn't taken hold:

    [   29.385696] xacterr on hcnum:2 ep_type:Bulk direction:IN
    [   29.385732] hc_init: Nonzero QTD error count:1 EP type:Bulk Direction:IN assigned hcnum:4
    [   29.385746] hc_init: ACK enabled due to error state on hcnum:4
    [   29.385756] hc_init: DATATGLERR enabled due to error state on IN hcnum:4
    [   29.385764] hc_init: NAK enabled due to error state on non-intr hcnum:4
    [   29.466279] hc_n_intr: ack! hcnum:4 hcint:0x00000022 hcintmsk:0x00000426
    [   29.466416] handle_ack: ACK! freed qtd detected num:4 ep_type:Bulk direction:IN
    [   29.466435] handle_ack: ACK! hcint:0x00000000 hcintmsk:0x00000000
    

    The state of the mask at the hc_n_intr level is as expected for a error-prone bulk IN endpoint prior to the channel being disabled, but a scant few function calls later the hcint register is mysteriously 0x00000000, as it should be after an xfercomp has completed and disabled the hc.

  13. ghollingworth commented on Feb 25, 2013

    @ghollingworth

    This is interesting, I've seen something similar in the FIQ processing I'm doing now, where I actually run through the FIQ twice for each interrupt even though at the end I write a mask to clear the interrupt and read it back to check the value.

    Added memory barriers to no avail… I could do with provoking the problem in a small test application (i.e. an RTOS app like uboot) so that I can get the Broadcom guys to run a sim of the chip to see what's happening.

    My assumption is that this is likely to do with some asynchronous clocking which means that although the mask is removed the interrupt is still pending in some later clocked logic…

    Gordon

    Gordon Hollingworth, Director of Engineering

    On 25 Feb 2013, at 19:48, P33M notifications@github.com wrote:

    Apologies for the steadily decreasing signal to noise ratio of this thread but yet more info:

    This is hardware wierdness. After tracing the actual interrupt servicing it appears that the ACK interrupt is being raised after a transfer complete interrupt despite the hc's interrupts being cleared and masked!

    This only occurs on (non-split) transfers which have previously halted in an error state, in my case I get a reproducible xacterr interrupt with my RTL5370 on cold boot for 2 IN transactions. If a transaction has previously bombed out, when requeued the ACK interrupt is unmasked and in theory used to reset the error count. If the xfercomp is subsequently triggered on the last packet in a transaction the QTD ends up being freed by xfercomp and the resulting re-entry into the IRQ will result in a freed byte being overwritten to 0x00. If the interrupts were happening around the time where something else was allocating memory this could result in corruption.

    Basically, by the time we actually get to the ACK interrupt handler both the interrupt and mask reg do in fact read 0x0000000, but during the top-level re-entry it looks like the register write hasn't taken hold:

    [ 29.385696] xacterr on hcnum:2 ep_type:Bulk direction:IN
    [ 29.385732] hc_init: Nonzero QTD error count:1 EP type:Bulk Direction:IN assigned hcnum:4
    [ 29.385746] hc_init: ACK enabled due to error state on hcnum:4
    [ 29.385756] hc_init: DATATGLERR enabled due to error state on IN hcnum:4
    [ 29.385764] hc_init: NAK enabled due to error state on non-intr hcnum:4
    [ 29.466279] hc_n_intr: ack! hcnum:4 hcint:0x00000022 hcintmsk:0x00000426
    [ 29.466416] handle_ack: ACK! freed qtd detected num:4 ep_type:Bulk direction:IN
    [ 29.466435] handle_ack: ACK! hcint:0x00000000 hcintmsk:0x00000000
    The state of the mask at the hc_n_intr level is as expected for a error-prone bulk IN endpoint prior to the channel being disabled, but a scant few function calls later the hcint register is mysteriously 0x00000000, as it should be after an xfercomp has completed and disabled the hc.

    —
    Reply to this email directly or view it on GitHub.

  14. P33M commented on Feb 25, 2013

    @P33M
    ContributorAuthor

    There definitely is something screwy happening - if I busy-wait on reading the mask (or hcint) register after the write in hc_cleanup until the result is 0, the loop is never executed (and still spurious ACKs). Similarly if I induce a ridiculously big delay (133us) before the hc_cleanup function returns, I still get spurious ACKs.

  15. P33M commented on Feb 28, 2013

    @P33M
    ContributorAuthor

    Welp here's my fix:

    This fix relies on the fact that we only hit this incorrect function call because of an unmasked ACK bit being set - conditions in which it is enabled described above. If the channel is already halted then don't do anything, otherwise proceed as normal and reset the error count. ACKs during split transfers result in a call to handle_ack_intr elsewhere in the processing chain. I tried to do this in a way that's the least intrusive to the overall execution flow - messing with this mass of spaghetti will probably only make things worse.

    I have tested it on a variety of USB devices 1.1+2.0 both directly connected to a Model A and behind a hub.

    As an aside, the processing time for USB transfer completion is something I wish I hadn't looked at - using GPIO profiling to toggle GPIOs when in various interrupt handlers, xfercomp takes anywhere between 20 and 50uS to complete - that means the CPU is spinning inside an interrupt for up to 40% of the remaining microframe!

  16. 10 remaining items

  17. darbyShaw commented on Jul 3, 2017

    @darbyShaw
    Contributor

    Sorry for the delay. We cannot possibly use kernel-4.9.30 directly since we don't have many of our drivers ported to 4.9.30. We ported them to 4.1.8. Can I accommodate the newest dwc_otg driver to 4.1.8? Also I am looking for ways that help debug this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions