Skip to content

Commit 03819ab

Browse files
oleremkuba-moo
authored andcommitted
net: usb: lan78xx: Fix double free issue with interrupt buffer allocation
In lan78xx_probe(), the buffer `buf` was being freed twice: once implicitly through `usb_free_urb(dev->urb_intr)` with the `URB_FREE_BUFFER` flag and again explicitly by `kfree(buf)`. This caused a double free issue. To resolve this, reordered `kmalloc()` and `usb_alloc_urb()` calls to simplify the initialization sequence and removed the redundant `kfree(buf)`. Now, `buf` is allocated after `usb_alloc_urb()`, ensuring it is correctly managed by `usb_fill_int_urb()` and freed by `usb_free_urb()` as intended. Fixes: a6df95c ("lan78xx: Fix memory allocation bug") Cc: John Efstathiades <[email protected]> Signed-off-by: Oleksij Rempel <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent f26a29a commit 03819ab

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

drivers/net/usb/lan78xx.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4414,37 +4414,38 @@ static int lan78xx_probe(struct usb_interface *intf,
44144414

44154415
period = ep_intr->desc.bInterval;
44164416
maxp = usb_maxpacket(dev->udev, dev->pipe_intr);
4417-
buf = kmalloc(maxp, GFP_KERNEL);
4418-
if (!buf) {
4417+
4418+
dev->urb_intr = usb_alloc_urb(0, GFP_KERNEL);
4419+
if (!dev->urb_intr) {
44194420
ret = -ENOMEM;
44204421
goto out5;
44214422
}
44224423

4423-
dev->urb_intr = usb_alloc_urb(0, GFP_KERNEL);
4424-
if (!dev->urb_intr) {
4424+
buf = kmalloc(maxp, GFP_KERNEL);
4425+
if (!buf) {
44254426
ret = -ENOMEM;
4426-
goto out6;
4427-
} else {
4428-
usb_fill_int_urb(dev->urb_intr, dev->udev,
4429-
dev->pipe_intr, buf, maxp,
4430-
intr_complete, dev, period);
4431-
dev->urb_intr->transfer_flags |= URB_FREE_BUFFER;
4427+
goto free_urbs;
44324428
}
44334429

4430+
usb_fill_int_urb(dev->urb_intr, dev->udev,
4431+
dev->pipe_intr, buf, maxp,
4432+
intr_complete, dev, period);
4433+
dev->urb_intr->transfer_flags |= URB_FREE_BUFFER;
4434+
44344435
dev->maxpacket = usb_maxpacket(dev->udev, dev->pipe_out);
44354436

44364437
/* Reject broken descriptors. */
44374438
if (dev->maxpacket == 0) {
44384439
ret = -ENODEV;
4439-
goto out6;
4440+
goto free_urbs;
44404441
}
44414442

44424443
/* driver requires remote-wakeup capability during autosuspend. */
44434444
intf->needs_remote_wakeup = 1;
44444445

44454446
ret = lan78xx_phy_init(dev);
44464447
if (ret < 0)
4447-
goto out7;
4448+
goto free_urbs;
44484449

44494450
ret = register_netdev(netdev);
44504451
if (ret != 0) {
@@ -4466,10 +4467,8 @@ static int lan78xx_probe(struct usb_interface *intf,
44664467

44674468
out8:
44684469
phy_disconnect(netdev->phydev);
4469-
out7:
4470+
free_urbs:
44704471
usb_free_urb(dev->urb_intr);
4471-
out6:
4472-
kfree(buf);
44734472
out5:
44744473
lan78xx_unbind(dev, intf);
44754474
out4:

0 commit comments

Comments
 (0)