Is it possible to pack an executable into a shared library and upon calling a function inside the said library:
- unpack the executable
- use the executable through
fork
The reason I am asking is because I was recently faced with a situation where my shared library was being loaded in a “sandbox” environment (maybe chroot based) and I would have really like the possibility of spawning a separate process for an executable (loose coupling).
As long as you have permission to write to a directory on a filesystem that isn’t mounted
noexec, then you could just store the executable in a large array ofunsigned charand write it out withfwrite, then usefork/execto run it.Really though, the best solution is just to use
fork()withoutexec– just have the child side call into a different function after thefork()(and then exit with_exit()when that function is done).