diff --git a/ssh/tailssh/fwdchild.go b/ssh/tailssh/fwdchild.go index a0dcb6ca6..3eda5c3be 100644 --- a/ssh/tailssh/fwdchild.go +++ b/ssh/tailssh/fwdchild.go @@ -26,10 +26,8 @@ // After a successful send the child holds its descriptor copy until the // monitor closes the result channel, its receipt acknowledgment. Listener // and TCP dial children then exit; Unix dial children relay through the private -// socketpair until the connection closes. On darwin the kernel's unix-socket -// GC reclaims a socket whose only reference is an in-flight SCM_RIGHTS -// message, so the sender's copy must cover the handoff until the receiver -// owns the descriptor; see beFwdChild and the monitor_rpc.go header. +// socketpair until the connection closes. See monitor_rpc.go for the Darwin +// socket-lifetime requirement that makes acknowledgment necessary. // // The monitor side of the protocol, fwdSpawnChild and fwdReadResult // below, drives these children. The op handlers live in monitor.go. @@ -66,8 +64,8 @@ const fwdUmask = 0o177 // fwdResultAckWait bounds the child's wait for the monitor's receipt // acknowledgment after sending its result descriptor. The monitor // closes the result channel immediately after taking ownership, so the -// wait normally ends at once; the deadline only covers a monitor that -// died without closing. +// wait normally ends at once. The deadline bounds the wait if the monitor +// does not acknowledge receipt. const fwdResultAckWait = 30 * time.Second // fwdRelayCloseDrainTimeout bounds delivery of queued client payload after a @@ -359,13 +357,8 @@ func beFwdChild(args []string) error { if err := fwdSendResult(c, "ok "+payload, files...); err != nil { return fmt.Errorf("ssh-fwd: sending result: %w", err) } - // Hold the descriptors, and this process, until the monitor has - // received them. On darwin, a socket whose only reference is an - // in-flight SCM_RIGHTS message is reclaimed by the kernel's unix-socket - // garbage collector. The monitor closes the result channel after - // taking ownership, so EOF is the receipt acknowledgment. The deadline - // bounds a monitor that died without closing; teardown SIGHUP also - // ends this process. + // Keep the descriptors open until the monitor acknowledges receipt by + // closing the result channel. See the protocol description above. c.SetReadDeadline(time.Now().Add(fwdResultAckWait)) var ack [1]byte _, ackErr := c.Read(ack[:]) diff --git a/ssh/tailssh/monitor.go b/ssh/tailssh/monitor.go index 71f081e2e..db5f13c4e 100644 --- a/ssh/tailssh/monitor.go +++ b/ssh/tailssh/monitor.go @@ -44,17 +44,17 @@ import ( ) const ( - // monExecOpDeadline bounds the exec op's spawn-and-reply phase. It - // does not bound the child's lifetime. + // monExecOpDeadline is the maximum accepted duration of an exec spawn. + // It is checked after cmd.Start returns; it does not interrupt Start + // or limit the child's lifetime. monExecOpDeadline = 30 * time.Second // monFwdOpDeadline bounds a forwarding child from spawn to // result. On expiry the child's group is SIGKILLed. monFwdOpDeadline = 10 * time.Second - // monTeardownReap bounds each reap sweep in teardown. A child still - // alive after the SIGKILL sweep is uninterruptible. It is left in - // its own group and abandoned. + // monTeardownReap bounds each reap phase in teardown. The monitor + // stops waiting after this interval even if children remain. monTeardownReap = 2 * time.Second ) @@ -368,9 +368,9 @@ func joinGids(gids []uint32) string { return strings.Join(parts, ",") } -// handleExec spawns one child, mapping the request's file descriptors -// to the child's stdio. monExecOpDeadline bounds the op from dispatch -// to the return of cmd.Start; it does not bound the child's lifetime. +// handleExec spawns one child, mapping the request's file descriptors to +// the child's stdio. If cmd.Start returns after monExecOpDeadline, the +// child is killed and the request fails. func (m *monitor) handleExec(f rpcFrame, fds []int) { start := time.Now() fail := func(err error) { @@ -646,15 +646,10 @@ func (m *monitor) killChild(c *monChild) { } } -// replyFD writes a reply carrying descriptors. On success they stay -// open, parked in pendingFds until tailscaled's fdack: -// on darwin, closing the sender's copy while the descriptor's only -// reference is the in-flight SCM_RIGHTS message lets the kernel's -// unix-socket GC reclaim the socket (receive side flushed, buffered -// data discarded) before the receiver's recvmsg, producing a relayed -// socket that reads EOF with its peer still alive. The receiver's -// acknowledgment is what releases the hold. On a write failure the -// channel is broken and the descriptor is closed here. +// replyFD writes a reply carrying descriptors and retains them in pendingFds +// until tailscaled acknowledges receipt. See monitor_rpc.go for the Darwin +// socket-lifetime requirement. A write failure closes the descriptors and +// marks the RPC channel broken. func (m *monitor) replyFD(f rpcFrame, fds ...int) { err := m.conn.writeFrame(f, fds...) if err != nil { @@ -739,8 +734,7 @@ func (m *monitor) notifyExit(child monitorProcess, code int) { // SIGKILL. Children that survive even the SIGKILL are abandoned in // their own groups. func (m *monitor) teardown() { - // Release every descriptor parked for an fdack: the peer is going - // away or gone, so no acknowledgment will arrive. + // Close descriptors whose receipt has not been acknowledged. for id, fds := range m.pendingFds { closeFds(fds) delete(m.pendingFds, id) @@ -766,11 +760,8 @@ func (m *monitor) teardown() { } } if len(pgids) > 0 { - // SIGKILL every remembered group, those whose leader survived - // the HUP and those whose leader died to it, then run one more - // bounded silent reap sweep. Anything still alive after this is - // in uninterruptible state and is abandoned, orphaned in its - // own group. + // Kill surviving group members even if their leader has exited, + // then wait for children to exit for at most monTeardownReap. for pgid := range pgids { _ = killGroup(pgid, unix.SIGKILL) } diff --git a/ssh/tailssh/monitor_rpc.go b/ssh/tailssh/monitor_rpc.go index b3881fa3a..ca1174082 100644 --- a/ssh/tailssh/monitor_rpc.go +++ b/ssh/tailssh/monitor_rpc.go @@ -28,12 +28,11 @@ // // A dial or listen reply's descriptor is held open by its sender until // the receiver acknowledges it with an fdack frame naming the reply's -// request ID. On darwin, a socket whose only reference is an in-flight -// SCM_RIGHTS message is reclaimed by the kernel's unix-socket garbage -// collector: its receive side is flushed and buffered data discarded -// while recvmsg still returns a usable descriptor. Continuous -// process-owned coverage across the handoff, released only on receipt, -// closes that window. +// request ID. On Darwin, a socket whose only reference is an in-flight +// SCM_RIGHTS message can be reclaimed by the kernel's Unix-socket garbage +// collector. In that case, recvmsg still returns a valid descriptor, but +// the socket's receive buffer has been flushed and its data discarded. +// Keeping the sender's copy open until receipt is acknowledged prevents this. package tailssh @@ -178,8 +177,8 @@ func rpcExitNotify(child monitorProcess, code int) rpcFrame { } // rpcFdAckReq acknowledges receipt of the descriptors a reply with the -// given request ID carried. The sender of those descriptors holds its own -// copies until this arrives; see replyFD in monitor.go for why. +// given request ID carried. The sender keeps its copies open until this +// acknowledgment arrives; see the socket-lifetime requirement above. func rpcFdAckReq(replyID int64) rpcFrame { return rpcFrame{ID: replyID, Op: opFdAck} } @@ -259,7 +258,7 @@ func heraldFrames(rbuf []byte, sets [][]int, queued func(k int) bool) ([]int, er return frames, nil } -// recvFds parses the SCM_RIGHTS descriptors from the ancillary data of +// recvFdSets parses the SCM_RIGHTS descriptors from the ancillary data of // one ReadMsgUnix call, using the oob slice and flags that call // returned, and marks every received descriptor close-on-exec before // returning. The caller owns the returned descriptors. diff --git a/ssh/tailssh/tailssh.go b/ssh/tailssh/tailssh.go index b32a38790..a1577d95c 100644 --- a/ssh/tailssh/tailssh.go +++ b/ssh/tailssh/tailssh.go @@ -217,11 +217,9 @@ func (srv *server) HandleSSHConn(nc net.Conn) error { return nil } -// Shutdown terminates all active connections and sessions. -// The raw socket is closed too, through conn.closeConn. Connections -// still inside their auth handshake, including ones parked in a -// HoldAndDelegate wait, are torn down rather than left to their own -// grace or hold deadline. +// Shutdown closes all active connections and waits for their sessions to exit. +// Closing the raw transport also interrupts authentication and cancels pending +// HoldAndDelegate requests. func (srv *server) Shutdown() { srv.mu.Lock() srv.shutdownCalled = true @@ -343,11 +341,8 @@ type conn struct { graceTimer *time.Timer ruleTimer *time.Timer - // teardownDone is set by stopConnTimers, HandleSSHConn's deferred - // conn teardown. Like the timers, it is guarded by mu. A recheck - // goroutine that started before the teardown, such as a policy-change - // kick or a timer fire, must not re-arm ruleTimer once the teardown - // has run. + // teardownDone is set by stopConnTimers under mu. It prevents policy + // rechecks from arming ruleTimer after connection cleanup. teardownDone bool // fwd is the connection's forwarding state. On unix it holds the @@ -1038,11 +1033,9 @@ func (c *conn) closeConn() { }) } -// revokeConn terminates the conn on a material policy change. Every -// session gets the user-visible "Access revoked." message via its own -// context cancel, the pre-existing kick path. The transport is then -// closed, so sessions, forwards, listeners, and the monitor all tear -// down with it. +// revokeConn cancels each session with an "Access revoked." message and +// closes the connection. This is triggered when a policy change invalidates +// its grant. func (c *conn) revokeConn(reason string) { metricPolicyChangeKick.Add(1) c.logf("session no longer valid per new SSH policy (%v); closing connection", reason) @@ -1390,16 +1383,8 @@ func (ss *sshSession) run() { if attached := ss.conn.srv.attachSessionToConnIfNotShutdown(ss); !attached { fmt.Fprintf(ss, "Tailscale SSH is shutting down\r\n") - // 255 signals an SSH-layer failure (the session never reached the - // user's command), distinct from any exit status the remote - // program might produce. The ssh(1) EXIT STATUS section - // documents this as the value the client reports "if an error - // occurred", and OpenSSH's own ssh.c uses exit(255) for every - // transport/protocol fatal path. Wrappers and CI use the 255 - // boundary to tell "connection broke" from "command exited N". - // See: - // https://man.openbsd.org/ssh#EXIT_STATUS - // https://github.com/openssh/openssh-portable/blob/V_10_2_P1/ssh.c#L1693 + // Use 255 for server failures before the command starts, following + // the ssh(1) convention for SSH errors. ss.Exit(255) return } @@ -1421,8 +1406,7 @@ func (ss *sshSession) run() { if err := canSwitchToLocalUser(lu); err != nil { ss.logf("%v", err) fmt.Fprintf(ss, "can't switch user\r\n") - // 255: SSH-layer failure, no user command ever ran. See the - // attachSession branch above for the full citation. + // The server could not start the command. ss.Exit(255) return } @@ -1440,16 +1424,9 @@ func (ss *sshSession) run() { } } - // Recording applies to sftp subsystem sessions (and scp -s, which - // rides the sftp subsystem) just as to interactive ones: a - // recording-mandatory tailnet must not gain an unrecorded - // file-transfer path, and the same OnRecordingFailure semantics - // apply — a recorder that can't be reached refuses the session - // rather than letting file access proceed silently unrecorded. - // SFTP recordings are metadata-only (see recording.metadataOnly): - // the SFTP wire protocol is binary, so pass-through capture is not - // human-meaningful and would balloon casts; the header still - // identifies the peer, local user, and connection. + // Recording policy also applies to SFTP sessions, including scp -s. + // These record session metadata rather than the binary protocol; see + // recording.metadataOnly. var rec *recording // or nil if disabled if ss.shouldRecord() { var err error @@ -1467,26 +1444,7 @@ func (ss *sshSession) run() { fmt.Fprintf(out, "can't start new recording\r\n") } ss.logf("startNewRecording: %v", err) - // 254 is Tailscale-specific: the SSH transport is fine - // and the user's command is well-formed, but the session - // recording policy could not be satisfied (recorder - // unreachable, upload denied, etc.) so we refuse to run - // the command at all. We need a code that operators can - // alert on without collapsing it into the generic buckets: - // - 1 is overloaded; any program can produce it - // (Bash exit-code conventions, codes 1-2) - // - 127 means "command not found" (POSIX 2018, sh - // §2.8.2) - // - 130 is Ctrl-C (128 + SIGINT) - // - 255 means "SSH itself failed" (ssh(1) EXIT STATUS) - // 254 sits in the reserved >128 region but is not claimed - // by any of the above, so it is unambiguous for - // "recording-required session denied". - // Refs: - // https://man.openbsd.org/ssh#EXIT_STATUS - // https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02 - // https://tldp.org/LDP/abs/html/exitcodes.html - // https://github.com/tailscale/tailscale/issues/18256 + // Exit 254 indicates that recording policy prevented session startup. ss.Exit(254) return } @@ -1508,16 +1466,8 @@ func (ss *sshSession) run() { } } } else if isNotFoundOrExecutable(err) { - // 127 = "command not found" per POSIX 2018 sh §2.8.2 - // ("if a command is not found, the exit status shall be - // 127"). 126 is reserved for "found but not executable"; - // we collapse both into 127 because exec.ErrNotFound and - // os.ErrNotExist don't distinguish the underlying cause - // reliably across platforms, and 127 is what users - // recognise (it's what bash / dash / zsh report). - // Refs: - // https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02 - // https://tldp.org/LDP/abs/html/exitcodes.html + // Use the POSIX shell convention for a missing command: + // https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02 exitCode = 127 } ss.Exit(exitCode) @@ -1526,14 +1476,9 @@ func (ss *sshSession) run() { ss.exitHandled = make(chan struct{}) go ss.killProcessOnContextDone() - // wg covers stdout and stderr only: their close ordering is - // load-bearing for the wire-level frame sequence below. - // - // The stdin copier is deliberately NOT in wg. It blocks reading from - // the SSH channel until the client half-closes its write side; many - // clients (go-scp, non-SFTP exec) don't half-close before they - // receive CHANNEL_CLOSE from us. Including it would deadlock. It - // self-cleans when deferred ss.Close runs. + // Wait for both output streams before sending CHANNEL_EOF. + // Do not wait for stdin: clients may keep it open until CHANNEL_CLOSE. + // The deferred ss.Close unblocks the stdin copier. var wg sync.WaitGroup go func() { @@ -1555,12 +1500,8 @@ func (ss *sshSession) run() { if ss.rdStderr != nil { wg.Go(func() { defer ss.rdStderr.Close() - // Record stderr under the "o" (output) direction: the - // asciinema cast format has no separate stderr channel, - // and pty sessions already fold stdout+stderr into the - // single recorded "o" stream. Wrapping it in rec.writer - // (rather than copying raw) is what keeps stderr in the - // recording for non-pty sessions. + // The asciinema format has no separate stderr channel. Record + // stderr as output, as for PTY sessions. if _, err := io.Copy(rec.writer("o", ss.Stderr()), ss.rdStderr); err != nil { logf("stderr copy: %v", err) } @@ -2032,13 +1973,9 @@ type recording struct { // continue if writing to the recording fails. failOpen bool - // metadataOnly is set for subsystem sessions (sftp). The cast - // records the CastHeader (peer identity, local user, connection, - // and the subsystem name) but no terminal output events: the SFTP - // wire protocol is binary, so pass-through capture would be - // neither human-meaningful nor reviewable, and would balloon cast - // sizes. The recorder upload and OnRecordingFailure lifecycle is - // identical to interactive sessions. + // metadataOnly disables I/O recording for subsystem sessions (SFTP). + // The cast header identifies the peer, local user, connection, and + // subsystem. Upload and recording-failure policy still apply. metadataOnly bool mu sync.Mutex // guards writes to, close of out