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 <[email protected]>
This commit is contained in:
ExorionandClaude Opus 5.5 committed 2026-11-23 23:37:05 +01:00
1 parent 015d45bfff
commit c21208c18a
1 file changed
+58
+58
View File
@@ -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<PreparedImage, PrepareError> {
let info = ImageInfo::parse(original)?;
@@ -81,6 +82,16 @@ pub fn prepare(original: &[u8]) -> Result<PreparedImage, PrepareError> {
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<PreparedImage, PrepareError> {
})
}
/// 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());