Skip to contentSkip to conversation

fix: drivers/usb/serial: garmin_write_bulk: urb freed prematurely on success path

PATCH In review WenTao Liang 2 participants 2 messages Jun 27 – Jul 6

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix: drivers/usb/serial: garmin_write_bulk: urb freed prematurely on success path

In garmin_write_bulk(), usb_free_urb(urb) is called unconditionally after URB submission, even on the success path. When the URB completes later, the completion callback may perform additional operations on the already-freed URB structure, causing use-after-free or double free.

Move usb_free_urb(urb) to the error path only, allowing the completion callback to properly manage the URB lifecycle on success.

Fixes c4ac4496e835 ("USB: serial: garmin_gps: fix memory leak on failed URB submit")
---
 drivers/usb/serial/garmin_gps.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/usb/serial/garmin_gps.c +2 -4
index 7205483a0115..775959e0188d 100644
@@ -1029,12 +1029,10 @@ static int garmin_write_bulk(struct usb_serial_port *port,
 		count = status;
 		usb_unanchor_urb(urb);
 		kfree(buffer);
+		usb_free_urb(urb);
+		return count;
 	}
 
-	/* we are done with this urb, so let the host driver
-	 * really free it when it is finished with it */
-	usb_free_urb(urb);
-
 	return count;
 }
 
Signature · 1 line
2.39.5 (Apple Git-154)
maintainer
#1 lore raw
Quoted 7 lines · WenTao Liang

In garmin_write_bulk(), usb_free_urb(urb) is called unconditionally after URB submission, even on the success path. When the URB completes later, the completion callback may perform additional operations on the already-freed URB structure, causing use-after-free or double free.

Move usb_free_urb(urb) to the error path only, allowing the completion callback to properly manage the URB lifecycle on success.

No, this patch is also broken. USB core holds a reference to the URB until the completion has been called.

Quoted 2 lines
Fixes c4ac4496e835 ("USB: serial: garmin_gps: fix memory leak on failed URB submit")

This commit has nothing to do with this.

Quoted 1 line

Johan