Solving mprotect() syscall failure

2.3k views Asked by At

I'm writing some ROP exploit code that calls mprotect via a syscall, after invoking int 0x80 eax is set to 0x0 indicating a success. Shifting execution to the target address still results in a SIGSEGV. I would love for someone to show me where I go wrong.

Some details, target address is the .data section, this is where I'll be writing by shellcode to: [20] 0x8146820->0x814c2b8 at 0x000fd820: .data ALLOC LOAD DATA HAS_CONTENTS

I set eax to 125, ebx to the page boundary 0x8146000, ecx to 0x1000 (4096 page size) and edx to 0x7 (RWX).

Just before the syscall the registers look like this:

eax            0x7d 125
ecx            0x1000   4096
edx            0x7  7
ebx            0x8146000    135553024
esp            0xbffff2b0   0xbffff2b0
ebp            0x8d0e0f0    0x8d0e0f0
esi            0x804fb85    134544261
edi            0x43434343   1128481603
eip            0x80c0182    0x80c0182 <mprotect+18>
eflags         0x202    [ IF ]
cs             0x73 115
ss             0x7b 123
ds             0x7b 123
es             0x7b 123
fs             0x0  0
gs             0x33 51
(gdb) disas $eip, $eip+20
Dump of assembler code from 0x80c0182 to 0x80c0196:
=> 0x080c0182 <mprotect+18>:    int    $0x80
   0x080c0184 <mprotect+20>:    pop    %ebx
   0x080c0185 <mprotect+21>:    cmp    $0xfffff001,%eax
   0x080c018a <mprotect+26>:    jae    0x80c7d80 <__syscall_error>
   0x080c0190 <mprotect+32>:    ret    

and after the syscall the registers are:

(gdb) si
0x080c0184 in mprotect ()
(gdb) i r
eax            0x0  0
ecx            0x1000   4096
edx            0x7  7
ebx            0x8146000    135553024
esp            0xbffff2b0   0xbffff2b0
ebp            0x8d0e0f0    0x8d0e0f0
esi            0x804fb85    134544261
edi            0x43434343   1128481603
eip            0x80c0184    0x80c0184 <mprotect+20>
eflags         0x202    [ IF ]
cs             0x73 115
ss             0x7b 123
ds             0x7b 123
es             0x7b 123
fs             0x0  0
gs             0x33 51

However the memory location does not show a change in permissions and attempting to execute instructions there terminates the application:

(gdb) x/4x 0x8146820
0x8146820:      0x00000000      0x00000000      0x08146154      0x0000ea60
(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
0x08146820 in data_start ()

Any suggestions on how/what to debug or what I'm doing wrong are welcome.

Edit I ran it under strace without the debugger attached, seems like the mprotect call is a success, yet execution fails:

stat64("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2197, ...}) = 0
mprotect(0x8146000, 4096, PROT_READ|PROT_WRITE|PROT_EXEC) = 0
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0} ---
+++ killed by SIGSEGV (core dumped) +++

Confirming crash address from core:

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x08146820 in data_start ()
1

There are 1 answers

1
Phillip On BEST ANSWER

Your mprotect call worked. The program crashes because 0x8146820 holds 0x0000, which disassembles to add [eax], al, and eax holds zero. But address 0 is not mapped. (That's why the segfault is at si_addr=0)