rootstate: split the trusted file read into a dir walk and a file open
OpenTrustedDir is OpenTrustedFile's root-trusted walk on its own, and OpenTrustedFileIn opens one name in the walked directory with the same file checks. A caller can now tell an untrusted directory chain from an untrusted file. Existing openers are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+107
−0
internal/rootstate/file_linux.go+33 −0internal/rootstate/file_linux_test.go+70 −0internal/rootstate/file_other.go+4 −0
diff --git a/internal/rootstate/file_linux.go b/internal/rootstate/file_linux.go| index 34a2db4ac..20d9bfebc 100644 |
| --- a/internal/rootstate/file_linux.go |
| +++ b/internal/rootstate/file_linux.go |
| @@ -30,6 +30,39 @@ func OpenTrustedFile(path string) (*os.File, error) { |
| return f, nil |
| } |
| +// OpenTrustedDir is OpenTrustedFile's directory walk on its own: Open's |
| +// public walk from / with every component root-owned, writable by neither |
| +// group nor others, and r-x for both. The returned descriptor is path's, |
| +// for OpenTrustedFileIn. A caller that must tell an untrusted chain |
| +// (os.ErrPermission) from an untrusted file walks first with this. |
| +func OpenTrustedDir(path string) (*os.File, error) { |
| + s, err := Open(path, false, true) |
| + if err != nil { |
| + return nil, err |
| + } |
| + return s.(*directory).file, nil |
| +} |
| + |
| +// OpenTrustedFileIn opens name in dir, a descriptor OpenTrustedDir or |
| +// OpenTrustedDirFrom returned, with OpenTrustedFile's checks on the file: |
| +// no symlink, a regular file with one link owned by dir's owner (root for |
| +// OpenTrustedDir) and writable by neither group nor others. |
| +func OpenTrustedFileIn(dir *os.File, name string) (*os.File, error) { |
| + var st unix.Stat_t |
| + if err := unix.Fstat(int(dir.Fd()), &st); err != nil { |
| + return nil, err |
| + } |
| + f, err := safefile.OpenFileAt(int(dir.Fd()), name, os.O_RDONLY, 0) |
| + if err != nil { |
| + return nil, err |
| + } |
| + if err := trusted(int(f.Fd()), st.Uid, unix.S_IFREG); err != nil { |
| + f.Close() |
| + return nil, err |
| + } |
| + return f, nil |
| +} |
| + |
| // OpenTrustedDirFrom is Open's walk from anchor to dir, trusting the |
| // ANCHOR's owner: every component below the anchor is opened by directory |
| // descriptor with O_NOFOLLOW, and each, the anchor included, must be a |
diff --git a/internal/rootstate/file_linux_test.go b/internal/rootstate/file_linux_test.go| index c1e0ea532..95f39e8d2 100644 |
| --- a/internal/rootstate/file_linux_test.go |
| +++ b/internal/rootstate/file_linux_test.go |
| @@ -40,3 +40,73 @@ func TestPlan14ProofBindingFileProvenance(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// OpenTrustedDir and OpenTrustedFileIn split OpenTrustedFile's read so a |
| +// caller can tell an untrusted directory chain from an untrusted file. |
| +func TestOpenTrustedDirThenFileIn(t *testing.T) { |
| + t.Run("root-owned chain", func(t *testing.T) { |
| + for _, path := range []string{"/", "/usr", "/usr/bin", "/usr/bin/true"} { |
| + var st unix.Stat_t |
| + if err := unix.Lstat(path, &st); err != nil { |
| + t.Fatal(err) |
| + } |
| + if st.Uid != 0 { |
| + t.Skipf("root-owned fixture unavailable: %s uid=%d", path, st.Uid) |
| + } |
| + } |
| + d, err := OpenTrustedDir("/usr/bin") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer d.Close() |
| + f, err := OpenTrustedFileIn(d, "true") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + f.Close() |
| + }) |
| + if os.Geteuid() != 0 { |
| + if _, err := OpenTrustedDir(t.TempDir()); !errors.Is(err, os.ErrPermission) { |
| + t.Fatalf("a non-root chain: %v, want os.ErrPermission", err) |
| + } |
| + } |
| + anchor := t.TempDir() |
| + if err := os.Chmod(anchor, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + body := filepath.Join(anchor, "body") |
| + if err := os.WriteFile(body, []byte("x"), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Chmod(body, 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(filepath.Join(anchor, "loose"), []byte("x"), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Chmod(filepath.Join(anchor, "loose"), 0o664); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Symlink(body, filepath.Join(anchor, "link")); err != nil { |
| + t.Fatal(err) |
| + } |
| + d, err := OpenTrustedDirFrom(anchor, anchor) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer d.Close() |
| + f, err := OpenTrustedFileIn(d, "body") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + f.Close() |
| + if _, err := OpenTrustedFileIn(d, "absent"); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("absent: %v, want os.ErrNotExist", err) |
| + } |
| + for _, name := range []string{"loose", "link", "../body", "."} { |
| + if f, err := OpenTrustedFileIn(d, name); err == nil { |
| + f.Close() |
| + t.Fatalf("%s: opened an untrusted file", name) |
| + } |
| + } |
| +} |
diff --git a/internal/rootstate/file_other.go b/internal/rootstate/file_other.go| index 326be62a1..0aea6033b 100644 |
| --- a/internal/rootstate/file_other.go |
| +++ b/internal/rootstate/file_other.go |
| @@ -6,6 +6,10 @@ import "os" |
| func OpenTrustedFile(string) (*os.File, error) { return nil, os.ErrPermission } |
| +func OpenTrustedDir(string) (*os.File, error) { return nil, os.ErrPermission } |
| + |
| +func OpenTrustedFileIn(*os.File, string) (*os.File, error) { return nil, os.ErrPermission } |
| + |
| func OpenTrustedDirFrom(string, string) (*os.File, error) { return nil, os.ErrPermission } |
| func OpenTrustedFileFrom(string, string) (*os.File, error) { return nil, os.ErrPermission } |