relinker: WindowsTlsBuilder reads one byte past the end of the buffer for a segment-prefixed instruction that ends the file #6391
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by midasmatterhorn on 2026-10-15T13:56:04Z.
WindowsTlsBuilderreads 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,
addressRegisteris computed unconditionally:Every sibling expression in the block guards the byte reads with the decoded length (
info.Length - position == 7,== 11, andregisterAddress()'slength - position < 2check), butbytes[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 executablePT_LOADfile 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 whenaddressis set, andaddressisnulloptwheneverLength - position == 1, so the value is always discarded on the path that reads out of bounds.Reproduced with a relinker-only build of
main92cdc7b62(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 executablePT_LOAD, which ends at EOF with the bytes64 90(fsprefix +nop, one instruction, 2 bytes):64 c3,65 90and66 64 90also trigger; a 3-byte64 0f 0bdoes not.Fixture generator (writes a minimal ELF whose last executable bytes are the given tail):
Suggested fix: compute
addressRegisteronly when it is used — move it insideif (address) { ... }(whereregisterAddresshas already provedLength - position >= 2) — or guard it the wayloadRegisteris 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.
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.