| 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) |
| } |