mirror of
https://github.com/tailscale/tailscale.git
synced 2026-10-29 06:28:57 -04:00
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 <james@tailscale.com>
127 lines
3.2 KiB
Go
127 lines
3.2 KiB
Go
// 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):
|
|
}
|
|
}
|