carlos / platform Public

host: the claim checks accept exactly root's egress nudge drop-in (#328)

Task 14 review finding 1: the nudge (set-property --runtime <unit>
IPAddressAllow=) leaves /run/systemd/system.control/<unit>.d/
50-IPAddressAllow.conf until reboot, and claimEffectiveUnit and the
predecessor capture refused any extra drop-in, so a nudged unit could
not be claim-launched after claims were switched on without a reboot.
Both now accept that one file, read as a trusted root file, when it
sets nothing but the empty IPAddressAllow= reset under [Service]; any
other content, name or extra drop-in is refused as before. The claim
launch witness's egress: mode makes the nudge's write on the run unit
first and launches with systemd's drop-in in place.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 2d9cf690d08fefacb5cf6d3dd646ee1202b832a3 parent 8fee93b
5 files changed, +243 −3
  • internal/host/claimunit_effective.go +75 −2
  • internal/host/claimunit_effective_test.go +113 −0
  • internal/host/claimunit_migrate_kernel.go +6 −1
  • internal/host/egress_startpaths_rootrunner_linux_test.go +48 −0
  • internal/host/plan14_installed_launch_root_linux_test.go +1 −0
diff --git a/internal/host/claimunit_effective.go b/internal/host/claimunit_effective.go
index edb86b230..7cb2c5a0d 100644
--- a/internal/host/claimunit_effective.go
+++ b/internal/host/claimunit_effective.go
@@ -130,9 +130,15 @@ func claimEffectiveUnit(ctx context.Context, s claimUnitShape, ctl func(context.
if ordinary {
executable = instancectl.SystemBinary
}
- if props["DropInPaths"] != "" {
+ // Root's egress nudge leaves one drop-in on a unit it ran on: accepted
+ // exactly, and only that one (claimDropinsBesidesNudge).
+ dropins, err := claimDropinsBesidesNudge(s.Unit, props["DropInPaths"])
+ if err != nil {
+ return err
+ }
+ if len(dropins) > 0 {
path := filepath.Join("/run/systemd/system", s.Unit+".d", "60-claim-launch.conf")
- if props["DropInPaths"] != path {
+ if len(dropins) != 1 || dropins[0] != path {
return claimEffectiveRefusal{"dropins", os.ErrPermission}
}
f, err := openClaimUnitDropin(s.Unit)
@@ -206,3 +212,70 @@ func claimIPSet(raw string) ([]string, error) {
}
return kept, nil
}
+
+// claimNudgeDropinPath is the drop-in systemd writes for root's egress
+// nudge on unit: instancectl's nudgeRunning runs `systemctl set-property
+// --runtime <unit> IPAddressAllow=` on every service running in a host
+// slice after a change to the slice's allow (run 20261025T173953-8a3366),
+// and systemd keeps it under /run/systemd/system.control until reboot.
+func claimNudgeDropinPath(unit string) string {
+ return filepath.Join("/run/systemd/system.control", unit+".d", "50-IPAddressAllow.conf")
+}
+
+// readClaimNudgeDropin reads a nudge drop-in through
+// rootstate.OpenTrustedFile (root-owned, not group- or world-writable,
+// one link). A variable only so a test can answer for a path it cannot
+// make trusted.
+var readClaimNudgeDropin = func(path string) ([]byte, error) {
+ f, err := rootstate.OpenTrustedFile(path)
+ if err != nil {
+ return nil, err
+ }
+ defer f.Close()
+ return io.ReadAll(io.LimitReader(f, rootstate.MaxRecord+1))
+}
+
+// claimNudgeDropinBody is true when a nudge drop-in sets nothing but the
+// empty IPAddressAllow= reset in the [Service] section: comments and
+// blank lines aside, exactly those two lines. An empty reset of the
+// unit's own allow cannot widen anything; any other content is refused.
+func claimNudgeDropinBody(b []byte) bool {
+ if len(b) > rootstate.MaxRecord {
+ return false
+ }
+ var lines []string
+ for _, line := range strings.Split(string(b), "\n") {
+ line = strings.TrimSpace(line)
+ if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") {
+ continue
+ }
+ lines = append(lines, line)
+ }
+ return slices.Equal(lines, []string{"[Service]", "IPAddressAllow="})
+}
+
+// claimDropinsBesidesNudge splits a unit's DropInPaths and returns every
+// drop-in but root's egress nudge, which it accepts only once, only at
+// claimNudgeDropinPath and only with claimNudgeDropinBody's content (Task
+// 14 review finding 1). The claim checks refuse whatever it returns, as
+// they refused every extra drop-in before.
+func claimDropinsBesidesNudge(unit, dropins string) ([]string, error) {
+ nudge := claimNudgeDropinPath(unit)
+ var rest []string
+ seen := false
+ for _, path := range strings.Fields(dropins) {
+ if path != nudge || seen {
+ rest = append(rest, path)
+ continue
+ }
+ seen = true
+ body, err := readClaimNudgeDropin(path)
+ if err != nil {
+ return nil, claimEffectiveRefusal{"nudge-dropin", err}
+ }
+ if !claimNudgeDropinBody(body) {
+ return nil, claimEffectiveRefusal{"nudge-dropin", os.ErrPermission}
+ }
+ }
+ return rest, nil
+}
diff --git a/internal/host/claimunit_effective_test.go b/internal/host/claimunit_effective_test.go
index 71b04ee5c..b3de2ed16 100644
--- a/internal/host/claimunit_effective_test.go
+++ b/internal/host/claimunit_effective_test.go
@@ -2,11 +2,13 @@ package host
import (
"context"
+ "os"
"slices"
"strings"
"testing"
"github.com/carlosframework/platform/internal/instancectl"
+ "github.com/carlosframework/platform/internal/rootstate"
)
func TestPlan14EffectiveBoundUnit(t *testing.T) {
@@ -68,3 +70,114 @@ func TestPlan14EffectiveNetworkReduction(t *testing.T) {
t.Fatal("broadened set accepted", a, c, err)
}
}
+
+// egressNudgeBody is the drop-in systemd writes for root's egress nudge
+// (`systemctl set-property --runtime <unit> IPAddressAllow=`, instancectl's
+// nudgeRunning): its header comment, the unit's section and the empty
+// reset.
+const egressNudgeBody = "# This is a drop-in unit file extension, created via \"systemctl set-property\"\n# or an equivalent operation. Do not edit.\n[Service]\nIPAddressAllow=\n"
+
+// egressNudgeCases are the drop-in sets the claim checks are given for a
+// unit: only the nudge's drop-in, exact, is accepted (Task 14 review
+// finding 1).
+func egressNudgeCases(unit string) []struct {
+ name, dropins, body string
+ ok bool
+} {
+ nudge := claimNudgeDropinPath(unit)
+ other := "/run/systemd/system.control/" + unit + ".d/50-IPAddressDeny.conf"
+ return []struct {
+ name, dropins, body string
+ ok bool
+ }{
+ {"the nudge's drop-in alone", nudge, egressNudgeBody, true},
+ {"the nudge's drop-in, no comment", nudge, "[Service]\nIPAddressAllow=\n", true},
+ {"the nudge's name carrying an allow", nudge, "[Service]\nIPAddressAllow=\nIPAddressAllow=10.0.0.0/8\n", false},
+ {"the nudge's name carrying another setting", nudge, "[Service]\nIPAddressAllow=\nIPAddressDeny=\n", false},
+ {"the nudge's name in another section", nudge, "[Unit]\nIPAddressAllow=\n", false},
+ {"the nudge's name, empty", nudge, "", false},
+ {"the nudge's content under another name", other, egressNudgeBody, false},
+ {"the nudge's drop-in and another", nudge + " " + other, egressNudgeBody, false},
+ {"the nudge's drop-in twice", nudge + " " + nudge, egressNudgeBody, false},
+ }
+}
+
+// withNudgeDropin swaps readClaimNudgeDropin (a package variable, so the
+// test is not parallel) for one that answers body for the nudge's path
+// and fails any other.
+func withNudgeDropin(t *testing.T, unit, body string) {
+ t.Helper()
+ old := readClaimNudgeDropin
+ t.Cleanup(func() { readClaimNudgeDropin = old })
+ readClaimNudgeDropin = func(path string) ([]byte, error) {
+ if path != claimNudgeDropinPath(unit) {
+ t.Errorf("read %s, not the nudge's drop-in", path)
+ return nil, os.ErrPermission
+ }
+ return []byte(body), nil
+ }
+}
+
+func TestClaimEffectiveUnitAcceptsOnlyTheEgressNudgeDropin(t *testing.T) {
+ _, _, shape := plan14UnitBindingFixture(t, "console.service", "", "")
+ shape.Program = strings.Repeat("a", 64)
+ definition, err := claimBoundUnitDefinition(shape)
+ if err != nil {
+ t.Fatal(err)
+ }
+ values := map[string]string{"FragmentPath": "/etc/systemd/system/console.service", "DropInPaths": "", "NeedDaemonReload": "no", "ExecStart": "{ path=/usr/bin/false ; argv[]=/usr/bin/false ; }", "IPAddressAllow": "", "IPAddressDeny": "", "Slice": instancectl.SliceName(shape.Host)}
+ for _, line := range strings.Split(string(definition), "\n") {
+ if k, v, ok := strings.Cut(line, "="); ok && k != "ExecStart" {
+ values[k] = v
+ }
+ }
+ for _, tc := range egressNudgeCases(shape.Unit) {
+ t.Run(tc.name, func(t *testing.T) {
+ withNudgeDropin(t, shape.Unit, tc.body)
+ ctl := func(_ context.Context, args ...string) (string, error) {
+ var out strings.Builder
+ for _, key := range strings.Split(args[2], ",") {
+ value := values[key]
+ if key == "DropInPaths" {
+ value = tc.dropins
+ }
+ out.WriteString(key + "=" + value + "\n")
+ }
+ return out.String(), nil
+ }
+ err := claimEffectiveUnit(t.Context(), shape, ctl)
+ if tc.ok && err != nil {
+ t.Fatal("the nudge's drop-in refused:", err)
+ }
+ if !tc.ok && err == nil {
+ t.Fatal("accepted", tc.dropins, tc.body)
+ }
+ })
+ }
+}
+
+func TestClaimPredecessorAcceptsOnlyTheEgressNudgeDropin(t *testing.T) {
+ b, in, _ := plan14UnitBindingFixture(t, "console.service", "", "")
+ binding, err := b.establish(t.Context(), in.Host)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, tc := range egressNudgeCases(binding.Shape.Unit) {
+ t.Run(tc.name, func(t *testing.T) {
+ withNudgeDropin(t, binding.Shape.Unit, tc.body)
+ k := claimPredecessorKernel{group: func(string) (rootstate.FileIdentity, bool, error) {
+ return rootstate.FileIdentity{Device: 1, Inode: 2}, true, nil
+ }}
+ k.ctl = func(context.Context, ...string) (string, error) {
+ return "ActiveState=active\nInvocationID=original\nControlGroup=/system.slice/console.service\nFragmentPath=/etc/systemd/system/console.service\nDropInPaths=" + tc.dropins, nil
+ }
+ _, err := k.capture(t.Context(), binding)
+ if tc.ok && err != nil {
+ t.Fatal("the nudge's drop-in refused:", err)
+ }
+ if !tc.ok && err == nil {
+ t.Fatal("accepted", tc.dropins, tc.body)
+ }
+ })
+ }
+}
diff --git a/internal/host/claimunit_migrate_kernel.go b/internal/host/claimunit_migrate_kernel.go
index aed8bf333..97edd3ad2 100644
--- a/internal/host/claimunit_migrate_kernel.go
+++ b/internal/host/claimunit_migrate_kernel.go
@@ -48,7 +48,12 @@ func (k claimPredecessorKernel) capture(ctx context.Context, b claimUnitBinding)
return p, err
}
group := strings.TrimPrefix(props["ControlGroup"], "/")
- if props["ActiveState"] != "active" || props["InvocationID"] == "" || !claimPredecessorPlacement(b.Shape.Unit, group) || props["ControlGroup"] != "/"+group || props["FragmentPath"] != "/etc/systemd/system/"+b.Shape.Unit || props["DropInPaths"] != "" {
+ // No drop-in but root's egress nudge, exactly (claimDropinsBesidesNudge).
+ dropins, err := claimDropinsBesidesNudge(b.Shape.Unit, props["DropInPaths"])
+ if err != nil {
+ return p, err
+ }
+ if props["ActiveState"] != "active" || props["InvocationID"] == "" || !claimPredecessorPlacement(b.Shape.Unit, group) || props["ControlGroup"] != "/"+group || props["FragmentPath"] != "/etc/systemd/system/"+b.Shape.Unit || len(dropins) != 0 {
return p, os.ErrPermission
}
id, populated, err := k.group(group)
diff --git a/internal/host/egress_startpaths_rootrunner_linux_test.go b/internal/host/egress_startpaths_rootrunner_linux_test.go
index cc29b1971..26a3a2a21 100644
--- a/internal/host/egress_startpaths_rootrunner_linux_test.go
+++ b/internal/host/egress_startpaths_rootrunner_linux_test.go
@@ -3,6 +3,7 @@
package host
import (
+ "context"
"crypto/ed25519"
"errors"
"io/fs"
@@ -1022,3 +1023,50 @@ func egressGrantNotLiveDiag(t *testing.T, b *egressBox, other, third egressOther
egressFirewallDiag(t, otherUnit, otherSlice, bc, target)
}
+
+// plan14EgressNudgeBeforeClaims leaves on the claim launch's run unit the
+// drop-in a pre-claim root's egress nudge leaves (Task 14 review finding
+// 1): nudgeRunning's own write, `systemctl set-property --runtime <unit>
+// IPAddressAllow=`, so the file is systemd's, as on a box that ran
+// pre-claim egress and then switched claims on without a reboot. It checks
+// the drop-in sits at claimNudgeDropinPath and that the claim checks'
+// shared helper, through the production reader, accepts it and nothing
+// else; the harness's production claim launch (claimEffectiveUnit) must
+// then succeed with it in place. The drop-in is removed on cleanup.
+func plan14EgressNudgeBeforeClaims(t *testing.T, unit string, ctl func(context.Context, ...string) (string, error)) {
+ t.Helper()
+ dir := filepath.Dir(claimNudgeDropinPath(unit))
+ for _, root := range []string{"/run/systemd/system.control", "/etc/systemd/system.control"} {
+ if _, err := os.Lstat(filepath.Join(root, unit+".d")); !errors.Is(err, fs.ErrNotExist) {
+ t.Fatal("unit already has set-property drop-ins", root, unit, err)
+ }
+ }
+ t.Cleanup(func() {
+ if err := os.RemoveAll(dir); err != nil {
+ t.Error(err)
+ }
+ if out, err := ctl(context.Background(), "daemon-reload"); err != nil {
+ t.Errorf("daemon-reload: %v: %s", err, out)
+ }
+ })
+ // The harness wrote the template just before: load it.
+ if out, err := ctl(t.Context(), "daemon-reload"); err != nil {
+ t.Fatalf("daemon-reload: %v: %s", err, out)
+ }
+ if out, err := ctl(t.Context(), "set-property", "--runtime", unit, "IPAddressAllow="); err != nil {
+ t.Fatalf("the nudge's write on %s: %v: %s", unit, err, out)
+ }
+ dropins, err := ctl(t.Context(), "show", "-p", "DropInPaths", "--value", unit)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !slices.Contains(strings.Fields(dropins), claimNudgeDropinPath(unit)) {
+ t.Fatalf("the nudge's drop-in is not at %s: DropInPaths %q", claimNudgeDropinPath(unit), dropins)
+ }
+ body, _ := os.ReadFile(claimNudgeDropinPath(unit))
+ t.Logf("the nudge's drop-in, as systemd wrote it:\n%s", body)
+ rest, err := claimDropinsBesidesNudge(unit, dropins)
+ if err != nil || len(rest) != 0 {
+ t.Fatalf("the claim checks refuse the nudge's drop-in: rest %q, %v\n%s", rest, err, body)
+ }
+}
diff --git a/internal/host/plan14_installed_launch_root_linux_test.go b/internal/host/plan14_installed_launch_root_linux_test.go
index d18b755eb..4396a1f3a 100644
--- a/internal/host/plan14_installed_launch_root_linux_test.go
+++ b/internal/host/plan14_installed_launch_root_linux_test.go
@@ -736,6 +736,7 @@ func plan14InstalledClaimLaunch(t *testing.T, boundUnit string, runtimeUpdate ..
if got, _ := ctl(t.Context(), "show", "-p", "IPAddressAllow", "--value", instancectl.SliceName(host)); got != "10.0.5.20/32" {
t.Fatalf("before the claim launch the slice holds %q, want the hand grant 10.0.5.20/32", got)
}
+ plan14EgressNudgeBeforeClaims(t, unit, ctl)
}
var response hostbox.RoleLaunchResponse
err = lock.With(t.Context(), func(ctx context.Context) error {