0
votes

I am trying to inject code into a running process using GDB and am getting a SIGSEGV everytime I attempt to run my code. I am on ubuntu (Oneiric). I export the code to an environment variable using a python script to print them out, then from within gdb, find the desired code with something like x/50s *((char**)environ), and then pick a better address to examine instructions from.

So, when I do x/30i *(addressOfCode), the assembly instructions there are an EXACT match with the ones I want to run. So, if I insert a breakpoint at the starting point of my code, jump there, and stepi, I get a segfault. Moreover, if I choose a place in memory and insert some NOPs, break at one of them, jump to it, and stepi, I also get a segfault.

How can I avoid this? Am I segfaulting just because I am jumping out of the designated memory for the process? What options are there for me to get around this?

Thanks for your help!

1

1 Answers

0
votes

Am I segfaulting just because I am jumping out of the designated memory for the process?

I can't tell what above statement means. There is no such thing as "designated memory for process".

Most likely, you are getting SIGSEGV because you don't have executable stack (and environment variables on Linux and many other UNIX variants are passed on the stack).

If you are on Linux, try linking with gcc main.c ... -Wl,-z,execstack. If that makes your code injection work, then non-executable stack is indeed your problem.