carlos / platform Public

host: the claim checks parse DropInPaths as systemctl show quotes it; the no-policy witness compares sets (#328)

Root-runner run 20261025T184830-a90f6c.

1. systemctl show prints each string of a list property through
   shell_maybe_quote: a drop-in path under an escaped unit name comes
   back as "/run/systemd/system.control/carlos-instance@shop\\x2da...".
   claimDropinsBesidesNudge split DropInPaths with strings.Fields, so in
   production a nudged unit of a host with a dash (and any ordinary unit
   with its claim-launch drop-in there) carried an unknown, quoted drop-in
   and the claim start or migration was refused. systemdShowList parses
   systemd's form exactly (quotes, the four backslash escapes, one space
   between strings) and refuses anything else; the claim checks and the
   nudge witness use it.

2. TestRootEgressNoPolicyByteIdentical compared IPAddressDeny as text;
   systemd prints the set in no fixed order. It now compares
   IPAddressAllow and IPAddressDeny as sets of prefixes and DropInPaths
   as parsed path sets; drop-in files by name and bytes as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 818a139260ffbd7b826dfcccef952f7524ad59eb parent d128beb
4 files changed, +207 −11
  • internal/host/claimunit_effective.go +12 −6
  • internal/host/egress_startpaths_rootrunner_linux_test.go +41 −5
  • internal/host/systemdshow.go +77 −0
  • internal/host/systemdshow_test.go +77 −0
