diff --git a/ssh/tailssh/fwdchild.go b/ssh/tailssh/fwdchild.go new file mode 100644 index 000000000..897d8b4ab --- /dev/null +++ b/ssh/tailssh/fwdchild.go @@ -0,0 +1,415 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build (linux && !android) || (darwin && !ios) || freebsd || openbsd + +// The ssh-monitor spawns one-shot forwarding children as "tailscaled +// be-child ssh-fwd", one per dial/listen op. +// +// The child runs as the connection's bound identity, applied by the +// monitor as SysProcAttr.Credential. It performs one dial or bind, +// returns the result descriptor to the monitor over an inherited +// socketpair as one status line, and exits. +// +// The result protocol, from child to monitor over the socketpair, is one +// short newline-terminated line with at most one descriptor attached: +// +// ok [payload] success, with the agent socket path or the +// bound TCP address as the payload +// err the op failed, with the reason preserved in +// the reply +// +// The monitor side of the protocol, fwdSpawnChild and fwdReadResult +// below, drives these children. The op handlers live in monitor.go. + +package tailssh + +import ( + "bytes" + "errors" + "flag" + "fmt" + "io/fs" + "net" + "os" + "os/exec" + "path/filepath" + "runtime" + "slices" + "strings" + "syscall" + "time" + + "golang.org/x/sys/unix" +) + +// fwdUmask is applied before every unix bind so the socket is created +// directly at 0600, matching OpenSSH StreamLocalBindMask semantics. The +// agent dir is created before the umask change, so it keeps 0700. +const fwdUmask = 0o177 + +// fwdMaxStatus caps the child's status line. Anything longer is a broken +// child, not a long error message. +const fwdMaxStatus = 4096 + +// fwdChildSpawnPrefix is the argv prefix after the executable path, the +// one the monitor uses to spawn a forwarding child. In tailscaled it is +// "be-child ssh-fwd". Tests override it through the TS_TEST_SSH_FWD_PREFIX +// environment variable, a test-only knob. +var fwdChildSpawnPrefix = []string{"be-child", "ssh-fwd"} + +func init() { + if v := os.Getenv("TS_TEST_SSH_FWD_PREFIX"); v != "" { + fwdChildSpawnPrefix = strings.Fields(v) + } +} + +// fwdChildSpec is one validated forwarding op, as the monitor passes it to +// the child via argv. +type fwdChildSpec struct { + op string // "dial" or "listen" + net string // "unix" or "tcp" for listen. Dial is always unix + addr string // socket path for unix, address for tcp. Empty for agent + unlink bool // listen unix only: unlink-if-owned pre-step + agent bool // listen only: create the agent-forwarding socket +} + +// fwdCredGroups builds the supplementary group list for a forwarding +// child's Credential. The primary gid comes first, matching BSD/Darwin's +// egid-first getgroups convention. On darwin the list is truncated to 16 +// entries because the kernel's setgroups returns EINVAL beyond that. +func fwdCredGroups(gid int, gids []int) []uint32 { + groups := gids + if len(groups) == 0 || groups[0] != gid { + groups = append([]int{gid}, groups...) + } + if runtime.GOOS == "darwin" && len(groups) > 16 { + groups = groups[:16] + } + out := make([]uint32, len(groups)) + for i, g := range groups { + out[i] = uint32(g) + } + return out +} + +// fwdSpawnChild spawns one forwarding child with the monitor's spawn-bound +// identity and returns the child plus the monitor's end of the result +// socketpair. The child inherits its socketpair end as fd 3, passed +// as --fd=3. +func fwdSpawnChild(m *monitor, spec fwdChildSpec) (*exec.Cmd, *net.UnixConn, error) { + exe, err := os.Executable() + if err != nil { + return nil, nil, fmt.Errorf("ssh-fwd: os.Executable: %w", err) + } + + argv := append(slices.Clone(fwdChildSpawnPrefix), + "--op="+spec.op, + "--fd=3", + ) + if spec.agent { + argv = append(argv, "--agent") + } else { + argv = append(argv, "--net="+spec.net, "--addr="+spec.addr) + if spec.unlink { + argv = append(argv, "--unlink=if-owned") + } + } + // A non-root monitor running as the bound identity inherits it; any + // other identity under a non-root monitor fails the fork with EPERM, + // which surfaces as the op's error. + var attr *syscall.SysProcAttr + if os.Getuid() != 0 && m.args.uid == os.Getuid() && m.args.gid == os.Getgid() { + attr = monChildAttr(false) // Setpgid, and Pdeathsig on linux. Identity is inherited. + } else { + attr = fwdChildAttr(m.args) // born unprivileged (Credential) in its own group + } + return spawnRPCChild("ssh-fwd", exe, argv, attr) +} + +// fwdReadResult reads the one-shot child's status line and result +// descriptor from the socketpair, bounded by the deadline. +func fwdReadResult(c *net.UnixConn, deadline time.Time) (ok bool, payload string, fd int, err error) { + if err := c.SetReadDeadline(deadline); err != nil { + return false, "", -1, err + } + defer c.SetReadDeadline(time.Time{}) + oob := make([]byte, unix.CmsgSpace(4)) + var line []byte + var fds []int + defer func() { + if err != nil { + closeFds(fds) + } + }() + for { + if i := bytes.IndexByte(line, '\n'); i >= 0 { + status, rest, _ := strings.Cut(string(line[:i]), " ") + var got int = -1 + if len(fds) > 1 { + return false, "", -1, fmt.Errorf("child sent %d descriptors, want at most 1", len(fds)) + } + if len(fds) == 1 { + got, fds = fds[0], nil + } + switch status { + case "ok": + if got < 0 { + return false, "", -1, errors.New("child reported ok without a socket") + } + return true, rest, got, nil + case "err": + if got >= 0 { + // Refuse defensively. + unix.Close(got) + } + return false, rest, -1, nil + default: + return false, "", -1, fmt.Errorf("bad child status %q", string(line[:i])) + } + } + if len(line) > fwdMaxStatus { + return false, "", -1, fmt.Errorf("child status line exceeds %d bytes", fwdMaxStatus) + } + buf := make([]byte, 1024) + n, oobn, flags, _, rerr := c.ReadMsgUnix(buf, oob) + if rerr != nil { + // Deadline expiry with os.ErrDeadlineExceeded, or EOF from the + // child dying without a result. The caller distinguishes by context. + return false, "", -1, fmt.Errorf("waiting for child result: %w", rerr) + } + sets, ferr := recvFdSets(oob[:oobn], flags) + if ferr != nil { + return false, "", -1, ferr + } + for _, set := range sets { + fds = append(fds, set...) + } + line = append(line, buf[:n]...) + } +} + +// fwdArgs are the ssh-fwd child's arguments. +type fwdArgs struct { + op string + net string + addr string + unlink bool + agent bool + fd int +} + +// parseFwdArgs parses the arguments following "be-child ssh-fwd". +func parseFwdArgs(args []string) (fwdArgs, error) { + var fa fwdArgs + var unlink string + + flags := flag.NewFlagSet("ssh-fwd", flag.ContinueOnError) + flags.StringVar(&fa.op, "op", "", "the op to perform: dial or listen") + flags.StringVar(&fa.net, "net", "", "network: unix or tcp (dial is unix-only)") + flags.StringVar(&fa.addr, "addr", "", "absolute socket path (unix) or address (tcp)") + flags.StringVar(&unlink, "unlink", "", `listen unix only: pre-bind unlink policy, "if-owned"`) + flags.BoolVar(&fa.agent, "agent", false, "listen only: create the agent-forwarding socket") + flags.IntVar(&fa.fd, "fd", -1, "inherited AF_UNIX stream fd for the result") + if err := flags.Parse(args); err != nil { + return fa, err + } + if fa.fd < 3 { + return fa, fmt.Errorf("invalid --fd %d: must be >= 3 (an ExtraFiles fd)", fa.fd) + } + switch fa.op { + case "dial": + if fa.agent { + return fa, errors.New("dial does not take --agent") + } + if fa.net != "unix" { + return fa, fmt.Errorf("dial is unix-only, got --net=%q", fa.net) + } + if fa.addr == "" || !filepath.IsAbs(fa.addr) { + return fa, fmt.Errorf("dial: --addr must be an absolute unix socket path, got %q", fa.addr) + } + case "listen": + switch { + case fa.agent: + if fa.net != "" || fa.addr != "" || unlink != "" { + return fa, errors.New("listen --agent takes no --net/--addr/--unlink") + } + case fa.net == "unix": + if fa.addr == "" || !filepath.IsAbs(fa.addr) { + return fa, fmt.Errorf("listen unix: --addr must be an absolute socket path, got %q", fa.addr) + } + if unlink != "" && unlink != "if-owned" { + return fa, fmt.Errorf("listen unix: --unlink must be if-owned, got %q", unlink) + } + fa.unlink = unlink == "if-owned" + case fa.net == "tcp": + if fa.addr == "" { + return fa, errors.New("listen tcp: --addr is required") + } + default: + return fa, fmt.Errorf("listen: --net must be unix or tcp, got %q", fa.net) + } + default: + return fa, fmt.Errorf("unknown --op %q", fa.op) + } + return fa, nil +} + +// beFwdChild is the entrypoint of the "tailscaled be-child ssh-fwd" +// subcommand. It is the one-shot, born-unprivileged dialer/listener +// child. +func beFwdChild(args []string) error { + fa, err := parseFwdArgs(args) + if err != nil { + return err + } + // Mark the inherited result fd close-on-exec immediately at entry: + // the spawn-path dup arrives without the flag. + if _, err := unix.FcntlInt(uintptr(fa.fd), unix.F_SETFD, unix.FD_CLOEXEC); err != nil { + return fmt.Errorf("ssh-fwd: fcntl(F_SETFD) on fd %d: %w", fa.fd, err) + } + c, err := wrapUnixConn(fa.fd) + if err != nil { + return fmt.Errorf("ssh-fwd: result fd: %w", err) + } + f, payload, err := fwdChildWork(fa) + if err != nil { + if f != nil { + f.Close() + } + // The op's error is the result. Deliver it, then exit non-zero. + // The monitor has the reason either way. + if serr := fwdSendResult(c, nil, "err "+err.Error()); serr != nil { + return fmt.Errorf("ssh-fwd: %v (and reporting it: %w)", err, serr) + } + return err + } + if err := fwdSendResult(c, f, "ok "+payload); err != nil { + f.Close() + return fmt.Errorf("ssh-fwd: sending result: %w", err) + } + // Close our dup. The kernel holds its own reference from the sendmsg, + // so the monitor's copy survives. + f.Close() + return nil +} + +// fwdSendResult delivers the status line and the result descriptor. +func fwdSendResult(c *net.UnixConn, f *os.File, line string) error { + var fds []int + if f != nil { + fds = []int{int(f.Fd())} + } + _, err := sendFds(c, []byte(line+"\n"), fds) + return err +} + +// fwdChildWork performs the op and returns the resulting socket as an +// *os.File plus a short status payload. The caller sends the file's +// descriptor to the monitor and then closes the file. The payload is the +// agent socket path, or the bound TCP address. +func fwdChildWork(fa fwdArgs) (*os.File, string, error) { + switch { + case fa.op == "dial": + // Dial is unix-only. + conn, err := (&net.Dialer{}).Dial("unix", fa.addr) + if err != nil { + return nil, "", err + } + uc, ok := conn.(*net.UnixConn) + if !ok { + conn.Close() + return nil, "", fmt.Errorf("ssh-fwd: dial returned %T", conn) + } + f, err := uc.File() + uc.Close() + if err != nil { + return nil, "", err + } + return f, "", nil + + case fa.op == "listen" && fa.agent: + // Agent-forwarding socket. See fwdUmask and ListenAgent. The + // dir's cleanup is the caller's. + dir, err := os.MkdirTemp("", "auth-agent") + if err != nil { + return nil, "", err + } + sock := filepath.Join(dir, "listener.sock") + unix.Umask(fwdUmask) + l, err := net.Listen("unix", sock) + if err != nil { + return nil, "", err + } + f, err := l.(*net.UnixListener).File() + if err != nil { + // Close unlinks the just-created socket, which is the desired + // cleanup. + l.Close() + return nil, "", err + } + // The original listener is left open: closing a + // net.Listen("unix") listener unlinks the socket path. The dup in + // f keeps it alive, and the original dies with this short-lived + // process. + return f, sock, nil + + case fa.op == "listen" && fa.net == "unix": + // Umask first, so the socket is created directly at 0600. Then + // the optional if-owned pre-unlink, then the bind. Parent + // directories are not created; an inaccessible parent fails the + // bind. + unix.Umask(fwdUmask) + if fa.unlink { + fwdUnlinkIfOwned(fa.addr) + } + l, err := net.Listen("unix", fa.addr) + if err != nil { + return nil, "", err + } + f, err := l.(*net.UnixListener).File() + if err != nil { + l.Close() + return nil, "", err + } + // See the agent case. Closing the original would unlink the socket. + return f, "", nil + + case fa.op == "listen" && fa.net == "tcp": + // The bind runs with the bound user's credentials. Privileged + // ports fail EACCES at the kernel. + l, err := net.Listen("tcp", fa.addr) + if err != nil { + return nil, "", err + } + addr := l.Addr().String() + f, err := l.(*net.TCPListener).File() + l.Close() // tcp listeners have no path to unlink + if err != nil { + return nil, "", err + } + return f, addr, nil + + default: + return nil, "", fmt.Errorf("ssh-fwd: unhandled op shape %+v", fa) + } +} + +// fwdUnlinkIfOwned implements the listen op's unlink-if-owned pre-step: +// an existing socket at addr is removed only when it is a socket owned +// by this process's uid. When the entry is not ours, the subsequent +// bind fails EADDRINUSE and the existing socket stays untouched. +func fwdUnlinkIfOwned(addr string) { + fi, err := os.Lstat(addr) + if err != nil { + // ENOENT means nothing to do. Any other error is reported by the bind. + return + } + if fi.Mode()&fs.ModeSocket == 0 { + return + } + if st, ok := fi.Sys().(*syscall.Stat_t); ok && st.Uid != uint32(os.Getuid()) { + return // not ours: advisory refusal + } + _ = os.Remove(addr) +} diff --git a/ssh/tailssh/fwdchild_linux_test.go b/ssh/tailssh/fwdchild_linux_test.go new file mode 100644 index 000000000..603470aa3 --- /dev/null +++ b/ssh/tailssh/fwdchild_linux_test.go @@ -0,0 +1,32 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build linux && !android + +package tailssh + +import ( + "errors" + "net" + + "golang.org/x/sys/unix" +) + +// peerCredUID returns the effective uid of the peer of a connected unix +// socket, read from the listening side with SO_PEERCRED. +func peerCredUID(c net.Conn) (int, error) { + uc, ok := c.(*net.UnixConn) + if !ok { + return 0, errors.New("not a unix conn") + } + f, err := uc.File() + if err != nil { + return 0, err + } + defer f.Close() + u, err := unix.GetsockoptUcred(int(f.Fd()), unix.SOL_SOCKET, unix.SO_PEERCRED) + if err != nil { + return 0, err + } + return int(u.Uid), nil +} diff --git a/ssh/tailssh/fwdchild_other_test.go b/ssh/tailssh/fwdchild_other_test.go new file mode 100644 index 000000000..e65156c9d --- /dev/null +++ b/ssh/tailssh/fwdchild_other_test.go @@ -0,0 +1,17 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build (darwin && !ios) || freebsd || openbsd + +package tailssh + +import ( + "errors" + "net" +) + +// peerCredUID is not implemented on this platform; it always returns an +// error. +func peerCredUID(c net.Conn) (int, error) { + return 0, errors.New("peer cred not implemented on this platform") +} diff --git a/ssh/tailssh/fwdchild_test.go b/ssh/tailssh/fwdchild_test.go new file mode 100644 index 000000000..b2e0651c2 --- /dev/null +++ b/ssh/tailssh/fwdchild_test.go @@ -0,0 +1,788 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build (linux && !android) || (darwin && !ios) || freebsd || openbsd + +// Tests for the one-shot user-credential forwarding children and the +// monitor's dial/listen ops. They drive a real monitor and real one-shot +// fwd children through the self-exec seams: monitorSpawnPrefix in +// monitor_test.go for the monitor, fwdChildSpawnPrefix for the fwd +// children. Root runs use sudo and bind monitors to the non-root test +// user created by ensureFwdTestUser; those tests skip without root. +// The same-uid variants run everywhere. + +package tailssh + +import ( + "context" + "encoding/json" + "fmt" + "io" + "io/fs" + "net" + "os" + "os/exec" + "os/user" + "path/filepath" + "runtime" + "strconv" + "strings" + "syscall" + "testing" + "time" +) + +// fwdTestUser is the non-root user root runs bind test monitors to. It is +// created once via useradd if absent. +const fwdTestUser = "sshfwda" + +var testFwdPrefix = []string{"-test.run=^TestSSHFwdHelperProcess$", "-test.timeout=0", "--", "ssh-fwd"} + +// TestSSHFwdHelperProcess is not a test. It is the entry point of the +// re-exec'd ssh-fwd child: the monitor's fwdChildSpawnPrefix seam routes +// here in tests, and beFwdChild runs the op. In a normal test run there +// is no "--" in os.Args and it returns immediately. +func TestSSHFwdHelperProcess(t *testing.T) { + for i, a := range os.Args { + if a == "--" && i+1 < len(os.Args) && os.Args[i+1] == "ssh-fwd" { + if err := beFwdChild(os.Args[i+2:]); err != nil { + // The op's outcome was already reported on the result + // channel; the exit code mirrors it. + fmt.Fprintln(os.Stderr, "ssh-fwd child:", err) + os.Exit(1) + } + os.Exit(0) + } + } +} + +// startFwdTestMonitor spawns a real monitor whose fwd-child spawns are +// routed to the helper test above, bound to the given identity. +func startFwdTestMonitor(t *testing.T, uid, gid int, groups []int) *monitorClient { + t.Helper() + makeExecutableReachable(t) + // The override must reach the monitor process, which spawns the fwd + // children, so it travels through the environment. + t.Setenv("TS_TEST_SSH_FWD_PREFIX", strings.Join(testFwdPrefix, " ")) + old := monitorSpawnPrefix + monitorSpawnPrefix = testMonitorPrefix + t.Cleanup(func() { monitorSpawnPrefix = old }) + mc, err := spawnMonitor(monitorExe(t), uid, gid, groups, fwdTestUser) + if err != nil { + t.Fatalf("spawnMonitor: %v", err) + } + t.Cleanup(func() { + if err := mc.Close(); err != nil { + t.Errorf("close monitor: %v", err) + } + }) + return mc +} + +// makeExecutableReachable makes the test binary executable by +// non-root test users: the build directory /tmp/go-build*/ is created +// 0700 by the invoking user, so under a sudo run the non-root bound +// children get permission denied on fork/exec. Only permissions are +// relaxed. +func makeExecutableReachable(t *testing.T) { + t.Helper() + if os.Getuid() != 0 { + return // same-user runs: the builder could already exec it + } + exe, err := os.Executable() + if err != nil { + t.Fatal(err) + } + fi, err := os.Stat(exe) + if err != nil { + t.Fatal(err) + } + if fi.Mode().Perm()&0o055 != 0o055 { + if err := os.Chmod(exe, fi.Mode().Perm()|0o055); err != nil { + t.Fatal(err) + } + } + for dir := filepath.Dir(exe); ; dir = filepath.Dir(dir) { + fi, err := os.Stat(dir) + if err != nil { + t.Fatal(err) + } + if fi.Mode().Perm()&0o055 != 0o055 { + if err := os.Chmod(dir, fi.Mode().Perm()|0o055); err != nil { + t.Fatal(err) + } + } + if parent := filepath.Dir(dir); parent == dir { + return // reached / + } + } +} + +// ensureFwdTestUser returns the uid/gid of the non-root test user, +// creating it once via useradd if absent. Root runs only. Non-root runs +// skip. The same-uid variants still pin the monitor-side mechanics. +func ensureFwdTestUser(t *testing.T) (int, int) { + t.Helper() + if os.Getuid() != 0 { + t.Skip("root-only test: binds the monitor to a non-root test user (run under sudo; the user is created once via useradd)") + } + u, err := user.Lookup(fwdTestUser) + if err != nil { + useradd, lerr := exec.LookPath("useradd") + if lerr != nil { + t.Skipf("useradd unavailable (%v); cannot create test user %s", lerr, fwdTestUser) + } + out, xerr := exec.Command(useradd, "-M", fwdTestUser).CombinedOutput() + if xerr != nil { + t.Fatalf("useradd -M %s: %v: %s", fwdTestUser, xerr, out) + } + u, err = user.Lookup(fwdTestUser) + if err != nil { + t.Fatalf("lookup %s after creation: %v", fwdTestUser, err) + } + } + uid, err := strconv.Atoi(u.Uid) + if err != nil { + t.Fatalf("parse uid %q: %v", u.Uid, err) + } + gid, err := strconv.Atoi(u.Gid) + if err != nil { + t.Fatalf("parse gid %q: %v", u.Gid, err) + } + return uid, gid +} + +// fwdBoundIdentity returns the identity test monitors bind to. Under +// root runs it is the dedicated non-root test user, which pins the +// cross-uid behavior. Otherwise it is the current user. +func fwdBoundIdentity(t *testing.T) (int, int, []int) { + t.Helper() + if os.Getuid() == 0 { + uid, gid := ensureFwdTestUser(t) + return uid, gid, []int{gid} + } + return os.Getuid(), os.Getgid(), testGroups(t) +} + +// fwdWritableDir returns a dir the bound identity can create sockets +// in: under a root run the temp dir is chowned to the bound user, or +// the child's bind fails EACCES. +func fwdWritableDir(t *testing.T, uid, gid int) string { + t.Helper() + dir, err := os.MkdirTemp("", "fwdtest") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { os.RemoveAll(dir) }) + if os.Getuid() == 0 && uid != os.Getuid() { + if err := os.Chown(dir, uid, gid); err != nil { + t.Fatal(err) + } + } + return dir +} + +// connRoundTrip writes data on c and returns the echo, under deadlines. +func connRoundTrip(t *testing.T, c net.Conn, data string) string { + t.Helper() + if err := c.SetDeadline(time.Now().Add(5 * time.Second)); err != nil { + t.Fatal(err) + } + if _, err := c.Write([]byte(data)); err != nil { + t.Fatalf("write: %v", err) + } + buf := make([]byte, len(data)) + if _, err := io.ReadFull(c, buf); err != nil { + t.Fatalf("read: %v", err) + } + return string(buf) +} + +// TestFwdDialRoundTrip dials a unix socket through the monitor's +// one-shot child, bound to the current uid, and verifies data flows +// through the returned descriptor and that the listener observes the +// dialer child's credentials via SO_PEERCRED as the bound uid. The +// assertion is Linux-only; elsewhere the helper is a no-op and the +// check degrades to a log line. +func TestFwdDialRoundTrip(t *testing.T) { + mc := startFwdTestMonitor(t, os.Getuid(), os.Getgid(), testGroups(t)) + + dir := fwdWritableDir(t, os.Getuid(), os.Getgid()) + path := filepath.Join(dir, "echo.sock") + l, err := net.Listen("unix", path) + if err != nil { + t.Fatal(err) + } + defer l.Close() + + type peerNote struct { + uid int + err error + } + peers := make(chan peerNote, 1) + go func() { + c, err := l.Accept() + if err != nil { + peers <- peerNote{0, err} + return + } + defer c.Close() + uid, perr := peerCredUID(c) + peers <- peerNote{uid, perr} + c.SetDeadline(time.Now().Add(5 * time.Second)) + b := make([]byte, 64) + n, _ := c.Read(b) + c.Write(b[:n]) + }() + + ctx, cancel := testCtx() + defer cancel() + c, err := mc.Dial(ctx, path) + if err != nil { + t.Fatalf("monitor dial: %v", err) + } + defer c.Close() + if got := connRoundTrip(t, c, "ping"); got != "ping" { + t.Fatalf("echo through monitor-dialed socket = %q, want %q", got, "ping") + } + if p := <-peers; p.err == nil { + if p.uid != os.Getuid() { + t.Fatalf("listener observed SO_PEERCRED euid %d, want the bound uid %d", p.uid, os.Getuid()) + } + } else { + t.Logf("SO_PEERCRED not asserted on %s: %v", runtime.GOOS, p.err) + } + + // A nonexistent path is an ordinary error reply, and the channel stays + // healthy afterwards. + if _, err := mc.Dial(ctx, filepath.Join(dir, "absent.sock")); err == nil { + t.Fatal("dial of nonexistent socket should fail") + } + if err := mc.Ping(ctx); err != nil { + t.Fatalf("ping after failed dial: %v", err) + } +} + +// TestFwdDialCrossUser is the cross-uid dial pin. Root runs need a +// root-owned socket and a non-root bound monitor, via the sshfwda test +// user. With the monitor bound to the non-root user: +// +// - a root-owned 0600 unix socket cannot be dialed +// - a world-accessible root-owned socket can be dialed, and the +// listener observes SO_PEERCRED euid == the bound uid +func TestFwdDialCrossUser(t *testing.T) { + uid, gid := ensureFwdTestUser(t) + mc := startFwdTestMonitor(t, uid, gid, []int{gid}) + ctx, cancel := testCtx() + defer cancel() + + dir := t.TempDir() // root-owned; sockets live here, binds do not + // Make the dir chain below /tmp traversable so the dial pins are + // about the socket mode, not the root-owned 0700 temp dirs. /tmp + // itself stays untouched; its sticky bit must survive for the + // unlink-if-owned test's semantics. + for d := dir; d != "/tmp"; d = filepath.Dir(d) { + if fi, err := os.Stat(d); err != nil { + t.Fatal(err) + } else if fi.Mode().Perm()&0o055 != 0o055 { + if err := os.Chmod(d, fi.Mode().Perm()|0o055); err != nil { + t.Fatal(err) + } + } + if filepath.Dir(d) == d { + break + } + } + + priv := filepath.Join(dir, "priv.sock") + l0, err := net.Listen("unix", priv) + if err != nil { + t.Fatal(err) + } + defer l0.Close() + if err := os.Chmod(priv, 0o600); err != nil { + t.Fatal(err) + } + if _, err := mc.Dial(ctx, priv); err == nil { + t.Fatal("dial of a root-owned 0600 socket as a non-root-bound monitor should fail (DAC)") + } else { + t.Logf("root-owned 0600 dial (want permission denied): %v", err) + if !strings.Contains(err.Error(), "permission denied") { + t.Errorf("want an EACCES-flavored error, got: %v", err) + } + } + + pub := filepath.Join(dir, "pub.sock") + l1, err := net.Listen("unix", pub) + if err != nil { + t.Fatal(err) + } + defer l1.Close() + if err := os.Chmod(pub, 0o666); err != nil { + t.Fatal(err) + } + peers := make(chan int, 1) + go func() { + c, err := l1.Accept() + if err != nil { + peers <- -1 + return + } + defer c.Close() + pu, perr := peerCredUID(c) + if perr != nil { + peers <- -1 + return + } + peers <- pu + c.SetDeadline(time.Now().Add(5 * time.Second)) + b := make([]byte, 16) + c.Read(b) + }() + c, err := mc.Dial(ctx, pub) + if err != nil { + t.Fatalf("dial of an accessible socket as the bound user: %v", err) + } + c.SetDeadline(time.Now().Add(5 * time.Second)) + c.Write([]byte("x")) + c.Close() + select { + case pu := <-peers: + if pu == -1 { + t.Skipf("SO_PEERCRED helper unavailable on %s", runtime.GOOS) + } + t.Logf("SO_PEERCRED observed euid %d == bound uid %d (the dialer ran as the bound user, never as the root monitor)", pu, uid) + if pu != uid { + t.Fatalf("SO_PEERCRED euid = %d, want the bound uid %d (and NOT 0)", pu, uid) + } + case <-time.After(5 * time.Second): + t.Fatal("listener never observed the dial") + } +} + +// TestFwdListenUnix binds a unix listener through the bound-identity child +// and pins the socket's construction: mode 0600 from creation, owner +// equal to the bound uid. The listener serves a connection end to end. +func TestFwdListenUnix(t *testing.T) { + uid, gid, groups := fwdBoundIdentity(t) + mc := startFwdTestMonitor(t, uid, gid, groups) + ctx, cancel := testCtx() + defer cancel() + + dir := fwdWritableDir(t, uid, gid) + path := filepath.Join(dir, "l.sock") + ln, err := mc.Listen(ctx, "unix", path, false) + if err != nil { + t.Fatalf("monitor listen unix: %v", err) + } + defer ln.Close() + + fi, err := os.Lstat(path) + if err != nil { + t.Fatal(err) + } + if got := fi.Mode().Perm(); got != 0o600 { + t.Errorf("socket mode = %o, want 600 (umask-at-bind)", got) + } + if fi.Mode()&fs.ModeSocket == 0 { + t.Errorf("%s is not a socket", path) + } + if st, ok := fi.Sys().(*syscall.Stat_t); ok && st.Uid != uint32(uid) { + t.Errorf("socket owner uid = %d, want the bound uid %d", st.Uid, uid) + } + + // Connect through it: accept works, data flows. + done := make(chan string, 1) + go func() { + a, err := ln.Accept() + if err != nil { + done <- "" + return + } + defer a.Close() + a.SetDeadline(time.Now().Add(5 * time.Second)) + b := make([]byte, 64) + n, _ := a.Read(b) + a.Write(b[:n]) + done <- string(b[:n]) + }() + c, err := net.Dial("unix", path) + if err != nil { + t.Fatalf("connect to monitor listener: %v", err) + } + defer c.Close() + if got := connRoundTrip(t, c, "ping"); got != "ping" { + t.Fatalf("echo through monitor listener = %q, want %q", got, "ping") + } + if got := <-done; got != "ping" { + t.Fatalf("accepted side saw %q, want %q", got, "ping") + } + + // Binding the same live path again without unlink fails EADDRINUSE. + if _, err := mc.Listen(ctx, "unix", path, false); err == nil { + t.Fatal("listen at an existing live socket path without unlink should fail") + } else if !strings.Contains(err.Error(), "address already in use") { + t.Errorf("want an EADDRINUSE-flavored error, got: %v", err) + } +} + +// TestFwdListenUnlinkIfOwned pins the listen op's unlink-if-owned +// semantics: the conn's own stale socket, left by a previous listen, +// is removed and rebound, and a foreign-owned socket is left alone +// with the bind failing EADDRINUSE. See fwdUnlinkIfOwned. The +// foreign-owned half needs root (creating a foreign-owned live +// socket). +func TestFwdListenUnlinkIfOwned(t *testing.T) { + uid, gid, groups := fwdBoundIdentity(t) + mc := startFwdTestMonitor(t, uid, gid, groups) + ctx, cancel := testCtx() + defer cancel() + + dir := fwdWritableDir(t, uid, gid) + path := filepath.Join(dir, "u.sock") + + ln1, err := mc.Listen(ctx, "unix", path, false) + if err != nil { + t.Fatalf("first listen: %v", err) + } + ln1.Close() // leaves the socket file in place (no unlink) + if _, err := os.Lstat(path); err != nil { + t.Fatalf("stale socket file should exist after Close: %v", err) + } + ln2, err := mc.Listen(ctx, "unix", path, true) + if err != nil { + t.Fatalf("listen with unlink-if-owned over own stale socket: %v", err) + } + defer ln2.Close() + c, err := net.Dial("unix", path) + if err != nil { + t.Fatalf("connect to rebound socket: %v", err) + } + c.Close() + + if os.Getuid() == 0 && uid != 0 { + // A foreign-owned live socket in the canonical sticky dir: the + // child's lstat refuses the unlink, the bind fails, and the + // original socket is untouched. + foreign := filepath.Join("/tmp", fmt.Sprintf("fwdtest-foreign-%d.sock", os.Getpid())) + fl, err := net.Listen("unix", foreign) + if err != nil { + t.Fatal(err) + } + defer fl.Close() + defer os.Remove(foreign) + if _, err := mc.Listen(ctx, "unix", foreign, true); err == nil { + t.Fatal("listen over a foreign-owned socket should fail") + } else if !strings.Contains(err.Error(), "address already in use") { + t.Errorf("want an EADDRINUSE-flavored error, got: %v", err) + } + // Untouched: the original socket still accepts. + c, err := net.Dial("unix", foreign) + if err != nil { + t.Fatalf("original socket dead after refused listen: %v", err) + } + c.Close() + } else if os.Getuid() != 0 { + t.Log("foreign-owned half of the unlink test is root-only (needs a socket owned by another user)") + } +} + +// TestFwdListenTCP binds TCP listeners as the bound (non-root) identity: +// loopback and wildcard binds work, the reply echoes the +// concrete bound address, and a privileged port fails EACCES by kernel +// construction. +func TestFwdListenTCP(t *testing.T) { + uid, gid, groups := fwdBoundIdentity(t) + if uid == 0 { + t.Skip("bound identity is root: privileged ports would succeed; the non-root-bound variant is the meaningful pin") + } + mc := startFwdTestMonitor(t, uid, gid, groups) + ctx, cancel := testCtx() + defer cancel() + + ln, err := mc.Listen(ctx, "tcp", "127.0.0.1:0", false) + if err != nil { + t.Fatalf("listen tcp 127.0.0.1:0: %v", err) + } + defer ln.Close() + if _, portStr, err := net.SplitHostPort(ln.Addr().String()); err != nil { + t.Fatal(err) + } else if port, _ := strconv.Atoi(portStr); port == 0 { + t.Fatalf("bound port = %q, want a concrete port", portStr) + } + + // The reply echoes the bound address. + r, fds, err := mc.call(ctx, rpcListenReq(0, "tcp", "127.0.0.1:0", false, false)) + if err != nil { + t.Fatalf("raw listen call: %v", err) + } + if r.Addr == "" { + closeFds(fds) + t.Fatalf("listen reply Addr echo missing: %+v", r) + } + if len(fds) != 1 { + closeFds(fds) + t.Fatalf("listen reply carried %d fds, want 1", len(fds)) + } + if ln2, err := wrapListenerFD(fds[0], r.Addr); err != nil { + t.Fatalf("wrap listener fd: %v", err) + } else { + ln2.Close() + } + + lnAll, err := mc.Listen(ctx, "tcp", "0.0.0.0:0", false) + if err != nil { + t.Fatalf("listen tcp 0.0.0.0:0: %v", err) + } + lnAll.Close() + + // A privileged port fails EACCES: the bind runs as the bound user. + for _, p := range []string{"1", "13", "23", "37"} { + _, err := mc.Listen(ctx, "tcp", "127.0.0.1:"+p, false) + if err == nil { + t.Fatalf("privileged bind 127.0.0.1:%s succeeded as uid %d: privilege leak", p, uid) + } + if strings.Contains(err.Error(), "permission denied") { + t.Logf("privileged port %s as uid %d (want permission denied): %v", p, uid, err) + return + } + // Not EACCES: fall through to the next candidate port (e.g. one + // already in use); exhausting them all fails below. + } + t.Fatal("no privileged-port candidate produced permission denied") +} + +// TestFwdListenAgent pins the agent-forwarding socket's construction: +// a user-owned 0700 temp dir and a 0600 socket inside it, created by +// umask-at-bind, the path +// in the reply, a working accept, and caller-side RemoveAll cleanup. +func TestFwdListenAgent(t *testing.T) { + uid, gid, groups := fwdBoundIdentity(t) + mc := startFwdTestMonitor(t, uid, gid, groups) + ctx, cancel := testCtx() + defer cancel() + + ln, sockPath, err := mc.ListenAgent(ctx) + if err != nil { + t.Fatalf("listen agent: %v", err) + } + defer ln.Close() + + dir := filepath.Dir(sockPath) + if base := filepath.Base(dir); !strings.HasPrefix(base, "auth-agent") { + t.Errorf("agent dir base = %q, want an auth-agent* temp dir", base) + } + dfi, err := os.Lstat(dir) + if err != nil { + t.Fatal(err) + } + if got := dfi.Mode().Perm(); got != 0o700 { + t.Errorf("agent dir mode = %o, want 700", got) + } + if st, ok := dfi.Sys().(*syscall.Stat_t); ok && st.Uid != uint32(uid) { + t.Errorf("agent dir owner uid = %d, want the bound uid %d", st.Uid, uid) + } + sfi, err := os.Lstat(sockPath) + if err != nil { + t.Fatal(err) + } + if sfi.Mode()&fs.ModeSocket == 0 { + t.Errorf("%s is not a socket", sockPath) + } + if got := sfi.Mode().Perm(); got != 0o600 { + t.Errorf("agent socket mode = %o, want 600 (umask-at-bind)", got) + } + if st, ok := sfi.Sys().(*syscall.Stat_t); ok && st.Uid != uint32(uid) { + t.Errorf("agent socket owner uid = %d, want the bound uid %d", st.Uid, uid) + } + + // Connect through it; the listener accepts. + c, err := net.Dial("unix", sockPath) + if err != nil { + t.Fatalf("connect to agent socket: %v", err) + } + if err := c.SetDeadline(time.Now().Add(5 * time.Second)); err != nil { + t.Fatal(err) + } + if _, err := c.Write([]byte("x")); err != nil { + t.Fatal(err) + } + c.Close() + accepted := make(chan error, 1) + go func() { + a, err := ln.Accept() + if a != nil { + a.Close() + } + accepted <- err + }() + if err := <-accepted; err != nil { + t.Fatalf("accept on agent listener: %v", err) + } + + // Caller-side cleanup of the whole dir. + if err := os.RemoveAll(dir); err != nil { + t.Fatalf("RemoveAll of agent dir: %v", err) + } +} + +// TestFwdZombieHygiene pins that one-shot fwd children are reaped +// promptly: after a burst of dials, none of the monitor's children remain +// zombies within ~1s. The SIGCHLD-driven sweep is the bound. +func TestFwdZombieHygiene(t *testing.T) { + if runtime.GOOS != "linux" { + t.Skip("zombie scan via /proc is linux-only") + } + mc := startFwdTestMonitor(t, os.Getuid(), os.Getgid(), testGroups(t)) + + dir := fwdWritableDir(t, os.Getuid(), os.Getgid()) + path := filepath.Join(dir, "z.sock") + l, err := net.Listen("unix", path) + if err != nil { + t.Fatal(err) + } + defer l.Close() + go func() { + for { + c, err := l.Accept() + if err != nil { + return + } + c.Close() + } + }() + ctx, cancel := testCtx() + defer cancel() + for i := 0; i < 5; i++ { + c, err := mc.Dial(ctx, path) + if err != nil { + t.Fatalf("dial %d: %v", i, err) + } + c.Close() + } + deadline := time.Now().Add(time.Second) + for { + if n := zombieChildrenOf(mc.Pid()); n == 0 { + return + } else if time.Now().After(deadline) { + t.Fatalf("%d zombie children of monitor %d remain after 1s", n, mc.Pid()) + } + time.Sleep(20 * time.Millisecond) + } +} + +// zombieChildrenOf counts a process's zombie children by scanning /proc +// (state Z with the given ppid). +func zombieChildrenOf(ppid int) int { + matches, err := filepath.Glob("/proc/[0-9]*/stat") + if err != nil { + return 0 + } + n := 0 + for _, m := range matches { + b, err := os.ReadFile(m) + if err != nil { + continue + } + s := string(b) + i := strings.LastIndexByte(s, ')') + if i < 0 || i+2 >= len(s) { + continue + } + fields := strings.Fields(s[i+2:]) + if len(fields) < 2 { + continue + } + if fields[0] == "Z" && fields[1] == strconv.Itoa(ppid) { + n++ + } + } + return n +} + +// TestFwdRequestsCarryNoIdentity pins that the dial/listen request +// shapes carry no identity keys on the wire. +func TestFwdRequestsCarryNoIdentity(t *testing.T) { + for _, f := range []rpcFrame{ + rpcDialReq(1, "/tmp/x.sock"), + rpcListenReq(2, "unix", "/tmp/x.sock", false, false), + rpcListenReq(3, "unix", "/tmp/x.sock", true, false), + rpcListenReq(4, "tcp", "127.0.0.1:0", false, false), + rpcListenReq(5, "", "", false, true), + } { + b, err := json.Marshal(f) + if err != nil { + t.Fatal(err) + } + s := string(b) + for _, key := range []string{`"uid"`, `"gid"`, `"groups"`, `"user"`, `"euid"`, `"cred"`} { + if strings.Contains(s, key) { + t.Fatalf("op %s request %s carries identity key %s", f.Op, s, key) + } + } + } + + // The ops refuse unsupported shapes (dial is unix-only and absolute; + // listen net must be unix, tcp, or agent), and the channel stays + // healthy afterwards. + mc := startFwdTestMonitor(t, os.Getuid(), os.Getgid(), testGroups(t)) + ctx, cancel := testCtx() + defer cancel() + for _, f := range []rpcFrame{ + {Op: opDial, Net: "tcp", Addr: "127.0.0.1:1"}, + {Op: opDial, Net: "unix", Addr: "relative.sock"}, + {Op: opDial, Net: "unix", Addr: "@/abstract"}, + {Op: opListen, Net: "udp", Addr: "x"}, + {Op: opListen, Net: "unix", Addr: "relative.sock"}, + } { + if _, _, err := mc.call(ctx, f); err == nil { + t.Fatalf("op %s %v: unexpectedly accepted", f.Op, f) + } + } + if err := mc.Ping(ctx); err != nil { + t.Fatalf("ping after refused ops: %v", err) + } +} + +// TestFwdDialLatencyBudget measures the per-dial cost: the spawn of the +// one-shot child, in production a full tailscaled binary re-exec, plus +// the unix dial round trip. The numbers are logged; the test makes no +// tight assertion. +func TestFwdDialLatencyBudget(t *testing.T) { + mc := startFwdTestMonitor(t, os.Getuid(), os.Getgid(), testGroups(t)) + + dir := fwdWritableDir(t, os.Getuid(), os.Getgid()) + path := filepath.Join(dir, "b.sock") + l, err := net.Listen("unix", path) + if err != nil { + t.Fatal(err) + } + defer l.Close() + go func() { + for { + c, err := l.Accept() + if err != nil { + return + } + c.Close() + } + }() + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + const n = 20 + var total, worst time.Duration + for i := 0; i < n; i++ { + start := time.Now() + c, err := mc.Dial(ctx, path) + if err != nil { + t.Fatalf("dial %d: %v", i, err) + } + d := time.Since(start) + total += d + if d > worst { + worst = d + } + c.Close() + } + t.Logf("per-dial budget (n=%d): mean %v, worst %v (spawn of the one-shot fwd child + unix dial round trip)", n, total/n, worst) +} diff --git a/ssh/tailssh/incubator.go b/ssh/tailssh/incubator.go index a3bcd3601..299c05b94 100644 --- a/ssh/tailssh/incubator.go +++ b/ssh/tailssh/incubator.go @@ -61,6 +61,7 @@ func registerIncubator() { childproc.Add("ssh", beIncubator) childproc.Add("sftp", beSFTP) childproc.Add("ssh-monitor", beMonitor) + childproc.Add("ssh-fwd", beFwdChild) } var ptyName = func(f *os.File) (string, error) { diff --git a/ssh/tailssh/monitor.go b/ssh/tailssh/monitor.go index e4874c3d5..e86228a42 100644 --- a/ssh/tailssh/monitor.go +++ b/ssh/tailssh/monitor.go @@ -38,11 +38,13 @@ import ( const ( // monExecOpDeadline bounds the exec op's spawn-and-reply phase. It - // does not bound the child's lifetime. monDialOpDeadline bounds a - // one-shot op from spawn to result. - monDialOpDeadline = 10 * time.Second + // does not bound the child's lifetime. monExecOpDeadline = 30 * time.Second + // monFwdOpDeadline bounds a one-shot forwarding child from spawn to + // result. On expiry the child's group is SIGKILLed. + monFwdOpDeadline = 10 * time.Second + // monTeardownReap bounds each reap sweep in teardown. A child still // alive after the SIGKILL sweep is uninterruptible. It is left in // its own group and abandoned. @@ -50,9 +52,12 @@ const ( ) // monChild is one entry of the monitor's child table, keyed by pid. -// Every child leads its own group, so pgid equals pid. +// Every child leads its own group, so pgid equals pid. A oneshot +// child, a dialer or listener, reports its outcome in its op's reply, +// and its exit is reaped silently by the sweep after its SIGCHLD. type monChild struct { - pgid int // the child's process group. Equal to its pid. + pgid int // the child's process group. Equal to its pid. + oneshot bool // dialer or listener child. Outcome carried by the op reply. } // monitor is the running ssh-monitor process state, owned entirely by @@ -259,10 +264,12 @@ func (m *monitor) handle(f rpcFrame, fds []int) { case opSignal: closeFds(fds) m.handleSignal(f) - case opDial, opListen: - // Reply a descriptive error rather than ignoring the frame. + case opDial: closeFds(fds) - m.reply(rpcErrReply(f.ID, f.Op, fmt.Errorf("op %q not implemented", f.Op))) + m.handleDial(f) + case opListen: + closeFds(fds) + m.handleListen(f) default: closeFds(fds) m.reply(rpcErrReply(f.ID, f.Op, fmt.Errorf("unknown op %q", f.Op))) @@ -294,6 +301,9 @@ func (m *monitor) handleExec(f rpcFrame, fds []int) { cmd.Env = f.Env } cmd.Dir = monChildDir(f.Dir) + // monStdio wraps the stdio descriptors. A failure before ownership + // moves to files closes every descriptor exactly once, in + // monStdio's error path. files, err := monStdio(fds) if err != nil { // monStdio's error path already closed every descriptor. Nil @@ -383,11 +393,121 @@ func killGroup(pgid int, sig unix.Signal) error { return unix.Kill(-pgid, sig) } +// handleDial serves the dial op. A one-shot dialer child dials the +// unix socket with the connection's bound identity, as documented in +// fwdchild.go. The connected descriptor is relayed to tailscaled in +// the reply. +func (m *monitor) handleDial(f rpcFrame) { + if f.Net != "unix" { + m.reply(rpcErrReply(f.ID, opDial, fmt.Errorf("dial: network %q not supported; dial is unix-only", f.Net))) + return + } + if f.Addr == "" || !filepath.IsAbs(f.Addr) { + m.reply(rpcErrReply(f.ID, opDial, errors.New("dial: addr must be an absolute unix socket path"))) + return + } + m.fwdOp(f.ID, opDial, fwdChildSpec{op: "dial", net: "unix", addr: f.Addr}, func(string) rpcFrame { + return rpcOKReply(f.ID, opDial) + }) +} + +// handleListen serves the listen op. A one-shot listener child runs +// with the bound identity. See fwdchild.go for the socket and dir +// construction. The reply carries the resulting path or address. +func (m *monitor) handleListen(f rpcFrame) { + spec := fwdChildSpec{op: "listen"} + switch { + case f.Agent: + spec.agent = true + case f.Net == "unix": + if f.Addr == "" || !filepath.IsAbs(f.Addr) { + m.reply(rpcErrReply(f.ID, opListen, errors.New("listen unix: addr must be an absolute socket path"))) + return + } + spec.net, spec.addr, spec.unlink = "unix", f.Addr, f.Unlink + case f.Net == "tcp": + if f.Addr == "" { + m.reply(rpcErrReply(f.ID, opListen, errors.New("listen tcp: addr is required"))) + return + } + spec.net, spec.addr = "tcp", f.Addr + default: + m.reply(rpcErrReply(f.ID, opListen, fmt.Errorf("listen: network %q not supported (unix, tcp, or agent)", f.Net))) + return + } + m.fwdOp(f.ID, opListen, spec, func(payload string) rpcFrame { + r := rpcFrame{ID: f.ID, Op: opListen, OK: true} + switch { + case f.Agent: + r.Path = payload // the agent socket path + case f.Net == "unix": + r.Path = f.Addr // echo the bound path + case f.Net == "tcp": + r.Addr = payload // echo the bound address (a 127.0.0.1:0 request yields the concrete port) + } + return r + }) +} + +// fwdOp runs one one-shot forwarding op. It spawns the child with the +// bound identity, waits for its result under monFwdOpDeadline, and +// relays the child's socket descriptor in the ok reply. The wait blocks +// the loop for at most the deadline. +func (m *monitor) fwdOp(id int64, op string, spec fwdChildSpec, reply func(payload string) rpcFrame) { + start := time.Now() + cmd, result, err := fwdSpawnChild(m, spec) + if err != nil { + m.reply(rpcErrReply(id, op, err)) + return + } + child := &monChild{pgid: cmd.Process.Pid, oneshot: true} + m.children[cmd.Process.Pid] = child + ok, payload, fd, rerr := fwdReadResult(result, start.Add(monFwdOpDeadline)) + result.Close() + if rerr != nil { + m.killChild(child) + m.reply(rpcErrReply(id, op, fmt.Errorf("%s: %w", op, rerr))) + return + } + if !ok { + // The child's failure reason is preserved verbatim. It is the + // child's own error, e.g. the kernel's EACCES for the bound user. + m.reply(rpcErrReply(id, op, errors.New(payload))) + return + } + // The child exits by itself right after its send. The sweep reaps + // it silently after its SIGCHLD. + m.replyFD(reply(payload), fd) +} + +// killChild SIGKILLs a one-shot child's group and reaps it with +// killGroupAndReap, a bounded non-blocking wait. A child that survives +// SIGKILL for the budget is left in the table, reaped by the sweep +// whenever it dies. +func (m *monitor) killChild(c *monChild) { + _ = killGroup(c.pgid, unix.SIGKILL) + if killGroupAndReap(c.pgid) { + delete(m.children, c.pgid) + } +} + +// replyFD writes a reply carrying one descriptor and closes the +// monitor's copy after the write. After sendmsg the kernel holds its +// own reference to the passed file, so the peer's copy survives ours +// closing. +func (m *monitor) replyFD(f rpcFrame, fd int) { + err := m.conn.writeFrame(f, fd) + unix.Close(fd) + if err != nil { + m.broken = true + } +} + // sweep reaps exited children with wait4(WNOHANG) and drops them from // the child table. With notify set, it emits one unsolicited exit // notification per reaped pid. func (m *monitor) sweep(notify bool) { - for pid := range m.children { + for pid, c := range m.children { var ws unix.WaitStatus wpid, err := unix.Wait4(pid, &ws, unix.WNOHANG, nil) switch { @@ -396,7 +516,7 @@ func (m *monitor) sweep(notify bool) { // either way. delete(m.children, pid) case wpid == pid: - if notify { + if notify && !c.oneshot { m.notifyExit(pid, monExitCode(ws)) } delete(m.children, pid) diff --git a/ssh/tailssh/monitor_client.go b/ssh/tailssh/monitor_client.go index b26434e01..0bc14c372 100644 --- a/ssh/tailssh/monitor_client.go +++ b/ssh/tailssh/monitor_client.go @@ -7,6 +7,9 @@ // It spawns the per-connection monitor process, re-exec'ing the // current binary. It multiplexes request IDs, dispatches unsolicited // exit notifications, and serializes writes. +// +// Dial, Listen, and ListenAgent implement the conn's forwarding socket +// ops. package tailssh @@ -55,7 +58,7 @@ func spawnMonitor(exe string, uid, gid int, groups []int, localUser string) (*mo mc := &monitorClient{ c: parentConn, rpc: newRPCConn(parentConn), - pending: make(map[int64]chan *rpcFrame), + pending: make(map[int64]chan *rpcResult), cmd: cmd, done: make(chan struct{}), } @@ -63,6 +66,15 @@ func spawnMonitor(exe string, uid, gid int, groups []int, localUser string) (*mo return mc, nil } +// rpcResult is one inbound reply. It holds the frame and, for the +// socket ops, the descriptor the reply carried. Ownership of the +// descriptors passes to the waiting call. If there is none, they are +// closed. +type rpcResult struct { + f rpcFrame + fds []int +} + // monitorClient is tailscaled's connection to one ssh-monitor process. type monitorClient struct { c *net.UnixConn @@ -75,7 +87,7 @@ type monitorClient struct { mu sync.Mutex // guards the fields below nextID int64 - pending map[int64]chan *rpcFrame + pending map[int64]chan *rpcResult onExit func(pid, code int) closed bool @@ -92,18 +104,21 @@ func (mc *monitorClient) Pid() int { func (mc *monitorClient) readLoop() { defer close(mc.done) for { - f, _, err := mc.rpc.readFrame() + f, fds, err := mc.rpc.readFrame() if err != nil { return } - mc.dispatch(f) + mc.dispatch(f, fds) } } -// dispatch routes one inbound frame. Replies go to their waiting call, -// unsolicited exit notifications to the registered callback. -func (mc *monitorClient) dispatch(f rpcFrame) { +// dispatch routes one inbound frame. Replies, with any carried +// descriptors, go to their waiting call; a reply whose caller gave up +// has its descriptors closed here. Unsolicited exit notifications go +// to the registered callback. +func (mc *monitorClient) dispatch(f rpcFrame, fds []int) { if f.ID == 0 { + closeFds(fds) // notifications never carry descriptors if f.Op == opExit { mc.mu.Lock() h := mc.onExit @@ -118,25 +133,31 @@ func (mc *monitorClient) dispatch(f rpcFrame) { mc.mu.Lock() ch, ok := mc.pending[f.ID] delete(mc.pending, f.ID) - mc.mu.Unlock() if ok { - ch <- &f // buffered. The waiter is gone at worst briefly. + ch <- &rpcResult{f: f, fds: fds} + } else { + // The caller gave up, on ctx done or client death, and nobody + // will consume the descriptors. Close them here. + closeFds(fds) } + mc.mu.Unlock() } -// call sends one request and waits for its reply, the context, or the RPC -// channel's death. -func (mc *monitorClient) call(ctx context.Context, f rpcFrame, fds ...int) (rpcFrame, error) { +// call sends one request and waits for its reply (with any descriptors it +// carries), the context, or the RPC channel's death. On every path that +// does not hand the descriptors to the caller they are closed exactly +// once. See dispatch for the race-free handoff. +func (mc *monitorClient) call(ctx context.Context, f rpcFrame, fds ...int) (rpcFrame, []int, error) { var zero rpcFrame mc.mu.Lock() if mc.closed { mc.mu.Unlock() - return zero, errors.New("ssh-monitor: client closed") + return zero, nil, errors.New("ssh-monitor: client closed") } mc.nextID++ id := mc.nextID f.ID = id - ch := make(chan *rpcFrame, 1) + ch := make(chan *rpcResult, 1) mc.pending[id] = ch mc.mu.Unlock() @@ -147,25 +168,39 @@ func (mc *monitorClient) call(ctx context.Context, f rpcFrame, fds ...int) (rpcF mc.mu.Lock() delete(mc.pending, id) mc.mu.Unlock() - return zero, fmt.Errorf("ssh-monitor: write %q: %w", f.Op, err) + return zero, nil, fmt.Errorf("ssh-monitor: write %q: %w", f.Op, err) } select { case r := <-ch: - if !r.OK { - return *r, errors.New("ssh-monitor: " + r.Err) + if !r.f.OK { + closeFds(r.fds) // an error reply should carry none; be safe + return r.f, nil, errors.New("ssh-monitor: " + r.f.Err) } - return *r, nil + return r.f, r.fds, nil case <-ctx.Done(): + // Delete the entry under the lock: after this, dispatch finds no + // waiter and closes a late reply's descriptors itself. A reply + // already sitting in the channel is drained here and closed. mc.mu.Lock() delete(mc.pending, id) mc.mu.Unlock() - return zero, fmt.Errorf("ssh-monitor: op %q: %w", f.Op, ctx.Err()) + select { + case r := <-ch: + closeFds(r.fds) + default: + } + return zero, nil, fmt.Errorf("ssh-monitor: op %q: %w", f.Op, ctx.Err()) case <-mc.done: mc.mu.Lock() delete(mc.pending, id) mc.mu.Unlock() - return zero, errors.New("ssh-monitor: RPC channel closed") + select { + case r := <-ch: + closeFds(r.fds) + default: + } + return zero, nil, errors.New("ssh-monitor: RPC channel closed") } } @@ -180,7 +215,7 @@ func (mc *monitorClient) SetExitCallback(h func(pid, code int)) { // Ping round-trips a ping request. func (mc *monitorClient) Ping(ctx context.Context) error { - _, err := mc.call(ctx, rpcPingReq(0)) + _, _, err := mc.call(ctx, rpcPingReq(0)) return err } @@ -193,7 +228,7 @@ func (mc *monitorClient) Exec(ctx context.Context, argv, env []string, dir strin fds[i] = int(f.Fd()) } f := rpcExecReq(0, argv, env, dir, ctty, len(fds)) - r, err := mc.call(ctx, f, fds...) + r, _, err := mc.call(ctx, f, fds...) if err != nil { return 0, err } @@ -202,10 +237,79 @@ func (mc *monitorClient) Exec(ctx context.Context, argv, env []string, dir strin // Signal asks the monitor to signal a child process group. func (mc *monitorClient) Signal(ctx context.Context, pid int, sig string) error { - _, err := mc.call(ctx, rpcSignalReq(0, pid, sig)) + _, _, err := mc.call(ctx, rpcSignalReq(0, pid, sig)) return err } +// Dial dials a unix socket through a one-shot child running with the +// connection's bound identity and returns the connected socket. +func (mc *monitorClient) Dial(ctx context.Context, addr string) (*net.UnixConn, error) { + _, fds, err := mc.call(ctx, rpcDialReq(0, addr)) + if err != nil { + return nil, err + } + if len(fds) != 1 { + closeFds(fds) + return nil, fmt.Errorf("ssh-monitor: dial reply carried %d fds, want 1", len(fds)) + } + return wrapUnixConn(fds[0]) // consumes the descriptor +} + +// Listen asks the monitor for a listener bound with the connection's +// bound identity; see fwdchild.go for the umask-at-bind construction +// and the optional if-owned pre-unlink. Closing the returned listener +// does not unlink a unix socket file; a later bind with unlink-if-owned +// removes stale socket files. +func (mc *monitorClient) Listen(ctx context.Context, network, addr string, unlink bool) (net.Listener, error) { + r, fds, err := mc.call(ctx, rpcListenReq(0, network, addr, unlink, false)) + if err != nil { + return nil, err + } + if len(fds) != 1 { + closeFds(fds) + return nil, fmt.Errorf("ssh-monitor: listen reply carried %d fds, want 1", len(fds)) + } + return wrapListenerFD(fds[0], r.Path) +} + +// ListenAgent asks the monitor for the agent-forwarding socket: a +// user-owned 0700 temp dir and, inside it, a 0600 socket at +// listener.sock, created by the bound-identity child. It returns the +// listener and the socket path. Removing the containing directory is +// the caller's job. +func (mc *monitorClient) ListenAgent(ctx context.Context) (ln net.Listener, path string, err error) { + r, fds, err := mc.call(ctx, rpcListenReq(0, "", "", false, true)) + if err != nil { + return nil, "", err + } + if r.Path == "" { + closeFds(fds) + return nil, "", errors.New("ssh-monitor: agent listen reply carried no path") + } + if len(fds) != 1 { + closeFds(fds) + return nil, "", fmt.Errorf("ssh-monitor: agent listen reply carried %d fds, want 1", len(fds)) + } + ln, err = wrapListenerFD(fds[0], r.Path) + if err != nil { + return nil, "", err + } + return ln, r.Path, nil +} + +// wrapListenerFD wraps a raw listener descriptor in a net.Listener. fd is +// consumed. net.FileListener dups it into the returned listener with +// close-on-exec set, and the original is closed here exactly once. +func wrapListenerFD(fd int, name string) (net.Listener, error) { + f := os.NewFile(uintptr(fd), name) + ln, err := net.FileListener(f) + f.Close() // exactly-once close of the raw descriptor. The dup in ln is unaffected + if err != nil { + return nil, err + } + return ln, nil +} + // Close tears the connection down by closing the RPC channel, which // triggers the monitor's teardown. Close waits for the monitor to // exit. A wedged monitor is killed after 5 seconds. diff --git a/ssh/tailssh/monitor_linux.go b/ssh/tailssh/monitor_linux.go index c6e73000f..e3e6748a9 100644 --- a/ssh/tailssh/monitor_linux.go +++ b/ssh/tailssh/monitor_linux.go @@ -36,3 +36,20 @@ func scrubFds() error { } return scrubFdsByScan("/proc/self/fd") } + +// fwdChildAttr returns the SysProcAttr for a one-shot forwarding child +// whose identity must be applied, that is a root monitor's child. The +// Credential carries the connection's bound identity from ma. The child +// leads its own group so the op deadline and teardown can kill the +// whole group, and Pdeathsig is set as in monChildAttr. +func fwdChildAttr(ma monitorArgs) *syscall.SysProcAttr { + return &syscall.SysProcAttr{ + Credential: &syscall.Credential{ + Uid: uint32(ma.uid), + Gid: uint32(ma.gid), + Groups: fwdCredGroups(ma.gid, ma.gids), + }, + Setpgid: true, + Pdeathsig: unix.SIGHUP, + } +} diff --git a/ssh/tailssh/monitor_other.go b/ssh/tailssh/monitor_other.go index 5c72c75f7..db5327a68 100644 --- a/ssh/tailssh/monitor_other.go +++ b/ssh/tailssh/monitor_other.go @@ -28,3 +28,19 @@ func scrubFds() error { } return scrubFdsByScan("/proc/self/fd") } + +// fwdChildAttr returns the SysProcAttr for a one-shot forwarding child +// whose identity must be applied, that is a root monitor's child. The +// Credential carries the connection's bound identity from ma, and the +// child leads its own group. This variant has no Pdeathsig field; see +// monitor_linux.go. +func fwdChildAttr(ma monitorArgs) *syscall.SysProcAttr { + return &syscall.SysProcAttr{ + Credential: &syscall.Credential{ + Uid: uint32(ma.uid), + Gid: uint32(ma.gid), + Groups: fwdCredGroups(ma.gid, ma.gids), + }, + Setpgid: true, + } +} diff --git a/ssh/tailssh/monitor_test.go b/ssh/tailssh/monitor_test.go index 6e109d924..cb64ae2a7 100644 --- a/ssh/tailssh/monitor_test.go +++ b/ssh/tailssh/monitor_test.go @@ -372,31 +372,6 @@ func TestMonitorBadExec(t *testing.T) { } } -// TestMonitorDialListenNotImplemented pins the placeholder shape. The -// ops are parsed but answered with a descriptive not-implemented error -// until their children are wired up. -func TestMonitorDialListenNotImplemented(t *testing.T) { - mc := startTestMonitor(t, os.Getuid(), os.Getgid(), testGroups(t)) - ctx, cancel := testCtx() - defer cancel() - - for _, f := range []rpcFrame{ - rpcDialReq(0, "/tmp/some.sock"), - rpcListenReq(0, "unix", "/tmp/some.sock", false, false), - rpcListenReq(0, "tcp", "127.0.0.1:0", false, false), - rpcListenReq(0, "", "", false, true /* agent */), - } { - if _, err := mc.call(ctx, f); err == nil { - t.Fatalf("op %q: unexpectedly implemented", f.Op) - } else if !strings.Contains(err.Error(), "not implemented") { - t.Fatalf("op %q: error %q does not describe the placeholder", f.Op, err) - } - } - if err := mc.Ping(ctx); err != nil { - t.Fatalf("ping after unimplemented ops: %v", err) - } -} - // TestMonitorTeardown closes the client and asserts the teardown path. // The monitor exits promptly, through EOF, group SIGHUP, reaping, // SIGKILL of survivors, and exit 0. Its children die with it.