mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-20 16:51:18 -08:00
Teardown signalled cmd.Process.Pid, which is the incubator. The user's shell is a grandchild and never saw it, so any HUP-trapping cleanup the user installed was silently skipped. New terminateSession helper signals the negated PID so the kernel delivers to every process in the group; ESRCH maps to nil, meaning the session already exited. plan9 has no Unix process groups and falls back to Process.Signal. Both command paths now lead their own group. newIncubatorCommand sets Setpgid, and so does the direct-exec path (no tailscaledPath: embedders and tests), which previously left the shell in tailscaled's group where kill(-pid) found no such pgid and the ESRCH looked like a clean exit. The PTY path gets this via Setsid in startWithPTY. TestSessionTerminationKillsProcess spawns a grandchild over the direct-exec path and fails without the Setpgid. This is not sufficient on its own when su is in play: su -c starts the command in a new session, so the group signal still cannot reach it. The next commit handles that; TestIntegrationSIGHUP, which covers the incubator path, passes only once both are in. Updates #18256 Signed-off-by: Kristoffer Dalby <[email protected]>
98 lines
2.8 KiB
Go
98 lines
2.8 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build linux || darwin
|
|
|
|
package tailssh
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
"tailscale.com/tstest"
|
|
)
|
|
|
|
// processWithMarkerRunning reports whether any process has marker in
|
|
// its command line. pgrep exits 1 for "no match", which is the only
|
|
// non-error "false" we accept.
|
|
func processWithMarkerRunning(t *testing.T, marker string) bool {
|
|
t.Helper()
|
|
err := exec.Command("pgrep", "-f", marker).Run()
|
|
if err == nil {
|
|
return true
|
|
}
|
|
var ee *exec.ExitError
|
|
if errors.As(err, &ee) && ee.ExitCode() == 1 {
|
|
return false
|
|
}
|
|
t.Fatalf("pgrep -f %q: %v", marker, err)
|
|
return false
|
|
}
|
|
|
|
// TestSessionTerminationKillsProcess asserts that tearing down an SSH
|
|
// session terminates the user's whole process tree, not just the
|
|
// immediate child. The in-process harness has no tailscaledPath, so
|
|
// this exercises the direct-exec (no incubator) path where the user
|
|
// shell spawns grandchildren: tailssh must signal the process group,
|
|
// which requires the direct-path cmd to be its own group leader.
|
|
func TestSessionTerminationKillsProcess(t *testing.T) {
|
|
h := newTestSSHHarness(t)
|
|
|
|
marker := fmt.Sprintf("tailssh-termination-test-%d-%d", os.Getpid(), time.Now().UnixNano())
|
|
// Two levels deep, on purpose. The outer sh is (or is exec'd over
|
|
// by) cmd.Process, which exec.CommandContext kills on its own; the
|
|
// inner sh is a grandchild that only dies if tailssh signals the
|
|
// whole process group. The loop keeps the inner sh from exec'ing
|
|
// over itself, so the marker stays in the survivor's argv.
|
|
cmd := fmt.Sprintf(`/bin/sh -c '/bin/sh -c "while :; do sleep 1; done #%s" & wait'`, marker)
|
|
|
|
t.Cleanup(func() {
|
|
// Don't leave a 30s sleeper behind on failure.
|
|
exec.Command("pkill", "-f", marker).Run()
|
|
})
|
|
|
|
cl, err := ssh.Dial("tcp", h.addr, &ssh.ClientConfig{
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
Timeout: 15 * time.Second,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer cl.Close()
|
|
s, err := cl.NewSession()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Start(cmd); err != nil {
|
|
t.Fatalf("Start: %v", err)
|
|
}
|
|
|
|
if err := tstest.WaitFor(10*time.Second, func() error {
|
|
if !processWithMarkerRunning(t, marker) {
|
|
return fmt.Errorf("marker process not started yet")
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
t.Fatalf("marker process never appeared: %v", err)
|
|
}
|
|
|
|
// Tear the whole connection down; the session context cancels and
|
|
// tailssh must terminate the process tree.
|
|
s.Close()
|
|
cl.Close()
|
|
|
|
if err := tstest.WaitFor(10*time.Second, func() error {
|
|
if processWithMarkerRunning(t, marker) {
|
|
return fmt.Errorf("process still running")
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
t.Fatalf("user process survived session teardown: %v", err)
|
|
}
|
|
}
|