xhci: Don't add a virt_dev to the devs array before it's fully allocated
authorMathias Nyman <mathias.nyman@linux.intel.com>
Fri, 8 Dec 2017 16:10:05 +0000 (18:10 +0200)
committerBen Hutchings <ben@decadent.org.uk>
Sat, 3 Mar 2018 15:50:45 +0000 (15:50 +0000)
commit3a70d5ab61400027633a873b9a2d958df39123c8
tree7747ef02dd112f05e3c1cdc8ca1c067ad8156af5
parentf95ea9d1980fd2dd98bb253cb4b033e447b37c9b
xhci: Don't add a virt_dev to the devs array before it's fully allocated

commit 5d9b70f7d52eb14bb37861c663bae44de9521c35 upstream.

Avoid null pointer dereference if some function is walking through the
devs array accessing members of a new virt_dev that is mid allocation.

Add the virt_dev to xhci->devs[i] _after_ the virt_device and all its
members are properly allocated.

issue found by KASAN: null-ptr-deref in xhci_find_slot_id_by_port

"Quick analysis suggests that xhci_alloc_virt_device() is not mutex
protected. If so, there is a time frame where xhci->devs[slot_id] is set
but not fully initialized. Specifically, xhci->devs[i]->udev can be NULL."

Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[bwh: Backported to 3.2: There is an extra failure path, so we may need to
 free dev->eps[0].ring]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
drivers/usb/host/xhci-mem.c