Skip to content

Commit ac94abb

Browse files
agabbasovgregkh
authored andcommitted
usb: gadget: composite: Fix use-after-free in usb_composite_overwrite_options
commit aec17e1e249567e82b26dafbb86de7d07fde8729 upstream. KASAN enabled configuration reports an error BUG: KASAN: use-after-free in usb_composite_overwrite_options+... [libcomposite] at addr ... Read of size 1 by task ... when some driver is un-bound and then bound again. For example, this happens with FunctionFS driver when "ffs-test" test application is run several times in a row. If the driver has empty manufacturer ID string in initial static data, it is then replaced with generated string. After driver unbinding the generated string is freed, but the driver data still keep that pointer. And if the driver is then bound again, that pointer is re-used for string emptiness check. The fix is to clean up the driver string data upon its unbinding to drop the pointer to freed memory. Fixes: cc2683c ("usb: gadget: Provide a default implementation of default manufacturer string") Signed-off-by: Andrew Gabbasov <andrew_gabbasov@mentor.com> Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 16c1ef6 commit ac94abb

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

drivers/usb/gadget/composite.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,8 @@ static DEVICE_ATTR_RO(suspended);
19051905
static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
19061906
{
19071907
struct usb_composite_dev *cdev = get_gadget_data(gadget);
1908+
struct usb_gadget_strings *gstr = cdev->driver->strings[0];
1909+
struct usb_string *dev_str = gstr->strings;
19081910

19091911
/* composite_disconnect() must already have been called
19101912
* by the underlying peripheral controller driver!
@@ -1924,6 +1926,9 @@ static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
19241926

19251927
composite_dev_cleanup(cdev);
19261928

1929+
if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
1930+
dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
1931+
19271932
kfree(cdev->def_manufacturer);
19281933
kfree(cdev);
19291934
set_gadget_data(gadget, NULL);

0 commit comments

Comments
 (0)