claim-empty-cohort-done fix round 2: the floor repair checks every ancestor by descriptor before it makes or changes anything
Astra's re-review (MEDIUM): repairRootFloorDir made a missing guard/moves or guard/seeds with the pathname mkdirAllExact, which follows an ancestor symlink, before its O_NOFOLLOW walk, and the walk checked the owner of the final directory only. A refused migration could already have made or chmodded a directory through a symlink or under an untrusted ancestor. As the controller ruled, consoleDir's descriptor walk is factored out unchanged into walkNoFollowDir (consoleDir keeps its checks and its behaviour), and the floor repair uses it: every directory from / to guard/ must be a real directory, owned by root and not writable by group or others, before anything is made. Only then is a missing floor directory made by Mkdirat beneath the checked guard/, opened with O_NOFOLLOW, checked root:root and Fchmodded to 0755. mkdirAllExact is gone from this path and stays for guard-arm's own directories. No new human-facing string. Tests: unprivileged ancestry cases (symlinked guard or system with the floor directory missing, guard missing, group- or world-writable ancestors, other-owned ancestors), each refusing with its own error and both trees unchanged; the refusal tests now walk from a 0755 base so each case refuses for its own reason; walkNoFollowDir's callers pinned under ruling 6. Root runner: TestRootFloorDirsUntrustedAncestry (new), the four rerun witnesses and consoleDir's install-console tests. Census: overlay plan14-root-reads-claim-empty-cohort-fix2.tsv (PROPOSED), "Fix round 2" in the step 8 census proposal.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
10 files changed,
+456
−87
docs/superpowers/notes/census-proposal-claim-step8-readiness.md+20 −0internal/host/activation_artifacts_linux.go+5 −33internal/host/claimstep8_ruling6_test.go+1 −0internal/host/claimstep8_witness_rootrunner_linux_test.go+5 −4internal/host/nofollowdir_linux.go+55 −0internal/host/rootmigration_floordirs_linux_test.go+177 −13internal/host/rootmigration_floordirs_rootrunner_linux_test.go+135 −0internal/host/rootmigration_production_linux.go+43 −37internal/provision/plan14_root_reads_test.go+9 −0internal/provision/testdata/plan14-root-reads-claim-empty-cohort-fix2.tsv+6 −0
diff --git a/docs/superpowers/notes/census-proposal-claim-step8-readiness.md b/docs/superpowers/notes/census-proposal-claim-step8-readiness.md| index 124b76728..e89d99a07 100644 |
| --- a/docs/superpowers/notes/census-proposal-claim-step8-readiness.md |
| +++ b/docs/superpowers/notes/census-proposal-claim-step8-readiness.md |
| @@ -82,3 +82,23 @@ Ruling 6: `prepareRootFloorDirs` is still called only from `migrateRootClaimAuth |
| Envelopes re-pinned: `internal/host`, `internal/hostops`. The new `doneRecords` and `prepareRootFloorDirsOwned` have no read site of their own, so they are envelope only. |
| 9B8: `internal/hostops/backfill_publish.go` `Backfill.Done`, still nonrecord (re-reviewed). The new listings only read. Its one Put, of the backfill manifest under the collecting etag, is unchanged. |
| + |
| +## Fix round 2 |
| + |
| +Status: PROPOSED. The ruling is owed. Astra's re-review of `claim-empty-cohort-done` found F1 resolved. It found one new MEDIUM in F2's repair: `repairRootFloorDir` made a missing floor directory with the pathname `mkdirAllExact`, which follows an ancestor symlink, before its `O_NOFOLLOW` walk, and that walk checked the owner only of the final directory. The controller ruled a descriptor walk shared with `consoleDir`. The re-pins are in a new overlay, `internal/provision/testdata/plan14-root-reads-claim-empty-cohort-fix2.tsv`, loaded last and marked `rev3=PROPOSED-claim-empty-cohort-fix2`. No accepted-legacy row is added, so the seal stands. |
| + |
| +- **The shared walk.** `walkNoFollowDir` (new, `internal/host/nofollowdir_linux.go`) is `consoleDir`'s walk moved out unchanged. It opens the caller's base, `/` in production, then each name beneath it with `O_NOFOLLOW|O_DIRECTORY`. The caller's check sees every directory before anything beneath it is opened or made. A missing name is made, only where the caller allows it, by `Mkdirat` then `Fchmodat` to 0755 beneath the checked parent, and the parent is fsynced. The name is then opened again with `O_NOFOLLOW`. |
| +- **`consoleDir`** keeps its own check (root-owned, not group or other writable, other-searchable; the last exactly 0755 root:root). It allows creation at every component when asked, as before, so its behaviour is the same. Its row loses the open and Openat sites to `walkNoFollowDir` and keeps its class. |
| +- **`repairRootFloorDir`** has no pathname `Lstat` or `mkdirAllExact` now. It walks from `/` with `walkNoFollowDir`, and every directory from `/` to `guard/` must be a real directory owned by root and not writable by group or others before anything is made. Only then is a missing `guard/moves` or `guard/seeds` made by `Mkdirat` beneath the checked `guard/`. It is opened with `O_NOFOLLOW`, must be a directory owned by root:root, and is Fchmodded to 0755. A symlink or a missing directory on the way, an untrusted ancestor, or a floor directory that is a symlink, a file or another owner's refuses with nothing made or changed. `mkdirAllExact` stays for guard-arm's own directories. |
| + |
| +Ruling 6: `prepareRootFloorDirs` is still called only from `migrateRootClaimAuthority`. `TestStep8CensusChangesReachOnlyClaimMode` also pins `walkNoFollowDir` to `consoleDir` and `repairRootFloorDir`. A claims-off or no-census box sees no change. |
| + |
| +| Unit | Change | Class | Executing identity | |
| +|---|---|---|---| |
| +| `internal/host/activation_artifacts_linux.go` `consoleDir` | re-pinned: its walk moved unchanged into `walkNoFollowDir`; its checks and its Fstat stay | authority G,J4 (unchanged) | `claims install-console`, explicit box installer, uid 0 | |
| +| `internal/host/nofollowdir_linux.go` `walkNoFollowDir` | new: `consoleDir`'s walk, shared | authority G,J4 (its callers' class) | uid 0 callers only: `consoleDir` and `repairRootFloorDir` | |
| +| `internal/host/rootmigration_production_linux.go` `repairRootFloorDir` | re-pinned: every ancestor checked through `walkNoFollowDir` before `Mkdirat` of the leaf, then `O_NOFOLLOW`, Fstat, Fchmod; no `Lstat`, no `mkdirAllExact` | authority G,J4 (unchanged) | `carlos system journal-migrate`, offline uid 0 | |
| + |
| +Envelope re-pinned: `internal/host`. `prepareRootFloorDirsOwned` gains the walk's base as a parameter and has no read site of its own. |
| + |
| +9B8: no record write changes. |
diff --git a/internal/host/activation_artifacts_linux.go b/internal/host/activation_artifacts_linux.go| index 2708a9d11..bf6e63f0f 100644 |
| --- a/internal/host/activation_artifacts_linux.go |
| +++ b/internal/host/activation_artifacts_linux.go |
| @@ -386,14 +386,12 @@ func readConsoleSource(source, sha string) ([]byte, error) { |
| // symlink. Every component must be root-owned, not writable by group or |
| // others and searchable by others; missing ones are created 0755 when |
| // create is set, and the parent is synced. The last must be exactly 0755. |
| +// The walk is walkNoFollowDir's, shared with the root migration's floor |
| +// repair. |
| func consoleDir(dir string, create bool) (*os.File, error) { |
| if os.Geteuid() != 0 || !filepath.IsAbs(dir) || filepath.Clean(dir) != dir || dir == "/" { |
| return nil, errActivation |
| } |
| - fd, err := unix.Open("/", unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0) |
| - if err != nil { |
| - return nil, err |
| - } |
| check := func(fd int, last bool) error { |
| var st unix.Stat_t |
| if err := unix.Fstat(fd, &st); err != nil { |
| @@ -404,35 +402,9 @@ func consoleDir(dir string, create bool) (*os.File, error) { |
| } |
| return nil |
| } |
| - names := strings.Split(strings.TrimPrefix(dir, "/"), "/") |
| - for i, name := range names { |
| - if err = check(fd, false); err != nil { |
| - unix.Close(fd) |
| - return nil, err |
| - } |
| - next, e := unix.Openat(fd, name, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) |
| - if e == unix.ENOENT && create { |
| - e = unix.Mkdirat(fd, name, 0o755) |
| - if e == nil { |
| - e = unix.Fchmodat(fd, name, 0o755, 0) |
| - } |
| - if e == nil || e == unix.EEXIST { |
| - if e = unix.Fsync(fd); e == nil { |
| - next, e = unix.Openat(fd, name, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) |
| - } |
| - } |
| - } |
| - unix.Close(fd) |
| - if e != nil { |
| - return nil, e |
| - } |
| - fd = next |
| - if i == len(names)-1 { |
| - if err = check(fd, true); err != nil { |
| - unix.Close(fd) |
| - return nil, err |
| - } |
| - } |
| + fd, err := walkNoFollowDir("/", strings.Split(strings.TrimPrefix(dir, "/"), "/"), func(bool) bool { return create }, check) |
| + if err != nil { |
| + return nil, err |
| } |
| return os.NewFile(uintptr(fd), dir), nil |
| } |
diff --git a/internal/host/claimstep8_ruling6_test.go b/internal/host/claimstep8_ruling6_test.go| index 52112f9a1..67b6bb60f 100644 |
| --- a/internal/host/claimstep8_ruling6_test.go |
| +++ b/internal/host/claimstep8_ruling6_test.go |
| @@ -70,6 +70,7 @@ func TestStep8CensusChangesReachOnlyClaimMode(t *testing.T) { |
| "prepareRootFloorDirs": {"migrateRootClaimAuthority"}, |
| "prepareRootFloorDirsOwned": {"prepareRootFloorDirs"}, |
| "repairRootFloorDir": {"prepareRootFloorDirsOwned"}, |
| + "walkNoFollowDir": {"consoleDir", "repairRootFloorDir"}, |
| "claimBoxDataCensus": {"claimDataCensus"}, |
| "claimDataCensus": {"claimGlobalSharedPaths"}, |
| "claimGlobalSharedPaths": {"OpenProductionClaimAssembly", "claimMigrationSharedPaths", "recoverRootInventoryStartup"}, |
diff --git a/internal/host/claimstep8_witness_rootrunner_linux_test.go b/internal/host/claimstep8_witness_rootrunner_linux_test.go| index 70ac5e4d2..fd94a977d 100644 |
| --- a/internal/host/claimstep8_witness_rootrunner_linux_test.go |
| +++ b/internal/host/claimstep8_witness_rootrunner_linux_test.go |
| @@ -114,8 +114,8 @@ func TestRootStep8WindowWitnessEmptyLeoTwoHosts(t *testing.T) { |
| // |
| // - Moves0700: guard/moves (guard-arm's floor history) left 0700; |
| // - Seeds0700: guard/seeds left 0700; |
| -// - Interrupted: guard/seeds made by mkdirAllExact's mkdir under umask |
| -// 077, its chmod never reached (0700, empty); |
| +// - Interrupted: guard/seeds made by an earlier build's mkdirAllExact |
| +// mkdir under umask 077, its chmod never reached (0700, empty); |
| // - Foreign: guard/seeds left 0700 but owned by the carlos user. The |
| // migration refuses it and leaves it as it was; once it is root's again |
| // the rerun repairs it and completes. |
| @@ -619,8 +619,9 @@ func step8EarlierAttempt(t *testing.T, opt, shape string, uid, gid int) { |
| t.Fatal(err) |
| } |
| case "interrupted": |
| - // mkdirAllExact's first half, os.MkdirAll(dir, 0755), under the |
| - // window shell's umask; the process died before its chmod. |
| + // An earlier build's mkdirAllExact, first half: os.MkdirAll(dir, |
| + // 0755) under the window shell's umask; the process died before |
| + // its chmod. |
| old := syscall.Umask(0o077) |
| err := os.MkdirAll(dir, 0o755) |
| syscall.Umask(old) |
diff --git a/internal/host/nofollowdir_linux.go b/internal/host/nofollowdir_linux.go| new file mode 100644 |
| index 000000000..14d69dcd8 |
| --- /dev/null |
| +++ b/internal/host/nofollowdir_linux.go |
| @@ -0,0 +1,55 @@ |
| +//go:build linux |
| + |
| +package host |
| + |
| +import "golang.org/x/sys/unix" |
| + |
| +// walkNoFollowDir is the one descriptor walk behind consoleDir and the root |
| +// migration's floor repair. It opens base, then each name beneath it in |
| +// turn with O_NOFOLLOW|O_DIRECTORY, so no symlink is followed below base. |
| +// check sees every directory on the way (last false) before anything |
| +// beneath it is opened or made, and the final directory (last true) once it |
| +// is open. A missing name is made only when create(last) allows it: Mkdirat |
| +// 0755 then Fchmodat 0755 beneath its checked parent, the parent fsynced, |
| +// and the name opened again with O_NOFOLLOW. It returns the checked final |
| +// directory's descriptor; errors are the system call's or check's own. |
| +func walkNoFollowDir(base string, names []string, create func(last bool) bool, check func(fd int, last bool) error) (int, error) { |
| + if len(names) == 0 { |
| + return -1, unix.EINVAL |
| + } |
| + fd, err := unix.Open(base, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0) |
| + if err != nil { |
| + return -1, err |
| + } |
| + for i, name := range names { |
| + last := i == len(names)-1 |
| + if err = check(fd, false); err != nil { |
| + unix.Close(fd) |
| + return -1, err |
| + } |
| + next, e := unix.Openat(fd, name, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) |
| + if e == unix.ENOENT && create(last) { |
| + e = unix.Mkdirat(fd, name, 0o755) |
| + if e == nil { |
| + e = unix.Fchmodat(fd, name, 0o755, 0) |
| + } |
| + if e == nil || e == unix.EEXIST { |
| + if e = unix.Fsync(fd); e == nil { |
| + next, e = unix.Openat(fd, name, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) |
| + } |
| + } |
| + } |
| + unix.Close(fd) |
| + if e != nil { |
| + return -1, e |
| + } |
| + fd = next |
| + if last { |
| + if err = check(fd, true); err != nil { |
| + unix.Close(fd) |
| + return -1, err |
| + } |
| + } |
| + } |
| + return fd, nil |
| +} |
diff --git a/internal/host/rootmigration_floordirs_linux_test.go b/internal/host/rootmigration_floordirs_linux_test.go| index 0608798bd..a848aa495 100644 |
| --- a/internal/host/rootmigration_floordirs_linux_test.go |
| +++ b/internal/host/rootmigration_floordirs_linux_test.go |
| @@ -5,15 +5,23 @@ package host |
| import ( |
| "context" |
| "errors" |
| + "fmt" |
| + "io/fs" |
| + "maps" |
| "os" |
| "path/filepath" |
| "slices" |
| "syscall" |
| "testing" |
| + |
| + "golang.org/x/sys/unix" |
| ) |
| // floorDirsOpt is an opt directory with system/ and system/guard at 0755, |
| -// as guard-arm leaves them, reached with no symlink on the way. |
| +// as guard-arm leaves them, reached with no symlink on the way. The tests |
| +// walk from opt itself as the base, owned by the test's own uid and made |
| +// 0755 whatever the test's umask: the real ancestors of a scratch directory |
| +// are not the test's. |
| func floorDirsOpt(t *testing.T) string { |
| t.Helper() |
| opt, err := filepath.EvalSymlinks(t.TempDir()) |
| @@ -24,7 +32,7 @@ func floorDirsOpt(t *testing.T) string { |
| if err := os.MkdirAll(guard, 0o755); err != nil { |
| t.Fatal(err) |
| } |
| - for _, dir := range []string{filepath.Join(opt, "system"), guard} { |
| + for _, dir := range []string{opt, filepath.Join(opt, "system"), guard} { |
| if err := os.Chmod(dir, 0o755); err != nil { |
| t.Fatal(err) |
| } |
| @@ -43,8 +51,8 @@ func floorDirMode(t *testing.T, dir string) os.FileMode { |
| // journal-migrate runs from the window's umask 077 root shell. Both public |
| // floor directories come out 0755, whether they are missing, left 0700 by |
| -// an earlier attempt, or left by an interrupted mkdirAllExact (mkdir done |
| -// under the umask, chmod never reached). |
| +// an earlier attempt, or left by an earlier build's interrupted |
| +// mkdirAllExact (mkdir done under the umask, chmod never reached). |
| func TestRootFloorDirsIgnoreUmask(t *testing.T) { |
| old := syscall.Umask(0o077) |
| defer syscall.Umask(old) |
| @@ -53,7 +61,7 @@ func TestRootFloorDirsIgnoreUmask(t *testing.T) { |
| "missing": func(string) error { return nil }, |
| "moves 0700": func(opt string) error { return os.Mkdir(rootFloorDir(opt), 0o700) }, |
| "seeds 0700": func(opt string) error { return os.Mkdir(rootSeedsFloorDir(opt), 0o700) }, |
| - "interrupted": func(opt string) error { return os.MkdirAll(rootSeedsFloorDir(opt), 0o755) }, // mkdirAllExact's mkdir, under 077 |
| + "interrupted": func(opt string) error { return os.MkdirAll(rootSeedsFloorDir(opt), 0o755) }, // an earlier build's mkdirAllExact mkdir, under 077 |
| "both 0700": func(opt string) error { |
| return errors.Join(os.Mkdir(rootFloorDir(opt), 0o700), os.Mkdir(rootSeedsFloorDir(opt), 0o700)) |
| }, |
| @@ -63,7 +71,7 @@ func TestRootFloorDirsIgnoreUmask(t *testing.T) { |
| if err := earlier(opt); err != nil { |
| t.Fatal(err) |
| } |
| - if err := prepareRootFloorDirsOwned(opt, uid, gid); err != nil { |
| + if err := prepareRootFloorDirsOwned(opt, opt, uid, gid); err != nil { |
| t.Fatal(err) |
| } |
| for _, dir := range []string{rootFloorDir(opt), rootSeedsFloorDir(opt), systemGuardDir(opt)} { |
| @@ -83,6 +91,7 @@ func TestRootFloorDirsRefuseWhatTheyDoNotOwn(t *testing.T) { |
| for name, c := range map[string]struct { |
| setup func(t *testing.T, opt string) string // returns the path whose mode must not change |
| uid, gid uint32 |
| + want []error // the refusal is one of these, so it is this case's own |
| }{ |
| "symlink": {setup: func(t *testing.T, opt string) string { |
| target := filepath.Join(opt, "elsewhere") |
| @@ -93,7 +102,7 @@ func TestRootFloorDirsRefuseWhatTheyDoNotOwn(t *testing.T) { |
| t.Fatal(err) |
| } |
| return target |
| - }, uid: uid, gid: gid}, |
| + }, uid: uid, gid: gid, want: []error{unix.ELOOP, unix.ENOTDIR}}, |
| "symlink on the way": {setup: func(t *testing.T, opt string) string { |
| real := filepath.Join(opt, "realguard") |
| if err := os.Rename(systemGuardDir(opt), real); err != nil { |
| @@ -106,32 +115,36 @@ func TestRootFloorDirsRefuseWhatTheyDoNotOwn(t *testing.T) { |
| t.Fatal(err) |
| } |
| return filepath.Join(real, "moves") |
| - }, uid: uid, gid: gid}, |
| + }, uid: uid, gid: gid, want: []error{unix.ELOOP, unix.ENOTDIR}}, |
| "file": {setup: func(t *testing.T, opt string) string { |
| if err := os.WriteFile(rootFloorDir(opt), nil, 0o600); err != nil { |
| t.Fatal(err) |
| } |
| return rootFloorDir(opt) |
| - }, uid: uid, gid: gid}, |
| + }, uid: uid, gid: gid, want: []error{unix.ENOTDIR}}, |
| + // Unprivileged, every directory here is the test's, so another |
| + // owner refuses at the base, before the leaf; the leaf's own owner |
| + // check is the root runner's (the Foreign rerun witness). |
| "other owner": {setup: func(t *testing.T, opt string) string { |
| if err := os.Mkdir(rootFloorDir(opt), 0o700); err != nil { |
| t.Fatal(err) |
| } |
| return rootFloorDir(opt) |
| - }, uid: uid + 1, gid: gid}, |
| + }, uid: uid + 1, gid: gid, want: []error{os.ErrPermission}}, |
| "other group": {setup: func(t *testing.T, opt string) string { |
| if err := os.Mkdir(rootFloorDir(opt), 0o700); err != nil { |
| t.Fatal(err) |
| } |
| return rootFloorDir(opt) |
| - }, uid: uid, gid: gid + 1}, |
| + }, uid: uid, gid: gid + 1, want: []error{os.ErrPermission}}, |
| } { |
| t.Run(name, func(t *testing.T) { |
| opt := floorDirsOpt(t) |
| left := c.setup(t, opt) |
| before := floorDirMode(t, left) |
| - if err := prepareRootFloorDirsOwned(opt, c.uid, c.gid); err == nil { |
| - t.Fatal("repair accepted it") |
| + err := prepareRootFloorDirsOwned(opt, opt, c.uid, c.gid) |
| + if !slices.ContainsFunc(c.want, func(w error) bool { return errors.Is(err, w) }) { |
| + t.Fatalf("repair = %v, want a refusal with one of %v", err, c.want) |
| } |
| if after := floorDirMode(t, left); after != before { |
| t.Fatalf("%s changed from %v to %v", left, before, after) |
| @@ -154,6 +167,157 @@ func TestRootFloorDirsRefuseWhatTheyDoNotOwn(t *testing.T) { |
| } |
| } |
| +// floorTree records every path under roots, with its mode, owner and, for |
| +// a symlink, its target, so a refusal can be shown to have changed nothing. |
| +func floorTree(t *testing.T, roots ...string) map[string]string { |
| + t.Helper() |
| + out := map[string]string{} |
| + for _, root := range roots { |
| + err := filepath.WalkDir(root, func(path string, _ fs.DirEntry, err error) error { |
| + if err != nil { |
| + return err |
| + } |
| + st, err := os.Lstat(path) |
| + if err != nil { |
| + return err |
| + } |
| + sys := st.Sys().(*syscall.Stat_t) |
| + v := fmt.Sprintf("%v %d:%d", st.Mode(), sys.Uid, sys.Gid) |
| + if st.Mode()&os.ModeSymlink != 0 { |
| + target, err := os.Readlink(path) |
| + if err != nil { |
| + return err |
| + } |
| + v += " -> " + target |
| + } |
| + out[path] = v |
| + return nil |
| + }) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + return out |
| +} |
| + |
| +// Fix round 2: the repair checks every directory from its base to the |
| +// floor directory's parent before it makes or changes anything. A symlink |
| +// or a missing directory on the way, an ancestor writable by group or |
| +// others, or one with another owner refuses, whether the floor directory |
| +// is missing (nothing is made, in either tree) or left 0700 (it stays so). |
| +func TestRootFloorDirsCheckAncestryFirst(t *testing.T) { |
| + uid, gid := uint32(os.Getuid()), uint32(os.Getgid()) |
| + elsewhere := func(t *testing.T) string { |
| + t.Helper() |
| + dir, err := filepath.EvalSymlinks(t.TempDir()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return dir |
| + } |
| + moves0700 := func(t *testing.T, opt string) { |
| + t.Helper() |
| + if err := os.Mkdir(rootFloorDir(opt), 0o700); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + for name, c := range map[string]struct { |
| + // setup returns any tree outside opt that must not change either. |
| + setup func(t *testing.T, opt string) []string |
| + uid, gid uint32 |
| + want []error // the refusal is one of these, so it is this case's own |
| + }{ |
| + "symlinked guard, moves missing": {setup: func(t *testing.T, opt string) []string { |
| + target := elsewhere(t) |
| + if err := os.Remove(systemGuardDir(opt)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Symlink(target, systemGuardDir(opt)); err != nil { |
| + t.Fatal(err) |
| + } |
| + return []string{target} |
| + }, uid: uid, gid: gid, want: []error{unix.ELOOP, unix.ENOTDIR}}, |
| + "symlinked system, moves missing": {setup: func(t *testing.T, opt string) []string { |
| + target := elsewhere(t) |
| + if err := os.Mkdir(filepath.Join(target, "guard"), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.RemoveAll(filepath.Join(opt, "system")); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Symlink(target, filepath.Join(opt, "system")); err != nil { |
| + t.Fatal(err) |
| + } |
| + return []string{target} |
| + }, uid: uid, gid: gid, want: []error{unix.ELOOP, unix.ENOTDIR}}, |
| + "guard missing": {setup: func(t *testing.T, opt string) []string { |
| + if err := os.Remove(systemGuardDir(opt)); err != nil { |
| + t.Fatal(err) |
| + } |
| + return nil |
| + }, uid: uid, gid: gid, want: []error{unix.ENOENT}}, |
| + "group-writable system, moves 0700": {setup: func(t *testing.T, opt string) []string { |
| + moves0700(t, opt) |
| + if err := os.Chmod(filepath.Join(opt, "system"), 0o775); err != nil { |
| + t.Fatal(err) |
| + } |
| + return nil |
| + }, uid: uid, gid: gid, want: []error{os.ErrPermission}}, |
| + "world-writable guard, moves missing": {setup: func(t *testing.T, opt string) []string { |
| + if err := os.Chmod(systemGuardDir(opt), 0o777); err != nil { |
| + t.Fatal(err) |
| + } |
| + return nil |
| + }, uid: uid, gid: gid, want: []error{os.ErrPermission}}, |
| + "world-writable base, moves 0700": {setup: func(t *testing.T, opt string) []string { |
| + moves0700(t, opt) |
| + if err := os.Chmod(opt, 0o757); err != nil { |
| + t.Fatal(err) |
| + } |
| + return nil |
| + }, uid: uid, gid: gid, want: []error{os.ErrPermission}}, |
| + // Every ancestor is another owner's; the floor directory would be |
| + // made by the test's own uid, so a create before the ancestry check |
| + // shows in the tree. |
| + "other-owned ancestors, moves missing": {setup: func(t *testing.T, opt string) []string { |
| + return nil |
| + }, uid: uid + 1, gid: gid, want: []error{os.ErrPermission}}, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + opt := floorDirsOpt(t) |
| + others := c.setup(t, opt) |
| + roots := append([]string{opt}, others...) |
| + before := floorTree(t, roots...) |
| + err := prepareRootFloorDirsOwned(opt, opt, c.uid, c.gid) |
| + if err == nil { |
| + t.Fatal("repair accepted it") |
| + } |
| + if !slices.ContainsFunc(c.want, func(w error) bool { return errors.Is(err, w) }) { |
| + t.Fatalf("refused with %v, want one of %v", err, c.want) |
| + } |
| + if after := floorTree(t, roots...); !maps.Equal(before, after) { |
| + t.Fatalf("the refused repair changed the tree:\nbefore %v\nafter %v", before, after) |
| + } |
| + for _, root := range others { |
| + if _, err := os.Lstat(filepath.Join(root, "moves")); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatal("moves made through the symlink", err) |
| + } |
| + } |
| + }) |
| + } |
| + // The repair's own argument checks: a floor directory not beneath the |
| + // base, or the base itself, refuses before any open. |
| + opt := floorDirsOpt(t) |
| + for _, base := range []string{rootFloorDir(opt), filepath.Join(opt, "elsewhere"), "relative"} { |
| + if err := repairRootFloorDir(base, rootFloorDir(opt), uid, gid); !errors.Is(err, os.ErrPermission) { |
| + t.Fatalf("base %s: %v", base, err) |
| + } |
| + } |
| + if _, err := os.Lstat(rootFloorDir(opt)); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatal("moves made", err) |
| + } |
| +} |
| + |
| // The migration repairs the floor directories under its lock and quiesce, |
| // before guardReady's first floor read; a refused repair stops it there, |
| // with no census and no record written. |
diff --git a/internal/host/rootmigration_floordirs_rootrunner_linux_test.go b/internal/host/rootmigration_floordirs_rootrunner_linux_test.go| new file mode 100644 |
| index 000000000..c0a79f040 |
| --- /dev/null |
| +++ b/internal/host/rootmigration_floordirs_rootrunner_linux_test.go |
| @@ -0,0 +1,135 @@ |
| +//go:build linux && rootrunner |
| + |
| +package host |
| + |
| +import ( |
| + "errors" |
| + "maps" |
| + "os" |
| + "path/filepath" |
| + "slices" |
| + "syscall" |
| + "testing" |
| + |
| + "golang.org/x/sys/unix" |
| +) |
| + |
| +// TestRootFloorDirsUntrustedAncestry is fix round 2's witness as real root, |
| +// through production's prepareRootFloorDirs (the walk from /, owner root): |
| +// the floor repair checks every directory from / to guard/ before it makes |
| +// or changes anything. An ancestor symlink with the floor directory missing |
| +// behind it, and an ancestor owned by someone else holding a root-owned |
| +// floor directory (or none), each refuse with both trees as they were. A |
| +// floor directory of another owner under a trusted path refuses too. A |
| +// trusted path under umask 077 is repaired to 0755 root:root. |
| +func TestRootFloorDirsUntrustedAncestry(t *testing.T) { |
| + if os.Geteuid() != 0 { |
| + t.Fatal("designated runner must execute as root") |
| + } |
| + const nobody = 65534 |
| + mkopt := func(t *testing.T) (string, string) { |
| + t.Helper() |
| + scratch := generationRootRunnerDir(t) |
| + opt := filepath.Join(scratch, "opt") |
| + for _, dir := range []string{opt, filepath.Join(opt, "system"), systemGuardDir(opt)} { |
| + if err := os.Mkdir(dir, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Chmod(dir, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + return scratch, opt |
| + } |
| + mkdir := func(t *testing.T, dir string, mode os.FileMode, uid int) { |
| + t.Helper() |
| + if err := os.Mkdir(dir, mode); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Chmod(dir, mode); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Lchown(dir, uid, uid); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + for name, c := range map[string]struct { |
| + setup func(t *testing.T, scratch, opt string) []string // trees outside opt that must not change |
| + want []error |
| + }{ |
| + "symlinked guard, moves missing": {setup: func(t *testing.T, scratch, opt string) []string { |
| + target := filepath.Join(scratch, "realguard") |
| + mkdir(t, target, 0o755, 0) |
| + if err := os.Remove(systemGuardDir(opt)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Symlink(target, systemGuardDir(opt)); err != nil { |
| + t.Fatal(err) |
| + } |
| + return []string{target} |
| + }, want: []error{unix.ELOOP, unix.ENOTDIR}}, |
| + "nobody's system, root's moves 0700": {setup: func(t *testing.T, scratch, opt string) []string { |
| + mkdir(t, rootFloorDir(opt), 0o700, 0) |
| + if err := os.Lchown(filepath.Join(opt, "system"), nobody, nobody); err != nil { |
| + t.Fatal(err) |
| + } |
| + return nil |
| + }, want: []error{os.ErrPermission}}, |
| + "nobody's guard, moves missing": {setup: func(t *testing.T, scratch, opt string) []string { |
| + if err := os.Lchown(systemGuardDir(opt), nobody, nobody); err != nil { |
| + t.Fatal(err) |
| + } |
| + return nil |
| + }, want: []error{os.ErrPermission}}, |
| + "nobody's moves 0700": {setup: func(t *testing.T, scratch, opt string) []string { |
| + mkdir(t, rootFloorDir(opt), 0o700, nobody) |
| + return nil |
| + }, want: []error{os.ErrPermission}}, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + scratch, opt := mkopt(t) |
| + roots := append([]string{opt}, c.setup(t, scratch, opt)...) |
| + before := floorTree(t, roots...) |
| + err := prepareRootFloorDirs(opt) |
| + if !slices.ContainsFunc(c.want, func(w error) bool { return errors.Is(err, w) }) { |
| + t.Fatalf("prepareRootFloorDirs = %v, want a refusal with one of %v", err, c.want) |
| + } |
| + if after := floorTree(t, roots...); !maps.Equal(before, after) { |
| + t.Fatalf("the refused repair changed the tree:\nbefore %v\nafter %v", before, after) |
| + } |
| + for _, root := range roots[1:] { |
| + if _, err := os.Lstat(filepath.Join(root, "moves")); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatal("moves made through the symlink", err) |
| + } |
| + } |
| + t.Logf("refused with both trees unchanged: %v", err) |
| + }) |
| + } |
| + |
| + // The trusted path, guard/moves left 0700 and guard/seeds missing, under |
| + // the window shell's umask 077: both come out 0755 root:root, and a |
| + // second run changes nothing. |
| + t.Run("trusted, umask 077", func(t *testing.T) { |
| + _, opt := mkopt(t) |
| + mkdir(t, rootFloorDir(opt), 0o700, 0) |
| + old := syscall.Umask(0o077) |
| + err := prepareRootFloorDirs(opt) |
| + syscall.Umask(old) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, dir := range []string{rootFloorDir(opt), rootSeedsFloorDir(opt)} { |
| + st, err := os.Lstat(dir) |
| + if err != nil || !st.IsDir() || st.Mode().Perm() != 0o755 || st.Sys().(*syscall.Stat_t).Uid != 0 || st.Sys().(*syscall.Stat_t).Gid != 0 { |
| + t.Fatalf("%s is not a 0755 root:root directory: %v %v", dir, st, err) |
| + } |
| + } |
| + before := floorTree(t, opt) |
| + if err := prepareRootFloorDirs(opt); err != nil { |
| + t.Fatal("second run", err) |
| + } |
| + if after := floorTree(t, opt); !maps.Equal(before, after) { |
| + t.Fatalf("the second run changed the tree:\nbefore %v\nafter %v", before, after) |
| + } |
| + }) |
| +} |
diff --git a/internal/host/rootmigration_production_linux.go b/internal/host/rootmigration_production_linux.go| index 4fc5c439e..c3ff604d6 100644 |
| --- a/internal/host/rootmigration_production_linux.go |
| +++ b/internal/host/rootmigration_production_linux.go |
| @@ -203,71 +203,77 @@ func migrateRootClaimAuthority(ctx context.Context, store release.Store, r Claim |
| // and guard/seeds, 0755 root:root whatever the operator shell's umask |
| // (#317's class). rootstate creates a public directory with Mkdirat(0755) |
| // and never chmods, so under umask 077 it comes out 0700 and rootstate's |
| -// public open refuses it; an interrupted mkdirAllExact (mkdir done, chmod |
| +// public open refuses it; an interrupted earlier attempt (mkdir done, chmod |
| // not) leaves the same. The migration runs this under its lock and |
| // quiesce, before guardReady's first floor read, so a rerun repairs what an |
| // earlier attempt left instead of refusing it forever; ready runs it again |
| // before it opens the floors. |
| // |
| -// A missing directory is made with mkdirAllExact. An existing one is |
| -// repaired only when it is a real directory (not a symlink, no symlink on |
| -// the way) owned by root:root; anything else refuses and is left alone. |
| -func prepareRootFloorDirs(opt string) error { return prepareRootFloorDirsOwned(opt, 0, 0) } |
| +// Nothing is changed until the path to it has been checked: see |
| +// repairRootFloorDir. |
| +func prepareRootFloorDirs(opt string) error { return prepareRootFloorDirsOwned("/", opt, 0, 0) } |
| -// prepareRootFloorDirsOwned is prepareRootFloorDirs with the owner the |
| -// directories must have; production passes root. Tests pass their own. |
| -func prepareRootFloorDirsOwned(opt string, uid, gid uint32) error { |
| +// prepareRootFloorDirsOwned is prepareRootFloorDirs with the directory the |
| +// walk starts from and the owner every directory on it must have; |
| +// production passes / and root. Unprivileged tests pass a scratch directory |
| +// of their own and their own uid and gid, since the real ancestors of their |
| +// scratch are not theirs. |
| +func prepareRootFloorDirsOwned(base, opt string, uid, gid uint32) error { |
| for _, dir := range []string{rootFloorDir(opt), rootSeedsFloorDir(opt)} { |
| - if err := repairRootFloorDir(dir, uid, gid); err != nil { |
| + if err := repairRootFloorDir(base, dir, uid, gid); err != nil { |
| return err |
| } |
| } |
| return nil |
| } |
| -func repairRootFloorDir(dir string, uid, gid uint32) error { |
| - if !filepath.IsAbs(dir) || filepath.Clean(dir) != dir || dir == "/" { |
| - return &os.PathError{Op: "repair", Path: dir, Err: os.ErrPermission} |
| +// repairRootFloorDir walks from base to dir by descriptor (walkNoFollowDir, |
| +// consoleDir's walk) before it changes anything. Every directory from base |
| +// to dir's parent must be a real directory, not a symlink, owned by uid and |
| +// not writable by group or others. Only then is dir made, if it is missing, |
| +// by Mkdirat beneath that checked parent; it is opened with O_NOFOLLOW, must |
| +// be a directory owned by uid and gid, and is Fchmodded to 0755. A symlink |
| +// or a missing directory on the way, an untrusted ancestor, or a dir that is |
| +// a symlink, a file or another owner's refuses, and nothing is made or |
| +// changed. |
| +func repairRootFloorDir(base, dir string, uid, gid uint32) error { |
| + refuse := func(err error) error { return &os.PathError{Op: "repair", Path: dir, Err: err} } |
| + if !filepath.IsAbs(base) || filepath.Clean(base) != base || !filepath.IsAbs(dir) || filepath.Clean(dir) != dir { |
| + return refuse(os.ErrPermission) |
| } |
| - if _, err := os.Lstat(dir); errors.Is(err, os.ErrNotExist) { |
| - if err := mkdirAllExact(dir, 0755); err != nil { |
| + rel, err := filepath.Rel(base, dir) |
| + if err != nil || rel == "." || rel == ".." || strings.HasPrefix(rel, "../") { |
| + return refuse(os.ErrPermission) |
| + } |
| + check := func(fd int, last bool) error { |
| + var st unix.Stat_t |
| + if err := unix.Fstat(fd, &st); err != nil { |
| return err |
| } |
| - } else if err != nil { |
| - return err |
| + if st.Mode&unix.S_IFMT != unix.S_IFDIR || st.Uid != uid || !last && st.Mode&0o022 != 0 || last && st.Gid != gid { |
| + return os.ErrPermission |
| + } |
| + return nil |
| } |
| - // Open it component by component with O_NOFOLLOW, so the descriptor |
| - // checked and chmodded is the directory itself, reached with no symlink. |
| - fd, err := unix.Open("/", unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0) |
| + fd, err := walkNoFollowDir(base, strings.Split(rel, "/"), func(last bool) bool { return last }, check) |
| if err != nil { |
| - return err |
| - } |
| - for _, name := range strings.Split(strings.TrimPrefix(dir, "/"), "/") { |
| - next, err := unix.Openat(fd, name, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) |
| - unix.Close(fd) |
| - if err != nil { |
| - return &os.PathError{Op: "repair", Path: dir, Err: err} |
| - } |
| - fd = next |
| + return refuse(err) |
| } |
| defer unix.Close(fd) |
| var st unix.Stat_t |
| if err := unix.Fstat(fd, &st); err != nil { |
| - return err |
| - } |
| - if st.Mode&unix.S_IFMT != unix.S_IFDIR || st.Uid != uid || st.Gid != gid { |
| - return &os.PathError{Op: "repair", Path: dir, Err: os.ErrPermission} |
| + return refuse(err) |
| } |
| if st.Mode&07777 != 0755 { |
| if err := unix.Fchmod(fd, 0755); err != nil { |
| - return &os.PathError{Op: "repair", Path: dir, Err: err} |
| + return refuse(err) |
| + } |
| + if err := unix.Fstat(fd, &st); err != nil { |
| + return refuse(err) |
| } |
| - } |
| - if err := unix.Fstat(fd, &st); err != nil { |
| - return err |
| } |
| if st.Mode&07777 != 0755 { |
| - return &os.PathError{Op: "repair", Path: dir, Err: os.ErrPermission} |
| + return refuse(os.ErrPermission) |
| } |
| return nil |
| } |
diff --git a/internal/provision/plan14_root_reads_test.go b/internal/provision/plan14_root_reads_test.go| index e18f2dba4..4a4eec029 100644 |
| --- a/internal/provision/plan14_root_reads_test.go |
| +++ b/internal/provision/plan14_root_reads_test.go |
| @@ -969,6 +969,15 @@ func TestPlan14RootReadCensus(t *testing.T) { |
| if err = loadRootReadInventory(filepath.Join(root, "internal/provision/testdata/plan14-root-reads-claim-empty-cohort-fix1.tsv"), &inv, true); err != nil { |
| t.Fatal(err) |
| } |
| + // Census overlay claim-empty-cohort-fix2 (fix round 2: the floor repair |
| + // walks from / by descriptor and checks every ancestor before it makes |
| + // or changes anything, through walkNoFollowDir, consoleDir's walk |
| + // factored out unchanged), loaded last. PROPOSED: the ruling is owed |
| + // (docs/superpowers/notes/census-proposal-claim-step8-readiness.md, |
| + // "Fix round 2"). It adds no accepted-legacy row, so the seal stands. |
| + if err = loadRootReadInventory(filepath.Join(root, "internal/provision/testdata/plan14-root-reads-claim-empty-cohort-fix2.tsv"), &inv, true); err != nil { |
| + t.Fatal(err) |
| + } |
| errs := append(compareRootReads(got, inv), checkRootReadLegacy(inv, seal)...) |
| errs = append(errs, checkRootReadDeclarations(got, inv)...) |
| if len(errs) > 0 { |
diff --git a/internal/provision/testdata/plan14-root-reads-claim-empty-cohort-fix2.tsv b/internal/provision/testdata/plan14-root-reads-claim-empty-cohort-fix2.tsv| new file mode 100644 |
| index 000000000..bbfc38cd4 |
| --- /dev/null |
| +++ b/internal/provision/testdata/plan14-root-reads-claim-empty-cohort-fix2.tsv |
| @@ -0,0 +1,6 @@ |
| +# Census overlay claim-empty-cohort-fix2 (fix round 2 for claim-empty-cohort-done: Astra's re-review finding on the floor repair, as ruled by the controller), loaded LAST, after claim-empty-cohort-fix1. PROPOSED: the ruling is owed; the proposal is docs/superpowers/notes/census-proposal-claim-step8-readiness.md, section "Fix round 2". |
| +# consoleDir's descriptor walk is factored out, unchanged, into walkNoFollowDir (new), and repairRootFloorDir now walks with it, checking every ancestor before it makes or changes anything; the pathname Lstat and mkdirAllExact are gone from the floor repair. consoleDir keeps its class and task exactly; walkNoFollowDir takes the class both its callers have. It adds no accepted-legacy row, so the seal stands. |
| +@coverage internal/host 4b1e0c8679f254b618c81c495c9af1aa3ec55d3f6a72ccc1a47b60850515333a |
| +internal/host/activation_artifacts_linux.go consoleDir authority G,J4 rev3=PROPOSED-claim-empty-cohort-fix2;identity=explicit box installer uid 0 (claims install-console);basis=walks from / by no-follow descriptors; every component root-owned, not group or other writable and other-searchable; creates missing components 0755 and fsyncs the parent; the protected directory must be exactly 0755 root:root. Fix round 2: the walk itself (the open of /, each O_NOFOLLOW Openat, Mkdirat and Fchmodat beneath a checked parent, the parent fsync and the reopen) moved unchanged into walkNoFollowDir; consoleDir keeps its own checks, its Fstat of every component, and passes create for every component, so its behaviour is the same file:Fstat@259 689a231d7b7dc9cd8db1adc7a250947ad9a1646a7549119034a69d13761ae7a8 |
| +internal/host/nofollowdir_linux.go walkNoFollowDir authority G,J4 rev3=PROPOSED-claim-empty-cohort-fix2;identity=uid 0 callers only: consoleDir (claims install-console) and repairRootFloorDir (carlos system journal-migrate, offline); basis=new, consoleDir's walk factored out unchanged: opens the caller's base (/ in production) then each name beneath it with O_RDONLY, O_DIRECTORY, O_NOFOLLOW and O_CLOEXEC; the caller's check sees every directory before anything beneath it is opened or made, and the final directory once open; a missing name is made only where the caller allows (Mkdirat 0755 then Fchmodat 0755 beneath the checked parent, the parent fsynced, the name reopened with O_NOFOLLOW); it reads no file content and returns the checked final descriptor file:Open@193,file:Openat@449,file:Openat@768 f9107577a7dbf5bf17472506903419e304e4f0300698f2e9b0e927f5bfd148d6 |
| +internal/host/rootmigration_production_linux.go repairRootFloorDir authority G,J4 rev3=PROPOSED-claim-empty-cohort-fix2;identity=carlos system journal-migrate, offline operator CLI, uid 0;basis=as ruled under census-ruling-claim-empty-cohort-fix1, repairs one of the two fixed floor directories (guard/moves, guard/seeds) to 0755 only when it is a directory owned by root:root. Fix round 2: no pathname Lstat or mkdirAllExact any more; it walks from / with walkNoFollowDir and Fstats every directory on the way, refusing unless each from / to guard/ is a real directory, not a symlink, owned by root and not writable by group or others, before anything is made; only then is a missing floor directory made by Mkdirat beneath the checked guard/, opened with O_NOFOLLOW, Fstatted (a directory owned by root:root) and Fchmodded to 0755, with one more Fstat to confirm; a symlink or a missing directory on the way, an untrusted ancestor, or a floor directory that is a symlink, a file or another owner's refuses with nothing made or changed; no file content is read file:Fstat@1124,file:Fstat@539,file:Fstat@952 d5937bfe755c2b86b38756a3016d56cf89a635b3bf0ff2869dc19d22a35622cb |