Files
tailscale/ssh/tailssh/monitor_termination_test.go
T
James Tucker 9008f3cd8b ssh/tailssh: commit monitor KillAll before closing revoked connections
When a connection was revoked (policy change or RuleExpires), revokeConn
only set an in-memory flag and immediately closed the transport,
deferring the monitor KillAll RPC to closeConnMonitor. Closing the
transport caused non-PTY session stdin to hit EOF so the session leader
could exit 0 and be reaped and deleted from the monitor's child table
before KillAll or Terminate arrived, leaving background jobs in the
leader's process group alive after monitor teardown.

Commit KillAll to the monitor in revokeConn before canceling sessions or
closing the transport, and prevent connMonitor from returning or
spawning a monitor once revocation has begun. In the monitor, have
handleKillAll begin termination on every current child immediately so
leaders stay pinned until SIGKILL even if they exit before Close, and
refuse new exec/dial/listen launches after KillAll.

Fixes #22915

Change-Id: Ia2df1647602051f395f6e2fe107150e6a938a37a
Signed-off-by: James Tucker <james@tailscale.com>
2026-12-04 22:18:27 +00:00

162 lines
4.5 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build (linux && !android) || (darwin && !ios)
package tailssh
import (
"bufio"
"context"
"fmt"
"os"
"strconv"
"strings"
"testing"
"time"
"golang.org/x/sys/unix"
"tailscale.com/tstest"
)
func TestMonitorTerminationRPCFailureFailsClosed(t *testing.T) {
for _, mode := range []string{"kill-all", "terminate", "unrequested-eof"} {
t.Run(mode, func(t *testing.T) {
mc := startTestMonitor(t, os.Getuid(), os.Getgid(), testGroups(t))
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
defer r.Close()
defer w.Close()
null, err := os.OpenFile(os.DevNull, os.O_RDWR, 0)
if err != nil {
t.Fatal(err)
}
defer null.Close()
child, err := mc.Exec(mustCtx(t), []string{"/bin/sh", "-c", "trap '' HUP; echo ready; while :; do sleep 1; done"}, nil, "/", false, nil, null, w, null)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = unix.Kill(-child.PID, unix.SIGKILL) })
w.Close()
r.SetReadDeadline(time.Now().Add(5 * time.Second))
if line, err := bufio.NewReader(r).ReadString('\n'); err != nil || line != "ready\n" {
t.Fatalf("ready = %q, %v", line, err)
}
if mode == "unrequested-eof" {
mc.c.Close()
} else {
// Fail the next termination request's write.
if err := mc.c.CloseWrite(); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if mode == "kill-all" {
err = mc.KillAll(ctx)
} else {
err = mc.Terminate(ctx, child, hangupEscalation)
}
if err == nil {
t.Fatal("mandate unexpectedly succeeded on broken RPC")
}
}
if err := mc.Close(); err != nil {
t.Fatal(err)
}
if err := tstest.WaitFor(5*time.Second, func() error {
if !pidGone(child.PID) {
return fmt.Errorf("child %d survived failed mandate", child.PID)
}
return nil
}); err != nil {
t.Fatal(err)
}
})
}
}
// TestMonitorKillAllPinsLeaderUntilClose checks that KillAll begins
// termination immediately, pinning child leaders even if they exit on stdin
// EOF before Close so teardown still kills surviving process-group members,
// and refuses subsequent child launches.
func TestMonitorKillAllPinsLeaderUntilClose(t *testing.T) {
mc := startTestMonitor(t, os.Getuid(), os.Getgid(), testGroups(t))
inR, inW, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
defer inR.Close()
defer inW.Close()
outR, outW, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
defer outR.Close()
defer outW.Close()
null, err := os.OpenFile(os.DevNull, os.O_RDWR, 0)
if err != nil {
t.Fatal(err)
}
defer null.Close()
// Leader waits on stdin EOF while its background child ignores SIGHUP.
child, err := mc.Exec(mustCtx(t),
[]string{"/bin/sh", "-c", "nohup sh -c 'echo $$; exec sleep 300' </dev/null & read x"},
nil, "/", false, nil, inR, outW, null)
if err != nil {
t.Fatal(err)
}
inR.Close()
outW.Close()
t.Cleanup(func() { _ = unix.Kill(-child.PID, unix.SIGKILL) })
outR.SetReadDeadline(time.Now().Add(5 * time.Second))
line, err := bufio.NewReader(outR).ReadString('\n')
if err != nil {
t.Fatalf("reading straggler PID: %v", err)
}
straggler, err := strconv.Atoi(strings.TrimSpace(line))
if err != nil || straggler <= 1 {
t.Fatalf("invalid straggler PID %q: %v", line, err)
}
t.Cleanup(func() { _ = unix.Kill(straggler, unix.SIGKILL) })
if err := mc.KillAll(mustCtx(t)); err != nil {
t.Fatalf("KillAll: %v", err)
}
// New launches must be refused once KillAll has been acknowledged.
if late, err := mc.Exec(mustCtx(t), []string{"/bin/true"}, nil, "/", false, nil); err == nil {
t.Fatalf("Exec after KillAll unexpectedly spawned pid %d", late.PID)
}
// Close stdin so the leader exits immediately before Close arrives.
inW.Close()
if err := tstest.WaitFor(2*time.Second, func() error {
if !pidGone(child.PID) {
return fmt.Errorf("leader %d still running", child.PID)
}
return nil
}); err != nil {
t.Fatal(err)
}
// Give any buggy sweep a chance to reap and forget the exited leader.
time.Sleep(200 * time.Millisecond)
if pidGone(straggler) {
t.Fatalf("HUP-immune straggler %d exited before escalation", straggler)
}
if err := mc.Close(); err != nil {
t.Fatalf("Close: %v", err)
}
if err := tstest.WaitFor(5*time.Second, func() error {
if !pidGone(straggler) {
return fmt.Errorf("straggler %d survived KillAll+Close", straggler)
}
return nil
}); err != nil {
t.Fatal(err)
}
}