Skip to content

Commit 8e35eee

Browse files
Prekshi Vyasclaude
andcommitted
fix: preserve existing directory ownership in prepare_filesystem()
prepare_filesystem() previously chowned every read_write path to run_as_user:run_as_group unconditionally, even for directories that already existed in the container image with intentional ownership. This caused image authors who set root:root on a read_write directory (to protect immutable subdirectories via DAC) to have that ownership silently overridden at sandbox start. Now only directories that prepare_filesystem() creates are chowned. Pre-existing directories are left with their image-level ownership intact. Fixes NVIDIA#783 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent c2e5256 commit 8e35eee

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

  • crates/openshell-sandbox/src

‎crates/openshell-sandbox/src/lib.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,13 +1755,17 @@ fn prepare_filesystem(policy: &SandboxPolicy) -> Result<()> {
17551755
path.display()
17561756
));
17571757
}
1758+
// Directory already exists in the image — respect the image
1759+
// author's ownership. They may have set root:root intentionally
1760+
// to protect immutable subdirectories via DAC while keeping the
1761+
// path in read_write for Landlock.
1762+
debug!(path = %path.display(), "read_write directory exists — preserving image ownership");
17581763
} else {
17591764
debug!(path = %path.display(), "Creating read_write directory");
17601765
std::fs::create_dir_all(path).into_diagnostic()?;
1766+
debug!(path = %path.display(), ?uid, ?gid, "Setting ownership on new read_write directory");
1767+
chown(path, uid, gid).into_diagnostic()?;
17611768
}
1762-
1763-
debug!(path = %path.display(), ?uid, ?gid, "Setting ownership on read_write directory");
1764-
chown(path, uid, gid).into_diagnostic()?;
17651769
}
17661770

17671771
Ok(())

0 commit comments

Comments
 (0)