TeamFAPS / PSVita-RE-tools

A bundle of RE tools for analyzing and modding PSVita OS
GNU General Public License v3.0
322 stars 35 forks source link

Fix ELF inject when sections offsets are not in ascending order #34

Open Nik-RE-dev opened 1 year ago

Nik-RE-dev commented 1 year ago

Here's structure of ELF segments in the file I was working with:

Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align LOAD 0x0000e0 0x81000000 0x00000000 0x234c04 0x234c04 R E 0x10 LOAD 0x234cf0 0x81235000 0x00000000 0x0be60 0x3c918 RW 0x10 LOOS+0 0x242ce0 0x00000000 0x00000000 0x59040 0x00000 0x10 LOOS+0 0x29bd20 0x00000000 0x00000000 0x02d3c 0x00000 0x10 LOOS+0xfffff01 0x240b50 0x00000000 0x00000000 0x02186 0x00000 0x10

LOOS+0 is PT_SCE_RELA LOOS+0xfffff01 is PT_SCE_LIBVERSION

Offsets of segments here is not in ascending order and the same mapping used in SELF container. But current implementation of elf_injector tool implicitly relies on ascending order of offsets when applying offset correction. In my case when offsets of 2-nd PT_LOAD and 1-st PT_SCE_RELA were corrected, writing compressed PT_SCE_LIBVERSION segment could overwrite already written PT_LOAD/PT_SCE_RELA segments.

I implemented sorting of compressed segments by offsets before computing offset correction.

CelesteBlue-dev commented 1 year ago

Hello. Thank you for fixing that bug. What is the TITLEID of that testcase ELF so that I can reproduce the bug and test the fix?

Nik-RE-dev commented 1 year ago

It's PCSG00246. Bug happened for me then I made enough changes in 1-st PT_SCE_RELA.