carlos / platform Public

rootfs probe: root-image judges the loop device, not the source string (#366)

On the v0.28.17 dev roll (Debian 13, systemd 257.13) every box's rootfs
probe failed root-image with "/ is squashfs on /proc/self/fd/3, not a
squashfs on a loop device": systemd 257 attaches RootImage= to a loop
device and mounts it through the loop fd, so mountinfo's source field is
/proc/self/fd/N. rootfs_ready was false everywhere (failing closed).

parseMountinfo now reads field 3 (MAJ:MIN) into mountEntry and refuses a
malformed one. rootIsImage requires the top mount at / to be squashfs on
the loop major (7), mounted ro, with a source of exactly /dev/loopN or
/proc/self/fd/N (a whitelist, not a prefix), plus the marker as before.
A device-mapper root (dm-verity included) is refused: the spec asks for a
loop device and not for verity. The probe fixture's root line is now the
systemd 257 shape.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 4c2114a1969251b5f5bff30cb646d2a9e75f9101 parent a912a34
2 files changed, +214 −12
  • internal/instanceexec/probe_rootfs.go +80 −9
  • internal/instanceexec/probe_rootfs_test.go +134 −3
diff --git a/internal/instanceexec/probe_rootfs.go b/internal/instanceexec/probe_rootfs.go
index 0e089438e..be45fdaff 100644
--- a/internal/instanceexec/probe_rootfs.go
+++ b/internal/instanceexec/probe_rootfs.go
@@ -75,8 +75,8 @@ func runRootfsChecks(e rootfsProbeEnv) instancectl.ProbeReport {
v := &verdicts{}
entries, merr := readMountinfo(e.mountinfo)
- // The root is the probe image: a squashfs on a loop device, carrying
- // the marker root wrote into it.
+ // The root is the probe image: a read-only squashfs on a loop device,
+ // carrying the marker root wrote into it.
if merr != nil {
v.add("root-image", "fail", merr.Error())
} else {
@@ -123,8 +123,13 @@ func runRootfsChecks(e rootfsProbeEnv) instancectl.ProbeReport {
// mountEntry is one line of /proc/self/mountinfo: the mount point, the
// root of the mount within its file system, the file system type, the
-// source and the per-mount options.
-type mountEntry struct{ Point, Root, FSType, Source, Options string }
+// source, the per-mount options, and the device (field 3, MAJ:MIN), which
+// is st_dev of the mount's files and so names the block device whatever
+// the source field reads.
+type mountEntry struct {
+ Point, Root, FSType, Source, Options string
+ Major, Minor uint32
+}
func readMountinfo(path string) ([]mountEntry, error) {
b, err := os.ReadFile(path)
@@ -137,7 +142,9 @@ func readMountinfo(path string) ([]mountEntry, error) {
// parseMountinfo reads proc_pid_mountinfo(5): six fields, optional fields
// up to a lone "-", then the type, the source and the super options.
// Paths carry octal escapes for space, tab, newline and backslash, which
-// are decoded. A line of another shape is an error, never skipped.
+// are decoded. A line of another shape, a device field among them that is
+// not two decimal numbers either side of one colon, is an error, never
+// skipped.
func parseMountinfo(b []byte) ([]mountEntry, error) {
var out []mountEntry
for i, line := range strings.Split(string(b), "\n") {
@@ -155,9 +162,14 @@ func parseMountinfo(b []byte) ([]mountEntry, error) {
if sep < 0 || len(f) < sep+3 {
return nil, fmt.Errorf("mountinfo line %d: %q is not a mount", i+1, line)
}
+ major, minor, ok := parseDevice(f[2])
+ if !ok {
+ return nil, fmt.Errorf("mountinfo line %d: device %q is not MAJ:MIN", i+1, f[2])
+ }
out = append(out, mountEntry{
Root: unescapeMountinfo(f[3]), Point: unescapeMountinfo(f[4]), Options: f[5],
FSType: f[sep+1], Source: unescapeMountinfo(f[sep+2]),
+ Major: major, Minor: minor,
})
}
if len(out) == 0 {
@@ -166,6 +178,34 @@ func parseMountinfo(b []byte) ([]mountEntry, error) {
return out, nil
}
+// parseDevice reads mountinfo's MAJ:MIN field: decimal numbers, each
+// fitting the kernel's 32-bit dev_t halves.
+func parseDevice(s string) (major, minor uint32, ok bool) {
+ a, b, found := strings.Cut(s, ":")
+ if !found || !allDigits(a) || !allDigits(b) {
+ return 0, 0, false
+ }
+ ma, err1 := strconv.ParseUint(a, 10, 32)
+ mi, err2 := strconv.ParseUint(b, 10, 32)
+ if err1 != nil || err2 != nil {
+ return 0, 0, false
+ }
+ return uint32(ma), uint32(mi), true
+}
+
+// allDigits reports whether s is one or more ASCII decimal digits.
+func allDigits(s string) bool {
+ if s == "" {
+ return false
+ }
+ for i := 0; i < len(s); i++ {
+ if s[i] < '0' || s[i] > '9' {
+ return false
+ }
+ }
+ return true
+}
+
// unescapeMountinfo decodes the kernel's \ooo escapes.
func unescapeMountinfo(s string) string {
if !strings.Contains(s, `\`) {
@@ -185,8 +225,23 @@ func unescapeMountinfo(s string) string {
return b.String()
}
-// rootIsImage: the mount on top at / is a squashfs on a loop device, and
-// the probe image's marker is a regular file at its root.
+// loopMajor is the loop block device's major number (LOOP_MAJOR,
+// include/uapi/linux/major.h). Every loop device has it, whatever its
+// minor; device-mapper (dm-verity among it) and disks have others.
+const loopMajor = 7
+
+// rootIsImage: the mount on top at / is the probe image, a read-only
+// squashfs on a loop device, and the image's marker is a regular file at
+// its root. The device is judged, not the source string: mountinfo's
+// MAJ:MIN is the mount's st_dev, so it must be the loop major, while the
+// source only says how the device was named at mount time. systemd 257
+// mounts RootImage= through the loop device's fd, so its source reads
+// /proc/self/fd/N (the v0.28.17 dev roll, 2026-10-26), where a mount by
+// path reads /dev/loopN; the source must be one of those two shapes and
+// nothing else. A dm-verity root is refused: the spec (C3) asks for the
+// image on a loop device and does not ask for verity. The per-mount "ro"
+// is the mount's own read-only flag; root-read-only (rootReadOnly) proves
+// the same from the other side, by a create answering EROFS.
func rootIsImage(entries []mountEntry, marker string) (string, bool) {
var root *mountEntry
for i := range entries {
@@ -198,8 +253,11 @@ func rootIsImage(entries []mountEntry, marker string) (string, bool) {
return "no mount at /", false
}
var bad []string
- if root.FSType != "squashfs" || !strings.HasPrefix(root.Source, "/dev/loop") {
- bad = append(bad, fmt.Sprintf("/ is %s on %s, not a squashfs on a loop device", root.FSType, root.Source))
+ if root.FSType != "squashfs" || root.Major != loopMajor || !loopSource(root.Source) {
+ bad = append(bad, fmt.Sprintf("/ is %s on %s (device %d:%d), not a squashfs on a loop device", root.FSType, root.Source, root.Major, root.Minor))
+ }
+ if !slices.Contains(strings.Split(root.Options, ","), "ro") {
+ bad = append(bad, fmt.Sprintf("/ is mounted %s, not ro", root.Options))
}
if fi, err := os.Lstat(marker); err != nil {
bad = append(bad, err.Error())
@@ -209,6 +267,19 @@ func rootIsImage(entries []mountEntry, marker string) (string, bool) {
return strings.Join(bad, "; "), len(bad) == 0
}
+// loopSource reports whether a mount source is one of the two shapes a
+// loop mount shows: /dev/loopN, or /proc/self/fd/N (a mount through the
+// loop device's fd). Nothing else passes: not /dev/loop-control, not
+// /dev/loopfoo, not a host path.
+func loopSource(s string) bool {
+ for _, prefix := range []string{"/dev/loop", "/proc/self/fd/"} {
+ if n, ok := strings.CutPrefix(s, prefix); ok && allDigits(n) {
+ return true
+ }
+ }
+ return false
+}
+
// mountsAllowlisted is C3's probe form: every mount point is / or one of
// the allowed points and trees, and no mount's root or source names the
// version store, the live links, the host's /var or root's credentials.
diff --git a/internal/instanceexec/probe_rootfs_test.go b/internal/instanceexec/probe_rootfs_test.go
index edc854846..19de176fd 100644
--- a/internal/instanceexec/probe_rootfs_test.go
+++ b/internal/instanceexec/probe_rootfs_test.go
@@ -23,8 +23,11 @@ import (
// sockets BindLogSockets= binds, /run/host/incoming and os-release, the
// host's /dev/pts, /dev/shm, /dev/mqueue and /dev/hugepages under the
// block's tmpfs mounts, ProtectKernelTunables= remounts, and ProtectHome=
-// over the image's /home and /root).
-const probeMountinfo = `600 550 7:3 / / ro,nosuid,nodev,relatime - squashfs /dev/loop3 ro,errors=continue
+// over the image's /home and /root). The root's source is the loop
+// device's fd path, which is what systemd 257 mounts RootImage= through
+// (the v0.28.17 dev roll, 2026-10-26: Debian 13, systemd 257.13); the
+// device is still the loop device, 7:3.
+const probeMountinfo = `600 550 7:3 / / ro,nosuid,nodev,relatime - squashfs /proc/self/fd/3 ro,errors=continue
601 600 0:90 / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw,hidepid=invisible,subset=pid
602 601 0:90 /sys /proc/sys ro,nosuid,nodev,noexec,relatime - proc proc rw,hidepid=invisible,subset=pid
603 601 0:24 /systemd/inaccessible/reg /proc/kallsyms ro,nosuid,nodev,noexec - tmpfs tmpfs rw,mode=755
@@ -155,6 +158,24 @@ func (e rootfsProbeEnv) setMount(t *testing.T, point, fstype, source string) {
}
}
+// setRootLine replaces the fixture's line for the image root (mount ID
+// 600) with line.
+func (e rootfsProbeEnv) setRootLine(t *testing.T, line string) {
+ t.Helper()
+ b, err := os.ReadFile(e.mountinfo)
+ if err != nil {
+ t.Fatal(err)
+ }
+ lines := strings.Split(strings.TrimRight(string(b), "\n"), "\n")
+ if !strings.HasPrefix(lines[0], "600 550 ") {
+ t.Fatalf("the fixture's first line is not the root: %q", lines[0])
+ }
+ lines[0] = line
+ if err := os.WriteFile(e.mountinfo, []byte(strings.Join(lines, "\n")+"\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+}
+
func (e rootfsProbeEnv) addMountLine(t *testing.T, line string) {
t.Helper()
f, err := os.OpenFile(e.mountinfo, os.O_APPEND|os.O_WRONLY, 0)
@@ -196,6 +217,24 @@ func TestRootfsProbeFailsWhenTheRootIsNotTheImage(t *testing.T) {
if r := runRootfsChecks(e); r.Ready || verdict(r, "root-image") != "fail" {
t.Error("a squashfs root that is not on a loop device passed")
}
+ // The device decides, through the whole report: squashfs on a disk
+ // partition (8:1) or on device-mapper (253:0, dm-verity's shape) is
+ // refused with the fd source systemd 257 shows, and the loop device
+ // passes with either source form.
+ for dev, want := range map[string]string{"8:1": "fail", "253:0": "fail", "7:3": "ok"} {
+ for _, source := range []string{"/proc/self/fd/3", "/dev/loop3"} {
+ e = goodRootfsEnv(t)
+ e.setRootLine(t, "600 550 "+dev+" / / ro,nosuid,nodev,relatime - squashfs "+source+" ro,errors=continue")
+ if r := runRootfsChecks(e); verdict(r, "root-image") != want || r.Ready != (want == "ok") {
+ t.Errorf("squashfs on %s from %s: root-image %s (%s), ready %v; want %s", dev, source, verdict(r, "root-image"), detail(r, "root-image"), r.Ready, want)
+ }
+ }
+ }
+ e = goodRootfsEnv(t)
+ e.setRootLine(t, "600 550 7:3 / / rw,nosuid,nodev,relatime - squashfs /proc/self/fd/3 ro,errors=continue")
+ if r := runRootfsChecks(e); r.Ready || verdict(r, "root-image") != "fail" {
+ t.Error("a root mounted read-write passed")
+ }
e = goodRootfsEnv(t)
os.Remove(e.rootMarker)
if r := runRootfsChecks(e); r.Ready || verdict(r, "root-image") != "fail" {
@@ -209,6 +248,89 @@ func TestRootfsProbeFailsWhenTheRootIsNotTheImage(t *testing.T) {
}
}
+// rootIsImage judges the device, not the source string: the mount on top
+// at / is a squashfs, read-only, on the loop block major (7), with a
+// source of one of the two shapes a loop mount shows ("/dev/loopN", or
+// "/proc/self/fd/N" when systemd 257 mounts through the loop device's fd),
+// and the probe marker is a regular file. Each refused case is wrong in
+// one respect only, so each pins one requirement.
+func TestRootIsImage(t *testing.T) {
+ marker := filepath.Join(t.TempDir(), ".carlos-rootfs-probe")
+ if err := os.WriteFile(marker, []byte("carlos rootfs probe image\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ other := "601 600 0:90 / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw\n"
+ for name, c := range map[string]struct {
+ root string
+ ok bool
+ }{
+ // What systemd 257 shows (dev roll, 2026-10-26).
+ "systemd 257 fd source": {"600 550 7:3 / / ro,nosuid,nodev,relatime - squashfs /proc/self/fd/3 ro,errors=continue", true},
+ "loop device source": {"600 550 7:3 / / ro,nosuid,nodev,relatime - squashfs /dev/loop3 ro,errors=continue", true},
+ "a high loop minor": {"600 550 7:1048576 / / ro,relatime - squashfs /dev/loop1048576 ro", true},
+ "a two-digit fd": {"600 550 7:12 / / ro,relatime - squashfs /proc/self/fd/12 ro", true},
+
+ "host ext4 root": {"600 550 8:1 / / rw,relatime - ext4 /dev/root rw", false},
+ "host xfs root": {"600 550 259:1 / / rw,relatime - xfs /dev/nvme0n1p1 rw", false},
+ // A host directory bound at / shows the host disk's device, with a
+ // root that is not the file system's own.
+ "host directory bound at /": {"600 550 8:1 /srv/image / ro,relatime - ext4 /dev/root rw", false},
+ // Squashfs on a block device that is not a loop device: only the
+ // major says so.
+ "squashfs on a disk, fd source": {"600 550 8:1 / / ro,relatime - squashfs /proc/self/fd/3 ro", false},
+ "squashfs on a disk, loop source": {"600 550 259:0 / / ro,relatime - squashfs /dev/loop3 ro", false},
+ // The spec (C3) asks for the image on a loop device and did not
+ // ask for verity, so a device-mapper root (dm-verity or any other
+ // dm target, a dynamic major such as 253) is refused, whatever its
+ // source reads.
+ "dm-verity root": {"600 550 253:0 / / ro,relatime - squashfs /dev/mapper/probe-verity ro", false},
+ "dm root, fd source": {"600 550 253:0 / / ro,relatime - squashfs /proc/self/fd/3 ro", false},
+ "dm root, dm-N source": {"600 550 254:0 / / ro,relatime - squashfs /dev/dm-0 ro", false},
+ "loop major, ext4": {"600 550 7:3 / / ro,relatime - ext4 /dev/loop3 ro", false},
+ "loop major, erofs": {"600 550 7:3 / / ro,relatime - erofs /proc/self/fd/3 ro", false},
+ "loop major, overlay": {"600 550 7:3 / / ro,relatime - overlay overlay ro", false},
+ "a read-write root": {"600 550 7:3 / / rw,relatime - squashfs /dev/loop3 ro", false},
+ "source /dev/loopfoo": {"600 550 7:3 / / ro,relatime - squashfs /dev/loopfoo ro", false},
+ "source /dev/loop": {"600 550 7:3 / / ro,relatime - squashfs /dev/loop ro", false},
+ "source /dev/loop3/x": {"600 550 7:3 / / ro,relatime - squashfs /dev/loop3/x ro", false},
+ "source /dev/loop-control": {"600 550 7:3 / / ro,relatime - squashfs /dev/loop-control ro", false},
+ "source a host image path": {"600 550 7:3 / / ro,relatime - squashfs /data/image.squashfs ro", false},
+ "source /proc/self/fd/": {"600 550 7:3 / / ro,relatime - squashfs /proc/self/fd/ ro", false},
+ "source /proc/self/fd/3x": {"600 550 7:3 / / ro,relatime - squashfs /proc/self/fd/3x ro", false},
+ "source another pid's fd": {"600 550 7:3 / / ro,relatime - squashfs /proc/1/fd/3 ro", false},
+ "source a loop dir path": {"600 550 7:3 / / ro,relatime - squashfs /proc/self/fd/../../../srv ro", false},
+ } {
+ t.Run(name, func(t *testing.T) {
+ entries, err := parseMountinfo([]byte(c.root + "\n" + other))
+ if err != nil {
+ t.Fatal(err)
+ }
+ d, ok := rootIsImage(entries, marker)
+ if ok != c.ok {
+ t.Errorf("rootIsImage = %v (%q), want %v", ok, d, c.ok)
+ }
+ })
+ }
+ // The mount on top at / is the one judged: an image under a host
+ // mount stacked over it is refused, and so is an image without the
+ // marker.
+ entries, err := parseMountinfo([]byte("600 550 7:3 / / ro - squashfs /proc/self/fd/3 ro\n700 600 8:1 / / rw - ext4 /dev/root rw\n"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if d, ok := rootIsImage(entries, marker); ok {
+ t.Errorf("a host root stacked over the image passed: %q", d)
+ }
+ entries, _ = parseMountinfo([]byte("600 550 7:3 / / ro - squashfs /proc/self/fd/3 ro\n"))
+ if d, ok := rootIsImage(entries, marker+".missing"); ok {
+ t.Errorf("an image without the marker passed: %q", d)
+ }
+ entries, _ = parseMountinfo([]byte(other))
+ if d, ok := rootIsImage(entries, marker); ok {
+ t.Errorf("no mount at / passed: %q", d)
+ }
+}
+
// C3 from inside: a mount from another instance's directory, from the
// version store or from the live links fails the allowlist, whatever
// else is right.
@@ -507,10 +629,19 @@ func TestRealMmapWX(t *testing.T) {
}
func TestParseMountinfo(t *testing.T) {
- entries, err := parseMountinfo([]byte("36 35 7:3 / / ro,relatime - squashfs /dev/loop3 ro\n40 36 0:25 /.instances/_probe /data/instances/_probe rw,nosuid,nodev,noexec,nosymfollow - ext4 /dev/root rw\n"))
+ entries, err := parseMountinfo([]byte("36 35 7:3 / / ro,relatime - squashfs /dev/loop3 ro\n40 36 259:65536 /.instances/_probe /data/instances/_probe rw,nosuid,nodev,noexec,nosymfollow - ext4 /dev/root rw\n"))
if err != nil || len(entries) != 2 {
t.Fatalf("%v %v", entries, err)
}
+ // Field 3 is the device, MAJ:MIN.
+ if entries[0].Major != 7 || entries[0].Minor != 3 || entries[1].Major != 259 || entries[1].Minor != 65536 {
+ t.Errorf("devices %d:%d and %d:%d", entries[0].Major, entries[0].Minor, entries[1].Major, entries[1].Minor)
+ }
+ for _, dev := range []string{"7", "7:", ":3", "x:3", "7:y", "7:3:1", "-7:3", "4294967296:0"} {
+ if _, err := parseMountinfo([]byte("36 35 " + dev + " / / ro - squashfs /dev/loop3 ro\n")); err == nil {
+ t.Errorf("a device field %q parsed", dev)
+ }
+ }
if entries[0].FSType != "squashfs" || entries[0].Source != "/dev/loop3" || entries[1].Root != "/.instances/_probe" || entries[1].Point != "/data/instances/_probe" {
t.Errorf("parsed %+v", entries)
}