diff --git a/internal/sandbox/runtime_state.go b/internal/sandbox/runtime_state.go index 4a5fdfc9a..958b6cab7 100644 --- a/internal/sandbox/runtime_state.go +++ b/internal/sandbox/runtime_state.go @@ -31,6 +31,11 @@ type SandboxRuntime struct { Cache string `json:"cache,omitempty"` Data string `json:"data,omitempty"` Temp string `json:"temp,omitempty"` + // Appended reports whether permissionProfileWithRuntime introduced the + // runtime root into the permission profile. It is false when the profile + // already granted the runtime root: that explicit grant belongs to the + // profile, not to the runtime, and the setup marker must keep it. + Appended bool `json:"appended,omitempty"` } func prepareSandboxRuntime(workspaceRoot string) (SandboxRuntime, func(), error) { @@ -223,6 +228,11 @@ func permissionProfileWithRuntime(profile PermissionProfile, runtimeState Sandbo return profile } } + // This entry is introduced by runtime augmentation. Record it on the + // runtime state (profile.Runtime points at this same value) so the setup + // marker can exclude exactly this entry — and no pre-existing grant that + // merely coincides with the runtime root. + runtimeState.Appended = true profile.FileSystem.WriteRoots = append(profile.FileSystem.WriteRoots, WritableRoot{Root: runtimeState.Root}) return profile } diff --git a/internal/sandbox/windows_setup.go b/internal/sandbox/windows_setup.go index ac7366a7c..887b30ae7 100644 --- a/internal/sandbox/windows_setup.go +++ b/internal/sandbox/windows_setup.go @@ -171,8 +171,50 @@ func WindowsSandboxSetupConfigFromCommand(config WindowsSandboxCommandConfig) Wi } } +// markerCommandConfig returns the command config used to fingerprint the +// setup marker: a copy of config whose permission profile no longer contains +// the sandbox runtime root appended by permissionProfileWithRuntime. Only an +// entry known to have been introduced by runtime augmentation is removed +// (SandboxRuntime.Appended): a pre-existing explicit grant for the same root +// stays, because the setup profile contains it too. The caller's profile is +// never mutated. +func markerCommandConfig(config WindowsSandboxSetupConfig) WindowsSandboxCommandConfig { + command := config.commandConfig() + runtime := command.PermissionProfile.Runtime + if runtime == nil || !runtime.Appended { + return command + } + root := windowsCapabilityPathKey(runtime.Root) + if root == "" { + return command + } + roots := command.PermissionProfile.FileSystem.WriteRoots + if n := len(roots); n > 0 && windowsCapabilityPathKey(roots[n-1].Root) == root && !windowsRootMatchesWorkspace(runtime.Root, config.WorkspaceRoots) { + // permissionProfileWithRuntime appends the runtime root at the end + // of WriteRoots, only when it is not already present. Strip + // exactly that appended entry: a root that merely coincides with + // the runtime root — e.g. the workspace root itself — must stay, + // because the setup profile contains it too. + trimmed := make([]WritableRoot, n-1) + copy(trimmed, roots[:n-1]) + command.PermissionProfile.FileSystem.WriteRoots = trimmed + } + return command +} + func BuildWindowsSandboxSetupMarker(config WindowsSandboxSetupConfig) (WindowsSandboxSetupMarker, error) { - plan, err := BuildWindowsACLPlan(config.commandConfig()) + // The marker must be computable from the same inputs on both sides: + // `zero sandbox setup` writes it from a profile that never carries a + // runtime root, while every real command validates it from a profile to + // which the runner appended one (permissionProfileWithRuntime). For + // workspaces containing the user cache directory the fallback runtime + // root is additionally random per process, so no stored marker could + // ever match a plan that includes it. The runtime root is therefore + // excluded from the marker fingerprint only — the execution ACL plan + // still applies its ACLs and the restricted token still carries its + // capability SID. + markerConfig := markerCommandConfig(config) + plan, err := BuildWindowsACLPlan(markerConfig) if err != nil { return WindowsSandboxSetupMarker{}, err } @@ -183,7 +225,7 @@ func BuildWindowsSandboxSetupMarker(config WindowsSandboxSetupConfig) (WindowsSa // Fingerprint the mode-INDEPENDENT network infrastructure (block filters // scoped to the offline-marker SID), NOT the per-command network mode, so the // marker validates for both allow and deny commands against this one setup. - infraPlan, err := BuildWindowsNetworkInfraPlan(config.commandConfig()) + infraPlan, err := BuildWindowsNetworkInfraPlan(markerConfig) if err != nil { return WindowsSandboxSetupMarker{}, err } @@ -260,7 +302,7 @@ func ValidateWindowsSandboxSetupMarker(config WindowsSandboxSetupConfig) error { return fmt.Errorf("windows sandbox setup is out of date: schema %d, want %d", actual.SchemaVersion, expected.SchemaVersion) } if actual.ACLPlanHash != expected.ACLPlanHash || actual.ACLPlanEntries != expected.ACLPlanEntries { - return errors.New("windows sandbox setup is out of date: permission roots or deny lists changed") + return fmt.Errorf("windows sandbox setup is out of date: permission roots or deny lists changed (acl entries %d, want %d; acl plan hash %s, want %s)", actual.ACLPlanEntries, expected.ACLPlanEntries, actual.ACLPlanHash, expected.ACLPlanHash) } // Mode-agnostic: validate the provisioned infrastructure, never the // per-command network mode — so an approved (allow) network command and an diff --git a/internal/sandbox/windows_setup_test.go b/internal/sandbox/windows_setup_test.go index ecb76831a..7ed1abeaa 100644 --- a/internal/sandbox/windows_setup_test.go +++ b/internal/sandbox/windows_setup_test.go @@ -157,6 +157,109 @@ func TestWindowsSandboxSetupMarkerValidatesBothNetworkModes(t *testing.T) { } } +// The runner appends the sandbox runtime root to every real command profile +// (permissionProfileWithRuntime), while `zero sandbox setup` never sees one — +// and for workspaces containing the user cache directory the fallback runtime +// root is random per process. A marker written by the setup must therefore +// validate for a command profile that carries the runtime root, and the marker +// itself must be identical with and without it. +func TestWindowsSandboxSetupMarkerIgnoresRuntimeRoot(t *testing.T) { + home := t.TempDir() + base := WindowsSandboxSetupConfig{ + SandboxHome: home, + CommandCWD: `C:\workspace`, + WorkspaceRoots: []string{`C:\workspace`}, + PermissionProfile: PermissionProfile{ + FileSystem: FileSystemPolicy{ + Kind: FileSystemRestricted, + WriteRoots: []WritableRoot{{Root: `C:\workspace`}}, + }, + Network: NetworkPolicy{Mode: NetworkDeny}, + }, + } + // Set up once, as `zero sandbox setup` would (no runtime root). + if _, err := WriteWindowsSandboxSetupMarker(base); err != nil { + t.Fatalf("WriteWindowsSandboxSetupMarker: %v", err) + } + // A real command profile carries the runtime root appended by the runner + // (through the actual producer, as the runner does it). + runtime := base + runtime.PermissionProfile = permissionProfileWithRuntime(base.PermissionProfile, SandboxRuntime{Root: `C:\workspace\runtime`}) + if got := len(runtime.PermissionProfile.FileSystem.WriteRoots); got != 2 { + t.Fatalf("fixture invalid: producer did not append the runtime root; got %d entries", got) + } + if err := ValidateWindowsSandboxSetupMarker(runtime); err != nil { + t.Fatalf("command profile with runtime root must validate against the setup marker, got: %v", err) + } + // The marker itself must be identical with and without the runtime root. + without, err := BuildWindowsSandboxSetupMarker(base) + if err != nil { + t.Fatalf("BuildWindowsSandboxSetupMarker without runtime: %v", err) + } + with, err := BuildWindowsSandboxSetupMarker(runtime) + if err != nil { + t.Fatalf("BuildWindowsSandboxSetupMarker with runtime: %v", err) + } + if without != with { + t.Fatalf("markers differ with runtime root: %+v vs %+v", without, with) + } + // The caller's profile must be untouched (no mutation through the copy). + if got := len(base.PermissionProfile.FileSystem.WriteRoots); got != 1 { + t.Fatalf("setup profile write roots = %d, want 1 (marker must not mutate the profile)", got) + } +} + +// A pre-existing explicit grant for the runtime directory must stay in the +// marker: permissionProfileWithRuntime appends nothing when the grant is +// already there, so stripping it would make validation compute a different +// ACL plan than the setup wrote (review feedback on #1251). +func TestWindowsSandboxSetupMarkerPreservesExistingRuntimeGrant(t *testing.T) { + const workspace = `C:\workspace` + const runtimeRoot = `C:\cache\zero\runtime\v1\explicit` + + setup := WindowsSandboxSetupConfig{ + SandboxHome: t.TempDir(), + CommandCWD: workspace, + WorkspaceRoots: []string{workspace}, + PermissionProfile: PermissionProfile{ + FileSystem: FileSystemPolicy{ + Kind: FileSystemRestricted, + WriteRoots: []WritableRoot{{Root: workspace}, {Root: runtimeRoot}}, + }, + Network: NetworkPolicy{Mode: NetworkDeny}, + }, + } + // Set up once, as `zero sandbox setup` would (the grant is explicit). + if _, err := WriteWindowsSandboxSetupMarker(setup); err != nil { + t.Fatalf("WriteWindowsSandboxSetupMarker: %v", err) + } + // The runner's producer appends nothing: the grant is already there. + command := setup + command.PermissionProfile = permissionProfileWithRuntime(setup.PermissionProfile, SandboxRuntime{Root: runtimeRoot}) + if got := len(command.PermissionProfile.FileSystem.WriteRoots); got != 2 { + t.Fatalf("fixture invalid: producer appended a root; got %d entries", got) + } + if err := ValidateWindowsSandboxSetupMarker(command); err != nil { + t.Fatalf("pre-existing explicit grant was removed from the command marker: %v", err) + } + // The marker itself must be identical with and without the runtime root. + without, err := BuildWindowsSandboxSetupMarker(setup) + if err != nil { + t.Fatalf("BuildWindowsSandboxSetupMarker without runtime: %v", err) + } + with, err := BuildWindowsSandboxSetupMarker(command) + if err != nil { + t.Fatalf("BuildWindowsSandboxSetupMarker with runtime: %v", err) + } + if without != with { + t.Fatalf("markers differ with pre-existing runtime grant: %+v vs %+v", without, with) + } + // The setup profile must be untouched (no mutation through the copy). + if got := len(setup.PermissionProfile.FileSystem.WriteRoots); got != 2 { + t.Fatalf("setup profile write roots = %d, want 2 (marker must not mutate the profile)", got) + } +} + // A pre-v5 marker on disk must be rejected as out of date so the schema bump // forces a clean re-setup (old markers had legacy DenyWrite ACEs). func TestWindowsSandboxSetupMarkerRejectsOldSchema(t *testing.T) {