netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] tun: avoid double free in tun_free_netdev
@ 2021-12-13 17:04 George Kennedy
  2021-12-14  2:11 ` Jakub Kicinski
  0 siblings, 1 reply; 2+ messages in thread
From: George Kennedy @ 2021-12-13 17:04 UTC (permalink / raw)
  To: kuba; +Cc: george.kennedy, stephen, davem, netdev, linux-kernel

Avoid double free in tun_free_netdev() by checking if
dev->reg_state is NETREG_UNREGISTERING in the tun_set_iff()
error paths. If dev->reg_state is NETREG_UNREGISTERING that means
the destructor will be called later.

BUG: KASAN: double-free or invalid-free in selinux_tun_dev_free_security+0x1a/0x20 security/selinux/hooks.c:5605

CPU: 0 PID: 25750 Comm: syz-executor416 Not tainted 5.16.0-rc2-syzk #1
Hardware name: Red Hat KVM, BIOS
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:88 [inline]
 dump_stack_lvl+0x89/0xb5 lib/dump_stack.c:106
 print_address_description.constprop.9+0x28/0x160 mm/kasan/report.c:247
 kasan_report_invalid_free+0x55/0x80 mm/kasan/report.c:372
 ____kasan_slab_free mm/kasan/common.c:346 [inline]
 __kasan_slab_free+0x107/0x120 mm/kasan/common.c:374
 kasan_slab_free include/linux/kasan.h:235 [inline]
 slab_free_hook mm/slub.c:1723 [inline]
 slab_free_freelist_hook mm/slub.c:1749 [inline]
 slab_free mm/slub.c:3513 [inline]
 kfree+0xac/0x2d0 mm/slub.c:4561
 selinux_tun_dev_free_security+0x1a/0x20 security/selinux/hooks.c:5605
 security_tun_dev_free_security+0x4f/0x90 security/security.c:2342
 tun_free_netdev+0xe6/0x150 drivers/net/tun.c:2215
 netdev_run_todo+0x4df/0x840 net/core/dev.c:10627
 rtnl_unlock+0x13/0x20 net/core/rtnetlink.c:112
 __tun_chr_ioctl+0x80c/0x2870 drivers/net/tun.c:3302
 tun_chr_ioctl+0x2f/0x40 drivers/net/tun.c:3311
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:874 [inline]
 __se_sys_ioctl fs/ioctl.c:860 [inline]
 __x64_sys_ioctl+0x19d/0x220 fs/ioctl.c:860
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x3a/0x80 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x44/0xae

Reported-by: syzkaller <syzkaller@googlegroups.com>
Signed-off-by: George Kennedy <george.kennedy@oracle.com>
---
Jakub, decided to go the less code churn route and just
check for dev->reg_state is NETREG_UNREGISTERING.

 drivers/net/tun.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 1572878..9ab530a 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -2206,10 +2206,6 @@ static void tun_free_netdev(struct net_device *dev)
 	BUG_ON(!(list_empty(&tun->disabled)));
 
 	free_percpu(dev->tstats);
-	/* We clear tstats so that tun_set_iff() can tell if
-	 * tun_free_netdev() has been called from register_netdevice().
-	 */
-	dev->tstats = NULL;
 
 	tun_flow_uninit(tun);
 	security_tun_dev_free_security(tun->security);
@@ -2770,18 +2766,15 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 
 err_detach:
 	tun_detach_all(dev);
-	/* We are here because register_netdevice() has failed.
-	 * If register_netdevice() already called tun_free_netdev()
-	 * while dealing with the error, dev->stats has been cleared.
-	 */
-	if (!dev->tstats)
-		goto err_free_dev;
-
 err_free_flow:
-	tun_flow_uninit(tun);
-	security_tun_dev_free_security(tun->security);
+	/* if NETREG_UNREGISTERING, destructor will be called later */
+	if (dev->reg_state != NETREG_UNREGISTERING) {
+		tun_flow_uninit(tun);
+		security_tun_dev_free_security(tun->security);
+	}
 err_free_stat:
-	free_percpu(dev->tstats);
+	if (dev->reg_state != NETREG_UNREGISTERING)
+		free_percpu(dev->tstats);
 err_free_dev:
 	free_netdev(dev);
 	return err;
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] tun: avoid double free in tun_free_netdev
  2021-12-13 17:04 [PATCH v2] tun: avoid double free in tun_free_netdev George Kennedy
@ 2021-12-14  2:11 ` Jakub Kicinski
  0 siblings, 0 replies; 2+ messages in thread
From: Jakub Kicinski @ 2021-12-14  2:11 UTC (permalink / raw)
  To: George Kennedy; +Cc: stephen, davem, netdev, linux-kernel

On Mon, 13 Dec 2021 12:04:11 -0500 George Kennedy wrote:
> Avoid double free in tun_free_netdev() by checking if
> dev->reg_state is NETREG_UNREGISTERING in the tun_set_iff()
> error paths. If dev->reg_state is NETREG_UNREGISTERING that means
> the destructor will be called later.
> 
> BUG: KASAN: double-free or invalid-free in selinux_tun_dev_free_security+0x1a/0x20 security/selinux/hooks.c:5605

> 
> Reported-by: syzkaller <syzkaller@googlegroups.com>
> Signed-off-by: George Kennedy <george.kennedy@oracle.com>
> ---
> Jakub, decided to go the less code churn route and just
> check for dev->reg_state is NETREG_UNREGISTERING.

I don't think this is correct. E.g. if NETDEV_POST_INIT notifier 
fails the destructor will already have been called and reg_state 
will be NETREG_UNINITIALIZED which is != NETREG_UNREGISTERING.

What I had in mind is replacing if (!dev->tstats) goto .. with 
if (reg_state == NETREG_UNREGISTERING) goto ..

But as proven partial destruction on failure of register_netdevice() is
extra tricky, I believe moving the code into ndo_init would be less
error-prone even if higher LoC delta. Unless there's some trickery in
the code move as well, I haven't investigated.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-12-14  2:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-13 17:04 [PATCH v2] tun: avoid double free in tun_free_netdev George Kennedy
2021-12-14  2:11 ` Jakub Kicinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).