diff --git a/hostinfo/hostinfo.go b/hostinfo/hostinfo.go index 576a9cf52..4fc5aeb59 100644 --- a/hostinfo/hostinfo.go +++ b/hostinfo/hostinfo.go @@ -13,6 +13,7 @@ import ( "os/exec" "runtime" "runtime/debug" + "slices" "strings" "sync" "sync/atomic" @@ -500,10 +501,47 @@ func IsSELinuxEnforcing() bool { if runtime.GOOS != "linux" { return false } - out, _ := exec.Command("getenforce").Output() + cmd := exec.Command("getenforce") + cmd.Env = EnvironWithoutSystemdServiceVars(nil) + out, _ := cmd.Output() return string(bytes.TrimSpace(out)) == "Enforcing" } +// systemdServiceEnvKeys are the environment variables systemd sets for +// a service instance. See systemd.exec(5), sd_notify(3), +// sd_listen_fds(3). They describe the daemon's main process, not a +// child. With NOTIFY_SOCKET inherited, libsystemd-linked helpers such +// as loginctl notify the tailscaled unit from a foreign PID. The other +// variables give children the service's watchdog or listening fds. +var systemdServiceEnvKeys = []string{ + "NOTIFY_SOCKET", + "WATCHDOG_PID", + "WATCHDOG_USEC", + "LISTEN_FDS", + "LISTEN_PID", + "LISTEN_FDNAMES", + "INVOCATION_ID", + "JOURNAL_STREAM", +} + +// EnvironWithoutSystemdServiceVars returns env, or the current +// environment when nil, without the systemd service-instance +// variables. Assign it to cmd.Env when tailscaled execs a helper +// binary. +func EnvironWithoutSystemdServiceVars(env []string) []string { + if env == nil { + env = os.Environ() + } + ret := make([]string, 0, len(env)) + for _, kv := range env { + if k, _, ok := strings.Cut(kv, "="); ok && slices.Contains(systemdServiceEnvKeys, k) { + continue + } + ret = append(ret, kv) + } + return ret +} + // IsNATLabGuestVM reports whether the current host is a NAT Lab guest VM. func IsNATLabGuestVM() bool { if runtime.GOOS == "linux" && distro.Get() == distro.Gokrazy { diff --git a/hostinfo/hostinfo_linux.go b/hostinfo/hostinfo_linux.go index c0246b35d..41f26572d 100644 --- a/hostinfo/hostinfo_linux.go +++ b/hostinfo/hostinfo_linux.go @@ -206,7 +206,11 @@ type logindRunner interface { getSessionType(string) (string, error) } -// osLogindRunner implements [logindRunner] by shelling out to the system logind. +// osLogindRunner implements [logindRunner] by shelling out to the +// system logind. The probes drop the systemd service variables. +// loginctl sends sd_notify(3) messages on exit and error. systemd +// rejects these messages because they come from the wrong PID. See +// [EnvironWithoutSystemdServiceVars]. type osLogindRunner struct{ ctx context.Context } // listSessions implements [logindRunner] using os calls. @@ -216,6 +220,7 @@ func (r *osLogindRunner) listSessions() ([]byte, error) { return nil, err } cmd := exec.CommandContext(r.ctx, path, "list-sessions", "--json=short") + cmd.Env = EnvironWithoutSystemdServiceVars(nil) output, err := cmd.Output() if err != nil { return nil, err @@ -232,6 +237,7 @@ func (r *osLogindRunner) getSessionType(session string) (string, error) { } cmd := exec.CommandContext(r.ctx, path, "show-session", session, "--property=Type", "--value") + cmd.Env = EnvironWithoutSystemdServiceVars(nil) output, err := cmd.Output() if err != nil { return "", err diff --git a/hostinfo/hostinfo_linux_test.go b/hostinfo/hostinfo_linux_test.go index 63380bfad..92f3272d8 100644 --- a/hostinfo/hostinfo_linux_test.go +++ b/hostinfo/hostinfo_linux_test.go @@ -6,7 +6,12 @@ package hostinfo import ( + "bytes" + "context" "errors" + "os" + "path/filepath" + "strings" "testing" ) @@ -183,3 +188,44 @@ func TestSystemdLogindFindDesktop(t *testing.T) { }) } } + +// TestLogindProbesDropSystemdServiceEnv checks that the loginctl +// probes drop the systemd service variables. With NOTIFY_SOCKET +// inherited, systemd rejects each probe's sd_notify(3) messages as +// coming from the wrong PID. A fake loginctl dumps its environment, so +// the test sees exactly what the probe child inherited. +func TestLogindProbesDropSystemdServiceEnv(t *testing.T) { + dir := t.TempDir() + dump := filepath.Join(dir, "probe-env") + + script := "#!/bin/sh\nenv >\"" + dump + "\"\necho '[]'\n" + if err := os.WriteFile(filepath.Join(dir, "loginctl"), []byte(script), 0o755); err != nil { + t.Fatal(err) + } + + // Simulate the daemon's environment under a Type=notify systemd unit. + t.Setenv("NOTIFY_SOCKET", "/fake/systemd/notify") + t.Setenv("PROBE_SANITY_VAR", "kept") + t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) + + out, err := (&osLogindRunner{ctx: context.Background()}).listSessions() + if err != nil { + t.Fatal(err) + } + if string(bytes.TrimSpace(out)) != "[]" { + t.Fatalf("probe output = %q", out) + } + + b, err := os.ReadFile(dump) + if err != nil { + t.Fatalf("probe did not dump its environment: %v", err) + } + for _, kv := range strings.Split(string(b), "\n") { + if strings.HasPrefix(kv, "NOTIFY_SOCKET=") { + t.Errorf("loginctl probe inherited %q", kv) + } + } + if !strings.Contains(string(b), "PROBE_SANITY_VAR=kept") { + t.Errorf("probe environment missing an ordinary caller variable; got:\n%s", b) + } +} diff --git a/hostinfo/hostinfo_test.go b/hostinfo/hostinfo_test.go index 876300a5c..7966d0193 100644 --- a/hostinfo/hostinfo_test.go +++ b/hostinfo/hostinfo_test.go @@ -6,6 +6,7 @@ package hostinfo import ( "encoding/json" "os" + "slices" "strings" "testing" ) @@ -77,3 +78,45 @@ func TestCustomHostnameFunc(t *testing.T) { t.Errorf("got %q, want %q", got, want) } } + +func TestEnvironWithoutSystemdServiceVars(t *testing.T) { + got := EnvironWithoutSystemdServiceVars([]string{ + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "NOTIFY_SOCKET=/run/systemd/notify", + "HOME=/home/user", + "WATCHDOG_PID=1234", + "WATCHDOG_USEC=60000000", + "LISTEN_FDS=2", + "LISTEN_PID=1", + "LISTEN_FDNAMES=tailscaled.socket", + "INVOCATION_ID=57fad4dc31874d3882b8907c50d167a2", + "JOURNAL_STREAM=9:1234567", + "USER=ubuntu", + }) + want := []string{ + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "HOME=/home/user", + "USER=ubuntu", + } + if !slices.Equal(got, want) { + t.Errorf("got %q; want %q", got, want) + } + + // A nil env means the current environment. + t.Setenv("NOTIFY_SOCKET", "/fake/systemd/notify") + got = EnvironWithoutSystemdServiceVars(nil) + for _, kv := range got { + if strings.HasPrefix(kv, "NOTIFY_SOCKET=") { + t.Errorf("current-environment form leaked %q", kv) + } + } + found := false + for _, kv := range os.Environ() { + if kv == "NOTIFY_SOCKET=/fake/systemd/notify" { + found = true + } + } + if !found { + t.Errorf("test setup: NOTIFY_SOCKET not in os.Environ()") + } +} diff --git a/ssh/tailssh/incubator.go b/ssh/tailssh/incubator.go index 41b1a397c..e0b6257ef 100644 --- a/ssh/tailssh/incubator.go +++ b/ssh/tailssh/incubator.go @@ -92,6 +92,10 @@ func tryExecInDir(ctx context.Context, dir string) error { run := func(path string) error { cmd := exec.CommandContext(ctx, path) cmd.Dir = dir + // The probe runs in the daemon's environment. Strip the systemd + // service-instance variables; see + // hostinfo.EnvironWithoutSystemdServiceVars. + cmd.Env = hostinfo.EnvironWithoutSystemdServiceVars(nil) return cmd.Run() } diff --git a/ssh/tailssh/monitor.go b/ssh/tailssh/monitor.go index db5f13c4e..19677d1cc 100644 --- a/ssh/tailssh/monitor.go +++ b/ssh/tailssh/monitor.go @@ -197,11 +197,12 @@ func beMonitor(args []string) error { // Both raw socketpair ends are close-on-exec at creation, as is the // parent end's wrapUnixConn dup. // -// env is used exactly as given: a nil env gives the child an empty -// environment, never the spawner's. Callers spawn the privileged -// monitor with the daemon's environment and user-identity children with -// none, so the daemon's environment never reaches a child running as -// the connection's user. +// env is used exactly as given. A nil env gives the child an empty +// environment, never the spawner's. Callers give the privileged +// monitor the daemon's environment without the systemd service +// variables, and give user-identity children an empty environment. +// The daemon's environment never reaches a child running as the +// connection's user. func spawnRPCChild(prefix, exe string, argv, env []string, attr *syscall.SysProcAttr) (*exec.Cmd, *net.UnixConn, error) { pair, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_STREAM, 0) if err != nil { diff --git a/ssh/tailssh/monitor_client.go b/ssh/tailssh/monitor_client.go index 55de1b99a..3bae5733c 100644 --- a/ssh/tailssh/monitor_client.go +++ b/ssh/tailssh/monitor_client.go @@ -25,6 +25,8 @@ import ( "sync" "syscall" "time" + + "tailscale.com/hostinfo" ) // monitorSpawnPrefix is the argv prefix following the executable path, @@ -48,8 +50,13 @@ func spawnMonitor(exe string, uid, gid uint32, groups []uint32, localUser string "--local-user="+localUser, "--rpc-fd=3", ) - // The monitor is its own session leader. - cmd, parentConn, err := spawnRPCChild("ssh-monitor", exe, argv, os.Environ(), &syscall.SysProcAttr{Setsid: true}) + // The monitor is its own session leader. It is a tailscaled + // re-exec, not a service instance. Give it the daemon's environment + // without the systemd service variables, so neither the monitor + // nor any helper it execs can notify the tailscaled unit. See + // [hostinfo.EnvironWithoutSystemdServiceVars]. + cmd, parentConn, err := spawnRPCChild("ssh-monitor", exe, argv, + hostinfo.EnvironWithoutSystemdServiceVars(nil), &syscall.SysProcAttr{Setsid: true}) if err != nil { return nil, err } diff --git a/ssh/tailssh/monitor_env_test.go b/ssh/tailssh/monitor_env_test.go new file mode 100644 index 000000000..cd61dc86e --- /dev/null +++ b/ssh/tailssh/monitor_env_test.go @@ -0,0 +1,126 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build linux && !android + +package tailssh + +import ( + "fmt" + "net" + "os" + "path/filepath" + "strings" + "testing" + "time" + + gliderssh "github.com/tailscale/gliderssh" + gossh "golang.org/x/crypto/ssh" +) + +// TestNotifySocketDoesNotReachChildren runs a real session through a +// real monitor with NOTIFY_SOCKET set. The session child and the +// monitor must not hold the variable. Nothing may send a notification +// to the socket. The socket check is best effort, because it only +// observes the daemon-side loginctl probes, which run on Debian-family +// hosts. Hostinfo's TestLogindProbesDropSystemdServiceEnv pins the +// probe's own environment. +func TestNotifySocketDoesNotReachChildren(t *testing.T) { + sockPath := filepath.Join(t.TempDir(), "notify") + ln, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: sockPath, Net: "unixgram"}) + if err != nil { + t.Fatal(err) + } + defer ln.Close() + notifications := make(chan string, 16) + go func() { + for { + b := make([]byte, 4096) + n, rerr := ln.Read(b) + if rerr != nil { + return + } + select { + case notifications <- string(b[:n]): + default: + } + } + }() + + // The daemon-side environment, as under a Type=notify systemd unit. + t.Setenv("NOTIFY_SOCKET", sockPath) + + connCh := make(chan *conn, 1) + addr := newMonitorSessionServer(t, func(c *conn, s gliderssh.Session) { + select { + case connCh <- c: + default: + } + c.newSSHSession(s).run() + }) + + cl, err := gossh.Dial("tcp", addr, &gossh.ClientConfig{ + HostKeyCallback: gossh.InsecureIgnoreHostKey(), + Timeout: 15 * time.Second, + }) + if err != nil { + t.Fatal(err) + } + defer cl.Close() + s, err := cl.NewSession() + if err != nil { + t.Fatal(err) + } + var out sessTestBuffer + s.Stdout = &out + s.Stderr = &out + if err := s.Run("echo ENV-BEGIN; env; echo ENV-END"); err != nil { + t.Logf("session command: %v", err) + } + + // The session child must not see the notify socket in its + // environment. + envOut := out.String() + if !strings.Contains(envOut, "ENV-BEGIN") { + t.Fatalf("no session output; got %q", envOut) + } + if strings.Contains(envOut, "NOTIFY_SOCKET=") { + t.Errorf("session child environment contains NOTIFY_SOCKET:\n%s", envOut) + } + + // The monitor process must not hold the daemon's systemd + // service-instance variables either. + var c *conn + select { + case c = <-connCh: + case <-time.After(10 * time.Second): + t.Fatal("session never started") + } + var mc *monitorClient + deadline := time.Now().Add(10 * time.Second) + for mc == nil { + mc = c.connMonitorIfAny() + if mc == nil { + if time.Now().After(deadline) { + t.Fatal("connection monitor never spawned") + } + time.Sleep(50 * time.Millisecond) + } + } + monEnv, err := os.ReadFile(fmt.Sprintf("/proc/%d/environ", mc.Pid())) + if err != nil { + t.Fatalf("reading monitor (pid %d) environment: %v", mc.Pid(), err) + } + for _, kv := range strings.Split(string(monEnv), "\x00") { + if strings.HasPrefix(kv, "NOTIFY_SOCKET=") { + t.Errorf("ssh-monitor child environment contains %q", kv) + } + } + + // No child may send notifications to the notify socket. + select { + case n := <-notifications: + t.Errorf("notification sent by a child process: %q", n) + case <-time.After(2 * time.Second): + } +}