Files
tailscale/ipn/ipnlocal/serve_unix_linux_test.go
Sam JeanandJames Tucker 3a38840ccc ipn/ipnlocal: check the tailscaled socket on every serve unix dial
The unix-socket reverse proxy checked its backend against the tailscaled
socket only when the handler was built, but each backend connection
re-resolved the path from scratch. A backend that was benign at config
time could later be replaced with a symlink to the tailscaled socket by
anyone able to write to its directory, and the next dial followed it.
The proxy dials as root, so the same swap could redirect a serve or
funnel backend at any root-only unix socket on the host.

Move the check to the dial itself, in a dialServeUnixSocket method
shared by the HTTP transport, the h2c transport and the TCP-forward
handler, all of which previously dialed unix on their own. It refuses
tailscaled's socket with ErrProxyToTailscaledSocket immediately before
connecting.

On Linux 5.6 and newer, the dial pins the resolved socket inode with
openat2(2): an O_PATH descriptor opened with RESOLVE_NO_MAGICLINKS,
and RESOLVE_BENEATH under the socket's parent directory where the path
allows it, or by whole path otherwise. The pinned file is checked
against the tailscaled socket, and the connection is made to the pinned
file through /proc/self/fd, so replacing the path cannot retarget the
connect. On kernels without openat2(2), and on other platforms, the
path is dialed directly.

The connected peer is re-checked after the dial on every platform. The
peer address of a path-bound listener is the path the socket was bound
to, not the path that was dialed, so a symlink swapped in before the
dial is identified after the connect. This check is best-effort:
connections that have no determinable peer name are allowed.

Fixes tailscale/corp#50481

Change-Id: Iae5db207519db9c139a512c0427dce3dfa3ca672
Co-authored-by: James Tucker <[email protected]>
Signed-off-by: Sam Jean <[email protected]>
Signed-off-by: James Tucker <[email protected]>
2026-10-27 13:08:08 -07:00

182 lines
4.8 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build linux && !ts_omit_serve
package ipnlocal
import (
"bytes"
"context"
"io"
"os"
"path/filepath"
"strconv"
"syscall"
"testing"
"golang.org/x/sys/unix"
)
// openat2Supported reports whether the kernel supports openat2(2)
// (Linux 5.6 and newer).
func openat2Supported() bool {
fd, err := unix.Openat2(unix.AT_FDCWD, "/", &unix.OpenHow{Flags: unix.O_PATH | unix.O_CLOEXEC})
if err == nil {
unix.Close(fd)
return true
}
return false
}
// TestPinUnixSocketPathResistsSymlinkSwap checks that replacing the dial
// path with a symlink after pinning does not change the pinned descriptor,
// and that a later pin of the swapped path refers to the tailscaled socket.
func TestPinUnixSocketPathResistsSymlinkSwap(t *testing.T) {
if !openat2Supported() {
t.Skip("kernel does not support openat2(2); the dial falls back to the pre/post checks")
}
dir := unixSocketTestDir(t)
tsSock := filepath.Join(dir, "tailscaled.sock")
mustListenUnix(t, tsSock)
backend := filepath.Join(dir, "backend.sock")
serveUnixEchoListener(t, backend)
b := newTestBackend(t)
b.sys.SocketPath = tsSock
// Backend behind a sibling symlink.
alias := filepath.Join(dir, "alias.sock")
if err := os.Symlink("backend.sock", alias); err != nil {
t.Fatal(err)
}
fd, ok := pinUnixSocketPath(alias)
if !ok {
t.Fatalf("pinUnixSocketPath(%q) did not pin", alias)
}
defer unix.Close(fd)
if b.isTailscaledSocketFd(fd) {
t.Fatal("pin of the benign alias matches the tailscaled socket")
}
// Replace the path with a symlink to the tailscaled socket after the pin.
if err := os.Remove(alias); err != nil {
t.Fatal(err)
}
if err := os.Symlink("tailscaled.sock", alias); err != nil {
t.Fatal(err)
}
// The pinned descriptor is unchanged...
if b.isTailscaledSocketFd(fd) {
t.Fatal("pin followed the swap onto the tailscaled socket")
}
// ...and the connect through it reaches the benign backend, not the
// socket the path now leads to.
c, err := dialPinnedUnixSocket(context.Background(), fd)
if err != nil {
t.Fatalf("dial through the pinned descriptor: %v", err)
}
defer c.Close()
msg := []byte("hello backend")
if _, err := c.Write(msg); err != nil {
t.Fatal(err)
}
got := make([]byte, len(msg))
if _, err := io.ReadFull(c, got); err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, msg) {
t.Fatalf("echo through pin = %q; want %q", got, msg)
}
// A new pin of the swapped path is the tailscaled socket.
fd, ok = pinUnixSocketPath(alias)
if !ok {
t.Fatalf("pinUnixSocketPath(%q) on the swapped path did not pin", alias)
}
defer unix.Close(fd)
if !b.isTailscaledSocketFd(fd) {
t.Fatal("pin of the swapped alias is not the tailscaled socket")
}
}
// TestPinUnixSocketPathAbsoluteSymlinkBackend checks that a backend behind
// an absolute symlink, which RESOLVE_BENEATH cannot resolve, is still
// pinned and dialable.
func TestPinUnixSocketPathAbsoluteSymlinkBackend(t *testing.T) {
if !openat2Supported() {
t.Skip("kernel does not support openat2(2); the dial falls back to the pre/post checks")
}
dir := unixSocketTestDir(t)
backend := filepath.Join(dir, "backend.sock")
serveUnixEchoListener(t, backend)
alias := filepath.Join(dir, "alias.sock")
if err := os.Symlink(backend, alias); err != nil {
t.Fatal(err)
}
fd, ok := pinUnixSocketPath(alias)
if !ok {
t.Fatalf("pinUnixSocketPath(%q) did not pin", alias)
}
defer unix.Close(fd)
c, err := dialPinnedUnixSocket(context.Background(), fd)
if err != nil {
t.Fatalf("dial through the pinned descriptor: %v", err)
}
defer c.Close()
msg := []byte("hello backend")
if _, err := c.Write(msg); err != nil {
t.Fatal(err)
}
got := make([]byte, len(msg))
if _, err := io.ReadFull(c, got); err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, msg) {
t.Fatalf("echo through pin = %q; want %q", got, msg)
}
}
// TestPinUnixSocketPathRefusesMagicLinks checks that a path that is a magic
// link (here, /proc/self/fd of the listener) is not pinned.
func TestPinUnixSocketPathRefusesMagicLinks(t *testing.T) {
if !openat2Supported() {
t.Skip("kernel does not support openat2(2); the dial falls back to the pre/post checks")
}
dir := unixSocketTestDir(t)
tsSock := filepath.Join(dir, "tailscaled.sock")
ln := mustListenUnix(t, tsSock)
sc, ok := ln.(syscall.Conn)
if !ok {
t.Fatalf("%T does not expose its descriptor", ln)
}
rc, err := sc.SyscallConn()
if err != nil {
t.Fatal(err)
}
var lnFD int
if err := rc.Control(func(fd uintptr) { lnFD = int(fd) }); err != nil {
t.Fatal(err)
}
magic := filepath.Join(dir, "magic.sock")
if err := os.Symlink("/proc/self/fd/"+strconv.Itoa(lnFD), magic); err != nil {
t.Fatal(err)
}
if fd, ok := pinUnixSocketPath(magic); ok {
unix.Close(fd)
t.Fatal("pinUnixSocketPath pinned through a magic link; want it refused")
}
}