Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/sandbox/runtime_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
48 changes: 45 additions & 3 deletions internal/sandbox/windows_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve an existing non-workspace grant that matches the runtime root

When the profile already grants the runtime directory as its last non-workspace write root, permissionProfileWithRuntime returns without appending anything. This condition still removes that existing grant. The setup marker includes it, but command validation now computes a different ACL plan and refuses the command as out of date. Re-running setup repeats the mismatch. The workspace-root check protects only one kind of pre-existing grant.

The fixture uses the actual producer, asserts that it did not append a root, writes the setup marker, then validates the command profile. In Windows, Linux and macOS CI, main 2c509ce9 passes; head f015c7ed fails with acl entries 2, want 1.

Keep explicit grants in the marker and remove only an entry known to have been introduced by runtime augmentation. Please cover the existing-grant case outside the workspace as well as the newly appended case.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this — you were right, v1 stripped a pre-existing grant whenever it happened to be the last write root. Fixed by recording the augmentation itself: permissionProfileWithRuntime now sets SandboxRuntime.Appended only when it actually appends the runtime root, and markerCommandConfig strips the last entry only in that case. An explicit grant for the same root stays in the marker. Added TestWindowsSandboxSetupMarkerPreservesExistingRuntimeGrant covering your case (it fails on v1 with acl entries 2, want 1 and passes now); the existing appended-root test now goes through the real producer. Full internal/sandbox suite passes, including -race.

@gnanam1990 gnanam1990 Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rechecked 8105fd63 with the original reproduction. The existing non-workspace runtime grant is retained: the producer leaves Appended false, and marker calculation keeps that grant.

The same fixture passes on main and this head, and still fails on f015c7ed, under the race detector on Windows and Linux. The supplemental macOS job was cancelled without running its steps. This resolves my original finding. This fixture checks marker calculation and grant provenance; it does not exercise a live restricted-token launch.

// 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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
103 changes: 103 additions & 0 deletions internal/sandbox/windows_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading