carlos / platform Public

generation census: bind a deleted-exe task to the image it was proven to run

Astra (fix round 1, finding 2): opening /proc/<pid>/exe pinned the image
hashed, not the task's executable. A task could exec an unsigned helper
after the hash and keep its trusted classification to the end of the
inspection; the final start-time check does not change across exec.

- deletedProgram keeps the hashed /proc/<pid>/exe descriptor open, and
  the lookup binds the task to that image's identity: device, inode,
  mount ID and, where the filesystem gives one, the kernel file handle.
- generationKernel.task rechecks the binding before each use of the
  trust (the namespace compare, the mount check) and at the end of the
  inspection: /proc/<pid>/exe, opened again, must be the same inode,
  still root-owned, unlinked and not group or other writable. A task
  that exec'd anything else refuses with a transient "exe binding"
  refusal a later pass can clear. The final recheck releases the
  descriptor.
- A kernel given a lookup without a recheck refuses every task the
  lookup trusts, so a call site that forgets it fails closed. The three
  production call sites wire both.

No execution exclusion exists for a service task's own exec: the launch
exclusion (participationRelease, Ledger.ExcludeLaunches) covers claim
launches only. Rechecks narrow the window; they do not close it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 31e5b011858c834162cb0715e96e3c2d5dbe5b59 parent b1b1cd0
6 files changed, +297 −23
  • internal/host/plan14_generation_kernel_linux_test.go +1 −1
  • internal/host/rootgeneration_census_linux.go +2 −1
  • internal/host/rootgeneration_census_recover_linux_test.go +128 −4
  • internal/host/rootgeneration_kernel_linux.go +30 −1
  • internal/host/rootgeneration_retired_linux.go +2 −1
  • internal/host/rootgeneration_services_linux.go +134 −15
