oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1//go:build linux
2
3package render
4
5import (
6 "errors"
7 "fmt"
8 "os"
9 "path/filepath"
10
11 "golang.org/x/sys/unix"
12
13 "amadan.net/oficina/equipo/internal/release"
14 "amadan.net/oficina/equipo/internal/wire"
15)
16
17// testBeforeVerify and testAfterVerify, when set, run just before the
18// tree's verification and between it and the check that the opened
19// objects are still the tree (tests move or replace things there).
20var testBeforeVerify, testAfterVerify func()
21
22// verifiedTree verifies the renderer tree against pin and returns the
23// tree's usr and etc/fonts, opened before the verification, as the
24// descriptors bubblewrap binds.
25//
26// Threat model: this user and root are trusted, as VerifyRenderer says;
27// nobody else may be able to change what is mounted. Everything is
28// reached from one descriptor of the tree's parent, opened with no
29// symlink followed: the parent must be owned by this user or root and
30// writable by nobody else, so nobody else can rename the tree or put
31// another in its place, and the ancestors above it no longer matter.
32// VerifyRenderer checks the tree through that same descriptor
33// (/proc/self/fd/<parent>/<name>) and that nobody else can write any of
34// it. Afterwards the tree, usr and etc/fonts are opened again from the
35// parent and must be the very objects opened before (the same device
36// and file number), so a replacement at any point between the first
37// open and the mount is refused. A replacement that is put back before the second open is
38// only possible for this user or root, and is out of scope.
39func verifiedTree(tree, pin string) (usr, fonts *os.File, err error) {
40 if !pinShaped(pin) {
41 return nil, nil, &release.RendererError{Health: wire.ToolUnpinned, Reason: "no renderer pin is linked into this runner"}
42 }
43 parent, name := filepath.Dir(tree), filepath.Base(tree)
44 if name == "/" || name == "." || name == ".." {
45 return nil, nil, mismatch("", "%q names no tree", tree)
46 }
47 pfd, err := openDir(unix.AT_FDCWD, parent, unix.RESOLVE_NO_SYMLINKS|unix.RESOLVE_NO_MAGICLINKS)
48 if errors.Is(err, unix.ENOENT) {
49 return nil, nil, &release.RendererError{Health: wire.ToolMissing, Reason: parent + " does not exist"}
50 }
51 if err != nil {
52 return nil, nil, mismatch("", "%s: %v", parent, err)
53 }
54 defer unix.Close(pfd)
55 var st unix.Stat_t
56 if err = unix.Fstat(pfd, &st); err != nil || st.Mode&0o022 != 0 || st.Uid != uint32(os.Geteuid()) && st.Uid != 0 {
57 return nil, nil, mismatch("", "%s, the tree's directory, can be written by another user", parent)
58 }
59 before, absent, err := openTreeParts(pfd, name)
60 if absent {
61 return nil, nil, &release.RendererError{Health: wire.ToolMissing, Reason: tree + " does not exist"}
62 }
63 if err != nil {
64 return nil, nil, mismatch("", "%s: %v", tree, err)
65 }
66 keep := false
67 defer func() {
68 if !keep {
69 closeFDs(before)
70 }
71 }()
72 if testBeforeVerify != nil {
73 testBeforeVerify()
74 }
75 if err = release.VerifyRenderer(fmt.Sprintf("/proc/self/fd/%d/%s", pfd, name), pin); err != nil {
76 return nil, nil, err
77 }
78 if testAfterVerify != nil {
79 testAfterVerify()
80 }
81 after, _, err := openTreeParts(pfd, name)
82 defer closeFDs(after)
83 if err != nil {
84 return nil, nil, mismatch("", "the tree changed while it was verified: %v", err)
85 }
86 for i, part := range []string{"", "usr", "etc/fonts"} {
87 if !sameObject(before[i], after[i]) {
88 return nil, nil, mismatch(part, "the tree changed while it was verified")
89 }
90 }
91 unix.Close(before[0])
92 keep = true
93 return os.NewFile(uintptr(before[1]), filepath.Join(tree, "usr")), os.NewFile(uintptr(before[2]), filepath.Join(tree, "etc/fonts")), nil
94}
95
96// openTreeParts opens the tree (name, below the parent pfd), its usr and
97// its etc/fonts, in that order. On an error none is left open, and
98// absent says the tree itself does not exist.
99func openTreeParts(pfd int, name string) (fds [3]int, absent bool, err error) {
100 fds = [3]int{-1, -1, -1}
101 const resolve = unix.RESOLVE_BENEATH | unix.RESOLVE_NO_SYMLINKS | unix.RESOLVE_NO_MAGICLINKS
102 if fds[0], err = openDir(pfd, name, resolve); err != nil {
103 return [3]int{-1, -1, -1}, errors.Is(err, unix.ENOENT), err
104 }
105 for i, rel := range []string{"usr", "etc/fonts"} {
106 if fds[i+1], err = openDir(fds[0], rel, resolve); err != nil {
107 fds[i+1] = -1
108 closeFDs(fds)
109 return [3]int{-1, -1, -1}, false, fmt.Errorf("%s: %w", rel, err)
110 }
111 }
112 return fds, false, nil
113}
114
115func openDir(at int, path string, resolve uint64) (int, error) {
116 return unix.Openat2(at, path, &unix.OpenHow{Flags: unix.O_PATH | unix.O_DIRECTORY | unix.O_CLOEXEC, Resolve: resolve})
117}
118
119func closeFDs(fds [3]int) {
120 for _, fd := range fds {
121 if fd >= 0 {
122 unix.Close(fd)
123 }
124 }
125}
126
127// sameObject is two descriptors of the same file.
128func sameObject(a, b int) bool {
129 var x, y unix.Stat_t
130 return a >= 0 && b >= 0 && unix.Fstat(a, &x) == nil && unix.Fstat(b, &y) == nil && x.Dev == y.Dev && x.Ino == y.Ino
131}
132
133// pinShaped is a SHA-256 in lower-case hex, as release.RendererPin gives.
134func pinShaped(pin string) bool {
135 if len(pin) != 64 {
136 return false
137 }
138 for _, c := range pin {
139 if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
140 return false
141 }
142 }
143 return true
144}
145
146func mismatch(path, format string, args ...any) error {
147 return &release.RendererError{Health: wire.ToolMismatch, Path: path, Reason: fmt.Sprintf(format, args...)}
148}
149