carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package rootstate
2
3import (
4 "errors"
5 "os"
6 "path/filepath"
7 "testing"
8
9 "golang.org/x/sys/unix"
10)
11
12func TestPlan14ProofBindingFileProvenance(t *testing.T) {
13 t.Run("root-owned regular file", func(t *testing.T) {
14 for _, path := range []string{"/", "/usr", "/usr/bin", "/usr/bin/true"} {
15 var st unix.Stat_t
16 if err := unix.Lstat(path, &st); err != nil {
17 t.Fatal(err)
18 }
19 if st.Uid != 0 {
20 t.Skipf("root-owned fixture unavailable: %s uid=%d", path, st.Uid)
21 }
22 }
23 f, err := OpenTrustedFile("/usr/bin/true")
24 if err != nil {
25 t.Fatal(err)
26 }
27 f.Close()
28 })
29 path := filepath.Join(t.TempDir(), "unit.service")
30 if err := os.WriteFile(path, []byte("[Service]\n"), 0644); err != nil {
31 t.Fatal(err)
32 }
33 if f, err := OpenTrustedFile(path); err == nil {
34 f.Close()
35 t.Fatal("accepted a unit under a writable/nonroot ancestor")
36 }
37 for _, path := range []string{"relative", "/etc/../etc/passwd", "/"} {
38 if _, err := OpenTrustedFile(path); !errors.Is(err, os.ErrPermission) {
39 t.Fatalf("%q: %v", path, err)
40 }
41 }
42}
43