//go:build darwin || testbuild package runner import ( "bufio" "bytes" "context" "encoding/json" "io" "net" "path/filepath" "strings" "golang.org/x/sys/unix" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runnerproto" ) // A Mac runner's MCP tool endpoint, equipo-runner tool-proxy --socket // (Mac runners spec §8.1, §8.2). Linux keeps its certified tool // proxy (internal/toolproxy, byte for byte); a Mac release has none, so // the runner binary relays the CLI's stdio MCP to the step's tool socket // itself, with the same bounds and the same framing. It is built for // darwin, and for the test build so Linux CI runs it; a Linux release // does not contain it. // macToolFrame bounds a request from the CLI and macReplyFrame a reply // from the runner, as internal/toolproxy's maxToolFrame and // maxReplyFrame do. const ( macToolFrame = 64 << 10 macReplyFrame = 4 << 20 ) // macDial dials the step socket once every check has passed. Tests // stand in to see that a refusal never reaches it. var macDial = (&net.Dialer{}).DialContext // macListenerUID is the user that holds the step socket's other end // (LOCAL_PEERCRED on a Mac). Tests stand in. var macListenerUID = func(c *net.UnixConn) (uint32, error) { raw, err := c.SyscallConn() if err != nil { return 0, err } var uid uint32 var perr error if err := raw.Control(func(fd uintptr) { uid, perr = peerUID(int(fd)) }); err != nil { return 0, err } return uid, perr } // ServeDarwinToolProxy is the Mac endpoint's stdio mode. path is the // step's tool socket: a clean absolute path that fits a Mac's sun_path, // reached without a symbolic link, in a 0700 directory of this user's, // a 0600 socket of this user's, held open by this user. Anything else is // refused before a byte is sent or written. func ServeDarwinToolProxy(ctx context.Context, input io.Reader, output io.Writer, path string) error { if !filepath.IsAbs(path) || filepath.Clean(path) != path || len(path) > provider.MaxDarwinSocketPath || strings.ContainsRune(path, 0) { return runnerproto.ErrInvalid } parent, err := openPrivateDir(filepath.Dir(path)) if err != nil { return err } var st unix.Stat_t err = unix.Fstatat(int(parent.Fd()), filepath.Base(path), &st, unix.AT_SYMLINK_NOFOLLOW) parent.Close() if err != nil || st.Mode&unix.S_IFMT != unix.S_IFSOCK || st.Mode&0777 != 0600 || !toolOwner(st.Uid) { return runnerproto.ErrInvalid } // No /proc/self/fd here: the path is dialled as checked. Only this // user can change what lies under a directory it alone may enter, // and the listener's credential is checked before anything is sent. conn, err := macDial(ctx, "unix", path) if err != nil { return err } defer conn.Close() unixConn, ok := conn.(*net.UnixConn) if !ok { return runnerproto.ErrInvalid } if uid, err := macListenerUID(unixConn); err != nil || !toolOwner(uid) { return runnerproto.ErrInvalid } stop := context.AfterFunc(ctx, func() { conn.Close() }) defer stop() return macRelay(input, output, conn) } // macMCPRequest is the JSON-RPC envelope the endpoint checks, as // internal/toolproxy's mcpRequest. type macMCPRequest struct { JSONRPC string `json:"jsonrpc"` ID json.RawMessage `json:"id,omitempty"` Method string `json:"method"` Params json.RawMessage `json:"params,omitempty"` } // macRelay carries the CLI's JSON-RPC lines to the runner's socket and // each reply back, one request at a time, as internal/toolproxy's relay. func macRelay(input io.Reader, output io.Writer, conn io.ReadWriter) error { reader := bufio.NewReaderSize(input, macToolFrame+1) replies := bufio.NewReaderSize(conn, macReplyFrame+1) for { if _, err := reader.Peek(1); err == io.EOF { return nil } else if err != nil { return err } raw, err := macFrame(reader, macToolFrame) if err != nil { return err } var q macMCPRequest if macDecode(raw, &q) != nil || q.JSONRPC != "2.0" { return runnerproto.ErrInvalid } if _, err := conn.Write(append(raw, '\n')); err != nil { return err } if len(q.ID) == 0 { continue } reply, err := macFrame(replies, macReplyFrame) if err != nil { return err } if _, err := output.Write(append(reply, '\n')); err != nil { return err } } } func macFrame(r *bufio.Reader, limit int) ([]byte, error) { raw, err := r.ReadSlice('\n') if err != nil || len(raw) > limit { return nil, runnerproto.ErrInvalid } return bytes.TrimSuffix(raw, []byte{'\n'}), nil } func macDecode(raw []byte, v any) error { d := json.NewDecoder(bytes.NewReader(raw)) d.DisallowUnknownFields() if err := d.Decode(v); err != nil { return err } var extra any if d.Decode(&extra) != io.EOF { return runnerproto.ErrInvalid } return nil }