USB: Gadget core: fix inconsistency in the interface tousb_add_gadget_udc_release()
authorAlan Stern <stern@rowland.harvard.edu>
Thu, 17 Aug 2017 18:49:55 +0000 (14:49 -0400)
committerFelipe Balbi <felipe.balbi@linux.intel.com>
Fri, 18 Aug 2017 07:26:13 +0000 (10:26 +0300)
commitafd7fd81f22bf90474216515dbd6088f9bd70343
treee792fef639028e81b5a42d0b361dd9aa81489a74
parentc37a9cd51dfa85ee59427c51b49dee74cec124c5
USB: Gadget core: fix inconsistency in the interface tousb_add_gadget_udc_release()

The usb_add_gadget_udc_release() routine in the USB gadget core will
sometimes but not always call the gadget's release function when an
error occurs.  More specifically, if the struct usb_udc allocation
fails then the release function is not called, and for other errors it
is.

As a result, users of this routine cannot know whether they need to
deallocate the memory containing the gadget structure following an
error.  This leads to unavoidable memory leaks or double frees.

This patch fixes the problem by splitting the existing
device_register() call into device_initialize() and device_add(), and
doing the udc allocation in between.  That way, even if the allocation
fails it is still possible to call device_del(), and so the release
function will be always called following an error.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Reported-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
drivers/usb/gadget/udc/core.c