| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/runner/provider/gotoolchain" |
| 9 | "golang.org/x/sys/unix" |
| 10 | ) |
| 11 | |
| 12 | func (p Policy) GoToolchain() bool { |
| 13 | for _, m := range p.Mounts { |
| 14 | if gotoolchain.Target(m.Target) { |
| 15 | return true |
| 16 | } |
| 17 | } |
| 18 | return false |
| 19 | } |
| 20 | |
| 21 | type goPin struct { |
| 22 | file *os.File |
| 23 | sum string |
| 24 | } |
| 25 | |
| 26 | // Go trees belong to the account lease, which spans policy builds, preflight |
| 27 | // and launch. Only the Go root is hashed. The module cache is trusted host |
| 28 | // state, mounted read-only; updates by the runner user do not change its pin. |
| 29 | func goTree(m Mount, b Binding) (fileDigest, error) { |
| 30 | home, err := os.UserHomeDir() |
| 31 | if err != nil || !m.ReadOnly || !gotoolchain.Target(m.Target) || !cleanPath(m.Source) || m.Source == "/tmp" || withinPath(home, m.Source) { |
| 32 | return fileDigest{}, ErrPolicy |
| 33 | } |
| 34 | if b.provenance == nil { |
| 35 | return fileDigest{}, ErrPolicy |
| 36 | } |
| 37 | l := b.provenance |
| 38 | l.goMu.Lock() |
| 39 | defer l.goMu.Unlock() |
| 40 | if l.goClosed { |
| 41 | return fileDigest{}, ErrPolicy |
| 42 | } |
| 43 | if pin, ok := l.goPins[m]; ok { |
| 44 | return fileDigest{Mount: m, SHA256: pin.sum}, nil |
| 45 | } |
| 46 | f, err := openGoDirectory(m) |
| 47 | if err != nil { |
| 48 | return fileDigest{}, err |
| 49 | } |
| 50 | sum := "" |
| 51 | if m.Target == "/opt/go" { |
| 52 | sum, err = gotoolchain.Digest(fmt.Sprintf("/proc/self/fd/%d/.", f.Fd())) |
| 53 | if err != nil { |
| 54 | f.Close() |
| 55 | return fileDigest{}, err |
| 56 | } |
| 57 | } |
| 58 | if l.goPins == nil { |
| 59 | l.goPins = map[Mount]goPin{} |
| 60 | } |
| 61 | l.goPins[m] = goPin{f, sum} |
| 62 | return fileDigest{Mount: m, SHA256: sum}, nil |
| 63 | } |
| 64 | |
| 65 | func openGoDirectory(m Mount) (*os.File, error) { |
| 66 | f, err := pinDirectory(m.Source) |
| 67 | if err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | var st unix.Stat_t |
| 71 | if unix.Fstat(int(f.Fd()), &st) != nil || (m.Target == "/opt/go-mod" && st.Uid != uint32(os.Getuid())) { |
| 72 | f.Close() |
| 73 | return nil, ErrPolicy |
| 74 | } |
| 75 | return f, nil |
| 76 | } |
| 77 | |
| 78 | // OpenGoTree returns a duplicate of the validated launch descriptor. The |
| 79 | // caller owns the duplicate; the account lease closes the original. |
| 80 | func (p Policy) OpenGoTree(b Binding, mount Mount) (*os.File, error) { |
| 81 | var m effectiveManifest |
| 82 | if digest(p.Manifest) != p.ManifestHash || json.Unmarshal(p.Manifest, &m) != nil { |
| 83 | return nil, ErrPolicy |
| 84 | } |
| 85 | if p.IsCheck() { |
| 86 | return openGoDirectory(mount) |
| 87 | } |
| 88 | l := b.provenance |
| 89 | if p.IsPi() && p.goLease != nil { |
| 90 | l = &p.goLease.lease |
| 91 | } |
| 92 | if l == nil { |
| 93 | return nil, ErrPolicy |
| 94 | } |
| 95 | l.goMu.Lock() |
| 96 | defer l.goMu.Unlock() |
| 97 | pin, ok := l.goPins[mount] |
| 98 | if !ok || l.goClosed { |
| 99 | return nil, ErrPolicy |
| 100 | } |
| 101 | found := false |
| 102 | for _, f := range m.Files { |
| 103 | if f.Mount == mount && f.SHA256 == pin.sum { |
| 104 | found = true |
| 105 | } |
| 106 | } |
| 107 | if !found { |
| 108 | return nil, ErrPolicy |
| 109 | } |
| 110 | fd, err := unix.FcntlInt(pin.file.Fd(), unix.F_DUPFD_CLOEXEC, 0) |
| 111 | if err != nil { |
| 112 | return nil, err |
| 113 | } |
| 114 | return os.NewFile(uintptr(fd), mount.Source), nil |
| 115 | } |
| 116 | |