How to Execute the ELF Binary which already resides inside the memory
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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
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.
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
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.
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
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.