From d85097af2ea1151cc7ba2277c26e5e3d0927d5bf Mon Sep 17 00:00:00 2001 From: ifBars Date: Mon, 23 Nov 2026 15:20:22 -0800 Subject: [PATCH 1/3] Keep an empty import descriptor for the Windows 10 loader --- crates/victory-loader/src/prepare.rs | 48 ++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/crates/victory-loader/src/prepare.rs b/crates/victory-loader/src/prepare.rs index 9895db1..1b601b4 100644 --- a/crates/victory-loader/src/prepare.rs +++ b/crates/victory-loader/src/prepare.rs @@ -35,7 +35,7 @@ impl PreparedImage { /// /// * `IMAGE_FILE_DLL` set, entry point cleared: the OS maps it like a DLL /// and calls nothing; -/// * import and bound-import directories cleared: we bind imports +/// * import directory made empty and bound-import directory cleared: we bind imports /// ourselves, using `info.imports` from the original; /// * TLS callback pointer cleared: the OS still allocates static TLS for /// the image, but callbacks only run when we call them (after binding); @@ -64,11 +64,20 @@ pub fn prepare(original: &[u8]) -> Result { dll_characteristics, )?; - for index in [dir::IMPORT, dir::BOUND_IMPORT] { - if let Some(at) = layout::data_directory_offset(&bytes, index) { - write_u32(&mut bytes, at, 0)?; - write_u32(&mut bytes, at + 4, 0)?; - } + // Windows 10 can read the import directory when an IAT is still + // advertised. Point it at the empty descriptor that ends the table. + if let Some(at) = layout::data_directory_offset(&bytes, dir::IMPORT) { + let terminator = import_terminator(&bytes, &info, at); + write_u32(&mut bytes, at, terminator.unwrap_or(0))?; + write_u32( + &mut bytes, + at + 4, + terminator.map_or(0, |_| IMPORT_DESCRIPTOR_SIZE), + )?; + } + if let Some(at) = layout::data_directory_offset(&bytes, dir::BOUND_IMPORT) { + write_u32(&mut bytes, at, 0)?; + write_u32(&mut bytes, at + 4, 0)?; } if let Some(tls) = &info.tls @@ -101,6 +110,21 @@ pub fn prepare(original: &[u8]) -> Result { }) } +const IMPORT_DESCRIPTOR_SIZE: u32 = 20; + +/// The RVA of the all-zero descriptor at the end of the import directory. +fn import_terminator(bytes: &[u8], info: &ImageInfo, directory: usize) -> Option { + let rva = layout::read_u32(bytes, directory)?; + if rva == 0 { + return None; + } + let size = layout::read_u32(bytes, directory + 4)?; + let end_rva = rva.checked_add(size.checked_sub(IMPORT_DESCRIPTOR_SIZE)?)?; + let offset = info.rva_to_file_offset(end_rva)? as usize; + let descriptor = bytes.get(offset..offset + IMPORT_DESCRIPTOR_SIZE as usize)?; + descriptor.iter().all(|&byte| byte == 0).then_some(end_rva) +} + /// Every `IMAGE_REL_BASED_DIR64` base relocation: (file offset of its entry, /// RVA it patches). Empty if the image has no readable relocation table. fn dir64_relocations(bytes: &[u8], info: &ImageInfo) -> Vec<(usize, u32)> { @@ -173,6 +197,18 @@ mod tests { assert!(patched.is_dll()); assert_eq!(patched.entry_rva, 0); assert!(patched.imports.is_empty()); + let import = layout::data_directory_offset(&original, dir::IMPORT).unwrap(); + let rva = layout::read_u32(&original, import).unwrap(); + let size = layout::read_u32(&original, import + 4).unwrap(); + assert_ne!(rva, 0); + assert_eq!( + layout::read_u32(&prepared.bytes, import), + Some(rva + size - IMPORT_DESCRIPTOR_SIZE) + ); + assert_eq!( + layout::read_u32(&prepared.bytes, import + 4), + Some(IMPORT_DESCRIPTOR_SIZE) + ); assert_eq!(patched.dll_characteristics & dll_flags::FORCE_INTEGRITY, 0); if let Some(tls) = &patched.tls { assert_eq!(tls.callbacks_va, 0); From 4da3d63666df07246808055fa70e5eef6717e988 Mon Sep 17 00:00:00 2001 From: ifBars Date: Mon, 23 Nov 2026 15:55:36 -0800 Subject: [PATCH 2/3] Clear the IAT directory alongside the import directory Windows 10 maps the prepared image with both entries empty. The binder already retains the original slot RVAs, so preparation need not locate a linker-provided terminator. --- crates/victory-loader/src/prepare.rs | 82 +++++++++++++--------------- crates/victory-pe/src/layout.rs | 1 + 2 files changed, 40 insertions(+), 43 deletions(-) diff --git a/crates/victory-loader/src/prepare.rs b/crates/victory-loader/src/prepare.rs index 1b601b4..336f278 100644 --- a/crates/victory-loader/src/prepare.rs +++ b/crates/victory-loader/src/prepare.rs @@ -35,8 +35,8 @@ impl PreparedImage { /// /// * `IMAGE_FILE_DLL` set, entry point cleared: the OS maps it like a DLL /// and calls nothing; -/// * import directory made empty and bound-import directory cleared: we bind imports -/// ourselves, using `info.imports` from the original; +/// * import, bound-import and IAT directories cleared: we bind the original +/// IAT slots ourselves, using `info.imports`; /// * TLS callback pointer cleared: the OS still allocates static TLS for /// the image, but callbacks only run when we call them (after binding); /// its base relocation is made a no-op so the 0 survives a rebase; @@ -64,20 +64,12 @@ pub fn prepare(original: &[u8]) -> Result { dll_characteristics, )?; - // Windows 10 can read the import directory when an IAT is still - // advertised. Point it at the empty descriptor that ends the table. - if let Some(at) = layout::data_directory_offset(&bytes, dir::IMPORT) { - let terminator = import_terminator(&bytes, &info, at); - write_u32(&mut bytes, at, terminator.unwrap_or(0))?; - write_u32( - &mut bytes, - at + 4, - terminator.map_or(0, |_| IMPORT_DESCRIPTOR_SIZE), - )?; - } - if let Some(at) = layout::data_directory_offset(&bytes, dir::BOUND_IMPORT) { - write_u32(&mut bytes, at, 0)?; - write_u32(&mut bytes, at + 4, 0)?; + // Clear the IAT directory along with the import directory for Windows 10. + // The slots themselves stay in the image for our binder. + for index in [dir::IMPORT, dir::BOUND_IMPORT, dir::IAT] { + if let Some(at) = layout::data_directory_offset(&bytes, index) { + write(&mut bytes, at, &[0; layout::DATA_DIRECTORY_SIZE])?; + } } if let Some(tls) = &info.tls @@ -110,21 +102,6 @@ pub fn prepare(original: &[u8]) -> Result { }) } -const IMPORT_DESCRIPTOR_SIZE: u32 = 20; - -/// The RVA of the all-zero descriptor at the end of the import directory. -fn import_terminator(bytes: &[u8], info: &ImageInfo, directory: usize) -> Option { - let rva = layout::read_u32(bytes, directory)?; - if rva == 0 { - return None; - } - let size = layout::read_u32(bytes, directory + 4)?; - let end_rva = rva.checked_add(size.checked_sub(IMPORT_DESCRIPTOR_SIZE)?)?; - let offset = info.rva_to_file_offset(end_rva)? as usize; - let descriptor = bytes.get(offset..offset + IMPORT_DESCRIPTOR_SIZE as usize)?; - descriptor.iter().all(|&byte| byte == 0).then_some(end_rva) -} - /// Every `IMAGE_REL_BASED_DIR64` base relocation: (file offset of its entry, /// RVA it patches). Empty if the image has no readable relocation table. fn dir64_relocations(bytes: &[u8], info: &ImageInfo) -> Vec<(usize, u32)> { @@ -197,18 +174,13 @@ mod tests { assert!(patched.is_dll()); assert_eq!(patched.entry_rva, 0); assert!(patched.imports.is_empty()); - let import = layout::data_directory_offset(&original, dir::IMPORT).unwrap(); - let rva = layout::read_u32(&original, import).unwrap(); - let size = layout::read_u32(&original, import + 4).unwrap(); - assert_ne!(rva, 0); - assert_eq!( - layout::read_u32(&prepared.bytes, import), - Some(rva + size - IMPORT_DESCRIPTOR_SIZE) - ); - assert_eq!( - layout::read_u32(&prepared.bytes, import + 4), - Some(IMPORT_DESCRIPTOR_SIZE) - ); + for index in [dir::IMPORT, dir::BOUND_IMPORT, dir::IAT] { + let at = layout::data_directory_offset(&original, index).unwrap(); + assert_eq!( + &prepared.bytes[at..at + layout::DATA_DIRECTORY_SIZE], + &[0; 8] + ); + } assert_eq!(patched.dll_characteristics & dll_flags::FORCE_INTEGRITY, 0); if let Some(tls) = &patched.tls { assert_eq!(tls.callbacks_va, 0); @@ -225,6 +197,30 @@ mod tests { assert_eq!(prepared.bytes.len(), original.len()); } + #[test] + fn keeps_iat_slots_when_import_size_excludes_the_terminator() { + let mut original = std::fs::read(std::env::current_exe().unwrap()).unwrap(); + let info = ImageInfo::parse(&original).unwrap(); + let directory = layout::data_directory_offset(&original, dir::IMPORT).unwrap(); + // This linker layout counts the descriptors for real modules only. + let descriptor_bytes = u32::try_from(info.imports.len() * 20).unwrap(); + write_u32(&mut original, directory + 4, descriptor_bytes).unwrap(); + let prepared = prepare(&original).unwrap(); + let iat = layout::data_directory_offset(&original, dir::IAT).unwrap(); + assert_ne!(layout::read_u32(&original, iat), Some(0)); + assert_eq!( + &prepared.bytes[iat..iat + layout::DATA_DIRECTORY_SIZE], + &[0; 8] + ); + assert_eq!(prepared.info.import_count(), info.import_count()); + for module in &prepared.info.imports { + for entry in &module.entries { + let at = info.rva_to_file_offset(entry.iat_slot_rva).unwrap() as usize; + assert_eq!(&prepared.bytes[at..at + 8], &original[at..at + 8]); + } + } + } + #[test] fn file_name_is_stable_per_build() { let original = std::fs::read(std::env::current_exe().unwrap()).unwrap(); diff --git a/crates/victory-pe/src/layout.rs b/crates/victory-pe/src/layout.rs index fd99b7f..3f8b0ce 100644 --- a/crates/victory-pe/src/layout.rs +++ b/crates/victory-pe/src/layout.rs @@ -37,6 +37,7 @@ pub mod dir { pub const BASERELOC: usize = 5; pub const LOAD_CONFIG: usize = 10; pub const BOUND_IMPORT: usize = 11; + pub const IAT: usize = 12; } /// `IMAGE_FILE_HEADER::Characteristics` flags. From 25a3e9596f7403ffea784e20b08696d937a71f2a Mon Sep 17 00:00:00 2001 From: ifBars Date: Mon, 23 Nov 2026 16:07:09 -0800 Subject: [PATCH 3/3] Keep an empty import descriptor so Windows assigns static TLS Use the descriptor count already validated by the PE parser, so the directory size need not include the null terminator. Check the OS-assigned static TLS index on a mapped test DLL. --- crates/victory-loader/src/prepare.rs | 86 ++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 17 deletions(-) diff --git a/crates/victory-loader/src/prepare.rs b/crates/victory-loader/src/prepare.rs index 336f278..c893508 100644 --- a/crates/victory-loader/src/prepare.rs +++ b/crates/victory-loader/src/prepare.rs @@ -35,8 +35,8 @@ impl PreparedImage { /// /// * `IMAGE_FILE_DLL` set, entry point cleared: the OS maps it like a DLL /// and calls nothing; -/// * import, bound-import and IAT directories cleared: we bind the original -/// IAT slots ourselves, using `info.imports`; +/// * import directory points to its empty terminator and the bound-import +/// directory is cleared: we bind the original slots using `info.imports`; /// * TLS callback pointer cleared: the OS still allocates static TLS for /// the image, but callbacks only run when we call them (after binding); /// its base relocation is made a no-op so the 0 survives a rebase; @@ -64,13 +64,24 @@ pub fn prepare(original: &[u8]) -> Result { dll_characteristics, )?; - // Clear the IAT directory along with the import directory for Windows 10. - // The slots themselves stay in the image for our binder. - for index in [dir::IMPORT, dir::BOUND_IMPORT, dir::IAT] { - if let Some(at) = layout::data_directory_offset(&bytes, index) { - write(&mut bytes, at, &[0; layout::DATA_DIRECTORY_SIZE])?; + // Windows 10 assigns static TLS to the prepared DLL when its empty + // import table is present. The parser has already walked one descriptor + // per module up to the null terminator, regardless of the recorded Size. + if let Some(at) = layout::data_directory_offset(&bytes, dir::IMPORT) { + let rva = layout::read_u32(&bytes, at).ok_or(PrepareError::Patch("no import RVA"))?; + if rva != 0 { + let terminator = u32::try_from(info.imports.len()) + .ok() + .and_then(|count| count.checked_mul(IMPORT_DESCRIPTOR_SIZE)) + .and_then(|offset| rva.checked_add(offset)) + .ok_or(PrepareError::Patch("import terminator RVA overflow"))?; + write_u32(&mut bytes, at, terminator)?; + write_u32(&mut bytes, at + 4, IMPORT_DESCRIPTOR_SIZE)?; } } + if let Some(at) = layout::data_directory_offset(&bytes, dir::BOUND_IMPORT) { + write(&mut bytes, at, &[0; layout::DATA_DIRECTORY_SIZE])?; + } if let Some(tls) = &info.tls && tls.callbacks_va != 0 @@ -102,6 +113,9 @@ pub fn prepare(original: &[u8]) -> Result { }) } +/// Size of an IMAGE_IMPORT_DESCRIPTOR in the PE file. +const IMPORT_DESCRIPTOR_SIZE: u32 = 20; + /// Every `IMAGE_REL_BASED_DIR64` base relocation: (file offset of its entry, /// RVA it patches). Empty if the image has no readable relocation table. fn dir64_relocations(bytes: &[u8], info: &ImageInfo) -> Vec<(usize, u32)> { @@ -174,13 +188,6 @@ mod tests { assert!(patched.is_dll()); assert_eq!(patched.entry_rva, 0); assert!(patched.imports.is_empty()); - for index in [dir::IMPORT, dir::BOUND_IMPORT, dir::IAT] { - let at = layout::data_directory_offset(&original, index).unwrap(); - assert_eq!( - &prepared.bytes[at..at + layout::DATA_DIRECTORY_SIZE], - &[0; 8] - ); - } assert_eq!(patched.dll_characteristics & dll_flags::FORCE_INTEGRITY, 0); if let Some(tls) = &patched.tls { assert_eq!(tls.callbacks_va, 0); @@ -203,14 +210,27 @@ mod tests { let info = ImageInfo::parse(&original).unwrap(); let directory = layout::data_directory_offset(&original, dir::IMPORT).unwrap(); // This linker layout counts the descriptors for real modules only. - let descriptor_bytes = u32::try_from(info.imports.len() * 20).unwrap(); + let descriptor_bytes = u32::try_from(info.imports.len()).unwrap() * IMPORT_DESCRIPTOR_SIZE; write_u32(&mut original, directory + 4, descriptor_bytes).unwrap(); let prepared = prepare(&original).unwrap(); + let empty = layout::read_u32(&prepared.bytes, directory).unwrap(); + assert_eq!( + empty, + layout::read_u32(&original, directory).unwrap() + descriptor_bytes + ); + assert_eq!( + layout::read_u32(&prepared.bytes, directory + 4), + Some(IMPORT_DESCRIPTOR_SIZE) + ); + let at = info.rva_to_file_offset(empty).unwrap() as usize; + assert_eq!( + &prepared.bytes[at..at + IMPORT_DESCRIPTOR_SIZE as usize], + &[0; 20] + ); let iat = layout::data_directory_offset(&original, dir::IAT).unwrap(); - assert_ne!(layout::read_u32(&original, iat), Some(0)); assert_eq!( &prepared.bytes[iat..iat + layout::DATA_DIRECTORY_SIZE], - &[0; 8] + &original[iat..iat + layout::DATA_DIRECTORY_SIZE] ); assert_eq!(prepared.info.import_count(), info.import_count()); for module in &prepared.info.imports { @@ -221,6 +241,38 @@ mod tests { } } + #[cfg(windows)] + #[test] + fn windows_assigns_static_tls_to_a_prepared_dll() { + // Model a title DLL using our own linked image. Loading a DLL leaves + // its assigned index intact; the executable-specific redirect resets it. + let mut original = std::fs::read(std::env::current_exe().unwrap()).unwrap(); + let flags = + layout::file_header_offset(&original).unwrap() + layout::FILE_HEADER_CHARACTERISTICS; + let characteristics = + ImageInfo::parse(&original).unwrap().characteristics | file_flags::DLL; + write_u16(&mut original, flags, characteristics).unwrap(); + let prepared = prepare(&original).unwrap(); + let info = &prepared.info; + let tls = info.tls.as_ref().expect("the test binary uses static TLS"); + let directory = info.rva_to_file_offset(tls.directory_rva).unwrap() as usize; + let index_va = layout::read_u64(&original, directory + 16).unwrap(); + let index_rva = index_va.checked_sub(info.image_base).unwrap(); + assert!(index_rva + 4 <= u64::from(info.size_of_image)); + + let folder = tempfile::tempdir().unwrap(); + let image = crate::load(&prepared, folder.path()).unwrap(); + // SAFETY: the mapped test image contains the checked DWORD above. + let assigned = + unsafe { ((image.base() + index_rva as usize) as *const u32).read_unaligned() }; + // SAFETY: the prepared DLL has never been attached or given to a caller. + unsafe { image.unload() }; + assert_ne!( + assigned, 0, + "the process executable already owns TLS slot zero" + ); + } + #[test] fn file_name_is_stable_per_build() { let original = std::fs::read(std::env::current_exe().unwrap()).unwrap();