From 4f7d3cee7abd2a534298c2f95ebd99b7f3f4a43e Mon Sep 17 00:00:00 2001 From: James Tucker Date: Sat, 24 Oct 2026 00:00:37 +0000 Subject: [PATCH] ssh/tailssh: refuse direct-exec fallback for non-root users as root When tailscaledPath is empty, newIncubatorCommand runs the session's shell directly, with no be-child incubator to drop privileges. A root tailscaled could therefore run a non-root user's session as root. Refuse that case, mirroring canSwitchToLocalUser's semantics. Updates tailscale/corp#50380 Signed-off-by: James Tucker (cherry picked from commit d484ac300e0c14a5a19e0df8aa68aeb9ab817168) --- ssh/tailssh/incubator.go | 21 ++++++++++++++++++++ ssh/tailssh/incubator_test.go | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/ssh/tailssh/incubator.go b/ssh/tailssh/incubator.go index d611832c9..c20c1eaa3 100644 --- a/ssh/tailssh/incubator.go +++ b/ssh/tailssh/incubator.go @@ -108,6 +108,19 @@ func tryExecInDir(ctx context.Context, dir string) error { return exec.ErrNotFound } +// directExecAllowed reports whether the direct-exec fallback (used when +// ss.conn.srv.tailscaledPath is empty) may run a session for lu as the given +// effective uid. Unlike the incubator path there is no privilege drop, so a +// root tailscaled must refuse to run a non-root user's session as root, +// mirroring canSwitchToLocalUser's semantics (a non-root process can only +// run a session as itself, which run() enforces). +func directExecAllowed(euid int, lu *userMeta) bool { + if euid != 0 { + return true + } + return isRootUser(lu) +} + // newIncubatorCommand returns a new exec.Cmd configured with // `tailscaled be-child ssh` as the entrypoint. // @@ -147,6 +160,14 @@ func (ss *sshSession) newIncubatorCommand(logf logger.Logf) (cmd *exec.Cmd, forw return nil, nil, errors.New("no tailscaled found on path, can't serve SFTP") } + // Without tailscaled there is no be-child incubator to drop + // privileges, so this fallback runs the session as the current + // user. A root tailscaled must refuse to run a non-root user's + // session as root, mirroring canSwitchToLocalUser's semantics. + if !directExecAllowed(os.Geteuid(), ss.conn.localUser) { + return nil, nil, fmt.Errorf("refusing to run session for local user %q as root without tailscaled", ss.conn.localUser.Username) + } + loginShell := ss.conn.localUser.LoginShell() args := shellArgs(isShell, ss.RawCommand()) logf("directly running %s %q", loginShell, args) diff --git a/ssh/tailssh/incubator_test.go b/ssh/tailssh/incubator_test.go index de22aa22b..69f9e92a3 100644 --- a/ssh/tailssh/incubator_test.go +++ b/ssh/tailssh/incubator_test.go @@ -10,6 +10,7 @@ import ( "encoding/json" "fmt" "net/netip" + "os" "os/user" "slices" "strings" @@ -288,3 +289,38 @@ func TestLoadForwardedEnvSanitizesPairs(t *testing.T) { t.Errorf("allowlist = %q, want [SSH_AUTH_SOCK GOOD]", keys) } } + +// TestDirectExecRootFallbackRefused verifies that the direct-exec fallback +// (no tailscaledPath) refuses to run a non-root local user's session when +// tailscaled runs as root, since without the be-child incubator there is no +// privilege drop and the session would run as root. +func TestDirectExecRootFallbackRefused(t *testing.T) { + for _, tt := range []struct { + name string + euid int + uid string + want bool + }{ + {name: "root-euid-root-user", euid: 0, uid: "0", want: true}, + {name: "root-euid-noncanonical-root-user", euid: 0, uid: "00", want: true}, + {name: "root-euid-non-root-user", euid: 0, uid: "1000", want: false}, + {name: "non-root-euid", euid: 1000, uid: "1000", want: true}, + } { + t.Run(tt.name, func(t *testing.T) { + lu := &userMeta{User: user.User{Username: "alice", Uid: tt.uid, Gid: tt.uid}} + if got := directExecAllowed(tt.euid, lu); got != tt.want { + t.Errorf("directExecAllowed(euid=%d, uid=%s) = %v; want %v", tt.euid, tt.uid, got, tt.want) + } + }) + } + + // Wiring: as root, the direct-exec fallback must refuse the session + // for the non-root local user (newTestSession's alice is uid 1000). + if os.Geteuid() == 0 { + ss := newTestSession(t, nil, nil) + ss.conn.srv.tailscaledPath = "" + if _, _, err := ss.newIncubatorCommand(logger.Discard); err == nil { + t.Error("newIncubatorCommand (direct exec, root euid, non-root local user) = nil error; want refusal") + } + } +}