| 1 | //go:build darwin || testbuild |
| 2 | |
| 3 | package runner |
| 4 | |
| 5 | import ( |
| 6 | "bufio" |
| 7 | "bytes" |
| 8 | "context" |
| 9 | "encoding/json" |
| 10 | "io" |
| 11 | "net" |
| 12 | "path/filepath" |
| 13 | "strings" |
| 14 | |
| 15 | "golang.org/x/sys/unix" |
| 16 | |
| 17 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 18 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 19 | ) |
| 20 | |
| 21 | // A Mac runner's MCP tool endpoint, equipo-runner tool-proxy --socket |
| 22 | // <path> (Mac runners spec §8.1, §8.2). Linux keeps its certified tool |
| 23 | // proxy (internal/toolproxy, byte for byte); a Mac release has none, so |
| 24 | // the runner binary relays the CLI's stdio MCP to the step's tool socket |
| 25 | // itself, with the same bounds and the same framing. It is built for |
| 26 | // darwin, and for the test build so Linux CI runs it; a Linux release |
| 27 | // does not contain it. |
| 28 | |
| 29 | // macToolFrame bounds a request from the CLI and macReplyFrame a reply |
| 30 | // from the runner, as internal/toolproxy's maxToolFrame and |
| 31 | // maxReplyFrame do. |
| 32 | const ( |
| 33 | macToolFrame = 64 << 10 |
| 34 | macReplyFrame = 4 << 20 |
| 35 | ) |
| 36 | |
| 37 | // macDial dials the step socket once every check has passed. Tests |
| 38 | // stand in to see that a refusal never reaches it. |
| 39 | var macDial = (&net.Dialer{}).DialContext |
| 40 | |
| 41 | // macListenerUID is the user that holds the step socket's other end |
| 42 | // (LOCAL_PEERCRED on a Mac). Tests stand in. |
| 43 | var macListenerUID = func(c *net.UnixConn) (uint32, error) { |
| 44 | raw, err := c.SyscallConn() |
| 45 | if err != nil { |
| 46 | return 0, err |
| 47 | } |
| 48 | var uid uint32 |
| 49 | var perr error |
| 50 | if err := raw.Control(func(fd uintptr) { uid, perr = peerUID(int(fd)) }); err != nil { |
| 51 | return 0, err |
| 52 | } |
| 53 | return uid, perr |
| 54 | } |
| 55 | |
| 56 | // ServeDarwinToolProxy is the Mac endpoint's stdio mode. path is the |
| 57 | // step's tool socket: a clean absolute path that fits a Mac's sun_path, |
| 58 | // reached without a symbolic link, in a 0700 directory of this user's, |
| 59 | // a 0600 socket of this user's, held open by this user. Anything else is |
| 60 | // refused before a byte is sent or written. |
| 61 | func ServeDarwinToolProxy(ctx context.Context, input io.Reader, output io.Writer, path string) error { |
| 62 | if !filepath.IsAbs(path) || filepath.Clean(path) != path || len(path) > provider.MaxDarwinSocketPath || strings.ContainsRune(path, 0) { |
| 63 | return runnerproto.ErrInvalid |
| 64 | } |
| 65 | parent, err := openPrivateDir(filepath.Dir(path)) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | var st unix.Stat_t |
| 70 | err = unix.Fstatat(int(parent.Fd()), filepath.Base(path), &st, unix.AT_SYMLINK_NOFOLLOW) |
| 71 | parent.Close() |
| 72 | if err != nil || st.Mode&unix.S_IFMT != unix.S_IFSOCK || st.Mode&0777 != 0600 || !toolOwner(st.Uid) { |
| 73 | return runnerproto.ErrInvalid |
| 74 | } |
| 75 | // No /proc/self/fd here: the path is dialled as checked. Only this |
| 76 | // user can change what lies under a directory it alone may enter, |
| 77 | // and the listener's credential is checked before anything is sent. |
| 78 | conn, err := macDial(ctx, "unix", path) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | defer conn.Close() |
| 83 | unixConn, ok := conn.(*net.UnixConn) |
| 84 | if !ok { |
| 85 | return runnerproto.ErrInvalid |
| 86 | } |
| 87 | if uid, err := macListenerUID(unixConn); err != nil || !toolOwner(uid) { |
| 88 | return runnerproto.ErrInvalid |
| 89 | } |
| 90 | stop := context.AfterFunc(ctx, func() { conn.Close() }) |
| 91 | defer stop() |
| 92 | return macRelay(input, output, conn) |
| 93 | } |
| 94 | |
| 95 | // macMCPRequest is the JSON-RPC envelope the endpoint checks, as |
| 96 | // internal/toolproxy's mcpRequest. |
| 97 | type macMCPRequest struct { |
| 98 | JSONRPC string `json:"jsonrpc"` |
| 99 | ID json.RawMessage `json:"id,omitempty"` |
| 100 | Method string `json:"method"` |
| 101 | Params json.RawMessage `json:"params,omitempty"` |
| 102 | } |
| 103 | |
| 104 | // macRelay carries the CLI's JSON-RPC lines to the runner's socket and |
| 105 | // each reply back, one request at a time, as internal/toolproxy's relay. |
| 106 | func macRelay(input io.Reader, output io.Writer, conn io.ReadWriter) error { |
| 107 | reader := bufio.NewReaderSize(input, macToolFrame+1) |
| 108 | replies := bufio.NewReaderSize(conn, macReplyFrame+1) |
| 109 | for { |
| 110 | if _, err := reader.Peek(1); err == io.EOF { |
| 111 | return nil |
| 112 | } else if err != nil { |
| 113 | return err |
| 114 | } |
| 115 | raw, err := macFrame(reader, macToolFrame) |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | var q macMCPRequest |
| 120 | if macDecode(raw, &q) != nil || q.JSONRPC != "2.0" { |
| 121 | return runnerproto.ErrInvalid |
| 122 | } |
| 123 | if _, err := conn.Write(append(raw, '\n')); err != nil { |
| 124 | return err |
| 125 | } |
| 126 | if len(q.ID) == 0 { |
| 127 | continue |
| 128 | } |
| 129 | reply, err := macFrame(replies, macReplyFrame) |
| 130 | if err != nil { |
| 131 | return err |
| 132 | } |
| 133 | if _, err := output.Write(append(reply, '\n')); err != nil { |
| 134 | return err |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | func macFrame(r *bufio.Reader, limit int) ([]byte, error) { |
| 140 | raw, err := r.ReadSlice('\n') |
| 141 | if err != nil || len(raw) > limit { |
| 142 | return nil, runnerproto.ErrInvalid |
| 143 | } |
| 144 | return bytes.TrimSuffix(raw, []byte{'\n'}), nil |
| 145 | } |
| 146 | |
| 147 | func macDecode(raw []byte, v any) error { |
| 148 | d := json.NewDecoder(bytes.NewReader(raw)) |
| 149 | d.DisallowUnknownFields() |
| 150 | if err := d.Decode(v); err != nil { |
| 151 | return err |
| 152 | } |
| 153 | var extra any |
| 154 | if d.Decode(&extra) != io.EOF { |
| 155 | return runnerproto.ErrInvalid |
| 156 | } |
| 157 | return nil |
| 158 | } |
| 159 | |