From 9cec2b89245f41f2126f9a787c5e43a0d84b7154 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Wed, 4 Nov 2026 12:15:39 -0800 Subject: [PATCH] pal: support long paths in secure file and directory creation On Windows, create_directory_with_security and create_file_with_security passed their paths to CreateDirectoryW and CreateFileW as given. Without the extended-length \\?\ prefix, CreateDirectoryW refuses a path of 248 or more UTF-16 code units, with ERROR_FILENAME_EXCED_RANGE, and both functions refuse one of 260 or more, with ERROR_PATH_NOT_FOUND. openvmm.exe declares no longPathAware manifest, so these limits apply whatever the host's long-path policy. A snapshot capture below a long destination parent therefore failed to create its staging directory: failed to create staging directory ...\.snapshot.staging-...: The system cannot find the path specified. (os error 3) The rest of the publication already handles long paths: it goes through std, which adds the prefix itself, or through handle-relative opens and hard links. Encode the paths of both functions with secure_create_path. It first resolves the path with std::path::absolute, as Win32 would, since the file system takes an extended-length path without normalizing its separators or its . and .. components. From 248 code units, it prefixes a disk path with \\?\ and replaces the leading separators of a UNC path with \\?\UNC\. Shorter paths, and verbatim and device paths, keep their form. So do NT paths, \??\..., which absolute would resolve against the current drive, and empty paths, which absolute refuses but Win32 reports with its own error. Names stay UTF-16 throughout, and a path that holds a NUL is refused as invalid input rather than truncated. The security attributes, access, sharing, CREATE_NEW disposition, and error propagation are unchanged, so each object still gets its DACL atomically at creation. Tests create directories and files at 247 and 248 code units, and below a parent of over 320 by an ordinary path, through child\.., by a verbatim path, and with a non-BMP name; read their contents and DACLs back; and recreate them, which fails with AlreadyExists. Others check the encoding of disk, UNC, relative, verbatim, NT, device, and empty paths, and create objects through an NT path. Publication tests publish a snapshot with a paired scratch image below a parent of over 320 code units, by copying the RAM file and, on Linux and Windows, by hard-linking it; read its manifest back; verify the scratch digest; and check that no staging directory remains. Without the fix, the PAL test of long paths fails at 248 code units, and the publication tests fail with the error above. See microsoft/nvx#1263. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../openvmm_helpers/src/snapshot/publish.rs | 105 +++++++++ support/pal/src/windows/security.rs | 207 +++++++++++++++++- 2 files changed, 306 insertions(+), 6 deletions(-) diff --git a/openvmm/openvmm_helpers/src/snapshot/publish.rs b/openvmm/openvmm_helpers/src/snapshot/publish.rs index 5ee102aa8d0..3116858e180 100644 --- a/openvmm/openvmm_helpers/src/snapshot/publish.rs +++ b/openvmm/openvmm_helpers/src/snapshot/publish.rs @@ -615,6 +615,111 @@ mod tests { assert!(error.to_string().contains("incomplete")); } + /// Snapshot fixtures below a directory beyond the legacy Win32 limits of + /// directory and file paths. + struct LongPathPublication { + _dir: tempfile::TempDir, + parent: PathBuf, + memory_path: PathBuf, + scratch_path: PathBuf, + manifest: SnapshotManifest, + } + + impl LongPathPublication { + fn new() -> Self { + let dir = tempfile::tempdir().unwrap(); + let mut long = dir.path().to_owned(); + while long.as_os_str().len() <= 320 { + long.push("long-snapshot-path-component"); + } + let fixtures = long.join("fixtures"); + let parent = long.join("snapshots"); + std::fs::create_dir_all(&fixtures).unwrap(); + std::fs::create_dir(&parent).unwrap(); + let memory_path = fixtures.join("memory.bin"); + let scratch_path = fixtures.join("scratch.img"); + let scratch = vec![0x5a_u8; 1024]; + std::fs::write(&memory_path, vec![0xa5_u8; 1024]).unwrap(); + std::fs::write(&scratch_path, &scratch).unwrap(); + Self { + _dir: dir, + parent, + memory_path, + scratch_path, + manifest: paired_scratch_manifest(&scratch), + } + } + + /// Checks that the parent holds only `snap_dir`, a published snapshot + /// whose manifest, state, memory, and paired scratch read back intact. + fn check_published(&self, snap_dir: &Path) { + let read_manifest = read_snapshot_manifest(snap_dir).unwrap(); + let verified = open_paired_scratch_file(snap_dir, &read_manifest) + .unwrap() + .unwrap(); + assert_eq!( + file_sha256(&verified, 1024, SCRATCH_FILE_NAME).unwrap(), + self.manifest + .machine_contract + .as_ref() + .unwrap() + .microvm_sandbox_blocks[1] + .identity + ); + assert_eq!( + std::fs::read(snap_dir.join(MEMORY_FILE_NAME)).unwrap(), + vec![0xa5_u8; 1024] + ); + assert_eq!( + std::fs::read(snap_dir.join(STATE_FILE_NAME)).unwrap(), + b"state" + ); + let entries: Vec<_> = std::fs::read_dir(&self.parent) + .unwrap() + .map(|entry| entry.unwrap().file_name()) + .collect(); + assert_eq!(entries, vec![snap_dir.file_name().unwrap().to_owned()]); + } + } + + #[test] + fn snapshots_are_published_under_long_paths() { + let publication = LongPathPublication::new(); + let snap_dir = publication.parent.join("snap"); + write_snapshot_from_memory_and_scratch_files( + &snap_dir, + &publication.manifest, + b"state", + &std::fs::File::open(&publication.memory_path).unwrap(), + Some(&std::fs::File::open(&publication.scratch_path).unwrap()), + ) + .unwrap(); + publication.check_published(&snap_dir); + } + + #[cfg(any(target_os = "linux", windows))] + #[test] + fn owned_memory_snapshots_are_published_under_long_paths() { + let publication = LongPathPublication::new(); + let snap_dir = publication.parent.join("snap"); + let memory_file = std::fs::OpenOptions::new() + .read(true) + .write(true) + .open(&publication.memory_path) + .unwrap(); + write_snapshot_from_owned_memory_and_scratch_files( + &snap_dir, + &publication.manifest, + b"state", + &memory_file, + Some(&std::fs::File::open(&publication.scratch_path).unwrap()), + ) + .unwrap(); + let published = std::fs::File::open(snap_dir.join(MEMORY_FILE_NAME)).unwrap(); + verify_hard_link_identity(&memory_file, &published, 1024).unwrap(); + publication.check_published(&snap_dir); + } + #[test] fn generation_identity_uses_structure_without_rehashing_scratch() { let dir = tempfile::tempdir().unwrap(); diff --git a/support/pal/src/windows/security.rs b/support/pal/src/windows/security.rs index 20283c8382f..b2a1cfac825 100644 --- a/support/pal/src/windows/security.rs +++ b/support/pal/src/windows/security.rs @@ -11,7 +11,9 @@ use std::io::ErrorKind; use std::marker::PhantomData; use std::ops::Deref; use std::os::windows::prelude::*; +use std::path::Component; use std::path::Path; +use std::path::Prefix; use std::ptr::NonNull; use std::ptr::null_mut; use std::str::FromStr; @@ -389,13 +391,54 @@ where } } +/// Encodes `path` for the Win32 creation functions. Without the extended-length +/// `\\?\` prefix, `CreateDirectoryW` refuses a path of 248 or more UTF-16 code +/// units and `CreateFileW` one of 260 or more. +/// +/// The path is resolved, as Win32 resolves it, before the prefix is added, +/// since the file system takes an extended-length path as given. Win32 also +/// takes an NT path, `\??\...`, as given, and refuses an empty path itself, so +/// both keep their form. +fn secure_create_path(path: &Path) -> std::io::Result { + fn encode(path: &Path) -> std::io::Result { + U16CString::from_os_str(path.as_os_str()) + .map_err(|error| std::io::Error::new(ErrorKind::InvalidInput, error)) + } + + let raw = path.as_os_str(); + if raw.is_empty() || raw.encode_wide().take(4).eq(r"\??\".encode_utf16()) { + return encode(path); + } + let path = std::path::absolute(path)?; + let name = encode(&path)?; + if name.len() < 248 { + return Ok(name); + } + let (prefix, skip) = match path.components().next() { + Some(Component::Prefix(prefix)) => match prefix.kind() { + Prefix::Disk(_) => (r"\\?\", 0), + Prefix::UNC(..) => (r"\\?\UNC\", 2), + _ => ("", 0), + }, + _ => ("", 0), + }; + U16CString::from_vec( + prefix + .encode_utf16() + .chain(name.as_slice().iter().copied().skip(skip)) + .collect::>(), + ) + .map_err(|error| std::io::Error::new(ErrorKind::InvalidInput, error)) +} + /// Creates a directory with `security_descriptor` applied atomically. +/// +/// `path` may exceed the legacy Win32 `MAX_PATH` limit. pub fn create_directory_with_security( path: &Path, security_descriptor: &SecurityDescriptor, ) -> std::io::Result<()> { - let path = U16CString::from_os_str(path.as_os_str()) - .map_err(|error| std::io::Error::new(ErrorKind::InvalidInput, error))?; + let path = secure_create_path(path)?; let attributes = SECURITY_ATTRIBUTES { nLength: size_of::() as u32, lpSecurityDescriptor: security_descriptor.as_ptr(), @@ -410,12 +453,13 @@ pub fn create_directory_with_security( } /// Exclusively creates a regular file with `security_descriptor` applied atomically. +/// +/// `path` may exceed the legacy Win32 `MAX_PATH` limit. pub fn create_file_with_security( path: &Path, security_descriptor: &SecurityDescriptor, ) -> std::io::Result { - let path = U16CString::from_os_str(path.as_os_str()) - .map_err(|error| std::io::Error::new(ErrorKind::InvalidInput, error))?; + let path = secure_create_path(path)?; let attributes = SECURITY_ATTRIBUTES { nLength: size_of::() as u32, lpSecurityDescriptor: security_descriptor.as_ptr(), @@ -451,6 +495,7 @@ mod tests { use std::io::Read; use std::io::Seek; use std::io::Write; + use std::os::windows::ffi::OsStrExt; use std::os::windows::ffi::OsStringExt; use std::path::PathBuf; use test_with_tracing::test; @@ -495,9 +540,12 @@ mod tests { "D:P(A;;FA;;;WD)".parse().unwrap() } + fn utf16_len(path: &Path) -> usize { + path.as_os_str().encode_wide().count() + } + fn dacl_sddl(path: &Path) -> io::Result { - let path = U16CString::from_os_str(path.as_os_str()) - .map_err(|error| io::Error::new(ErrorKind::InvalidInput, error))?; + let path = secure_create_path(path)?; let mut required = 0; unsafe { GetFileSecurityW( @@ -587,6 +635,153 @@ mod tests { assert_eq!(dacl_sddl(&file).unwrap(), file_dacl); } + #[test] + fn secure_create_path_encodes_long_paths() { + fn encode(path: &str) -> String { + String::from_utf16(secure_create_path(Path::new(path)).unwrap().as_slice()).unwrap() + } + let name = |length| "a".repeat(length); + + // Disk paths keep their form below 248 UTF-16 code units. + let short = format!(r"C:\{}", name(244)); + let long = format!(r"C:\{}", name(245)); + assert_eq!(encode(&short), short); + assert_eq!(encode(&long), format!(r"\\?\{long}")); + + // UNC paths replace their leading separators. + let short = format!(r"\\server\share\{}", name(232)); + let long = format!(r"\\server\share\{}", name(233)); + assert_eq!(encode(&short), short); + assert_eq!(encode(&long), format!(r"\\?\UNC\{}", &long[2..])); + + // Separators, `.`, `..`, and relative paths are resolved first. + assert_eq!( + encode(&format!("C:/{}/./child/../leaf", name(300))), + format!(r"\\?\C:\{}\leaf", name(300)) + ); + let relative = std::env::current_dir().unwrap().join(name(300)); + assert_eq!(encode(&name(300)), encode(relative.to_str().unwrap())); + + // Verbatim, NT, and device paths are already in their final form, and + // Win32 reports its own error for an empty path. + for path in [ + format!(r"\\?\C:\{}", name(300)), + format!(r"\\?\UNC\server\share\{}", name(300)), + format!(r"\??\C:\{}", name(300)), + format!(r"\\.\C:\{}", name(300)), + String::new(), + ] { + assert_eq!(encode(&path), path); + } + + assert_eq!( + secure_create_path(Path::new(&format!("C:\\{}\0b", name(300)))) + .unwrap_err() + .kind(), + ErrorKind::InvalidInput + ); + } + + #[test] + fn secure_creation_supports_long_paths() { + let root = TestDirectory::new(); + let descriptor = protected_descriptor(); + let expected = descriptor.to_sddl().unwrap(); + + // Straddle the 248-unit limit of legacy Win32 directory paths. + for length in [247, 248] { + let name_length = length - utf16_len(&root.0) - 1; + let directory = root.join("d".repeat(name_length)); + let file = root.join("f".repeat(name_length)); + assert_eq!(utf16_len(&directory), length); + create_directory_with_security(&directory, &descriptor).unwrap(); + create_file_with_security(&file, &descriptor).unwrap(); + assert_eq!(dacl_sddl(&directory).unwrap(), expected); + assert_eq!(dacl_sddl(&file).unwrap(), expected); + } + + // Exceed the legacy limits of both directory and file paths. + let mut parent = root.join("long"); + while utf16_len(&parent) <= 320 { + parent.push("long-path-component"); + } + fs::create_dir_all(&parent).unwrap(); + let mut verbatim_parent = OsString::from(r"\\?\"); + verbatim_parent.push(&parent); + let verbatim_parent = PathBuf::from(verbatim_parent); + assert!(matches!( + verbatim_parent.components().next(), + Some(Component::Prefix(prefix)) if matches!(prefix.kind(), Prefix::VerbatimDisk(_)) + )); + + let cases = [ + (parent.clone(), "ordinary"), + (parent.join("child").join(".."), "resolved"), + (verbatim_parent, "verbatim"), + (parent.clone(), "astral-\u{1F600}"), + ]; + let mut names = Vec::new(); + for (requested_parent, case) in cases { + let directory_name = format!("{case}-directory"); + let file_name = format!("{case}-file"); + let requested_directory = requested_parent.join(&directory_name); + let requested_file = requested_parent.join(&file_name); + + create_directory_with_security(&requested_directory, &descriptor).unwrap(); + let mut file = create_file_with_security(&requested_file, &descriptor).unwrap(); + file.write_all(case.as_bytes()).unwrap(); + drop(file); + + let directory = parent.join(&directory_name); + let file = parent.join(&file_name); + assert!(fs::metadata(&directory).unwrap().is_dir(), "{case}"); + assert_eq!(fs::read(&file).unwrap(), case.as_bytes(), "{case}"); + assert_eq!(dacl_sddl(&directory).unwrap(), expected, "{case}"); + assert_eq!(dacl_sddl(&file).unwrap(), expected, "{case}"); + + assert_eq!( + create_directory_with_security(&requested_directory, &descriptor) + .unwrap_err() + .kind(), + ErrorKind::AlreadyExists, + "{case}" + ); + assert_eq!( + create_file_with_security(&requested_file, &descriptor) + .unwrap_err() + .kind(), + ErrorKind::AlreadyExists, + "{case}" + ); + assert_eq!(fs::read(&file).unwrap(), case.as_bytes(), "{case}"); + names.extend([OsString::from(directory_name), OsString::from(file_name)]); + } + + // Every object has its exact UTF-16 name, and `child` was never created. + let mut created: Vec<_> = fs::read_dir(&parent) + .unwrap() + .map(|entry| entry.unwrap().file_name()) + .collect(); + created.sort(); + names.sort(); + assert_eq!(created, names); + } + + #[test] + fn nt_paths_keep_their_form() { + let root = TestDirectory::new(); + let descriptor = protected_descriptor(); + let mut nt_root = OsString::from(r"\??\"); + nt_root.push(&root.0); + let nt_root = PathBuf::from(nt_root); + + create_directory_with_security(&nt_root.join("directory"), &descriptor).unwrap(); + create_file_with_security(&nt_root.join("file"), &descriptor).unwrap(); + + assert!(fs::metadata(root.join("directory")).unwrap().is_dir()); + assert!(fs::metadata(root.join("file")).unwrap().is_file()); + } + #[test] fn created_file_is_read_write_and_fully_shared() { let root = TestDirectory::new();