net: WireGuard secure network tunnel
Subsystems (3)
Hello Jason A. Donenfeld,
The patch e7096c131e51: "net: WireGuard secure network tunnel" from Dec 9, 2019, leads to the following static checker warning:
net/core/dev.c:10103 netdev_run_todo() warn: 'dev->_tx' double freed
net/core/dev.c
10071 /* Wait for rcu callbacks to finish before next phase */
10072 if (!list_empty(&list))
10073 rcu_barrier();
10074
10075 while (!list_empty(&list)) {
10076 struct net_device *dev
10077 = list_first_entry(&list, struct net_device, todo_list);
10078 list_del(&dev->todo_list);
10079
10080 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
10081 pr_err("network todo '%s' but state %d\n",
10082 dev->name, dev->reg_state);
10083 dump_stack();
10084 continue;
10085 }
10086
10087 dev->reg_state = NETREG_UNREGISTERED;
10088
10089 netdev_wait_allrefs(dev);
10090
10091 /* paranoia */
10092 BUG_ON(netdev_refcnt_read(dev));
10093 BUG_ON(!list_empty(&dev->ptype_all));
10094 BUG_ON(!list_empty(&dev->ptype_specific));
10095 WARN_ON(rcu_access_pointer(dev->ip_ptr));
10096 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
10097 #if IS_ENABLED(CONFIG_DECNET)
10098 WARN_ON(dev->dn_ptr);
10099 #endif
10100 if (dev->priv_destructor)
10101 dev->priv_destructor(dev);
^^^^^^^^^^^^^^^^^^^^^^^^^The wg_destruct() functions frees "dev".
10102 if (dev->needs_free_netdev)
^^^^^
Use after free.
10103 free_netdev(dev);
10104
10105 /* Report a network device has been unregistered */ 10106 rtnl_lock(); 10107 dev_net(dev)->dev_unreg_count--; 10108 __rtnl_unlock(); 10109 wake_up(&netdev_unregistering_wq);
10110
10111 /* Free network device */
10112 kobject_put(&dev->dev.kobj);
10113 }
10114 }
regards,
dan carpenter
Quoted 55 lines · Dan Carpenter
Hello Jason A. Donenfeld,
The patch
e7096c131e51: "net: WireGuard secure network tunnel" from Dec 9, 2019, leads to the following static checker warning:net/core/dev.c:10103 netdev_run_todo() warn: 'dev->_tx' double freed
net/core/dev.c
10071 /* Wait for rcu callbacks to finish before next phase */
10072 if (!list_empty(&list))
10073rcu_barrier();
10074
10075 while (!list_empty(&list)) {
10076 structnet_device*dev
10077 =list_first_entry(&list, struct net_device, todo_list);
10078list_del(&dev->todo_list);
10079
10080 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
10081 pr_err("network todo '%s' but state %d\n",
10082dev->name,dev->reg_state);
10083dump_stack();
10084 continue;
10085 }
10086
10087dev->reg_state=NETREG_UNREGISTERED;
10088
10089netdev_wait_allrefs(dev);
1009010091 /* paranoia */ 10092 BUG_ON(netdev_refcnt_read(dev)); 10093 BUG_ON(!list_empty(&dev->ptype_all)); 10094 BUG_ON(!list_empty(&dev->ptype_specific)); 10095 WARN_ON(rcu_access_pointer(dev->ip_ptr)); 10096 WARN_ON(rcu_access_pointer(dev->ip6_ptr)); 10097 #if IS_ENABLED(CONFIG_DECNET) 10098 WARN_ON(dev->dn_ptr); 10099 #endif 10100 if (dev->priv_destructor) 10101 dev->priv_destructor(dev); ^^^^^^^^^^^^^^^^^^^^^^^^^The
wg_destruct()functions frees "dev".10102 if (
dev->needs_free_netdev)^^^^^Use after free.
10103
free_netdev(dev);
1010410105 /* Report a network device has been unregistered */ 10106 rtnl_lock(); 10107 dev_net(dev)->dev_unreg_count--; 10108 __rtnl_unlock(); 10109 wake_up(&netdev_unregistering_wq);
Quoted 8 lines
10110
10111 /* Free network device */
10112kobject_put(&dev->dev.kobj);
10113 }
10114 }regards,
dan carpenter
I actually recall a patch ~3 years ago from DaveM trying to make netdev tear down semantics a bit cleaner, by distinguishing between the case when netdevs free their own dev and when they don't. I'm not sure whether wireguard should set needs_free_netdev or not, but I vaguely remember reasoning about that a long time ago and deciding, "no". However, branching on dev->needs_free_netdev seems like it must be a UaF always in the case where needs_free_netdev is false, since in that case, the destructor should free it (I think). I'll send a patch, and see if DaveM likes that, or if he'd prefer I just set needs_free_netdev in wireguard and remove the free_netdev call in wg_destruct.
Thanks for the report.
Jason
PATCH 1/1 net: read dev->needs_free_netdev before potentially freeing dev
In reply to Dan CarpenterIf dev->needs_free_netdev is true, it means that netdev_run_todo should call free_netdev(dev) after it calls dev->priv_destructor. If dev->needs_free_netdev is false, then it means that either dev->priv_destructor is taking care of calling free_netdev(dev), or something else, elsewhere, is doing that. In this case, branching on "if (dev->needs_free_netdev)" after calling dev->priv_destructor is a potential UaF. This patch fixes the issue by reading dev->needs_free_netdev before calling dev->priv_destructor.
---
I believe that the bug Dan reported would easily be fixed as well by just setting dev->needs_free_netdev=true and removing the call to free_netdev(dev) in wg_destruct, in wireguard. If you think that this is the more proper fix -- and that the problem actually isn't this flow in dev.c and any code that might hit this UaF is wrong -- let me know and I'll send in a patch for wireguard instead.
net/core/dev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/core/dev.c +3 -1
@@ -10073,6 +10073,8 @@ void netdev_run_todo(void)while (!list_empty (&list)) {struct net_device *dev = list_first_entry(&list, struct net_device, todo_list); +bool needs_free_netdev = dev->needs_free_netdev; +list_del (&dev->todo_list);if (unlikely (dev->reg_state !=NETREG_UNREGISTERING )) {
@@ -10097,7 +10099,7 @@ void netdev_run_todo(void)#endif if (dev->priv_destructor) dev->priv_destructor (dev); -if (dev->needs_free_netdev) +if (needs_free_netdev)free_netdev (dev);/* Report a network device has been unregistered */
Signature · 1 line
2.28.0
Re: [PATCH net] net: read dev->needs_free_netdev before potentially freeing dev
Quoted 6 lines · Jason A. Donenfeld
I believe that the bug Dan reported would easily be fixed as well by just setting dev->needs_free_netdev=true and removing the call to
free_netdev(dev)inwg_destruct, in wireguard. If you think that this is the more proper fix -- and that the problem actually isn't this flow indev.cand any code that might hit this UaF is wrong -- let me know and I'll send in a patch for wireguard instead.
I think ppp might be hit by the same bug, actually. netdev_run_todo->ppp_dev_priv_destructor()->ppp_destroy_interface()->free_netdev(dev), followed by "if (dev->needs_free_netdev)".
Re: [PATCH net] net: read dev->needs_free_netdev before potentially freeing dev
In reply to Jason A. DonenfeldQuoted 9 lines · Jason A. Donenfeld
If
dev->needs_free_netdevis true, it means thatnetdev_run_todoshould callfree_netdev(dev)after it callsdev->priv_destructor. Ifdev->needs_free_netdevis false, then it means that eitherdev->priv_destructoris taking care of callingfree_netdev(dev), or something else, elsewhere, is doing that. In this case, branching on "if (dev->needs_free_netdev)" after callingdev->priv_destructoris a potential UaF. This patch fixes the issue by readingdev->needs_free_netdevbefore callingdev->priv_destructor.
No, I misread the code. Sorry. This patch is not required. We can use "dev" up to the end of the function where we do:
/* Free network device */ kobject_put(&dev->dev.kobj);
That's where the final reference is released.
regards,
dan carpenter