carlos / platform Public

egress: the kernel proof installs unit files so the template's deny goes in verbatim (#328)

systemd-run -p IPAddressDeny= refuses the symbolic localhost and
multicast keywords the unit-file parser accepts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 5ce107d9f823de9daedb6ceee49bc61630bccb57 parent 2295ec6
1 file changed, +46 −15
  • internal/host/egress_kernel_rootrunner_linux_test.go +46 −15
diff --git a/internal/host/egress_kernel_rootrunner_linux_test.go b/internal/host/egress_kernel_rootrunner_linux_test.go
index 0175078bf..d55583fa9 100644
--- a/internal/host/egress_kernel_rootrunner_linux_test.go
+++ b/internal/host/egress_kernel_rootrunner_linux_test.go
@@ -74,7 +74,7 @@ func TestRootEgressKernelProof(t *testing.T) {
if err := unix.Statfs("/sys/fs/cgroup", &st); err != nil || st.Type != unix.CGROUP2_SUPER_MAGIC {
t.Fatalf("needs cgroup v2 at /sys/fs/cgroup: type %#x %v", st.Type, err)
}
- for _, tool := range []string{"ip", "systemctl", "systemd-run"} {
+ for _, tool := range []string{"ip", "systemctl"} {
if _, err := exec.LookPath(tool); err != nil {
t.Fatal("needs", tool, err)
}
@@ -83,6 +83,15 @@ func TestRootEgressKernelProof(t *testing.T) {
if err != nil || !filepath.IsAbs(exe) {
t.Fatal("test binary path", exe, err)
}
+ // exe and the scratch socket paths are written into unit files below
+ // unquoted: refuse anything systemd would split, quote or expand.
+ unitSafe := func(p string) {
+ t.Helper()
+ if strings.ContainsAny(p, " \t\n\"'\\%$;") {
+ t.Fatalf("path %q is not safe to write into a unit file", p)
+ }
+ }
+ unitSafe(exe)
ctl := func(args ...string) (string, error) {
out, err := exec.Command("systemctl", args...).CombinedOutput()
@@ -163,6 +172,7 @@ func TestRootEgressKernelProof(t *testing.T) {
t.Error(err)
}
})
+ unitSafe(scratch)
// The dummy link and its listeners.
ip := func(args ...string) {
@@ -258,35 +268,56 @@ func TestRootEgressKernelProof(t *testing.T) {
time.Sleep(100 * time.Millisecond)
}
}
- // start runs this binary's plan14-tenant in a transient unit carrying
- // the template's deny, and returns a client for its socket.
+ // start runs this binary's plan14-tenant in a unit file it installs
+ // under /run/systemd/system with the template's deny, and returns a
+ // client for its socket.
start := func(unit, inSlice, sock string) *http.Client {
t.Helper()
+ name := unit + ".service"
+ path := filepath.Join("/run/systemd/system", name)
+ if _, err := os.Lstat(path); !errors.Is(err, fs.ErrNotExist) {
+ t.Fatal("unit file already exists", path, err)
+ }
t.Cleanup(func() {
- if out, _ := ctl("show", "-p", "LoadState", "--value", unit); out != "not-found" {
- if out, err := ctl("stop", unit); err != nil {
- t.Errorf("stop %s: %v: %s", unit, err, out)
+ if out, _ := ctl("show", "-p", "LoadState", "--value", name); out != "not-found" {
+ if out, err := ctl("stop", name); err != nil {
+ t.Errorf("stop %s: %v: %s", name, err, out)
}
- ctl("reset-failed", unit)
}
+ if err := os.Remove(path); err != nil && !errors.Is(err, fs.ErrNotExist) {
+ t.Error(err)
+ }
+ if out, err := ctl("daemon-reload"); err != nil {
+ t.Errorf("daemon-reload: %v: %s", err, out)
+ }
+ ctl("reset-failed", name)
for deadline := time.Now().Add(5 * time.Second); ; time.Sleep(100 * time.Millisecond) {
- out, _ := ctl("show", "-p", "LoadState", "--value", unit)
+ out, _ := ctl("show", "-p", "LoadState", "--value", name)
if out == "not-found" {
break
}
if time.Now().After(deadline) {
- t.Errorf("unit %s left behind: %s", unit, out)
+ t.Errorf("unit %s left behind: %s", name, out)
break
}
}
})
socket := filepath.Join(scratch, sock)
- out, err := exec.Command("systemd-run", "--unit="+unit, "--slice="+inSlice,
- "-p", "IPAddressDeny="+deny,
- "-E", "PLAN14_TENANT_SOCKET="+socket,
- exe, "plan14-tenant").CombinedOutput()
- if err != nil {
- t.Fatalf("systemd-run %s: %v: %s", unit, err, out)
+ // A unit file, not systemd-run -p: the deny line is the template's
+ // verbatim, symbolic keywords (localhost, multicast) included, which
+ // only the unit-file parser accepts.
+ body := "[Unit]\nDescription=carlos egress kernel proof " + unit + "\n\n[Service]\n" +
+ "Slice=" + inSlice + "\n" +
+ "IPAddressDeny=" + deny + "\n" +
+ "Environment=PLAN14_TENANT_SOCKET=" + socket + "\n" +
+ "ExecStart=" + exe + " plan14-tenant\n"
+ if err := os.WriteFile(path, []byte(body), 0644); err != nil {
+ t.Fatal(err)
+ }
+ mustCtl("daemon-reload")
+ if out, err := ctl("start", name); err != nil {
+ logs, _ := exec.Command("journalctl", "-u", name, "--no-pager", "-n", "40").CombinedOutput()
+ t.Fatalf("start %s: %v: %s\n%s", name, err, out, logs)
}
transport := &http.Transport{DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, "unix", socket)