| 1 | //go:build linux |
| 2 | |
| 3 | package release |
| 4 | |
| 5 | import ( |
| 6 | "crypto/sha256" |
| 7 | "encoding/hex" |
| 8 | "errors" |
| 9 | "io" |
| 10 | "os" |
| 11 | "slices" |
| 12 | |
| 13 | "golang.org/x/sys/unix" |
| 14 | |
| 15 | "amadan.net/oficina/equipo/internal/wire" |
| 16 | ) |
| 17 | |
| 18 | // verifyRendererTree is VerifyRenderer's check of the disk, the tree to |
| 19 | // be owned by uid or root. Every entry is reached from the tree's own |
| 20 | // directory descriptor by one name at a time (openat, fstatat and |
| 21 | // readlinkat with no-follow), so no link on disk is ever followed, and |
| 22 | // the checks on a directory or file are made on the descriptor that is |
| 23 | // then read. |
| 24 | func verifyRendererTree(dir, pin string, uid uint32) error { |
| 25 | root, err := unix.Open(dir, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) |
| 26 | if errors.Is(err, unix.ENOENT) { |
| 27 | return &RendererError{Health: wire.ToolMissing, Reason: dir + " does not exist"} |
| 28 | } |
| 29 | if err != nil { |
| 30 | return rendererMismatch("", "%s is not a directory this runner can read: %v", dir, err) |
| 31 | } |
| 32 | defer unix.Close(root) |
| 33 | if _, err := rendererSafe(root, "", unix.S_IFDIR, uid); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | manifest, err := readRendererManifest(root, uid) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | if sum := sha256.Sum256(manifest); hex.EncodeToString(sum[:]) != pin { |
| 41 | return rendererMismatch("MANIFEST", "its SHA-256 is %x, the pin is %s", sum, pin) |
| 42 | } |
| 43 | entries, err := parseRendererManifest(manifest) |
| 44 | if err != nil { |
| 45 | return rendererMismatch("MANIFEST", "%v", err) |
| 46 | } |
| 47 | w := rendererWalk{entries: entries, seen: map[string]bool{}, uid: uid} |
| 48 | if err := w.dir(root, ""); err != nil { |
| 49 | return err |
| 50 | } |
| 51 | var missing []string |
| 52 | for path := range entries { |
| 53 | if !w.seen[path] { |
| 54 | missing = append(missing, path) |
| 55 | } |
| 56 | } |
| 57 | if len(missing) > 0 { |
| 58 | slices.Sort(missing) |
| 59 | return rendererMismatch(missing[0], "listed in MANIFEST, not in the tree (%d missing)", len(missing)) |
| 60 | } |
| 61 | return nil |
| 62 | } |
| 63 | |
| 64 | // readRendererManifest reads the tree's MANIFEST: a regular file, not a |
| 65 | // link, safe as any file of the tree is, and no larger than the build |
| 66 | // could have written. Like every entry, it is not opened unless fstatat |
| 67 | // finds a regular file there (opening a device can do things). |
| 68 | func readRendererManifest(root int, uid uint32) ([]byte, error) { |
| 69 | var st unix.Stat_t |
| 70 | if err := unix.Fstatat(root, "MANIFEST", &st, unix.AT_SYMLINK_NOFOLLOW); err != nil { |
| 71 | return nil, rendererMismatch("MANIFEST", "cannot be checked: %v", err) |
| 72 | } |
| 73 | if st.Mode&unix.S_IFMT != unix.S_IFREG { |
| 74 | return nil, rendererMismatch("MANIFEST", "is a %s; the list must be a file", rendererKind(st.Mode)) |
| 75 | } |
| 76 | fd, err := unix.Openat(root, "MANIFEST", unix.O_RDONLY|unix.O_NOFOLLOW|unix.O_NONBLOCK|unix.O_NOCTTY|unix.O_CLOEXEC, 0) |
| 77 | if err != nil { |
| 78 | return nil, rendererMismatch("MANIFEST", "cannot be opened as a file: %v", err) |
| 79 | } |
| 80 | f := os.NewFile(uintptr(fd), "MANIFEST") |
| 81 | defer f.Close() |
| 82 | if _, err := rendererSafe(fd, "MANIFEST", unix.S_IFREG, uid); err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | b, err := io.ReadAll(io.LimitReader(f, rendererManifestMax+1)) |
| 86 | if err != nil { |
| 87 | return nil, rendererMismatch("MANIFEST", "cannot be read: %v", err) |
| 88 | } |
| 89 | if len(b) > rendererManifestMax { |
| 90 | return nil, rendererMismatch("MANIFEST", "larger than %d bytes", rendererManifestMax) |
| 91 | } |
| 92 | return b, nil |
| 93 | } |
| 94 | |
| 95 | // rendererSafe checks what fd is: of kind (S_IFDIR or S_IFREG), owned by |
| 96 | // uid or root, nothing group or world writable, and, for a file, not |
| 97 | // linked from anywhere else (one name only). It returns fd's stat. |
| 98 | func rendererSafe(fd int, path string, kind uint32, uid uint32) (unix.Stat_t, error) { |
| 99 | var st unix.Stat_t |
| 100 | if err := unix.Fstat(fd, &st); err != nil { |
| 101 | return st, rendererMismatch(path, "cannot be checked: %v", err) |
| 102 | } |
| 103 | if st.Mode&unix.S_IFMT != kind { |
| 104 | return st, rendererMismatch(path, "is a %s, not a %s", rendererKind(st.Mode), rendererKind(kind)) |
| 105 | } |
| 106 | if st.Uid != uid && st.Uid != 0 { |
| 107 | return st, rendererMismatch(path, "is owned by user %d, not this runner's %d or root", st.Uid, uid) |
| 108 | } |
| 109 | if st.Mode&0o022 != 0 { |
| 110 | return st, rendererMismatch(path, "is group or world writable (%04o)", st.Mode&0o7777) |
| 111 | } |
| 112 | if kind == unix.S_IFREG && st.Nlink != 1 { |
| 113 | return st, rendererMismatch(path, "has %d names, not one", st.Nlink) |
| 114 | } |
| 115 | return st, nil |
| 116 | } |
| 117 | |
| 118 | func rendererKind(mode uint32) string { |
| 119 | switch mode & unix.S_IFMT { |
| 120 | case unix.S_IFDIR: |
| 121 | return "dir" |
| 122 | case unix.S_IFREG: |
| 123 | return "file" |
| 124 | case unix.S_IFLNK: |
| 125 | return "link" |
| 126 | } |
| 127 | return "special file" |
| 128 | } |
| 129 | |
| 130 | type rendererWalk struct { |
| 131 | entries map[string]rendererEntry |
| 132 | seen map[string]bool |
| 133 | uid uint32 |
| 134 | } |
| 135 | |
| 136 | // dir checks the entries of the open directory fd, the tree's path rel |
| 137 | // ("" for its root), and the directories under it. |
| 138 | func (w *rendererWalk) dir(fd int, rel string) error { |
| 139 | names, err := w.names(fd, rel) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | for _, name := range names { |
| 144 | path := name |
| 145 | if rel != "" { |
| 146 | path = rel + "/" + name |
| 147 | } |
| 148 | e := w.entries[path] |
| 149 | var st unix.Stat_t |
| 150 | if err := unix.Fstatat(fd, name, &st, unix.AT_SYMLINK_NOFOLLOW); err != nil { |
| 151 | return rendererMismatch(path, "cannot be checked: %v", err) |
| 152 | } |
| 153 | if got := rendererKind(st.Mode); got != e.kind { |
| 154 | return rendererMismatch(path, "is a %s, MANIFEST lists a %s", got, e.kind) |
| 155 | } |
| 156 | w.seen[path] = true |
| 157 | switch e.kind { |
| 158 | case "dir": |
| 159 | sub, err := unix.Openat(fd, name, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) |
| 160 | if err != nil { |
| 161 | return rendererMismatch(path, "cannot be opened as a directory: %v", err) |
| 162 | } |
| 163 | _, err = rendererSafe(sub, path, unix.S_IFDIR, w.uid) |
| 164 | if err == nil { |
| 165 | err = w.dir(sub, path) |
| 166 | } |
| 167 | unix.Close(sub) |
| 168 | if err != nil { |
| 169 | return err |
| 170 | } |
| 171 | case "file": |
| 172 | if err := w.file(fd, name, path, e); err != nil { |
| 173 | return err |
| 174 | } |
| 175 | case "link": |
| 176 | buf := make([]byte, unix.PathMax) |
| 177 | n, err := unix.Readlinkat(fd, name, buf) |
| 178 | if err != nil || n >= len(buf) { |
| 179 | return rendererMismatch(path, "cannot be read as a link: %v", err) |
| 180 | } |
| 181 | if got := string(buf[:n]); got != e.target { |
| 182 | return rendererMismatch(path, "points at %q, MANIFEST lists %q", got, e.target) |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | return nil |
| 187 | } |
| 188 | |
| 189 | // names lists the directory fd, sorted, refusing at once any name |
| 190 | // MANIFEST does not list (MANIFEST itself, at the root, is skipped: it |
| 191 | // was checked first), so an unlisted directory is never entered and a |
| 192 | // huge one is never held in memory. |
| 193 | func (w *rendererWalk) names(fd int, rel string) ([]string, error) { |
| 194 | buf := make([]byte, 32<<10) |
| 195 | var names []string |
| 196 | for { |
| 197 | n, err := unix.Getdents(fd, buf) |
| 198 | if errors.Is(err, unix.EINTR) { |
| 199 | continue |
| 200 | } |
| 201 | if err != nil { |
| 202 | return nil, rendererMismatch(rel, "cannot be listed: %v", err) |
| 203 | } |
| 204 | if n <= 0 { |
| 205 | break |
| 206 | } |
| 207 | var batch []string |
| 208 | _, _, batch = unix.ParseDirent(buf[:n], -1, nil) |
| 209 | for _, name := range batch { |
| 210 | path := name |
| 211 | if rel != "" { |
| 212 | path = rel + "/" + name |
| 213 | } |
| 214 | if rel == "" && name == "MANIFEST" { |
| 215 | continue |
| 216 | } |
| 217 | if _, ok := w.entries[path]; !ok { |
| 218 | return nil, rendererMismatch(path, "is in the tree, not in MANIFEST") |
| 219 | } |
| 220 | names = append(names, name) |
| 221 | } |
| 222 | } |
| 223 | slices.Sort(names) |
| 224 | return names, nil |
| 225 | } |
| 226 | |
| 227 | // file checks a file entry: opened without following a link and without |
| 228 | // blocking (a fifo swapped in after fstatat cannot hang the check), a |
| 229 | // regular file with exactly the listed mode, safe, and the listed hash. |
| 230 | func (w *rendererWalk) file(dirfd int, name, path string, e rendererEntry) error { |
| 231 | fd, err := unix.Openat(dirfd, name, unix.O_RDONLY|unix.O_NOFOLLOW|unix.O_NONBLOCK|unix.O_NOCTTY|unix.O_CLOEXEC, 0) |
| 232 | if err != nil { |
| 233 | return rendererMismatch(path, "cannot be opened as a file: %v", err) |
| 234 | } |
| 235 | f := os.NewFile(uintptr(fd), path) |
| 236 | defer f.Close() |
| 237 | st, err := rendererSafe(fd, path, unix.S_IFREG, w.uid) |
| 238 | if err != nil { |
| 239 | return err |
| 240 | } |
| 241 | if st.Mode&0o7777 != e.mode { |
| 242 | return rendererMismatch(path, "has mode %04o, MANIFEST lists %04o", st.Mode&0o7777, e.mode) |
| 243 | } |
| 244 | h := sha256.New() |
| 245 | if _, err := io.Copy(h, f); err != nil { |
| 246 | return rendererMismatch(path, "cannot be read: %v", err) |
| 247 | } |
| 248 | if got := hex.EncodeToString(h.Sum(nil)); got != e.sha256 { |
| 249 | return rendererMismatch(path, "has SHA-256 %s, MANIFEST lists %s", got, e.sha256) |
| 250 | } |
| 251 | return nil |
| 252 | } |
| 253 | |