diff --git a/ipn/ipnlocal/serve.go b/ipn/ipnlocal/serve.go index 9142e2e62..41180739f 100644 --- a/ipn/ipnlocal/serve.go +++ b/ipn/ipnlocal/serve.go @@ -382,6 +382,10 @@ func (b *LocalBackend) setServeConfigLocked(config *ipn.ServeConfig, etag string return err } + if b.serveTargetsTailscaledSocket(config.View()) { + return errors.New("serving to the tailscaled socket is prohibited for security reasons") + } + var bs []byte if config != nil { j, err := json.Marshal(config) @@ -695,10 +699,19 @@ func (b *LocalBackend) tcpHandlerForServeTCP(tcph ipn.TCPPortHandlerView, dport return func(conn net.Conn) error { defer conn.Close() conn = b.meteredConnForService(conn, forVIPService) + // Never forward to tailscaled's own socket: the local API + // authorizes a unix-socket caller by the peer credentials of the + // dialing process, which here is tailscaled itself, so a forward + // would hand a remote peer a root-authorized local API. + socketPath, isUnix := strings.CutPrefix(backDst, "unix:") + if isUnix && b.isTailscaledSocket(socketPath) { + b.logf("localbackend: refusing TCP proxy on port %v (from %v) to the tailscaled socket %q", dport, srcAddr, socketPath) + return ErrProxyToTailscaledSocket + } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) var backConn net.Conn var err error - if socketPath, ok := strings.CutPrefix(backDst, "unix:"); ok { + if isUnix { var d net.Dialer backConn, err = d.DialContext(ctx, "unix", socketPath) } else { @@ -1755,6 +1768,47 @@ func vipServiceHash(logf logger.Logf, services []*tailcfg.VIPService) string { return hex.EncodeToString(buf[:]) } +// serveTargetsTailscaledSocket reports whether sc has any TCP-forward or web +// handler that proxies to tailscaled's own socket. +// +// The local API authorizes a unix-socket caller by the peer credentials of the +// dialing process, which is tailscaled itself, running as root. Proxying serve +// to it would hand a tailnet peer a root-authorized local API, so it is +// refused -- the same reason proxyHandlerForBackend and the TCP-forward dial +// return ErrProxyToTailscaledSocket. Foreground and VIP-service handlers are as +// exposed as background ones, so all are checked. +func (b *LocalBackend) serveTargetsTailscaledSocket(sc ipn.ServeConfigView) bool { + if !sc.Valid() { + return false + } + isProhibitedSock := func(target string) bool { + p, ok := strings.CutPrefix(target, "unix:") + return ok && b.isTailscaledSocket(p) + } + // Webs covers top-level, foreground and service handlers; TCPs covers + // top-level and foreground but not services, so those are checked below. + for _, wc := range sc.Webs() { + for _, h := range wc.Handlers().All() { + if isProhibitedSock(h.Proxy()) { + return true + } + } + } + for _, h := range sc.TCPs() { + if isProhibitedSock(h.TCPForward()) { + return true + } + } + for _, svc := range sc.Services().All() { + for _, h := range svc.TCP().All() { + if isProhibitedSock(h.TCPForward()) { + return true + } + } + } + return false +} + // validateServeConfigUpdate validates changes proposed by incoming serve // configuration. func validateServeConfigUpdate(existing, incoming ipn.ServeConfigView) error { diff --git a/ipn/ipnlocal/serve_test.go b/ipn/ipnlocal/serve_test.go index ad175ba3a..ee7599c62 100644 --- a/ipn/ipnlocal/serve_test.go +++ b/ipn/ipnlocal/serve_test.go @@ -2213,3 +2213,91 @@ func TestValidateServeConfigUpdate(t *testing.T) { }) } } + +// mkTempSocket creates an empty file to stand in for a unix socket and returns +// its path. isTailscaledSocket matches by os.SameFile, so a regular file is +// enough and its name does not matter. +func mkTempSocket(t *testing.T) string { + t.Helper() + f, err := os.CreateTemp(t.TempDir(), "sock") + if err != nil { + t.Fatal(err) + } + f.Close() + return f.Name() +} + +// TestServeTargetsTailscaledSocket checks the guard that refuses a serve config +// proxying to tailscaled's own socket, across every place a handler can live and +// through a symlink alias. +func TestServeTargetsTailscaledSocket(t *testing.T) { + b := newTestBackend(t) + + sock := mkTempSocket(t) + b.sys.SocketPath = sock + other := mkTempSocket(t) + alias := sock + ".symlink" + if err := os.Symlink(sock, alias); err != nil { + t.Fatal(err) + } + + tcpTo := func(target string) *ipn.ServeConfig { + return &ipn.ServeConfig{TCP: map[uint16]*ipn.TCPPortHandler{443: {TCPForward: target}}} + } + + tests := []struct { + name string + conf *ipn.ServeConfig + want bool + }{ + {"nil", nil, false}, + {"no-unix-tcp", tcpTo("127.0.0.1:8080"), false}, + {"other-socket", tcpTo("unix:" + other), false}, + {"tcp-forward", tcpTo("unix:" + sock), true}, + {"tcp-forward-via-symlink", tcpTo("unix:" + alias), true}, + { + "web-proxy", + &ipn.ServeConfig{Web: map[ipn.HostPort]*ipn.WebServerConfig{ + "example.ts.net:443": {Handlers: map[string]*ipn.HTTPHandler{"/": {Proxy: "unix:" + sock}}}, + }}, + true, + }, + { + "foreground-tcp", + &ipn.ServeConfig{Foreground: map[string]*ipn.ServeConfig{ + "sess": tcpTo("unix:" + sock), + }}, + true, + }, + { + "service-tcp", + &ipn.ServeConfig{Services: map[tailcfg.ServiceName]*ipn.ServiceConfig{ + "svc:foo": {TCP: map[uint16]*ipn.TCPPortHandler{443: {TCPForward: "unix:" + sock}}}, + }}, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := b.serveTargetsTailscaledSocket(tt.conf.View()); got != tt.want { + t.Errorf("serveTargetsTailscaledSocket = %v, want %v", got, tt.want) + } + }) + } +} + +// TestSetServeConfigRejectsTailscaledSocket confirms the guard is wired into +// the config-set path, so "tailscale serve" is refused with a surfaced error +// rather than silently accepted. +func TestSetServeConfigRejectsTailscaledSocket(t *testing.T) { + b := newTestBackend(t) + sock := mkTempSocket(t) + b.sys.SocketPath = sock + + err := b.setServeConfigLocked(&ipn.ServeConfig{ + TCP: map[uint16]*ipn.TCPPortHandler{443: {TCPForward: "unix:" + sock}}, + }, "") + if err == nil { + t.Fatal("setServeConfigLocked accepted a forward to the tailscaled socket; want it refused") + } +}