LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to Execute the ELF Binary which already resides inside the memory (https://www.linuxquestions.org/questions/programming-9/how-to-execute-the-elf-binary-which-already-resides-inside-the-memory-667710/)

bloomstar7 09-05-2008 03:15 AM

How to Execute the ELF Binary which already resides inside the memory
 
Hi Guyz,

Is there any way to execute the ELF binary which already resides inside the memory. If so could you please let me know.

Thanks in Advance,
Bloom Star

jim mcnamara 09-05-2008 01:22 PM

How do you know a binary resides in memory? Are you referring to shared libraries? or sharefd memory?

bloomstar7 09-06-2008 04:07 AM

Actually in my current scenario, we are extracting the elf binary and keeping it in memory. I just want to execute that binary directly from memory itself.
I know there are ways to load and execute the elf binary once i copy that to the file system. But i want to avoid keeping it in the file system.

unSpawn 09-06-2008 04:43 AM

Could you please explain why you consider a scenario like that?

jiml8 09-06-2008 11:58 AM

Security is one reason why you might do this. I am contemplating something similar because I need to put a public key in some code. What I myself might be doing is encrypting the entire code module that contains the code to create the key, then loading the module from disk at runtime, decrypting it on the fly, and executing it.

That said, I don't know how to execute it once loaded either. I'll be researching it, but if someone here already knows the answer, that would be great.

unSpawn 09-06-2008 01:08 PM

With "you" I actually meant bloomstar7 and "explain" not in a roundabout or theoretical way?

jiml8 09-06-2008 01:26 PM

I know you meant bloomstar. I weighed in because I am looking at the exact same problem, and I have a perfectly legitimate reason for wanting to do it.

ErV 09-06-2008 07:49 PM

comment removed

ta0kira 09-07-2008 07:57 AM

I contemplated this several years ago because I wanted to create an encrypted binary. That said, I never got around to figuring it out.

The problem with having the file itself loaded into memory is that nothing is mapped how it should be. The dynamic loader counts on a tangible file to map symbols into a process image and I don't know if it can do that with something already in memory. You might be stuck writing your own version of the dynamic loader that works from a memory location.

ELF programs are generally preceded by a small program in ld.so that loads all of the appropriate symbols from shared libraries. This program reads the executable file itself for its dependencies. I don't know if the loader even loads the entire binary file at once. In any case, it isn't just a matter of putting the program into memory and saying "go!"

How trivial is the binary "file"? Is it out of the question to just write it to a real file and execute it from there?
ta0kira

bloomstar7 09-07-2008 09:25 AM

As i explained above i choose this scenario because of security concern. Actually my ELF contains the security information which i want to avoid keeping it on the file system.

As per the current comments it looks like there is no way of executing the ELF directly form the memory itself. If there is a way please feel free to comment it.

And as of the other option, i am thinking of keeping it at some random location inside the file system, loading it using "execve" command and immediately deleting it form the file system. It means my ELF will present on file system at some random location for small amount of time. For doing this all of my ELF should be loaded in to the memory when i execute "execve" command. I am not sure whether "execve" command loads whole ELF in to the memory or not.(ofcourse if i have enough ram to fit whole of my elf).

Here just i want to confirm whether there is any way or option to force loading whole ELF in to the memory in single time? If so please let me know.

Please feel free to comment your opinions if you feel there is any other better way of doing it.

ta0kira 09-07-2008 09:34 AM

Well, how does the binary get into memory? Is it compiled into its location, or does it originate from an encrypted file? Have you thought about creating an ext2 image just large enough to hold the binary, then encrypting that image with a loopback?
ta0kira

bloomstar7 09-08-2008 09:46 AM

Yes. Binary came in to memory by extracting the encrypted file. I am not sure of how to create the ext2 image of the binary. If you don't mind could you please explain me a little bit.

ta0kira 09-08-2008 04:28 PM

This can be a little complex, depending on if your system has dm-crypt or just losetup. Is /usr/sbin/cryptsetup a valid program on your system? I'll have to formulate something simple because I've been using my own scripts for this for 3+ years and they do a whole lot more than what you need.
ta0kira

kralizec 04-01-2010 07:21 AM

I know this is an ooooooold post... but... I'm trying to do the same thing at the moment... anyone got news on getting executed an elf that's already in ram?

gnashley 04-01-2010 01:34 PM

XIP -execute-in-place?


All times are GMT -5. The time now is 08:20 PM.