fix(relinker): reject guest TLS loads from unmodelled control block fields

Loads at fs:1 through fs:0x27 read tcb_dtv and tcb_thread on the FreeBSD layout, which the Windows thread control block does not model, so they returned zero. Accept only the TLS block, fs:0 and the stack guard at fs:0x28, and fail the relink for the rest.
This commit is contained in:
QDarkRex
2026-10-04 14:09:19 +07:00
parent 508a9710ae
commit ab5c339039
2 changed files with 9 additions and 8 deletions
@@ -109,11 +109,12 @@ PeDirectory WindowsTlsBuilder::Build(const std::vector<std::uint8_t>& source, co
const auto blockSize = CheckedRva((tls->MemorySize + alignment - 1) & ~(alignment - 1));
const auto templateOffset = CheckedRva((64 + alignment - 1) & ~(alignment - 1));
constexpr std::uint32_t threadControlBlockSize = 0x30;
constexpr std::int64_t stackGuardDisplacement = 0x28;
for (const auto& access : accesses) {
if (access.StoreImmediate || access.Displacement == 0) continue;
const auto displacement = static_cast<std::int64_t>(std::bit_cast<std::int32_t>(access.Displacement));
if (displacement < -static_cast<std::int64_t>(blockSize) || displacement + 8 > threadControlBlockSize)
throw Domain::RelinkerException("Windows guest TLS load displacement " + std::to_string(displacement) + " is outside the thread TLS block", access.FileOffset);
if (displacement != stackGuardDisplacement && (displacement > 0 || displacement < -static_cast<std::int64_t>(blockSize)))
throw Domain::RelinkerException("Windows guest TLS load displacement " + std::to_string(displacement) + " is not in the thread TLS block, 0 or the stack guard at 0x28", access.FileOffset);
}
PeSection data{".gtls", nextRva, SectionRead | SectionWrite | 0x40u, std::vector<std::uint8_t>(templateOffset + blockSize + threadControlBlockSize)};
@@ -60,8 +60,8 @@ def displacement_load(register, displacement, flags, round_trip=False):
immediate(1, 0x2468ace013579bdf)
emit(bytes.fromhex("48 89 08"))
if displacement == 0:
emit(fs_load(2, 8))
immediate(1, positive[8])
emit(fs_load(2, 40))
immediate(1, positive[40])
emit(bytes.fromhex("48 39 ca"))
check()
else:
@@ -115,8 +115,8 @@ def displacement_cases():
if register == 4:
continue
for flags in (0x202, 0xad7):
for displacement, round_trip in ((0, False), (8, False), (16, False), (40, False),
(-64, False), (-40, False), (-8, False), (40, True)):
for displacement, round_trip in ((0, False), (40, False), (-64, False), (-40, False),
(-8, False), (40, True)):
offset, body = displacement_load(register, displacement, flags, round_trip)
image = make_image("register", "unwind", body=body)
struct.pack_into("<IIQQQQQQ", image, 176, 7, 4, 0x800, 0x800, 0x800, 48, 48, 32)
@@ -129,12 +129,12 @@ def displacement_cases():
def displacement_bounds_cases():
for register in (0, 12):
for displacement in (0x30, 0x2c, -65, -0x80000000, 0x7fffffff):
for displacement in (1, 8, 0x10, 0x20, 0x27, 0x29, 0x2c, 0x30, -65, -0x80000000, 0x7fffffff):
image = make_image("register", "unwind", body=fs_load(register, displacement) + b"\xc3")
struct.pack_into("<IIQQQQQQ", image, 176, 7, 4, 0x800, 0x800, 0x800, 48, 48, 32)
if register == 12:
image[176:232], image[288:344] = image[288:344], image[176:232]
error = f"Windows guest TLS load displacement {displacement} is outside the thread TLS block"
error = f"Windows guest TLS load displacement {displacement} is not in the thread TLS block, 0 or the stack guard at 0x28"
yield f"displacement-bounds-{register}-{displacement}", image, error