//go:build linux package release import ( "crypto/sha256" "encoding/hex" "errors" "io" "os" "slices" "golang.org/x/sys/unix" "amadan.net/oficina/equipo/internal/wire" ) // verifyRendererTree is VerifyRenderer's check of the disk, the tree to // be owned by uid or root. Every entry is reached from the tree's own // directory descriptor by one name at a time (openat, fstatat and // readlinkat with no-follow), so no link on disk is ever followed, and // the checks on a directory or file are made on the descriptor that is // then read. func verifyRendererTree(dir, pin string, uid uint32) error { root, err := unix.Open(dir, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) if errors.Is(err, unix.ENOENT) { return &RendererError{Health: wire.ToolMissing, Reason: dir + " does not exist"} } if err != nil { return rendererMismatch("", "%s is not a directory this runner can read: %v", dir, err) } defer unix.Close(root) if _, err := rendererSafe(root, "", unix.S_IFDIR, uid); err != nil { return err } manifest, err := readRendererManifest(root, uid) if err != nil { return err } if sum := sha256.Sum256(manifest); hex.EncodeToString(sum[:]) != pin { return rendererMismatch("MANIFEST", "its SHA-256 is %x, the pin is %s", sum, pin) } entries, err := parseRendererManifest(manifest) if err != nil { return rendererMismatch("MANIFEST", "%v", err) } w := rendererWalk{entries: entries, seen: map[string]bool{}, uid: uid} if err := w.dir(root, ""); err != nil { return err } var missing []string for path := range entries { if !w.seen[path] { missing = append(missing, path) } } if len(missing) > 0 { slices.Sort(missing) return rendererMismatch(missing[0], "listed in MANIFEST, not in the tree (%d missing)", len(missing)) } return nil } // readRendererManifest reads the tree's MANIFEST: a regular file, not a // link, safe as any file of the tree is, and no larger than the build // could have written. Like every entry, it is not opened unless fstatat // finds a regular file there (opening a device can do things). func readRendererManifest(root int, uid uint32) ([]byte, error) { var st unix.Stat_t if err := unix.Fstatat(root, "MANIFEST", &st, unix.AT_SYMLINK_NOFOLLOW); err != nil { return nil, rendererMismatch("MANIFEST", "cannot be checked: %v", err) } if st.Mode&unix.S_IFMT != unix.S_IFREG { return nil, rendererMismatch("MANIFEST", "is a %s; the list must be a file", rendererKind(st.Mode)) } fd, err := unix.Openat(root, "MANIFEST", unix.O_RDONLY|unix.O_NOFOLLOW|unix.O_NONBLOCK|unix.O_NOCTTY|unix.O_CLOEXEC, 0) if err != nil { return nil, rendererMismatch("MANIFEST", "cannot be opened as a file: %v", err) } f := os.NewFile(uintptr(fd), "MANIFEST") defer f.Close() if _, err := rendererSafe(fd, "MANIFEST", unix.S_IFREG, uid); err != nil { return nil, err } b, err := io.ReadAll(io.LimitReader(f, rendererManifestMax+1)) if err != nil { return nil, rendererMismatch("MANIFEST", "cannot be read: %v", err) } if len(b) > rendererManifestMax { return nil, rendererMismatch("MANIFEST", "larger than %d bytes", rendererManifestMax) } return b, nil } // rendererSafe checks what fd is: of kind (S_IFDIR or S_IFREG), owned by // uid or root, nothing group or world writable, and, for a file, not // linked from anywhere else (one name only). It returns fd's stat. func rendererSafe(fd int, path string, kind uint32, uid uint32) (unix.Stat_t, error) { var st unix.Stat_t if err := unix.Fstat(fd, &st); err != nil { return st, rendererMismatch(path, "cannot be checked: %v", err) } if st.Mode&unix.S_IFMT != kind { return st, rendererMismatch(path, "is a %s, not a %s", rendererKind(st.Mode), rendererKind(kind)) } if st.Uid != uid && st.Uid != 0 { return st, rendererMismatch(path, "is owned by user %d, not this runner's %d or root", st.Uid, uid) } if st.Mode&0o022 != 0 { return st, rendererMismatch(path, "is group or world writable (%04o)", st.Mode&0o7777) } if kind == unix.S_IFREG && st.Nlink != 1 { return st, rendererMismatch(path, "has %d names, not one", st.Nlink) } return st, nil } func rendererKind(mode uint32) string { switch mode & unix.S_IFMT { case unix.S_IFDIR: return "dir" case unix.S_IFREG: return "file" case unix.S_IFLNK: return "link" } return "special file" } type rendererWalk struct { entries map[string]rendererEntry seen map[string]bool uid uint32 } // dir checks the entries of the open directory fd, the tree's path rel // ("" for its root), and the directories under it. func (w *rendererWalk) dir(fd int, rel string) error { names, err := w.names(fd, rel) if err != nil { return err } for _, name := range names { path := name if rel != "" { path = rel + "/" + name } e := w.entries[path] var st unix.Stat_t if err := unix.Fstatat(fd, name, &st, unix.AT_SYMLINK_NOFOLLOW); err != nil { return rendererMismatch(path, "cannot be checked: %v", err) } if got := rendererKind(st.Mode); got != e.kind { return rendererMismatch(path, "is a %s, MANIFEST lists a %s", got, e.kind) } w.seen[path] = true switch e.kind { case "dir": sub, err := unix.Openat(fd, name, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) if err != nil { return rendererMismatch(path, "cannot be opened as a directory: %v", err) } _, err = rendererSafe(sub, path, unix.S_IFDIR, w.uid) if err == nil { err = w.dir(sub, path) } unix.Close(sub) if err != nil { return err } case "file": if err := w.file(fd, name, path, e); err != nil { return err } case "link": buf := make([]byte, unix.PathMax) n, err := unix.Readlinkat(fd, name, buf) if err != nil || n >= len(buf) { return rendererMismatch(path, "cannot be read as a link: %v", err) } if got := string(buf[:n]); got != e.target { return rendererMismatch(path, "points at %q, MANIFEST lists %q", got, e.target) } } } return nil } // names lists the directory fd, sorted, refusing at once any name // MANIFEST does not list (MANIFEST itself, at the root, is skipped: it // was checked first), so an unlisted directory is never entered and a // huge one is never held in memory. func (w *rendererWalk) names(fd int, rel string) ([]string, error) { buf := make([]byte, 32<<10) var names []string for { n, err := unix.Getdents(fd, buf) if errors.Is(err, unix.EINTR) { continue } if err != nil { return nil, rendererMismatch(rel, "cannot be listed: %v", err) } if n <= 0 { break } var batch []string _, _, batch = unix.ParseDirent(buf[:n], -1, nil) for _, name := range batch { path := name if rel != "" { path = rel + "/" + name } if rel == "" && name == "MANIFEST" { continue } if _, ok := w.entries[path]; !ok { return nil, rendererMismatch(path, "is in the tree, not in MANIFEST") } names = append(names, name) } } slices.Sort(names) return names, nil } // file checks a file entry: opened without following a link and without // blocking (a fifo swapped in after fstatat cannot hang the check), a // regular file with exactly the listed mode, safe, and the listed hash. func (w *rendererWalk) file(dirfd int, name, path string, e rendererEntry) error { fd, err := unix.Openat(dirfd, name, unix.O_RDONLY|unix.O_NOFOLLOW|unix.O_NONBLOCK|unix.O_NOCTTY|unix.O_CLOEXEC, 0) if err != nil { return rendererMismatch(path, "cannot be opened as a file: %v", err) } f := os.NewFile(uintptr(fd), path) defer f.Close() st, err := rendererSafe(fd, path, unix.S_IFREG, w.uid) if err != nil { return err } if st.Mode&0o7777 != e.mode { return rendererMismatch(path, "has mode %04o, MANIFEST lists %04o", st.Mode&0o7777, e.mode) } h := sha256.New() if _, err := io.Copy(h, f); err != nil { return rendererMismatch(path, "cannot be read: %v", err) } if got := hex.EncodeToString(h.Sum(nil)); got != e.sha256 { return rendererMismatch(path, "has SHA-256 %s, MANIFEST lists %s", got, e.sha256) } return nil }