Skip to contentSkip to conversation

net: usb: rtl8150: cleaning up races with ceasing IO

PATCH New Oliver Neukum 2 participants 5 messages Nov 25

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

cover net: usb: rtl8150: cleaning up races with ceasing IO

The driver has the fundamental issue that it uses a tasklet that reschedules itself and resubmits an URB, which in case of an error can schedule the tasklet and so on.
This cycle cannot be broken by adding primitives for cancelling tasklets and URBs but it also takes a flag preventing rescheduling or resubmission of tasklet and URB.

It turns out that this is quite complex because the issue hits in close(), suspend() and disconnect(). The required changes became so complex that I decided that this needs to be broken up in series of patches and needs testing.

V3: Added patches for close() and suspend() which Sashiko noted to be absent
V2: V2: Sashiko pointed out errors which conceptually arose from handling close() and disconnect() in the same way. Treating disconnect() separatly makes them go away

#1 lore raw

PATCH 1/3 net: usb: rtl8150: break cycle between tasklet and URB

The driver shows two different cycles that can result in an URB or the tasklet or both to be in active use after disconnect() or suspend() return, which will in turn lead to a use after free.
Firstly, the tasklet will in its error handling reschedule itself without checking the flag that indicates that the device has been disconnected or suspended. Secondly, the URB and the tasklet can reschedule respectively resubmit each other in turn. To break that cycle in addition to checking the flag the URB has to be poisoned.

---
 drivers/net/usb/rtl8150.c | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)
drivers/net/usb/rtl8150.c +27 -6
index d51e43170e03..547a14beafb8 100644
@@ -353,6 +353,13 @@ static void free_all_urbs(rtl8150_t * dev)
 	usb_free_urb(dev->intr_urb);
 }
 
+static void terminate_all_urbs(rtl8150_t *dev)
+{
+	usb_poison_urb(dev->rx_urb);
+	usb_poison_urb(dev->tx_urb);
+	usb_poison_urb(dev->intr_urb);
+}
+
 static void unlink_all_urbs(rtl8150_t * dev)
 {
 	usb_kill_urb(dev->rx_urb);
@@ -528,12 +535,15 @@ static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message)
 {
 	rtl8150_t *dev = usb_get_intfdata(intf);
 
+	spin_lock_irq(&dev->rx_pool_lock);
+	set_bit(RTL8150_UNPLUG, &dev->flags);
+	spin_unlock_irq(&dev->rx_pool_lock);
+
 	netif_device_detach(dev->netdev);
 
-	if (netif_running(dev->netdev)) {
-		usb_kill_urb(dev->rx_urb);
-		usb_kill_urb(dev->intr_urb);
-	}
+	usb_poison_urb(dev->rx_urb);
+	usb_poison_urb(dev->intr_urb);
+
 	return 0;
 }
 
@@ -541,6 +551,13 @@ static int rtl8150_resume(struct usb_interface *intf)
 {
 	rtl8150_t *dev = usb_get_intfdata(intf);
 
+	spin_lock_irq(&dev->rx_pool_lock);
+	clear_bit(RTL8150_UNPLUG, &dev->flags);
+	spin_unlock_irq(&dev->rx_pool_lock);
+
+	usb_unpoison_urb(dev->rx_urb);
+	usb_unpoison_urb(dev->intr_urb);
+
 	netif_device_attach(dev->netdev);
 	if (netif_running(dev->netdev)) {
 		dev->rx_urb->status = 0;
@@ -618,7 +635,10 @@ static void rx_fixup(struct tasklet_struct *t)
 
 	return;
 tlsched:
-	tasklet_schedule(&dev->tl);
+	spin_lock_irq(&dev->rx_pool_lock);
+	if (!test_bit(RTL8150_UNPLUG, &dev->flags))
+		tasklet_schedule(&dev->tl);
+	spin_unlock_irq(&dev->rx_pool_lock);
 }
 
 static int enable_net_traffic(rtl8150_t * dev)
@@ -970,7 +990,8 @@ static void rtl8150_disconnect(struct usb_interface *intf)
 		set_bit(RTL8150_UNPLUG, &dev->flags);
 		tasklet_kill(&dev->tl);
 		unregister_netdev(dev->netdev);
-		unlink_all_urbs(dev);
+		terminate_all_urbs(dev);
+		tasklet_kill(&dev->tl);
 		free_all_urbs(dev);
 		free_skb_pool(dev);
 		dev_kfree_skb(dev->rx_skb);
Signature · 1 line
2.56.1
#2 lore raw

PATCH 2/3 net: usb: rtl8150: fix race in open() error handling

In reply to Oliver Neukum

In case open fails the URB already submitted must be killed. Because rx_urb can schedule a tasklet, which would have to be killed as well, it is better to submit intr_urb first in order to get correct error handling.

---
 drivers/net/usb/rtl8150.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/net/usb/rtl8150.c +11 -9
index 547a14beafb8..5de917ce250f 100644
@@ -525,7 +525,7 @@ static void intr_callback(struct urb *urb)
 	res = usb_submit_urb (urb, GFP_ATOMIC);
 	if (res == -ENODEV)
 		netif_device_detach(dev->netdev);
-	else if (res)
+	else if (res && res != -EPERM)
 		dev_err(&dev->udev->dev,
 			"can't resubmit intr, %s-%s/input0, status %d\n",
 			dev->udev->bus->bus_name, dev->udev->devpath, res);
@@ -775,20 +775,22 @@ static int rtl8150_open(struct net_device *netdev)
 
 	usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
 		      dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
-	if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL))) {
+	usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
+			dev->intr_buff, INTBUFSIZE, intr_callback,
+			dev, dev->intr_interval);
+	res = usb_submit_urb(dev->intr_urb, GFP_KERNEL);
+	if (res) {
 		if (res == -ENODEV)
 			netif_device_detach(dev->netdev);
-		dev_warn(&netdev->dev, "rx_urb submit failed: %d\n", res);
+		dev_warn(&netdev->dev, "intr_urb submit failed: %d\n", res);
 		return res;
 	}
