hso_net_start_xmit() stashes the skb in odev->skb_tx_buf and stops the queue when hso_get_activity() reports -EAGAIN, which happens when the USB device is suspended or no longer configured. If the interface is then brought down, or the device is disconnected, that skb is simply left behind: hso_net_close() and hso_free_net_device() never release it, and hso_net_open() clears the pointer, so the skb and its data buffer are leaked. hso_resume() loses a frame in the same way when the device is still not ready: it passes skb_tx_buf to hso_net_start_xmit() and then clears the pointer unconditionally, so a frame stashed again by that call is never freed. Take the stashed skb with xchg() in both paths so that only one of them can obtain it, serialize the replay in hso_resume() against hso_net_close() with a per-interface mutex, and take the netdev TX lock around the replay so that it is also serialized with the stash in hso_net_start_xmit(). The state check uses test_bit(HSO_NET_RUNNING), the bit that hso_net_open() and hso_net_close() maintain. Reported-by: syzbot+bf62ca9997f43d2fa521@syzkaller.appspotmail.com Link: https://lore.kernel.org/all/6af74e31.fe96acdb.12252b.0009.GAE@google.com/ Fixes: 72dc1c096c70 ("HSO: add option hso driver") Fixes: c213f286f2cf ("[netdrvr] hso: dev_kfree_skb crash fix") Signed-off-by: Aibing Chen --- v4: - take the netdev TX lock around the replay in hso_resume() so it is serialized with the stash in hso_net_start_xmit(). v3: - serialize the replay in hso_resume() against hso_net_close() with a per-interface mutex. - check HSO_NET_RUNNING instead of the IFF_UP alias. v2: - take the stashed skb with xchg() so that only one path can free it. - add the two Fixes tags. drivers/net/usb/hso.c | 51 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/drivers/net/usb/hso.c b/drivers/net/usb/hso.c index 71caa3764..247c58cc7 100644 --- a/drivers/net/usb/hso.c +++ b/drivers/net/usb/hso.c @@ -142,6 +142,9 @@ struct hso_net { struct sk_buff *skb_rx_buf; struct sk_buff *skb_tx_buf; + /* serializes the hso_resume() replay with hso_net_close() */ + struct mutex tx_replay_lock; + enum pkt_parse_state rx_parse_state; spinlock_t net_lock; @@ -720,6 +723,14 @@ static int hso_net_open(struct net_device *net) static int hso_net_close(struct net_device *net) { struct hso_net *odev = netdev_priv(net); + struct sk_buff *skb; + + /* + * Serialize with the replay in hso_resume(): either the frame is + * replayed before we stop the device, or it is taken and dropped + * here, never both and never after the queue is gone. + */ + mutex_lock(&odev->tx_replay_lock); /* we don't need the queue anymore */ netif_stop_queue(net); @@ -728,6 +739,11 @@ static int hso_net_close(struct net_device *net) hso_stop_net_device(odev->parent); + skb = xchg(&odev->skb_tx_buf, NULL); + dev_kfree_skb(skb); + + mutex_unlock(&odev->tx_replay_lock); + /* done */ return 0; } @@ -2398,6 +2414,7 @@ static void hso_net_init(struct net_device *net) /* and initialize the semaphore */ spin_lock_init(&hso_net->net_lock); + mutex_init(&hso_net->tx_replay_lock); } /* Adds a network device in the network device table */ @@ -3032,6 +3049,7 @@ static int hso_resume(struct usb_interface *iface) { int i, result = 0; struct hso_net *hso_net; + struct sk_buff *skb; /* Start all serial ports */ for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) { @@ -3051,21 +3069,38 @@ static int hso_resume(struct usb_interface *iface) if (network_table[i] && (network_table[i]->interface == iface)) { hso_net = dev2net(network_table[i]); - if (hso_net->flags & IFF_UP) { - /* First transmit any lingering data, - then restart the device. */ - if (hso_net->skb_tx_buf) { + mutex_lock(&hso_net->tx_replay_lock); + + /* + * First transmit any lingering data, then restart + * the device. + */ + if (test_bit(HSO_NET_RUNNING, &hso_net->flags)) { + /* + * The frame is stashed by hso_net_start_xmit() + * under the TX lock, so take the same lock here: + * otherwise a concurrent transmit could store + * after we took the pointer and leave the queue + * stopped with the frame still stashed. + */ + netif_tx_lock_bh(hso_net->net); + skb = xchg(&hso_net->skb_tx_buf, NULL); + if (skb) { dev_dbg(&iface->dev, "Transmitting" " lingering data\n"); - hso_net_start_xmit(hso_net->skb_tx_buf, - hso_net->net); - hso_net->skb_tx_buf = NULL; + hso_net_start_xmit(skb, hso_net->net); } + netif_tx_unlock_bh(hso_net->net); + result = hso_start_net_device(network_table[i]); - if (result) + if (result) { + mutex_unlock(&hso_net->tx_replay_lock); goto out; + } } + + mutex_unlock(&hso_net->tx_replay_lock); } }