fix(relinker): keep ELF bounds checks from overflowing

ElfReader checked ranges as offset + size > buffer size. An offset near
UINT64_MAX wrapped the sum, passed the check and read outside the buffer,
which AddressSanitizer reports as a heap-buffer-overflow. The relinker
then failed later with unrelated messages built from the stray bytes.
The checks now compare without adding to the offset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01R5WJS4ZdQynAJesCZKH5hb
This commit is contained in:
Alberto Ruiz
2026-09-30 16:38:27 +02:00
parent c71be475fc
commit 46e63f2ec1
3 changed files with 47 additions and 5 deletions
+2
View File
@@ -155,6 +155,8 @@ if(BUILD_TESTING)
find_package(Python3 QUIET COMPONENTS Interpreter)
if(Python3_Interpreter_FOUND)
add_test(NAME elf_offsets COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_elf_offsets.py $<TARGET_FILE:relinker>)
add_test(NAME optional_plt COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_optional_plt.py $<TARGET_FILE:relinker>)
add_test(NAME empty_tls COMMAND ${Python3_EXECUTABLE}
@@ -4,6 +4,14 @@
namespace Relinker {
namespace {
bool _rangeFits(const std::uint64_t offset, const std::uint64_t length, const std::size_t size) {
return offset <= size && length <= size - offset;
}
}
ElfReader::ElfReader(std::vector<std::uint8_t> fileBuffer)
: _fileBuffer(std::move(fileBuffer))
{
@@ -21,7 +29,7 @@ std::uint8_t ElfReader::_readU8At(FileByteOffset fileByteOffset) const {
}
std::uint16_t ElfReader::_readU16At(FileByteOffset fileByteOffset) const {
if (fileByteOffset + 2 > _fileBuffer.size()) {
if (!_rangeFits(fileByteOffset, 2, _fileBuffer.size())) {
throw RelinkerException("FileByteOffset out of bounds", fileByteOffset);
}
std::uint16_t value;
@@ -30,7 +38,7 @@ std::uint16_t ElfReader::_readU16At(FileByteOffset fileByteOffset) const {
}
std::uint32_t ElfReader::_readU32At(FileByteOffset fileByteOffset) const {
if (fileByteOffset + 4 > _fileBuffer.size()) {
if (!_rangeFits(fileByteOffset, 4, _fileBuffer.size())) {
throw RelinkerException("FileByteOffset out of bounds", fileByteOffset);
}
std::uint32_t value;
@@ -39,7 +47,7 @@ std::uint32_t ElfReader::_readU32At(FileByteOffset fileByteOffset) const {
}
std::uint64_t ElfReader::_readU64At(FileByteOffset fileByteOffset) const {
if (fileByteOffset + 8 > _fileBuffer.size()) {
if (!_rangeFits(fileByteOffset, 8, _fileBuffer.size())) {
throw RelinkerException("FileByteOffset out of bounds", fileByteOffset);
}
std::uint64_t value;
@@ -191,7 +199,7 @@ FileByteOffset ElfReader::TranslateVirtualAddress(VirtualAddress address) const
}
std::vector<std::uint8_t> ElfReader::ReadSection(const SectionHeader& header) const {
if (header.Offset + header.SectionSize > _fileBuffer.size()) {
if (!_rangeFits(header.Offset, header.SectionSize, _fileBuffer.size())) {
throw RelinkerException("Section offset out of bounds", header.Offset);
}
@@ -201,7 +209,7 @@ std::vector<std::uint8_t> ElfReader::ReadSection(const SectionHeader& header) co
}
std::vector<std::uint8_t> ElfReader::ReadSegment(const ProgramHeader& header) const {
if (header.Offset + header.FileSize > _fileBuffer.size()) {
if (!_rangeFits(header.Offset, header.FileSize, _fileBuffer.size())) {
throw RelinkerException("Segment offset out of bounds", header.Offset);
}
@@ -0,0 +1,32 @@
from pathlib import Path
import struct
import subprocess
import sys
import tempfile
from test_optional_plt import fixture
def main():
relinker = Path(sys.argv[1]).resolve()
cases = [
("program-header-offset", 0x20, "FileByteOffset out of bounds (offset 0xffffffffffffffff)"),
("load-offset", 64 + 8, "Segment offset out of bounds (offset 0xffffffffffffffff)"),
("dynamic-offset", 120 + 8, "FileByteOffset out of bounds (offset 0xffffffffffffffff)"),
]
with tempfile.TemporaryDirectory(prefix="anyps5-elf-offsets-") as directory:
for name, field, error in cases:
source = Path(directory) / (name + ".elf")
data = fixture()
struct.pack_into("<Q", data, field, 0xffffffffffffffff)
source.write_bytes(data)
for mode in ([], ["--windows"]):
output = source.with_suffix(".out")
result = subprocess.run([str(relinker), "--skip-sce-module", *mode, str(source), str(output)],
capture_output=True, text=True, timeout=20)
assert result.returncode == 2 and error in result.stderr and not output.exists(), (name, mode, result)
print("ELF offset tests passed")
if __name__ == "__main__":
main()