carlos / platform Public

db forward round 1, fix 1 (root runner): a real tenant unit cannot reach or start a db-read worker

TestRootDBReadTenantCannotReachTheWorker installs the production tenant
template (carlos-instance@.service, its sandbox line for line, ExecStart
this binary as a witness) and shows the tenant, running as carlos, can
connect to neither the worker socket nor the old path, the socket's
NAccepted does not move and no worker starts: with db reads armed (the
agent's probe passing) and with CARLOS_DB_READS=0 and no hibernation (the
adopt pass leaves the socket stopped and disabled, and nothing answers the
agent either). The runner's host.env now arms db reads, since the socket
follows the gate.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 178ef7b061bf0c438a9b182af6890bc3cd5971c4 parent e105041
3 files changed, +187 −2
  • internal/host/dbreads_rootrunner_linux_test.go +160 −2
  • internal/host/dbreads_worker_dispatch_linux_test.go +22 −0
  • internal/host/plan14_generation_registry_root_linux_test.go +5 −0
diff --git a/internal/host/dbreads_rootrunner_linux_test.go b/internal/host/dbreads_rootrunner_linux_test.go
index 36af9db4c..16d5b61d6 100644
--- a/internal/host/dbreads_rootrunner_linux_test.go
+++ b/internal/host/dbreads_rootrunner_linux_test.go
@@ -126,7 +126,10 @@ func newDBReadRunner(t *testing.T, tmpSize string) *dbreadRunner {
sqliteFixture(t, "/data/registry.db")
sqliteFixture(t, "/etc/carlos/witness.db")
sqliteFixture(t, "/data/instances/other.example.com/other.db")
- if err := os.WriteFile("/etc/carlos/host.env", []byte("CARLOS_REGION=uk\n"), 0o640); err != nil {
+ // host.env arms db reads (dbReadsSettings), so the adopt pass's
+ // ensureSystemUnits arms the socket: it stays dark otherwise.
+ r.rootPub, r.rootPriv, _ = ed25519.GenerateKey(rand.Reader)
+ if err := os.WriteFile("/etc/carlos/host.env", []byte(dbreadRunnerHostEnv(r.rootPub)), 0o640); err != nil {
t.Fatal(err)
}
if err := os.WriteFile("/opt/carlos/carlos", self, 0o755); err != nil {
@@ -159,7 +162,6 @@ func newDBReadRunner(t *testing.T, tmpSize string) *dbreadRunner {
t.Fatal(err)
}
t.Cleanup(func() { os.RemoveAll(r.dir) })
- r.rootPub, r.rootPriv, _ = ed25519.GenerateKey(rand.Reader)
replica := filepath.Join(r.dir, "replica", "instances", dbreadRunnerHost)
dbdoortest.Replicate(t, replica, 3)
yes := true
@@ -173,6 +175,12 @@ func newDBReadRunner(t *testing.T, tmpSize string) *dbreadRunner {
return r
}
+// dbreadRunnerHostEnv is a host.env that arms db reads, plus extra lines.
+func dbreadRunnerHostEnv(pub ed25519.PublicKey, extra ...string) string {
+ lines := append([]string{"CARLOS_REGION=uk", "CARLOS_HIBERNATE_BUCKET=data-uk", "CARLOS_RELEASE_PUBKEY=" + base64.StdEncoding.EncodeToString(pub)}, extra...)
+ return strings.Join(lines, "\n") + "\n"
+}
+
func (r *dbreadRunner) store() release.Store {
return release.NewDirStore(filepath.Join(r.dir, "store"))
}
@@ -656,3 +664,153 @@ func TestRootDBReadPayload(t *testing.T) {
}
say()
}
+
+// dbreadTenantUnit is the real tenant template, carlos-instance@.service,
+// installed under its own name with only what a runner cannot provide taken
+// out: the edge and mount dependencies, the per-instance slice, the stop
+// hook and the log socket. Its sandbox (User=carlos, the hidden /run/carlos,
+// /data, /opt/carlos and /etc/carlos, the address families) is the
+// production one, line for line. ExecStart is this binary as a tenant that
+// tries to reach the db-read worker.
+const dbreadTenantUnit = "carlos-dbread-tenant@.service"
+
+func installDBReadTenantUnit(t *testing.T, instance string) {
+ t.Helper()
+ raw, err := unitsFS.ReadFile("units/carlos-instance@.service")
+ if err != nil {
+ t.Fatal(err)
+ }
+ var out []string
+ for _, line := range strings.Split(string(raw), "\n") {
+ switch {
+ case strings.HasPrefix(line, "BindsTo="), strings.HasPrefix(line, "Requires="), strings.HasPrefix(line, "After="),
+ strings.HasPrefix(line, "ExecStopPost="), strings.HasPrefix(line, "Slice="):
+ continue
+ case strings.HasPrefix(line, "ExecStart="):
+ line = "ExecStart=/opt/carlos/carlos dbread-tenant-witness"
+ case line == "Type=simple":
+ line = "Type=oneshot"
+ case strings.HasPrefix(line, "StandardOutput=file:"):
+ line = "StandardOutput=journal"
+ }
+ out = append(out, line)
+ }
+ unit := strings.Join(out, "\n")
+ if !strings.Contains(unit, "TemporaryFileSystem=/run/carlos:ro") || !strings.Contains(unit, "User=carlos") {
+ t.Fatal("the tenant template no longer hides /run/carlos as carlos; the witness would prove nothing")
+ }
+ path := filepath.Join("/etc/systemd/system", dbreadTenantUnit)
+ if err := os.WriteFile(path, []byte(unit), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { os.Remove(path); runSystemctl("daemon-reload") })
+ mustSystemctl(t, "daemon-reload")
+ uid, gid := carlosIDs(t)
+ for _, d := range []string{"/data/instances/" + instance, "/run/carlos/instances/" + instance} {
+ if err := os.MkdirAll(d, 0o750); err != nil {
+ t.Fatal(err)
+ }
+ os.Chown(d, uid, gid)
+ }
+ for _, d := range []string{"/run/carlos-control/exe/" + instance + "/app", "/run/carlos-control/exe/" + instance + "/live"} {
+ if err := os.MkdirAll(d, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ }
+ t.Cleanup(func() {
+ os.RemoveAll("/data/instances/" + instance)
+ os.RemoveAll("/run/carlos/instances/" + instance)
+ os.RemoveAll("/run/carlos-control/exe/" + instance)
+ })
+}
+
+func carlosIDs(t *testing.T) (int, int) {
+ t.Helper()
+ u, err := user.Lookup("carlos")
+ if err != nil {
+ t.Fatal(err)
+ }
+ uid, _ := strconv.Atoi(u.Uid)
+ gid, _ := strconv.Atoi(u.Gid)
+ return uid, gid
+}
+
+// tenantReach runs the installed tenant once and returns its report, after
+// checking that no connection reached the socket and no worker started.
+func tenantReach(t *testing.T, instance string) map[string]string {
+ t.Helper()
+ accepted := func() string {
+ out, _ := runSystemctl("show", "-p", "NAccepted", "--value", dbReadSocketUnit)
+ return strings.TrimSpace(out)
+ }
+ before := accepted()
+ report := filepath.Join("/data/instances", instance, "dbread-witness.json")
+ os.Remove(report)
+ if out, err := runSystemctl("start", strings.Replace(dbreadTenantUnit, "@", "@"+instance, 1)); err != nil {
+ jout, _ := exec.Command("journalctl", "-u", strings.Replace(dbreadTenantUnit, "@", "@"+instance, 1), "-n", "30", "--no-pager").CombinedOutput()
+ t.Fatalf("the tenant unit did not run: %v: %s\n%s", err, out, jout)
+ }
+ raw, err := os.ReadFile(report)
+ if err != nil {
+ t.Fatalf("the tenant wrote no report: %v", err)
+ }
+ var rep map[string]string
+ if err := json.Unmarshal(raw, &rep); err != nil {
+ t.Fatal(err)
+ }
+ t.Logf("tenant report: %v", rep)
+ if rep["uid"] == "" || rep["uid"] == "0" {
+ t.Fatalf("the witness did not run as the tenant user: %v", rep)
+ }
+ for _, k := range []string{"connect " + dbReadSocketPath, "connect /run/carlos-dbread/worker.sock"} {
+ if v, ok := rep[k]; !ok || v == "" {
+ t.Errorf("a tenant could %s: %v", k, rep)
+ }
+ }
+ if after := accepted(); after != before {
+ t.Errorf("the socket accepted a tenant connection: NAccepted %s -> %s", before, after)
+ }
+ if n := unitsActive(t); n != 0 {
+ t.Errorf("%d db-read workers running after the tenant's attempt", n)
+ }
+ return rep
+}
+
+// Astra round 1, finding 1, on real units: a real tenant unit (the
+// production carlos-instance@ sandbox, running as carlos) can neither
+// connect to nor activate a db-read worker, with db reads armed on the box
+// and with them off; and with them off nothing listens, for the agent
+// either. Mutations: put the socket back at /run/carlos-dbread (the tenant
+// connects, NAccepted moves); drop the gate in ensureSystemUnits (the
+// socket listens with CARLOS_DB_READS=0).
+func TestRootDBReadTenantCannotReachTheWorker(t *testing.T) {
+ r := newDBReadRunner(t, "40g")
+ const instance = "tenant.example.com"
+ installDBReadTenantUnit(t, instance)
+
+ // Armed: the socket listens, the agent's probe passes, the tenant
+ // reaches nothing.
+ if out, _ := systemctlProbe("is-active", dbReadSocketUnit); strings.TrimSpace(out) != "active" {
+ t.Fatalf("armed box: carlos-dbread.socket is %q", out)
+ }
+ if rep, _ := r.payload("probe", true, ""); rep["capable"] != true {
+ t.Fatalf("armed box: the agent's probe failed: %v", rep)
+ }
+ tenantReach(t, instance)
+
+ for _, off := range [][]string{{"CARLOS_DB_READS=0"}, {"CARLOS_HIBERNATE_BUCKET="}} {
+ if err := os.WriteFile("/etc/carlos/host.env", []byte(dbreadRunnerHostEnv(r.rootPub, off...)), 0o640); err != nil {
+ t.Fatal(err)
+ }
+ ensureSystemUnits("/etc", filepath.Join(t.TempDir(), "lock"), runSystemctl, systemctlProbe, t.Logf)
+ active, _ := systemctlProbe("is-active", dbReadSocketUnit)
+ enabled, _ := systemctlProbe("is-enabled", dbReadSocketUnit)
+ if strings.TrimSpace(active) == "active" || strings.TrimSpace(enabled) == "enabled" {
+ t.Fatalf("%v: carlos-dbread.socket is %s and %s", off, strings.TrimSpace(active), strings.TrimSpace(enabled))
+ }
+ if rep, _ := r.payload("probe", true, ""); rep["capable"] == true {
+ t.Fatalf("%v: something still answers on the worker socket: %v", off, rep)
+ }
+ tenantReach(t, instance)
+ }
+}
diff --git a/internal/host/dbreads_worker_dispatch_linux_test.go b/internal/host/dbreads_worker_dispatch_linux_test.go
index 7d9c819ef..ddfca5279 100644
--- a/internal/host/dbreads_worker_dispatch_linux_test.go
+++ b/internal/host/dbreads_worker_dispatch_linux_test.go
@@ -10,6 +10,7 @@ import (
"net"
"os"
"path/filepath"
+ "strconv"
"strings"
"time"
@@ -151,3 +152,24 @@ func dbreadsParseNetDev(s string) []string {
}
return out
}
+
+// dbreadsTenantWitness is this test binary as a tenant inside the installed
+// tenant unit (TestRootDBReadTenantCannotReachTheWorker): it tries every way
+// to the db-read worker a tenant running as carlos might have, and writes
+// what happened ("" for success) to its working directory.
+func dbreadsTenantWitness() {
+ report := map[string]string{"uid": strconv.Itoa(os.Getuid())}
+ for _, p := range []string{dbReadSocketPath, "/run/carlos-dbread/worker.sock"} {
+ c, err := net.DialTimeout("unix", p, 2*time.Second)
+ if err == nil {
+ c.Close()
+ report["connect "+p] = ""
+ } else {
+ report["connect "+p] = err.Error()
+ }
+ }
+ b, _ := json.Marshal(report)
+ if err := os.WriteFile("dbread-witness.json", b, 0o600); err != nil {
+ os.Exit(1)
+ }
+}
diff --git a/internal/host/plan14_generation_registry_root_linux_test.go b/internal/host/plan14_generation_registry_root_linux_test.go
index dd5c04fc5..8b0114686 100644
--- a/internal/host/plan14_generation_registry_root_linux_test.go
+++ b/internal/host/plan14_generation_registry_root_linux_test.go
@@ -22,6 +22,11 @@ func TestMain(m *testing.M) {
dbreadsTestWorker()
return
}
+ // A tenant inside the installed tenant unit, reaching for the worker.
+ if len(os.Args) == 2 && os.Args[1] == "dbread-tenant-witness" {
+ dbreadsTenantWitness()
+ return
+ }
// The agent half of the db-read root-runner tests, run as carlos.
if os.Getenv("DBREAD_RUNNER_PAYLOAD") != "" {
flag.Set("test.run", "^TestRootDBReadPayload$")