Skip to content

Commit 14a0d63

Browse files
Oliver Neukumdavem330
authored andcommitted
usbnet: include wait queue head in device structure
This fixes a race which happens by freeing an object on the stack. Quoting Julius: > The issue is > that it calls usbnet_terminate_urbs() before that, which temporarily > installs a waitqueue in dev->wait in order to be able to wait on the > tasklet to run and finish up some queues. The waiting itself looks > okay, but the access to 'dev->wait' is totally unprotected and can > race arbitrarily. I think in this case usbnet_bh() managed to succeed > it's dev->wait check just before usbnet_terminate_urbs() sets it back > to NULL. The latter then finishes and the waitqueue_t structure on its > stack gets overwritten by other functions halfway through the > wake_up() call in usbnet_bh(). The fix is to just not allocate the data structure on the stack. As dev->wait is abused as a flag it also takes a runtime PM change to fix this bug. Signed-off-by: Oliver Neukum <oneukum@suse.de> Reported-by: Grant Grundler <grundler@google.com> Tested-by: Grant Grundler <grundler@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 681daee commit 14a0d63

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

‎drivers/net/usb/usbnet.c‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -752,14 +752,12 @@ EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
752752
// precondition: never called in_interrupt
753753
static void usbnet_terminate_urbs(struct usbnet *dev)
754754
{
755-
DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
756755
DECLARE_WAITQUEUE(wait, current);
757756
int temp;
758757

759758
/* ensure there are no more active urbs */
760-
add_wait_queue(&unlink_wakeup, &wait);
759+
add_wait_queue(&dev->wait, &wait);
761760
set_current_state(TASK_UNINTERRUPTIBLE);
762-
dev->wait = &unlink_wakeup;
763761
temp = unlink_urbs(dev, &dev->txq) +
764762
unlink_urbs(dev, &dev->rxq);
765763

@@ -773,15 +771,14 @@ static void usbnet_terminate_urbs(struct usbnet *dev)
773771
"waited for %d urb completions\n", temp);
774772
}
775773
set_current_state(TASK_RUNNING);
776-
dev->wait = NULL;
777-
remove_wait_queue(&unlink_wakeup, &wait);
774+
remove_wait_queue(&dev->wait, &wait);
778775
}
779776

780777
int usbnet_stop (struct net_device *net)
781778
{
782779
struct usbnet *dev = netdev_priv(net);
783780
struct driver_info *info = dev->driver_info;
784-
int retval;
781+
int retval, pm;
785782

786783
clear_bit(EVENT_DEV_OPEN, &dev->flags);
787784
netif_stop_queue (net);
@@ -791,6 +788,8 @@ int usbnet_stop (struct net_device *net)
791788
net->stats.rx_packets, net->stats.tx_packets,
792789
net->stats.rx_errors, net->stats.tx_errors);
793790

791+
/* to not race resume */
792+
pm = usb_autopm_get_interface(dev->intf);
794793
/* allow minidriver to stop correctly (wireless devices to turn off
795794
* radio etc) */
796795
if (info->stop) {
@@ -817,6 +816,9 @@ int usbnet_stop (struct net_device *net)
817816
dev->flags = 0;
818817
del_timer_sync (&dev->delay);
819818
tasklet_kill (&dev->bh);
819+
if (!pm)
820+
usb_autopm_put_interface(dev->intf);
821+
820822
if (info->manage_power &&
821823
!test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags))
822824
info->manage_power(dev, 0);
@@ -1437,11 +1439,12 @@ static void usbnet_bh (unsigned long param)
14371439
/* restart RX again after disabling due to high error rate */
14381440
clear_bit(EVENT_RX_KILL, &dev->flags);
14391441

1440-
// waiting for all pending urbs to complete?
1441-
if (dev->wait) {
1442-
if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1443-
wake_up (dev->wait);
1444-
}
1442+
/* waiting for all pending urbs to complete?
1443+
* only then can we forgo submitting anew
1444+
*/
1445+
if (waitqueue_active(&dev->wait)) {
1446+
if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
1447+
wake_up_all(&dev->wait);
14451448

14461449
// or are we maybe short a few urbs?
14471450
} else if (netif_running (dev->net) &&
@@ -1580,6 +1583,7 @@ usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
15801583
dev->driver_name = name;
15811584
dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
15821585
| NETIF_MSG_PROBE | NETIF_MSG_LINK);
1586+
init_waitqueue_head(&dev->wait);
15831587
skb_queue_head_init (&dev->rxq);
15841588
skb_queue_head_init (&dev->txq);
15851589
skb_queue_head_init (&dev->done);
@@ -1791,9 +1795,10 @@ int usbnet_resume (struct usb_interface *intf)
17911795
spin_unlock_irq(&dev->txq.lock);
17921796

17931797
if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
1794-
/* handle remote wakeup ASAP */
1795-
if (!dev->wait &&
1796-
netif_device_present(dev->net) &&
1798+
/* handle remote wakeup ASAP
1799+
* we cannot race against stop
1800+
*/
1801+
if (netif_device_present(dev->net) &&
17971802
!timer_pending(&dev->delay) &&
17981803
!test_bit(EVENT_RX_HALT, &dev->flags))
17991804
rx_alloc_submit(dev, GFP_NOIO);

‎include/linux/usb/usbnet.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct usbnet {
3030
struct driver_info *driver_info;
3131
const char *driver_name;
3232
void *driver_priv;
33-
wait_queue_head_t *wait;
33+
wait_queue_head_t wait;
3434
struct mutex phy_mutex;
3535
unsigned char suspend_count;
3636
unsigned char pkt_cnt, pkt_err;

0 commit comments

Comments
 (0)