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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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 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?
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?
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.