-
Notifications
You must be signed in to change notification settings - Fork 31
/
memrun.go
36 lines (29 loc) · 750 Bytes
/
memrun.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"fmt"
"io/ioutil"
"os"
"syscall"
"unsafe"
)
// the constant values below are valid for x86_64
const (
mfdCloexec = 0x0001
memfdCreate = 319
)
func runFromMemory(displayName string, filePath string) {
fdName := "" // *string cannot be initialized
fd, _, _ := syscall.Syscall(memfdCreate, uintptr(unsafe.Pointer(&fdName)), uintptr(mfdCloexec), 0)
buffer, _ := ioutil.ReadFile(filePath)
_, _ = syscall.Write(int(fd), buffer)
fdPath := fmt.Sprintf("/proc/self/fd/%d", fd)
_ = syscall.Exec(fdPath, []string{displayName}, nil)
}
func main() {
lenArgs := len(os.Args)
if lenArgs < 3 || lenArgs > 3 {
fmt.Println("Usage: memrun process_name elf_binary")
os.Exit(1)
}
runFromMemory(os.Args[1], os.Args[2])
}