diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 7fccebd23..a6fc4f3c8 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -6469,6 +6469,19 @@ func (b *LocalBackend) initPeerAPIListenerLocked() { // We don't care about the error here. Not all platforms set this. // If ps.listen needs it, it will check for zero values and error out. tsIfIndex, _ := netmon.TailscaleInterfaceIndex() + if tsIfIndex == 0 { + // On Linux nothing calls netmon.SetTailscaleInterfaceProps, + // so the tun device is the source of truth for the + // interface name. peerapi uses the index to bind its + // listener to the tunnel interface there. + if tun, ok := b.sys.Tun.GetOK(); ok { + if name, err := tun.Name(); err == nil { + if iface, err := net.InterfaceByName(name); err == nil { + tsIfIndex = iface.Index + } + } + } + } ln, err = ps.listen(a.Addr(), tsIfIndex) if err != nil { diff --git a/ipn/ipnlocal/peerapi.go b/ipn/ipnlocal/peerapi.go index 0e31c4370..5451056f2 100644 --- a/ipn/ipnlocal/peerapi.go +++ b/ipn/ipnlocal/peerapi.go @@ -44,8 +44,12 @@ import ( "tailscale.com/wgengine/filter" ) -// initListenConfig, if non-nil, is called during peerAPIListener setup. It is used only -// on iOS and macOS to set socket options to bind the listener to the Tailscale interface. +// initListenConfig, if non-nil, is called during peerAPIListener setup. +// On iOS and macOS, it sets socket options to bind the listener to the +// Tailscale interface. On Linux, it binds the listener to the tunnel +// interface, since peerapi traffic from other nodes is served by +// netstack in userspace and the kernel listener only needs to serve +// the local host. var initListenConfig func(config *net.ListenConfig, addr netip.Addr, tunIfIndex int) error // peerDNSQueryHandler is implemented by tsdns.Resolver. @@ -73,13 +77,9 @@ func (s *peerAPIServer) listen(ip netip.Addr, tunIfIndex int) (ln net.Listener, if initListenConfig != nil { // On iOS/macOS, this sets the lc.Control hook to // setsockopt the interface index to bind to, to get - // out of the network sandbox. - - // A zero tunIfIndex is invalid for peerapi. A zero value will not get us - // out of the network sandbox. Caller should log and retry. - if tunIfIndex == 0 { - return nil, fmt.Errorf("peerapi: cannot listen on %s with tunIfIndex 0", ipStr) - } + // out of the network sandbox. On Linux, it binds the + // listener to the tunnel interface; see + // initListenConfigTun. if err := initListenConfig(&lc, ip, tunIfIndex); err != nil { return nil, err diff --git a/ipn/ipnlocal/peerapi_linux.go b/ipn/ipnlocal/peerapi_linux.go new file mode 100644 index 000000000..98d4f5f6e --- /dev/null +++ b/ipn/ipnlocal/peerapi_linux.go @@ -0,0 +1,45 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build linux && !android + +package ipnlocal + +import ( + "net" + "net/netip" + + "tailscale.com/net/netns" +) + +func init() { + initListenConfig = initListenConfigTun +} + +// initListenConfigTun binds the peerapi listener to the tunnel interface +// with SO_BINDTODEVICE, for consistency with the other platforms: macOS +// and iOS bind the same listener to the tunnel interface with +// IP_BOUND_IF, and Windows is protected by its strong host model. +// Unlike those, Linux accepts a packet addressed to the node's own +// Tailscale IP from any interface it arrives on, so the socket option +// is what makes the bind consistent here. +// +// No legitimate traffic is lost. Connections to the node's Tailscale +// IP from the local host are delivered via the tunnel interface, peer +// connections arrive that way in builds without userspace netstack, +// and with netstack compiled in (the usual case) peer connections are +// terminated in userspace and never reach the kernel listener at all. +// In netstack mode there is no tunnel interface and the listener binds +// all addresses; the only connections that reach it are loopback dials +// from netstack and from the local host, so loopback is the right +// device there. +func initListenConfigTun(config *net.ListenConfig, addr netip.Addr, tunIfIndex int) error { + device := "lo" + if tunIfIndex != 0 { + iface, err := net.InterfaceByIndex(tunIfIndex) + if err == nil { + device = iface.Name + } + } + return netns.SetListenConfigInterfaceName(config, device) +} diff --git a/ipn/ipnlocal/peerapi_macios_ext.go b/ipn/ipnlocal/peerapi_macios_ext.go index 70c1fb850..26a599aaf 100644 --- a/ipn/ipnlocal/peerapi_macios_ext.go +++ b/ipn/ipnlocal/peerapi_macios_ext.go @@ -6,6 +6,7 @@ package ipnlocal import ( + "fmt" "net" "net/netip" @@ -20,5 +21,10 @@ func init() { // through the iOS/macOS Network/System Extension (Packet Tunnel // Provider) sandbox. func initListenConfigNetworkExtension(nc *net.ListenConfig, ip netip.Addr, ifaceIndex int) error { + // A zero ifaceIndex is invalid for peerapi. A zero value will not get us + // out of the network sandbox. Caller should log and retry. + if ifaceIndex == 0 { + return fmt.Errorf("peerapi: cannot listen on %s with ifaceIndex 0", ip) + } return netns.SetListenConfigInterfaceIndex(nc, ifaceIndex) } diff --git a/net/netns/netns_linux.go b/net/netns/netns_linux.go index 02b2dd89b..c9ea740a5 100644 --- a/net/netns/netns_linux.go +++ b/net/netns/netns_linux.go @@ -6,7 +6,9 @@ package netns import ( + "errors" "fmt" + "log" "net" "os" "sync" @@ -131,3 +133,35 @@ func bindToDevice(fd uintptr) error { } return nil } + +// SetListenConfigInterfaceName sets lc.Control so that sockets created +// with it are bound to the named interface using SO_BINDTODEVICE, and +// thus only receive packets that arrived on that interface. It is the +// Linux counterpart of darwin's SetListenConfigInterfaceIndex. +// +// Failures to set the socket option are logged and otherwise ignored: +// the socket then simply listens as it would have without this call. +// Setting SO_BINDTODEVICE requires CAP_NET_RAW, and hardening must not +// take out the caller's listener on systems where that is unavailable. +func SetListenConfigInterfaceName(lc *net.ListenConfig, ifName string) error { + if lc == nil { + return errors.New("nil ListenConfig") + } + if lc.Control != nil { + return errors.New("ListenConfig.Control already set") + } + lc.Control = func(network, address string, c syscall.RawConn) error { + var sockErr error + err := c.Control(func(fd uintptr) { + sockErr = unix.SetsockoptString(int(fd), unix.SOL_SOCKET, unix.SO_BINDTODEVICE, ifName) + }) + if sockErr != nil { + log.Printf("[unexpected] netns: SO_BINDTODEVICE(%q) on %v %v: %v", ifName, network, address, sockErr) + } + if err != nil { + return fmt.Errorf("RawConn.Control on %T: %w", c, err) + } + return nil + } + return nil +} diff --git a/net/netns/netns_linux_test.go b/net/netns/netns_linux_test.go index e467ee414..48d8e40c4 100644 --- a/net/netns/netns_linux_test.go +++ b/net/netns/netns_linux_test.go @@ -4,7 +4,12 @@ package netns import ( + "context" + "net" + "os" "testing" + + "golang.org/x/sys/unix" ) func TestSocketMarkWorks(t *testing.T) { @@ -12,3 +17,44 @@ func TestSocketMarkWorks(t *testing.T) { // we cannot actually assert whether the test runner has SO_MARK available // or not, as we don't know. We're just checking that it doesn't panic. } + +func TestSetListenConfigInterfaceName(t *testing.T) { + // Setting SO_BINDTODEVICE requires CAP_NET_RAW, which the test + // runner has when running as root and may or may not have otherwise. + if os.Geteuid() != 0 { + t.Skip("skipping; setting SO_BINDTODEVICE requires CAP_NET_RAW") + } + + var lc net.ListenConfig + if err := SetListenConfigInterfaceName(&lc, "lo"); err != nil { + t.Fatal(err) + } + ln, err := lc.Listen(context.Background(), "tcp4", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer ln.Close() + + tcpLn, ok := ln.(*net.TCPListener) + if !ok { + t.Fatalf("got listener of type %T, want *net.TCPListener", ln) + } + rc, err := tcpLn.SyscallConn() + if err != nil { + t.Fatal(err) + } + var got string + var sockErr error + err = rc.Control(func(fd uintptr) { + got, sockErr = unix.GetsockoptString(int(fd), unix.SOL_SOCKET, unix.SO_BINDTODEVICE) + }) + if err != nil { + t.Fatalf("RawConn.Control: %v", err) + } + if sockErr != nil { + t.Fatalf("getsockopt SO_BINDTODEVICE: %v", sockErr) + } + if got != "lo" { + t.Errorf("SO_BINDTODEVICE = %q, want %q", got, "lo") + } +}