Buffer Overflow in LoadFileImpl
The function LoadFileImpl in FEXCore/Source/Utils/FileLoading.cpp line 42 always calls pread with an offset of 0 instead of using CurrentOffset:
while (CurrentOffset != FileSize && (Read = pread(FD, &Data.at(CurrentOffset), FileSize, CurrentOffset)) > 0) {
CurrentOffset += Read;
}
If the pread function returns fewer bytes than requested (see read(2)) the while loop will get executed again. Since the offset is always 0 data is again read from the beginning of the file.
Furthermore, since the destination buffer is indexed via CurrentOffset a (heap) buffer overflow occurs on subsequent pread calls.
Steps to reproduce:
On Linux v6.7.0 calling LoadFileImpl on a large file (>= 2.1G) triggers the buffer overflow.
Fix:
Replace the offset 0 with CurrentOffset in LoadFileImpl.
Also update the FileSize argument to be the number of remaining bytes which still need to be read.