2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
package safesocket
|
|
|
|
|
ipn, safesocket: use Windows token in LocalAPI
On Windows, the idiomatic way to check access on a named pipe is for
the server to impersonate the client on its current OS thread, perform
access checks using the client's access token, and then revert the OS
thread's access token back to its true self.
The access token is a better representation of the client's rights than just
a username/userid check, as it represents the client's effective rights
at connection time, which might differ from their normal rights.
This patch updates safesocket to do the aforementioned impersonation,
extract the token handle, and then revert the impersonation. We retain
the token handle for the remaining duration of the connection (the token
continues to be valid even after we have reverted back to self).
Since the token is a property of the connection, I changed ipnauth to wrap
the concrete net.Conn to include the token. I then plumbed that change
through ipnlocal, ipnserver, and localapi as necessary.
I also added a PermitLocalAdmin flag to the localapi Handler which I intend
to use for controlling access to a few new localapi endpoints intended
for configuring auto-update.
Updates https://github.com/tailscale/tailscale/issues/755
Signed-off-by: Aaron Klotz <aaron@tailscale.com>
2023-10-25 20:48:05 +00:00
|
|
|
//go:generate go run golang.org/x/sys/windows/mkwinsyscall -output zsyscall_windows.go pipe_windows.go
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
import (
|
ipn, safesocket: use Windows token in LocalAPI
On Windows, the idiomatic way to check access on a named pipe is for
the server to impersonate the client on its current OS thread, perform
access checks using the client's access token, and then revert the OS
thread's access token back to its true self.
The access token is a better representation of the client's rights than just
a username/userid check, as it represents the client's effective rights
at connection time, which might differ from their normal rights.
This patch updates safesocket to do the aforementioned impersonation,
extract the token handle, and then revert the impersonation. We retain
the token handle for the remaining duration of the connection (the token
continues to be valid even after we have reverted back to self).
Since the token is a property of the connection, I changed ipnauth to wrap
the concrete net.Conn to include the token. I then plumbed that change
through ipnlocal, ipnserver, and localapi as necessary.
I also added a PermitLocalAdmin flag to the localapi Handler which I intend
to use for controlling access to a few new localapi endpoints intended
for configuring auto-update.
Updates https://github.com/tailscale/tailscale/issues/755
Signed-off-by: Aaron Klotz <aaron@tailscale.com>
2023-10-25 20:48:05 +00:00
|
|
|
"context"
|
2020-02-05 22:16:58 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
ipn, safesocket: use Windows token in LocalAPI
On Windows, the idiomatic way to check access on a named pipe is for
the server to impersonate the client on its current OS thread, perform
access checks using the client's access token, and then revert the OS
thread's access token back to its true self.
The access token is a better representation of the client's rights than just
a username/userid check, as it represents the client's effective rights
at connection time, which might differ from their normal rights.
This patch updates safesocket to do the aforementioned impersonation,
extract the token handle, and then revert the impersonation. We retain
the token handle for the remaining duration of the connection (the token
continues to be valid even after we have reverted back to self).
Since the token is a property of the connection, I changed ipnauth to wrap
the concrete net.Conn to include the token. I then plumbed that change
through ipnlocal, ipnserver, and localapi as necessary.
I also added a PermitLocalAdmin flag to the localapi Handler which I intend
to use for controlling access to a few new localapi endpoints intended
for configuring auto-update.
Updates https://github.com/tailscale/tailscale/issues/755
Signed-off-by: Aaron Klotz <aaron@tailscale.com>
2023-10-25 20:48:05 +00:00
|
|
|
"runtime"
|
2020-02-05 22:16:58 +00:00
|
|
|
"syscall"
|
ipn, safesocket: use Windows token in LocalAPI
On Windows, the idiomatic way to check access on a named pipe is for
the server to impersonate the client on its current OS thread, perform
access checks using the client's access token, and then revert the OS
thread's access token back to its true self.
The access token is a better representation of the client's rights than just
a username/userid check, as it represents the client's effective rights
at connection time, which might differ from their normal rights.
This patch updates safesocket to do the aforementioned impersonation,
extract the token handle, and then revert the impersonation. We retain
the token handle for the remaining duration of the connection (the token
continues to be valid even after we have reverted back to self).
Since the token is a property of the connection, I changed ipnauth to wrap
the concrete net.Conn to include the token. I then plumbed that change
through ipnlocal, ipnserver, and localapi as necessary.
I also added a PermitLocalAdmin flag to the localapi Handler which I intend
to use for controlling access to a few new localapi endpoints intended
for configuring auto-update.
Updates https://github.com/tailscale/tailscale/issues/755
Signed-off-by: Aaron Klotz <aaron@tailscale.com>
2023-10-25 20:48:05 +00:00
|
|
|
"time"
|
2022-11-21 17:00:20 +00:00
|
|
|
|
ipn, safesocket: use Windows token in LocalAPI
On Windows, the idiomatic way to check access on a named pipe is for
the server to impersonate the client on its current OS thread, perform
access checks using the client's access token, and then revert the OS
thread's access token back to its true self.
The access token is a better representation of the client's rights than just
a username/userid check, as it represents the client's effective rights
at connection time, which might differ from their normal rights.
This patch updates safesocket to do the aforementioned impersonation,
extract the token handle, and then revert the impersonation. We retain
the token handle for the remaining duration of the connection (the token
continues to be valid even after we have reverted back to self).
Since the token is a property of the connection, I changed ipnauth to wrap
the concrete net.Conn to include the token. I then plumbed that change
through ipnlocal, ipnserver, and localapi as necessary.
I also added a PermitLocalAdmin flag to the localapi Handler which I intend
to use for controlling access to a few new localapi endpoints intended
for configuring auto-update.
Updates https://github.com/tailscale/tailscale/issues/755
Signed-off-by: Aaron Klotz <aaron@tailscale.com>
2023-10-25 20:48:05 +00:00
|
|
|
"github.com/tailscale/go-winio"
|
|
|
|
"golang.org/x/sys/windows"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
safesocket: add ConnectionStrategy, provide control over fallbacks
fee2d9fad added support for cmd/tailscale to connect to IPNExtension.
It came in two parts: If no socket was provided, dial IPNExtension first,
and also, if dialing the socket failed, fall back to IPNExtension.
The second half of that support caused the integration tests to fail
when run on a machine that was also running IPNExtension.
The integration tests want to wait until the tailscaled instances
that they spun up are listening. They do that by dialing the new
instance. But when that dial failed, it was falling back to IPNExtension,
so it appeared (incorrectly) that tailscaled was running.
Hilarity predictably ensued.
If a user (or a test) explicitly provides a socket to dial,
it is a reasonable assumption that they have a specific tailscaled
in mind and don't want to fall back to IPNExtension.
It is certainly true of the integration tests.
Instead of adding a bool to Connect, split out the notion of a
connection strategy. For now, the implementation remains the same,
but with the details hidden a bit. Later, we can improve that.
Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
2021-12-08 21:55:55 +00:00
|
|
|
func connect(s *ConnectionStrategy) (net.Conn, error) {
|
ipn, safesocket: use Windows token in LocalAPI
On Windows, the idiomatic way to check access on a named pipe is for
the server to impersonate the client on its current OS thread, perform
access checks using the client's access token, and then revert the OS
thread's access token back to its true self.
The access token is a better representation of the client's rights than just
a username/userid check, as it represents the client's effective rights
at connection time, which might differ from their normal rights.
This patch updates safesocket to do the aforementioned impersonation,
extract the token handle, and then revert the impersonation. We retain
the token handle for the remaining duration of the connection (the token
continues to be valid even after we have reverted back to self).
Since the token is a property of the connection, I changed ipnauth to wrap
the concrete net.Conn to include the token. I then plumbed that change
through ipnlocal, ipnserver, and localapi as necessary.
I also added a PermitLocalAdmin flag to the localapi Handler which I intend
to use for controlling access to a few new localapi endpoints intended
for configuring auto-update.
Updates https://github.com/tailscale/tailscale/issues/755
Signed-off-by: Aaron Klotz <aaron@tailscale.com>
2023-10-25 20:48:05 +00:00
|
|
|
dl := time.Now().Add(20 * time.Second)
|
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), dl)
|
|
|
|
defer cancel()
|
|
|
|
// We use the identification impersonation level so that tailscaled may
|
|
|
|
// obtain information about our token for access control purposes.
|
|
|
|
return winio.DialPipeAccessImpLevel(ctx, s.path, windows.GENERIC_READ|windows.GENERIC_WRITE, winio.PipeImpLevelIdentification)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setFlags(network, address string, c syscall.RawConn) error {
|
|
|
|
return c.Control(func(fd uintptr) {
|
|
|
|
syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET,
|
|
|
|
syscall.SO_REUSEADDR, 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-21 17:00:20 +00:00
|
|
|
// windowsSDDL is the Security Descriptor set on the namedpipe.
|
|
|
|
// It provides read/write access to all users and the local system.
|
2023-04-14 23:52:44 +00:00
|
|
|
// It is a var for testing, do not change this value.
|
|
|
|
var windowsSDDL = "O:BAG:BAD:PAI(A;OICI;GWGR;;;BU)(A;OICI;GWGR;;;SY)"
|
2022-11-21 17:00:20 +00:00
|
|
|
|
2023-01-30 17:34:51 +00:00
|
|
|
func listen(path string) (net.Listener, error) {
|
2022-11-21 17:00:20 +00:00
|
|
|
lc, err := winio.ListenPipe(
|
|
|
|
path,
|
|
|
|
&winio.PipeConfig{
|
|
|
|
SecurityDescriptor: windowsSDDL,
|
|
|
|
InputBufferSize: 256 * 1024,
|
|
|
|
OutputBufferSize: 256 * 1024,
|
|
|
|
},
|
|
|
|
)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2023-01-30 17:34:51 +00:00
|
|
|
return nil, fmt.Errorf("namedpipe.Listen: %w", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
ipn, safesocket: use Windows token in LocalAPI
On Windows, the idiomatic way to check access on a named pipe is for
the server to impersonate the client on its current OS thread, perform
access checks using the client's access token, and then revert the OS
thread's access token back to its true self.
The access token is a better representation of the client's rights than just
a username/userid check, as it represents the client's effective rights
at connection time, which might differ from their normal rights.
This patch updates safesocket to do the aforementioned impersonation,
extract the token handle, and then revert the impersonation. We retain
the token handle for the remaining duration of the connection (the token
continues to be valid even after we have reverted back to self).
Since the token is a property of the connection, I changed ipnauth to wrap
the concrete net.Conn to include the token. I then plumbed that change
through ipnlocal, ipnserver, and localapi as necessary.
I also added a PermitLocalAdmin flag to the localapi Handler which I intend
to use for controlling access to a few new localapi endpoints intended
for configuring auto-update.
Updates https://github.com/tailscale/tailscale/issues/755
Signed-off-by: Aaron Klotz <aaron@tailscale.com>
2023-10-25 20:48:05 +00:00
|
|
|
return &winIOPipeListener{Listener: lc}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WindowsClientConn is an implementation of net.Conn that permits retrieval of
|
|
|
|
// the Windows access token associated with the connection's client. The
|
|
|
|
// embedded net.Conn must be a go-winio PipeConn.
|
|
|
|
type WindowsClientConn struct {
|
|
|
|
net.Conn
|
|
|
|
token windows.Token
|
|
|
|
}
|
|
|
|
|
|
|
|
// winioPipeHandle is fulfilled by the underlying code implementing go-winio's
|
|
|
|
// PipeConn interface.
|
|
|
|
type winioPipeHandle interface {
|
|
|
|
// Fd returns the Windows handle associated with the connection.
|
|
|
|
Fd() uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
func resolvePipeHandle(c net.Conn) windows.Handle {
|
|
|
|
wph, ok := c.(winioPipeHandle)
|
|
|
|
if !ok {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return windows.Handle(wph.Fd())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *WindowsClientConn) handle() windows.Handle {
|
|
|
|
return resolvePipeHandle(conn.Conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientPID returns the pid of conn's client, or else an error.
|
|
|
|
func (conn *WindowsClientConn) ClientPID() (int, error) {
|
|
|
|
var pid uint32
|
|
|
|
if err := getNamedPipeClientProcessId(conn.handle(), &pid); err != nil {
|
|
|
|
return -1, fmt.Errorf("GetNamedPipeClientProcessId: %w", err)
|
|
|
|
}
|
|
|
|
return int(pid), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Token returns the Windows access token of the client user.
|
|
|
|
func (conn *WindowsClientConn) Token() windows.Token {
|
|
|
|
return conn.token
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *WindowsClientConn) Close() error {
|
|
|
|
if conn.token != 0 {
|
|
|
|
conn.token.Close()
|
|
|
|
conn.token = 0
|
|
|
|
}
|
|
|
|
return conn.Conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
type winIOPipeListener struct {
|
|
|
|
net.Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lw *winIOPipeListener) Accept() (net.Conn, error) {
|
|
|
|
conn, err := lw.Listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := clientUserAccessToken(conn)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &WindowsClientConn{
|
|
|
|
Conn: conn,
|
|
|
|
token: token,
|
|
|
|
}, nil
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
ipn, safesocket: use Windows token in LocalAPI
On Windows, the idiomatic way to check access on a named pipe is for
the server to impersonate the client on its current OS thread, perform
access checks using the client's access token, and then revert the OS
thread's access token back to its true self.
The access token is a better representation of the client's rights than just
a username/userid check, as it represents the client's effective rights
at connection time, which might differ from their normal rights.
This patch updates safesocket to do the aforementioned impersonation,
extract the token handle, and then revert the impersonation. We retain
the token handle for the remaining duration of the connection (the token
continues to be valid even after we have reverted back to self).
Since the token is a property of the connection, I changed ipnauth to wrap
the concrete net.Conn to include the token. I then plumbed that change
through ipnlocal, ipnserver, and localapi as necessary.
I also added a PermitLocalAdmin flag to the localapi Handler which I intend
to use for controlling access to a few new localapi endpoints intended
for configuring auto-update.
Updates https://github.com/tailscale/tailscale/issues/755
Signed-off-by: Aaron Klotz <aaron@tailscale.com>
2023-10-25 20:48:05 +00:00
|
|
|
|
|
|
|
func clientUserAccessToken(c net.Conn) (windows.Token, error) {
|
|
|
|
h := resolvePipeHandle(c)
|
|
|
|
if h == 0 {
|
|
|
|
return 0, fmt.Errorf("not a windows handle: %T", c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Impersonation touches thread-local state, so we need to lock until the
|
|
|
|
// client access token has been extracted.
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
if err := impersonateNamedPipeClient(h); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
// Revert the current thread's impersonation.
|
|
|
|
if err := windows.RevertToSelf(); err != nil {
|
|
|
|
panic(fmt.Errorf("could not revert impersonation: %w", err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Extract the client's access token from the thread-local state.
|
|
|
|
var token windows.Token
|
|
|
|
if err := windows.OpenThreadToken(windows.CurrentThread(), windows.TOKEN_DUPLICATE|windows.TOKEN_QUERY, true, &token); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//sys getNamedPipeClientProcessId(h windows.Handle, clientPid *uint32) (err error) [int32(failretval)==0] = kernel32.GetNamedPipeClientProcessId
|
|
|
|
//sys impersonateNamedPipeClient(h windows.Handle) (err error) [int32(failretval)==0] = advapi32.ImpersonateNamedPipeClient
|