ssh/tailssh: register connections atomically with shutdown

Check shutdown and register initialized connections under the same lock. Reject a connection if shutdown ran during its construction, stopping its login-grace timer and closing the transport. Also close the transport when connection construction fails.

Cover shutdown before and during connection construction without starting an SSH handshake.

Updates tailscale/corp#50380

Change-Id: I634036a7e073b24d7ff80d6a6d0ae991c40f3c69
Signed-off-by: James Tucker <[email protected]>
(cherry picked from commit 617c38d523)
This commit is contained in:
James Tucker committed 2026-10-27 20:14:22 +00:00
1 parent 342ebd6f99
commit 5919a9d1e9
2 files changed
+119 -9

No files matched your search

+97
View File
@@ -0,0 +1,97 @@
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build (linux && !android) || (darwin && !ios) || freebsd || openbsd || plan9
package tailssh
import (
"io"
"net"
"testing"
"time"
)
// connSetupBackend pauses connection construction after newConn's shutdown check.
type connSetupBackend struct {
ipnLocalBackend
varRoot string
entered chan struct{} // closed when the backend has begun setup
release chan struct{} // tests must close this to unblock setup
}
func (b *connSetupBackend) TailscaleVarRoot() string {
close(b.entered)
<-b.release
return b.varRoot
}
func TestHandleSSHConnDuringShutdown(t *testing.T) {
t.Run("DuringSetup", func(t *testing.T) {
b := &connSetupBackend{
varRoot: t.TempDir(),
entered: make(chan struct{}),
release: make(chan struct{}),
}
srv := &server{lb: b, logf: t.Logf}
client, serverConn := net.Pipe()
defer client.Close()
defer serverConn.Close()
if err := client.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
t.Fatal(err)
}
done := make(chan error, 1)
go func() { done <- srv.HandleSSHConn(serverConn) }()
<-b.entered
srv.Shutdown()
close(b.release)
select {
case err := <-done:
if err == nil {
t.Fatal("HandleSSHConn succeeded after shutdown")
}
case <-time.After(5 * time.Second):
t.Fatal("HandleSSHConn did not return after shutdown")
}
if got := srv.NumActiveConns(); got != 0 {
t.Errorf("active connections = %d, want 0", got)
}
var buf [1]byte
if _, err := client.Read(buf[:]); err != io.EOF {
t.Errorf("client Read = %v, want EOF", err)
}
})
t.Run("BeforeSetup", func(t *testing.T) {
b := &connSetupBackend{
varRoot: t.TempDir(),
entered: make(chan struct{}),
release: make(chan struct{}),
}
srv := &server{lb: b, logf: t.Logf}
client, serverConn := net.Pipe()
defer client.Close()
defer serverConn.Close()
if err := client.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
t.Fatal(err)
}
srv.Shutdown()
done := make(chan error, 1)
go func() { done <- srv.HandleSSHConn(serverConn) }()
close(b.release)
select {
case err := <-done:
if err == nil {
t.Fatal("HandleSSHConn succeeded after shutdown")
}
case <-time.After(5 * time.Second):
t.Fatal("HandleSSHConn did not return after shutdown")
}
if got := srv.NumActiveConns(); got != 0 {
t.Errorf("active connections = %d, want 0", got)
}
var buf [1]byte
if _, err := client.Read(buf[:]); err != io.EOF {
t.Errorf("client Read = %v, want EOF", err)
}
})
}
+22 -9
View File
@@ -154,13 +154,22 @@ func (srv *server) attachSessionToConnIfNotShutdown(ss *sshSession) bool {
return true
}
func (srv *server) trackActiveConn(c *conn, add bool) {
// registerConn registers c unless shutdown has started, and reports whether
// c was successfully registered. The caller must initialize c's transport
// and authentication context before calling it.
func (srv *server) registerConn(c *conn) bool {
srv.mu.Lock()
defer srv.mu.Unlock()
if add {
mak.Set(&srv.activeConns, c, true)
return
if srv.shutdownCalled {
return false
}
mak.Set(&srv.activeConns, c, true)
return true
}
func (srv *server) unregisterConn(c *conn) {
srv.mu.Lock()
defer srv.mu.Unlock()
delete(srv.activeConns, c)
}
@@ -175,6 +184,7 @@ func (srv *server) NumActiveConns() int {
// This is the entry point for all SSH connections.
// When this returns, the connection is closed.
func (srv *server) HandleSSHConn(nc net.Conn) error {
defer nc.Close()
metricIncomingConnections.Add(1)
c, err := srv.newConn()
if err != nil {
@@ -188,8 +198,12 @@ func (srv *server) HandleSSHConn(nc net.Conn) error {
c.netConn = nc
c.preAuthCtx, c.preAuthCancel = context.WithCancel(context.Background())
c.armLoginGrace()
srv.trackActiveConn(c, true) // add
defer srv.trackActiveConn(c, false) // remove
if !srv.registerConn(c) {
c.stopConnTimers()
c.closeConn()
return errors.New("server is shutting down")
}
defer srv.unregisterConn(c)
defer c.preAuthCancel()
defer c.stopConnTimers()
// The connection is done when HandleConn returns. Tear down the
@@ -584,9 +598,8 @@ func (srv *server) newConn() (*conn, error) {
srv.mu.Lock()
if srv.shutdownCalled {
srv.mu.Unlock()
// Stop accepting new connections.
// Connections in the auth phase are handled in handleConnPostSSHAuth.
// Existing sessions are terminated by Shutdown.
// Avoid constructing a connection after shutdown. registerConn
// checks again before publishing the initialized connection.
return nil, errors.New("server is shutting down")
}
srv.mu.Unlock()