gvisor demos: task 2 fix round 1 (CI template check, symlink tests)
Move template/pin and nonce guards into ordinary host tests, sharing the same measured constants with rootrunner witnesses. Use real parent and dangling leaf symlink targets and assert no target file is created. Apply the controller ruling for the leaf O_EXCL refusal while keeping ErrSymlink mandatory for the parent. Both cases fail their refusal and no-create assertions with a deliberately following opener, and pass with the production opener restored. Production bytes and census are unchanged.
Co-Authored-By: Astra (gpt-6-astra) via codex
3 files changed,
+196
−171
internal/host/gvisor_hardening_rootrunner_linux_test.go+0 −159internal/host/gvisor_units_test.go+169 −0internal/instanceexec/sandbox_linux_test.go+27 −12
CI failed — run details
Intent
diff --git a/internal/host/gvisor_hardening_rootrunner_linux_test.go b/internal/host/gvisor_hardening_rootrunner_linux_test.go| index f587cfde8..8d8129d50 100644 |
| --- a/internal/host/gvisor_hardening_rootrunner_linux_test.go |
| +++ b/internal/host/gvisor_hardening_rootrunner_linux_test.go |
| @@ -20,22 +20,6 @@ import ( |
| "golang.org/x/sys/unix" |
| ) |
| -// Preserve the exact measurement input. The template comparison below requires |
| -// the shipped units and pinned properties to match these tested bytes. |
| -const gvisorCandidateHardening = `ProtectSystem=strict |
| -ProtectHome=yes |
| -PrivateTmp=yes |
| -ProtectKernelTunables=yes |
| -ProtectKernelModules=yes |
| -ProtectKernelLogs=yes |
| -ProtectClock=yes |
| -ProtectHostname=yes |
| -ProtectControlGroups=yes |
| -LockPersonality=yes |
| -RestrictRealtime=yes |
| -SystemCallArchitectures=native |
| -` |
| - |
| func TestRootGVisorHardening(t *testing.T) { |
| f := newGVisorFixture(t) |
| for _, kind := range []string{"run", "probe", "build"} { |
| @@ -54,27 +38,6 @@ func TestRootGVisorHardening(t *testing.T) { |
| } |
| } |
| -func TestGVisorMeasuredTemplates(t *testing.T) { |
| - checkGVisorMeasuredTemplates(t) |
| -} |
| - |
| -func checkGVisorMeasuredTemplates(t *testing.T) { |
| - t.Helper() |
| - if execsandbox.GVisorHardeningLines() != gvisorCandidateHardening { |
| - t.Fatal("pinned hardening differs from measured candidates") |
| - } |
| - for name, skeleton := range map[string]string{ |
| - "carlos-instance-gvisor@.service": gvisorRunSkeleton, |
| - "carlos-instance-gvisor-probe.service": gvisorProbeSkeleton, |
| - "carlos-demo-build-gvisor@.service": gvisorBuildSkeleton, |
| - } { |
| - raw, err := unitsFS.ReadFile("units/" + name) |
| - if err != nil || string(raw) != skeleton+execsandbox.GVisorHardeningLines() { |
| - t.Errorf("%s differs from the measured skeleton and hardening: %v", name, err) |
| - } |
| - } |
| -} |
| - |
| func TestRootGVisorTrampoline(t *testing.T) { |
| f := newGVisorFixture(t) |
| unit := f.start("run") |
| @@ -376,35 +339,6 @@ func (f *gvisorFixture) checkGuest(e gvisorGuestEvidence, child bool, limit stri |
| } |
| } |
| -func gvisorProbeReport(raw []byte, nonce string) (instancectl.ProbeReport, error) { |
| - var report instancectl.ProbeReport |
| - if err := json.Unmarshal(raw, &report); err != nil { |
| - return report, err |
| - } |
| - if nonce == "" || report.Nonce != nonce { |
| - return report, errors.New("probe nonce mismatch") |
| - } |
| - return report, nil |
| -} |
| - |
| -func TestGVisorProbeReportNonce(t *testing.T) { |
| - for _, tc := range []struct { |
| - raw, nonce string |
| - ok bool |
| - }{ |
| - {`{"nonce":"fresh","ready":false}`, "fresh", true}, |
| - {`{"nonce":"stale"}`, "fresh", false}, |
| - {`{}`, "fresh", false}, |
| - {`{}`, "", false}, |
| - {`{"nonce":"fresh"} {"nonce":"fresh"}`, "fresh", false}, |
| - {`{"nonce":`, "fresh", false}, |
| - } { |
| - if _, err := gvisorProbeReport([]byte(tc.raw), tc.nonce); (err == nil) != tc.ok { |
| - t.Errorf("report=%q nonce=%q: %v", tc.raw, tc.nonce, err) |
| - } |
| - } |
| -} |
| - |
| func (f *gvisorFixture) waitFinished(unit string, remain bool) { |
| t := f.t |
| t.Helper() |
| @@ -506,96 +440,3 @@ func (f *gvisorFixture) stop(unit string) { |
| } |
| } |
| } |
| - |
| -const gvisorRunSkeleton = `[Unit] |
| -Description=carlos gVisor demo instance %I |
| -BindsTo=carlos-edge.service |
| -Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| -After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| -[Service] |
| -Type=exec |
| -User=root |
| -Group=root |
| -Slice=carlos-instances-%i.slice |
| -ExecStart= |
| -Restart=no |
| -KillMode=mixed |
| -# Let attached runsc wait for the guest after ExecStop sends TERM to guest init. |
| -KillSignal=SIGCONT |
| -TimeoutStopSec=20 |
| -SuccessExitStatus=129 130 141 143 |
| -StandardOutput=file:/run/carlos/logs/%I.run.sock |
| -StandardError=inherit |
| -RuntimeDirectory=carlos-runsc |
| -RuntimeDirectoryMode=0700 |
| -RuntimeDirectoryPreserve=yes |
| -ReadWritePaths=/run/carlos-runsc /data/instances/%I /run/carlos/instances/%I |
| -TasksMax=320 |
| -OOMScoreAdjust=500 |
| -OOMPolicy=kill |
| -` |
| - |
| -const gvisorProbeSkeleton = `[Unit] |
| -Description=carlos gVisor instance probe |
| -Requires=data-instances.mount run-carlos-instances.mount |
| -After=data-instances.mount run-carlos-instances.mount |
| -[Service] |
| -Type=exec |
| -User=root |
| -Group=root |
| -Slice=carlos-instances.slice |
| -ExecStart= |
| -Restart=no |
| -KillMode=mixed |
| -# Let attached runsc wait for the guest after ExecStop sends TERM to guest init. |
| -KillSignal=SIGCONT |
| -RuntimeMaxSec=30 |
| -TimeoutStopSec=20 |
| -StandardOutput=append:/run/carlos-control/gvisor-probe.out |
| -StandardError=journal |
| -RuntimeDirectory=carlos-runsc |
| -RuntimeDirectoryMode=0700 |
| -RuntimeDirectoryPreserve=yes |
| -ReadWritePaths=/run/carlos-runsc /data/instances/_probe /run/carlos/instances/_probe |
| -MemoryMax=301989888 |
| -MemoryHigh=271790892 |
| -MemorySwapMax=301989888 |
| -TasksMax=320 |
| -CPUQuota=50% |
| -CPUWeight=50 |
| -IOWeight=50 |
| -OOMPolicy=kill |
| -` |
| - |
| -const gvisorBuildSkeleton = `[Unit] |
| -Description=carlos gVisor offline demo build |
| -BindsTo=carlos-instance-control.service |
| -After=carlos-instance-control.service |
| -[Service] |
| -Type=exec |
| -RemainAfterExit=yes |
| -User=root |
| -Group=root |
| -Slice=carlos-demo-builds.slice |
| -ExecStart= |
| -Restart=no |
| -KillMode=control-group |
| -RuntimeMaxSec=180 |
| -TimeoutStopSec=5 |
| -SendSIGKILL=yes |
| -OOMPolicy=kill |
| -MemoryMax=1107296256 |
| -MemoryHigh=996566625 |
| -MemorySwapMax=0 |
| -TasksMax=192 |
| -CPUQuota=50% |
| -CPUWeight=10 |
| -UMask=0077 |
| -StandardInput=null |
| -StandardOutput=null |
| -StandardError=null |
| -RuntimeDirectory=carlos-runsc |
| -RuntimeDirectoryMode=0700 |
| -RuntimeDirectoryPreserve=yes |
| -ReadWritePaths=/run/carlos-runsc |
| -` |
diff --git a/internal/host/gvisor_units_test.go b/internal/host/gvisor_units_test.go| new file mode 100644 |
| index 000000000..792fa7c3b |
| --- /dev/null |
| +++ b/internal/host/gvisor_units_test.go |
| @@ -0,0 +1,169 @@ |
| +package host |
| + |
| +import ( |
| + "encoding/json" |
| + "errors" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/execsandbox" |
| + "github.com/carlosframework/platform/internal/instancectl" |
| +) |
| + |
| +// Preserve the exact measurement input. The template comparison below requires |
| +// the shipped units and pinned properties to match these tested bytes. |
| +const gvisorCandidateHardening = `ProtectSystem=strict |
| +ProtectHome=yes |
| +PrivateTmp=yes |
| +ProtectKernelTunables=yes |
| +ProtectKernelModules=yes |
| +ProtectKernelLogs=yes |
| +ProtectClock=yes |
| +ProtectHostname=yes |
| +ProtectControlGroups=yes |
| +LockPersonality=yes |
| +RestrictRealtime=yes |
| +SystemCallArchitectures=native |
| +` |
| + |
| +func TestGVisorMeasuredTemplates(t *testing.T) { |
| + checkGVisorMeasuredTemplates(t) |
| +} |
| + |
| +func checkGVisorMeasuredTemplates(t *testing.T) { |
| + t.Helper() |
| + if execsandbox.GVisorHardeningLines() != gvisorCandidateHardening { |
| + t.Fatal("pinned hardening differs from measured candidates") |
| + } |
| + for name, skeleton := range map[string]string{ |
| + "carlos-instance-gvisor@.service": gvisorRunSkeleton, |
| + "carlos-instance-gvisor-probe.service": gvisorProbeSkeleton, |
| + "carlos-demo-build-gvisor@.service": gvisorBuildSkeleton, |
| + } { |
| + raw, err := unitsFS.ReadFile("units/" + name) |
| + if err != nil || string(raw) != skeleton+execsandbox.GVisorHardeningLines() { |
| + t.Errorf("%s differs from the measured skeleton and hardening: %v", name, err) |
| + } |
| + } |
| +} |
| + |
| +func gvisorProbeReport(raw []byte, nonce string) (instancectl.ProbeReport, error) { |
| + var report instancectl.ProbeReport |
| + if err := json.Unmarshal(raw, &report); err != nil { |
| + return report, err |
| + } |
| + if nonce == "" || report.Nonce != nonce { |
| + return report, errors.New("probe nonce mismatch") |
| + } |
| + return report, nil |
| +} |
| + |
| +func TestGVisorProbeReportNonce(t *testing.T) { |
| + for _, tc := range []struct { |
| + raw, nonce string |
| + ok bool |
| + }{ |
| + {`{"nonce":"fresh","ready":false}`, "fresh", true}, |
| + {`{"nonce":"stale"}`, "fresh", false}, |
| + {`{}`, "fresh", false}, |
| + {`{}`, "", false}, |
| + {`{"nonce":"fresh"} {"nonce":"fresh"}`, "fresh", false}, |
| + {`{"nonce":`, "fresh", false}, |
| + } { |
| + if _, err := gvisorProbeReport([]byte(tc.raw), tc.nonce); (err == nil) != tc.ok { |
| + t.Errorf("report=%q nonce=%q: %v", tc.raw, tc.nonce, err) |
| + } |
| + } |
| +} |
| + |
| +const gvisorRunSkeleton = `[Unit] |
| +Description=carlos gVisor demo instance %I |
| +BindsTo=carlos-edge.service |
| +Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +[Service] |
| +Type=exec |
| +User=root |
| +Group=root |
| +Slice=carlos-instances-%i.slice |
| +ExecStart= |
| +Restart=no |
| +KillMode=mixed |
| +# Let attached runsc wait for the guest after ExecStop sends TERM to guest init. |
| +KillSignal=SIGCONT |
| +TimeoutStopSec=20 |
| +SuccessExitStatus=129 130 141 143 |
| +StandardOutput=file:/run/carlos/logs/%I.run.sock |
| +StandardError=inherit |
| +RuntimeDirectory=carlos-runsc |
| +RuntimeDirectoryMode=0700 |
| +RuntimeDirectoryPreserve=yes |
| +ReadWritePaths=/run/carlos-runsc /data/instances/%I /run/carlos/instances/%I |
| +TasksMax=320 |
| +OOMScoreAdjust=500 |
| +OOMPolicy=kill |
| +` |
| + |
| +const gvisorProbeSkeleton = `[Unit] |
| +Description=carlos gVisor instance probe |
| +Requires=data-instances.mount run-carlos-instances.mount |
| +After=data-instances.mount run-carlos-instances.mount |
| +[Service] |
| +Type=exec |
| +User=root |
| +Group=root |
| +Slice=carlos-instances.slice |
| +ExecStart= |
| +Restart=no |
| +KillMode=mixed |
| +# Let attached runsc wait for the guest after ExecStop sends TERM to guest init. |
| +KillSignal=SIGCONT |
| +RuntimeMaxSec=30 |
| +TimeoutStopSec=20 |
| +StandardOutput=append:/run/carlos-control/gvisor-probe.out |
| +StandardError=journal |
| +RuntimeDirectory=carlos-runsc |
| +RuntimeDirectoryMode=0700 |
| +RuntimeDirectoryPreserve=yes |
| +ReadWritePaths=/run/carlos-runsc /data/instances/_probe /run/carlos/instances/_probe |
| +MemoryMax=301989888 |
| +MemoryHigh=271790892 |
| +MemorySwapMax=301989888 |
| +TasksMax=320 |
| +CPUQuota=50% |
| +CPUWeight=50 |
| +IOWeight=50 |
| +OOMPolicy=kill |
| +` |
| + |
| +const gvisorBuildSkeleton = `[Unit] |
| +Description=carlos gVisor offline demo build |
| +BindsTo=carlos-instance-control.service |
| +After=carlos-instance-control.service |
| +[Service] |
| +Type=exec |
| +RemainAfterExit=yes |
| +User=root |
| +Group=root |
| +Slice=carlos-demo-builds.slice |
| +ExecStart= |
| +Restart=no |
| +KillMode=control-group |
| +RuntimeMaxSec=180 |
| +TimeoutStopSec=5 |
| +SendSIGKILL=yes |
| +OOMPolicy=kill |
| +MemoryMax=1107296256 |
| +MemoryHigh=996566625 |
| +MemorySwapMax=0 |
| +TasksMax=192 |
| +CPUQuota=50% |
| +CPUWeight=10 |
| +UMask=0077 |
| +StandardInput=null |
| +StandardOutput=null |
| +StandardError=null |
| +RuntimeDirectory=carlos-runsc |
| +RuntimeDirectoryMode=0700 |
| +RuntimeDirectoryPreserve=yes |
| +ReadWritePaths=/run/carlos-runsc |
| +` |
diff --git a/internal/instanceexec/sandbox_linux_test.go b/internal/instanceexec/sandbox_linux_test.go| index 590047878..e978a711d 100644 |
| --- a/internal/instanceexec/sandbox_linux_test.go |
| +++ b/internal/instanceexec/sandbox_linux_test.go |
| @@ -205,20 +205,35 @@ func TestGVisorExitRecordOwned(t *testing.T) { |
| t.Fatal("overwrote record") |
| } |
| for _, component := range []string{"leaf", "parent"} { |
| - run := makeRun() |
| - link := filepath.Join(run, name) |
| - if component == "parent" { |
| - link = filepath.Join(run, "instances", host) |
| - if err := os.Remove(link); err != nil { |
| + t.Run(component, func(t *testing.T) { |
| + run := makeRun() |
| + link := filepath.Join(run, name) |
| + targetDir := t.TempDir() |
| + targetFile := filepath.Join(targetDir, instancectl.ExitRecordName(instancectl.RoleRun, inv)) |
| + target := targetFile |
| + if component == "parent" { |
| + link = filepath.Join(run, "instances", host) |
| + target = targetDir |
| + if err := os.Remove(link); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := os.Symlink(target, link); err != nil { |
| t.Fatal(err) |
| } |
| - } |
| - if err := os.Symlink(filepath.Join(native, name), link); err != nil { |
| - t.Fatal(err) |
| - } |
| - if err := writeExitRecordOwned(run, instancectl.RoleRun, host, env, now, os.Getuid(), os.Getgid()); err == nil { |
| - t.Fatalf("followed %s symlink", component) |
| - } |
| + err := writeExitRecordOwned(run, instancectl.RoleRun, host, env, now, os.Getuid(), os.Getgid()) |
| + refused := errors.Is(err, instancefs.ErrSymlink) |
| + if component == "leaf" && err != nil { |
| + // O_EXCL can refuse the leaf before openat2 resolves the symlink. |
| + refused = refused || errors.Is(err, os.ErrExist) || err.Error() == "record for invocation "+inv+" already exists" |
| + } |
| + if !refused { |
| + t.Errorf("unexpected %s symlink refusal: %v", component, err) |
| + } |
| + if _, err := os.Lstat(targetFile); !errors.Is(err, os.ErrNotExist) { |
| + t.Errorf("%s symlink target gained a record: %v", component, err) |
| + } |
| + }) |
| } |
| for _, args := range [][]string{{"--sandbox=gvisor", "--rootfs", "run", host}, {"--sandbox=gvisor", "seed", host}, {"--sandbox=gvisor", "run", host + ":1"}} { |
| if _, err := parseExitArgs(args); err == nil { |