How can I execute an executable from memory?

Say I included binary in my program at compile time, so I keep it in a variable something like var myExec =[]byte{'s','o','m','e',' ','b','y','t','e','s'} So my question is whether there is a way to execute this binary in my program without writing it back to disk and calling exec or fork on it ? I am writing my application in the Golang, so the method I'm looking for is to do this using Go or C (using CGO).

Basically, I'm looking for something like a bash script pipeline in bash, I just don’t know where I can connect the bytes of my own executable to run it and write it to disk, and then allow os to read it again, it seems a lot of extra work to be done

+5
source share
1 answer

In C, and assuming Linux, you can change the protection of a memory region using the mprotect() system call so that it can execute (i.e.: turn a data region into a code region). After that, you can execute this memory area by jumping into it.

+1
source

Source: https://habr.com/ru/post/1269030/


All Articles