Repository navigation
Expand file tree
/
Copy pathloader_linux.c
More file actions
99 lines (92 loc) · 3.16 KB
/
Copy pathloader_linux.c
File metadata and controls
99 lines (92 loc) · 3.16 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// RUN: %neverc -c %s -o %t.o
/*
* DynCode loader for Linux / Android (arm64 and x86_64).
*
* Reads a flat `.bin` dyncode file, maps it with W^X discipline
* (RW for writing, then mprotect to RX before calling), and flushes
* the i-cache on arm64 (x86 has coherent caches).
*
* Two calling conventions are exposed based on argc:
*
* ./loader <dyncode.bin> <int0> <int1>
* -> treats entry as `int f(int, int)` and uses its return
* value as the process exit code. Useful for the
* computation-only dyncodes that do not self-exit.
*
* ./loader <dyncode.bin>
* -> treats entry as `void f(void)`; the dyncode is expected
* to terminate via the kernel (exit / exit_group syscall).
* The loader itself returns 0 if control ever falls through.
*
* Build (native):
* cc -O2 -o loader_linux loader_linux.c
*
* Android cross-build:
* $NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android29-clang \
* -O2 -o loader_android loader_linux.c
*
* Tested platforms:
* - Ubuntu 22.04 arm64
* - Ubuntu 22.04 x86_64
* - Android 14 arm64 (needs MADV_UNMERGEABLE; SELinux may require
* `setenforce 0` or a policy allowing execmem for the test user).
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
static void icache_flush(void *addr, size_t sz) {
#if defined(__aarch64__) || defined(__arm__)
__builtin___clear_cache((char *)addr, (char *)addr + sz);
#else
(void)addr;
(void)sz;
#endif
}
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "usage: %s <dyncode.bin> [arg0] [arg1]\n", argv[0]);
return 1;
}
const char *path = argv[1];
int fd = open(path, O_RDONLY);
if (fd < 0) { perror("open"); return 1; }
struct stat st;
if (fstat(fd, &st) < 0) { perror("fstat"); close(fd); return 1; }
size_t sz = (size_t)st.st_size;
if (sz == 0) { fprintf(stderr, "empty: %s\n", path); close(fd); return 1; }
/* Round up to a page so mmap gets a whole-page allocation. */
long psize = sysconf(_SC_PAGESIZE);
size_t alloc_sz = ((sz + (size_t)psize - 1) / (size_t)psize) * (size_t)psize;
/* W^X: map RW, copy payload, then flip to RX. */
void *mem = mmap(NULL, alloc_sz, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (mem == MAP_FAILED) { perror("mmap"); close(fd); return 1; }
ssize_t n = 0, total = 0;
while ((size_t)total < sz) {
n = read(fd, (char *)mem + total, sz - (size_t)total);
if (n <= 0) { perror("read"); munmap(mem, alloc_sz); close(fd); return 1; }
total += n;
}
close(fd);
if (mprotect(mem, alloc_sz, PROT_READ | PROT_EXEC) < 0) {
perror("mprotect RX"); munmap(mem, alloc_sz); return 1;
}
icache_flush(mem, sz);
int ret;
if (argc >= 4) {
int a0 = atoi(argv[2]);
int a1 = atoi(argv[3]);
int (*fn)(int, int) = (int (*)(int, int))mem;
ret = fn(a0, a1);
} else {
void (*fn)(void) = (void (*)(void))mem;
fn();
ret = 0;
}
munmap(mem, alloc_sz);
return ret;
}