-
Notifications
You must be signed in to change notification settings - Fork 90
/
fix_kaslr_arm64.c
321 lines (254 loc) · 6.32 KB
/
fix_kaslr_arm64.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#ifdef __linux__
#include <linux/limits.h>
#else
#include <limits.h>
#endif
#define KERNEL_TEXT (va_kern_text[va_bits])
#define MIN_ADDR (va_min_addr[va_bits])
#define KERN_VA(p) (void *)((unsigned long)(p) - (unsigned long)kern_buf + KERNEL_TEXT)
#define LOCAL_VA(p) (void *)((unsigned long)(p) - KERNEL_TEXT + (unsigned long)kern_buf)
#define IN_RANGE(p, b, l) (((uint8_t *)(p) >= (uint8_t *)(b)) && ((uint8_t *)(p) < ((uint8_t *)(b) + (ssize_t)(l))))
#define R_AARCH64_RELATIVE (0x403)
#define R_AARCH64_ABS (0x101)
#define ARCH_BITS (64)
uint8_t *kern_buf;
size_t kern_size;
size_t kern_mmap_size;
char infile[PATH_MAX];
char outfile[PATH_MAX];
int va_bits = 39;
size_t va_kern_text[ARCH_BITS] = {0};
size_t va_min_addr[ARCH_BITS] = {0};
struct rela_entry_t {
uint64_t offset;
uint64_t info;
uint64_t sym;
};
struct Elf64_Sym {
uint32_t st_name;
uint8_t st_info;
uint8_t st_other;
uint16_t st_shndx;
uint64_t st_value;
uint64_t st_size;
};
struct rela_entry_t *rela_start;
struct rela_entry_t *rela_end;
static inline int32_t
extract_signed_bitfield (uint32_t insn, unsigned width, unsigned offset)
{
unsigned shift_l = sizeof (int32_t) * 8 - (offset + width);
unsigned shift_r = sizeof (int32_t) * 8 - width;
return ((int32_t) insn << shift_l) >> shift_r;
} __attribute__((always_inline))
static inline int
parse_insn_adrp(uint32_t insn, ssize_t *offset)
{
uint32_t immlo = (insn >> 29) & 0x3;
int32_t immhi = extract_signed_bitfield(insn, 19, 5) << 2;
*offset = (immhi | immlo) * 4096;
return 0;
} __attribute__((always_inline))
static inline int
parse_insn_add(uint32_t insn, uint32_t *inc)
{
*inc = (insn >> 10) & 0xfff;
return 0;
} __attribute__((always_inline))
static inline int alloc_kern_buf()
{
struct stat st;
size_t malloc_size;
int fd;
if (stat(infile, &st) == -1) {
perror("stat failed");
return -1;
}
kern_size = (size_t)st.st_size;
kern_mmap_size = (kern_size + 0xfff) & (~0xfff);
kern_buf = (uint8_t *)mmap(NULL, kern_mmap_size,
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (kern_buf == (void *)-1) {
perror("mmap failed");
return -1;
}
printf("kern_buf @ %p, mmap_size = %lu\n", kern_buf, kern_mmap_size);
fd = open(infile, O_RDONLY);
if (fd == -1) {
perror("open failed");
return -1;
}
read(fd, kern_buf, kern_size);
close(fd);
return 0;
} __attribute__((always_inline))
/* Calculates critical locations
* - rela_entry
* - rela_end;
*/
static inline int parse_rela_sect_smart()
{
#define CONT_THRESHOLD 50
#define GAP_THRESHOLD 5
struct rela_entry_t *p;
int cont = 0;
p = (struct rela_entry_t *)kern_buf;
for (;;) {
if ((size_t)p - (size_t)kern_buf >= kern_mmap_size) {
printf("Failed to locate .rela section. Bail out.\n");
exit(-1);
}
if (p->info == R_AARCH64_RELATIVE ||
p->info == R_AARCH64_ABS) {
if (p->offset >= MIN_ADDR &&
p->sym >= MIN_ADDR) {
cont++;
}
}
else if ((p->info & 0xfff) == 0x101) {
cont++;
}
else {
cont = 0;
}
if (cont == CONT_THRESHOLD) {
rela_start = p - (CONT_THRESHOLD - 1);
printf("rela_start = %p\n", KERN_VA(p));
for (;;) {
struct rela_entry_t *p1;
while (p->info == R_AARCH64_RELATIVE ||
p->info == R_AARCH64_ABS ||
(p->info & 0xfff) == 0x101) {
p++;
}
p1 = p;
while ((p1 - p) < GAP_THRESHOLD) {
if (p1->info == R_AARCH64_RELATIVE ||
p1->info == R_AARCH64_ABS ||
(p1->info & 0xfff) == 0x101) {
break;
}
p1++;
}
if ((p1 - p) >= GAP_THRESHOLD) {
break;
}
else {
p = p1;
}
}
printf("p->info = 0x%lx\n", p->info);
rela_end = p;
printf("rela_end = %p\n", KERN_VA(p));
return 0;
}
if (cont) {
p++;
}
else {
p = (struct rela_entry_t*)((size_t)p + sizeof(void *));
}
}
return -1;
}
static inline int relocate_kernel()
{
#define KERNEL_SLIDE (0)
struct rela_entry_t *rela_entry = rela_start;
int64_t sym_offset;
uint64_t sym_info;
size_t sym_addr;
int count = 0;
while (rela_entry < rela_end)
{
sym_offset = rela_entry->offset;
sym_info = rela_entry->info;
sym_addr = rela_entry->sym;
size_t *p = (size_t *)(sym_offset + KERNEL_SLIDE);
if (sym_info == R_AARCH64_RELATIVE) {
size_t new_addr = sym_addr + KERNEL_SLIDE;
// printf("<%p>\n", (void *)new_addr);
*(size_t *)LOCAL_VA(p) = new_addr;
}
else if ((uint32_t)sym_info == R_AARCH64_ABS) {
struct Elf64_Sym *elf64_sym;
elf64_sym = (struct Elf64_Sym *)((size_t)rela_end + 24 * ((sym_info >> 32) & 0xffffffff));
if (elf64_sym->st_shndx) {
size_t real_stext = elf64_sym->st_value;
if ((int64_t)elf64_sym->st_shndx != -15) {
real_stext += KERNEL_SLIDE;
}
// printf("[%p]\n", (void *)(real_stext + sym_addr));
*(size_t *)LOCAL_VA(p) = real_stext + sym_addr;
}
}
rela_entry++;
count++;
}
printf("%d entries processed\n", count);
return 0;
} __attribute__((always_inline))
static inline int write_outfile()
{
int fd;
fd = open(outfile, O_CREAT | O_RDWR, 0666);
if (fd == -1) {
perror("outfile");
return -1;
}
write(fd, kern_buf, kern_size);
close(fd);
return 0;
} __attribute__((always_inline))
int main(int argc, char **argv)
{
/* initialize va */
va_kern_text[39] = 0xffffff8008080000UL;
va_min_addr[39] = 0xffffff8000000000UL;
va_kern_text[48] = 0xFFFF000008080000UL;
va_min_addr[48] = 0xFFFF000000000000UL;
if (argc != 3 && argc != 4) {
printf("Usage: fix_kaslr_arm64 <infile> <outfile> [va_bits]\n");
printf("By default, va_bits = 39\n");
return -1;
}
if (argc == 4) {
va_bits = (int)strtol(argv[3], NULL, 10);
if (va_bits < 0 || va_bits >= ARCH_BITS) {
printf("Invalid va_bits!\n");
return -1;
}
if (va_kern_text[va_bits] == 0 ||
va_min_addr[va_bits] == 0) {
printf("Unsupported va_bits!\n");
return -1;
}
}
strncpy(infile, argv[1], PATH_MAX);
strncpy(outfile, argv[2], PATH_MAX);
printf("Original kernel: %s, output file: %s\n", infile, outfile);
if (alloc_kern_buf()) {
return -1;
}
if (parse_rela_sect_smart()) {
return -1;
}
if (relocate_kernel()) {
return -1;
}
if (write_outfile()) {
return -1;
}
munmap(kern_buf, kern_mmap_size);
return 0;
}