package provider import ( "encoding/json" "fmt" "os" "amadan.net/oficina/equipo/internal/runner/provider/gotoolchain" "golang.org/x/sys/unix" ) func (p Policy) GoToolchain() bool { for _, m := range p.Mounts { if gotoolchain.Target(m.Target) { return true } } return false } type goPin struct { file *os.File sum string } // Go trees belong to the account lease, which spans policy builds, preflight // and launch. Only the Go root is hashed. The module cache is trusted host // state, mounted read-only; updates by the runner user do not change its pin. func goTree(m Mount, b Binding) (fileDigest, error) { home, err := os.UserHomeDir() if err != nil || !m.ReadOnly || !gotoolchain.Target(m.Target) || !cleanPath(m.Source) || m.Source == "/tmp" || withinPath(home, m.Source) { return fileDigest{}, ErrPolicy } if b.provenance == nil { return fileDigest{}, ErrPolicy } l := b.provenance l.goMu.Lock() defer l.goMu.Unlock() if l.goClosed { return fileDigest{}, ErrPolicy } if pin, ok := l.goPins[m]; ok { return fileDigest{Mount: m, SHA256: pin.sum}, nil } f, err := openGoDirectory(m) if err != nil { return fileDigest{}, err } sum := "" if m.Target == "/opt/go" { sum, err = gotoolchain.Digest(fmt.Sprintf("/proc/self/fd/%d/.", f.Fd())) if err != nil { f.Close() return fileDigest{}, err } } if l.goPins == nil { l.goPins = map[Mount]goPin{} } l.goPins[m] = goPin{f, sum} return fileDigest{Mount: m, SHA256: sum}, nil } func openGoDirectory(m Mount) (*os.File, error) { f, err := pinDirectory(m.Source) if err != nil { return nil, err } var st unix.Stat_t if unix.Fstat(int(f.Fd()), &st) != nil || (m.Target == "/opt/go-mod" && st.Uid != uint32(os.Getuid())) { f.Close() return nil, ErrPolicy } return f, nil } // OpenGoTree returns a duplicate of the validated launch descriptor. The // caller owns the duplicate; the account lease closes the original. func (p Policy) OpenGoTree(b Binding, mount Mount) (*os.File, error) { var m effectiveManifest if digest(p.Manifest) != p.ManifestHash || json.Unmarshal(p.Manifest, &m) != nil { return nil, ErrPolicy } if p.IsCheck() { return openGoDirectory(mount) } l := b.provenance if p.IsPi() && p.goLease != nil { l = &p.goLease.lease } if l == nil { return nil, ErrPolicy } l.goMu.Lock() defer l.goMu.Unlock() pin, ok := l.goPins[mount] if !ok || l.goClosed { return nil, ErrPolicy } found := false for _, f := range m.Files { if f.Mount == mount && f.SHA256 == pin.sum { found = true } } if !found { return nil, ErrPolicy } fd, err := unix.FcntlInt(pin.file.Fd(), unix.F_DUPFD_CLOEXEC, 0) if err != nil { return nil, err } return os.NewFile(uintptr(fd), mount.Source), nil }