082-0002-usb-core-usb_alloc_dev-fix-setting-of-portnum.patch 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. From 7222c832254a75dcd67d683df75753d4a4e125bb Mon Sep 17 00:00:00 2001
  2. From: Nicolai Stange <nicstange@gmail.com>
  3. Date: Thu, 17 Mar 2016 23:53:02 +0100
  4. Subject: [PATCH] usb/core: usb_alloc_dev(): fix setting of ->portnum
  5. With commit 69bec7259853 ("USB: core: let USB device know device node"),
  6. the port1 argument of usb_alloc_dev() gets overwritten as follows:
  7. ... usb_alloc_dev(..., unsigned port1)
  8. {
  9. ...
  10. if (!parent->parent) {
  11. port1 = usb_hcd_find_raw_port_number(..., port1);
  12. }
  13. ...
  14. }
  15. Later on, this now overwritten port1 gets assigned to ->portnum:
  16. dev->portnum = port1;
  17. However, since xhci_find_raw_port_number() isn't idempotent, the
  18. aforementioned commit causes a number of KASAN splats like the following:
  19. BUG: KASAN: slab-out-of-bounds in xhci_find_raw_port_number+0x98/0x170
  20. at addr ffff8801d9311670
  21. Read of size 8 by task kworker/2:1/87
  22. [...]
  23. Workqueue: usb_hub_wq hub_event
  24. 0000000000000188 000000005814b877 ffff8800cba17588 ffffffff8191447e
  25. 0000000041b58ab3 ffffffff82a03209 ffffffff819143a2 ffffffff82a252f4
  26. ffff8801d93115e0 0000000000000188 ffff8801d9311628 ffff8800cba17588
  27. Call Trace:
  28. [<ffffffff8191447e>] dump_stack+0xdc/0x15e
  29. [<ffffffff819143a2>] ? _atomic_dec_and_lock+0xa2/0xa2
  30. [<ffffffff814e2cd1>] ? print_section+0x61/0xb0
  31. [<ffffffff814e4939>] print_trailer+0x179/0x2c0
  32. [<ffffffff814f0d84>] object_err+0x34/0x40
  33. [<ffffffff814f4388>] kasan_report_error+0x2f8/0x8b0
  34. [<ffffffff814eb91e>] ? __slab_alloc+0x5e/0x90
  35. [<ffffffff812178c0>] ? __lock_is_held+0x90/0x130
  36. [<ffffffff814f5091>] kasan_report+0x71/0xa0
  37. [<ffffffff814ec082>] ? kmem_cache_alloc_trace+0x212/0x560
  38. [<ffffffff81d99468>] ? xhci_find_raw_port_number+0x98/0x170
  39. [<ffffffff814f33d4>] __asan_load8+0x64/0x70
  40. [<ffffffff81d99468>] xhci_find_raw_port_number+0x98/0x170
  41. [<ffffffff81db0105>] xhci_setup_addressable_virt_dev+0x235/0xa10
  42. [<ffffffff81d9ea51>] xhci_setup_device+0x3c1/0x1430
  43. [<ffffffff8121cddd>] ? trace_hardirqs_on+0xd/0x10
  44. [<ffffffff81d9fac0>] ? xhci_setup_device+0x1430/0x1430
  45. [<ffffffff81d9fad3>] xhci_address_device+0x13/0x20
  46. [<ffffffff81d2081a>] hub_port_init+0x55a/0x1550
  47. [<ffffffff81d28705>] hub_event+0xef5/0x24d0
  48. [<ffffffff81d27810>] ? hub_port_debounce+0x2f0/0x2f0
  49. [<ffffffff8195e1ee>] ? debug_object_deactivate+0x1be/0x270
  50. [<ffffffff81210203>] ? print_rt_rq+0x53/0x2d0
  51. [<ffffffff8121657d>] ? trace_hardirqs_off+0xd/0x10
  52. [<ffffffff8226acfb>] ? _raw_spin_unlock_irqrestore+0x5b/0x60
  53. [<ffffffff81250000>] ? irq_domain_set_hwirq_and_chip+0x30/0xb0
  54. [<ffffffff81256339>] ? debug_lockdep_rcu_enabled+0x39/0x40
  55. [<ffffffff812178c0>] ? __lock_is_held+0x90/0x130
  56. [<ffffffff81196877>] process_one_work+0x567/0xec0
  57. [...]
  58. Afterwards, xhci reports some functional errors:
  59. xhci_hcd 0000:00:14.0: ERROR: unexpected setup address command completion
  60. code 0x11.
  61. xhci_hcd 0000:00:14.0: ERROR: unexpected setup address command completion
  62. code 0x11.
  63. usb 4-3: device not accepting address 2, error -22
  64. Fix this by not overwriting the port1 argument in usb_alloc_dev(), but
  65. storing the raw port number as required by OF in an additional variable,
  66. raw_port.
  67. Fixes: 69bec7259853 ("USB: core: let USB device know device node")
  68. Signed-off-by: Nicolai Stange <nicstange@gmail.com>
  69. Acked-by: Alan Stern <stern@rowland.harvard.edu>
  70. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  71. ---
  72. drivers/usb/core/usb.c | 5 +++--
  73. 1 file changed, 3 insertions(+), 2 deletions(-)
  74. --- a/drivers/usb/core/usb.c
  75. +++ b/drivers/usb/core/usb.c
  76. @@ -423,6 +423,7 @@ struct usb_device *usb_alloc_dev(struct
  77. struct usb_device *dev;
  78. struct usb_hcd *usb_hcd = bus_to_hcd(bus);
  79. unsigned root_hub = 0;
  80. + unsigned raw_port = port1;
  81. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  82. if (!dev)
  83. @@ -497,11 +498,11 @@ struct usb_device *usb_alloc_dev(struct
  84. if (!parent->parent) {
  85. /* device under root hub's port */
  86. - port1 = usb_hcd_find_raw_port_number(usb_hcd,
  87. + raw_port = usb_hcd_find_raw_port_number(usb_hcd,
  88. port1);
  89. }
  90. dev->dev.of_node = usb_of_get_child_node(parent->dev.of_node,
  91. - port1);
  92. + raw_port);
  93. /* hub driver sets up TT records */
  94. }