-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathobquery.c
1849 lines (1264 loc) · 51.2 KB
/
obquery.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
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
You may only use this code if you agree to the terms of the Windows Research Kernel Source Code License agreement (see License.txt).
If you do not agree to the terms, do not use the code.
Module Name:
obquery.c
Abstract:
Query Object system service
--*/
#include "obp.h"
//
// Local procedure prototypes
//
//
// The following structure is used to pass the call back routine
// "ObpSetHandleAttributes" the captured object information and
// the processor mode of the caller.
//
typedef struct __OBP_SET_HANDLE_ATTRIBUTES {
OBJECT_HANDLE_FLAG_INFORMATION ObjectInformation;
KPROCESSOR_MODE PreviousMode;
} OBP_SET_HANDLE_ATTRIBUTES, *POBP_SET_HANDLE_ATTRIBUTES;
BOOLEAN
ObpSetHandleAttributes (
IN OUT PVOID TableEntry,
IN ULONG_PTR Parameter
);
#pragma alloc_text(PAGE, NtQueryObject)
#pragma alloc_text(PAGE, ObpQueryNameString)
#pragma alloc_text(PAGE, ObQueryNameString)
#pragma alloc_text(PAGE, ObQueryTypeName)
#pragma alloc_text(PAGE, ObQueryTypeInfo)
#pragma alloc_text(PAGE, ObQueryObjectAuditingByHandle)
#pragma alloc_text(PAGE, NtSetInformationObject)
#pragma alloc_text(PAGE, ObpSetHandleAttributes)
#pragma alloc_text(PAGE, ObSetHandleAttributes)
NTSTATUS
NtQueryObject (
__in HANDLE Handle,
__in OBJECT_INFORMATION_CLASS ObjectInformationClass,
__out_bcount_opt(ObjectInformationLength) PVOID ObjectInformation,
__in ULONG ObjectInformationLength,
__out_opt PULONG ReturnLength
)
/*++
Routine description:
This routine is used to query information about a given object
Arguments:
Handle - Supplies a handle to the object being queried. This value
is ignored if the requested information class is for type
information.
ObjectInformationClass - Specifies the type of information to return
ObjectInformation - Supplies an output buffer for the information being
returned
ObjectInformationLength - Specifies, in bytes, the length of the
preceding object information buffer
ReturnLength - Optionally receives the length, in bytes, used to store
the object information
Return Value:
An appropriate status value
--*/
{
KPROCESSOR_MODE PreviousMode;
NTSTATUS Status;
PVOID Object;
POBJECT_HEADER ObjectHeader;
POBJECT_HEADER_QUOTA_INFO QuotaInfo;
POBJECT_HEADER_NAME_INFO NameInfo;
POBJECT_TYPE ObjectType;
POBJECT_HEADER ObjectDirectoryHeader;
POBJECT_DIRECTORY ObjectDirectory;
ACCESS_MASK GrantedAccess;
POBJECT_HANDLE_FLAG_INFORMATION HandleFlags;
OBJECT_HANDLE_INFORMATION HandleInformation = {0};
ULONG NameInfoSize;
ULONG SecurityDescriptorSize;
ULONG TempReturnLength;
OBJECT_BASIC_INFORMATION ObjectBasicInfo;
POBJECT_TYPES_INFORMATION TypesInformation;
POBJECT_TYPE_INFORMATION TypeInfo;
ULONG i;
PAGED_CODE();
//
// Initialize our local variables
//
TempReturnLength = 0;
//
// Get previous processor mode and probe output argument if necessary.
//
PreviousMode = KeGetPreviousMode();
if (PreviousMode != KernelMode) {
try {
if (ObjectInformationClass != ObjectHandleFlagInformation) {
ProbeForWrite( ObjectInformation,
ObjectInformationLength,
sizeof( ULONG ));
} else {
ProbeForWrite( ObjectInformation,
ObjectInformationLength,
1 );
}
//
// We'll use a local temp return length variable to pass
// through to the later ob query calls which will increment
// its value. We can't pass the users return length directly
// because the user might also be altering its value behind
// our back.
//
if (ARGUMENT_PRESENT( ReturnLength )) {
ProbeForWriteUlong( ReturnLength );
}
} except( EXCEPTION_EXECUTE_HANDLER ) {
return( GetExceptionCode() );
}
}
//
// If the query is not for types information then we
// will have to get the object in question. Otherwise
// for types information there really isn't an object
// to grab.
//
if (ObjectInformationClass != ObjectTypesInformation) {
Status = ObReferenceObjectByHandle( Handle,
0,
NULL,
PreviousMode,
&Object,
&HandleInformation );
if (!NT_SUCCESS( Status )) {
return( Status );
}
GrantedAccess = HandleInformation.GrantedAccess;
ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
ObjectType = ObjectHeader->Type;
} else {
GrantedAccess = 0;
Object = NULL;
ObjectHeader = NULL;
ObjectType = NULL;
Status = STATUS_SUCCESS;
}
//
// Now process the particular information class being
// requested
//
switch( ObjectInformationClass ) {
case ObjectBasicInformation:
//
// Make sure the output buffer is long enough and then
// fill in the appropriate fields into our local copy
// of basic information.
//
if (ObjectInformationLength != sizeof( OBJECT_BASIC_INFORMATION )) {
ObDereferenceObject( Object );
return( STATUS_INFO_LENGTH_MISMATCH );
}
ObjectBasicInfo.Attributes = HandleInformation.HandleAttributes;
if (ObjectHeader->Flags & OB_FLAG_PERMANENT_OBJECT) {
ObjectBasicInfo.Attributes |= OBJ_PERMANENT;
}
if (ObjectHeader->Flags & OB_FLAG_EXCLUSIVE_OBJECT) {
ObjectBasicInfo.Attributes |= OBJ_EXCLUSIVE;
}
ObjectBasicInfo.GrantedAccess = GrantedAccess;
ObjectBasicInfo.HandleCount = (ULONG)ObjectHeader->HandleCount;
ObjectBasicInfo.PointerCount = (ULONG)ObjectHeader->PointerCount;
QuotaInfo = OBJECT_HEADER_TO_QUOTA_INFO( ObjectHeader );
if (QuotaInfo != NULL) {
ObjectBasicInfo.PagedPoolCharge = QuotaInfo->PagedPoolCharge;
ObjectBasicInfo.NonPagedPoolCharge = QuotaInfo->NonPagedPoolCharge;
} else {
ObjectBasicInfo.PagedPoolCharge = 0;
ObjectBasicInfo.NonPagedPoolCharge = 0;
}
if (ObjectType == ObpSymbolicLinkObjectType) {
ObjectBasicInfo.CreationTime = ((POBJECT_SYMBOLIC_LINK)Object)->CreationTime;
} else {
RtlZeroMemory( &ObjectBasicInfo.CreationTime,
sizeof( ObjectBasicInfo.CreationTime ));
}
//
// Compute the size of the object name string by taking its name plus
// separators and traversing up to the root adding each directories
// name length plus separators
//
NameInfo = ObpReferenceNameInfo( ObjectHeader );
if ((NameInfo != NULL) && (NameInfo->Directory != NULL)) {
PVOID ReferencedDirectory = NULL;
//
// We grab the root directory lock and test again the directory
//
ObjectDirectory = NameInfo->Directory;
ASSERT (ObjectDirectory);
ObfReferenceObject( ObjectDirectory );
ReferencedDirectory = ObjectDirectory;
NameInfoSize = sizeof( OBJ_NAME_PATH_SEPARATOR ) + NameInfo->Name.Length;
ObpDereferenceNameInfo( NameInfo );
NameInfo = NULL;
while (ObjectDirectory) {
ObjectDirectoryHeader = OBJECT_TO_OBJECT_HEADER( ObjectDirectory );
NameInfo = ObpReferenceNameInfo( ObjectDirectoryHeader );
if ((NameInfo != NULL) && (NameInfo->Directory != NULL)) {
NameInfoSize += sizeof( OBJ_NAME_PATH_SEPARATOR ) + NameInfo->Name.Length;
ObjectDirectory = NameInfo->Directory;
ObfReferenceObject( ObjectDirectory );
ObpDereferenceNameInfo( NameInfo );
NameInfo = NULL;
ObDereferenceObject( ReferencedDirectory );
ReferencedDirectory = ObjectDirectory;
} else {
break;
}
}
if (ReferencedDirectory) {
ObDereferenceObject( ReferencedDirectory );
}
NameInfoSize += sizeof( OBJECT_NAME_INFORMATION ) + sizeof( UNICODE_NULL );
} else {
NameInfoSize = 0;
}
ObpDereferenceNameInfo( NameInfo );
NameInfo = NULL;
ObjectBasicInfo.NameInfoSize = NameInfoSize;
ObjectBasicInfo.TypeInfoSize = ObjectType->Name.Length + sizeof( UNICODE_NULL ) +
sizeof( OBJECT_TYPE_INFORMATION );
if ((GrantedAccess & READ_CONTROL) &&
ARGUMENT_PRESENT( ObjectHeader->SecurityDescriptor )) {
SECURITY_INFORMATION SecurityInformation;
//
// Request a complete security descriptor
//
SecurityInformation = OWNER_SECURITY_INFORMATION |
GROUP_SECURITY_INFORMATION |
DACL_SECURITY_INFORMATION |
SACL_SECURITY_INFORMATION;
SecurityDescriptorSize = 0;
(ObjectType->TypeInfo.SecurityProcedure)( Object,
QuerySecurityDescriptor,
&SecurityInformation,
NULL,
&SecurityDescriptorSize,
&ObjectHeader->SecurityDescriptor,
ObjectType->TypeInfo.PoolType,
&ObjectType->TypeInfo.GenericMapping );
} else {
SecurityDescriptorSize = 0;
}
ObjectBasicInfo.SecurityDescriptorSize = SecurityDescriptorSize;
//
// Now that we've packaged up our local copy of basic info we need
// to copy it into the output buffer and set the return
// length
//
try {
*(POBJECT_BASIC_INFORMATION) ObjectInformation = ObjectBasicInfo;
TempReturnLength = ObjectInformationLength;
} except( EXCEPTION_EXECUTE_HANDLER ) {
//
// Fall through, since we cannot undo what we have done.
//
}
break;
case ObjectNameInformation:
//
// Call a local worker routine
//
Status = ObpQueryNameString( Object,
(POBJECT_NAME_INFORMATION)ObjectInformation,
ObjectInformationLength,
&TempReturnLength,
PreviousMode );
break;
case ObjectTypeInformation:
//
// Call a local worker routine
//
Status = ObQueryTypeInfo( ObjectType,
(POBJECT_TYPE_INFORMATION)ObjectInformation,
ObjectInformationLength,
&TempReturnLength );
break;
case ObjectTypesInformation:
try {
//
// The first thing we do is set the return length to cover the
// types info record. Later in each call to query type info
// this value will be updated as necessary
//
TempReturnLength = sizeof( OBJECT_TYPES_INFORMATION );
//
// Make sure there is enough room to hold the types info record
// and if so then compute the number of defined types there are
//
TypesInformation = (POBJECT_TYPES_INFORMATION)ObjectInformation;
if (ObjectInformationLength < sizeof( OBJECT_TYPES_INFORMATION ) ) {
Status = STATUS_INFO_LENGTH_MISMATCH;
} else {
TypesInformation->NumberOfTypes = 0;
for (i=0; i<OBP_MAX_DEFINED_OBJECT_TYPES; i++) {
ObjectType = ObpObjectTypes[ i ];
if (ObjectType == NULL) {
break;
}
TypesInformation->NumberOfTypes += 1;
}
}
//
// For each defined type we will query the type info for the
// object type and adjust the TypeInfo pointer to the next
// free spot
//
TypeInfo = (POBJECT_TYPE_INFORMATION)(((PUCHAR)TypesInformation) + ALIGN_UP( sizeof(*TypesInformation), ULONG_PTR ));
for (i=0; i<OBP_MAX_DEFINED_OBJECT_TYPES; i++) {
ObjectType = ObpObjectTypes[ i ];
if (ObjectType == NULL) {
break;
}
Status = ObQueryTypeInfo( ObjectType,
TypeInfo,
ObjectInformationLength,
&TempReturnLength );
if (NT_SUCCESS( Status )) {
TypeInfo = (POBJECT_TYPE_INFORMATION)
((PCHAR)(TypeInfo+1) + ALIGN_UP( TypeInfo->TypeName.MaximumLength, ULONG_PTR ));
}
}
} except( EXCEPTION_EXECUTE_HANDLER ) {
Status = GetExceptionCode();
}
break;
case ObjectHandleFlagInformation:
try {
//
// Set the amount of data we are going to return
//
TempReturnLength = sizeof(OBJECT_HANDLE_FLAG_INFORMATION);
HandleFlags = (POBJECT_HANDLE_FLAG_INFORMATION)ObjectInformation;
//
// Make sure we have enough room for the query, and if so we'll
// set the output based on the flags stored in the handle
//
if (ObjectInformationLength < sizeof( OBJECT_HANDLE_FLAG_INFORMATION)) {
Status = STATUS_INFO_LENGTH_MISMATCH;
} else {
HandleFlags->Inherit = FALSE;
if (HandleInformation.HandleAttributes & OBJ_INHERIT) {
HandleFlags->Inherit = TRUE;
}
HandleFlags->ProtectFromClose = FALSE;
if (HandleInformation.HandleAttributes & OBJ_PROTECT_CLOSE) {
HandleFlags->ProtectFromClose = TRUE;
}
}
} except( EXCEPTION_EXECUTE_HANDLER ) {
Status = GetExceptionCode();
}
break;
default:
//
// To get to this point we must have had an object and the
// information class is not defined, so we should dereference the
// object and return to our user the bad status
//
ObDereferenceObject( Object );
return( STATUS_INVALID_INFO_CLASS );
}
//
// Now if the caller asked for a return length we'll set it from
// our local copy
//
try {
if (ARGUMENT_PRESENT( ReturnLength ) ) {
*ReturnLength = TempReturnLength;
}
} except( EXCEPTION_EXECUTE_HANDLER ) {
//
// Fall through, since we cannot undo what we have done.
//
}
//
// In the end we can free the object if there was one and return
// to our caller
//
if (Object != NULL) {
ObDereferenceObject( Object );
}
return( Status );
}
NTSTATUS
ObSetHandleAttributes (
__in HANDLE Handle,
__in POBJECT_HANDLE_FLAG_INFORMATION HandleFlags,
__in KPROCESSOR_MODE PreviousMode
)
{
BOOLEAN AttachedToProcess = FALSE;
KAPC_STATE ApcState;
OBP_SET_HANDLE_ATTRIBUTES CapturedInformation;
PVOID ObjectTable;
HANDLE ObjectHandle;
NTSTATUS Status;
PAGED_CODE();
CapturedInformation.PreviousMode = PreviousMode;
CapturedInformation.ObjectInformation = *HandleFlags;
//
// Get the address of the object table for the current process. Or
// get the system handle table if this is a kernel handle and we are
// in kernel mode
//
if (IsKernelHandle( Handle, PreviousMode )) {
//
// Make the handle look like a regular handle
//
ObjectHandle = DecodeKernelHandle( Handle );
//
// The global kernel handle table
//
ObjectTable = ObpKernelHandleTable;
//
// Go to the system process
//
if (PsGetCurrentProcess() != PsInitialSystemProcess) {
KeStackAttachProcess (&PsInitialSystemProcess->Pcb, &ApcState);
AttachedToProcess = TRUE;
}
} else {
ObjectTable = ObpGetObjectTable();
ObjectHandle = Handle;
}
//
// Make the change to the handle table entry. The callback
// routine will do the actual change
//
if (ExChangeHandle( ObjectTable,
ObjectHandle,
ObpSetHandleAttributes,
(ULONG_PTR)&CapturedInformation) ) {
Status = STATUS_SUCCESS;
} else {
Status = STATUS_ACCESS_DENIED;
}
//
// If we are attached to the system process then return
// back to our caller
//
if (AttachedToProcess) {
KeUnstackDetachProcess(&ApcState);
AttachedToProcess = FALSE;
}
return Status;
}
NTSTATUS
NTAPI
NtSetInformationObject (
__in HANDLE Handle,
__in OBJECT_INFORMATION_CLASS ObjectInformationClass,
__in_bcount(ObjectInformationLength) PVOID ObjectInformation,
__in ULONG ObjectInformationLength
)
/*++
Routine description:
This routine is used to set handle information about a specified
handle
Arguments:
Handle - Supplies the handle being modified
ObjectInformationClass - Specifies the class of information being
modified. The only accepted value is ObjectHandleFlagInformation
ObjectInformation - Supplies the buffer containing the handle
flag information structure
ObjectInformationLength - Specifies the length, in bytes, of the
object information buffer
Return Value:
An appropriate status value
--*/
{
NTSTATUS Status;
OBJECT_HANDLE_FLAG_INFORMATION CapturedFlags;
KPROCESSOR_MODE PreviousMode;
PAGED_CODE();
Status = STATUS_INVALID_INFO_CLASS;
switch (ObjectInformationClass) {
case ObjectHandleFlagInformation:
{
if (ObjectInformationLength != sizeof(OBJECT_HANDLE_FLAG_INFORMATION)) {
return STATUS_INFO_LENGTH_MISMATCH;
}
//
// Get previous processor mode and probe and capture the input
// buffer
//
PreviousMode = KeGetPreviousMode();
try {
if (PreviousMode != KernelMode) {
ProbeForRead(ObjectInformation, ObjectInformationLength, 1);
}
CapturedFlags = *(POBJECT_HANDLE_FLAG_INFORMATION)ObjectInformation;
} except(ExSystemExceptionFilter()) {
return GetExceptionCode();
}
Status = ObSetHandleAttributes (Handle,
&CapturedFlags,
PreviousMode);
}
break;
case ObjectSessionInformation:
{
PreviousMode = KeGetPreviousMode();
if (!SeSinglePrivilegeCheck( SeTcbPrivilege,
PreviousMode)) {
Status = STATUS_PRIVILEGE_NOT_HELD;
} else {
PVOID Object;
OBJECT_HANDLE_INFORMATION HandleInformation;
Status = ObReferenceObjectByHandle(Handle,
0,
ObpDirectoryObjectType,
PreviousMode,
&Object,
&HandleInformation
);
if (NT_SUCCESS(Status)) {
POBJECT_DIRECTORY Directory;
OBP_LOOKUP_CONTEXT LockContext;
Directory = (POBJECT_DIRECTORY)Object;
ObpInitializeLookupContext( &LockContext );
ObpLockDirectoryExclusive(Directory, &LockContext);
Directory->SessionId = PsGetCurrentProcessSessionId();
ObpUnlockDirectory(Directory, &LockContext);
ObDereferenceObject(Object);
}
}
}
break;
}
//
// And return to our caller
//
return Status;
}
#define OBP_MISSING_NAME_LITERAL L"..."
#define OBP_MISSING_NAME_LITERAL_SIZE (sizeof( OBP_MISSING_NAME_LITERAL ) - sizeof( UNICODE_NULL ))
NTSTATUS
ObQueryNameString (
__in PVOID Object,
__out_bcount(Length) POBJECT_NAME_INFORMATION ObjectNameInfo,
__in ULONG Length,
__out PULONG ReturnLength
)
/*++
Routine description:
This routine processes a query of an object's name information
Arguments:
Object - Supplies the object being queried
ObjectNameInfo - Supplies the buffer to store the name string
information
Length - Specifies the length, in bytes, of the original object
name info buffer.
ReturnLength - Contains the number of bytes already used up
in the object name info. On return this receives an updated
byte count.
(Length minus ReturnLength) is really now many bytes are left
in the output buffer. The buffer supplied to this call may
actually be offset within the original users buffer
Return Value:
An appropriate status value
--*/
{
return ObpQueryNameString( Object,
ObjectNameInfo,
Length,
ReturnLength,
KernelMode );
}
NTSTATUS
ObpQueryNameString (
IN PVOID Object,
OUT POBJECT_NAME_INFORMATION ObjectNameInfo,
IN ULONG Length,
OUT PULONG ReturnLength,
IN KPROCESSOR_MODE Mode
)
/*++
Routine description:
This routine processes a query of an object's name information
Arguments:
Object - Supplies the object being queried
ObjectNameInfo - Supplies the buffer to store the name string
information
Length - Specifies the length, in bytes, of the original object
name info buffer.
ReturnLength - Contains the number of bytes already used up
in the object name info. On return this receives an updated
byte count.
(Length minus ReturnLength) is really now many bytes are left
in the output buffer. The buffer supplied to this call may
actually be offset within the original users buffer
Mode - Mode of caller
Return Value:
An appropriate status value
--*/
{
NTSTATUS Status = STATUS_UNSUCCESSFUL;
POBJECT_HEADER ObjectHeader;
POBJECT_HEADER_NAME_INFO NameInfo;
POBJECT_HEADER ObjectDirectoryHeader;
POBJECT_DIRECTORY ObjectDirectory;
ULONG NameInfoSize = 0;
PUNICODE_STRING String;
PWCH StringBuffer;
ULONG NameSize;
PVOID ReferencedObject = NULL;
BOOLEAN DoFullQuery = TRUE;
ULONG BufferLength;
PWCH OriginalBuffer;
BOOLEAN ForceRetry = FALSE;
PAGED_CODE();
//
// Get the object header and name info record if it exists
//
ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
NameInfo = ObpReferenceNameInfo( ObjectHeader );
//
// If the object type has a query name callback routine then
// that is how we get the name
//
if (ObjectHeader->Type->TypeInfo.QueryNameProcedure != NULL) {
try {
#if DBG
KIRQL SaveIrql;
#endif
ObpBeginTypeSpecificCallOut( SaveIrql );
ObpEndTypeSpecificCallOut( SaveIrql, "Query", ObjectHeader->Type, Object );
Status = (*ObjectHeader->Type->TypeInfo.QueryNameProcedure)( Object,
(BOOLEAN)((NameInfo != NULL) && (NameInfo->Name.Length != 0)),
ObjectNameInfo,
Length,
ReturnLength,
Mode );
} except( EXCEPTION_EXECUTE_HANDLER ) {
Status = GetExceptionCode();
}
ObpDereferenceNameInfo( NameInfo );
return( Status );
}
//
// Otherwise, the object type does not specify a query name
// procedure so we get to do the work. The first thing
// to check is if the object doesn't even have a name. If
// object doesn't have a name then we'll return an empty name
// info structure.
//
RETRY:
if ((NameInfo == NULL) || (NameInfo->Name.Buffer == NULL)) {
//
// Compute the length of our return buffer, set the output
// if necessary and make sure the supplied buffer is large
// enough
//
NameInfoSize = sizeof( OBJECT_NAME_INFORMATION );
try {
*ReturnLength = NameInfoSize;
} except( EXCEPTION_EXECUTE_HANDLER ) {
ObpDereferenceNameInfo( NameInfo );
return( GetExceptionCode() );
}
if (Length < NameInfoSize) {
ObpDereferenceNameInfo( NameInfo );
return( STATUS_INFO_LENGTH_MISMATCH );
}
//
// Initialize the output buffer to be an empty string
// and then return to our caller
//
try {
ObjectNameInfo->Name.Length = 0;
ObjectNameInfo->Name.MaximumLength = 0;
ObjectNameInfo->Name.Buffer = NULL;
} except( EXCEPTION_EXECUTE_HANDLER ) {
//
// Fall through, since we cannot undo what we have done.
//
ObpDereferenceNameInfo(NameInfo);
return( GetExceptionCode() );
}
ObpDereferenceNameInfo(NameInfo);
return( STATUS_SUCCESS );
}
try {