demobuild: chmod the build cache through an fd, not by path
The systemd-family build made the cache world-writable with os.Chmod on each path the walk listed, as root, on a tree the previous build's tenant wrote: a directory swapped for a link after the listing took the chmod to wherever the link pointed. chmodCacheDirs now opens each directory with instancefs.OpenDirBeneath (no link at any component) and chmods the descriptor, as ownBuildDirectories does for gVisor. TestCacheDirsChmodThroughFD swaps a directory for a link at a seam.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+114
−12
internal/demobuild/unit.go+39 −12internal/demobuild/unit_cache_chmod_test.go+75 −0
diff --git a/internal/demobuild/unit.go b/internal/demobuild/unit.go| index 55705913a..ebecd28d9 100644 |
| --- a/internal/demobuild/unit.go |
| +++ b/internal/demobuild/unit.go |
| @@ -225,18 +225,7 @@ func (w *Worker) prepareUnit(ctx context.Context, dir string, src Source) (clean |
| if err != nil { |
| return cleanup, err |
| } |
| - } else if err = filepath.WalkDir(l.cache, func(p string, de fs.DirEntry, e error) error { |
| - if e != nil { |
| - return e |
| - } |
| - if de.Type()&os.ModeSymlink != 0 { |
| - return ErrBuild |
| - } |
| - if de.IsDir() { |
| - return os.Chmod(p, 0777) |
| - } |
| - return nil |
| - }); err != nil { |
| + } else if err = chmodCacheDirs(l.cache, 0777); err != nil { |
| return cleanup, err |
| } |
| step = "open-unit-root" |
| @@ -707,3 +696,41 @@ func buildFailure(step string, err error) error { |
| log.Printf("demo-build: %s: %v", step, err) |
| return ErrBuild |
| } |
| + |
| +// beforeCacheChmod is a test seam: where a directory swapped for a link |
| +// after the walk listed it would race a chmod by path. |
| +var beforeCacheChmod = func(path string) {} |
| + |
| +// chmodCacheDirs sets mode on every directory of the build cache. The walk |
| +// refuses a link it lists, as before, but a listing is not the chmod: the |
| +// previous build's tenant wrote this tree, and a directory it swaps for a |
| +// link after the listing would take a chmod by path, as root, to wherever |
| +// the link points. So each directory is opened beneath the cache with no |
| +// link at any component (instancefs.OpenDirBeneath, as ownBuildDirectories |
| +// does for the gVisor family) and chmodded through that descriptor. Linux |
| +// only, like the systemd family that calls it; elsewhere OpenDirBeneath |
| +// answers ErrUnsupported. |
| +func chmodCacheDirs(cache string, mode os.FileMode) error { |
| + return filepath.WalkDir(cache, func(p string, de fs.DirEntry, e error) error { |
| + if e != nil { |
| + return e |
| + } |
| + if de.Type()&os.ModeSymlink != 0 { |
| + return ErrBuild |
| + } |
| + if !de.IsDir() { |
| + return nil |
| + } |
| + rel, err := filepath.Rel(cache, p) |
| + if err != nil { |
| + return err |
| + } |
| + beforeCacheChmod(p) |
| + f, err := instancefs.OpenDirBeneath(cache, rel) |
| + if err != nil { |
| + return err |
| + } |
| + defer f.Close() |
| + return f.Chmod(mode) |
| + }) |
| +} |
diff --git a/internal/demobuild/unit_cache_chmod_test.go b/internal/demobuild/unit_cache_chmod_test.go| new file mode 100644 |
| index 000000000..66213905a |
| --- /dev/null |
| +++ b/internal/demobuild/unit_cache_chmod_test.go |
| @@ -0,0 +1,75 @@ |
| +//go:build linux |
| + |
| +package demobuild |
| + |
| +import ( |
| + "os" |
| + "path/filepath" |
| + "testing" |
| +) |
| + |
| +// The systemd-family build cache is made world-writable directory by |
| +// directory, as root, on a tree the previous build's tenant wrote. A |
| +// directory swapped for a link to somewhere else between the walk listing |
| +// it and the chmod must not hand that somewhere else mode 0777: the chmod |
| +// goes through a descriptor opened beneath the cache with no link at any |
| +// component, so the swap is refused, and ordinary directories still end |
| +// 0777. |
| +func TestCacheDirsChmodThroughFD(t *testing.T) { |
| + root := t.TempDir() |
| + cache := filepath.Join(root, "cache") |
| + outside := filepath.Join(root, "outside") |
| + for _, d := range []string{filepath.Join(cache, "a", "deep"), filepath.Join(cache, "swap"), outside} { |
| + if err := os.MkdirAll(d, 0700); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := os.WriteFile(filepath.Join(cache, "a", "file"), []byte("x"), 0600); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + if err := chmodCacheDirs(cache, 0777); err != nil { |
| + t.Fatalf("ordinary tree: %v", err) |
| + } |
| + for _, d := range []string{cache, filepath.Join(cache, "a"), filepath.Join(cache, "a", "deep"), filepath.Join(cache, "swap")} { |
| + if m := cachePerm(t, d); m != 0777 { |
| + t.Errorf("%s is %v, want 0777", d, m) |
| + } |
| + } |
| + if m := cachePerm(t, filepath.Join(cache, "a", "file")); m != 0600 { |
| + t.Errorf("a file was chmodded: %v", m) |
| + } |
| + |
| + if err := os.Chmod(filepath.Join(cache, "swap"), 0700); err != nil { |
| + t.Fatal(err) |
| + } |
| + swap := filepath.Join(cache, "swap") |
| + prev := beforeCacheChmod |
| + t.Cleanup(func() { beforeCacheChmod = prev }) |
| + beforeCacheChmod = func(p string) { |
| + if p != swap { |
| + return |
| + } |
| + if err := os.Remove(swap); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Symlink(outside, swap); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := chmodCacheDirs(cache, 0777); err == nil { |
| + t.Error("a directory swapped for a link was chmodded without an error") |
| + } |
| + if m := cachePerm(t, outside); m != 0700 { |
| + t.Errorf("the link's target is %v after the walk, want 0700: the chmod followed the swapped-in link", m) |
| + } |
| +} |
| + |
| +func cachePerm(t *testing.T, p string) os.FileMode { |
| + t.Helper() |
| + fi, err := os.Lstat(p) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return fi.Mode().Perm() |
| +} |