tap_del_queues() is currently called from the macvtap and ipvtap dellink handlers. Two things in this area only work because tap_open() runs under RTNL, which a following patch removes: 1) Not all unregistrations go through dellink. If register_netdevice() fails after the NETDEV_REGISTER notifier created the char device, or if macvlan_common_newlink() / ipvlan_link_new() fail after register_netdevice(), the device is unregistered without tap_del_queues() being called. Nothing can open the char device in this window today, because RTNL is held from the minor allocation to its release. 2) tap_del_queues() sets tap->numvtaps to MAX_TAP_QUEUES to "guarantee that any future tap_set_queue will fail", but since commit 815f236d6227 ("macvtap: add TUNSETQUEUE ioctl") tap_set_queue() tests tap->numqueues, which is zero at this point. A later tap_set_queue() would succeed and write to tap->taps[MAX_TAP_QUEUES], one element past the array. Again, this can not happen today since the minor is released in the same RTNL section. Call tap_del_queues() from the NETDEV_UNREGISTER notifier instead, so that every unregistration path is covered. The notifier is also invoked when the device moves to another netns, in which case the queues must stay attached: only act when the device is really being unregistered. Replace the numvtaps trick with an explicit tap->dead flag, tested in tap_set_queue(). No functional change intended: none of the above is reachable as long as tap_open() holds RTNL. These issues were found by code inspection, with the help of an AI coding assistant, while looking at RTNL removal in tap. They were not triggered. Signed-off-by: Eric Dumazet --- drivers/net/ipvlan/ipvtap.c | 8 +++++--- drivers/net/macvtap.c | 8 +++++--- drivers/net/tap.c | 5 ++++- include/linux/if_tap.h | 2 ++ 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/drivers/net/ipvlan/ipvtap.c b/drivers/net/ipvlan/ipvtap.c index 66c949d9426103edea10b2580f096f399f139f4f..d17daa1040967a1d80cede5d740d957204fe1186 100644 --- a/drivers/net/ipvlan/ipvtap.c +++ b/drivers/net/ipvlan/ipvtap.c @@ -112,10 +112,7 @@ static int ipvtap_newlink(struct net_device *dev, static void __ipvtap_dellink(struct net *net, struct net_device *dev, struct list_head *head) { - struct ipvtap_dev *vlantap = netdev_priv(dev); - netdev_rx_handler_unregister(dev); - tap_del_queues(&vlantap->tap); __ipvlan_link_delete(net, dev, head); } @@ -185,6 +182,11 @@ static int ipvtap_device_event(struct notifier_block *unused, return notifier_from_errno(err); break; case NETDEV_UNREGISTER: + /* NETDEV_UNREGISTER is also sent when the device moves to + * another netns, queues must survive this. + */ + if (dev->reg_state == NETREG_UNREGISTERING) + tap_del_queues(&vlantap->tap); /* vlan->minor == 0 if NETDEV_REGISTER above failed */ if (vlantap->tap.minor == 0) break; diff --git a/drivers/net/macvtap.c b/drivers/net/macvtap.c index cc975dfb738051cd43d70e6b95d7499400e345aa..3e88ca35cff2f9c755f8aff694e1c4ca83566741 100644 --- a/drivers/net/macvtap.c +++ b/drivers/net/macvtap.c @@ -120,10 +120,7 @@ static int macvtap_newlink(struct net_device *dev, static void macvtap_dellink(struct net_device *dev, struct list_head *head) { - struct macvtap_dev *vlantap = netdev_priv(dev); - netdev_rx_handler_unregister(dev); - tap_del_queues(&vlantap->tap); macvlan_dellink(dev, head); } @@ -186,6 +183,11 @@ static int macvtap_device_event(struct notifier_block *unused, return notifier_from_errno(err); break; case NETDEV_UNREGISTER: + /* NETDEV_UNREGISTER is also sent when the device moves to + * another netns, queues must survive this. + */ + if (dev->reg_state == NETREG_UNREGISTERING) + tap_del_queues(&vlantap->tap); /* vlan->minor == 0 if NETDEV_REGISTER above failed */ if (vlantap->tap.minor == 0) break; diff --git a/drivers/net/tap.c b/drivers/net/tap.c index 0f3ad8fd6ab66003093d2aedd4d7e7a6c4f882e9..ded0be1dfdd1e3123acb08e75aa9a874c9e949b5 100644 --- a/drivers/net/tap.c +++ b/drivers/net/tap.c @@ -102,6 +102,9 @@ static int tap_enable_queue(struct tap_dev *tap, struct file *file, static int tap_set_queue(struct tap_dev *tap, struct file *file, struct tap_queue *q) { + if (tap->dead) + return -ENODEV; + if (tap->numqueues == MAX_TAP_QUEUES) return -EBUSY; @@ -248,7 +251,7 @@ void tap_del_queues(struct tap_dev *tap) BUG_ON(tap->numvtaps); BUG_ON(tap->numqueues); /* guarantee that any future tap_set_queue will fail */ - tap->numvtaps = MAX_TAP_QUEUES; + tap->dead = true; } EXPORT_SYMBOL_GPL(tap_del_queues); diff --git a/include/linux/if_tap.h b/include/linux/if_tap.h index 553552fa635c3e1e53d1a63c203d32e4c4fd5a4f..e9c4c9272585cdb14cf0ea7ea7d96acc3e8964b7 100644 --- a/include/linux/if_tap.h +++ b/include/linux/if_tap.h @@ -43,6 +43,8 @@ struct tap_dev { int numqueues; netdev_features_t tap_features; int minor; + /* Set once the net_device is unregistered, no queue can be added. */ + bool dead; void (*update_features)(struct tap_dev *tap, netdev_features_t features); void (*count_tx_dropped)(struct tap_dev *tap); -- 2.53.0