Skip to contentSkip to conversation

can: mcba_usb: Fix double free of RX buffers in mcba_urb_unlink()

PATCH In review Alexandre Becholey 2 participants 2 messages Nov 19 – Nov 22

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

PATCH 1/1 can: mcba_usb: Fix double free of RX buffers in mcba_urb_unlink()

mcba_urb_unlink() frees the RX buffers but leaves the stale pointers in priv->rxbuf[]. It runs from both mcba_usb_close() and mcba_usb_disconnect(), so once the interface has been up, the buffers are freed twice: on unplug after "ip link set can0 down", on unplug while can0 is up, or on a second up/down cycle. No special device behaviour is needed; on a host controller without DMA the double usb_free_coherent() is a double kfree().

Clear each rxbuf[] pointer after freeing it, which makes the function safe to call twice again, as it was before commit 91c02557174b ("can:
mcba_usb: fix memory leak in mcba_usb").

The bug was found, and the reproducer and this fix were written, by AI agents (Claude Mythos and Claude Opus models). The analysis and the patch were reviewed by a human.

Fixes 91c02557174b ("can: mcba_usb: fix memory leak in mcba_usb")

---
An emulated adapter on dummy_hcd: "ip link set can0 up/down", then unplug, gives a KASAN double-free in hcd_buffer_free() on v7.3.1 and master 9d66f26fefac; the two other sequences as well. All clean with the fix, and a second "ip link set up" still works. Reproducer available on request.

 drivers/net/can/usb/mcba_usb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/can/usb/mcba_usb.c +3 -1
index 04170326dc7e..b40b323da9d3 100644
@@ -718,9 +718,11 @@ static void mcba_urb_unlink(struct mcba_priv *priv)
 
 	usb_kill_anchored_urbs(&priv->rx_submitted);
 
-	for (i = 0; i < MCBA_MAX_RX_URBS; ++i)
+	for (i = 0; i < MCBA_MAX_RX_URBS; ++i) {
 		usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
 				  priv->rxbuf[i], priv->rxbuf_dma[i]);
+		priv->rxbuf[i] = NULL;
+	}
 
 	usb_kill_anchored_urbs(&priv->tx_submitted);
 }
#1 lore raw

Hi Alexandre,

Alexandre Becholey <[email protected]> says:

Quoted 8 lines

mcba_urb_unlink() frees the RX buffers but leaves the stale pointers in priv->rxbuf[]. It runs from both mcba_usb_close() and mcba_usb_disconnect(), so once the interface has been up, the buffers are freed twice: on unplug after "ip link set can0 down", on unplug while can0 is up, or on a second up/down cycle. No special device behaviour is needed; on a host controller without DMA the double usb_free_coherent() is a double kfree().

Sounds reasonable.

I wonder, tho, did you test on real HW or used emulated one via usb gadgets?

What caught my eye is that mcba_usb_open() never reallocates URBs closed in mcba_usb_close(). So I'd expect device to not work properly after second "ip link set can0 up".

Maybe it shouldn't close them at the first place?

Quoted 27 lines

Clear each rxbuf[] pointer after freeing it, which makes the function> safe to call twice again, as it was before commit 91c02557174b ("can:
mcba_usb: fix memory leak in mcba_usb").

The bug was found, and the reproducer and this fix were written, by AI agents (Claude Mythos and Claude Opus models). The analysis and the patch were reviewed by a human.

Fixes 91c02557174b ("can: mcba_usb: fix memory leak in mcba_usb")

---
An emulated adapter on dummy_hcd: "ip link set can0 up/down", then unplug, gives a KASAN double-free in hcd_buffer_free() on v7.3.1 and master 9d66f26fefac; the two other sequences as well. All clean with the fix, and a second "ip link set up" still works. Reproducer available on request.

  drivers/net/can/usb/mcba_usb.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/can/usb/mcba_usb.c +0 -0
index 04170326dc7e..b40b323da9d3 100644
@@ -718,9 +718,11 @@ static void mcba_urb_unlink(struct mcba_priv *priv)
 
  	usb_kill_anchored_urbs(&priv->rx_submitted);

drivers/net/can/usb/mcba_usb.c static void mcba_urb_unlink(struct mcba_priv *priv) · line 728

Quoted 10 lines
drivers/net/can/usb/mcba_usb.c +3 -1
 
-	for (i = 0; i < MCBA_MAX_RX_URBS; ++i)
+	for (i = 0; i < MCBA_MAX_RX_URBS; ++i) {
  		usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
  				  priv->rxbuf[i], priv->rxbuf_dma[i]);
+		priv->rxbuf[i] = NULL;
+	}
 
  	usb_kill_anchored_urbs(&priv->tx_submitted);
  }

With regards,
Pavel Skripkin