Files
tailscale/safesocket/pipe_windows_test.go
Nick Khyl 2917ea8d0e ipn/ipnauth, safesocket: defer named pipe client's token retrieval until ipnserver needs it
An error returned by net.Listener.Accept() causes the owning http.Server to shut down.
With the deprecation of net.Error.Temporary(), there's no way for the http.Server to test
whether the returned error is temporary / retryable or not (see golang/go#66252).

Because of that, errors returned by (*safesocket.winIOPipeListener).Accept() cause the LocalAPI
server (aka ipnserver.Server) to shut down, and tailscaled process to exit.

While this might be acceptable in the case of non-recoverable errors, such as programmer errors,
we shouldn't shut down the entire tailscaled process for client- or connection-specific errors,
such as when we couldn't obtain the client's access token because the client attempts to connect
at the Anonymous impersonation level. Instead, the LocalAPI server should gracefully handle
these errors by denying access and returning a 401 Unauthorized to the client.

In tailscale/tscert#15, we fixed a known bug where Caddy and other apps using tscert would attempt
to connect at the Anonymous impersonation level and fail. However, we should also fix this on the tailscaled
side to prevent a potential DoS, where a local app could deliberately open the Tailscale LocalAPI named pipe
at the Anonymous impersonation level and cause tailscaled to exit.

In this PR, we defer token retrieval until (*WindowsClientConn).Token() is called and propagate the returned token
or error via ipnauth.GetConnIdentity() to ipnserver, which handles it the same way as other ipnauth-related errors.

Fixes #18212
Fixes tailscale/tscert#13

Signed-off-by: Nick Khyl <nickk@tailscale.com>
2025-12-23 14:04:45 -06:00

113 lines
2.4 KiB
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package safesocket
import (
"fmt"
"testing"
"tailscale.com/util/winutil"
)
func init() {
// downgradeSDDL is a test helper that downgrades the windowsSDDL variable if
// the currently running user does not have sufficient priviliges to set the
// SDDL.
downgradeSDDL = func() (cleanup func()) {
// The current default descriptor can not be set by mere mortal users,
// so we need to undo that for executing tests as a regular user.
if !winutil.IsCurrentProcessElevated() {
var orig string
orig, windowsSDDL = windowsSDDL, ""
return func() { windowsSDDL = orig }
}
return func() {}
}
}
// TestExpectedWindowsTypes is a copy of TestBasics specialized for Windows with
// type assertions about the types of listeners and conns we expect.
func TestExpectedWindowsTypes(t *testing.T) {
t.Cleanup(downgradeSDDL())
const sock = `\\.\pipe\tailscale-test`
ln, err := Listen(sock)
if err != nil {
t.Fatal(err)
}
if got, want := fmt.Sprintf("%T", ln), "*safesocket.winIOPipeListener"; got != want {
t.Errorf("got listener type %q; want %q", got, want)
}
errs := make(chan error, 2)
go func() {
s, err := ln.Accept()
if err != nil {
errs <- err
return
}
ln.Close()
wcc, ok := s.(*WindowsClientConn)
if !ok {
s.Close()
errs <- fmt.Errorf("accepted type %T; want WindowsClientConn", s)
return
}
if wcc.winioPipeConn.Fd() == 0 {
t.Error("accepted conn had unexpected zero fd")
}
tok, err := wcc.Token()
if err != nil {
t.Errorf("failed to retrieve client token: %v", err)
}
if tok == 0 {
t.Error("accepted conn had unexpected zero token")
}
tok.Close()
s.Write([]byte("hello"))
b := make([]byte, 1024)
n, err := s.Read(b)
if err != nil {
errs <- err
return
}
t.Logf("server read %d bytes.", n)
if string(b[:n]) != "world" {
errs <- fmt.Errorf("got %#v, expected %#v\n", string(b[:n]), "world")
return
}
s.Close()
errs <- nil
}()
go func() {
c, err := Connect(sock)
if err != nil {
errs <- err
return
}
c.Write([]byte("world"))
b := make([]byte, 1024)
n, err := c.Read(b)
if err != nil {
errs <- err
return
}
if string(b[:n]) != "hello" {
errs <- fmt.Errorf("got %#v, expected %#v\n", string(b[:n]), "hello")
}
c.Close()
errs <- nil
}()
for range 2 {
if err := <-errs; err != nil {
t.Fatal(err)
}
}
}