UPSTREAM: xhci: fix null-pointer dereference when destroying half-built segment rings
authorJulius Werner <jwerner@chromium.org>
Sat, 27 Oct 2012 00:34:03 +0000 (17:34 -0700)
committerGerrit <chrome-bot@google.com>
Tue, 13 Nov 2012 06:48:21 +0000 (22:48 -0800)
commit22272af40501b9d7cc24768cd71c22893b016365
tree24a50cb9811fea12149569cd31215b40c83ba70e
parentd90b9d57d2c072802aefc73752239da13fde96ad
UPSTREAM: xhci: fix null-pointer dereference when destroying half-built segment rings

xhci_alloc_segments_for_ring() builds a list of xhci_segments and links
the tail to head at the end (forming a ring). When it bails out for OOM
reasons half-way through, it tries to destroy its half-built list with
xhci_free_segments_for_ring(), even though it is not a ring yet. This
causes a null-pointer dereference upon hitting the last element.

Furthermore, one of its callers (xhci_ring_alloc()) mistakenly believes
the output parameters to be valid upon this kind of OOM failure, and
calls xhci_ring_free() on them. Since the (incomplete) list/ring should
already be destroyed in that case, this would lead to a use after free.

This patch fixes those issues by having xhci_alloc_segments_for_ring()
destroy its half-built, non-circular list manually and destroying the
invalid struct xhci_ring in xhci_ring_alloc() with a plain kfree().

BUG=chrome-os-partner:15124
TEST=Hard to reproduce since the underlying bug was fixed already.

Has been accepted upstream by Sarah Sharp at
https://lkml.org/lkml/2012/11/12/313

Change-Id: I8035a390ab410873b97bce23dded5f0cfc14dcf6
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/36745
Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
drivers/usb/host/xhci-mem.c