Compare commits

...

2 Commits

Author SHA1 Message Date
JackDoan bc04261d0b linux: let the kernel resolve the tun.dev %d template atomically
Resolving the template in userspace (LinkList then TUNSETIFF) races with
concurrent instances: both can pick nebula0, and with multiqueue the loser
silently attaches as a second queue to the winner's device instead of
failing. TUNSETIFF natively substitutes a single %d via dev_alloc_name,
atomically and always allocating a fresh device, so keep the fail-fast
validation but pass the template through and read the resolved name back.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Fg7oD3uZ4jZjjQ4gRsPAwB
2026-07-09 18:37:30 -05:00
JackDoan 57e1a9b6af linux: allow %d anywhere in the tun.dev template 2026-07-08 13:43:43 -05:00
3 changed files with 81 additions and 5 deletions
+4
View File
@@ -242,6 +242,10 @@ tun:
# When tun is disabled, a lighthouse can be started without a local tun interface (and therefore without root) # When tun is disabled, a lighthouse can be started without a local tun interface (and therefore without root)
disabled: false disabled: false
# Name of the device. If not set, a default will be chosen by the OS. # Name of the device. If not set, a default will be chosen by the OS.
# For Linux: a single `%d` anywhere in the name is treated as a template and replaced with the
# lowest number that yields an unused device name (e.g. `nebula%d` becomes `nebula0`, then `nebula1`, and so on, `neb%dprod` becomes `neb0prod`).
# Only on Linux: `nebula%d` is the default if tun.dev is unset.
# The name, both before and after %d substitution, must be shorter than the kernel limit of 16 characters.
# For macOS: if set, must be in the form `utun[0-9]+`. # For macOS: if set, must be in the form `utun[0-9]+`.
# For NetBSD: Required to be set, must be in the form `tun[0-9]+` # For NetBSD: Required to be set, must be in the form `tun[0-9]+`
dev: nebula1 dev: nebula1
+35 -4
View File
@@ -5,6 +5,7 @@ package overlay
import ( import (
"encoding/binary" "encoding/binary"
"errors"
"fmt" "fmt"
"io" "io"
"log/slog" "log/slog"
@@ -250,6 +251,17 @@ func newTunFromFd(c *config.C, l *slog.Logger, deviceFd int, vpnNetworks []netip
} }
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, multiqueue bool) (*tun, error) { func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, multiqueue bool) (*tun, error) {
// Validate the device name up front so a bad tun.dev fails fast, before we
// open /dev/net/tun or leak a file descriptor. A single %d in the name is
// substituted by the kernel during TUNSETIFF (dev_alloc_name) with the
// lowest number that yields an unused device name. Resolving the template
// in the kernel keeps the pick-a-name/create-the-device pair atomic, so
// concurrent callers can never race each other to the same name.
tunName := c.GetString("tun.dev", "nebula%d")
if err := validateTunName(tunName); err != nil {
return nil, err
}
fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0) fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
if err != nil { if err != nil {
// If /dev/net/tun doesn't exist, try to create it (will happen in docker) // If /dev/net/tun doesn't exist, try to create it (will happen in docker)
@@ -277,12 +289,11 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, multiqueue
if multiqueue { if multiqueue {
req.Flags |= unix.IFF_MULTI_QUEUE req.Flags |= unix.IFF_MULTI_QUEUE
} }
nameStr := c.GetString("tun.dev", "") copy(req.Name[:], tunName)
copy(req.Name[:], nameStr)
if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil { if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil {
_ = unix.Close(fd) _ = unix.Close(fd)
return nil, &NameError{ return nil, &NameError{
Name: nameStr, Name: tunName,
Underlying: err, Underlying: err,
} }
} }
@@ -298,6 +309,27 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, multiqueue
return t, nil return t, nil
} }
func validateTunName(tunName string) error {
if !strings.Contains(tunName, "%d") {
if len(tunName) >= unix.IFNAMSIZ {
return fmt.Errorf("tun.dev %q is not shorter than the maximum device name length of %d", tunName, unix.IFNAMSIZ)
}
return nil
}
if strings.Count(tunName, "%d") > 1 {
return fmt.Errorf("tun.dev template %q may only contain a single %%d", tunName)
}
if tunName == "%d" {
return errors.New("please don't name your tun device '%d'")
}
// The kernel substitutes the %d itself and requires the template, like a
// literal name, to be NUL-terminated within IFNAMSIZ bytes.
if len(tunName) >= unix.IFNAMSIZ {
return fmt.Errorf("tun.dev template %q is not shorter than the maximum device name length of %d", tunName, unix.IFNAMSIZ)
}
return nil
}
// newTunGeneric does all the stuff common to different tun initialization paths. It will close your files on error. // newTunGeneric does all the stuff common to different tun initialization paths. It will close your files on error.
func newTunGeneric(c *config.C, l *slog.Logger, fd int, vpnNetworks []netip.Prefix) (*tun, error) { func newTunGeneric(c *config.C, l *slog.Logger, fd int, vpnNetworks []netip.Prefix) (*tun, error) {
tfd, err := newTunFd(fd) tfd, err := newTunFd(fd)
@@ -768,7 +800,6 @@ func (t *tun) isGatewayInVpnNetworks(gwAddr netip.Addr) bool {
func (t *tun) getGatewaysFromRoute(r *netlink.Route) routing.Gateways { func (t *tun) getGatewaysFromRoute(r *netlink.Route) routing.Gateways {
var gateways routing.Gateways var gateways routing.Gateways
link, err := netlink.LinkByName(t.Device) link, err := netlink.LinkByName(t.Device)
if err != nil { if err != nil {
t.l.Error("Ignoring route update: failed to get link by name", "deviceName", t.Device) t.l.Error("Ignoring route update: failed to get link by name", "deviceName", t.Device)
+42 -1
View File
@@ -3,7 +3,12 @@
package overlay package overlay
import "testing" import (
"strings"
"testing"
"golang.org/x/sys/unix"
)
var runAdvMSSTests = []struct { var runAdvMSSTests = []struct {
name string name string
@@ -32,3 +37,39 @@ func TestTunAdvMSS(t *testing.T) {
}) })
} }
} }
func TestValidateTunName(t *testing.T) {
// A device name must be shorter than IFNAMSIZ (i.e. IFNAMSIZ-1 chars max).
maxLenName := strings.Repeat("a", unix.IFNAMSIZ-1)
tests := []struct {
name string
tmpl string
wantErr bool
}{
{"short literal name is fine", "nebula1", false},
{"literal name at the max length is fine", maxLenName, false},
{"literal name at IFNAMSIZ is rejected", strings.Repeat("a", unix.IFNAMSIZ), true},
{"trailing template is fine", "nebula%d", false},
{"mid-string template is fine", "neb%dprod", false},
{"leading template is fine", "%dnebula", false},
{"template at the max length is fine", strings.Repeat("a", unix.IFNAMSIZ-3) + "%d", false},
{"template at IFNAMSIZ is rejected", strings.Repeat("a", unix.IFNAMSIZ-2) + "%d", true},
{"bare %d is rejected", "%d", true},
{"multiple %d is rejected", "neb%d%dprod", true},
{"over-long template is rejected", strings.Repeat("a", unix.IFNAMSIZ-1) + "%d", true},
{"over-long mid-string template is rejected", "neb%d" + strings.Repeat("a", unix.IFNAMSIZ-3), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateTunName(tt.tmpl)
if tt.wantErr && err == nil {
t.Fatalf("expected an error for %q, got none", tt.tmpl)
}
if !tt.wantErr && err != nil {
t.Fatalf("unexpected error for %q: %v", tt.tmpl, err)
}
})
}
}