Skip to contentSkip to conversation

usbip: stub: keep URB array state consistent on errors

PATCH In review Weiming Shi 2 participants 2 messages Sep 12 – Sep 23

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

PATCH 1/1 usbip: stub: keep URB array state consistent on errors

stub_priv_alloc() links a zeroed stub_priv to priv_init before the URB array is allocated. The error handler may therefore consume the object after stub_recv_cmd_submit() reports SDEV_EVENT_ERROR_MALLOC.

Do not publish num_urbs until the array exists. If a later URB allocation fails, clear the freed array pointer and count before the event handler can walk them. This prevents a NULL dereference on array-allocation failure and freed-array reads that can lead to invalid URB use or a double free on later failures.

Fixes ea44d190764b ("usbip: Implement SG support to vhci-hcd and stub driver")
Reported-by [email protected]
Assisted-by Codex:gpt-5
---
 drivers/usb/usbip/stub_rx.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/usbip/stub_rx.c +3 -1
index 1e9ae578810d9..33058fb05b2f4 100644
@@ -534,10 +534,10 @@ static void stub_recv_cmd_submit(struct stub_device *sdev,
 	}
 
 	/* allocate urb array */
-	priv->num_urbs = num_urbs;
 	priv->urbs = kmalloc_objs(*priv->urbs, num_urbs);
 	if (!priv->urbs)
 		goto err_urbs;
+	priv->num_urbs = num_urbs;
 
 	/* setup a urb */
 	if (support_sg) {
@@ -641,6 +641,8 @@ static void stub_recv_cmd_submit(struct stub_device *sdev,
 
 err_urb:
 	kfree(priv->urbs);
+	priv->urbs = NULL;
+	priv->num_urbs = 0;
 err_urbs:
 	kfree(buffer);
 	sgl_free(sgl);
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 9 lines · Weiming Shi

stub_priv_alloc() links a zeroed stub_priv to priv_init before the URB array is allocated. The error handler may therefore consume the object after stub_recv_cmd_submit() reports SDEV_EVENT_ERROR_MALLOC.

Do not publish num_urbs until the array exists. If a later URB allocation fails, clear the freed array pointer and count before the event handler can walk them. This prevents a NULL dereference on array-allocation failure and freed-array reads that can lead to invalid URB use or a double free on later failures.

How is this problem reproduced? Why is this a problem in this code path?

Quoted 28 lines
Fixes ea44d190764b ("usbip: Implement SG support to vhci-hcd and stub driver")
Reported-by [email protected]
Assisted-by Codex:gpt-5
---
  drivers/usb/usbip/stub_rx.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/usbip/stub_rx.c +1 -1
index 1e9ae578810d9..33058fb05b2f4 100644
@@ -534,10 +534,10 @@ static void stub_recv_cmd_submit(struct stub_device *sdev,
  	}
 
  	/* allocate urb array */
-	priv->num_urbs = num_urbs;
  	priv->urbs = kmalloc_objs(*priv->urbs, num_urbs);
  	if (!priv->urbs)
  		goto err_urbs;
+	priv->num_urbs = num_urbs;
 
  	/* setup a urb */
  	if (support_sg) {

drivers/usb/usbip/stub_rx.c static void stub_recv_cmd_submit(struct stub_device *sdev, · line 648

Quoted 8 lines
drivers/usb/usbip/stub_rx.c +2 -0
@@ -641,6 +641,8 @@ static void stub_recv_cmd_submit(struct stub_device *sdev,
 
  err_urb:
  	kfree(priv->urbs);
+	priv->urbs = NULL;
+	priv->num_urbs = 0;
  err_urbs:
  	kfree(buffer);
  	sgl_free(sgl);

thanks,
-- Shuah