forked from nforest/droidimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix_kaslr_samsung.c
276 lines (215 loc) · 5.92 KB
/
fix_kaslr_samsung.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
#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>
#include <linux/limits.h>
#define KERNEL_TEXT (0xffffffc000080000UL)
#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))))
uint8_t *kern_buf;
size_t kern_size;
size_t kern_mmap_size;
char infile[PATH_MAX];
char outfile[PATH_MAX];
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()
{
#define PATTERN_RELOCATE_KERNEL (0xd1420277)
int found;
uint32_t *p_insn;
uint32_t insn;
ssize_t offset;
p_insn = (uint32_t *)kern_buf;
found = 0;
while (IN_RANGE(p_insn, kern_buf, kern_size - sizeof(*p_insn) * 4)) {
if (*p_insn == PATTERN_RELOCATE_KERNEL) {
found = 1;
break;
}
p_insn++;
}
printf("__relocate_kernel @ %p(%p)\n", p_insn, KERN_VA(p_insn));
if (!found) {
printf("Can't find __relocate_kernel!\n");
return -1;
}
p_insn++; insn = *p_insn;
printf("insn = %08x\n", insn);
parse_insn_adrp(insn, &offset);
rela_end = (struct rela_entry_t *)(((size_t)p_insn & (~0xfffUL)) + offset);
printf("0x%x %p(%p)\n", (int32_t)offset, rela_end, KERN_VA(rela_end));
p_insn++; insn = *p_insn;
printf("insn = %08x\n", insn);
parse_insn_add(insn, (uint32_t *)&offset);
rela_end = (struct rela_entry_t *)((size_t)rela_end +(uint32_t)offset);
printf("0x%x %p(%p)\n", (int32_t)offset, rela_end, KERN_VA(rela_end));
p_insn++; insn = *p_insn;
printf("insn = %08x\n", insn);
parse_insn_adrp(insn, &offset);
rela_start = (struct rela_entry_t *)(((size_t)p_insn & (~0xfffUL)) + offset);
printf("0x%x %p(%p)\n", (int32_t)offset, rela_start, KERN_VA(rela_start));
p_insn++; insn = *p_insn;
printf("insn = %08x\n", insn);
parse_insn_add(insn, (uint32_t *)&offset);
rela_start = (struct rela_entry_t *)((size_t)rela_start +(uint32_t)offset);
printf("0x%x %p(%p)\n", (int32_t)offset, rela_start, KERN_VA(rela_start));
if (!IN_RANGE(rela_start, kern_buf, kern_size)) {
printf("rela_start out of range!\n");
return -1;
}
if (!IN_RANGE(rela_end, kern_buf, kern_size)) {
printf("rela_end out of range!\n");
return -1;
}
if (rela_end <= rela_start) {
printf("rela_start/end invalid!\n");
return -1;
}
return 0;
} __attribute__((always_inline))
static inline int relocate_kernel()
{
#define KERNEL_SLIDE (0)
#define R_AARCH64_RELATIVE (0x403)
#define R_AARCH64_ABS (0x101)
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;
*(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;
}
*(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);
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)
{
if (argc != 3) {
printf("Usage: fix_kaslr <infile> <outfile>\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()) {
return -1;
}
if (relocate_kernel()) {
return -1;
}
if (write_outfile()) {
return -1;
}
munmap(kern_buf, kern_mmap_size);
return 0;
}