dandroid
dandroid

Reputation: 405

run code stored in memory

Problem:

run a non-trivial c program stored on the heap or data section of another c program as asm instructions.

My progress:

Ran a set of simple instructions that print something to stdout. The instructions are stored on the heap and I allowed the page containing the instructions to be executed and then calling into the raw data as though it was a function. This worked fine.

Next up, I want given any statically linked c program, to just read it's binary and be able to run it's main function while it is in memory from another c program.

I believe the issues are: * jumping to where the main function code is * changing the binary file's addresses which were created when linking so they are relative to where the code lies now in memory

Please let me know if my approach is good or whether I missed something important and what is the best way to go about it.

Thank you

Upvotes: 3

Views: 1721

Answers (1)

BraveNewCurrency
BraveNewCurrency

Reputation: 13065

Modern OSes try not to let you execute code in your data exactly because it's a security nightmare. http://en.wikipedia.org/wiki/No-execute_bit

Even if you get past that, there will be lots more 'gotchas' because both programs will think that they 'own' the stack/heap/etc. Once the new program executes, it's various bits of RAM from the old program will get stomped on. (exec exists just for this reason, to cleanly go from one program to another.)

If you really need to load code, you should make the first one a library, then use dlopen to run it. (You can use objcopy to extract just the subroutine you want and turn it into a library.)

Alternately, you can start the program (in another process) and use strace to inject a little bit of your code into their process to control it.

(If you're really trying to get into shell code, you should have said so. That's a whole 'nother can of worms.)

Upvotes: 2

Related Questions