Commit f85ba78068ac137fe9c1f50d25405d2783d75c77

Authored by David Woodhouse
Committed by David S. Miller
1 parent 0456b4f8b7

tun: add IFF_TUN_EXCL flag to avoid opening a persistent device.

When creating a certain types of VPN, NetworkManager will first attempt
to find an available tun device by iterating through 'vpn%d' until it
finds one that isn't already busy. Then it'll set that to be persistent
and owned by the otherwise unprivileged user that the VPN dæmon itself
runs as.

There's a race condition here -- during the period where the vpn%d
device is created and we're waiting for the VPN dæmon to actually
connect and use it, if we try to create _another_ device we could end up
re-using the same one -- because trying to open it again doesn't get
-EBUSY as it would while it's _actually_ busy.

So solve this, we add an IFF_TUN_EXCL flag which causes tun_set_iff() to
fail if it would be opening an existing persistent tundevice -- so that
we can make sure we're getting an entirely _new_ device.

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Showing 2 changed files with 3 additions and 0 deletions Side-by-side Diff

... ... @@ -874,6 +874,8 @@
874 874  
875 875 dev = __dev_get_by_name(net, ifr->ifr_name);
876 876 if (dev) {
  877 + if (ifr->ifr_flags & IFF_TUN_EXCL)
  878 + return -EBUSY;
877 879 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
878 880 tun = netdev_priv(dev);
879 881 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
include/linux/if_tun.h
... ... @@ -55,6 +55,7 @@
55 55 #define IFF_NO_PI 0x1000
56 56 #define IFF_ONE_QUEUE 0x2000
57 57 #define IFF_VNET_HDR 0x4000
  58 +#define IFF_TUN_EXCL 0x8000
58 59  
59 60 /* Features for GSO (TUNSETOFFLOAD). */
60 61 #define TUN_F_CSUM 0x01 /* You can hand me unchecksummed packets. */