From c21208c18a4e43e1abe961d7efa787ac3e31133b Mon Sep 17 00:00:00 2001 From: Exorion <8749995+Exorion1er@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:37:05 +0100 Subject: [PATCH] Loader: keep the cleared TLS callback pointer out of base relocations prepare() zeroes the title's TLS AddressOfCallBacks but left the IMAGE_REL_BASED_DIR64 entry that patches it. When the loader maps the prepared copy away from its preferred base, that entry turns the 0 into the rebase delta and the loader reads TLS callbacks from there. On Windows the copy lands at 0x140000000 so the delta is 0; under Wine, which maps DYNAMIC_BASE images high, LoadLibraryExW faults in ntdll before any title code runs. The entry is now rewritten as IMAGE_REL_BASED_ABSOLUTE (a no-op), and the prepare test checks that no relocation targets the cleared field. Co-Authored-By: Claude Opus 5.5 --- crates/victory-loader/src/prepare.rs | 58 ++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/crates/victory-loader/src/prepare.rs b/crates/victory-loader/src/prepare.rs index 1cbd193..9895db1 100644 --- a/crates/victory-loader/src/prepare.rs +++ b/crates/victory-loader/src/prepare.rs @@ -39,6 +39,7 @@ impl PreparedImage { /// 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); +/// its base relocation is made a no-op so the 0 survives a rebase; /// * `FORCE_INTEGRITY` cleared, checksum zeroed: the copy is unsigned. pub fn prepare(original: &[u8]) -> Result { let info = ImageInfo::parse(original)?; @@ -81,6 +82,16 @@ pub fn prepare(original: &[u8]) -> Result { at as usize + layout::TLS64_ADDRESS_OF_CALLBACKS, 0, )?; + // The field's base relocation would turn the 0 into `delta` whenever + // the loader maps the copy away from its preferred base (Wine always + // does for DYNAMIC_BASE images), and the loader then reads callbacks + // from there. Make that entry a no-op. + let field_rva = tls.directory_rva + layout::TLS64_ADDRESS_OF_CALLBACKS as u32; + for (entry, target) in dir64_relocations(&bytes, &info) { + if target == field_rva { + write_u16(&mut bytes, entry, 0)?; // IMAGE_REL_BASED_ABSOLUTE + } + } } Ok(PreparedImage { @@ -90,6 +101,46 @@ pub fn prepare(original: &[u8]) -> Result { }) } +/// 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)> { + const DIR64: u16 = 10; + let mut out = Vec::new(); + let Some(d) = layout::data_directory_offset(bytes, dir::BASERELOC) else { + return out; + }; + let (Some(rva), Some(size)) = (layout::read_u32(bytes, d), layout::read_u32(bytes, d + 4)) + else { + return out; + }; + let Some(start) = info.rva_to_file_offset(rva).filter(|_| size != 0) else { + return out; + }; + let (mut at, end) = (start as usize, start as usize + size as usize); + while at + 8 <= end { + let (Some(page), Some(block)) = + (layout::read_u32(bytes, at), layout::read_u32(bytes, at + 4)) + else { + break; + }; + let block = block as usize; + if block < 8 || at + block > end { + break; + } + for entry in (at + 8..at + block).step_by(2) { + let Some(e) = bytes.get(entry..entry + 2) else { + break; + }; + let e = u16::from_le_bytes([e[0], e[1]]); + if e >> 12 == DIR64 { + out.push((entry, page + u32::from(e & 0xFFF))); + } + } + at += block; + } + out +} + fn write_u16(bytes: &mut [u8], at: usize, v: u16) -> Result<(), PrepareError> { write(bytes, at, &v.to_le_bytes()) } @@ -125,6 +176,13 @@ mod tests { assert_eq!(patched.dll_characteristics & dll_flags::FORCE_INTEGRITY, 0); if let Some(tls) = &patched.tls { assert_eq!(tls.callbacks_va, 0); + let field = tls.directory_rva + layout::TLS64_ADDRESS_OF_CALLBACKS as u32; + assert!( + dir64_relocations(&prepared.bytes, &patched) + .iter() + .all(|&(_, target)| target != field), + "the cleared callback pointer must not be relocated" + ); } // Nothing else moved: same sections, same size. assert_eq!(patched.sections.len(), prepared.info.sections.len());