Selaa lähdekoodia

Merge pull request #4 from Exorion1er/fix/tls-callbacks-reloc

Loader: keep the cleared TLS callback pointer out of base relocations
Marvis 3 päivää sitten
vanhempi
sitoutus
e8f3b1c0f2
1 muutettua tiedostoa jossa 58 lisäystä ja 0 poistoa
  1. 58 0
      crates/victory-loader/src/prepare.rs

+ 58 - 0
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<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());