-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemexecve.c
40 lines (26 loc) · 857 Bytes
/
memexecve.c
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
37
38
#define _GNU_SOURCE /* for memfd */
#include <sys/mman.h> /* memfd_create */
#include <unistd.h> /* write */
#include <stdio.h>
#include <stdlib.h>
#include "memexecve.h"
void exec_elf(char* raw_elf, size_t elf_size, char** argv, char** envp)
{
int fd;
char procfs_filename[0x100] = {};
/*
* Create an anonymous file which we can refer
* to via `fd` */
fd = memfd_create("ELF_HANDLE", MFD_CLOEXEC);
/*
* Write our elf data to it - subsequent reads by execve
* will return our written data. */
write(fd, raw_elf, elf_size);
/* loop back, execve will fail otherwise */
lseek(fd, SEEK_SET, 0);
sprintf(procfs_filename, "/proc/self/fd/%u", fd);
execve(procfs_filename, argv, envp);
exit(0);
/* Make sure we're never ever returning */
__builtin_unreachable();
}