//go:build linux package render import ( "errors" "fmt" "os" "path/filepath" "golang.org/x/sys/unix" "amadan.net/oficina/equipo/internal/release" "amadan.net/oficina/equipo/internal/wire" ) // testBeforeVerify and testAfterVerify, when set, run just before the // tree's verification and between it and the check that the opened // objects are still the tree (tests move or replace things there). var testBeforeVerify, testAfterVerify func() // verifiedTree verifies the renderer tree against pin and returns the // tree's usr and etc/fonts, opened before the verification, as the // descriptors bubblewrap binds. // // Threat model: this user and root are trusted, as VerifyRenderer says; // nobody else may be able to change what is mounted. Everything is // reached from one descriptor of the tree's parent, opened with no // symlink followed: the parent must be owned by this user or root and // writable by nobody else, so nobody else can rename the tree or put // another in its place, and the ancestors above it no longer matter. // VerifyRenderer checks the tree through that same descriptor // (/proc/self/fd//) and that nobody else can write any of // it. Afterwards the tree, usr and etc/fonts are opened again from the // parent and must be the very objects opened before (the same device // and file number), so a replacement at any point between the first // open and the mount is refused. A replacement that is put back before the second open is // only possible for this user or root, and is out of scope. func verifiedTree(tree, pin string) (usr, fonts *os.File, err error) { if !pinShaped(pin) { return nil, nil, &release.RendererError{Health: wire.ToolUnpinned, Reason: "no renderer pin is linked into this runner"} } parent, name := filepath.Dir(tree), filepath.Base(tree) if name == "/" || name == "." || name == ".." { return nil, nil, mismatch("", "%q names no tree", tree) } pfd, err := openDir(unix.AT_FDCWD, parent, unix.RESOLVE_NO_SYMLINKS|unix.RESOLVE_NO_MAGICLINKS) if errors.Is(err, unix.ENOENT) { return nil, nil, &release.RendererError{Health: wire.ToolMissing, Reason: parent + " does not exist"} } if err != nil { return nil, nil, mismatch("", "%s: %v", parent, err) } defer unix.Close(pfd) var st unix.Stat_t if err = unix.Fstat(pfd, &st); err != nil || st.Mode&0o022 != 0 || st.Uid != uint32(os.Geteuid()) && st.Uid != 0 { return nil, nil, mismatch("", "%s, the tree's directory, can be written by another user", parent) } before, absent, err := openTreeParts(pfd, name) if absent { return nil, nil, &release.RendererError{Health: wire.ToolMissing, Reason: tree + " does not exist"} } if err != nil { return nil, nil, mismatch("", "%s: %v", tree, err) } keep := false defer func() { if !keep { closeFDs(before) } }() if testBeforeVerify != nil { testBeforeVerify() } if err = release.VerifyRenderer(fmt.Sprintf("/proc/self/fd/%d/%s", pfd, name), pin); err != nil { return nil, nil, err } if testAfterVerify != nil { testAfterVerify() } after, _, err := openTreeParts(pfd, name) defer closeFDs(after) if err != nil { return nil, nil, mismatch("", "the tree changed while it was verified: %v", err) } for i, part := range []string{"", "usr", "etc/fonts"} { if !sameObject(before[i], after[i]) { return nil, nil, mismatch(part, "the tree changed while it was verified") } } unix.Close(before[0]) keep = true return os.NewFile(uintptr(before[1]), filepath.Join(tree, "usr")), os.NewFile(uintptr(before[2]), filepath.Join(tree, "etc/fonts")), nil } // openTreeParts opens the tree (name, below the parent pfd), its usr and // its etc/fonts, in that order. On an error none is left open, and // absent says the tree itself does not exist. func openTreeParts(pfd int, name string) (fds [3]int, absent bool, err error) { fds = [3]int{-1, -1, -1} const resolve = unix.RESOLVE_BENEATH | unix.RESOLVE_NO_SYMLINKS | unix.RESOLVE_NO_MAGICLINKS if fds[0], err = openDir(pfd, name, resolve); err != nil { return [3]int{-1, -1, -1}, errors.Is(err, unix.ENOENT), err } for i, rel := range []string{"usr", "etc/fonts"} { if fds[i+1], err = openDir(fds[0], rel, resolve); err != nil { fds[i+1] = -1 closeFDs(fds) return [3]int{-1, -1, -1}, false, fmt.Errorf("%s: %w", rel, err) } } return fds, false, nil } func openDir(at int, path string, resolve uint64) (int, error) { return unix.Openat2(at, path, &unix.OpenHow{Flags: unix.O_PATH | unix.O_DIRECTORY | unix.O_CLOEXEC, Resolve: resolve}) } func closeFDs(fds [3]int) { for _, fd := range fds { if fd >= 0 { unix.Close(fd) } } } // sameObject is two descriptors of the same file. func sameObject(a, b int) bool { var x, y unix.Stat_t return a >= 0 && b >= 0 && unix.Fstat(a, &x) == nil && unix.Fstat(b, &y) == nil && x.Dev == y.Dev && x.Ino == y.Ino } // pinShaped is a SHA-256 in lower-case hex, as release.RendererPin gives. func pinShaped(pin string) bool { if len(pin) != 64 { return false } for _, c := range pin { if (c < '0' || c > '9') && (c < 'a' || c > 'f') { return false } } return true } func mismatch(path, format string, args ...any) error { return &release.RendererError{Health: wire.ToolMismatch, Path: path, Reason: fmt.Sprintf(format, args...)} }