From 0692517f13b09fd14512ec160436c5ce558071f5 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Fri, 22 Apr 2022 21:32:33 +1000 Subject: [PATCH] ElfObject: Ignore section headers when offset is invalid This happens in V-Rally 3: (ELF) Section header offset 14853196 is larger than file size 5898828 --- pcsx2/Elfheader.cpp | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/pcsx2/Elfheader.cpp b/pcsx2/Elfheader.cpp index 47a2bb514d..864527453c 100644 --- a/pcsx2/Elfheader.cpp +++ b/pcsx2/Elfheader.cpp @@ -62,17 +62,27 @@ void ElfObject::initElfHeaders(bool isPSXElf) DevCon.WriteLn( L"Initializing Elf: %d bytes", data.GetSizeInBytes()); - if ( header.e_phnum > 0 ) - proghead = (ELF_PHR*)&data[header.e_phoff]; + if (header.e_phnum > 0) + { + if ((header.e_phoff + sizeof(ELF_PHR)) <= data.GetSizeInBytes()) + proghead = reinterpret_cast(&data[header.e_phoff]); + else + Console.Error("(ELF) Program header offset %u is larger than file size %u", header.e_phoff, data.GetSizeInBytes()); + } - if ( header.e_shnum > 0 ) - secthead = (ELF_SHR*)&data[header.e_shoff]; + if (header.e_shnum > 0) + { + if ((header.e_shoff + sizeof(ELF_SHR)) <= data.GetSizeInBytes()) + secthead = reinterpret_cast(&data[header.e_shoff]); + else + Console.Error("(ELF) Section header offset %u is larger than file size %u", header.e_shoff, data.GetSizeInBytes()); + } - if ( ( header.e_shnum > 0 ) && ( header.e_shentsize != sizeof(ELF_SHR) ) ) - Console.Error( "(ELF) Size of section headers is not standard" ); + if ((header.e_shnum > 0) && (header.e_shentsize != sizeof(ELF_SHR))) + Console.Error("(ELF) Size of section headers is not standard"); - if ( ( header.e_phnum > 0 ) && ( header.e_phentsize != sizeof(ELF_PHR) ) ) - Console.Error( "(ELF) Size of program headers is not standard" ); + if ((header.e_phnum > 0) && (header.e_phentsize != sizeof(ELF_PHR))) + Console.Error("(ELF) Size of program headers is not standard"); //getCRC();