//go:build linux package release import ( "crypto/sha256" "encoding/hex" "errors" "fmt" "io/fs" "os" "path/filepath" "slices" "strings" "syscall" "testing" "amadan.net/oficina/equipo/internal/testci" "amadan.net/oficina/equipo/internal/wire" ) // writeVerifyTree writes a small renderer tree under dir/tree, as an // install leaves one: dirs 0755, files 0755 or 0644, relative links, and // its MANIFEST. It returns the tree and the pin. Modes are set // explicitly, so the test does not depend on the umask. func writeVerifyTree(t *testing.T, dir string) (string, string) { t.Helper() root := filepath.Join(dir, "tree") mkdir := func(p string) { if err := os.Mkdir(filepath.Join(root, p), 0o755); err != nil { t.Fatal(err) } if err := os.Chmod(filepath.Join(root, p), 0o755); err != nil { t.Fatal(err) } } file := func(p, body string, mode os.FileMode) { if err := os.WriteFile(filepath.Join(root, p), []byte(body), 0o600); err != nil { t.Fatal(err) } if err := os.Chmod(filepath.Join(root, p), mode); err != nil { t.Fatal(err) } } link := func(p, target string) { if err := os.Symlink(target, filepath.Join(root, p)); err != nil { t.Fatal(err) } } mkdir("") for _, d := range []string{"etc", "etc/fonts", "usr", "usr/bin", "usr/lib", "usr/lib/chromium", "usr/share", "usr/share/fonts"} { mkdir(d) } file("usr/lib/chromium/chromium-headless-shell", "browser", 0o755) file("usr/lib-extra", "sorts before usr/lib/", 0o644) file("usr/share/fonts/f.conf", "", 0o644) link("usr/bin/chromium-headless-shell", "../lib/chromium/chromium-headless-shell") link("etc/fonts/f.conf", "../../usr/share/fonts/f.conf") return root, writeVerifyManifest(t, root) } // writeVerifyManifest lists the tree as it is on disk now, writes the // MANIFEST (0644) and returns its SHA-256: what an attacker able to // rewrite the whole tree would also do. func writeVerifyManifest(t *testing.T, root string) string { t.Helper() var lines []string err := filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error { if err != nil || p == root { return err } rel, _ := filepath.Rel(root, p) if rel == "MANIFEST" { return nil } info, err := os.Lstat(p) if err != nil { return err } switch { case info.IsDir(): lines = append(lines, "dir "+rel+"\n") case info.Mode()&fs.ModeSymlink != 0: target, err := os.Readlink(p) if err != nil { return err } lines = append(lines, "link "+rel+" "+target+"\n") default: body, err := os.ReadFile(p) if err != nil { return err } sum := sha256.Sum256(body) lines = append(lines, fmt.Sprintf("file %s %s %04o\n", rel, hex.EncodeToString(sum[:]), info.Mode().Perm())) } return nil }) if err != nil { t.Fatal(err) } // Sorted by path, as the build sorts: "usr/lib-extra" before "usr/lib/...". slices.SortFunc(lines, func(a, b string) int { return strings.Compare(strings.Fields(a)[1], strings.Fields(b)[1]) }) text := strings.Join(lines, "") manifest := filepath.Join(root, "MANIFEST") _ = os.Remove(manifest) if err := os.WriteFile(manifest, []byte(text), 0o600); err != nil { t.Fatal(err) } if err := os.Chmod(manifest, 0o644); err != nil { t.Fatal(err) } sum := sha256.Sum256([]byte(text)) return hex.EncodeToString(sum[:]) } func TestVerifyRendererAcceptsThePinnedTree(t *testing.T) { root, pin := writeVerifyTree(t, t.TempDir()) if err := VerifyRenderer(root, pin); err != nil { t.Fatal(err) } if got := RendererHealth(VerifyRenderer(root, pin)); got != wire.ToolHealthy { t.Fatalf("health %q", got) } } // The verifier and scripts/build-renderer-tree.py agree on the MANIFEST: // the tree the build keeps verifies against the pin it prints. func TestVerifyRendererAcceptsTheBuiltTree(t *testing.T) { testci.Files(t, "../../scripts/build-renderer-tree.py") testci.Tools(t, "python3") dir := t.TempDir() f := newRendererFixture() // A trailing slash or /. on a directory target is fine, for Linux, // the build and the verifier alike. f.packages["libc6"] = append(f.packages["libc6"], debMember{name: "./usr/lib64/lib", link: "../lib/", mode: 0o777}, debMember{name: "./usr/lib64/lib-dot", link: "lib/.", mode: 0o777}) pinFile, urls := f.write(t, dir) tree := filepath.Join(dir, "tree") pin, stderr, err := buildRendererTree(t, pinFile, urls, filepath.Join(dir, "renderer.tar.gz"), 0, "--tree", tree) if err != nil { t.Fatalf("build: %v\n%s", err, stderr) } if err := VerifyRenderer(tree, strings.TrimSpace(pin)); err != nil { t.Fatal(err) } } func TestVerifyRendererMissingTree(t *testing.T) { dir := t.TempDir() _, pin := writeVerifyTree(t, dir) for _, p := range []string{filepath.Join(dir, "absent"), filepath.Join(dir, "absent", "tree")} { err := VerifyRenderer(p, pin) if !errors.Is(err, ErrRendererMissing) || RendererHealth(err) != wire.ToolMissing { t.Errorf("%s: %v", p, err) } } // A path that is not absolute and clean is a caller's mistake, not // a missing tree. for _, p := range []string{"tree", dir + "/./tree", dir + "/tree/"} { if err := VerifyRenderer(p, pin); !errors.Is(err, ErrRendererMismatch) { t.Errorf("%q: %v", p, err) } } } // Every way the installed tree can differ from the pinned one is a // mismatch, named by the entry that differs ("" is the tree's root). func TestVerifyRendererRefuses(t *testing.T) { const browser = "usr/lib/chromium/chromium-headless-shell" const link = "usr/bin/chromium-headless-shell" for _, c := range []struct { name string path string // the entry the error names why string // and what it says of it edit func(t *testing.T, root, outside string) uid int // when set, verify as this user instead }{ {name: "tampered byte", why: "has SHA-256", path: browser, edit: func(t *testing.T, root, _ string) { writeTreeFile(t, filepath.Join(root, browser), "Browser", 0o755) }}, {name: "extra file", why: "not in MANIFEST", path: "usr/lib/chromium/extra", edit: func(t *testing.T, root, _ string) { writeTreeFile(t, filepath.Join(root, "usr/lib/chromium/extra"), "x", 0o644) }}, {name: "extra file at the root", why: "not in MANIFEST", path: "extra", edit: func(t *testing.T, root, _ string) { writeTreeFile(t, filepath.Join(root, "extra"), "x", 0o644) }}, {name: "extra directory", why: "not in MANIFEST", path: "usr/lib/extra", edit: func(t *testing.T, root, _ string) { treeMust(t, os.Mkdir(filepath.Join(root, "usr/lib/extra"), 0o755)) }}, {name: "extra fifo", why: "not in MANIFEST", path: "usr/fifo", edit: func(t *testing.T, root, _ string) { treeMust(t, syscall.Mkfifo(filepath.Join(root, "usr/fifo"), 0o644)) }}, {name: "missing file", why: "not in the tree", path: "usr/share/fonts/f.conf", edit: func(t *testing.T, root, _ string) { treeMust(t, os.Remove(filepath.Join(root, "usr/share/fonts/f.conf"))) }}, {name: "missing directory", why: "not in the tree", path: "usr/share", edit: func(t *testing.T, root, _ string) { treeMust(t, os.RemoveAll(filepath.Join(root, "usr/share"))) }}, {name: "missing link", why: "not in the tree", path: link, edit: func(t *testing.T, root, _ string) { treeMust(t, os.Remove(filepath.Join(root, link))) }}, {name: "link retargeted", why: "points at", path: link, edit: func(t *testing.T, root, _ string) { treeMust(t, os.Remove(filepath.Join(root, link))) treeMust(t, os.Symlink("../lib/chromium/./chromium-headless-shell", filepath.Join(root, link))) }}, {name: "mode widened", why: "group or world writable", path: "usr/lib-extra", edit: func(t *testing.T, root, _ string) { treeMust(t, os.Chmod(filepath.Join(root, "usr/lib-extra"), 0o664)) }}, {name: "mode changed", why: "has mode 0700", path: browser, edit: func(t *testing.T, root, _ string) { treeMust(t, os.Chmod(filepath.Join(root, browser), 0o700)) }}, {name: "setuid added", why: "has mode 4755", path: browser, edit: func(t *testing.T, root, _ string) { treeMust(t, os.Chmod(filepath.Join(root, browser), 0o755|os.ModeSetuid)) }}, {name: "directory group writable", why: "group or world writable", path: "usr/lib", edit: func(t *testing.T, root, _ string) { treeMust(t, os.Chmod(filepath.Join(root, "usr/lib"), 0o775)) }}, {name: "directory world writable and sticky", why: "group or world writable", path: "usr/share/fonts", edit: func(t *testing.T, root, _ string) { treeMust(t, os.Chmod(filepath.Join(root, "usr/share/fonts"), 0o777|os.ModeSticky)) }}, {name: "root group writable", why: "group or world writable", path: "", edit: func(t *testing.T, root, _ string) { treeMust(t, os.Chmod(root, 0o775)) }}, {name: "MANIFEST group writable", why: "group or world writable", path: "MANIFEST", edit: func(t *testing.T, root, _ string) { treeMust(t, os.Chmod(filepath.Join(root, "MANIFEST"), 0o664)) }}, {name: "MANIFEST missing", why: "cannot be checked", path: "MANIFEST", edit: func(t *testing.T, root, _ string) { treeMust(t, os.Remove(filepath.Join(root, "MANIFEST"))) }}, {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) { treeMust(t, os.Rename(filepath.Join(root, "MANIFEST"), filepath.Join(outside, "MANIFEST"))) treeMust(t, os.Symlink(filepath.Join(outside, "MANIFEST"), filepath.Join(root, "MANIFEST"))) }}, {name: "MANIFEST a fifo", why: "is a special file; the list must be a file", path: "MANIFEST", edit: func(t *testing.T, root, _ string) { treeMust(t, os.Remove(filepath.Join(root, "MANIFEST"))) treeMust(t, syscall.Mkfifo(filepath.Join(root, "MANIFEST"), 0o644)) }}, {name: "MANIFEST swapped with a matching tree", why: "the pin is", path: "MANIFEST", edit: func(t *testing.T, root, _ string) { writeTreeFile(t, filepath.Join(root, browser), "another browser", 0o755) writeVerifyManifest(t, root) }}, {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) { copyDir := filepath.Join(outside, "chromium") treeMust(t, os.Rename(filepath.Join(root, "usr/lib/chromium"), copyDir)) treeMust(t, os.Symlink(copyDir, filepath.Join(root, "usr/lib/chromium"))) }}, {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) { treeMust(t, os.Rename(filepath.Join(root, browser), filepath.Join(outside, "browser"))) treeMust(t, os.Symlink(filepath.Join(outside, "browser"), filepath.Join(root, browser))) }}, {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) { treeMust(t, os.Remove(filepath.Join(root, link))) writeTreeFile(t, filepath.Join(root, link), "browser", 0o755) }}, {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) { treeMust(t, os.Remove(filepath.Join(root, "usr/lib-extra"))) treeMust(t, os.Mkdir(filepath.Join(root, "usr/lib-extra"), 0o755)) }}, {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) { treeMust(t, os.RemoveAll(filepath.Join(root, "usr/share/fonts"))) writeTreeFile(t, filepath.Join(root, "usr/share/fonts"), "", 0o644) }}, {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) { treeMust(t, os.Remove(filepath.Join(root, "usr/lib-extra"))) treeMust(t, syscall.Mkfifo(filepath.Join(root, "usr/lib-extra"), 0o644)) }}, {name: "file hard linked from outside", why: "has 2 names", path: browser, edit: func(t *testing.T, root, outside string) { treeMust(t, os.Link(filepath.Join(root, browser), filepath.Join(outside, "browser"))) }}, {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) { treeMust(t, os.Rename(root, filepath.Join(outside, "tree"))) treeMust(t, os.Symlink(filepath.Join(outside, "tree"), root)) }}, {name: "owned by another user", why: "is owned by user", path: "", uid: os.Geteuid() + 1}, } { t.Run(c.name, func(t *testing.T) { if c.uid != 0 && os.Geteuid() == 0 { t.Skip("root owns the tree; any owner check passes for root") } dir := t.TempDir() root, pin := writeVerifyTree(t, dir) outside := filepath.Join(dir, "outside") treeMust(t, os.Mkdir(outside, 0o755)) if c.edit != nil { c.edit(t, root, outside) } var err error if c.uid != 0 { err = verifyRendererTree(root, pin, uint32(c.uid)) } else { err = VerifyRenderer(root, pin) } var re *RendererError if !errors.As(err, &re) || !errors.Is(err, ErrRendererMismatch) || RendererHealth(err) != wire.ToolMismatch { t.Fatalf("want a mismatch, got %v", err) } if re.Path != c.path || !strings.Contains(re.Reason, c.why) { t.Fatalf("got %v, want %q: ...%s", err, c.path, c.why) } }) } } // A MANIFEST that matches its pin is still read strictly: a pinned list // the build could not have written (here, a link out of the tree) is a // mismatch, as is one too large to be the build's. func TestVerifyRendererRefusesAPinnedBadManifest(t *testing.T) { root, _ := writeVerifyTree(t, t.TempDir()) treeMust(t, os.Symlink("../../outside", filepath.Join(root, "usr/out"))) err := VerifyRenderer(root, writeVerifyManifest(t, root)) var re *RendererError if !errors.As(err, &re) || re.Health != wire.ToolMismatch || re.Path != "MANIFEST" || !strings.Contains(re.Reason, "climbs out of the tree") { t.Fatalf("escaping link: %v", err) } // Linux refuses f/ and f/. (ENOTDIR) when f is a file, directly or // through another link; so does the verifier, even when pinned. for _, links := range [][][2]string{ {{"usr/bad", "lib-extra/"}}, {{"usr/bad", "lib-extra/."}}, {{"usr/bad", "via/"}, {"usr/via", "lib-extra"}}, {{"usr/bad", "via"}, {"usr/via", "lib-extra/."}}, } { root, _ := writeVerifyTree(t, t.TempDir()) for _, l := range links { treeMust(t, os.Symlink(l[1], filepath.Join(root, l[0]))) } err := VerifyRenderer(root, writeVerifyManifest(t, root)) if !errors.As(err, &re) || re.Path != "MANIFEST" || !strings.Contains(re.Reason, "is not a directory") { t.Fatalf("%v: %v", links, err) } } big := make([]byte, rendererManifestMax+1) for i := range big { big[i] = '\n' } writeTreeFile(t, filepath.Join(root, "MANIFEST"), string(big), 0o644) sum := sha256.Sum256(big) err = VerifyRenderer(root, hex.EncodeToString(sum[:])) if !errors.As(err, &re) || re.Health != wire.ToolMismatch || re.Path != "MANIFEST" || !strings.Contains(re.Reason, "larger than") { t.Fatalf("oversized MANIFEST: %v", err) } } func writeTreeFile(t *testing.T, p, body string, mode os.FileMode) { t.Helper() treeMust(t, os.WriteFile(p, []byte(body), 0o600)) treeMust(t, os.Chmod(p, mode)) } func treeMust(t *testing.T, err error) { t.Helper() if err != nil { t.Fatal(err) } }