carlos / platform Public

host: R1 fixes for no-install applies and the control mount check

An apply that installs nothing (no-op request, build already on disk)
now creates the live sandbox directories too, behind the same group
maintenance permit installUnits needs (R1 P2-1).

refreshControlSandboxRootfs judges oci-rootfs from the mount that
contains it: a writable parent with no mount of the path's own is
healthy and never restarted; malformed mountinfo is unknown and leaves
control alone (R1 P2-2).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev b584902c0bd1f165039e9b0d675359e9417e720e parent 905f5cc
3 files changed, +193 −4
  • internal/host/execsandboxfloor.go +75 −4
  • internal/host/runsc_update_dirs_linux_test.go +99 −0
  • internal/host/systemapply.go +19 −0
diff --git a/internal/host/execsandboxfloor.go b/internal/host/execsandboxfloor.go
index 04d83f1cd..438b5c566 100644
--- a/internal/host/execsandboxfloor.go
+++ b/internal/host/execsandboxfloor.go
@@ -9,6 +9,7 @@ import (
"log"
"os"
"path/filepath"
+ "slices"
"strings"
"github.com/carlosframework/platform/internal/execsandbox"
@@ -173,10 +174,13 @@ func refreshControlSandboxRootfs(etcDir string, a *systemApplier, systemctl, pro
logf("adopt: %s mount table: %v", claimControlUnit, err)
return
}
- for _, line := range strings.Split(string(raw), "\n") {
- if f := strings.Fields(line); len(f) > 4 && f[4] == rootfs {
- return
- }
+ writable, known := mountWritable(raw, rootfs)
+ if !known {
+ logf("adopt: %s mount table does not settle whether %s is writable; leaving it", claimControlUnit, rootfs)
+ return
+ }
+ if writable {
+ return
}
logf("adopt: %s started before %s existed; restarting it so gVisor can write there", claimControlUnit, rootfs)
if _, err := systemctl("try-restart", claimControlUnit); err != nil {
@@ -184,6 +188,73 @@ func refreshControlSandboxRootfs(etcDir string, a *systemApplier, systemctl, pro
}
}
+// mountWritable judges path from the mount that contains it in a
+// /proc/<pid>/mountinfo table: the entry with the longest mount point that
+// is path or one of its parents, later entries winning ties (they are
+// mounted over earlier ones). Its per-mount options say rw or ro. A
+// writable parent with no mount point of the path's own is healthy:
+// systemd drops a ReadWritePaths= entry a writable parent already covers.
+// Any malformed line, or no containing mount, is unknown.
+func mountWritable(table []byte, path string) (writable, known bool) {
+ best := -1
+ for _, line := range strings.Split(string(table), "\n") {
+ if strings.TrimSpace(line) == "" {
+ continue
+ }
+ f := strings.Fields(line)
+ sep := slices.Index(f, "-")
+ if len(f) < 10 || sep < 6 || len(f) < sep+4 {
+ return false, false
+ }
+ point, ok := unescapeMountinfo(f[4])
+ if !ok || !strings.HasPrefix(point, "/") {
+ return false, false
+ }
+ if point != "/" && point != path && !strings.HasPrefix(path, point+"/") {
+ continue
+ }
+ if len(point) < best {
+ continue
+ }
+ best = len(point)
+ writable, known = false, false
+ for _, opt := range strings.Split(f[5], ",") {
+ switch opt {
+ case "rw":
+ writable, known = true, true
+ case "ro":
+ writable, known = false, true
+ }
+ }
+ }
+ return writable, known
+}
+
+// unescapeMountinfo undoes the kernel's \ooo octal escapes (space, tab,
+// newline, backslash) in a mountinfo path field.
+func unescapeMountinfo(s string) (string, bool) {
+ var b strings.Builder
+ for i := 0; i < len(s); i++ {
+ if s[i] != '\\' {
+ b.WriteByte(s[i])
+ continue
+ }
+ if i+4 > len(s) {
+ return "", false
+ }
+ v := 0
+ for _, c := range s[i+1 : i+4] {
+ if c < '0' || c > '7' {
+ return "", false
+ }
+ v = v*8 + int(c-'0')
+ }
+ b.WriteByte(byte(v))
+ i += 3
+ }
+ return b.String(), true
+}
+
// Called by instance-control as root: the agent cannot read root's 0600 host.env.
func sandboxEnvironmentProblem(read func() (func(string) string, error)) error {
getenv, err := read()
diff --git a/internal/host/runsc_update_dirs_linux_test.go b/internal/host/runsc_update_dirs_linux_test.go
index 5d89597b9..f7407ece6 100644
--- a/internal/host/runsc_update_dirs_linux_test.go
+++ b/internal/host/runsc_update_dirs_linux_test.go
@@ -261,9 +261,13 @@ func TestSandboxUpdateControlRestartForNewRootfs(t *testing.T) {
for _, tc := range []struct {
name, mode, pid string
mounted, want bool
+ parent, garbled bool
}{
{name: "gvisor, started before the directory", mode: "gvisor", pid: "4242", want: true},
{name: "gvisor, path mounted", mode: "gvisor", pid: "4242", mounted: true},
+ // R1 P2-2: a writable parent covers the path with no mount of its own.
+ {name: "gvisor, writable parent", mode: "gvisor", pid: "4242", parent: true},
+ {name: "gvisor, malformed table", mode: "gvisor", pid: "4242", garbled: true},
{name: "gvisor, not running", mode: "gvisor", pid: "0"},
{name: "systemd", mode: "systemd", pid: "4242"},
} {
@@ -280,6 +284,13 @@ func TestSandboxUpdateControlRestartForNewRootfs(t *testing.T) {
if tc.mounted {
table += "40 22 259:1 " + rootfs + " " + rootfs + " rw,relatime shared:1 - ext4 /dev/root rw\n"
}
+ if tc.parent {
+ parent := filepath.Dir(rootfs)
+ table += "41 22 259:1 " + parent + " " + parent + " rw,relatime shared:1 - ext4 /dev/root rw\n"
+ }
+ if tc.garbled {
+ table += "42 22 259:1 " + rootfs + "\n"
+ }
return []byte(table), nil
}
probe := func(args ...string) (string, error) {
@@ -296,7 +307,12 @@ func TestSandboxUpdateControlRestartForNewRootfs(t *testing.T) {
calls = append(calls, strings.Join(args, " "))
return "", nil
}
+ // Repeated passes: a healthy namespace is never restarted, and an
+ // unhealthy one is asked to restart on each pass it stays unhealthy.
ensureSystemUnits(etc, filepath.Join(systemRootDir(opt), systemLockFile), call, probe, t.Logf)
+ if !tc.want {
+ ensureSystemUnits(etc, filepath.Join(systemRootDir(opt), systemLockFile), call, probe, t.Logf)
+ }
want := []string(nil)
if tc.want {
want = []string{"try-restart " + claimControlUnit}
@@ -307,3 +323,86 @@ func TestSandboxUpdateControlRestartForNewRootfs(t *testing.T) {
})
}
}
+
+// R1 P2-1: the new build's first pass may be an apply that installs
+// nothing (a no-op request, or the build already on disk). It too creates
+// both live directories; a group member without a permit does not.
+func TestSandboxUpdateApplyWithoutInstallCreatesDirectories(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ noOp bool
+ group bool
+ }{
+ {name: "same version"},
+ {name: "no-op request", noOp: true},
+ {name: "group without permit", group: true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ f := newApplyFixture(t, "stable", "v2")
+ f.promote("v2", []byte("#!/bin/sh\necho carlos v2\n"), tc.noOp)
+ f.answer["version:"+systemBinaryPath(f.a.optDir)] = "carlos v2\n"
+ if tc.group {
+ if err := os.MkdirAll(filepath.Join(f.a.etcDir, "carlos"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(f.a.etcDir, "carlos/group.json"), []byte("{}"), 0600); err != nil {
+ t.Fatal(err)
+ }
+ }
+ dirs := map[string]os.FileMode{filepath.Join(filepath.Dir(f.a.etcDir), "var/lib/carlos/oci-rootfs"): 0700, filepath.Join(f.a.optDir, "runsc"): 0755}
+ for path := range dirs {
+ if _, err := os.Lstat(path); !errors.Is(err, os.ErrNotExist) {
+ t.Fatalf("not an old box: %s: %v", path, err)
+ }
+ }
+ err := f.a.Run(context.Background(), false)
+ t.Logf("run: %v; result %+v", err, f.result())
+ if !tc.group {
+ if r := f.result(); r == nil || r.Outcome != systemOutcomeNoted {
+ t.Fatalf("result = %+v", r)
+ }
+ }
+ for path, mode := range dirs {
+ st, err := os.Lstat(path)
+ if tc.group {
+ if !errors.Is(err, os.ErrNotExist) {
+ t.Fatalf("group member without a permit created %s: %v", path, err)
+ }
+ continue
+ }
+ if err != nil || !st.IsDir() || st.Mode().Perm() != mode {
+ t.Fatalf("%s not created %04o: %v %v", path, mode, st, err)
+ }
+ }
+ })
+ }
+}
+
+func TestMountWritableJudgesTheContainingMount(t *testing.T) {
+ const path = "/var/lib/carlos/oci-rootfs"
+ root := "22 1 259:1 / / ro,relatime shared:1 - ext4 /dev/root rw\n"
+ for _, tc := range []struct {
+ name, table string
+ writable, known bool
+ }{
+ {"read-only root only", root, false, true},
+ {"exact rw mount", root + "40 22 259:1 /var/lib/carlos/oci-rootfs /var/lib/carlos/oci-rootfs rw,relatime - ext4 /dev/root rw\n", true, true},
+ {"rw parent", root + "41 22 259:1 /var/lib/carlos /var/lib/carlos rw,nosuid - ext4 /dev/root rw\n", true, true},
+ {"rw parent, ro child over it", root + "41 22 259:1 /var/lib/carlos /var/lib/carlos rw - ext4 /dev/root rw\n43 41 259:1 /var/lib/carlos/oci-rootfs /var/lib/carlos/oci-rootfs ro - ext4 /dev/root rw\n", false, true},
+ {"later mount over the same point wins", root + "40 22 259:1 / /var/lib/carlos/oci-rootfs rw - ext4 /dev/root rw\n44 40 259:1 / /var/lib/carlos/oci-rootfs ro - ext4 /dev/root rw\n", false, true},
+ {"sibling prefix is not a parent", root + "45 22 259:1 /var/lib/carlos-app /var/lib/carlos-app rw - ext4 /dev/root rw\n", false, true},
+ {"escaped mount point", root + "46 22 259:1 /x /var/lib/carlos\\040x rw - ext4 /dev/root rw\n", false, true},
+ {"truncated line", root + "47 22 259:1 /var/lib/carlos\n", false, false},
+ {"no separator", "22 1 259:1 / / rw,relatime shared:1 x ext4 /dev/root rw\n", false, false},
+ {"bad escape", root + "48 22 259:1 / /var/\\9zz rw - ext4 /dev/root rw\n", false, false},
+ {"empty", "", false, false},
+ {"no rw or ro option", "22 1 259:1 / / relatime - ext4 /dev/root rw\n", false, false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ w, k := mountWritable([]byte(tc.table), path)
+ if w != tc.writable || k != tc.known {
+ t.Fatalf("mountWritable = %v, %v; want %v, %v", w, k, tc.writable, tc.known)
+ }
+ })
+ }
+}
diff --git a/internal/host/systemapply.go b/internal/host/systemapply.go
index 6b34780f8..0d1814c2d 100644
--- a/internal/host/systemapply.go
+++ b/internal/host/systemapply.go
@@ -483,10 +483,12 @@ func (a *systemApplier) runLocked(ctx context.Context, recover bool, operation f
return err
}
if req.NoOp {
+ a.migrateSandboxDirectories()
return a.writeResult(req, systemOutcomeNoted, "")
}
installed := a.onDiskBuild(ctx)
if p.Current.Version == installed {
+ a.migrateSandboxDirectories()
return a.writeResult(req, systemOutcomeNoted, "")
}
// The rootfs floor is the console's (console/rootfsfloor.go): it judges
@@ -1459,6 +1461,23 @@ func (a *systemApplier) journal(j systemJournal) error {
return writeJSONDurable(a.file("journal.json"), j, 0o600)
}
+// migrateSandboxDirectories gives an apply that installs nothing (a no-op
+// request, or the build already on disk) the live sandbox directories
+// installUnits would have created: after an update by a build that never
+// made them (v0.34.33), the new build's first pass may be exactly such an
+// apply. A group member needs its maintenance permit, as installUnits
+// does. Failure is logged and leaves the outcome alone; the next adopt
+// pass tries again.
+func (a *systemApplier) migrateSandboxDirectories() {
+ if err := a.requireGroupMaintenance("apply", "rollback"); err != nil {
+ a.ops.logf("system apply: sandbox directories wait for a maintenance permit: %v", err)
+ return
+ }
+ if err := ensureSandboxDirectories(filepath.Dir(a.etcDir), a.optDir); err != nil {
+ a.ops.logf("system apply: sandbox directories: %v", err)
+ }
+}
+
// installUnits removes what the target build dropped, copies its units in,
// then sysusers BEFORE tmpfiles — tmpfiles silently drops a line whose
// group does not resolve, and carlos-edge never starts again after the