diff --git a/CHANGELOG.md b/CHANGELOG.md
index e686c9c4..81eedc6b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -188,6 +188,7 @@ remains unsupported.
### Fixed
- Made the static probe and the binder share one bounded import parser, after the truncated-name fix for #270. Truncated, unterminated, empty or non-ASCII names, reserved bits and endless tables are reported by the probe and refused by the binder.
+- Fixed TLS callback metadata that was accepted or misread without an error (#78). A non-zero `AddressOfCallbacks` outside the image's sections, or 4 GiB or more above the image base, is now refused; it used to mean "no callbacks" or read the list from an unrelated place. The callback list must reach its null entry inside its section, where the section's zero-filled tail counts, and a list of more than 64 callbacks is refused where it used to be cut short. `execute_tls_callbacks` refuses a stack top that is not 16-byte aligned or cannot hold the entry frame, which used to overflow a subtraction, and a file cut inside the TLS data-directory entry no longer panics the parser. `TlsError` gains four variants for these cases. Twelve unit tests in `crates/loader/src/tls.rs` use original headers and code, and also check the argument registers, the stack pointer at entry and after return, callback order and the instruction budget. Expectations come from Microsoft's public PE Format and x64 calling convention pages; nothing ran on Windows, and independent review is pending.
- Fixed import binding writing some slots before finding an unresolved import, and service calls taking effect before an unreadable return address was noticed. A service that changes a nonvolatile register, including XMM6 to XMM15, now stops the guest (part of #201).
- Fixed older import-generator templates that reported success for unimplemented wrappers, used a nonexistent exit reason, and read floating-point arguments from integer registers.
- Fixed guest flags in the native DBT (#64, #70). They lived only in host NZCV, so they were lost across helper calls and block boundaries and register ADD, SUB and XOR set none. JB, JAE, JBE and JA used ARM carry polarity, which is inverted for subtraction. Flags are now stored in the guest state.
diff --git a/crates/loader/src/tls.rs b/crates/loader/src/tls.rs
index 8f0da2bb..6e5b073e 100644
--- a/crates/loader/src/tls.rs
+++ b/crates/loader/src/tls.rs
@@ -11,11 +11,39 @@ use crate::memory::{GuestMemory, MemoryError, MemoryPermissions};
use crate::pe::ParsedPe;
use std::fmt;
+/// Most callbacks one TLS directory may list.
+pub const MAX_TLS_CALLBACKS: usize = 64;
+
+/// Bytes below the stack top that a callback is entered with: the return
+/// address and the 32-byte home space the Win64 convention gives every callee.
+const CALLBACK_FRAME: u64 = 40;
+
/// Errors arising during TLS setup or callback invocation.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TlsError {
- DirectoryOutOfBounds { rva: u32 },
- InvalidTemplateRange { start: u64, end: u64 },
+ DirectoryOutOfBounds {
+ rva: u32,
+ },
+ InvalidTemplateRange {
+ start: u64,
+ end: u64,
+ },
+ /// `AddressOfCallbacks` is not zero and not inside a section of the image.
+ CallbackArrayOutOfBounds {
+ address: u64,
+ },
+ /// The callback array leaves its section before a null entry.
+ CallbackArrayUnterminated {
+ address: u64,
+ },
+ /// The callback array lists more than [`MAX_TLS_CALLBACKS`] entries.
+ TooManyCallbacks {
+ limit: usize,
+ },
+ /// The stack cannot hold a callback's entry frame or is not 16-byte aligned.
+ InvalidCallbackStack {
+ stack_top: u64,
+ },
CallbackExecutionFault(ExitReason),
Memory(MemoryError),
}
@@ -32,6 +60,27 @@ impl fmt::Display for TlsError {
"invalid TLS template range: 0x{start:016x}..0x{end:016x}"
)
}
+ Self::CallbackArrayOutOfBounds { address } => {
+ write!(
+ f,
+ "TLS callback array at 0x{address:016x} is outside the image"
+ )
+ }
+ Self::CallbackArrayUnterminated { address } => {
+ write!(
+ f,
+ "TLS callback array at 0x{address:016x} has no null entry in its section"
+ )
+ }
+ Self::TooManyCallbacks { limit } => {
+ write!(f, "TLS callback array lists more than {limit} callbacks")
+ }
+ Self::InvalidCallbackStack { stack_top } => {
+ write!(
+ f,
+ "stack top 0x{stack_top:016x} cannot hold an aligned TLS callback frame"
+ )
+ }
Self::CallbackExecutionFault(e) => {
write!(f, "fault during TLS callback execution: {e}")
}
@@ -78,7 +127,7 @@ pub fn parse_tls_directory(bytes: &[u8], pe: &ParsedPe) -> Result