relinker: WindowsTlsBuilder reads one byte past the end of the buffer for a segment-prefixed instruction that ends the file #6391

Closed
opened 2026-10-15 13:56:04 +00:00 by midasmatterhorn · 1 comment
midasmatterhorn commented 2026-10-15 13:56:04 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by midasmatterhorn on 2026-10-15T13:56:04Z.


WindowsTlsBuilder reads one byte past the end of the ELF buffer for a segment-prefixed instruction that ends the file. The value is always discarded, so conversions are not affected; it is a memory-safety defect (heap-buffer-overflow read of size 1 under ASan).

At core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp:146, addressRegister is computed unconditionally:

const auto addressRegister = static_cast<std::uint8_t>(((bytes[position + 1] >> 3) & 7) | ((info.RexPrefix & 4) << 1));
if (address && addressRegister != 4) {

Every sibling expression in the block guards the byte reads with the decoded length (info.Length - position == 7, == 11, and registerAddress()'s length - position < 2 check), but bytes[position + 1] here is evaluated even when the opcode is the last byte of the instruction (position + 1 == info.Length). When that instruction also ends the executable PT_LOAD file range and the range ends at EOF — the normal shape of a stripped eboot — the read is one byte past the end of the file buffer. The result is only used when address is set, and address is nullopt whenever Length - position == 1, so the value is always discarded on the path that reads out of bounds.

Reproduced with a relinker-only build of main 92cdc7b62 (Apple clang 21.0.0, -fsanitize=address), macOS 27.0.1 arm64. The input is a minimal PS5-style ELF whose entry-reachable code runs to the end of the last executable PT_LOAD, which ends at EOF with the bytes 64 90 (fs prefix + nop, one instruction, 2 bytes):

python3 make_min.py min_6490.elf "64 90"      # fixture generator below
./relinker --skip-sce-module --windows min_6490.elf out.exe
#   FAIL: Unsupported Windows guest TLS instruction (bytes: 64 90) (offset 0x1ffe)   <- correct rejection
ASAN_OPTIONS=detect_leaks=0 ./probe min_6490.elf
#   ==...==ERROR: AddressSanitizer: heap-buffer-overflow ... READ of size 1 ...
#       #0 ...WindowsTlsBuilder::Build(...) WindowsTlsBuilder.cpp:146
#   0x... is located 0 bytes after 8192-byte region

64 c3, 65 90 and 66 64 90 also trigger; a 3-byte 64 0f 0b does not.

Fixture generator (writes a minimal ELF whose last executable bytes are the given tail):

import struct, sys

image = bytearray(0x1000)
image[:16] = b"\x7fELF\x02\x01\x01" + bytes(9)
struct.pack_into("<HHIQQQIHHHHHH", image, 16, 3, 62, 1, 0x1200, 64, 0, 0, 64, 56, 4, 64, 0, 0)
image[0x200:0x206] = b"\xff\x25\xfa\x00\x00\x00"
image[0x210:0x216] = b"\xb8\x2a\x00\x00\x00\xc3"
struct.pack_into("<QQq", image, 0x700, 0x300, 8, 0x210)
tags = [(5, 0x600), (10, 1), (6, 0x620), (11, 24), (7, 0x700), (8, 24), (9, 24), (0, 0)]
struct.pack_into("<IIQQQQQQ", image, 64, 1, 7, 0, 0, 0, len(image), len(image), 0x1000)
struct.pack_into("<IIQQQQQQ", image, 120, 2, 6, 0x400, 0x400, 0x400, len(tags) * 16, len(tags) * 16, 8)
image.extend(b"\x90" * 0x1000)
struct.pack_into("<IIQQQQQQ", image, 64 + 2 * 56, 7, 4, 0x800, 0x800, 0x800, 8, 16, 16)
struct.pack_into("<IIQQQQQQ", image, 64 + 3 * 56, 1, 5, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000)
for index, tag in enumerate(tags):
    struct.pack_into("<qQ", image, 0x400 + index * 16, *tag)
tail = bytes.fromhex(sys.argv[2])
image[0x2000 - len(tail):0x2000] = tail
open(sys.argv[1], "wb").write(bytes(image))

Suggested fix: compute addressRegister only when it is used — move it inside if (address) { ... } (where registerAddress has already proved Length - position >= 2) — or guard it the way loadRegister is guarded.

Note on overlap: open pull requests #5890 and #6102 both change this file; whichever lands first, this fix can rebase on it.

Found by an AI-assisted audit (DeepSeek V4.1 Flash via OpenCode); the ASan report above was executed. I can send the one-line hoist with a regression test.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5867513191 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6391). Originally posted by [midasmatterhorn](https://github.com/midasmatterhorn) on 2026-10-15T13:56:04Z. --- `WindowsTlsBuilder` reads one byte past the end of the ELF buffer for a segment-prefixed instruction that ends the file. The value is always discarded, so conversions are not affected; it is a memory-safety defect (heap-buffer-overflow read of size 1 under ASan). At core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp:146, `addressRegister` is computed unconditionally: ```cpp const auto addressRegister = static_cast<std::uint8_t>(((bytes[position + 1] >> 3) & 7) | ((info.RexPrefix & 4) << 1)); if (address && addressRegister != 4) { ``` Every sibling expression in the block guards the byte reads with the decoded length (`info.Length - position == 7`, `== 11`, and `registerAddress()`'s `length - position < 2` check), but `bytes[position + 1]` here is evaluated even when the opcode is the last byte of the instruction (`position + 1 == info.Length`). When that instruction also ends the executable `PT_LOAD` file range and the range ends at EOF — the normal shape of a stripped eboot — the read is one byte past the end of the file buffer. The result is only used when `address` is set, and `address` is `nullopt` whenever `Length - position == 1`, so the value is always discarded on the path that reads out of bounds. Reproduced with a relinker-only build of `main` `92cdc7b62` (Apple clang 21.0.0, `-fsanitize=address`), macOS 27.0.1 arm64. The input is a minimal PS5-style ELF whose entry-reachable code runs to the end of the last executable `PT_LOAD`, which ends at EOF with the bytes `64 90` (`fs` prefix + `nop`, one instruction, 2 bytes): ``` python3 make_min.py min_6490.elf "64 90" # fixture generator below ./relinker --skip-sce-module --windows min_6490.elf out.exe # FAIL: Unsupported Windows guest TLS instruction (bytes: 64 90) (offset 0x1ffe) <- correct rejection ASAN_OPTIONS=detect_leaks=0 ./probe min_6490.elf # ==...==ERROR: AddressSanitizer: heap-buffer-overflow ... READ of size 1 ... # #0 ...WindowsTlsBuilder::Build(...) WindowsTlsBuilder.cpp:146 # 0x... is located 0 bytes after 8192-byte region ``` `64 c3`, `65 90` and `66 64 90` also trigger; a 3-byte `64 0f 0b` does not. Fixture generator (writes a minimal ELF whose last executable bytes are the given tail): ```python import struct, sys image = bytearray(0x1000) image[:16] = b"\x7fELF\x02\x01\x01" + bytes(9) struct.pack_into("<HHIQQQIHHHHHH", image, 16, 3, 62, 1, 0x1200, 64, 0, 0, 64, 56, 4, 64, 0, 0) image[0x200:0x206] = b"\xff\x25\xfa\x00\x00\x00" image[0x210:0x216] = b"\xb8\x2a\x00\x00\x00\xc3" struct.pack_into("<QQq", image, 0x700, 0x300, 8, 0x210) tags = [(5, 0x600), (10, 1), (6, 0x620), (11, 24), (7, 0x700), (8, 24), (9, 24), (0, 0)] struct.pack_into("<IIQQQQQQ", image, 64, 1, 7, 0, 0, 0, len(image), len(image), 0x1000) struct.pack_into("<IIQQQQQQ", image, 120, 2, 6, 0x400, 0x400, 0x400, len(tags) * 16, len(tags) * 16, 8) image.extend(b"\x90" * 0x1000) struct.pack_into("<IIQQQQQQ", image, 64 + 2 * 56, 7, 4, 0x800, 0x800, 0x800, 8, 16, 16) struct.pack_into("<IIQQQQQQ", image, 64 + 3 * 56, 1, 5, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000) for index, tag in enumerate(tags): struct.pack_into("<qQ", image, 0x400 + index * 16, *tag) tail = bytes.fromhex(sys.argv[2]) image[0x2000 - len(tail):0x2000] = tail open(sys.argv[1], "wb").write(bytes(image)) ``` Suggested fix: compute `addressRegister` only when it is used — move it inside `if (address) { ... }` (where `registerAddress` has already proved `Length - position >= 2`) — or guard it the way `loadRegister` is guarded. Note on overlap: open pull requests #5890 and #6102 both change this file; whichever lands first, this fix can rebase on it. Found by an AI-assisted audit (DeepSeek V4.1 Flash via OpenCode); the ASan report above was executed. I can send the one-line hoist with a regression test.
MarcZX10 commented 2026-10-15 14:05:20 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-15T14:05:20Z.


I confirmed the out-of-bounds read with the reported EOF-shaped executable segment. PR #6401 computes the register byte only when the decoded address form has already established that the byte exists, and adds the EOF regression fixture. The relinker-only AddressSanitizer build and full TLS function coverage suite pass.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6194000692 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6391#issuecomment-6194000692). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-15T14:05:20Z. --- I confirmed the out-of-bounds read with the reported EOF-shaped executable segment. [PR #6401](https://github.com/boykopovar/AnyPS5/pull/6401) computes the register byte only when the decoded address form has already established that the byte exists, and adds the EOF regression fixture. The relinker-only AddressSanitizer build and full TLS function coverage suite pass.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#6391
No description provided.