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 <[email protected]>
(cherry picked from commit d484ac300e)
This commit is contained in:
James Tucker committed 2026-10-27 20:12:07 +00:00
1 parent 0284e166ca
commit 4f7d3cee7a
2 files changed
+57

No files matched your search

+21
View File
@@ -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)
+36
View File
@@ -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")
}
}
}