Commit Graph
3 Commits
Author SHA1 Message Date
Kristoffer Dalby 7f8afb526c ssh/tailssh: signal the session's process group, not just the child
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]>
2026-07-27 13:18:07 +00:00
Kristoffer Dalby 1587e7dea6 ssh/tailssh: only use /usr/bin/login for interactive TTY shells on darwin
macOS /usr/bin/login -pq exits 0 once the child is spawned, so the
child's exit status is lost: `ssh mac exit 5` returned 0, `ssh mac
/nonexistent` returned 0 instead of 127. We can't change Apple's binary,
so don't use it where it misbehaves.

Route exec sessions (with and without a TTY) and non-TTY shells through
handleSSHInProcess, the path Linux already uses, which runs the command
via cmd.Run + os.Exit(code). Only interactive TTY shells keep login.

The trade: maybeStartLoginSession is a no-op outside linux and findSU
returns "" for non-linux, so the three redirected darwin shapes now get
no PAM "remote" session and no utmpx entry. They are absent from `who`
and `last`. Interactive TTY shells, where that accounting matters most,
are unaffected.

New pure shouldUseLogin holds the per-GOOS policy so TestShouldUseLogin
can pin all four darwin combinations on any host. TestOpenSSHExitCodes
gains tty_exec and stdin_shell cases, exercised by the macOS CI job.

Updates #18256

Signed-off-by: Kristoffer Dalby <[email protected]>
2026-07-27 12:56:33 +00:00
Kristoffer Dalby 76c29282b3 ssh/tailssh: add exit-status test harness
Extract the in-process SSH server harness out of tailssh_test.go into
tailssh_exitcodes_test.go so the exit-status tests can share it, and add
the integrationtest-tagged exit-code suite (Go and OpenSSH clients).

Updates #18256

Signed-off-by: Kristoffer Dalby <[email protected]>
2026-07-27 12:37:50 +00:00