carlos / platform Public

generation census: the signed-image proof runs under the census deadline

Astra (round 2, finding 3): env.binary took no context, and its production
implementation readRootFloorBinary did an io.Copy hash and an identity-cache
scan with no cancellation, so a slow image or cache could run the census past
its 20 s window. readRootFloorBinary is now a wrapper over
readRootFloorBinaryContext: the hash stops between reads
(generationContextReader, moved beside it) and the identity scan stops before
each identity (rootFloorSignedIdentity) once the context ends. The service
lookup hands every proof (the deleted image, the guarded edge's guard and
selected images) the census pass's deadline context.

Witnesses: TestSignedBinaryProofStopsWhenTheCensusDeadlineEnds (both helpers),
TestRecoverCensusDeadlineReachesTheSignedBinaryProof (the lookup hands the
deadline to the proof, deleted-image and guarded-edge). Not witnessed by a
non-root test: the plumbing from readRootFloorBinaryContext into the helpers
(the proof needs root-owned files), so a mutant that drops the context inside
it survives; recorded in the census note.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 3e48c56374cdba8983bbc297abb27994e44e289a parent 7faac6e
3 files changed, +132 −29
  • internal/host/rootgeneration_census_recover_linux_test.go +83 −6
  • internal/host/rootgeneration_services_linux.go +5 −19
  • internal/host/rootmovesfloor_refresh.go +44 −4