diff --git a/internal/host/claimunit_effective.go b/internal/host/claimunit_effective.go
index e94021f8f..272ce1e5d 100644
--- a/internal/host/claimunit_effective.go
+++ b/internal/host/claimunit_effective.go
@@ -258,16 +258,22 @@ func claimNudgeDropinBody(b []byte) bool {
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.
+// claimDropinsBesidesNudge splits a unit's DropInPaths as systemctl show
+// prints it (systemdShowList: a path under a unit name systemd escaped is
+// quoted) 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, and refuse a list systemd would not have printed.
func claimDropinsBesidesNudge(unit, dropins string) ([]string, error) {
+ paths, err := systemdShowList(dropins)
+ if err != nil {
+ return nil, claimEffectiveRefusal{"dropins", os.ErrPermission}
+ }
nudge := claimNudgeDropinPath(unit)
var rest []string
seen := false
- for _, path := range strings.Fields(dropins) {
+ for _, path := range paths {
if path != nudge || seen {
rest = append(rest, path)
continue
diff --git a/internal/host/egress_startpaths_rootrunner_linux_test.go b/internal/host/egress_startpaths_rootrunner_linux_test.go
index 37b03eb2d..5af68a410 100644
--- a/internal/host/egress_startpaths_rootrunner_linux_test.go
+++ b/internal/host/egress_startpaths_rootrunner_linux_test.go
@@ -1061,7 +1061,8 @@ func plan14EgressNudgeBeforeClaims(t *testing.T, unit string, ctl func(context.C
if err != nil {
t.Fatal(err)
}
- if !slices.Contains(strings.Fields(dropins), claimNudgeDropinPath(unit)) {
+ paths, err := systemdShowList(dropins)
+ if err != nil || !slices.Contains(paths, claimNudgeDropinPath(unit)) {
t.Fatalf("the nudge's drop-in is not at %s: DropInPaths %q", claimNudgeDropinPath(unit), dropins)
}
body, _ := os.ReadFile(claimNudgeDropinPath(unit))
@@ -1206,8 +1207,10 @@ func egressNoPolicyStarts(t *testing.T, wired bool) map[string]map[string]string
}
// egressObserve is what systemd shows for the box's host: for each of its
-// units, its slice and carlos-instances.slice, IPAddressAllow,
-// IPAddressDeny and DropInPaths, and each runtime drop-in under
+// units, its slice and carlos-instances.slice, IPAddressAllow and
+// IPAddressDeny as sets of prefixes (systemd prints a set in no fixed
+// order: run 20261025T184830-a90f6c), DropInPaths as the set of paths
+// systemdShowList parses, and each runtime drop-in under
// /run/systemd/system.control/<name>.d by file name and bytes.
func egressObserve(t *testing.T, b *egressBox) map[string]string {
t.Helper()
@@ -1222,9 +1225,16 @@ func egressObserve(t *testing.T, b *egressBox) map[string]string {
instancectl.InstancesSlice,
}
for _, name := range names {
- for _, prop := range []string{"IPAddressAllow", "IPAddressDeny", "DropInPaths"} {
- out["show "+name+" "+prop] = egressShow(t, name, prop)
+ for _, prop := range []string{"IPAddressAllow", "IPAddressDeny"} {
+ out["show "+name+" "+prop] = egressPrefixSet(t, name+" "+prop, egressShow(t, name, prop))
}
+ raw := egressShow(t, name, "DropInPaths")
+ paths, err := systemdShowList(raw)
+ if err != nil {
+ t.Fatalf("%s DropInPaths %q: %v", name, raw, err)
+ }
+ slices.Sort(paths)
+ out["show "+name+" DropInPaths"] = strings.Join(paths, "\n")
dir := filepath.Join("/run/systemd/system.control", name+".d")
entries, err := os.ReadDir(dir)
if errors.Is(err, fs.ErrNotExist) {
@@ -1243,3 +1253,29 @@ func egressObserve(t *testing.T, b *egressBox) map[string]string {
}
return out
}
+
+// egressPrefixSet is an IP address list as systemd shows it, as a sorted
+// set of masked prefixes (a bare address is its full-length prefix),
+// one per line.
+func egressPrefixSet(t *testing.T, what, list string) string {
+ t.Helper()
+ var set []string
+ for _, tok := range strings.Fields(list) {
+ var p netip.Prefix
+ if strings.Contains(tok, "/") {
+ var err error
+ if p, err = netip.ParsePrefix(tok); err != nil {
+ t.Fatalf("%s: %q: %v", what, tok, err)
+ }
+ } else {
+ a, err := netip.ParseAddr(tok)
+ if err != nil {
+ t.Fatalf("%s: %q: %v", what, tok, err)
+ }
+ p = netip.PrefixFrom(a, a.BitLen())
+ }
+ set = append(set, p.Masked().String())
+ }
+ slices.Sort(set)
+ return strings.Join(slices.Compact(set), "\n")
+}
diff --git a/internal/host/systemdshow.go b/internal/host/systemdshow.go
new file mode 100644
index 000000000..835861864
--- /dev/null
+++ b/internal/host/systemdshow.go
@@ -0,0 +1,77 @@
+package host
+
+import (
+ "errors"
+ "strings"
+)
+
+// systemdShowList splits a string-list property as `systemctl show` prints
+// it (DropInPaths, say), with or without --value: the strings one space
+// apart, each as systemd's shell_maybe_quote(s, 0) left it. A string with
+// nothing a shell would need quoted is printed as it is; any other is
+// wrapped in double quotes, with a backslash before each `"`, `\`, “ ` “
+// and `$` in it and every other byte as it is. So a unit name systemd
+// escaped (carlos-instance@shop\x2da.example.service) comes back from a
+// drop-in path as "/run/systemd/system.control/carlos-instance@shop\\x2da.example.service.d/50-IPAddressAllow.conf"
+// (root-runner run 20261025T184830-a90f6c).
+//
+// Anything systemd does not print that way is an error: an unterminated
+// quote, a backslash inside quotes before any other byte, a quote, a
+// backslash or a control byte outside quotes, two spaces in a row, or a
+// quoted string with no space after it. The claim checks refuse on an
+// error.
+func systemdShowList(value string) ([]string, error) {
+ var out []string
+ for i := 0; i < len(value); {
+ if len(out) > 0 {
+ if value[i] != ' ' {
+ return nil, errors.New("systemctl show list: no space between strings")
+ }
+ i++
+ if i == len(value) {
+ return nil, errors.New("systemctl show list: a trailing space")
+ }
+ }
+ if value[i] != '"' {
+ j := strings.IndexByte(value[i:], ' ')
+ if j < 0 {
+ j = len(value) - i
+ }
+ s := value[i : i+j]
+ if s == "" {
+ return nil, errors.New("systemctl show list: an empty string")
+ }
+ for k := 0; k < len(s); k++ {
+ if c := s[k]; c < ' ' || c == 127 || c == '"' || c == '\\' {
+ return nil, errors.New("systemctl show list: an unquoted string systemd would have quoted")
+ }
+ }
+ out = append(out, s)
+ i += j
+ continue
+ }
+ var b strings.Builder
+ closed := false
+ for i++; i < len(value); i++ {
+ c := value[i]
+ if c == '"' {
+ closed = true
+ i++
+ break
+ }
+ if c == '\\' {
+ i++
+ if i == len(value) || !strings.ContainsRune("\"\\`$", rune(value[i])) {
+ return nil, errors.New("systemctl show list: a backslash systemd would not have written")
+ }
+ c = value[i]
+ }
+ b.WriteByte(c)
+ }
+ if !closed {
+ return nil, errors.New("systemctl show list: an unterminated quote")
+ }
+ out = append(out, b.String())
+ }
+ return out, nil
+}
diff --git a/internal/host/systemdshow_test.go b/internal/host/systemdshow_test.go
new file mode 100644
index 000000000..13735161a
--- /dev/null
+++ b/internal/host/systemdshow_test.go
@@ -0,0 +1,77 @@
+package host
+
+import (
+ "slices"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/instancectl"
+)
+
+// systemdNudgeDropInPaths is DropInPaths as systemd printed it for a
+// nudged unit of a host with a dash, verbatim (root-runner run
+// 20261025T184830-a90f6c: `systemctl show -p DropInPaths --value`).
+const systemdNudgeDropInPaths = `"/run/systemd/system.control/carlos-instance@shop\\x2da.example.service.d/50-IPAddressAllow.conf"`
+
+func TestSystemdShowList(t *testing.T) {
+ t.Parallel()
+ nudge := `/run/systemd/system.control/carlos-instance@shop\x2da.example.service.d/50-IPAddressAllow.conf`
+ for _, tc := range []struct {
+ name, in string
+ want []string
+ err bool
+ }{
+ {name: "empty", in: ""},
+ {name: "systemd's quoted escaped unit", in: systemdNudgeDropInPaths, want: []string{nudge}},
+ {name: "plain paths", in: "/etc/systemd/system/a.service.d/x.conf /run/systemd/system/a.service.d/y.conf",
+ want: []string{"/etc/systemd/system/a.service.d/x.conf", "/run/systemd/system/a.service.d/y.conf"}},
+ {name: "plain then quoted", in: "/run/systemd/system/u.d/60-claim-launch.conf " + systemdNudgeDropInPaths,
+ want: []string{"/run/systemd/system/u.d/60-claim-launch.conf", nudge}},
+ {name: "quoted then plain", in: systemdNudgeDropInPaths + " /x", want: []string{nudge, "/x"}},
+ {name: "every escape shell_maybe_quote writes, and a space", in: `"/a b\"c\\d\` + "`" + `e\$f"`, want: []string{"/a b\"c\\d`e$f"}},
+ {name: "unterminated quote", in: `"/a`, err: true},
+ {name: "a backslash before another byte", in: `"/a\x2d"`, err: true},
+ {name: "a trailing backslash", in: `"/a\`, err: true},
+ {name: "a backslash outside quotes", in: `/a\x2d`, err: true},
+ {name: "a quote inside a plain string", in: `/a"b"`, err: true},
+ {name: "two strings run together", in: `"/a""/b"`, err: true},
+ {name: "two spaces", in: "/a /b", err: true},
+ {name: "a trailing space", in: "/a ", err: true},
+ {name: "a leading space", in: " /a", err: true},
+ {name: "a control byte outside quotes", in: "/a\t/b", err: true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ got, err := systemdShowList(tc.in)
+ if (err != nil) != tc.err || !slices.Equal(got, tc.want) {
+ t.Fatalf("systemdShowList(%q) = %q, %v; want %q, error %v", tc.in, got, err, tc.want, tc.err)
+ }
+ })
+ }
+}
+
+// TestClaimDropinsBesidesNudgeQuotedByShow: the claim checks take
+// DropInPaths as systemd prints it for a nudged unit of a host with a
+// dash, quoted, and accept the nudge (Task 15 root-runner run
+// 20261025T184830-a90f6c; before, the quoted path was an unknown drop-in
+// and the claim start was refused). Not parallel: withNudgeDropin swaps
+// readClaimNudgeDropin.
+func TestClaimDropinsBesidesNudgeQuotedByShow(t *testing.T) {
+ unit := instancectl.UnitName(instancectl.RoleRun, "shop-a.example")
+ if claimNudgeDropinPath(unit) != `/run/systemd/system.control/carlos-instance@shop\x2da.example.service.d/50-IPAddressAllow.conf` {
+ t.Fatalf("fixture: the nudge path is %s", claimNudgeDropinPath(unit))
+ }
+ withNudgeDropin(t, unit, egressNudgeBody)
+ rest, err := claimDropinsBesidesNudge(unit, systemdNudgeDropInPaths)
+ if err != nil || len(rest) != 0 {
+ t.Fatalf("claimDropinsBesidesNudge(systemd's DropInPaths) = %q, %v; want the nudge accepted", rest, err)
+ }
+ // Another drop-in, quoted the same way, is still returned to refuse.
+ other := `"/run/systemd/system/carlos-instance@shop\\x2da.example.service.d/60-claim-launch.conf"`
+ rest, err = claimDropinsBesidesNudge(unit, systemdNudgeDropInPaths+" "+other)
+ if err != nil || !slices.Equal(rest, []string{`/run/systemd/system/carlos-instance@shop\x2da.example.service.d/60-claim-launch.conf`}) {
+ t.Fatalf("with another drop-in: %q, %v", rest, err)
+ }
+ // A list systemd would not print is refused.
+ if _, err := claimDropinsBesidesNudge(unit, `"/run/unterminated`); err == nil {
+ t.Fatal("a malformed DropInPaths was not refused")
+ }
+}