mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-20 00:31:17 -08:00
hostinfo,ssh/tailssh: don't leak systemd service env to child processes
tailscaled runs under a Type=notify unit, so NOTIFY_SOCKET is in its environment. The desktop probe ran loginctl with that environment inherited. loginctl sends sd_notify(3) messages on exit and error, and lives in the daemon's cgroup, so systemd took each message as coming from the tailscaled unit and rejected it: tailscaled.service: Got notification message from PID 1234, but reception only permitted for main PID 1000 One failing exec sends two messages, ERRNO=... and EXIT_STATUS=... That is the pair of rejected lines seen at each SSH session on Debian-family hosts. The per-connection ssh-monitor child also received the daemon's full environment. Helpers exec'd by tailscaled now get the daemon's environment without the systemd service-instance variables: NOTIFY_SOCKET, WATCHDOG_PID, WATCHDOG_USEC, LISTEN_FDS, LISTEN_PID, LISTEN_FDNAMES, INVOCATION_ID, JOURNAL_STREAM. The scrub covers the loginctl and getenforce probes, the direct-exec fallback probe, and the ssh-monitor spawn. Session children still never receive the daemon's environment. The monitor still gives children running as the connection's user no daemon variables. The daemon's own environment is unchanged. Updates tailscale/corp#50380 Change-Id: Ic686726473e851e2855e75eea11d7b3f75440311 Signed-off-by: James Tucker <[email protected]>
This commit is contained in:
1 parent
b101cb8bef
commit
f7c1b8e526
8 files changed
+280
-9
No files matched your search
+39
-1
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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()")
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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):
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user