diff --git a/internal/host/rootgeneration_census_recover_linux_test.go b/internal/host/rootgeneration_census_recover_linux_test.go
index e460e9579..8a5f0c2c3 100644
--- a/internal/host/rootgeneration_census_recover_linux_test.go
+++ b/internal/host/rootgeneration_census_recover_linux_test.go
@@ -71,7 +71,7 @@ func deletedExe(t *testing.T) *deletedExeFixture {
return map[string]string{"ControlGroup": group, "MainPID": f.mainPID, "ExecStart": f.execStart, "FragmentPath": "/etc/systemd/system/" + unit}, nil
},
trust: func(string) error { return nil },
- binary: func(opt, path string) (rootFloorBinary, error) {
+ binary: func(_ context.Context, opt, path string) (rootFloorBinary, error) {
switch path {
case filepath.Join(opt, "carlos"):
return rootFloorBinary{Digest: f.current}, nil
@@ -161,11 +161,11 @@ func TestRecoverCensusAcceptsTheEdgesDeletedExeInSupervise(t *testing.T) {
if guarded {
f.execStart = "{ path=/opt/carlos/system/guard/carlos ; argv[]=/opt/carlos/system/guard/carlos system guard-exec edge ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=41 ; code=(null) ; status=0/0 }"
binary := f.env.binary
- f.env.binary = func(opt, path string) (rootFloorBinary, error) {
+ f.env.binary = func(ctx context.Context, opt, path string) (rootFloorBinary, error) {
if path == "/opt/carlos/system/guard/carlos" {
return rootFloorBinary{Digest: "guard"}, nil
}
- b, err := binary(opt, path)
+ b, err := binary(ctx, opt, path)
b.File.Inode = 7 // the live <opt>/carlos, not the deleted image
return b, err
}
@@ -201,7 +201,9 @@ func TestRecoverCensusRefusesOtherDeletedExes(t *testing.T) {
case "unsigned":
f.writeImage(t, "some other build", 0o755, true)
case "no-proof":
- f.env.binary = func(string, string) (rootFloorBinary, error) { return rootFloorBinary{}, errRootFloorHistory }
+ f.env.binary = func(context.Context, string, string) (rootFloorBinary, error) {
+ return rootFloorBinary{}, errRootFloorHistory
+ }
case "linked":
f.writeImage(t, deletedExeBody, 0o755, false)
case "group-writable":
@@ -643,11 +645,11 @@ func (f *deletedExeFixture) guardedEdge(t *testing.T) {
f.unit = "carlos-edge.service"
f.execStart = "{ path=/opt/carlos/system/guard/carlos ; argv[]=/opt/carlos/system/guard/carlos system guard-exec edge ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=41 ; code=(null) ; status=0/0 }"
binary := f.env.binary
- f.env.binary = func(opt, path string) (rootFloorBinary, error) {
+ f.env.binary = func(ctx context.Context, opt, path string) (rootFloorBinary, error) {
if path == "/opt/carlos/system/guard/carlos" {
return rootFloorBinary{Digest: "guard"}, nil
}
- b, err := binary(opt, path)
+ b, err := binary(ctx, opt, path)
b.File.Inode = 7
return b, err
}
@@ -713,3 +715,78 @@ func TestRecoverCensusDeletedExeOfARootTask(t *testing.T) {
}
})
}
+
+// slowImage returns one byte per read and ends ctx after the first.
+type slowImage struct {
+ cancel func()
+ reads int
+}
+
+func (s *slowImage) Read(p []byte) (int, error) {
+ s.reads++
+ if s.reads == 1 {
+ s.cancel()
+ }
+ if len(p) == 0 {
+ return 0, nil
+ }
+ p[0] = 'x'
+ return 1, nil
+}
+
+func TestSignedBinaryProofStopsWhenTheCensusDeadlineEnds(t *testing.T) {
+ // The hash: a slow image whose read outlives the deadline.
+ ctx, cancel := context.WithCancel(t.Context())
+ image := &slowImage{cancel: cancel}
+ if _, err := rootFloorDigest(ctx, image); !errors.Is(err, context.Canceled) || image.reads > 2 {
+ t.Fatalf("digest = %v after %d reads, want it stopped at the deadline", err, image.reads)
+ }
+ // The identity scan: once the deadline has passed it reads no identity.
+ reads := 0
+ ctx, cancel = context.WithCancel(t.Context())
+ cancel()
+ if _, err := rootFloorSignedIdentity(ctx, nil, rootFloorBinary{Digest: "d"}, []string{"a.json"}, func(string) ([]byte, error) { reads++; return nil, nil }); !errors.Is(err, context.Canceled) || reads != 0 {
+ t.Fatalf("identity scan after the deadline = %v after %d reads", err, reads)
+ }
+}
+
+func TestRecoverCensusDeadlineReachesTheSignedBinaryProof(t *testing.T) {
+ // The lookup hands the census's deadline context to the signed-binary
+ // proof, for the deleted image and for the guarded edge's guard and
+ // selected images: a proof that only returns when that context ends
+ // is cut off at the deadline.
+ for _, guarded := range []bool{false, true} {
+ t.Run(map[bool]string{false: "deleted-image", true: "guarded-edge"}[guarded], func(t *testing.T) {
+ f := deletedExe(t)
+ if guarded {
+ f.guardedEdge(t)
+ }
+ proofs := 0
+ f.env.binary = func(ctx context.Context, opt, path string) (rootFloorBinary, error) {
+ proofs++
+ if _, ok := ctx.Deadline(); !ok {
+ t.Error("signed-binary proof ran without the census deadline")
+ }
+ select {
+ case <-ctx.Done():
+ return rootFloorBinary{}, ctx.Err()
+ case <-time.After(5 * time.Second):
+ return rootFloorBinary{}, errors.New("proof outlived the deadline")
+ }
+ }
+ start := time.Now()
+ _, _, err := generationCensusRetryFor(t.Context(), func(ctx context.Context) (string, string, error) {
+ return "boot", "digest", f.kernelCensus(ctx)
+ }, 100*time.Millisecond, time.Now, generationSleep)
+ if !errors.Is(err, errGenerationCensusLate) {
+ t.Fatalf("census = %v, want a late refusal", err)
+ }
+ if took := time.Since(start); took > 2*time.Second {
+ t.Fatalf("census took %s past a 100 ms window: the proof ignored its context", took)
+ }
+ if proofs == 0 {
+ t.Fatal("the proof never ran")
+ }
+ })
+ }
+}
diff --git a/internal/host/rootgeneration_services_linux.go b/internal/host/rootgeneration_services_linux.go
index fce85e3ed..9075333ef 100644
--- a/internal/host/rootgeneration_services_linux.go
+++ b/internal/host/rootgeneration_services_linux.go
@@ -38,7 +38,7 @@ type generationServiceEnv struct {
owner uint32
show func(ctx context.Context, unit string) (map[string]string, error)
trust func(path string) error
- binary func(opt, path string) (rootFloorBinary, error)
+ binary func(ctx context.Context, opt, path string) (rootFloorBinary, error)
}
// generationServiceRefusal is a refusal of the service lookup. It names the
@@ -61,7 +61,7 @@ func (r *generationServiceRefusal) Unwrap() error { return r.err }
// task, and recheck the executable binding of a task it trusted
// (generationKernel.recheck).
func generationServiceReader(ctx context.Context) (func(string, []byte) (bool, error), func(string, bool) error) {
- return generationServiceEnv{proc: "/proc", opt: "/opt/carlos", show: generationServiceShow, trust: generationServiceTrust, binary: readRootFloorBinary}.reader(ctx)
+ return generationServiceEnv{proc: "/proc", opt: "/opt/carlos", show: generationServiceShow, trust: generationServiceTrust, binary: readRootFloorBinaryContext}.reader(ctx)
}
// generationExeID is what binds a task to the image it was proven to run:
@@ -146,7 +146,7 @@ func (env generationServiceEnv) reader(ctx context.Context) (func(string, []byte
proved = true
var errs []error
for _, name := range []string{"carlos", "carlos.prev"} {
- b, err := env.binary(env.opt, filepath.Join(env.opt, name))
+ b, err := env.binary(ctx, env.opt, filepath.Join(env.opt, name))
if err != nil {
if name == "carlos" || !errors.Is(err, os.ErrNotExist) {
errs = append(errs, fmt.Errorf("%s: %w", name, err))
@@ -227,10 +227,10 @@ func (env generationServiceEnv) reader(ctx context.Context) (func(string, []byte
if !generationGuardEdgeStart(unit, props["ExecStart"]) {
return false, refuse("guard start", os.ErrPermission)
}
- if _, err := env.binary(env.opt, s.executable); err != nil {
+ if _, err := env.binary(ctx, env.opt, s.executable); err != nil {
return false, refuse("guard binary", err)
}
- selected, err := env.binary(env.opt, filepath.Join(env.opt, "carlos"))
+ selected, err := env.binary(ctx, env.opt, filepath.Join(env.opt, "carlos"))
if err != nil {
return false, refuse("selected binary", err)
}
@@ -392,20 +392,6 @@ func (env generationServiceEnv) deletedProgram(ctx context.Context, task, progra
return f, nil
}
-// generationContextReader stops a long read, the image hash, when the
-// census's deadline passes.
-type generationContextReader struct {
- ctx context.Context
- r io.Reader
-}
-
-func (c generationContextReader) Read(p []byte) (int, error) {
- if err := c.ctx.Err(); err != nil {
- return 0, err
- }
- return c.r.Read(p)
-}
-
func generationGuardEdgeStart(unit, start string) bool {
if unit != "carlos-edge.service" || strings.Count(start, "{") != 1 || strings.Count(start, "}") != 1 {
return false
diff --git a/internal/host/rootmovesfloor_refresh.go b/internal/host/rootmovesfloor_refresh.go
index 2dfd7b464..1edffebba 100644
--- a/internal/host/rootmovesfloor_refresh.go
+++ b/internal/host/rootmovesfloor_refresh.go
@@ -2,6 +2,7 @@ package host
import (
"context"
+ "crypto/ed25519"
"crypto/sha256"
"encoding/hex"
"encoding/json"
@@ -149,6 +150,14 @@ func rootFloorAdvertisementFor(public rootstate.Store, p rootFloorProjection) (r
// A binary's signed identity, not its mutable version sidecar, supplies its
// release and protocol. The cache is root-owned; each envelope is reverified.
func readRootFloorBinary(opt, path string) (rootFloorBinary, error) {
+ return readRootFloorBinaryContext(context.Background(), opt, path)
+}
+
+// readRootFloorBinaryContext is readRootFloorBinary under ctx: the hash of
+// the image stops between reads, and the scan of the identity cache before
+// each identity, once ctx ends. The generation census's service lookup
+// proves signed images with it under the census deadline.
+func readRootFloorBinaryContext(ctx context.Context, opt, path string) (rootFloorBinary, error) {
var b rootFloorBinary
f, err := rootstate.OpenAuthorityFile(path)
if err != nil {
@@ -166,11 +175,9 @@ func readRootFloorBinary(opt, path string) (rootFloorBinary, error) {
if st.Size() > 512<<20 || st.Mode()&0111 == 0 {
return b, os.ErrPermission
}
- sum := sha256.New()
- if _, err := io.Copy(sum, f); err != nil {
+ if b.Digest, err = rootFloorDigest(ctx, f); err != nil {
return b, err
}
- b.Digest = hex.EncodeToString(sum.Sum(nil))
pub, err := release.ParsePublicKey(os.Getenv("CARLOS_RELEASE_PUBKEY"), "CARLOS_RELEASE_PUBKEY")
if err != nil {
return b, err
@@ -184,8 +191,27 @@ func readRootFloorBinary(opt, path string) (rootFloorBinary, error) {
if err != nil {
return b, err
}
+ return rootFloorSignedIdentity(ctx, pub, b, names, identities.Read)
+}
+
+// rootFloorDigest is the sha256 of an image, read through ctx.
+func rootFloorDigest(ctx context.Context, image io.Reader) (string, error) {
+ sum := sha256.New()
+ if _, err := io.Copy(sum, generationContextReader{ctx: ctx, r: image}); err != nil {
+ return "", err
+ }
+ return hex.EncodeToString(sum.Sum(nil)), nil
+}
+
+// rootFloorSignedIdentity finds the cached signed identity whose router
+// artifact for this platform is b's digest, verifying each envelope, and
+// stops before the next identity once ctx ends.
+func rootFloorSignedIdentity(ctx context.Context, pub ed25519.PublicKey, b rootFloorBinary, names []string, read func(string) ([]byte, error)) (rootFloorBinary, error) {
for _, name := range names {
- raw, err := identities.Read(name)
+ if err := ctx.Err(); err != nil {
+ return b, err
+ }
+ raw, err := read(name)
if err != nil {
return b, err
}
@@ -201,6 +227,20 @@ func readRootFloorBinary(opt, path string) (rootFloorBinary, error) {
return b, errRootFloorHistory
}
+// generationContextReader stops a long read, an image hash, once its
+// context ends.
+type generationContextReader struct {
+ ctx context.Context
+ r io.Reader
+}
+
+func (c generationContextReader) Read(p []byte) (int, error) {
+ if err := c.ctx.Err(); err != nil {
+ return 0, err
+ }
+ return c.r.Read(p)
+}
+
func rootFloorDowngradeIdle(ctx context.Context) error {
l, held, err := rootparticipation.TransactionLedger(ctx)
if err != nil {