forked from winsiderss/systeminformer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.c
More file actions
1291 lines (1120 loc) · 38.5 KB
/
Copy pathobject.c
File metadata and controls
1291 lines (1120 loc) · 38.5 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
/*
* KProcessHacker
*
* Copyright (C) 2010-2016 wj32
*
* This file is part of Process Hacker.
*
* Process Hacker 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.
*
* Process Hacker 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 Process Hacker. If not, see <http://www.gnu.org/licenses/>.
*/
#include <kph.h>
#include <dyndata.h>
#ifdef _X86_
#define KERNEL_HANDLE_BIT (0x80000000)
#else
#define KERNEL_HANDLE_BIT (0xffffffff80000000)
#endif
#define IsKernelHandle(Handle) ((LONG_PTR)(Handle) < 0)
#define MakeKernelHandle(Handle) ((HANDLE)((ULONG_PTR)(Handle) | KERNEL_HANDLE_BIT))
typedef struct _KPHP_ENUMERATE_PROCESS_HANDLES_CONTEXT
{
PVOID Buffer;
PVOID BufferLimit;
PVOID CurrentEntry;
ULONG Count;
NTSTATUS Status;
} KPHP_ENUMERATE_PROCESS_HANDLES_CONTEXT, *PKPHP_ENUMERATE_PROCESS_HANDLES_CONTEXT;
BOOLEAN KphpEnumerateProcessHandlesEnumCallback61(
_Inout_ PHANDLE_TABLE_ENTRY HandleTableEntry,
_In_ HANDLE Handle,
_In_ PVOID Context
);
BOOLEAN KphpEnumerateProcessHandlesEnumCallback(
_In_ PHANDLE_TABLE HandleTable,
_Inout_ PHANDLE_TABLE_ENTRY HandleTableEntry,
_In_ HANDLE Handle,
_In_ PVOID Context
);
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, KphReferenceProcessHandleTable)
#pragma alloc_text(PAGE, KphDereferenceProcessHandleTable)
#pragma alloc_text(PAGE, KphUnlockHandleTableEntry)
#pragma alloc_text(PAGE, KphpEnumerateProcessHandlesEnumCallback61)
#pragma alloc_text(PAGE, KphpEnumerateProcessHandlesEnumCallback)
#pragma alloc_text(PAGE, KpiEnumerateProcessHandles)
#pragma alloc_text(PAGE, KphQueryNameObject)
#pragma alloc_text(PAGE, KphQueryNameFileObject)
#pragma alloc_text(PAGE, KpiQueryInformationObject)
#pragma alloc_text(PAGE, KpiSetInformationObject)
#pragma alloc_text(PAGE, KphOpenNamedObject)
#endif
/**
* Gets a pointer to the handle table of a process.
*
* \param Process A process object.
*
* \return A pointer to the handle table, or NULL if the process is terminating or the request is
* not supported. You must call KphDereferenceProcessHandleTable() when the handle table is no
* longer needed.
*/
PHANDLE_TABLE KphReferenceProcessHandleTable(
_In_ PEPROCESS Process
)
{
PHANDLE_TABLE handleTable = NULL;
PAGED_CODE();
// Fail if we don't have an offset.
if (KphDynEpObjectTable == ULONG_MAX)
return NULL;
// Prevent the process from terminating and get its handle table.
if (NT_SUCCESS(PsAcquireProcessExitSynchronization(Process)))
{
handleTable = *(PHANDLE_TABLE *)PTR_ADD_OFFSET(Process, KphDynEpObjectTable);
if (!handleTable)
PsReleaseProcessExitSynchronization(Process);
}
return handleTable;
}
/**
* Dereferences the handle table of a process.
*
* \param Process A process object.
*/
VOID KphDereferenceProcessHandleTable(
_In_ PEPROCESS Process
)
{
PAGED_CODE();
PsReleaseProcessExitSynchronization(Process);
}
VOID KphUnlockHandleTableEntry(
_In_ PHANDLE_TABLE HandleTable,
_In_ PHANDLE_TABLE_ENTRY HandleTableEntry
)
{
PEX_PUSH_LOCK handleContentionEvent;
PAGED_CODE();
// Set the unlocked bit.
#if (defined _M_X64) || (defined _M_ARM64)
InterlockedExchangeAdd64(&HandleTableEntry->Value, 1);
#else
InterlockedExchangeAdd(&HandleTableEntry->Value, 1);
#endif
// Allow waiters to wake up.
handleContentionEvent = (PEX_PUSH_LOCK)PTR_ADD_OFFSET(HandleTable, KphDynHtHandleContentionEvent);
if (*(PULONG_PTR)handleContentionEvent != 0)
ExfUnblockPushLock(handleContentionEvent, NULL);
}
BOOLEAN KphpEnumerateProcessHandlesEnumCallback61(
_Inout_ PHANDLE_TABLE_ENTRY HandleTableEntry,
_In_ HANDLE Handle,
_In_ PVOID Context
)
{
PKPHP_ENUMERATE_PROCESS_HANDLES_CONTEXT context = Context;
KPH_PROCESS_HANDLE handleInfo;
POBJECT_HEADER objectHeader;
POBJECT_TYPE objectType;
PKPH_PROCESS_HANDLE entryInBuffer;
PAGED_CODE();
objectHeader = ObpDecodeObject(HandleTableEntry->Object);
handleInfo.Handle = Handle;
handleInfo.Object = objectHeader ? &objectHeader->Body : NULL;
handleInfo.GrantedAccess = ObpDecodeGrantedAccess(HandleTableEntry->GrantedAccess);
handleInfo.ObjectTypeIndex = USHORT_MAX;
handleInfo.Reserved1 = 0;
handleInfo.HandleAttributes = ObpGetHandleAttributes(HandleTableEntry);
handleInfo.Reserved2 = 0;
if (handleInfo.Object)
{
objectType = ObGetObjectType(handleInfo.Object);
if (objectType && KphDynOtIndex != ULONG_MAX)
handleInfo.ObjectTypeIndex = (USHORT)*(PUCHAR)PTR_ADD_OFFSET(objectType, KphDynOtIndex);
}
// Advance the current entry pointer regardless of whether the information will be written; this
// will allow the parent function to report the correct return length.
entryInBuffer = context->CurrentEntry;
context->CurrentEntry = PTR_ADD_OFFSET(context->CurrentEntry, sizeof(KPH_PROCESS_HANDLE));
context->Count++;
// Only write if we have not exceeded the buffer length. Also check for a potential overflow (if
// the process has an extremely large number of handles, the buffer pointer may wrap).
if (
(ULONG_PTR)entryInBuffer >= (ULONG_PTR)context->Buffer &&
(ULONG_PTR)entryInBuffer + sizeof(KPH_PROCESS_HANDLE) <= (ULONG_PTR)context->BufferLimit
)
{
__try
{
*entryInBuffer = handleInfo;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
// Report an error.
if (context->Status == STATUS_SUCCESS)
context->Status = GetExceptionCode();
}
}
else
{
// Report that the buffer is too small.
if (context->Status == STATUS_SUCCESS)
context->Status = STATUS_BUFFER_TOO_SMALL;
}
return FALSE;
}
BOOLEAN KphpEnumerateProcessHandlesEnumCallback(
_In_ PHANDLE_TABLE HandleTable,
_Inout_ PHANDLE_TABLE_ENTRY HandleTableEntry,
_In_ HANDLE Handle,
_In_ PVOID Context
)
{
BOOLEAN result;
PAGED_CODE();
result = KphpEnumerateProcessHandlesEnumCallback61(HandleTableEntry, Handle, Context);
KphUnlockHandleTableEntry(HandleTable, HandleTableEntry);
return result;
}
/**
* Enumerates the handles of a process.
*
* \param ProcessHandle A handle to a process.
* \param Buffer The buffer in which the handle information will be stored.
* \param BufferLength The number of bytes available in \a Buffer.
* \param ReturnLength A variable which receives the number of bytes required to be available in
* \a Buffer.
* \param AccessMode The mode in which to perform access checks.
*/
NTSTATUS KpiEnumerateProcessHandles(
_In_ HANDLE ProcessHandle,
_Out_writes_bytes_(BufferLength) PVOID Buffer,
_In_opt_ ULONG BufferLength,
_Out_opt_ PULONG ReturnLength,
_In_ KPROCESSOR_MODE AccessMode
)
{
NTSTATUS status;
BOOLEAN result;
PEPROCESS process;
PHANDLE_TABLE handleTable;
KPHP_ENUMERATE_PROCESS_HANDLES_CONTEXT context;
PAGED_CODE();
if (KphDynNtVersion >= PHNT_WIN8 && KphDynHtHandleContentionEvent == ULONG_MAX)
{
return STATUS_NOT_SUPPORTED;
}
if (AccessMode != KernelMode)
{
__try
{
ProbeForWrite(Buffer, BufferLength, sizeof(ULONG));
if (ReturnLength)
ProbeForWrite(ReturnLength, sizeof(ULONG), sizeof(ULONG));
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
return GetExceptionCode();
}
}
// Reference the process object.
status = ObReferenceObjectByHandle(
ProcessHandle,
0,
*PsProcessType,
AccessMode,
&process,
NULL
);
if (!NT_SUCCESS(status))
return status;
// Get its handle table.
handleTable = KphReferenceProcessHandleTable(process);
if (!handleTable)
{
ObDereferenceObject(process);
return STATUS_UNSUCCESSFUL;
}
// Initialize the enumeration context.
context.Buffer = Buffer;
context.BufferLimit = PTR_ADD_OFFSET(Buffer, BufferLength);
context.CurrentEntry = ((PKPH_PROCESS_HANDLE_INFORMATION)Buffer)->Handles;
context.Count = 0;
context.Status = STATUS_SUCCESS;
// Enumerate the handles.
if (KphDynNtVersion >= PHNT_WIN8)
{
result = ExEnumHandleTable(
handleTable,
KphpEnumerateProcessHandlesEnumCallback,
&context,
NULL
);
}
else
{
result = ExEnumHandleTable(
handleTable,
(PEX_ENUM_HANDLE_CALLBACK)KphpEnumerateProcessHandlesEnumCallback61,
&context,
NULL
);
}
KphDereferenceProcessHandleTable(process);
ObDereferenceObject(process);
// Write the number of handles if we can.
if (BufferLength >= UFIELD_OFFSET(KPH_PROCESS_HANDLE_INFORMATION, Handles))
{
if (AccessMode != KernelMode)
{
__try
{
((PKPH_PROCESS_HANDLE_INFORMATION)Buffer)->HandleCount = context.Count;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
return GetExceptionCode();
}
}
else
{
((PKPH_PROCESS_HANDLE_INFORMATION)Buffer)->HandleCount = context.Count;
}
}
// Supply the return length if the caller wanted it.
if (ReturnLength)
{
ULONG returnLength;
// Note: if the CurrentEntry pointer wrapped, this will give the wrong return length.
returnLength = (ULONG)((ULONG_PTR)context.CurrentEntry - (ULONG_PTR)Buffer);
if (AccessMode != KernelMode)
{
__try
{
*ReturnLength = returnLength;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
return GetExceptionCode();
}
}
else
{
*ReturnLength = returnLength;
}
}
return context.Status;
}
/**
* Queries the name of an object.
*
* \param Object A pointer to an object.
* \param Buffer The buffer in which the object name will be stored.
* \param BufferLength The number of bytes available in \a Buffer.
* \param ReturnLength A variable which receives the number of bytes required to be available in
* \a Buffer.
*/
NTSTATUS KphQueryNameObject(
_In_ PVOID Object,
_Out_writes_bytes_(BufferLength) POBJECT_NAME_INFORMATION Buffer,
_In_ ULONG BufferLength,
_Out_ PULONG ReturnLength
)
{
NTSTATUS status;
POBJECT_TYPE objectType;
PAGED_CODE();
objectType = ObGetObjectType(Object);
// Check if we are going to hang when querying the object, and use
// the special file object query function if needed.
if (objectType == *IoFileObjectType &&
(((PFILE_OBJECT)Object)->Busy || ((PFILE_OBJECT)Object)->Waiters))
{
status = KphQueryNameFileObject(Object, Buffer, BufferLength, ReturnLength);
dprintf("KphQueryNameFileObject: status 0x%x\n", status);
}
else
{
status = ObQueryNameString(Object, Buffer, BufferLength, ReturnLength);
dprintf("ObQueryNameString: status 0x%x\n", status);
}
// Make the error returns consistent.
if (status == STATUS_BUFFER_OVERFLOW) // returned by I/O subsystem
status = STATUS_BUFFER_TOO_SMALL;
if (status == STATUS_INFO_LENGTH_MISMATCH) // returned by ObQueryNameString
status = STATUS_BUFFER_TOO_SMALL;
if (NT_SUCCESS(status))
dprintf("KphQueryNameObject: %.*S\n", Buffer->Name.Length / (USHORT)sizeof(WCHAR), Buffer->Name.Buffer);
else
dprintf("KphQueryNameObject: status 0x%x\n", status);
return status;
}
/**
* Queries the name of a file object.
*
* \param FileObject A pointer to a file object.
* \param Buffer The buffer in which the object name will be stored.
* \param BufferLength The number of bytes available in \a Buffer.
* \param ReturnLength A variable which receives the number of bytes required to be available in
* \a Buffer.
*/
NTSTATUS KphQueryNameFileObject(
_In_ PFILE_OBJECT FileObject,
_Out_writes_bytes_(BufferLength) POBJECT_NAME_INFORMATION Buffer,
_In_ ULONG BufferLength,
_Out_ PULONG ReturnLength
)
{
NTSTATUS status = STATUS_SUCCESS;
ULONG returnLength;
PCHAR objectName;
ULONG usedLength;
ULONG subNameLength;
PFILE_OBJECT relatedFileObject;
PAGED_CODE();
// We need at least the size of OBJECT_NAME_INFORMATION to continue.
if (BufferLength < sizeof(OBJECT_NAME_INFORMATION))
{
*ReturnLength = sizeof(OBJECT_NAME_INFORMATION);
return STATUS_BUFFER_TOO_SMALL;
}
// Assume failure.
Buffer->Name.Length = 0;
// We will place the object name directly after the UNICODE_STRING structure in the buffer.
Buffer->Name.Buffer = (PWSTR)PTR_ADD_OFFSET(Buffer, sizeof(OBJECT_NAME_INFORMATION));
// Retain a local pointer to the object name so we can manipulate the pointer.
objectName = (PCHAR)Buffer->Name.Buffer;
// A variable that keeps track of how much space we have used.
usedLength = sizeof(OBJECT_NAME_INFORMATION);
// Check if the file object has an associated device (e.g. "\Device\NamedPipe", "\Device\Mup").
// We can use the user-supplied buffer for this since if the buffer isn't big enough, we can't
// proceed anyway (we are going to use the name).
if (FileObject->DeviceObject)
{
status = ObQueryNameString(
FileObject->DeviceObject,
Buffer,
BufferLength,
&returnLength
);
if (!NT_SUCCESS(status))
{
if (status == STATUS_INFO_LENGTH_MISMATCH)
status = STATUS_BUFFER_TOO_SMALL;
*ReturnLength = returnLength;
return status;
}
// The UNICODE_STRING in the buffer is now filled in. We will append to the object name
// later, so we need to fix the object name pointer by adding the length, in bytes, of the
// device name string we just got.
objectName += Buffer->Name.Length;
usedLength += Buffer->Name.Length;
}
// Check if the file object has a file name component. If not, we can't do anything else, so we
// just return the name we have already.
if (!FileObject->FileName.Buffer)
{
*ReturnLength = usedLength;
return STATUS_SUCCESS;
}
// The file object has a name. We need to walk up the file object chain and append the names of
// the related file objects in reverse order. This means we need to calculate the total length
// first.
relatedFileObject = FileObject;
subNameLength = 0;
do
{
subNameLength += relatedFileObject->FileName.Length;
// Avoid infinite loops.
if (relatedFileObject == relatedFileObject->RelatedFileObject)
break;
relatedFileObject = relatedFileObject->RelatedFileObject;
} while (relatedFileObject);
usedLength += subNameLength;
// Check if we have enough space to write the whole thing.
if (usedLength > BufferLength)
{
*ReturnLength = usedLength;
return STATUS_BUFFER_TOO_SMALL;
}
// We're ready to begin copying the names.
// Add the name length because we're copying in reverse order.
objectName += subNameLength;
relatedFileObject = FileObject;
do
{
objectName -= relatedFileObject->FileName.Length;
memcpy(objectName, relatedFileObject->FileName.Buffer, relatedFileObject->FileName.Length);
// Avoid infinite loops.
if (relatedFileObject == relatedFileObject->RelatedFileObject)
break;
relatedFileObject = relatedFileObject->RelatedFileObject;
} while (relatedFileObject);
// Update the length.
Buffer->Name.Length += (USHORT)subNameLength;
// Pass the return length back.
*ReturnLength = usedLength;
return STATUS_SUCCESS;
}
/**
* Queries object information.
*
* \param ProcessHandle A handle to a process.
* \param Handle A handle which is present in the process referenced by \a ProcessHandle.
* \param ObjectInformationClass The type of information to retrieve.
* \param ObjectInformation The buffer in which the information will be stored.
* \param ObjectInformationLength The number of bytes available in \a ObjectInformation.
* \param ReturnLength A variable which receives the number of bytes required to be available in
* \a ObjectInformation.
* \param AccessMode The mode in which to perform access checks.
*/
NTSTATUS KpiQueryInformationObject(
_In_ HANDLE ProcessHandle,
_In_ HANDLE Handle,
_In_ KPH_OBJECT_INFORMATION_CLASS ObjectInformationClass,
_Out_writes_bytes_(ObjectInformationLength) PVOID ObjectInformation,
_In_ ULONG ObjectInformationLength,
_Out_opt_ PULONG ReturnLength,
_In_ KPROCESSOR_MODE AccessMode
)
{
NTSTATUS status;
PEPROCESS process;
KPROCESSOR_MODE referenceMode;
KAPC_STATE apcState;
ULONG returnLength;
PAGED_CODE();
if (AccessMode != KernelMode)
{
__try
{
ProbeForWrite(ObjectInformation, ObjectInformationLength, sizeof(ULONG));
if (ReturnLength)
ProbeForWrite(ReturnLength, sizeof(ULONG), sizeof(ULONG));
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
return GetExceptionCode();
}
}
status = ObReferenceObjectByHandle(
ProcessHandle,
0,
*PsProcessType,
AccessMode,
&process,
NULL
);
if (!NT_SUCCESS(status))
return status;
if (process == PsInitialSystemProcess)
{
// A check was added in Windows 7 - if we're attached to the System process, the handle must
// be a kernel handle.
Handle = MakeKernelHandle(Handle);
referenceMode = KernelMode;
}
else
{
// Make sure the handle isn't a kernel handle if we're not attached to the System process.
// This means we can avoid referencing then opening the objects later when calling
// ZwQueryObject, etc.
if (IsKernelHandle(Handle))
{
ObDereferenceObject(process);
return STATUS_INVALID_HANDLE;
}
referenceMode = AccessMode;
}
switch (ObjectInformationClass)
{
case KphObjectBasicInformation:
{
OBJECT_BASIC_INFORMATION basicInfo;
KeStackAttachProcess(process, &apcState);
status = ZwQueryObject(
Handle,
ObjectBasicInformation,
&basicInfo,
sizeof(OBJECT_BASIC_INFORMATION),
NULL
);
KeUnstackDetachProcess(&apcState);
if (NT_SUCCESS(status))
{
if (ObjectInformationLength == sizeof(OBJECT_BASIC_INFORMATION))
{
__try
{
*(POBJECT_BASIC_INFORMATION)ObjectInformation = basicInfo;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
status = GetExceptionCode();
}
}
else
{
status = STATUS_INFO_LENGTH_MISMATCH;
}
}
returnLength = sizeof(OBJECT_BASIC_INFORMATION);
}
break;
case KphObjectNameInformation:
{
PVOID object;
ULONG allocateSize;
POBJECT_NAME_INFORMATION nameInfo;
returnLength = sizeof(OBJECT_NAME_INFORMATION);
// Attach to the process a get a pointer to the object.
KeStackAttachProcess(process, &apcState);
status = ObReferenceObjectByHandle(
Handle,
0,
NULL,
referenceMode,
&object,
NULL
);
KeUnstackDetachProcess(&apcState);
if (NT_SUCCESS(status))
{
allocateSize = ObjectInformationLength;
if (allocateSize < sizeof(OBJECT_NAME_INFORMATION)) // make sure we never try to allocate 0 bytes
allocateSize = sizeof(OBJECT_NAME_INFORMATION);
nameInfo = ExAllocatePoolQuotaZero(PagedPool, allocateSize, 'QhpK');
if (nameInfo)
{
status = KphQueryNameObject(
object,
nameInfo,
ObjectInformationLength,
&returnLength
);
dprintf("KpiQueryInformationObject: called KphQueryNameObject: Handle: 0x%Ix, ObjectInformationLength: %u, returnLength: %u\n",
(ULONG_PTR)Handle, ObjectInformationLength, returnLength);
if (NT_SUCCESS(status))
{
// Fix up the buffer pointer.
if (nameInfo->Name.Buffer)
nameInfo->Name.Buffer = (PVOID)((ULONG_PTR)nameInfo->Name.Buffer - (ULONG_PTR)nameInfo + (ULONG_PTR)ObjectInformation);
__try
{
memcpy(ObjectInformation, nameInfo, ObjectInformationLength);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
status = GetExceptionCode();
}
}
ExFreePoolWithTag(nameInfo, 'QhpK');
}
else
{
status = STATUS_INSUFFICIENT_RESOURCES;
}
ObDereferenceObject(object);
}
}
break;
case KphObjectTypeInformation:
{
ULONG allocateSize;
POBJECT_TYPE_INFORMATION typeInfo;
returnLength = sizeof(OBJECT_TYPE_INFORMATION);
allocateSize = ObjectInformationLength;
if (allocateSize < sizeof(OBJECT_TYPE_INFORMATION))
allocateSize = sizeof(OBJECT_TYPE_INFORMATION);
// ObQueryTypeInfo uses ObjectType->Name.MaximumLength instead of
// ObjectType->Name.Length + sizeof(WCHAR) to calculate the required buffer size. In
// Windows 8, certain object types (e.g. TmTx) do NOT include the null terminator in
// MaximumLength, which causes ObQueryTypeInfo to overrun the given buffer. To work
// around this bug, we add some (generous) padding to our allocation.
allocateSize += sizeof(ULONGLONG);
typeInfo = ExAllocatePoolQuotaZero(PagedPool, allocateSize, 'QhpK');
if (typeInfo)
{
KeStackAttachProcess(process, &apcState);
status = ZwQueryObject(
Handle,
ObjectTypeInformation,
typeInfo,
ObjectInformationLength,
&returnLength
);
KeUnstackDetachProcess(&apcState);
if (NT_SUCCESS(status))
{
// Fix up the buffer pointer.
if (typeInfo->TypeName.Buffer)
typeInfo->TypeName.Buffer = (PVOID)((ULONG_PTR)typeInfo->TypeName.Buffer - (ULONG_PTR)typeInfo + (ULONG_PTR)ObjectInformation);
__try
{
memcpy(ObjectInformation, typeInfo, ObjectInformationLength);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
status = GetExceptionCode();
}
}
ExFreePoolWithTag(typeInfo, 'QhpK');
}
else
{
status = STATUS_INSUFFICIENT_RESOURCES;
}
}
break;
case KphObjectHandleFlagInformation:
{
OBJECT_HANDLE_FLAG_INFORMATION handleFlagInfo;
KeStackAttachProcess(process, &apcState);
status = ZwQueryObject(
Handle,
ObjectHandleFlagInformation,
&handleFlagInfo,
sizeof(OBJECT_HANDLE_FLAG_INFORMATION),
NULL
);
KeUnstackDetachProcess(&apcState);
if (NT_SUCCESS(status))
{
if (ObjectInformationLength == sizeof(OBJECT_HANDLE_FLAG_INFORMATION))
{
__try
{
*(POBJECT_HANDLE_FLAG_INFORMATION)ObjectInformation = handleFlagInfo;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
status = GetExceptionCode();
}
}
else
{
status = STATUS_INFO_LENGTH_MISMATCH;
}
}
returnLength = sizeof(OBJECT_HANDLE_FLAG_INFORMATION);
}
break;
case KphObjectProcessBasicInformation:
{
PROCESS_BASIC_INFORMATION basicInfo;
KeStackAttachProcess(process, &apcState);
status = ZwQueryInformationProcess(
Handle,
ProcessBasicInformation,
&basicInfo,
sizeof(PROCESS_BASIC_INFORMATION),
NULL
);
KeUnstackDetachProcess(&apcState);
if (NT_SUCCESS(status))
{
if (ObjectInformationLength == sizeof(PROCESS_BASIC_INFORMATION))
{
__try
{
*(PPROCESS_BASIC_INFORMATION)ObjectInformation = basicInfo;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
status = GetExceptionCode();
}
}
else
{
status = STATUS_INFO_LENGTH_MISMATCH;
}
}
returnLength = sizeof(PROCESS_BASIC_INFORMATION);
}
break;
case KphObjectThreadBasicInformation:
{
THREAD_BASIC_INFORMATION basicInfo;
KeStackAttachProcess(process, &apcState);
status = ZwQueryInformationThread(
Handle,
ThreadBasicInformation,
&basicInfo,
sizeof(THREAD_BASIC_INFORMATION),
NULL
);
KeUnstackDetachProcess(&apcState);
if (NT_SUCCESS(status))
{
if (ObjectInformationLength == sizeof(THREAD_BASIC_INFORMATION))
{
__try
{
*(PTHREAD_BASIC_INFORMATION)ObjectInformation = basicInfo;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
status = GetExceptionCode();
}
}
else
{
status = STATUS_INFO_LENGTH_MISMATCH;
}
}
returnLength = sizeof(THREAD_BASIC_INFORMATION);
}
break;
case KphObjectEtwRegBasicInformation:
{
PVOID etwReg;
PVOID objectType;
PUNICODE_STRING objectTypeName;
UNICODE_STRING etwRegistrationName;
PVOID guidEntry;
ETWREG_BASIC_INFORMATION basicInfo;
// Check dynamic data requirements.
if (KphDynEgeGuid != ULONG_MAX &&
KphDynEreGuidEntry != ULONG_MAX &&
KphDynOtName != ULONG_MAX)
{
// Attach to the process and get a pointer to the object. We don't have a pointer to
// the EtwRegistration object type, so we'll just have to check the type name.
KeStackAttachProcess(process, &apcState);
status = ObReferenceObjectByHandle(
Handle,
0,
NULL,
referenceMode,
&etwReg,
NULL
);
KeUnstackDetachProcess(&apcState);
if (NT_SUCCESS(status))
{
// Check the type name.
objectType = ObGetObjectType(etwReg);
if (objectType)
{
objectTypeName = (PUNICODE_STRING)PTR_ADD_OFFSET(objectType, KphDynOtName);
RtlInitUnicodeString(&etwRegistrationName, L"EtwRegistration");
if (!RtlEqualUnicodeString(objectTypeName, &etwRegistrationName, FALSE))
{
status = STATUS_OBJECT_TYPE_MISMATCH;
}
}
else
{
status = STATUS_NOT_SUPPORTED;
}
if (NT_SUCCESS(status))
{
guidEntry = *(PVOID *)PTR_ADD_OFFSET(etwReg, KphDynEreGuidEntry);
if (guidEntry)
basicInfo.Guid = *(GUID *)PTR_ADD_OFFSET(guidEntry, KphDynEgeGuid);
else
memset(&basicInfo.Guid, 0, sizeof(GUID));
basicInfo.SessionId = 0; // not implemented
if (ObjectInformationLength == sizeof(ETWREG_BASIC_INFORMATION))
{
__try
{
*(PETWREG_BASIC_INFORMATION)ObjectInformation = basicInfo;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
status = GetExceptionCode();
}
}
else
{
status = STATUS_INFO_LENGTH_MISMATCH;
}
}
ObDereferenceObject(etwReg);
}
}
else
{
status = STATUS_NOT_SUPPORTED;
}
returnLength = sizeof(ETWREG_BASIC_INFORMATION);
}
break;
case KphObjectFileObjectInformation:
{
PFILE_OBJECT fileObject;
KPH_FILE_OBJECT_INFORMATION objectInfo;
KeStackAttachProcess(process, &apcState);
status = ObReferenceObjectByHandle(