ipn/ipnlocal: refuse serving TCP to the tailscaled socket (#21966)

tailscale serve --tcp=<port> unix:<path> dialed the target socket with
no check, so pointing it at tailscaled's own LocalAPI socket proxied a
tailnet peer straight into the LocalAPI. The LocalAPI authorizes a
unix-socket caller by the peer credentials of the dialing process --
tailscaled itself, as root -- so the peer got a write-capable LocalAPI
and could change the node's settings.

The HTTP proxy path already refused this via isTailscaledSocket; the
TCP-forward path, added later, did not. Reject it at config-set time in
setServeConfigLocked so "tailscale serve" fails up front, and keep a
dial-site guard returning ErrProxyToTailscaledSocket as a backstop for
configs set by other means.

RELNOTE: tailscale serve no longer proxies to tailscaled's own socket

Fixes tailscale/corp#50425

Change-Id: I5ed02e83d32bdfb09c8164907bf38cabcc406169

Signed-off-by: Sam Jean <[email protected]>
(cherry picked from commit 713be260c3)
This commit is contained in:
Sam Jean authored and James Tucker committed 2026-10-27 20:14:22 +00:00
1 parent 1fb2bb81e6
commit 21b7a44c44
2 files changed
+143 -1

No files matched your search

+55 -1
View File
@@ -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 {
+88
View File
@@ -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")
}
}