Skip to content

Commit 3cb5ae7

Browse files
gkennedy12gregkh
authored andcommitted
tun: avoid double free in tun_free_netdev
commit 158b515 upstream. Avoid double free in tun_free_netdev() by moving the dev->tstats and tun->security allocs to a new ndo_init routine (tun_net_init()) that will be called by register_netdevice(). ndo_init is paired with the desctructor (tun_free_netdev()), so if there's an error in register_netdevice() the destructor will handle the frees. 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 <[email protected]> Signed-off-by: George Kennedy <[email protected]> Suggested-by: Jakub Kicinski <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 03d00f7 commit 3cb5ae7

File tree

1 file changed

+59
-56
lines changed

1 file changed

+59
-56
lines changed

drivers/net/tun.c

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,19 @@ struct tun_struct {
209209
struct tun_prog __rcu *steering_prog;
210210
struct tun_prog __rcu *filter_prog;
211211
struct ethtool_link_ksettings link_ksettings;
212+
/* init args */
213+
struct file *file;
214+
struct ifreq *ifr;
212215
};
213216

214217
struct veth {
215218
__be16 h_vlan_proto;
216219
__be16 h_vlan_TCI;
217220
};
218221

222+
static void tun_flow_init(struct tun_struct *tun);
223+
static void tun_flow_uninit(struct tun_struct *tun);
224+
219225
static int tun_napi_receive(struct napi_struct *napi, int budget)
220226
{
221227
struct tun_file *tfile = container_of(napi, struct tun_file, napi);
@@ -953,6 +959,49 @@ static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
953959

954960
static const struct ethtool_ops tun_ethtool_ops;
955961

962+
static int tun_net_init(struct net_device *dev)
963+
{
964+
struct tun_struct *tun = netdev_priv(dev);
965+
struct ifreq *ifr = tun->ifr;
966+
int err;
967+
968+
dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
969+
if (!dev->tstats)
970+
return -ENOMEM;
971+
972+
spin_lock_init(&tun->lock);
973+
974+
err = security_tun_dev_alloc_security(&tun->security);
975+
if (err < 0) {
976+
free_percpu(dev->tstats);
977+
return err;
978+
}
979+
980+
tun_flow_init(tun);
981+
982+
dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
983+
TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
984+
NETIF_F_HW_VLAN_STAG_TX;
985+
dev->features = dev->hw_features | NETIF_F_LLTX;
986+
dev->vlan_features = dev->features &
987+
~(NETIF_F_HW_VLAN_CTAG_TX |
988+
NETIF_F_HW_VLAN_STAG_TX);
989+
990+
tun->flags = (tun->flags & ~TUN_FEATURES) |
991+
(ifr->ifr_flags & TUN_FEATURES);
992+
993+
INIT_LIST_HEAD(&tun->disabled);
994+
err = tun_attach(tun, tun->file, false, ifr->ifr_flags & IFF_NAPI,
995+
ifr->ifr_flags & IFF_NAPI_FRAGS, false);
996+
if (err < 0) {
997+
tun_flow_uninit(tun);
998+
security_tun_dev_free_security(tun->security);
999+
free_percpu(dev->tstats);
1000+
return err;
1001+
}
1002+
return 0;
1003+
}
1004+
9561005
/* Net device detach from fd. */
9571006
static void tun_net_uninit(struct net_device *dev)
9581007
{
@@ -1169,6 +1218,7 @@ static int tun_net_change_carrier(struct net_device *dev, bool new_carrier)
11691218
}
11701219

11711220
static const struct net_device_ops tun_netdev_ops = {
1221+
.ndo_init = tun_net_init,
11721222
.ndo_uninit = tun_net_uninit,
11731223
.ndo_open = tun_net_open,
11741224
.ndo_stop = tun_net_close,
@@ -1252,6 +1302,7 @@ static int tun_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
12521302
}
12531303

12541304
static const struct net_device_ops tap_netdev_ops = {
1305+
.ndo_init = tun_net_init,
12551306
.ndo_uninit = tun_net_uninit,
12561307
.ndo_open = tun_net_open,
12571308
.ndo_stop = tun_net_close,
@@ -1292,7 +1343,7 @@ static void tun_flow_uninit(struct tun_struct *tun)
12921343
#define MAX_MTU 65535
12931344

12941345
/* Initialize net device. */
1295-
static void tun_net_init(struct net_device *dev)
1346+
static void tun_net_initialize(struct net_device *dev)
12961347
{
12971348
struct tun_struct *tun = netdev_priv(dev);
12981349

@@ -2206,11 +2257,6 @@ static void tun_free_netdev(struct net_device *dev)
22062257
BUG_ON(!(list_empty(&tun->disabled)));
22072258

22082259
free_percpu(dev->tstats);
2209-
/* We clear tstats so that tun_set_iff() can tell if
2210-
* tun_free_netdev() has been called from register_netdevice().
2211-
*/
2212-
dev->tstats = NULL;
2213-
22142260
tun_flow_uninit(tun);
22152261
security_tun_dev_free_security(tun->security);
22162262
__tun_set_ebpf(tun, &tun->steering_prog, NULL);
@@ -2716,41 +2762,16 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
27162762
tun->rx_batched = 0;
27172763
RCU_INIT_POINTER(tun->steering_prog, NULL);
27182764

2719-
dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2720-
if (!dev->tstats) {
2721-
err = -ENOMEM;
2722-
goto err_free_dev;
2723-
}
2724-
2725-
spin_lock_init(&tun->lock);
2726-
2727-
err = security_tun_dev_alloc_security(&tun->security);
2728-
if (err < 0)
2729-
goto err_free_stat;
2730-
2731-
tun_net_init(dev);
2732-
tun_flow_init(tun);
2765+
tun->ifr = ifr;
2766+
tun->file = file;
27332767

2734-
dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
2735-
TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
2736-
NETIF_F_HW_VLAN_STAG_TX;
2737-
dev->features = dev->hw_features | NETIF_F_LLTX;
2738-
dev->vlan_features = dev->features &
2739-
~(NETIF_F_HW_VLAN_CTAG_TX |
2740-
NETIF_F_HW_VLAN_STAG_TX);
2741-
2742-
tun->flags = (tun->flags & ~TUN_FEATURES) |
2743-
(ifr->ifr_flags & TUN_FEATURES);
2744-
2745-
INIT_LIST_HEAD(&tun->disabled);
2746-
err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI,
2747-
ifr->ifr_flags & IFF_NAPI_FRAGS, false);
2748-
if (err < 0)
2749-
goto err_free_flow;
2768+
tun_net_initialize(dev);
27502769

27512770
err = register_netdevice(tun->dev);
2752-
if (err < 0)
2753-
goto err_detach;
2771+
if (err < 0) {
2772+
free_netdev(dev);
2773+
return err;
2774+
}
27542775
/* free_netdev() won't check refcnt, to avoid race
27552776
* with dev_put() we need publish tun after registration.
27562777
*/
@@ -2767,24 +2788,6 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
27672788

27682789
strcpy(ifr->ifr_name, tun->dev->name);
27692790
return 0;
2770-
2771-
err_detach:
2772-
tun_detach_all(dev);
2773-
/* We are here because register_netdevice() has failed.
2774-
* If register_netdevice() already called tun_free_netdev()
2775-
* while dealing with the error, dev->stats has been cleared.
2776-
*/
2777-
if (!dev->tstats)
2778-
goto err_free_dev;
2779-
2780-
err_free_flow:
2781-
tun_flow_uninit(tun);
2782-
security_tun_dev_free_security(tun->security);
2783-
err_free_stat:
2784-
free_percpu(dev->tstats);
2785-
err_free_dev:
2786-
free_netdev(dev);
2787-
return err;
27882791
}
27892792

27902793
static void tun_get_iff(struct tun_struct *tun, struct ifreq *ifr)

0 commit comments

Comments
 (0)