-	usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
-		     dev->intr_buff, INTBUFSIZE, intr_callback,
-		     dev, dev->intr_interval);
-	if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL))) {
+	res = usb_submit_urb(dev->rx_urb, GFP_KERNEL);
+	if (res) {
 		if (res == -ENODEV)
 			netif_device_detach(dev->netdev);
-		dev_warn(&netdev->dev, "intr_urb submit failed: %d\n", res);
-		usb_kill_urb(dev->rx_urb);
+		usb_kill_urb(dev->intr_urb);
+		dev_warn(&netdev->dev, "rx_urb submit failed: %d\n", res);
 		return res;
 	}
 	enable_net_traffic(dev);
Signature · 1 line
2.56.1
#3 lore raw

PATCH 3/3 net: usb: rtl8150: cease all IO upon close

In reply to Oliver Neukum

close() suffers from the same cyle of resubmission and rescheduling as disconnect. It can be broken by cancelling the tasklet while the URBs are poisoned.

---
 drivers/net/usb/rtl8150.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
drivers/net/usb/rtl8150.c +15 -9
index 5de917ce250f..f09792c4c680 100644
@@ -97,6 +97,7 @@ enum rtl8150_usb_ep {
 #define	RX_REG_SET		1
 #define	RTL8150_UNPLUG		2
 #define	RX_URB_FAIL		3
+#define RTL8150_QUIET		4
 
 /* Define these values to match your device */
 #define	VENDOR_ID_REALTEK		0x0bda
@@ -360,11 +361,11 @@ static void terminate_all_urbs(rtl8150_t *dev)
 	usb_poison_urb(dev->intr_urb);
 }
 
-static void unlink_all_urbs(rtl8150_t * dev)
+static void enable_all_urbs(rtl8150_t *dev)
 {
-	usb_kill_urb(dev->rx_urb);
-	usb_kill_urb(dev->tx_urb);
-	usb_kill_urb(dev->intr_urb);
+	usb_unpoison_urb(dev->rx_urb);
+	usb_unpoison_urb(dev->tx_urb);
+	usb_unpoison_urb(dev->intr_urb);
 }
 
 static inline struct sk_buff *pull_skb(rtl8150_t *dev)
@@ -453,7 +454,8 @@ static void read_bulk_callback(struct urb *urb)
 
 	return;
 resched:
-	tasklet_schedule(&dev->tl);
+	if (!test_bit(RTL8150_QUIET, &dev->flags))
+		tasklet_schedule(&dev->tl);
 }
 
 static void write_bulk_callback(struct urb *urb)
@@ -536,7 +538,7 @@ static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message)
 	rtl8150_t *dev = usb_get_intfdata(intf);
 
 	spin_lock_irq(&dev->rx_pool_lock);
-	set_bit(RTL8150_UNPLUG, &dev->flags);
+	set_bit(RTL8150_QUIET, &dev->flags);
 	spin_unlock_irq(&dev->rx_pool_lock);
 
 	netif_device_detach(dev->netdev);
@@ -552,7 +554,7 @@ static int rtl8150_resume(struct usb_interface *intf)
 	rtl8150_t *dev = usb_get_intfdata(intf);
 
 	spin_lock_irq(&dev->rx_pool_lock);
-	clear_bit(RTL8150_UNPLUG, &dev->flags);
+	clear_bit(RTL8150_QUIET, &dev->flags);
 	spin_unlock_irq(&dev->rx_pool_lock);
 
 	usb_unpoison_urb(dev->rx_urb);
@@ -636,7 +638,8 @@ static void rx_fixup(struct tasklet_struct *t)
 	return;
 tlsched:
 	spin_lock_irq(&dev->rx_pool_lock);
-	if (!test_bit(RTL8150_UNPLUG, &dev->flags))
+	if (!test_bit(RTL8150_UNPLUG, &dev->flags) &&
+			!test_bit(RTL8150_QUIET, &dev->flags))
 		tasklet_schedule(&dev->tl);
 	spin_unlock_irq(&dev->rx_pool_lock);
 }
@@ -805,9 +808,12 @@ static int rtl8150_close(struct net_device *netdev)
 	rtl8150_t *dev = netdev_priv(netdev);
 
 	netif_stop_queue(netdev);
+	set_bit(RTL8150_QUIET, &dev->flags);
 	if (!test_bit(RTL8150_UNPLUG, &dev->flags))
 		disable_net_traffic(dev);
-	unlink_all_urbs(dev);
+	terminate_all_urbs(dev);
+	tasklet_kill(&dev->tl);
+	enable_all_urbs(dev);
 
 	return 0;
 }
Signature · 1 line
2.56.1
#4 lore raw
In reply to Oliver Neukum
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.