diff --git a/ssh/tailssh/process_unix.go b/ssh/tailssh/process_unix.go index 1a8243a54..8789d4544 100644 --- a/ssh/tailssh/process_unix.go +++ b/ssh/tailssh/process_unix.go @@ -132,15 +132,24 @@ func (ss *sshSession) handleSSHAgentForwarding(s gliderssh.Session, lu *userMeta } socket := ln.Addr().String() dir := filepath.Dir(socket) - // Make sure the socket is accessible only by the user. + // Make sure the socket is accessible only by the user, and hand the + // socket and its containing directory to the user (like sshd's + // agent forwarding): the dir stays 0700 (as MkdirTemp created it) + // until the socket is in place and owned by the user, then the dir + // is chowned to the user too so the session can reach its socket + // without it being group/world-accessible. if err := os.Chmod(socket, 0600); err != nil { return err } if err := os.Chown(socket, int(uid), int(gid)); err != nil { return err } - // Make sure the dir is also accessible. - if err := os.Chmod(dir, 0755); err != nil { + if err := os.Chown(dir, int(uid), int(gid)); err != nil { + return err + } + // Defensively restore the 0700 mode in case the umask or a future + // change loosened it. + if err := os.Chmod(dir, 0700); err != nil { return err } @@ -148,3 +157,19 @@ func (ss *sshSession) handleSSHAgentForwarding(s gliderssh.Session, lu *userMeta ss.agentListener = ln return nil } + +// closeAgentForwarding closes the agent forwarding listener and removes the +// temporary directory that holds its socket, so neither outlives the +// session. +func (ss *sshSession) closeAgentForwarding() { + ln := ss.agentListener + if ln == nil { + return + } + ss.agentListener = nil + dir := filepath.Dir(ln.Addr().String()) + ln.Close() + if err := os.RemoveAll(dir); err != nil { + ss.logf("ssh: removing agent forwarding dir: %v", err) + } +} diff --git a/ssh/tailssh/process_unix_test.go b/ssh/tailssh/process_unix_test.go new file mode 100644 index 000000000..050d316ed --- /dev/null +++ b/ssh/tailssh/process_unix_test.go @@ -0,0 +1,144 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build (linux && !android) || (darwin && !ios) || freebsd || openbsd + +package tailssh + +import ( + "context" + "errors" + "fmt" + "net" + "os" + "os/user" + "path/filepath" + "sync" + "syscall" + "testing" + + gliderssh "github.com/tailscale/gliderssh" + gossh "golang.org/x/crypto/ssh" + "tailscale.com/tailcfg" + "tailscale.com/types/logger" +) + +// agentFwdConn is a gossh.Conn stub: ForwardAgentConnections only type +// asserts the context value to gossh.Conn, and OpenChannel is only reached +// if a connection arrives on the forwarded socket, which these tests never +// trigger. +type agentFwdConn struct{ gossh.Conn } + +func (agentFwdConn) OpenChannel(string, []byte) (gossh.Channel, <-chan *gossh.Request, error) { + return nil, nil, errors.New("no channel") +} + +// agentFwdContext implements gliderssh.Context: Value makes AgentRequested +// report true and ContextKeyConn return a usable conn stub; everything else +// is inert. +type agentFwdContext struct { + context.Context + *sync.Mutex +} + +func (agentFwdContext) Value(key any) any { + if key == gliderssh.ContextKeyConn { + return agentFwdConn{} + } + return true +} +func (agentFwdContext) User() string { return "alice" } +func (agentFwdContext) SessionID() string { return "test" } +func (agentFwdContext) ClientVersion() string { return "2.0" } +func (agentFwdContext) ServerVersion() string { return "2.0" } +func (agentFwdContext) RemoteAddr() net.Addr { return nil } +func (agentFwdContext) LocalAddr() net.Addr { return nil } +func (agentFwdContext) Permissions() *gliderssh.Permissions { return nil } +func (agentFwdContext) SetValue(key, value any) {} + +// agentFwdSession is a minimal gliderssh.Session for handleSSHAgentForwarding. +type agentFwdSession struct { + gliderssh.Session + ctx gliderssh.Context +} + +func (s agentFwdSession) Context() gliderssh.Context { return s.ctx } + +func newAgentTestSession() (*sshSession, *agentFwdSession) { + c := &conn{ + srv: &server{logf: logger.Discard}, + finalAction: &tailcfg.SSHAction{Accept: true, AllowAgentForwarding: true}, + } + ctx := agentFwdContext{Context: context.Background(), Mutex: &sync.Mutex{}} + ss := &sshSession{ + conn: c, + logf: logger.Discard, + Session: agentFwdSession{ctx: ctx}, // minimal Session; only Context() is used + } + return ss, &agentFwdSession{ctx: ctx} +} + +// TestAgentForwardDirCleanup verifies that the agent forwarding socket's +// temp directory is removed when the session ends. +func TestAgentForwardDirCleanup(t *testing.T) { + ln, err := gliderssh.NewAgentListener() + if err != nil { + t.Fatal(err) + } + ss, _ := newAgentTestSession() + ss.agentListener = ln + + dir := filepath.Dir(ln.Addr().String()) + if _, err := os.Stat(dir); err != nil { + t.Fatalf("stat agent dir: %v", err) + } + ss.closeAgentForwarding() + if _, err := os.Stat(dir); !os.IsNotExist(err) { + t.Fatalf("agent dir %q still exists after closeAgentForwarding (err=%v)", dir, err) + } + // Closing again must be a no-op (run's defer plus any other cleanup). + ss.closeAgentForwarding() +} + +// TestHandleSSHAgentForwardingDirHygiene verifies the socket and its +// containing directory are accessible only by the local user (0600 socket, +// 0700 user-owned directory), not group/world-accessible root-owned state. +func TestHandleSSHAgentForwardingDirHygiene(t *testing.T) { + if os.Geteuid() != 0 { + t.Skipf("skipping when not running as root: os.Chown is required") + } + + const uid, gid = 1234, 1235 + ss, s := newAgentTestSession() + lu := &userMeta{User: user.User{Username: "alice", Uid: fmt.Sprint(uid), Gid: fmt.Sprint(gid)}} + + if err := ss.handleSSHAgentForwarding(s, lu); err != nil { + t.Fatalf("handleSSHAgentForwarding: %v", err) + } + t.Cleanup(ss.closeAgentForwarding) + + socketPath := ss.agentListener.Addr().String() + dir := filepath.Dir(socketPath) + + si, err := os.Stat(socketPath) + if err != nil { + t.Fatalf("stat socket: %v", err) + } + if got := si.Mode().Perm(); got != 0600 { + t.Errorf("socket mode = %o; want 0600", got) + } + if int(si.Sys().(*syscall.Stat_t).Uid) != uid { + t.Errorf("socket uid = %d; want %d", si.Sys().(*syscall.Stat_t).Uid, uid) + } + + di, err := os.Stat(dir) + if err != nil { + t.Fatalf("stat dir: %v", err) + } + if got := di.Mode().Perm(); got != 0700 { + t.Errorf("dir mode = %o; want 0700", got) + } + if int(di.Sys().(*syscall.Stat_t).Uid) != uid { + t.Errorf("dir uid = %d; want %d (must be owned by the session user, not root)", di.Sys().(*syscall.Stat_t).Uid, uid) + } +} diff --git a/ssh/tailssh/tailssh.go b/ssh/tailssh/tailssh.go index 40c2cbd62..cbd2dc97e 100644 --- a/ssh/tailssh/tailssh.go +++ b/ssh/tailssh/tailssh.go @@ -982,7 +982,7 @@ func (ss *sshSession) run() { ss.logf("agent forwarding failed: %v", err) } else if ss.agentListener != nil { // TODO(maisem/bradfitz): add a way to close all session resources - defer ss.agentListener.Close() + defer ss.closeAgentForwarding() } if ss.shouldRecord() {