diff --git a/ssh/tailssh/fwdmonitor.go b/ssh/tailssh/fwdmonitor.go index 0cf6461e6..2a968f7c3 100644 --- a/ssh/tailssh/fwdmonitor.go +++ b/ssh/tailssh/fwdmonitor.go @@ -45,12 +45,17 @@ import ( "golang.org/x/sys/unix" + "tailscale.com/envknob" "tailscale.com/types/logger" gliderssh "github.com/tailscale/gliderssh" gossh "golang.org/x/crypto/ssh" ) +// sshForwardGatewayPorts permits reverse TCP forwards to bind +// non-loopback addresses. It is the equivalent of sshd's GatewayPorts. +var sshForwardGatewayPorts = envknob.RegisterBool("TS_SSH_FORWARD_GATEWAY_PORTS") + // fwdMaxSunPathLen is the sun_path capacity. It is the lexical length // cap on unix socket paths, the same one gliderssh's path validator // enforced. The cap is 108 on Linux and 104 on macOS. diff --git a/ssh/tailssh/process_unix.go b/ssh/tailssh/process_unix.go index 70df04bca..c49d4a07b 100644 --- a/ssh/tailssh/process_unix.go +++ b/ssh/tailssh/process_unix.go @@ -11,12 +11,9 @@ import ( "fmt" "os" "os/exec" - "path/filepath" "runtime" "syscall" "time" - - gliderssh "github.com/tailscale/gliderssh" ) // monitorProcess identifies one exec child. ID is the exec request ID, which @@ -149,58 +146,3 @@ func systemHostKeyFile(typ string) string { } return "/etc/ssh/ssh_host_" + typ + "_key" } - -// handleSSHAgentForwarding starts a Unix socket listener and in the background -// forwards agent connections between the listener and the gliderssh.Session. -// On success, it assigns ss.agentListener. -// -// The listener and its containing directory are created by a user-cred -// child through conn.fwdListenAgent. See the docs there for the modes. -func (ss *sshSession) handleSSHAgentForwarding(s gliderssh.Session, lu *userMeta) error { - if !gliderssh.AgentRequested(ss) || !ss.conn.finalAction.AllowAgentForwarding { - return nil - } - // The latched AllowAgentForwarding bit may be stale. See recheckPolicy. - if !ss.conn.recheckPolicy() { - return nil - } - if sshDisableForwarding() { - // TODO(bradfitz): or do we want to return an error here instead so the user - // gets an error if they ran with ssh -A? But for now we just silently - // don't work, like the condition above. - return nil - } - ss.logf("ssh: agent forwarding requested") - ln, socket, err := ss.conn.fwdListenAgent(s.Context(), lu) - if err != nil { - return err - } - // The listener's own address is the kernel's bound path; socket is - // the path the child reported and the monitor validated. They must - // agree: the cleanup removes the directory of the validated path, so - // a child whose report and actual bind disagree is refused. - if got := ln.Addr().String(); got != socket { - ln.Close() - return fmt.Errorf("agent forwarding socket path %q does not match the listener's bound path %q", socket, got) - } - ss.agentListener = ln - go gliderssh.ForwardAgentConnections(ln, s) - return nil -} - -// closeAgentForwarding closes the agent forwarding listener and removes the -// temporary directory that holds its socket, so neither outlives the -// session. The removal rules are platform-specific: on the monitor -// platforms the directory was created by a user-cred child while the -// removal runs with the daemon's credentials; see -// removeAgentForwardingDir in fwdmonitor.go and fwdplan9.go. -func (ss *sshSession) closeAgentForwarding() { - ln := ss.agentListener - if ln == nil { - return - } - ss.agentListener = nil - dir := filepath.Dir(ln.Addr().String()) - ln.Close() - removeAgentForwardingDir(dir, ss.conn.localUser, ss.logf) -} diff --git a/ssh/tailssh/tailssh.go b/ssh/tailssh/tailssh.go index a893a66ab..2ee67b22c 100644 --- a/ssh/tailssh/tailssh.go +++ b/ssh/tailssh/tailssh.go @@ -55,10 +55,7 @@ var ( sshVerboseLogging = envknob.RegisterBool("TS_DEBUG_SSH_VLOG") sshDisableSFTP = envknob.RegisterBool("TS_SSH_DISABLE_SFTP") sshDisableForwarding = envknob.RegisterBool("TS_SSH_DISABLE_FORWARDING") - // sshForwardGatewayPorts permits reverse TCP forwards to bind - // non-loopback addresses. It is the equivalent of sshd's GatewayPorts. - sshForwardGatewayPorts = envknob.RegisterBool("TS_SSH_FORWARD_GATEWAY_PORTS") - sshDisablePTY = envknob.RegisterBool("TS_SSH_DISABLE_PTY") + sshDisablePTY = envknob.RegisterBool("TS_SSH_DISABLE_PTY") // Session teardown timeouts. Unset or non-positive values use the defaults. sshShutdownTimeout = envknob.RegisterDuration("TS_SSH_SHUTDOWN_TIMEOUT") @@ -1439,6 +1436,62 @@ func (c *conn) detachSession(ss *sshSession) { var errSessionDone = errors.New("session is done") +// handleSSHAgentForwarding starts a Unix socket listener and in the background +// forwards agent connections between the listener and the gliderssh.Session. +// On success, it assigns ss.agentListener. +// +// The listener and its containing directory come from the platform's +// conn.fwdListenAgent: on the monitor platforms a user-cred child creates +// them, on plan9 tailscaled does. See the docs there for the modes. +func (ss *sshSession) handleSSHAgentForwarding(s gliderssh.Session, lu *userMeta) error { + if !gliderssh.AgentRequested(ss) || !ss.conn.finalAction.AllowAgentForwarding { + return nil + } + // The latched AllowAgentForwarding bit may be stale. See recheckPolicy. + if !ss.conn.recheckPolicy() { + return nil + } + if sshDisableForwarding() { + // TODO(bradfitz): or do we want to return an error here instead so the user + // gets an error if they ran with ssh -A? But for now we just silently + // don't work, like the condition above. + return nil + } + ss.logf("ssh: agent forwarding requested") + ln, socket, err := ss.conn.fwdListenAgent(s.Context(), lu) + if err != nil { + return err + } + // The listener's own address is the kernel's bound path; socket is + // the path the child reported and the monitor validated. They must + // agree: the cleanup removes the directory of the validated path, so + // a child whose report and actual bind disagree is refused. + if got := ln.Addr().String(); got != socket { + ln.Close() + return fmt.Errorf("agent forwarding socket path %q does not match the listener's bound path %q", socket, got) + } + ss.agentListener = ln + go gliderssh.ForwardAgentConnections(ln, s) + return nil +} + +// closeAgentForwarding closes the agent forwarding listener and removes the +// temporary directory that holds its socket, so neither outlives the +// session. The removal rules are platform-specific: on the monitor +// platforms the directory was created by a user-cred child while the +// removal runs with the daemon's credentials; see +// removeAgentForwardingDir in fwdmonitor.go and fwdplan9.go. +func (ss *sshSession) closeAgentForwarding() { + ln := ss.agentListener + if ln == nil { + return + } + ss.agentListener = nil + dir := filepath.Dir(ln.Addr().String()) + ln.Close() + removeAgentForwardingDir(dir, ss.conn.localUser, ss.logf) +} + // run is the entrypoint for a newly accepted SSH session. // // It handles ss once it's been accepted and determined @@ -1676,10 +1729,6 @@ func (c *conn) ruleExpired(r *tailcfg.SSHRule) bool { return r.RuleExpires.Before(c.srv.now()) } -func (c *conn) evalSSHPolicy(pol *tailcfg.SSHPolicy) (*tailcfg.SSHAction, string, []string, *tailcfg.SSHRule, evalResult) { - return c.evalSSHPolicyWithUserLookup(pol, userLookup) -} - func (c *conn) evalSSHPolicyWithUserLookup(pol *tailcfg.SSHPolicy, lookup func(string) (*userMeta, error)) (a *tailcfg.SSHAction, localUser string, acceptEnv []string, rule *tailcfg.SSHRule, result evalResult) { failedOnUser := false for _, r := range pol.Rules { @@ -1706,10 +1755,6 @@ var ( errInvalidConn = errors.New("invalid connection state") ) -func (c *conn) matchRule(r *tailcfg.SSHRule) (*tailcfg.SSHAction, string, []string, error) { - return c.matchRuleWithUserLookup(r, userLookup) -} - func (c *conn) matchRuleWithUserLookup(r *tailcfg.SSHRule, lookup func(string) (*userMeta, error)) (a *tailcfg.SSHAction, localUser string, acceptEnv []string, err error) { defer func() { c.vlogf("matchRule(%+v): %v", r, err) diff --git a/ssh/tailssh/tailssh_test.go b/ssh/tailssh/tailssh_test.go index f49a2fd83..e19c5118c 100644 --- a/ssh/tailssh/tailssh_test.go +++ b/ssh/tailssh/tailssh_test.go @@ -1760,3 +1760,15 @@ func TestIsRootUser(t *testing.T) { } } } + +// evalSSHPolicy is [conn.evalSSHPolicyWithUserLookup] with the real user +// lookup, for tests. Production callers go through evaluatePolicy. +func (c *conn) evalSSHPolicy(pol *tailcfg.SSHPolicy) (*tailcfg.SSHAction, string, []string, *tailcfg.SSHRule, evalResult) { + return c.evalSSHPolicyWithUserLookup(pol, userLookup) +} + +// matchRule is [conn.matchRuleWithUserLookup] with the real user lookup, +// for tests. +func (c *conn) matchRule(r *tailcfg.SSHRule) (*tailcfg.SSHAction, string, []string, error) { + return c.matchRuleWithUserLookup(r, userLookup) +}