From 909a6fa3736795edb09564bc3285d3ef0ceefb31 Mon Sep 17 00:00:00 2001 From: danwood <118035379+Floating-Y@users.noreply.github.com> Date: Mon, 2 Nov 2026 10:29:28 +0800 Subject: [PATCH] fix(node): preserve overlay xattrs across detach and restore --- crates/sandstorm-core/src/node/bundle.rs | 2 +- .../sandstorm-core/src/node/detach_tests.rs | 204 +++++++++++++++++- crates/sandstorm-core/src/node/runtime.rs | 11 +- 3 files changed, 208 insertions(+), 9 deletions(-) diff --git a/crates/sandstorm-core/src/node/bundle.rs b/crates/sandstorm-core/src/node/bundle.rs index e328bea3..ea48e146 100644 --- a/crates/sandstorm-core/src/node/bundle.rs +++ b/crates/sandstorm-core/src/node/bundle.rs @@ -330,7 +330,7 @@ pub(super) fn unpack_into(archive: &Path, dest: &Path) -> Result<()> { let status = Exec::new("sh") .arg("-c") .arg(format!( - "zstd -dq -T0 -c {} | tar -C {} -xf -", + "zstd -dq -T0 -c {} | tar --xattrs --xattrs-include='trusted.overlay.*' --xattrs-include='user.overlay.*' -C {} -xf -", shell_quote(&archive.to_string_lossy()), shell_quote(&dest.to_string_lossy()) )) diff --git a/crates/sandstorm-core/src/node/detach_tests.rs b/crates/sandstorm-core/src/node/detach_tests.rs index e52023b3..6332473a 100644 --- a/crates/sandstorm-core/src/node/detach_tests.rs +++ b/crates/sandstorm-core/src/node/detach_tests.rs @@ -182,13 +182,7 @@ fn successful_detach_restores_all_contents_and_cleans_local_data() { } let restored = fixture.dir.path().join("restored"); std::fs::create_dir(&restored).unwrap(); - // Match bundle::unpack_into's existing restore pipeline and flags. - let status = shell("zstd -dq -T0 -c \"$1\" | tar -C \"$2\" -xf -") - .arg(fixture.runtime.archive_path("sb1")) - .arg(&restored) - .status() - .unwrap(); - assert!(status.success()); + super::super::bundle::unpack_into(&fixture.runtime.archive_path("sb1"), &restored).unwrap(); assert_eq!( std::fs::read(restored.join("nested/it's data")).unwrap(), b"\0complete\xff" @@ -198,3 +192,199 @@ fn successful_detach_restores_all_contents_and_cleans_local_data() { b"second file" ); } + +fn set_overlay_attribute(path: &Path, name: &str) { + use std::os::unix::ffi::OsStrExt; + let path = std::ffi::CString::new(path.as_os_str().as_bytes()).unwrap(); + let name = std::ffi::CString::new(name).unwrap(); + // Both C strings and the one-byte value remain valid throughout the call. + let result = + unsafe { libc::setxattr(path.as_ptr(), name.as_ptr(), b"y".as_ptr().cast(), 1, 0) }; + assert_eq!( + result, + 0, + "setting overlay attribute: {}", + std::io::Error::last_os_error() + ); +} + +#[test] +fn detach_preserves_user_overlay_opaque_attribute() { + use std::os::unix::ffi::OsStrExt; + let fixture = Fixture::new(); + let directory = fixture.runtime.cfg.work_dir.join("layers/sb1/upper/nested"); + set_overlay_attribute(&directory, "user.overlay.opaque"); + fixture.runtime.detach("sb1").unwrap(); + let restored = fixture.dir.path().join("restored upper's"); + std::fs::create_dir(&restored).unwrap(); + super::super::bundle::unpack_into(&fixture.runtime.archive_path("sb1"), &restored).unwrap(); + let path = std::ffi::CString::new(restored.join("nested").as_os_str().as_bytes()).unwrap(); + let mut value = [0u8; 1]; + // The destination buffer has the size passed to getxattr. + let length = unsafe { + libc::getxattr( + path.as_ptr(), + c"user.overlay.opaque".as_ptr(), + value.as_mut_ptr().cast(), + value.len(), + ) + }; + assert_eq!( + length, + 1, + "reading restored attribute: {}", + std::io::Error::last_os_error() + ); + assert_eq!(value, *b"y"); +} + +struct OverlayFixture { + fixture: Option, + mounted: bool, +} + +impl OverlayFixture { + fn merged(&self) -> PathBuf { + self.fixture.as_ref().unwrap().dir.path().join("merged") + } + + fn mount(&mut self, userxattr: bool) { + let fixture = self.fixture.as_ref().unwrap(); + let root = fixture.dir.path(); + let upper = fixture.runtime.cfg.work_dir.join("layers/sb1/upper"); + let options = format!( + "lowerdir={},upperdir={},workdir={}{}", + root.join("lower").display(), + upper.display(), + root.join("overlay-work").display(), + if userxattr { ",userxattr" } else { "" } + ); + let output = Exec::new("mount") + .args(["-t", "overlay", "overlay", "-o", &options]) + .arg(self.merged()) + .output() + .unwrap(); + assert!( + output.status.success(), + "overlay mount failed (requires CAP_SYS_ADMIN): {}", + String::from_utf8_lossy(&output.stderr) + ); + self.mounted = true; + } + + fn unmount(&mut self) { + let status = Exec::new("umount").arg(self.merged()).status().unwrap(); + assert!(status.success(), "unmounting test overlay failed"); + self.mounted = false; + } +} + +impl Drop for OverlayFixture { + fn drop(&mut self) { + if self.mounted { + let unmounted = Exec::new("umount") + .arg(self.merged()) + .status() + .is_ok_and(|status| status.success()); + if !unmounted { + // Never recursively delete a mounted tree, even while unwinding a failed test. + let fixture = self.fixture.take().unwrap(); + eprintln!( + "overlay cleanup failed; retained {}", + fixture.dir.keep().display() + ); + } + } + } +} + +fn overlay_detach_round_trip(userxattr: bool) { + let fixture = Fixture::new(); + let root = fixture.dir.path(); + for directory in ["lower/deleted", "lower/replaced", "merged", "overlay-work"] { + std::fs::create_dir_all(root.join(directory)).unwrap(); + } + std::fs::write(root.join("lower/deleted/old"), b"deleted template data").unwrap(); + std::fs::write(root.join("lower/replaced/old"), b"hidden template data").unwrap(); + let mut overlay = OverlayFixture { + fixture: Some(fixture), + mounted: false, + }; + overlay.mount(userxattr); + std::fs::remove_dir_all(overlay.merged().join("deleted")).unwrap(); + std::fs::remove_dir_all(overlay.merged().join("replaced")).unwrap(); + std::fs::create_dir(overlay.merged().join("replaced")).unwrap(); + std::fs::write(overlay.merged().join("new"), b"new sandbox data").unwrap(); + assert!(!overlay.merged().join("replaced/old").exists()); + overlay.unmount(); + let fixture = overlay.fixture.as_ref().unwrap(); + fixture.runtime.detach("sb1").unwrap(); + let upper = fixture.runtime.cfg.work_dir.join("layers/sb1/upper"); + std::fs::create_dir_all(&upper).unwrap(); + super::super::bundle::unpack_into(&fixture.runtime.archive_path("sb1"), &upper).unwrap(); + overlay.mount(userxattr); + assert!(!overlay.merged().join("deleted").exists()); + assert!(!overlay.merged().join("replaced/old").exists()); + assert_eq!( + std::fs::read_dir(overlay.merged().join("replaced")) + .unwrap() + .count(), + 0 + ); + assert_eq!( + std::fs::read(overlay.merged().join("new")).unwrap(), + b"new sandbox data" + ); + overlay.unmount(); +} + +#[test] +#[ignore = "requires Linux overlayfs and CAP_SYS_ADMIN; run explicitly as root"] +fn detach_overlay_trusted_round_trip() { + overlay_detach_round_trip(false); +} + +#[test] +#[ignore = "requires Linux overlayfs userxattr and CAP_SYS_ADMIN; run explicitly as root"] +fn detach_overlay_userxattr_round_trip() { + overlay_detach_round_trip(true); +} + +#[test] +fn detach_preserves_sparse_disk_contents() { + use std::io::{Read, Seek, SeekFrom, Write}; + use std::os::unix::fs::MetadataExt; + let fixture = Fixture::new(); + let path = fixture + .runtime + .cfg + .work_dir + .join("layers/sb1/upper/disk.ext4"); + let mut disk = std::fs::File::create(path).unwrap(); + disk.write_all(b"disk header").unwrap(); + disk.seek(SeekFrom::Start(32 * 1024 * 1024)).unwrap(); + disk.write_all(b"disk tail").unwrap(); + drop(disk); + fixture.runtime.detach("sb1").unwrap(); + let restored = fixture.dir.path().join("restored"); + std::fs::create_dir(&restored).unwrap(); + super::super::bundle::unpack_into(&fixture.runtime.archive_path("sb1"), &restored).unwrap(); + let mut disk = std::fs::File::open(restored.join("disk.ext4")).unwrap(); + let metadata = disk.metadata().unwrap(); + assert_eq!(metadata.len(), 32 * 1024 * 1024 + 9); + assert!( + metadata.blocks() * 512 < metadata.len() / 2, + "restored disk must remain sparse" + ); + let mut header = [0; 11]; + disk.read_exact(&mut header).unwrap(); + assert_eq!(&header, b"disk header"); + let mut hole = [1; 16]; + disk.seek(SeekFrom::Start(16 * 1024 * 1024)).unwrap(); + disk.read_exact(&mut hole).unwrap(); + assert_eq!(hole, [0; 16]); + disk.seek(SeekFrom::Start(32 * 1024 * 1024)).unwrap(); + let mut tail = Vec::new(); + disk.read_to_end(&mut tail).unwrap(); + assert_eq!(tail, b"disk tail"); +} diff --git a/crates/sandstorm-core/src/node/runtime.rs b/crates/sandstorm-core/src/node/runtime.rs index 5f1159de..0c9247b7 100644 --- a/crates/sandstorm-core/src/node/runtime.rs +++ b/crates/sandstorm-core/src/node/runtime.rs @@ -1525,7 +1525,16 @@ impl Runtime { let mut tar = tar .arg("-C") .arg(&layer) - .args(["-S", "-cf", "-", "."]) + // Opaque directories and whiteouts must still hide the template after restore. + .args([ + "--xattrs", + "--xattrs-include=trusted.overlay.*", + "--xattrs-include=user.overlay.*", + "-S", + "-cf", + "-", + ".", + ]) .stdout(Stdio::piped()) .spawn() .context("starting tar to archive layer")?;