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]>
This commit is contained in:
Kristoffer Dalby committed 2026-07-27 12:56:33 +00:00
1 parent 47bc9c4fd5
commit 1587e7dea6
3 files changed
+130 -26

No files matched your search

+41 -20
View File
@@ -493,6 +493,39 @@ func runningAsRoot() bool {
return euid == 0
}
// shouldUseLogin decides whether tryExecLogin should hand the session off
// to /usr/bin/login or fall through to other mechanisms. It is pure to keep
// the per-GOOS policy testable on any host.
//
// Constraints encoded here:
//
// - login on linux/freebsd/openbsd cannot exec a command, only launch a
// shell, and requires a TTY (without one it exits immediately, breaking
// mosh and VSCode).
// - login on darwin can exec commands, but /usr/bin/login -pq exits 0 as
// soon as the child is spawned, so the child's exit status is lost
// (#18256). Only interactive TTY shells go through login, where the
// PAM "remote" session and utmpx accounting are worth that trade;
// exec sessions and non-TTY shells need the real exit status.
func shouldUseLogin(goos string, hasTTY, isShell bool) (use bool, reason string) {
switch goos {
case linux, freebsd, openbsd:
if !isShell {
return false, "login on " + goos + " cannot exec a command, only a shell"
}
if !hasTTY {
return false, "login on " + goos + " requires a TTY"
}
return true, ""
case darwin:
if hasTTY && isShell {
return true, ""
}
return false, "darwin login -pq swallows the child exit status (#18256); only interactive TTY shells use login"
}
return false, "unsupported GOOS: " + goos
}
// tryExecLogin attempts to handle the ssh session by creating a full login
// shell using the login command. If it never tried, it returns nil. If it
// failed to do so, it returns an error.
@@ -501,36 +534,24 @@ func runningAsRoot() bool {
// the login session, trigger PAM authentication, and get the "remote" PAM
// profile.
//
// However, login is subject to some limitations.
// However, login is subject to some limitations:
//
// 1. login cannot be used to execute commands except on macOS.
// 2. On Linux and BSD, login requires a TTY to keep running.
// 1. login cannot be used to execute commands except on macOS, and on
// macOS /usr/bin/login -pq does not propagate the child's exit
// status (#18256), so only interactive TTY shells use it there.
// 2. On Linux and BSD, login requires a TTY to keep running.
//
// In these cases, tryExecLogin returns (false, nil) to indicate that processing
// In these cases, tryExecLogin returns nil to indicate that processing
// should fall through to other methods, such as using the su command.
//
// Note that this uses unix.Exec to replace the current process, so in cases
// where we actually do run login, no subsequent Go code will execute.
func tryExecLogin(dlogf logger.Logf, ia incubatorArgs) error {
// Only the macOS version of the login command supports executing a
// command, all other versions only support launching a shell without
// taking any arguments.
if !ia.isShell && runtime.GOOS != darwin {
dlogf("won't use login because we're not in a shell or on macOS")
if use, reason := shouldUseLogin(runtime.GOOS, ia.hasTTY, ia.isShell); !use {
dlogf("not using login: %s", reason)
return nil
}
switch runtime.GOOS {
case linux, freebsd, openbsd:
if !ia.hasTTY {
dlogf("can't use login because of missing TTY")
// We can only use the login command if a shell was requested with
// a TTY. If there is no TTY, login exits immediately, which
// breaks things like mosh and VSCode.
return nil
}
}
loginCmdPath, err := exec.LookPath("login")
if err != nil {
dlogf("failed to get login args: %s", err)
+64
View File
@@ -0,0 +1,64 @@
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build (linux && !android) || (darwin && !ios) || freebsd || openbsd
package tailssh
import (
"fmt"
"testing"
)
func TestShouldUseLogin(t *testing.T) {
tests := []struct {
goos string
hasTTY bool
isShell bool
want bool
}{
// darwin: /usr/bin/login -pq exits 0 once its child is spawned,
// losing the child's exit status (#18256). Only interactive TTY
// shells go through login, where the PAM "remote" session and
// utmpx accounting are worth that trade; every exec and every
// non-TTY session needs the real exit status.
{darwin, true, true, true},
{darwin, true, false, false},
{darwin, false, true, false},
{darwin, false, false, false},
// linux: login can't exec commands and needs a TTY.
{linux, true, true, true},
{linux, true, false, false},
{linux, false, true, false},
{linux, false, false, false},
// freebsd: same as linux.
{freebsd, true, true, true},
{freebsd, true, false, false},
{freebsd, false, true, false},
{freebsd, false, false, false},
// openbsd: same as linux.
{openbsd, true, true, true},
{openbsd, true, false, false},
{openbsd, false, true, false},
{openbsd, false, false, false},
}
for _, tt := range tests {
name := fmt.Sprintf("%s/tty=%v/shell=%v", tt.goos, tt.hasTTY, tt.isShell)
t.Run(name, func(t *testing.T) {
got, reason := shouldUseLogin(tt.goos, tt.hasTTY, tt.isShell)
if got != tt.want {
t.Errorf("shouldUseLogin(%q, hasTTY=%v, isShell=%v) = %v (%q); want %v",
tt.goos, tt.hasTTY, tt.isShell, got, reason, tt.want)
}
if got && reason != "" {
t.Errorf("use=true should have empty reason, got %q", reason)
}
if !got && reason == "" {
t.Errorf("use=false should have non-empty reason")
}
})
}
}
@@ -244,12 +244,20 @@ func TestOpenSSHExitCodes(t *testing.T) {
tests := []struct {
name string
cmd string
cmd string // remote command; empty = shell session
stdin string // fed to the ssh client's stdin
forceTTY bool // -tt instead of -T
wantCode int
}{
{"success", "true", 0},
{"exit_code_passthrough", "exit 42", 42},
{"command_not_found", "/nonexistent/binary", 127},
{"success", "true", "", false, 0},
{"exit_code_passthrough", "exit 42", "", false, 42},
{"command_not_found", "/nonexistent/binary", "", false, 127},
// TTY exec and non-TTY stdin-fed shell: the two session shapes
// that, on darwin, used to route through /usr/bin/login -pq,
// which exits 0 once its child spawns and loses the child's
// exit status (#18256).
{"tty_exec_exit_code", "exit 42", "", true, 42},
{"stdin_shell_exit_code", "", "exit 42\n", false, 42},
}
for _, tt := range tests {
@@ -259,10 +267,16 @@ func TestOpenSSHExitCodes(t *testing.T) {
runOnce := func() (rc int, out []byte) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
ttyFlag := "-T"
if tt.forceTTY {
// Double -t forces a remote PTY even though the
// test binary itself has no controlling terminal.
ttyFlag = "-tt"
}
cmd := exec.CommandContext(ctx, sshPath,
"-vvv",
"-F", "/dev/null",
"-T",
ttyFlag,
"-o", "BatchMode=yes",
"-o", "ConnectTimeout=15",
"-o", "GSSAPIAuthentication=no",
@@ -278,8 +292,13 @@ func TestOpenSSHExitCodes(t *testing.T) {
"-o", "UserKnownHostsFile=/dev/null",
"-p", port,
username+"@"+host,
tt.cmd,
)
if tt.cmd != "" {
cmd.Args = append(cmd.Args, tt.cmd)
}
if tt.stdin != "" {
cmd.Stdin = strings.NewReader(tt.stdin)
}
o, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded {
t.Logf("ssh command timed out; output:\n%s", o)