diff --git a/internal/host/plan14_generation_kernel_linux_test.go b/internal/host/plan14_generation_kernel_linux_test.go
index 687efe2d0..4e17551a4 100644
--- a/internal/host/plan14_generation_kernel_linux_test.go
+++ b/internal/host/plan14_generation_kernel_linux_test.go
@@ -279,7 +279,7 @@ func TestPlan14GenerationServiceReferences(t *testing.T) {
case "target mount":
generationWrite(t, f.task+"/mountinfo", fmt.Sprintf("1 0 %d:%d / / rw - ext4 disk rw\n2 1 %d:%d %s /bound ro - ext4 disk ro\n", generationMajor(b.Executable.Device), generationMinor(b.Executable.Device), generationMajor(b.Executable.Device), generationMinor(b.Executable.Device), b.Tree))
}
- k := generationKernel{proc: f.proc, service: func(string, []byte) (bool, error) { return mode != "unknown", nil }}
+ k := generationKernel{proc: f.proc, service: func(string, []byte) (bool, error) { return mode != "unknown", nil }, recheck: func(string, bool) error { return nil }}
_, err := k.absent(t.Context(), f.reservation)
if (err == nil) != (mode == "service") {
t.Fatalf("service reference boundary: %v", err)
diff --git a/internal/host/rootgeneration_census_linux.go b/internal/host/rootgeneration_census_linux.go
index d1dcb5681..361176102 100644
--- a/internal/host/rootgeneration_census_linux.go
+++ b/internal/host/rootgeneration_census_linux.go
@@ -98,7 +98,8 @@ func productionGenerationKernelCensus(ctx context.Context) (boot, digest string,
}
records, _ := ctx.Value(generationCensusReservationsKey{}).([]release.GenerationReservation)
return generationCensusRetry(ctx, func() (string, string, error) {
- return (generationKernel{proc: "/proc", cgroups: "/sys/fs/cgroup", contained: records, service: generationServiceReader(ctx)}).census(ctx, tree)
+ service, recheck := generationServiceReader(ctx)
+ return (generationKernel{proc: "/proc", cgroups: "/sys/fs/cgroup", contained: records, service: service, recheck: recheck}).census(ctx, tree)
})
}
diff --git a/internal/host/rootgeneration_census_recover_linux_test.go b/internal/host/rootgeneration_census_recover_linux_test.go
index f1477d9ce..ffd660207 100644
--- a/internal/host/rootgeneration_census_recover_linux_test.go
+++ b/internal/host/rootgeneration_census_recover_linux_test.go
@@ -35,6 +35,8 @@ type deletedExeFixture struct {
tt *testing.T
// deletedName is the exe link's base name; "" is "carlos (deleted)".
deletedName string
+ // imageFile is the open descriptor of the task's current image.
+ imageFile *os.File
}
func (f *deletedExeFixture) t() *testing.T { return f.tt }
@@ -100,6 +102,7 @@ func (f *deletedExeFixture) writeImage(t *testing.T, body string, mode os.FileMo
t.Fatal(err)
}
t.Cleanup(func() { img.Close() })
+ f.imageFile = img
if unlink {
if err := os.Remove(path); err != nil {
t.Fatal(err)
@@ -116,10 +119,17 @@ func (f *deletedExeFixture) writeImage(t *testing.T, body string, mode os.FileMo
generationLink(t, deleted, f.task+"/exe")
}
+// kernelCensus is one census pass through the production lookup, as
+// productionGenerationKernelCensus wires it: classify and recheck.
+func (f *deletedExeFixture) kernelCensus(ctx context.Context) error {
+ service, recheck := f.env.reader(ctx)
+ _, err := (generationKernel{proc: f.proc, service: service, recheck: recheck}).absent(ctx, f.reservation)
+ return err
+}
+
func (f *deletedExeFixture) census(t *testing.T) error {
t.Helper()
- _, err := (generationKernel{proc: f.proc, service: f.env.reader(t.Context())}).absent(t.Context(), f.reservation)
- return err
+ return f.kernelCensus(t.Context())
}
func TestRecoverCensusAcceptsADeletedExeOfTheCurrentOrPreviousBuild(t *testing.T) {
@@ -316,7 +326,7 @@ func TestRecoverCensusRetriesATransientRefusalWithinTheWindow(t *testing.T) {
if passes == 4 {
f.mainPID = "41"
}
- _, err := (generationKernel{proc: f.proc, service: f.env.reader(t.Context())}).absent(t.Context(), f.reservation)
+ err := f.kernelCensus(t.Context())
return "boot", "digest", err
}
if _, _, err := generationCensusRetryFor(t.Context(), inspect, 20*time.Second, now, sleep); err != nil {
@@ -332,7 +342,7 @@ func TestRecoverCensusRetriesATransientRefusalWithinTheWindow(t *testing.T) {
persistent := func() (string, string, error) {
passes++
f.mainPID = "0"
- _, err := (generationKernel{proc: f.proc, service: f.env.reader(t.Context())}).absent(t.Context(), f.reservation)
+ err := f.kernelCensus(t.Context())
return "", "", err
}
_, _, err := generationCensusRetryFor(t.Context(), persistent, 20*time.Second, now, sleep)
@@ -401,3 +411,117 @@ func TestProductionGenerationCensusRefusalsAreTyped(t *testing.T) {
t.Fatalf("census refusal = %#v, want a typed census refusal", err)
}
}
+
+// execRace runs one census pass through the production lookup with a hook
+// around each of its rechecks: before(final) runs before the recheck and
+// after(final) after it. The hooks stand in for the task exec'ing (the
+// same PID, the same start time) while the census inspects it.
+func (f *deletedExeFixture) execRace(t *testing.T, after func(final bool, call int), before func(final bool, call int)) error {
+ t.Helper()
+ classify, recheck := f.env.reader(t.Context())
+ calls := 0
+ hooked := func(path string, final bool) error {
+ calls++
+ if before != nil {
+ before(final, calls)
+ }
+ err := recheck(path, final)
+ if after != nil {
+ after(final, calls)
+ }
+ return err
+ }
+ _, err := (generationKernel{proc: f.proc, service: classify, recheck: hooked}).absent(t.Context(), f.reservation)
+ return err
+}
+
+func TestRecoverCensusRefusesATaskThatExecsAfterItsImageWasProven(t *testing.T) {
+ helper := func(f *deletedExeFixture) { generationRelink(t, f.unrelated, f.task+"/exe") }
+ for _, tc := range []struct {
+ name string
+ race func(f *deletedExeFixture) (after, before func(bool, int))
+ }{
+ // The task execs an unsigned helper between the hash and the first
+ // use of the trust.
+ {"after-classification", func(f *deletedExeFixture) (func(bool, int), func(bool, int)) {
+ return nil, func(_ bool, call int) {
+ if call == 1 {
+ helper(f)
+ }
+ }
+ }},
+ // It execs another image that reads the same, "<opt>/carlos
+ // (deleted)" with signed bytes, but is not the inode hashed.
+ {"same-name-other-inode", func(f *deletedExeFixture) (func(bool, int), func(bool, int)) {
+ return nil, func(_ bool, call int) {
+ if call == 1 {
+ f.writeImage(t, deletedExeBody, 0o755, true)
+ }
+ }
+ }},
+ // It execs the helper after the first check and is back on the
+ // proven image by the end: the check at the mounts use sees it.
+ {"between-uses", func(f *deletedExeFixture) (func(bool, int), func(bool, int)) {
+ target, err := os.Readlink(f.task + "/exe")
+ if err != nil {
+ t.Fatal(err)
+ }
+ return func(final bool, call int) {
+ if !final && call == 1 {
+ helper(f)
+ }
+ }, func(final bool, _ int) {
+ if final {
+ generationRelink(t, target, f.task+"/exe")
+ }
+ }
+ }},
+ // The proven image itself is made group-writable mid-inspection.
+ {"image-made-writable", func(f *deletedExeFixture) (func(bool, int), func(bool, int)) {
+ return nil, func(_ bool, call int) {
+ if call == 1 {
+ if err := f.imageFile.Chmod(0o775); err != nil {
+ t.Fatal(err)
+ }
+ }
+ }
+ }},
+ // It execs the helper after the last use, before the end.
+ {"before-the-end", func(f *deletedExeFixture) (func(bool, int), func(bool, int)) {
+ return func(final bool, call int) {
+ if !final && call == 2 {
+ helper(f)
+ }
+ }, nil
+ }},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ f := deletedExe(t)
+ if err := f.execRace(t, nil, nil); err != nil {
+ t.Fatalf("census without a race refused: %v", err)
+ }
+ after, before := tc.race(f)
+ err := f.execRace(t, after, before)
+ if err == nil {
+ t.Fatal("census accepted a task that exec'd away from its proven image")
+ }
+ if !strings.Contains(err.Error(), "exe binding check refused") {
+ t.Fatalf("refusal %q does not name the exe binding check", err)
+ }
+ var task *generationTaskRefusal
+ if !errors.As(err, &task) || !task.transient() {
+ t.Fatalf("refusal %v is not a transient task refusal a later pass can clear", err)
+ }
+ })
+ }
+}
+
+func TestRecoverCensusNeedsABindingForATrustedTask(t *testing.T) {
+ // A kernel wired with a lookup but no recheck refuses every task the
+ // lookup trusts.
+ f := deletedExe(t)
+ classify, _ := f.env.reader(t.Context())
+ if _, err := (generationKernel{proc: f.proc, service: classify}).absent(t.Context(), f.reservation); err == nil || !strings.Contains(err.Error(), "no executable binding") {
+ t.Fatalf("census without a recheck = %v", err)
+ }
+}
diff --git a/internal/host/rootgeneration_kernel_linux.go b/internal/host/rootgeneration_kernel_linux.go
index 03a3209b1..969191b79 100644
--- a/internal/host/rootgeneration_kernel_linux.go
+++ b/internal/host/rootgeneration_kernel_linux.go
@@ -30,6 +30,10 @@ type generationKernel struct {
owner uint32
contained []release.GenerationReservation
service func(string, []byte) (bool, error)
+ // recheck holds a task service trusted to the image it was trusted for,
+ // at each use of that trust and, final, at the end of its inspection
+ // (generationServiceEnv.recheck). A trusted task needs it.
+ recheck func(path string, final bool) error
}
func generationRead(path string) ([]byte, error) {
@@ -389,6 +393,24 @@ func (k generationKernel) task(ctx context.Context, path string, b release.Gener
if rootErr != nil && !trusted {
return "", rootErr
}
+ bound := func(final bool) error {
+ if !trusted {
+ return nil
+ }
+ if k.recheck == nil {
+ return fmt.Errorf("service: no executable binding: %w", os.ErrPermission)
+ }
+ if err := k.recheck(path, final); err != nil {
+ return fmt.Errorf("service: %w", err)
+ }
+ return nil
+ }
+ if trusted && k.recheck != nil {
+ defer k.recheck(path, true)
+ }
+ if err := bound(false); err != nil {
+ return "", err
+ }
if !kernel {
for name, want := range ns {
got, err := os.Readlink(path + "/ns/" + name)
@@ -418,6 +440,9 @@ func (k generationKernel) task(ctx context.Context, path string, b release.Gener
if err != nil {
return "", err
}
+ if err := bound(false); err != nil {
+ return "", err
+ }
if err := generationMounts(mounts, b, trusted); err != nil {
return "", fmt.Errorf("mounts: %w", err)
}
@@ -433,6 +458,9 @@ func (k generationKernel) task(ctx context.Context, path string, b release.Gener
if again != start {
return "", errGenerationTaskReplaced
}
+ if err := bound(true); err != nil {
+ return "", err
+ }
return start, nil
}
func (k generationKernel) absent(ctx context.Context, r release.GenerationReservation) (string, error) {
@@ -526,7 +554,8 @@ func productionGenerationAbsence(ctx context.Context, r release.GenerationReserv
if err != nil {
return "", err
}
- return (generationKernel{proc: "/proc", service: generationServiceReader(ctx)}).absent(ctx, current)
+ service, recheck := generationServiceReader(ctx)
+ return (generationKernel{proc: "/proc", service: service, recheck: recheck}).absent(ctx, current)
}
func generationRecordedAbsence(ctx context.Context, r release.GenerationReservation) (release.GenerationReservation, error) {
diff --git a/internal/host/rootgeneration_retired_linux.go b/internal/host/rootgeneration_retired_linux.go
index ebf91d641..6b3a48c8f 100644
--- a/internal/host/rootgeneration_retired_linux.go
+++ b/internal/host/rootgeneration_retired_linux.go
@@ -243,7 +243,8 @@ func productionRetiredGenerationKernelCensus(ctx context.Context) (string, strin
}
func generationRetiredController(ctx context.Context, b release.GenerationBinding) error {
- k := generationKernel{proc: "/proc", service: generationServiceReader(ctx)}
+ service, recheck := generationServiceReader(ctx)
+ k := generationKernel{proc: "/proc", service: service, recheck: recheck}
if k.controllerGone(ctx, b) == nil {
return nil
}
diff --git a/internal/host/rootgeneration_services_linux.go b/internal/host/rootgeneration_services_linux.go
index a75d7b92f..524a8eead 100644
--- a/internal/host/rootgeneration_services_linux.go
+++ b/internal/host/rootgeneration_services_linux.go
@@ -57,10 +57,54 @@ func (r *generationServiceRefusal) Error() string {
func (r *generationServiceRefusal) Unwrap() error { return r.err }
-func generationServiceReader(ctx context.Context) func(string, []byte) (bool, error) {
+// generationServiceReader is the production service lookup: classify a
+// 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)
}
+// generationExeID is what binds a task to the image it was proven to run:
+// the device and inode of the descriptor opened through /proc/<pid>/exe,
+// the kernel's file handle for it where the filesystem gives one, and its
+// mount ID. rootstate.ObjectIdentity carries the same handle but also
+// resolves the mount in root's own mount table, which a service in a
+// private mount namespace does not share, so only its kernel facts are
+// used here.
+type generationExeID struct {
+ device, inode, mount uint64
+ handle string
+ handleType int32
+}
+
+func generationExeIdentity(f *os.File) (generationExeID, error) {
+ var st unix.Stat_t
+ if err := unix.Fstat(int(f.Fd()), &st); err != nil {
+ return generationExeID{}, err
+ }
+ var sx unix.Statx_t
+ if err := unix.Statx(int(f.Fd()), "", unix.AT_EMPTY_PATH, unix.STATX_MNT_ID, &sx); err != nil {
+ return generationExeID{}, err
+ }
+ id := generationExeID{device: uint64(st.Dev), inode: st.Ino, mount: sx.Mnt_id}
+ handle, _, err := unix.NameToHandleAt(int(f.Fd()), "", unix.AT_EMPTY_PATH)
+ if err == nil {
+ id.handle, id.handleType = hex.EncodeToString(handle.Bytes()), handle.Type()
+ } else if !errors.Is(err, unix.EOPNOTSUPP) {
+ return generationExeID{}, err
+ }
+ return id, nil
+}
+
+// generationExeBinding is a task the lookup trusted because the image it
+// ran was proven: that image's descriptor, kept open for the inspection so
+// its inode cannot be reused, and its identity.
+type generationExeBinding struct {
+ file *os.File
+ id generationExeID
+ unit, group, exe string
+}
+
func generationServiceShow(ctx context.Context, unit string) (map[string]string, error) {
out, err := exec.CommandContext(ctx, "systemctl", "show", unit, "-p", "ControlGroup", "-p", "MainPID", "-p", "ExecStart", "-p", "FragmentPath", "-p", "DropInPaths").Output()
if err != nil {
@@ -88,8 +132,9 @@ func generationServiceTrust(file string) error {
return nil
}
-func (env generationServiceEnv) reader(ctx context.Context) func(string, []byte) (bool, error) {
+func (env generationServiceEnv) reader(ctx context.Context) (func(string, []byte) (bool, error), func(string, bool) error) {
units := map[string]generationService{}
+ bound := map[string]*generationExeBinding{}
var proven []string
var provenErr error
proved := false
@@ -116,7 +161,17 @@ func (env generationServiceEnv) reader(ctx context.Context) func(string, []byte)
}
return proven, provenErr
}
- return func(path string, status []byte) (bool, error) {
+ classify := func(path string, status []byte) (bool, error) {
+ var held *os.File
+ defer func() {
+ if held != nil {
+ held.Close()
+ }
+ }()
+ if old := bound[path]; old != nil {
+ old.file.Close()
+ delete(bound, path)
+ }
name, nameErr := os.Readlink(path + "/exe")
exe := name
if nameErr != nil {
@@ -201,9 +256,11 @@ func (env generationServiceEnv) reader(ctx context.Context) func(string, []byte)
if s.selected != (rootstate.FileIdentity{}) {
want = s.selectedPath
}
- if err := env.deletedProgram(path, program, want, prove); err != nil {
+ image, err := env.deletedProgram(path, program, want, prove)
+ if err != nil {
return false, refuse("deleted exe", err)
}
+ held = image
} else {
actual, err := generationServiceExecutable(path, name)
if err != nil {
@@ -227,8 +284,61 @@ func (env generationServiceEnv) reader(ctx context.Context) func(string, []byte)
}
return false, refuse("main cgroup", err)
}
+ if held != nil {
+ id, err := generationExeIdentity(held)
+ if err != nil {
+ return false, refuse("exe binding", err)
+ }
+ bound[path] = &generationExeBinding{file: held, id: id, unit: unit, group: group, exe: exe}
+ held = nil
+ }
return true, nil
}
+ return classify, func(path string, final bool) error { return env.recheck(bound, path, final) }
+}
+
+// recheck holds a trusted task to the image it was proven to run, at each
+// later use of that trust and at the end of its inspection (final).
+// /proc/<pid>/exe, opened again, must be the very inode that was hashed (device, inode, mount and file handle),
+// still a root-owned regular file with no links left and no group or other
+// write. A task that exec'd anything else since, the same PID with the same
+// start time, refuses; a later census pass may classify it afresh. The
+// bytes are not hashed again: the identity is the hashed descriptor's, which
+// stays open, and the kernel refuses writes to an image being executed
+// (ETXTBSY). final releases the binding whatever the result. A task the
+// lookup trusted without a proven image (its live exe was the unit's
+// selected program, as on main) has no binding and passes.
+func (env generationServiceEnv) recheck(bound map[string]*generationExeBinding, path string, final bool) error {
+ b := bound[path]
+ if b == nil {
+ return nil
+ }
+ if final {
+ defer func() {
+ b.file.Close()
+ delete(bound, path)
+ }()
+ }
+ refuse := func(err error) error {
+ return &generationServiceRefusal{check: "exe binding", unit: b.unit, group: b.group, exe: b.exe, err: err}
+ }
+ f, err := os.Open(path + "/exe")
+ if err != nil {
+ return refuse(err)
+ }
+ defer f.Close()
+ id, err := generationExeIdentity(f)
+ if err != nil {
+ return refuse(err)
+ }
+ var st unix.Stat_t
+ if err := unix.Fstat(int(f.Fd()), &st); err != nil {
+ return refuse(err)
+ }
+ if id != b.id || st.Uid != env.owner || st.Mode&unix.S_IFMT != unix.S_IFREG || st.Mode&0022 != 0 || st.Nlink != 0 {
+ return refuse(fmt.Errorf("the task's image is no longer the one proven: %w", os.ErrPermission))
+ }
+ return nil
}
// deletedProgram accepts a non-root service task still running a carlos
@@ -241,36 +351,45 @@ func (env generationServiceEnv) reader(ctx context.Context) func(string, []byte)
// (<opt>/carlos, <opt>/carlos.prev), proven by readRootFloorBinary, the
// proof the guard makes before it execs one. Anything else refuses, and
// the census's reference checks still run on an accepted task.
-func (env generationServiceEnv) deletedProgram(task, program, want string, prove func() ([]string, error)) error {
+//
+// On success it returns the image's descriptor, open: the lookup keeps it
+// to bind the task to that image for the rest of its inspection (recheck).
+func (env generationServiceEnv) deletedProgram(task, program, want string, prove func() ([]string, error)) (*os.File, error) {
if program != want || want != filepath.Join(env.opt, "carlos") {
- return fmt.Errorf("%s is not this unit's router binary: %w", program, os.ErrPermission)
+ return nil, fmt.Errorf("%s is not this unit's router binary: %w", program, os.ErrPermission)
}
f, err := os.Open(task + "/exe")
if err != nil {
- return err
+ return nil, err
}
- defer f.Close()
+ keep := false
+ defer func() {
+ if !keep {
+ f.Close()
+ }
+ }()
var st unix.Stat_t
if err := unix.Fstat(int(f.Fd()), &st); err != nil {
- return err
+ return nil, err
}
if st.Uid != env.owner || st.Mode&unix.S_IFMT != unix.S_IFREG || st.Mode&0022 != 0 || st.Nlink != 0 || st.Size > 512<<20 {
- return fmt.Errorf("image is not an unlinked root-owned file: %w", os.ErrPermission)
+ return nil, fmt.Errorf("image is not an unlinked root-owned file: %w", os.ErrPermission)
}
sum := sha256.New()
if n, err := io.Copy(sum, io.LimitReader(f, 512<<20+1)); err != nil {
- return err
+ return nil, err
} else if n > 512<<20 {
- return os.ErrPermission
+ return nil, os.ErrPermission
}
digests, err := prove()
if err != nil {
- return err
+ return nil, err
}
if !slices.Contains(digests, hex.EncodeToString(sum.Sum(nil))) {
- return fmt.Errorf("image is neither the current nor the previous signed router build: %w", os.ErrPermission)
+ return nil, fmt.Errorf("image is neither the current nor the previous signed router build: %w", os.ErrPermission)
}
- return nil
+ keep = true
+ return f, nil
}
func generationGuardEdgeStart(unit, start string) bool {