forked from ufrisk/MemProcFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmm.c
2881 lines (2750 loc) · 104 KB
/
vmm.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
// vmm.c : implementation of functions related to virtual memory management support.
//
// (c) Ulf Frisk, 2018-2022
// Author: Ulf Frisk, [email protected]
//
#include "vmm.h"
#include "mm.h"
#include "mm_pfn.h"
#include "pdb.h"
#include "vmmheap.h"
#include "vmmproc.h"
#include "vmmvm.h"
#include "vmmwin.h"
#include "vmmwindef.h"
#include "vmmwinobj.h"
#include "vmmwinpool.h"
#include "vmmwinreg.h"
#include "vmmwinsvc.h"
#include "vmmevil.h"
#include "vmmnet.h"
#include "pluginmanager.h"
#include "charutil.h"
#include "util.h"
#ifdef _WIN32
#include <sddl.h>
#endif /* _WIN32 */
// ----------------------------------------------------------------------------
// CACHE FUNCTIONALITY:
// PHYSICAL MEMORY CACHING FOR READS AND PAGE TABLES
// ----------------------------------------------------------------------------
#define VMM_CACHE_GET_BUCKET(qwA) ((VMM_CACHE_BUCKETS - 1) & ((qwA >> 12) + 13 * (qwA + _rotr16((WORD)qwA, 9) + _rotr((DWORD)qwA, 17) + _rotr64(qwA, 31))))
/*
* Retrieve cache table from ctxVmm given a specific tag.
*/
PVMM_CACHE_TABLE VmmCacheTableGet(_In_ VMM_HANDLE H, _In_ DWORD wTblTag)
{
switch(wTblTag) {
case VMM_CACHE_TAG_PHYS:
H->vmm.Cache.PHYS.cMaxMems = VMM_CACHE_REGION_MEMS_PHYS;
return &H->vmm.Cache.PHYS;
case VMM_CACHE_TAG_TLB:
H->vmm.Cache.TLB.cMaxMems = VMM_CACHE_REGION_MEMS_TLB;
return &H->vmm.Cache.TLB;
case VMM_CACHE_TAG_PAGING:
H->vmm.Cache.PAGING.cMaxMems = VMM_CACHE_REGION_MEMS_PAGING;
return &H->vmm.Cache.PAGING;
default:
return NULL;
}
}
/*
* Clear the oldest region of all InUse entries and make it the new active region.
* -- wTblTag
*/
VOID VmmCacheClearPartial(_In_ VMM_HANDLE H, _In_ DWORD dwTblTag)
{
PVMM_CACHE_TABLE t;
PVMMOB_CACHE_MEM pOb;
PSLIST_ENTRY e;
DWORD iR;
PVMM_PROCESS pObProcess = NULL;
t = VmmCacheTableGet(H, dwTblTag);
if(!t || !t->fActive) { return; }
EnterCriticalSection(&t->Lock);
iR = (t->iR + (VMM_CACHE_REGIONS - 1)) % VMM_CACHE_REGIONS;
// 1: clear all entries from region
AcquireSRWLockExclusive(&t->R[iR].LockSRW);
while((e = InterlockedPopEntrySList(&t->R[iR].ListHeadInUse))) {
pOb = CONTAINING_RECORD(e, VMMOB_CACHE_MEM, SListInUse);
// remove region refcount of object - callback will take care of
// re-insertion into empty list when refcount becomes low enough.
Ob_DECREF(pOb);
}
ZeroMemory(t->R[iR].B, VMM_CACHE_BUCKETS * sizeof(PVMMOB_CACHE_MEM));
ReleaseSRWLockExclusive(&t->R[iR].LockSRW);
t->iR = iR;
t->fAllActiveRegions = t->fAllActiveRegions || (t->iR == 0);
LeaveCriticalSection(&t->Lock);
// 2: if tlb cache clear -> update process 'is spider done' flag
if(t->fAllActiveRegions && (dwTblTag == VMM_CACHE_TAG_TLB)) {
while((pObProcess = VmmProcessGetNext(H, pObProcess, 0))) {
if(pObProcess->fTlbSpiderDone) {
EnterCriticalSection(&pObProcess->LockUpdate);
pObProcess->fTlbSpiderDone = FALSE;
LeaveCriticalSection(&pObProcess->LockUpdate);
}
}
}
}
/*
* Clear the specified cache from all entries.
* -- dwTblTag
*/
VOID VmmCacheClear(_In_ VMM_HANDLE H, _In_ DWORD dwTblTag)
{
DWORD i;
for(i = 0; i < VMM_CACHE_REGIONS; i++) {
VmmCacheClearPartial(H, dwTblTag);
}
}
/*
* Retrieve an item from the cache.
* CALLER DECREF: return
* -- dwTblTag
* -- qwA
* -- fCurrentRegionOnly = only retrieve from the currently active cache region.
* -- return
*/
PVMMOB_CACHE_MEM VmmCacheGetEx(_In_ VMM_HANDLE H, _In_ DWORD dwTblTag, _In_ QWORD qwA, _In_ BOOL fCurrentRegionOnly)
{
PVMM_CACHE_TABLE t;
DWORD iB, iR, iRB, iRC;
PVMMOB_CACHE_MEM pOb;
t = VmmCacheTableGet(H, dwTblTag);
if(!t || !t->fActive) { return NULL; }
iB = VMM_CACHE_GET_BUCKET(qwA);
iRB = t->iR;
for(iRC = 0; iRC < VMM_CACHE_REGIONS; iRC++) {
iR = (iRB + iRC) % VMM_CACHE_REGIONS;
AcquireSRWLockShared(&t->R[iR].LockSRW);
pOb = t->R[iR].B[iB];
while(pOb && (pOb->h.qwA != qwA)) {
pOb = pOb->FLink;
}
if(pOb) {
Ob_INCREF(pOb);
ReleaseSRWLockShared(&t->R[iR].LockSRW);
return pOb;
}
ReleaseSRWLockShared(&t->R[iR].LockSRW);
if(fCurrentRegionOnly) { break; }
}
return NULL;
}
/*
* Retrieve an item from the cache.
* CALLER DECREF: return
* -- dwTblTag
* -- qwA
* -- return
*/
PVMMOB_CACHE_MEM VmmCacheGet(_In_ VMM_HANDLE H, _In_ DWORD dwTblTag, _In_ QWORD qwA)
{
return VmmCacheGetEx(H, dwTblTag, qwA, FALSE);
}
VOID VmmCache_CallbackRefCount1(PVMMOB_CACHE_MEM pOb)
{
VMM_HANDLE H = ((POB)pOb)->H;
PVMM_CACHE_TABLE t = VmmCacheTableGet(H, ((POB)pOb)->_tag);
if(!t) {
VmmLog(H, MID_VMM, LOGLEVEL_CRITICAL, "ERROR - SHOULD NOT HAPPEN - INVALID OBJECT TAG %02X", ((POB)pOb)->_tag);
return;
}
if(!t->fActive) { return; }
Ob_INCREF(pOb);
InterlockedPushEntrySList(&t->R[pOb->iR].ListHeadEmpty, &pOb->SListEmpty);
}
PVMMOB_CACHE_MEM VmmCacheReserve(_In_ VMM_HANDLE H, _In_ DWORD dwTblTag)
{
PVMM_CACHE_TABLE t;
PVMMOB_CACHE_MEM pOb;
PSLIST_ENTRY e;
WORD cLoopProtect = 0;
t = VmmCacheTableGet(H, dwTblTag);
if(!t || !t->fActive) { return NULL; }
while(!(e = InterlockedPopEntrySList(&t->R[t->iR].ListHeadEmpty))) {
if(QueryDepthSList(&t->R[t->iR].ListHeadTotal) < t->cMaxMems) {
// below max threshold -> create new
pOb = Ob_AllocEx(H, t->tag, LMEM_ZEROINIT, sizeof(VMMOB_CACHE_MEM), NULL, (OB_CLEANUP_CB)VmmCache_CallbackRefCount1);
if(!pOb) { return NULL; }
pOb->iR = t->iR;
pOb->h.version = MEM_SCATTER_VERSION;
pOb->h.cb = 0x1000;
pOb->h.pb = pOb->pb;
pOb->h.qwA = MEM_SCATTER_ADDR_INVALID;
Ob_INCREF(pOb); // "total list" reference
InterlockedPushEntrySList(&t->R[pOb->iR].ListHeadTotal, &pOb->SListTotal);
return pOb; // return fresh object - refcount = 2.
}
// reclaim existing entries by clearing the oldest cache region.
VmmCacheClearPartial(H, dwTblTag);
if(++cLoopProtect == VMM_CACHE_REGIONS) {
VmmLog(H, MID_VMM, LOGLEVEL_WARNING, "SHOULD NOT HAPPEN - CACHE %04X DRAINED OF ENTRIES", dwTblTag);
Sleep(10);
}
}
pOb = CONTAINING_RECORD(e, VMMOB_CACHE_MEM, SListEmpty);
pOb->h.qwA = MEM_SCATTER_ADDR_INVALID;
pOb->h.f = FALSE;
return pOb; // reference overtaken by callee (from EmptyList)
}
/*
* Return an entry retrieved with VmmCacheReserve to the cache.
* NB! no other items may be returned with this function!
* FUNCTION DECREF: pOb
* -- H
* -- pOb
*/
VOID VmmCacheReserveReturn(_In_ VMM_HANDLE H, _In_opt_ PVMMOB_CACHE_MEM pOb)
{
PVMM_CACHE_TABLE t;
if(!pOb) { return; }
t = VmmCacheTableGet(H, ((POB)pOb)->_tag);
if(!t) {
VmmLog(H, MID_VMM, LOGLEVEL_CRITICAL, "ERROR - SHOULD NOT HAPPEN - INVALID OBJECT TAG %02X", ((POB)pOb)->_tag);
return;
}
if(!t->fActive || !pOb->h.f || (pOb->h.qwA == MEM_SCATTER_ADDR_INVALID)) {
// decrement refcount of object - callback will take care of
// re-insertion into empty list when refcount becomes low enough.
Ob_DECREF(pOb);
return;
}
// insert into map - refcount will be overtaken by "cache region".
pOb->iB = VMM_CACHE_GET_BUCKET(pOb->h.qwA);
AcquireSRWLockExclusive(&t->R[pOb->iR].LockSRW);
InterlockedPushEntrySList(&t->R[pOb->iR].ListHeadInUse, &pOb->SListInUse);
// insert into "bucket"
pOb->BLink = NULL;
pOb->FLink = t->R[pOb->iR].B[pOb->iB];
if(pOb->FLink) { pOb->FLink->BLink = pOb; }
t->R[pOb->iR].B[pOb->iB] = pOb;
ReleaseSRWLockExclusive(&t->R[pOb->iR].LockSRW);
}
VOID VmmCacheClose(_In_ VMM_HANDLE H, _In_ DWORD dwTblTag)
{
PVMM_CACHE_TABLE t;
PVMMOB_CACHE_MEM pOb;
PSLIST_ENTRY e;
DWORD iR;
t = VmmCacheTableGet(H, dwTblTag);
if(!t || !t->fActive) { return; }
t->fActive = FALSE;
EnterCriticalSection(&t->Lock);
for(iR = 0; iR < VMM_CACHE_REGIONS; iR++) {
AcquireSRWLockExclusive(&t->R[iR].LockSRW);
// remove from "empty list"
while((e = InterlockedPopEntrySList(&t->R[iR].ListHeadEmpty))) {
pOb = CONTAINING_RECORD(e, VMMOB_CACHE_MEM, SListEmpty);
Ob_DECREF(pOb);
}
// remove from "in use list"
while((e = InterlockedPopEntrySList(&t->R[iR].ListHeadInUse))) {
pOb = CONTAINING_RECORD(e, VMMOB_CACHE_MEM, SListInUse);
Ob_DECREF(pOb);
}
// remove from "total list"
while((e = InterlockedPopEntrySList(&t->R[iR].ListHeadTotal))) {
pOb = CONTAINING_RECORD(e, VMMOB_CACHE_MEM, SListTotal);
Ob_DECREF(pOb);
}
}
DeleteCriticalSection(&t->Lock);
}
VOID VmmCacheInitialize(_In_ VMM_HANDLE H, _In_ DWORD dwTblTag)
{
DWORD iR, iMEM;
PVMM_CACHE_TABLE t;
PVMMOB_CACHE_MEM pOb;
t = VmmCacheTableGet(H, dwTblTag);
if(!t || t->fActive) { return; }
for(iR = 0; iR < VMM_CACHE_REGIONS; iR++) {
InitializeSRWLock(&t->R[iR].LockSRW);
InitializeSListHead(&t->R[iR].ListHeadEmpty);
InitializeSListHead(&t->R[iR].ListHeadInUse);
InitializeSListHead(&t->R[iR].ListHeadTotal);
if(VMM_CACHE_REGION_MEMS_INITALLOC) {
for(iMEM = 0; iMEM < t->cMaxMems; iMEM++) {
pOb = Ob_AllocEx(H, dwTblTag, LMEM_ZEROINIT, sizeof(VMMOB_CACHE_MEM), NULL, (OB_CLEANUP_CB)VmmCache_CallbackRefCount1);
if(!pOb) { continue; }
pOb->iR = iR;
pOb->h.version = MEM_SCATTER_VERSION;
pOb->h.cb = 0x1000;
pOb->h.pb = pOb->pb;
pOb->h.qwA = MEM_SCATTER_ADDR_INVALID;
Ob_INCREF(pOb);
InterlockedPushEntrySList(&t->R[iR].ListHeadEmpty, &pOb->SListEmpty);
InterlockedPushEntrySList(&t->R[iR].ListHeadTotal, &pOb->SListTotal);
}
}
}
InitializeCriticalSection(&t->Lock);
t->tag = dwTblTag;
t->fActive = TRUE;
}
/*
* Invalidate a cache entry (if exists)
*/
VOID VmmCacheInvalidate_2(_In_ VMM_HANDLE H, _In_ DWORD dwTblTag, _In_ QWORD qwA)
{
PVMM_CACHE_TABLE t;
PVMMOB_CACHE_MEM pOb;
t = VmmCacheTableGet(H, dwTblTag);
if(!t || !t->fActive) { return; }
while((pOb = VmmCacheGet(H, dwTblTag, qwA))) {
AcquireSRWLockExclusive(&t->R[pOb->iR].LockSRW);
// remove from bucket list
if(pOb->FLink) {
pOb->FLink->BLink = pOb->BLink;
}
if(pOb->BLink) {
pOb->BLink->FLink = pOb->FLink;
} else {
t->R[pOb->iR].B[pOb->iB] = pOb->FLink;
}
// NB! "leak" object - i.e. keep it on InUse list until the cache
// region itself gets cleared. somewhat ugly, but simple...
ReleaseSRWLockExclusive(&t->R[pOb->iR].LockSRW);
Ob_DECREF(pOb);
}
}
VOID VmmCacheInvalidate(_In_ VMM_HANDLE H, _In_ QWORD pa)
{
VmmCacheInvalidate_2(H, VMM_CACHE_TAG_TLB, pa);
VmmCacheInvalidate_2(H, VMM_CACHE_TAG_PHYS, pa);
}
PVMMOB_CACHE_MEM VmmCacheGet_FromDeviceOnMiss(_In_ VMM_HANDLE H, _In_ DWORD dwTblTag, _In_ DWORD dwTblTagSecondaryOpt, _In_ QWORD qwA)
{
PVMMOB_CACHE_MEM pObMEM, pObReservedMEM;
PMEM_SCATTER pMEM;
pObMEM = VmmCacheGet(H, dwTblTag, qwA);
if(pObMEM) { return pObMEM; }
if((pObReservedMEM = VmmCacheReserve(H, dwTblTag))) {
pMEM = &pObReservedMEM->h;
pMEM->qwA = qwA;
if(dwTblTagSecondaryOpt && (pObMEM = VmmCacheGet(H, dwTblTagSecondaryOpt, qwA))) {
pMEM->f = TRUE;
memcpy(pMEM->pb, pObMEM->pb, 0x1000);
Ob_DECREF(pObMEM);
pObMEM = NULL;
}
if(!pMEM->f) {
LcReadScatter(H->hLC, 1, &pMEM);
}
if(pMEM->f) {
Ob_INCREF(pObReservedMEM);
VmmCacheReserveReturn(H, pObReservedMEM);
return pObReservedMEM;
}
VmmCacheReserveReturn(H, pObReservedMEM);
}
return NULL;
}
/*
* Retrieve a page table from a given physical address (if possible).
* CALLER DECREF: return
* -- H
* -- pa
* -- fCacheOnly
* -- return = Cache entry on success, NULL on fail.
*/
PVMMOB_CACHE_MEM VmmTlbGetPageTable(_In_ VMM_HANDLE H, _In_ QWORD pa, _In_ BOOL fCacheOnly)
{
PVMMOB_CACHE_MEM pObMEM;
pObMEM = VmmCacheGet(H, VMM_CACHE_TAG_TLB, pa);
if(pObMEM) {
InterlockedIncrement64(&H->vmm.stat.cTlbCacheHit);
return pObMEM;
}
if(fCacheOnly) { return NULL; }
// try retrieve from (1) TLB cache, (2) PHYS cache, (3) device
pObMEM = VmmCacheGet_FromDeviceOnMiss(H, VMM_CACHE_TAG_TLB, VMM_CACHE_TAG_PHYS, pa);
if(!pObMEM) {
InterlockedIncrement64(&H->vmm.stat.cTlbReadFail);
return NULL;
}
InterlockedIncrement64(&H->vmm.stat.cTlbReadSuccess);
if(VmmTlbPageTableVerify(H, pObMEM->h.pb, pObMEM->h.qwA, FALSE)) {
return pObMEM;
}
Ob_DECREF(pObMEM);
return NULL;
}
/*
* Translate a virtual address to a physical address by walking the page tables.
* The successfully translated Physical Address (PA) is returned in ppa.
* Upon fail the PTE will be returned in ppa (if possible) - which may be used
* to further lookup virtual memory in case of PageFile or Win10 MemCompression.
* -- H
* -- paDTB
* -- fUserOnly
* -- va
* -- ppa
* -- return
*/
_Success_(return)
BOOL VmmVirt2PhysEx(_In_ VMM_HANDLE H, _In_ QWORD paDTB, _In_ BOOL fUserOnly, _In_ QWORD va, _Out_ PQWORD ppa)
{
*ppa = 0;
if(H->vmm.tpMemoryModel == VMM_MEMORYMODEL_NA) { return FALSE; }
return H->vmm.fnMemoryModel.pfnVirt2Phys(H, paDTB, fUserOnly, -1, va, ppa);
}
/*
* Translate a virtual address to a physical address by walking the page tables.
* The successfully translated Physical Address (PA) is returned in ppa.
* Upon fail the PTE will be returned in ppa (if possible) - which may be used
* to further lookup virtual memory in case of PageFile or Win10 MemCompression.
* -- H
* -- pProcess
* -- va
* -- ppa
* -- return
*/
_Success_(return)
BOOL VmmVirt2Phys(_In_ VMM_HANDLE H, _In_opt_ PVMM_PROCESS pProcess, _In_ QWORD va, _Out_ PQWORD ppa)
{
*ppa = 0;
if(!pProcess || (H->vmm.tpMemoryModel == VMM_MEMORYMODEL_NA)) { return FALSE; }
return H->vmm.fnMemoryModel.pfnVirt2Phys(H, pProcess->paDTB, pProcess->fUserOnly, -1, va, ppa);
}
/*
* Spider the TLB (page table cache) to load all page table pages into the cache.
* This is done to speed up various subsequent virtual memory accesses.
* NB! pages may fall out of the cache if it's in heavy use or doe to timing.
* -- H
* -- pProcess
*/
VOID VmmTlbSpider(_In_ VMM_HANDLE H, _In_ PVMM_PROCESS pProcess)
{
if(H->vmm.tpMemoryModel == VMM_MEMORYMODEL_NA) { return; }
H->vmm.fnMemoryModel.pfnTlbSpider(H, pProcess);
}
/*
* Try verify that a supplied page table in pb is valid by analyzing it.
* -- H
* -- pb = 0x1000 bytes containing the page table page.
* -- pa = physical address if the page table page.
* -- fSelfRefReq = is a self referential entry required to be in the map? (PML4 for Windows).
*/
BOOL VmmTlbPageTableVerify(_In_ VMM_HANDLE H, _Inout_ PBYTE pb, _In_ QWORD pa, _In_ BOOL fSelfRefReq)
{
if(H->vmm.tpMemoryModel == VMM_MEMORYMODEL_NA) { return FALSE; }
return H->vmm.fnMemoryModel.pfnTlbPageTableVerify(H, pb, pa, fSelfRefReq);
}
/*
* Prefetch a set of physical addresses contained in pTlbPrefetch into the Tlb.
* NB! pTlbPrefetch must not be updated/altered during the function call.
* -- H
* -- pProcess
* -- pTlbPrefetch = the page table addresses to prefetch (on entry) and empty set on exit.
*/
VOID VmmTlbPrefetch(_In_ VMM_HANDLE H, _In_ POB_SET pTlbPrefetch)
{
QWORD pbTlb = 0;
DWORD cTlbs, i = 0;
PPVMMOB_CACHE_MEM ppObMEMs = NULL;
PPMEM_SCATTER ppMEMs = NULL;
if(!(cTlbs = ObSet_Size(pTlbPrefetch))) { goto fail; }
if(!(ppMEMs = LocalAlloc(0, cTlbs * sizeof(PMEM_SCATTER)))) { goto fail; }
if(!(ppObMEMs = LocalAlloc(0, cTlbs * sizeof(PVMMOB_CACHE_MEM)))) { goto fail; }
while((cTlbs = min(0x2000, ObSet_Size(pTlbPrefetch)))) { // protect cache bleed -> max 0x2000 pages/round
for(i = 0; i < cTlbs; i++) {
ppObMEMs[i] = VmmCacheReserve(H, VMM_CACHE_TAG_TLB);
ppMEMs[i] = &ppObMEMs[i]->h;
ppMEMs[i]->qwA = ObSet_Pop(pTlbPrefetch);
}
LcReadScatter(H->hLC, cTlbs, ppMEMs);
for(i = 0; i < cTlbs; i++) {
if(ppMEMs[i]->f && !VmmTlbPageTableVerify(H, ppMEMs[i]->pb, ppMEMs[i]->qwA, FALSE)) {
ppMEMs[i]->f = FALSE; // "fail" invalid page table read
}
VmmCacheReserveReturn(H, ppObMEMs[i]);
}
}
fail:
LocalFree(ppMEMs);
LocalFree(ppObMEMs);
}
/*
* Prefetch a set of addresses contained in pPrefetchPages into the cache. This
* is useful when reading data from somewhat known addresses over higher latency
* connections.
* NB! pPrefetchPages must not be updated/altered during the function call.
* -- H
* -- pProcess
* -- pPrefetchPages
* -- flags
*/
VOID VmmCachePrefetchPages(_In_ VMM_HANDLE H, _In_opt_ PVMM_PROCESS pProcess, _In_opt_ POB_SET pPrefetchPages, _In_ QWORD flags)
{
QWORD qwA = 0;
DWORD cPages, iMEM = 0;
PPMEM_SCATTER ppMEMs = NULL;
cPages = ObSet_Size(pPrefetchPages);
if(!cPages || (H->vmm.flags & VMM_FLAG_NOCACHE)) { return; }
if(!LcAllocScatter1(cPages, &ppMEMs)) { return; }
while((qwA = ObSet_GetNext(pPrefetchPages, qwA))) {
ppMEMs[iMEM++]->qwA = qwA & ~0xfff;
}
if(pProcess) {
VmmReadScatterVirtual(H, pProcess, ppMEMs, iMEM, flags | VMM_FLAG_CACHE_RECENT_ONLY);
} else {
VmmReadScatterPhysical(H, ppMEMs, iMEM, flags | VMM_FLAG_CACHE_RECENT_ONLY);
}
LcMemFree(ppMEMs);
}
/*
* Prefetch a set of addresses. This is useful when reading data from somewhat
* known addresses over higher latency connections.
* -- H
* -- pProcess
* -- cAddresses
* -- ... = variable list of total cAddresses of addresses of type QWORD.
*/
VOID VmmCachePrefetchPages2(_In_ VMM_HANDLE H, _In_opt_ PVMM_PROCESS pProcess, _In_ DWORD cAddresses, ...)
{
va_list arguments;
POB_SET pObSet = NULL;
if(!cAddresses || !(pObSet = ObSet_New(H))) { return; }
va_start(arguments, cAddresses);
while(cAddresses) {
ObSet_Push(pObSet, va_arg(arguments, QWORD) & ~0xfff);
cAddresses--;
}
va_end(arguments);
VmmCachePrefetchPages(H, pProcess, pObSet, 0);
Ob_DECREF(pObSet);
}
/*
* Prefetch a set of addresses contained in pPrefetchPagesNonPageAligned into
* the cache by first converting them to page aligned pages. This is used when
* reading data from somewhat known addresses over higher latency connections.
* NB! pPrefetchPagesNonPageAligned must not be altered during the function call.
* -- H
* -- pProcess
* -- pPrefetchPagesNonPageAligned
* -- cb
* -- flags
*/
VOID VmmCachePrefetchPages3(_In_ VMM_HANDLE H, _In_opt_ PVMM_PROCESS pProcess, _In_opt_ POB_SET pPrefetchPagesNonPageAligned, _In_ DWORD cb, _In_ QWORD flags)
{
QWORD qwA = 0;
POB_SET pObSetAlign;
if(!cb || !pPrefetchPagesNonPageAligned) { return; }
if(0 == ObSet_Size(pPrefetchPagesNonPageAligned)) { return; }
if(!(pObSetAlign = ObSet_New(H))) { return; }
while((qwA = ObSet_GetNext(pPrefetchPagesNonPageAligned, qwA))) {
ObSet_Push_PageAlign(pObSetAlign, qwA, cb);
}
VmmCachePrefetchPages(H, pProcess, pObSetAlign, flags);
Ob_DECREF(pObSetAlign);
}
/*
* Prefetch an array of optionally non-page aligned addresses. This is useful
* when reading data from somewhat known addresses over higher latency connections.
* -- H
* -- pProcess
* -- cAddresses
* -- pqwAddresses = array of addresses to fetch
* -- cb
* -- flags
*/
VOID VmmCachePrefetchPages4(_In_ VMM_HANDLE H, _In_opt_ PVMM_PROCESS pProcess, _In_ DWORD cAddresses, _In_ PQWORD pqwAddresses, _In_ DWORD cb, _In_ QWORD flags)
{
POB_SET pObSet = NULL;
if(!cAddresses || !(pObSet = ObSet_New(H))) { return; }
while(cAddresses) {
cAddresses--;
if(pqwAddresses[cAddresses]) {
ObSet_Push_PageAlign(pObSet, pqwAddresses[cAddresses], cb);
}
}
VmmCachePrefetchPages(H, pProcess, pObSet, 0);
Ob_DECREF(pObSet);
}
/*
* Prefetch memory of optionally non-page aligned addresses which are derived
* from pmPrefetchObjects by the pfnFilter filter function.
* -- H
* -- pProcess
* -- pmPrefetch = map of objects.
* -- cb
* -- flags
* -- pfnFilter = filter as required by ObMap_FilterSet function.
* -- return = at least one object is found to be prefetched into cache.
*/
BOOL VmmCachePrefetchPages5(_In_ VMM_HANDLE H, _In_opt_ PVMM_PROCESS pProcess, _In_opt_ POB_MAP pmPrefetch, _In_ DWORD cb, _In_ QWORD flags, _In_ VOID(*pfnFilter)(_In_ QWORD k, _In_ PVOID v, _Inout_ POB_SET ps))
{
POB_SET psObCache = ObMap_FilterSet(pmPrefetch, pfnFilter);
BOOL fResult = ObSet_Size(psObCache) > 0;
VmmCachePrefetchPages3(H, pProcess, psObCache, cb, flags);
Ob_DECREF(psObCache);
return fResult;
}
// ----------------------------------------------------------------------------
// PROCESS MANAGEMENT FUNCTIONALITY:
//
// The process 'object' represents a process in the analyzed system.
//
// The process 'object' is an object manager refcount object. The processes may
// contain, in addition to values, sub-objects such as maps of loaded modules
// and memory.
//
// Before updates to the process object happens the 'LockUpdate' generally
// should be acquired.
//
// The active processes are contained in a 'process table' which is also an
// object manager refcount object. Atmoic access (get and increase refcount) is
// guarded by a object manager container which allows for easy retrieval of the
// process table. The process table may also contain a process table for new
// not yet committed process objects. When processes are refreshed in the back-
// ground they are created (or copied by refcount increase) into the new table.
// Once all processes are enumerated the function 'VmmProcessCreateFinish' is
// called and replaces the 'old' table with the 'new' table which becomes the
// active table. The 'old' replaced table is refcount-decreased and possibly
// free'd as a result.
//
// The process object: VMM_PROCESS
// The process table object (only used internally): VMMOB_PROCESS_TABLE
// ----------------------------------------------------------------------------
VOID VmmProcess_TokenTryEnsure(_In_ VMM_HANDLE H, _In_ PVMMOB_PROCESS_TABLE pt)
{
BOOL f, f32 = H->vmm.f32;
DWORD j, i = 0, iM, cbHdr, cb, dwIntegrityLevelIndex = 0;
QWORD va, *pva = NULL;
BYTE pb[0x1000];
PVMM_PROCESS *ppProcess = NULL, pObSystemProcess = NULL;
PVMM_OFFSET_EPROCESS poe = &H->vmm.offset.EPROCESS;
LPSTR szSidIntegrity = NULL;
BOOL fSidIntegrity;
VMM_PROCESS_INTEGRITY_LEVEL IntegrityLevel;
union {
SID SID;
BYTE pb[SECURITY_MAX_SID_SIZE];
} SidIntegrity;
f = poe->opt.TOKEN_TokenId && // token offsets/symbols initialized.
(pObSystemProcess = VmmProcessGet(H, 4)) &&
(pva = LocalAlloc(LMEM_ZEROINIT, pt->c * 2 * sizeof(QWORD))) &&
(ppProcess = LocalAlloc(LMEM_ZEROINIT, pt->c * sizeof(PVMM_PROCESS)));
if(!f) { goto fail; }
cbHdr = f32 ? 0x2c : 0x5c;
cb = cbHdr + poe->opt.TOKEN_cb;
if((cb > 0x1000) || !poe->opt.TOKEN_cb) { goto fail; }
// 1: Get Process and Token VA:
iM = pt->_iFLink;
while(iM && i < pt->c) {
if((ppProcess[i] = pt->_M[iM]) && !ppProcess[i]->win.TOKEN.fInitialized) {
va = VMM_PTR_OFFSET(f32, ppProcess[i]->win.EPROCESS.pb, poe->opt.Token) & (f32 ? ~0x7 : ~0xf);
if(VMM_KADDR(f32, va)) {
ppProcess[i]->win.TOKEN.va = va;
pva[i] = va - cbHdr; // adjust for _OBJECT_HEADER and Pool Header
}
}
iM = pt->_iFLinkM[iM];
i++;
}
// 2: Read Token:
VmmCachePrefetchPages4(H, pObSystemProcess, (DWORD)pt->c, pva, cb, 0);
for(i = 0; i < pt->c; i++) {
if(!pva[i] || !VmmRead2(H, pObSystemProcess, pva[i], pb, cb, VMM_FLAG_FORCECACHE_READ)) { continue; }
// 2.1: fetch TOKEN.UserAndGroups (user id [_SID_AND_ATTRIBUTES])
pva[i] = VMM_PTR_OFFSET(f32, pb + cbHdr, poe->opt.TOKEN_UserAndGroups);
if(!VMM_KADDR(f32, pva[i])) { pva[i] = 0; continue; }
ppProcess[i]->win.TOKEN.vaUserAndGroups = pva[i];
// 2.2: fetch various offsets
for(j = 0, f = FALSE; !f && (j < cbHdr); j += (f32 ? 0x08 : 0x10)) {
f = VMM_POOLTAG_SHORT(*(PDWORD)(pb + j), 'Toke');
}
if(!f) { pva[i] = 0; continue; }
ppProcess[i]->win.TOKEN.qwLUID = *(PQWORD)(pb + cbHdr + poe->opt.TOKEN_TokenId);
ppProcess[i]->win.TOKEN.dwSessionId = *(PDWORD)(pb + cbHdr + poe->opt.TOKEN_SessionId);
if(poe->opt.TOKEN_UserAndGroupCount) {
ppProcess[i]->win.TOKEN.dwUserAndGroupCount = *(PDWORD)(pb + cbHdr + poe->opt.TOKEN_UserAndGroupCount);
}
if(poe->opt.TOKEN_IntegrityLevelIndex) {
dwIntegrityLevelIndex = *(PDWORD)(pb + cbHdr + poe->opt.TOKEN_IntegrityLevelIndex);
if(dwIntegrityLevelIndex > ppProcess[i]->win.TOKEN.dwUserAndGroupCount) { dwIntegrityLevelIndex = 0; }
}
// 2.3: fetch TOKEN.UserAndGroups+dwIntegrityLevelIndex (integrity level [_SID_AND_ATTRIBUTES])
if(dwIntegrityLevelIndex) {
va = VMM_PTR_OFFSET(f32, pb + cbHdr, poe->opt.TOKEN_UserAndGroups) + dwIntegrityLevelIndex * (f32 ? 8ULL : 16ULL);
if(VMM_KADDR(f32, va)) { pva[pt->c + i] = va; }
}
}
// 3: Read SID user & integrity ptr:
VmmCachePrefetchPages4(H, pObSystemProcess, 2 * (DWORD)pt->c, pva, 8, 0);
for(i = 0; i < pt->c; i++) {
// user:
f = pva[i] && VmmRead2(H, pObSystemProcess, pva[i], pb, 8, VMM_FLAG_FORCECACHE_READ) &&
(va = VMM_PTR_OFFSET(f32, pb, 0)) &&
VMM_KADDR(f32, va);
pva[i] = f ? va : 0;
// integrity:
f = pva[pt->c + i] && VmmRead2(H, pObSystemProcess, pva[pt->c + i], pb, 8, VMM_FLAG_FORCECACHE_READ) &&
(va = VMM_PTR_OFFSET(f32, pb, 0)) &&
VMM_KADDR(f32, va);
pva[pt->c + i] = f ? va : 0;
}
// 4: Get SID user & integrity:
VmmCachePrefetchPages4(H, pObSystemProcess, 2 * (DWORD)pt->c, pva, SECURITY_MAX_SID_SIZE, 0);
for(i = 0; i < pt->c; i++) {
if(!ppProcess[i]) { continue; }
// user:
ppProcess[i]->win.TOKEN.fSidUserValid =
(va = pva[i]) &&
VmmRead2(H, pObSystemProcess, va, ppProcess[i]->win.TOKEN.SidUser.pb, SECURITY_MAX_SID_SIZE, VMM_FLAG_FORCECACHE_READ) &&
IsValidSid(&ppProcess[i]->win.TOKEN.SidUser.SID);
// integrity:
fSidIntegrity =
(va = pva[pt->c + i]) &&
VmmRead2(H, pObSystemProcess, va, SidIntegrity.pb, SECURITY_MAX_SID_SIZE, VMM_FLAG_FORCECACHE_READ) &&
IsValidSid(&SidIntegrity.SID) &&
ConvertSidToStringSidA(&SidIntegrity.SID, &szSidIntegrity);
if(fSidIntegrity) {
// https://redcanary.com/blog/process-integrity-levels/
IntegrityLevel = VMM_PROCESS_INTEGRITY_LEVEL_UNKNOWN;
if(!strcmp(szSidIntegrity, "S-1-16-16384")) { IntegrityLevel = VMM_PROCESS_INTEGRITY_LEVEL_SYSTEM; } else
if(!strcmp(szSidIntegrity, "S-1-16-0")) { IntegrityLevel = VMM_PROCESS_INTEGRITY_LEVEL_UNTRUSTED; } else
if(!strcmp(szSidIntegrity, "S-1-16-4096")) { IntegrityLevel = VMM_PROCESS_INTEGRITY_LEVEL_LOW; } else
if(!strcmp(szSidIntegrity, "S-1-16-8192")) { IntegrityLevel = VMM_PROCESS_INTEGRITY_LEVEL_MEDIUM; } else
if(!strcmp(szSidIntegrity, "S-1-16-8448")) { IntegrityLevel = VMM_PROCESS_INTEGRITY_LEVEL_MEDIUMPLUS; } else
if(!strcmp(szSidIntegrity, "S-1-16-12288")) { IntegrityLevel = VMM_PROCESS_INTEGRITY_LEVEL_HIGH; } else
if(!strcmp(szSidIntegrity, "S-1-16-20480")) { IntegrityLevel = VMM_PROCESS_INTEGRITY_LEVEL_PROTECTED; };
ppProcess[i]->win.TOKEN.IntegrityLevel = IntegrityLevel;
LocalFree(szSidIntegrity); szSidIntegrity = NULL;
}
}
// 5: finish up:
for(i = 0; i < pt->c; i++) {
// 5.1: user
if(!ppProcess[i]) { continue; }
ppProcess[i]->win.TOKEN.fSidUserValid =
ppProcess[i]->win.TOKEN.fSidUserValid &&
ConvertSidToStringSidA(&ppProcess[i]->win.TOKEN.SidUser.SID, &ppProcess[i]->win.TOKEN.szSID) &&
(ppProcess[i]->win.TOKEN.dwHashSID = CharUtil_Hash32A(ppProcess[i]->win.TOKEN.szSID, FALSE));
ppProcess[i]->win.TOKEN.fInitialized = TRUE;
}
fail:
LocalFree(pva);
LocalFree(ppProcess);
Ob_DECREF(pObSystemProcess);
}
/*
* Global Synchronization/Lock of VmmProcess_TokenTryEnsure()
* -- H
* -- pt
* -- pProcess
*/
VOID VmmProcess_TokenTryEnsureLock(_In_ VMM_HANDLE H, _In_ PVMMOB_PROCESS_TABLE pt, _In_ PVMM_PROCESS pProcess)
{
if(pProcess->win.TOKEN.fInitialized) { return; }
EnterCriticalSection(&H->vmm.LockMaster);
if(!pProcess->win.TOKEN.fInitialized) {
VmmProcess_TokenTryEnsure(H, pt);
}
LeaveCriticalSection(&H->vmm.LockMaster);
}
/*
* Retrieve a process for a given PID and optional PVMMOB_PROCESS_TABLE.
* CALLER DECREF: return
* -- H
* -- pt
* -- dwPID
* -- flags = 0 (recommended) or VMM_FLAG_PROCESS_TOKEN.
* -- return
*/
PVMM_PROCESS VmmProcessGetEx(_In_ VMM_HANDLE H, _In_opt_ PVMMOB_PROCESS_TABLE pt, _In_ DWORD dwPID, _In_ QWORD flags)
{
BOOL fToken = ((flags | H->vmm.flags) & VMM_FLAG_PROCESS_TOKEN);
PVMM_PROCESS pObProcess, pObProcessClone;
PVMMOB_PROCESS_TABLE pObTable;
DWORD i, iStart;
if(!pt) {
pObTable = (PVMMOB_PROCESS_TABLE)ObContainer_GetOb(H->vmm.pObCPROC);
pObProcess = VmmProcessGetEx(H, pObTable, dwPID, flags);
Ob_DECREF(pObTable);
return pObProcess;
}
i = iStart = dwPID % VMM_PROCESSTABLE_ENTRIES_MAX;
while(TRUE) {
if(!pt->_M[i]) { goto fail; }
if(pt->_M[i]->dwPID == dwPID) {
pObProcess = (PVMM_PROCESS)Ob_INCREF(pt->_M[i]);
if(pObProcess && fToken && !pObProcess->win.TOKEN.fInitialized) { VmmProcess_TokenTryEnsureLock(H, pt, pObProcess); }
return pObProcess;
}
if(++i == VMM_PROCESSTABLE_ENTRIES_MAX) { i = 0; }
if(i == iStart) { goto fail; }
}
fail:
if(dwPID & VMM_PID_PROCESS_CLONE_WITH_KERNELMEMORY) {
if((pObProcess = VmmProcessGetEx(H, pt, dwPID & ~VMM_PID_PROCESS_CLONE_WITH_KERNELMEMORY, flags))) {
if((pObProcessClone = VmmProcessClone(H, pObProcess))) {
pObProcessClone->fUserOnly = FALSE;
}
Ob_DECREF(pObProcess);
return pObProcessClone;
}
}
return NULL;
}
/*
* Retrieve a process for a given PID.
* CALLER DECREF: return
* -- H
* -- dwPID
* -- return = a process struct, or NULL if not found.
*/
inline PVMM_PROCESS VmmProcessGet(_In_ VMM_HANDLE H, _In_ DWORD dwPID)
{
return VmmProcessGetEx(H, NULL, dwPID, 0);
}
/*
* Retrieve processes sorted in a map keyed by either EPROCESS or PID.
* CALLER DECREF: return
* -- H
* -- fByEPROCESS = TRUE: keyed by vaEPROCESS, FALSE: keyed by PID.
* -- flags = 0 (recommended) or VMM_FLAG_PROCESS_[TOKEN|SHOW_TERMINATED].
* -- return
*/
_Success_(return != NULL)
POB_MAP VmmProcessGetAll(_In_ VMM_HANDLE H, _In_ BOOL fByEPROCESS, _In_ QWORD flags)
{
BOOL fShowTerminated = ((flags | H->vmm.flags) & VMM_FLAG_PROCESS_SHOW_TERMINATED);
BOOL fToken = ((flags | H->vmm.flags) & VMM_FLAG_PROCESS_TOKEN);
PVMMOB_PROCESS_TABLE ptOb = NULL;
POB_MAP pmOb = NULL;
PVMM_PROCESS pProcess = NULL;
WORD iProcess = 0;
DWORD i = 0;
QWORD qwKey = 0;
if(!(pmOb = ObMap_New(H, OB_MAP_FLAGS_OBJECT_OB))) { goto fail; }
if(!(ptOb = (PVMMOB_PROCESS_TABLE)ObContainer_GetOb(H->vmm.pObCPROC))) { goto fail; }
iProcess = ptOb->_iFLink;
pProcess = ptOb->_M[iProcess];
while(pProcess) {
if(!pProcess->dwState || fShowTerminated) {
if(pProcess && fToken && !pProcess->win.TOKEN.fInitialized) { VmmProcess_TokenTryEnsureLock(H, ptOb, pProcess); }
qwKey = fByEPROCESS ? pProcess->win.EPROCESS.va : pProcess->dwPID;
ObMap_Push(pmOb, qwKey, pProcess);
i++;
}
iProcess = ptOb->_iFLinkM[iProcess];
pProcess = ptOb->_M[iProcess];
if(!pProcess || (iProcess == ptOb->_iFLink)) { break; }
}
Ob_INCREF(pmOb);
fail:
Ob_DECREF(ptOb);
return Ob_DECREF(pmOb);
}
/*
* Retrieve the next process given a process and a process table. This may be
* useful when iterating over a process list. NB! Listing of next item may fail
* prematurely if the previous process is terminated while having a reference
* to it.
* FUNCTION DECREF: pProcess
* CALLER DECREF: return
* -- H
* -- pt
* -- pProcess = a process struct, or NULL if first.
* NB! function DECREF's pProcess and must not be used after call!
* -- flags = 0 (recommended) or VMM_FLAG_PROCESS_[TOKEN|SHOW_TERMINATED].
* -- return = a process struct, or NULL if not found.
*/
PVMM_PROCESS VmmProcessGetNextEx(_In_ VMM_HANDLE H, _In_opt_ PVMMOB_PROCESS_TABLE pt, _In_opt_ PVMM_PROCESS pProcess, _In_ QWORD flags)
{
BOOL fToken = ((flags | H->vmm.flags) & VMM_FLAG_PROCESS_TOKEN);
BOOL fShowTerminated = ((flags | H->vmm.flags) & VMM_FLAG_PROCESS_SHOW_TERMINATED);
PVMM_PROCESS pProcessNew;
DWORD i, iStart;
if(!pt) {
pt = (PVMMOB_PROCESS_TABLE)ObContainer_GetOb(H->vmm.pObCPROC);
if(!pt) { goto fail; }
pProcessNew = VmmProcessGetNextEx(H, pt, pProcess, flags);
Ob_DECREF(pt);
return pProcessNew;
}
restart:
if(!pProcess) {
i = pt->_iFLink;
if(!pt->_M[i]) { goto fail; }
pProcessNew = (PVMM_PROCESS)Ob_INCREF(pt->_M[i]);
Ob_DECREF(pProcess);
pProcess = pProcessNew;
if(pProcess && pProcess->dwState && !fShowTerminated) { goto restart; }
if(pProcess && fToken && !pProcess->win.TOKEN.fInitialized) { VmmProcess_TokenTryEnsureLock(H, pt, pProcess); }
return pProcess;
}
i = iStart = pProcess->dwPID % VMM_PROCESSTABLE_ENTRIES_MAX;
while(TRUE) {
if(!pt->_M[i]) { goto fail; }
if(pt->_M[i]->dwPID == pProcess->dwPID) {
// current process -> retrieve next!
i = pt->_iFLinkM[i];
if(!pt->_M[i]) { goto fail; }
pProcessNew = (PVMM_PROCESS)Ob_INCREF(pt->_M[i]);
Ob_DECREF(pProcess);
pProcess = pProcessNew;
if(pProcess && pProcess->dwState && !fShowTerminated) { goto restart; }
if(pProcess && fToken && !pProcess->win.TOKEN.fInitialized) { VmmProcess_TokenTryEnsureLock(H, pt, pProcess); }
return pProcess;
}
if(++i == VMM_PROCESSTABLE_ENTRIES_MAX) { i = 0; }
if(i == iStart) { goto fail; }
}
fail:
Ob_DECREF(pProcess);
return NULL;
}
/*
* Retrieve the next process given a process. This may be useful when iterating
* over a process list. NB! Listing of next item may fail prematurely if the
* previous process is terminated while having a reference to it.
* FUNCTION DECREF: pProcess
* CALLER DECREF: return
* -- H
* -- pProcess = a process struct, or NULL if first.
* NB! function DECREF's pProcess and must not be used after call!
* -- flags = 0 (recommended) or VMM_FLAG_PROCESS_[TOKEN|SHOW_TERMINATED]
* -- return = a process struct, or NULL if not found.
*/
PVMM_PROCESS VmmProcessGetNext(_In_ VMM_HANDLE H, _In_opt_ PVMM_PROCESS pProcess, _In_ QWORD flags)
{
return VmmProcessGetNextEx(H, NULL, pProcess, flags);
}
/*
* Object manager callback before 'static process' object cleanup
* decrease refcount of any internal objects.
*/
VOID VmmProcessStatic_CloseObCallback(_In_ PVOID pVmmOb)
{
PVMMOB_PROCESS_PERSISTENT pProcessStatic = (PVMMOB_PROCESS_PERSISTENT)pVmmOb;
Ob_DECREF_NULL(&pProcessStatic->pObCMapVadPrefetch);
Ob_DECREF_NULL(&pProcessStatic->pObCLdrModulesPrefetch32);
Ob_DECREF_NULL(&pProcessStatic->pObCLdrModulesPrefetch64);
Ob_DECREF_NULL(&pProcessStatic->pObCLdrModulesInjected);
Ob_DECREF_NULL(&pProcessStatic->pObCMapThreadPrefetch);
LocalFree(pProcessStatic->uszPathKernel);
LocalFree(pProcessStatic->UserProcessParams.uszCommandLine);
LocalFree(pProcessStatic->UserProcessParams.uszImagePathName);
LocalFree(pProcessStatic->UserProcessParams.uszWindowTitle);
LocalFree(pProcessStatic->UserProcessParams.uszEnvironment);
}
/*
* Initialize a new static context that will remain between process refreshes.
* This should only be done at first process initialization of that PID.
*/
_Success_(return)
BOOL VmmProcessStatic_Initialize(_In_ VMM_HANDLE H, _In_ PVMM_PROCESS pProcess)
{
if(pProcess->pObPersistent) { return FALSE; }
if(!(pProcess->pObPersistent = Ob_AllocEx(H, OB_TAG_VMM_PROCESS_PERSISTENT, LMEM_ZEROINIT, sizeof(VMMOB_PROCESS_PERSISTENT), VmmProcessStatic_CloseObCallback, NULL))) { goto fail; }
if(!(pProcess->pObPersistent->pObCMapVadPrefetch = ObContainer_New())) { goto fail; }
if(!(pProcess->pObPersistent->pObCLdrModulesPrefetch32 = ObContainer_New())) { goto fail; }
if(!(pProcess->pObPersistent->pObCLdrModulesPrefetch64 = ObContainer_New())) { goto fail; }
if(!(pProcess->pObPersistent->pObCLdrModulesInjected = ObContainer_New())) { goto fail; }
if(!(pProcess->pObPersistent->pObCMapThreadPrefetch = ObContainer_New())) { goto fail; }
return TRUE;
fail:
Ob_DECREF_NULL(&pProcess->pObPersistent);
return FALSE;
}
/*
* Object manager callback before 'process' object cleanup - decrease refcount
* of any internal 'memory map' and 'module map' objects.
*/
VOID VmmProcess_CloseObCallback(_In_ PVOID pVmmOb)
{
PVMM_PROCESS pProcess = (PVMM_PROCESS)pVmmOb;