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 release
4
5import (
6 "crypto/sha256"
7 "encoding/hex"
8 "errors"
9 "fmt"
10 "io/fs"
11 "os"
12 "path/filepath"
13 "slices"
14 "strings"
15 "syscall"
16 "testing"
17
18 "amadan.net/oficina/equipo/internal/testci"
19 "amadan.net/oficina/equipo/internal/wire"
20)
21
22// writeVerifyTree writes a small renderer tree under dir/tree, as an
23// install leaves one: dirs 0755, files 0755 or 0644, relative links, and
24// its MANIFEST. It returns the tree and the pin. Modes are set
25// explicitly, so the test does not depend on the umask.
26func writeVerifyTree(t *testing.T, dir string) (string, string) {
27 t.Helper()
28 root := filepath.Join(dir, "tree")
29 mkdir := func(p string) {
30 if err := os.Mkdir(filepath.Join(root, p), 0o755); err != nil {
31 t.Fatal(err)
32 }
33 if err := os.Chmod(filepath.Join(root, p), 0o755); err != nil {
34 t.Fatal(err)
35 }
36 }
37 file := func(p, body string, mode os.FileMode) {
38 if err := os.WriteFile(filepath.Join(root, p), []byte(body), 0o600); err != nil {
39 t.Fatal(err)
40 }
41 if err := os.Chmod(filepath.Join(root, p), mode); err != nil {
42 t.Fatal(err)
43 }
44 }
45 link := func(p, target string) {
46 if err := os.Symlink(target, filepath.Join(root, p)); err != nil {
47 t.Fatal(err)
48 }
49 }
50 mkdir("")
51 for _, d := range []string{"etc", "etc/fonts", "usr", "usr/bin", "usr/lib", "usr/lib/chromium", "usr/share", "usr/share/fonts"} {
52 mkdir(d)
53 }
54 file("usr/lib/chromium/chromium-headless-shell", "browser", 0o755)
55 file("usr/lib-extra", "sorts before usr/lib/", 0o644)
56 file("usr/share/fonts/f.conf", "<fontconfig/>", 0o644)
57 link("usr/bin/chromium-headless-shell", "../lib/chromium/chromium-headless-shell")
58 link("etc/fonts/f.conf", "../../usr/share/fonts/f.conf")
59 return root, writeVerifyManifest(t, root)
60}
61
62// writeVerifyManifest lists the tree as it is on disk now, writes the
63// MANIFEST (0644) and returns its SHA-256: what an attacker able to
64// rewrite the whole tree would also do.
65func writeVerifyManifest(t *testing.T, root string) string {
66 t.Helper()
67 var lines []string
68 err := filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
69 if err != nil || p == root {
70 return err
71 }
72 rel, _ := filepath.Rel(root, p)
73 if rel == "MANIFEST" {
74 return nil
75 }
76 info, err := os.Lstat(p)
77 if err != nil {
78 return err
79 }
80 switch {
81 case info.IsDir():
82 lines = append(lines, "dir "+rel+"\n")
83 case info.Mode()&fs.ModeSymlink != 0:
84 target, err := os.Readlink(p)
85 if err != nil {
86 return err
87 }
88 lines = append(lines, "link "+rel+" "+target+"\n")
89 default:
90 body, err := os.ReadFile(p)
91 if err != nil {
92 return err
93 }
94 sum := sha256.Sum256(body)
95 lines = append(lines, fmt.Sprintf("file %s %s %04o\n", rel, hex.EncodeToString(sum[:]), info.Mode().Perm()))
96 }
97 return nil
98 })
99 if err != nil {
100 t.Fatal(err)
101 }
102 // Sorted by path, as the build sorts: "usr/lib-extra" before "usr/lib/...".
103 slices.SortFunc(lines, func(a, b string) int {
104 return strings.Compare(strings.Fields(a)[1], strings.Fields(b)[1])
105 })
106 text := strings.Join(lines, "")
107 manifest := filepath.Join(root, "MANIFEST")
108 _ = os.Remove(manifest)
109 if err := os.WriteFile(manifest, []byte(text), 0o600); err != nil {
110 t.Fatal(err)
111 }
112 if err := os.Chmod(manifest, 0o644); err != nil {
113 t.Fatal(err)
114 }
115 sum := sha256.Sum256([]byte(text))
116 return hex.EncodeToString(sum[:])
117}
118
119func TestVerifyRendererAcceptsThePinnedTree(t *testing.T) {
120 root, pin := writeVerifyTree(t, t.TempDir())
121 if err := VerifyRenderer(root, pin); err != nil {
122 t.Fatal(err)
123 }
124 if got := RendererHealth(VerifyRenderer(root, pin)); got != wire.ToolHealthy {
125 t.Fatalf("health %q", got)
126 }
127}
128
129// The verifier and scripts/build-renderer-tree.py agree on the MANIFEST:
130// the tree the build keeps verifies against the pin it prints.
131func TestVerifyRendererAcceptsTheBuiltTree(t *testing.T) {
132 testci.Files(t, "../../scripts/build-renderer-tree.py")
133 testci.Tools(t, "python3")
134 dir := t.TempDir()
135 f := newRendererFixture()
136 // A trailing slash or /. on a directory target is fine, for Linux,
137 // the build and the verifier alike.
138 f.packages["libc6"] = append(f.packages["libc6"],
139 debMember{name: "./usr/lib64/lib", link: "../lib/", mode: 0o777},
140 debMember{name: "./usr/lib64/lib-dot", link: "lib/.", mode: 0o777})
141 pinFile, urls := f.write(t, dir)
142 tree := filepath.Join(dir, "tree")
143 pin, stderr, err := buildRendererTree(t, pinFile, urls, filepath.Join(dir, "renderer.tar.gz"), 0, "--tree", tree)
144 if err != nil {
145 t.Fatalf("build: %v\n%s", err, stderr)
146 }
147 if err := VerifyRenderer(tree, strings.TrimSpace(pin)); err != nil {
148 t.Fatal(err)
149 }
150}
151
152func TestVerifyRendererMissingTree(t *testing.T) {
153 dir := t.TempDir()
154 _, pin := writeVerifyTree(t, dir)
155 for _, p := range []string{filepath.Join(dir, "absent"), filepath.Join(dir, "absent", "tree")} {
156 err := VerifyRenderer(p, pin)
157 if !errors.Is(err, ErrRendererMissing) || RendererHealth(err) != wire.ToolMissing {
158 t.Errorf("%s: %v", p, err)
159 }
160 }
161 // A path that is not absolute and clean is a caller's mistake, not
162 // a missing tree.
163 for _, p := range []string{"tree", dir + "/./tree", dir + "/tree/"} {
164 if err := VerifyRenderer(p, pin); !errors.Is(err, ErrRendererMismatch) {
165 t.Errorf("%q: %v", p, err)
166 }
167 }
168}
169
170// Every way the installed tree can differ from the pinned one is a
171// mismatch, named by the entry that differs ("" is the tree's root).
172func TestVerifyRendererRefuses(t *testing.T) {
173 const browser = "usr/lib/chromium/chromium-headless-shell"
174 const link = "usr/bin/chromium-headless-shell"
175 for _, c := range []struct {
176 name string
177 path string // the entry the error names
178 why string // and what it says of it
179 edit func(t *testing.T, root, outside string)
180 uid int // when set, verify as this user instead
181 }{
182 {name: "tampered byte", why: "has SHA-256", path: browser, edit: func(t *testing.T, root, _ string) {
183 writeTreeFile(t, filepath.Join(root, browser), "Browser", 0o755)
184 }},
185 {name: "extra file", why: "not in MANIFEST", path: "usr/lib/chromium/extra", edit: func(t *testing.T, root, _ string) {
186 writeTreeFile(t, filepath.Join(root, "usr/lib/chromium/extra"), "x", 0o644)
187 }},
188 {name: "extra file at the root", why: "not in MANIFEST", path: "extra", edit: func(t *testing.T, root, _ string) {
189 writeTreeFile(t, filepath.Join(root, "extra"), "x", 0o644)
190 }},
191 {name: "extra directory", why: "not in MANIFEST", path: "usr/lib/extra", edit: func(t *testing.T, root, _ string) {
192 treeMust(t, os.Mkdir(filepath.Join(root, "usr/lib/extra"), 0o755))
193 }},
194 {name: "extra fifo", why: "not in MANIFEST", path: "usr/fifo", edit: func(t *testing.T, root, _ string) {
195 treeMust(t, syscall.Mkfifo(filepath.Join(root, "usr/fifo"), 0o644))
196 }},
197 {name: "missing file", why: "not in the tree", path: "usr/share/fonts/f.conf", edit: func(t *testing.T, root, _ string) {
198 treeMust(t, os.Remove(filepath.Join(root, "usr/share/fonts/f.conf")))
199 }},
200 {name: "missing directory", why: "not in the tree", path: "usr/share", edit: func(t *testing.T, root, _ string) {
201 treeMust(t, os.RemoveAll(filepath.Join(root, "usr/share")))
202 }},
203 {name: "missing link", why: "not in the tree", path: link, edit: func(t *testing.T, root, _ string) {
204 treeMust(t, os.Remove(filepath.Join(root, link)))
205 }},
206 {name: "link retargeted", why: "points at", path: link, edit: func(t *testing.T, root, _ string) {
207 treeMust(t, os.Remove(filepath.Join(root, link)))
208 treeMust(t, os.Symlink("../lib/chromium/./chromium-headless-shell", filepath.Join(root, link)))
209 }},
210 {name: "mode widened", why: "group or world writable", path: "usr/lib-extra", edit: func(t *testing.T, root, _ string) {
211 treeMust(t, os.Chmod(filepath.Join(root, "usr/lib-extra"), 0o664))
212 }},
213 {name: "mode changed", why: "has mode 0700", path: browser, edit: func(t *testing.T, root, _ string) {
214 treeMust(t, os.Chmod(filepath.Join(root, browser), 0o700))
215 }},
216 {name: "setuid added", why: "has mode 4755", path: browser, edit: func(t *testing.T, root, _ string) {
217 treeMust(t, os.Chmod(filepath.Join(root, browser), 0o755|os.ModeSetuid))
218 }},
219 {name: "directory group writable", why: "group or world writable", path: "usr/lib", edit: func(t *testing.T, root, _ string) {
220 treeMust(t, os.Chmod(filepath.Join(root, "usr/lib"), 0o775))
221 }},
222 {name: "directory world writable and sticky", why: "group or world writable", path: "usr/share/fonts", edit: func(t *testing.T, root, _ string) {
223 treeMust(t, os.Chmod(filepath.Join(root, "usr/share/fonts"), 0o777|os.ModeSticky))
224 }},
225 {name: "root group writable", why: "group or world writable", path: "", edit: func(t *testing.T, root, _ string) {
226 treeMust(t, os.Chmod(root, 0o775))
227 }},
228 {name: "MANIFEST group writable", why: "group or world writable", path: "MANIFEST", edit: func(t *testing.T, root, _ string) {
229 treeMust(t, os.Chmod(filepath.Join(root, "MANIFEST"), 0o664))
230 }},
231 {name: "MANIFEST missing", why: "cannot be checked", path: "MANIFEST", edit: func(t *testing.T, root, _ string) {
232 treeMust(t, os.Remove(filepath.Join(root, "MANIFEST")))
233 }},
234 {name: "MANIFEST a link to the pinned one", why: "is a link; the list must be a file", path: "MANIFEST", edit: func(t *testing.T, root, outside string) {
235 treeMust(t, os.Rename(filepath.Join(root, "MANIFEST"), filepath.Join(outside, "MANIFEST")))
236 treeMust(t, os.Symlink(filepath.Join(outside, "MANIFEST"), filepath.Join(root, "MANIFEST")))
237 }},
238 {name: "MANIFEST a fifo", why: "is a special file; the list must be a file", path: "MANIFEST", edit: func(t *testing.T, root, _ string) {
239 treeMust(t, os.Remove(filepath.Join(root, "MANIFEST")))
240 treeMust(t, syscall.Mkfifo(filepath.Join(root, "MANIFEST"), 0o644))
241 }},
242 {name: "MANIFEST swapped with a matching tree", why: "the pin is", path: "MANIFEST", edit: func(t *testing.T, root, _ string) {
243 writeTreeFile(t, filepath.Join(root, browser), "another browser", 0o755)
244 writeVerifyManifest(t, root)
245 }},
246 {name: "directory replaced by a link to a copy", why: "is a link, MANIFEST lists a dir", path: "usr/lib/chromium", edit: func(t *testing.T, root, outside string) {
247 copyDir := filepath.Join(outside, "chromium")
248 treeMust(t, os.Rename(filepath.Join(root, "usr/lib/chromium"), copyDir))
249 treeMust(t, os.Symlink(copyDir, filepath.Join(root, "usr/lib/chromium")))
250 }},
251 {name: "file replaced by a link to a copy", why: "is a link, MANIFEST lists a file", path: browser, edit: func(t *testing.T, root, outside string) {
252 treeMust(t, os.Rename(filepath.Join(root, browser), filepath.Join(outside, "browser")))
253 treeMust(t, os.Symlink(filepath.Join(outside, "browser"), filepath.Join(root, browser)))
254 }},
255 {name: "link replaced by its target's copy", why: "is a file, MANIFEST lists a link", path: link, edit: func(t *testing.T, root, _ string) {
256 treeMust(t, os.Remove(filepath.Join(root, link)))
257 writeTreeFile(t, filepath.Join(root, link), "browser", 0o755)
258 }},
259 {name: "file replaced by a directory", why: "is a dir, MANIFEST lists a file", path: "usr/lib-extra", edit: func(t *testing.T, root, _ string) {
260 treeMust(t, os.Remove(filepath.Join(root, "usr/lib-extra")))
261 treeMust(t, os.Mkdir(filepath.Join(root, "usr/lib-extra"), 0o755))
262 }},
263 {name: "directory replaced by a file", why: "is a file, MANIFEST lists a dir", path: "usr/share/fonts", edit: func(t *testing.T, root, _ string) {
264 treeMust(t, os.RemoveAll(filepath.Join(root, "usr/share/fonts")))
265 writeTreeFile(t, filepath.Join(root, "usr/share/fonts"), "", 0o644)
266 }},
267 {name: "file replaced by a fifo", why: "is a special file, MANIFEST lists a file", path: "usr/lib-extra", edit: func(t *testing.T, root, _ string) {
268 treeMust(t, os.Remove(filepath.Join(root, "usr/lib-extra")))
269 treeMust(t, syscall.Mkfifo(filepath.Join(root, "usr/lib-extra"), 0o644))
270 }},
271 {name: "file hard linked from outside", why: "has 2 names", path: browser, edit: func(t *testing.T, root, outside string) {
272 treeMust(t, os.Link(filepath.Join(root, browser), filepath.Join(outside, "browser")))
273 }},
274 {name: "root a link to the pinned tree", why: "not a directory this runner can read", path: "", edit: func(t *testing.T, root, outside string) {
275 treeMust(t, os.Rename(root, filepath.Join(outside, "tree")))
276 treeMust(t, os.Symlink(filepath.Join(outside, "tree"), root))
277 }},
278 {name: "owned by another user", why: "is owned by user", path: "", uid: os.Geteuid() + 1},
279 } {
280 t.Run(c.name, func(t *testing.T) {
281 if c.uid != 0 && os.Geteuid() == 0 {
282 t.Skip("root owns the tree; any owner check passes for root")
283 }
284 dir := t.TempDir()
285 root, pin := writeVerifyTree(t, dir)
286 outside := filepath.Join(dir, "outside")
287 treeMust(t, os.Mkdir(outside, 0o755))
288 if c.edit != nil {
289 c.edit(t, root, outside)
290 }
291 var err error
292 if c.uid != 0 {
293 err = verifyRendererTree(root, pin, uint32(c.uid))
294 } else {
295 err = VerifyRenderer(root, pin)
296 }
297 var re *RendererError
298 if !errors.As(err, &re) || !errors.Is(err, ErrRendererMismatch) || RendererHealth(err) != wire.ToolMismatch {
299 t.Fatalf("want a mismatch, got %v", err)
300 }
301 if re.Path != c.path || !strings.Contains(re.Reason, c.why) {
302 t.Fatalf("got %v, want %q: ...%s", err, c.path, c.why)
303 }
304 })
305 }
306}
307
308// A MANIFEST that matches its pin is still read strictly: a pinned list
309// the build could not have written (here, a link out of the tree) is a
310// mismatch, as is one too large to be the build's.
311func TestVerifyRendererRefusesAPinnedBadManifest(t *testing.T) {
312 root, _ := writeVerifyTree(t, t.TempDir())
313 treeMust(t, os.Symlink("../../outside", filepath.Join(root, "usr/out")))
314 err := VerifyRenderer(root, writeVerifyManifest(t, root))
315 var re *RendererError
316 if !errors.As(err, &re) || re.Health != wire.ToolMismatch || re.Path != "MANIFEST" || !strings.Contains(re.Reason, "climbs out of the tree") {
317 t.Fatalf("escaping link: %v", err)
318 }
319
320 // Linux refuses f/ and f/. (ENOTDIR) when f is a file, directly or
321 // through another link; so does the verifier, even when pinned.
322 for _, links := range [][][2]string{
323 {{"usr/bad", "lib-extra/"}},
324 {{"usr/bad", "lib-extra/."}},
325 {{"usr/bad", "via/"}, {"usr/via", "lib-extra"}},
326 {{"usr/bad", "via"}, {"usr/via", "lib-extra/."}},
327 } {
328 root, _ := writeVerifyTree(t, t.TempDir())
329 for _, l := range links {
330 treeMust(t, os.Symlink(l[1], filepath.Join(root, l[0])))
331 }
332 err := VerifyRenderer(root, writeVerifyManifest(t, root))
333 if !errors.As(err, &re) || re.Path != "MANIFEST" || !strings.Contains(re.Reason, "is not a directory") {
334 t.Fatalf("%v: %v", links, err)
335 }
336 }
337
338 big := make([]byte, rendererManifestMax+1)
339 for i := range big {
340 big[i] = '\n'
341 }
342 writeTreeFile(t, filepath.Join(root, "MANIFEST"), string(big), 0o644)
343 sum := sha256.Sum256(big)
344 err = VerifyRenderer(root, hex.EncodeToString(sum[:]))
345 if !errors.As(err, &re) || re.Health != wire.ToolMismatch || re.Path != "MANIFEST" || !strings.Contains(re.Reason, "larger than") {
346 t.Fatalf("oversized MANIFEST: %v", err)
347 }
348}
349
350func writeTreeFile(t *testing.T, p, body string, mode os.FileMode) {
351 t.Helper()
352 treeMust(t, os.WriteFile(p, []byte(body), 0o600))
353 treeMust(t, os.Chmod(p, mode))
354}
355
356func treeMust(t *testing.T, err error) {
357 t.Helper()
358 if err != nil {
359 t.Fatal(err)
360 }
361}
362