carlos / platform Public

ship --check: pin prepare's chmod-then-chown order (R1 P2)

Astra round 1 (P2): TestPrepareCheckDir chowned to the directory's
existing owner, so deleting the Lchown or moving it before the Chmod
still passed. CI has no root to give a directory away, so the two calls
go through package seams (checkChmod, checkLchown; production never
changes them) and TestPrepareCheckDirSetsModeThenOwner pins exactly
chmod(dir, 0700) then lchown(dir, 65534, 65534), through the function
and the verb; a refused symlink and a failed chmod make no further call.
Mutations caught: drop the Lchown; swap the order; a wrong group. The
capability behaviour itself (root with CAP_CHOWN alone) stays witnessed
by the rootful Podman run on the dev box.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 500ec2ba20b4ccef410f22f443170faf250f72de parent 29e0c39
2 files changed, +66 −2
  • ship_check_prepare_test.go +56 −0
  • ship_check_probe.go +10 −2
diff --git a/ship_check_prepare_test.go b/ship_check_prepare_test.go
index 939b671e3..d0cbc331f 100644
--- a/ship_check_prepare_test.go
+++ b/ship_check_prepare_test.go
@@ -4,9 +4,11 @@ package main
import (
"errors"
+ "fmt"
"os"
"os/exec"
"path/filepath"
+ "slices"
"strconv"
"strings"
"syscall"
@@ -74,6 +76,60 @@ func TestPrepareCheckDir(t *testing.T) {
}
}
+// prepareCheckDir sets the mode, then the owner, with exactly the
+// arguments the check needs. The order is the property: in the prepare
+// container root holds CAP_CHOWN alone, so once the directory is 65534's it
+// could no longer chmod it (that needs CAP_FOWNER). CI has no root to give
+// a directory away, so this pins the calls through the checkChmod and
+// checkLchown seams; the capabilities themselves are witnessed by the
+// rootful Podman run on a dev box (rootfs Plan 4 Task 11, 2026-10-29).
+func TestPrepareCheckDirSetsModeThenOwner(t *testing.T) {
+ var calls []string
+ oldChmod, oldLchown := checkChmod, checkLchown
+ t.Cleanup(func() { checkChmod, checkLchown = oldChmod, oldLchown })
+ checkChmod = func(name string, mode os.FileMode) error {
+ calls = append(calls, fmt.Sprintf("chmod %s %#o", name, mode))
+ return nil
+ }
+ checkLchown = func(name string, uid, gid int) error {
+ calls = append(calls, fmt.Sprintf("lchown %s %d:%d", name, uid, gid))
+ return nil
+ }
+
+ dir := filepath.Join(t.TempDir(), "check")
+ if err := os.Mkdir(dir, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := prepareCheckDir(dir, 65534, 65534); err != nil {
+ t.Fatalf("prepareCheckDir: %v", err)
+ }
+ want := []string{"chmod " + dir + " 0700", "lchown " + dir + " 65534:65534"}
+ if !slices.Equal(calls, want) {
+ t.Errorf("calls %q, want %q", calls, want)
+ }
+
+ // The verb gives --uid as the group too.
+ calls = nil
+ if got := runRootfsCheckPrepare([]string{"--dir", dir, "--uid", "65534"}); got != 0 || !slices.Equal(calls, want) {
+ t.Errorf("the verb: exit %d, calls %q, want 0 and %q", got, calls, want)
+ }
+
+ // A refused path is never changed; a chmod that fails stops the chown.
+ link := filepath.Join(t.TempDir(), "link")
+ if err := os.Symlink(dir, link); err != nil {
+ t.Fatal(err)
+ }
+ calls = nil
+ if err := prepareCheckDir(link, 65534, 65534); err == nil || len(calls) != 0 {
+ t.Errorf("a symlink: %v, calls %q", err, calls)
+ }
+ checkChmod = func(string, os.FileMode) error { return errors.New("operation not permitted") }
+ calls = nil
+ if err := prepareCheckDir(dir, 65534, 65534); err == nil || len(calls) != 0 {
+ t.Errorf("a failed chmod: %v, calls %q", err, calls)
+ }
+}
+
// The verb's exit statuses: 0 done, 1 refused, 2 a flag error.
func TestRunRootfsCheckPrepareExits(t *testing.T) {
dir := filepath.Join(t.TempDir(), "check")
diff --git a/ship_check_probe.go b/ship_check_probe.go
index 9f01df979..96184ff4c 100644
--- a/ship_check_probe.go
+++ b/ship_check_probe.go
@@ -120,6 +120,14 @@ func runRootfsCheckPrepare(args []string) int {
return 0
}
+// checkChmod and checkLchown are prepareCheckDir's two changes. Vars so a
+// test can pin their order and arguments: CI has no root to give a
+// directory away. Production never changes them.
+var (
+ checkChmod = os.Chmod
+ checkLchown = os.Lchown
+)
+
// prepareCheckDir makes dir mode 0700 and owned by uid:gid: dir alone,
// never what is in it, and never through a symlink, which Lstat refuses
// with anything else that is not a directory. The mode goes first: once
@@ -133,10 +141,10 @@ func prepareCheckDir(dir string, uid, gid int) error {
if !fi.IsDir() {
return fmt.Errorf("%s is not a directory", dir)
}
- if err := os.Chmod(dir, 0o700); err != nil {
+ if err := checkChmod(dir, 0o700); err != nil {
return err
}
- return os.Lchown(dir, uid, gid)
+ return checkLchown(dir, uid, gid)
}
// checkProbeGet asks GET / once, for the check's host, and answers the