Simon
Simon

Reputation: 2115

Doing an ASM call / ret in C

I try to do a simple call / ret sequence in assembly (from c code compiled with GCC), by manually writing the ret op code, and making a call to the ret address:

void *addr;
addr = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
// Writing the ret op code
((char*)addr)[0] = 0xC3;
// Going to addr with the ret
asm volatile("call *%0" : : "r" (addr));

But I get a segmentation fault. Anyone would know why, and how to correct ?

Upvotes: 1

Views: 547

Answers (1)

univerio
univerio

Reputation: 20548

In order to be able to execute instructions on a memory page, read and write privileges are not enough; it also needs to be marked executable (PROT_EXEC).

Upvotes: 1

Related Questions