2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ob/oblife.c
5 * PURPOSE: Manages the lifetime of an Object, including its creation,
6 * and deletion, as well as setting or querying any of its
7 * information while it is active. Since Object Types are also
8 * Objects, those are also managed here.
9 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
11 * Thomas Weidenmueller (w3seek@reactos.org)
14 /* INCLUDES ******************************************************************/
20 extern ULONG NtGlobalFlag
;
22 POBJECT_TYPE ObpTypeObjectType
= NULL
;
23 KEVENT ObpDefaultObject
;
24 KGUARDED_MUTEX ObpDeviceMapLock
;
26 GENERAL_LOOKASIDE ObpNameBufferLookasideList
, ObpCreateInfoLookasideList
;
28 WORK_QUEUE_ITEM ObpReaperWorkItem
;
29 volatile PVOID ObpReaperList
;
31 ULONG ObpObjectsCreated
, ObpObjectsWithName
, ObpObjectsWithPoolQuota
;
32 ULONG ObpObjectsWithHandleDB
, ObpObjectsWithCreatorInfo
;
33 POBJECT_TYPE ObpObjectTypes
[32];
35 /* PRIVATE FUNCTIONS *********************************************************/
39 ObpDeallocateObject(IN PVOID Object
)
42 POBJECT_HEADER Header
;
43 POBJECT_TYPE ObjectType
;
44 POBJECT_HEADER_HANDLE_INFO HandleInfo
;
45 POBJECT_HEADER_NAME_INFO NameInfo
;
46 POBJECT_HEADER_CREATOR_INFO CreatorInfo
;
47 POBJECT_HEADER_QUOTA_INFO QuotaInfo
;
48 ULONG PagedPoolCharge
, NonPagedPoolCharge
;
51 /* Get the header and assume this is what we'll free */
52 Header
= OBJECT_TO_OBJECT_HEADER(Object
);
53 ObjectType
= Header
->Type
;
54 HeaderLocation
= Header
;
56 /* To find the header, walk backwards from how we allocated */
57 if ((CreatorInfo
= OBJECT_HEADER_TO_CREATOR_INFO(Header
)))
59 HeaderLocation
= CreatorInfo
;
61 if ((NameInfo
= OBJECT_HEADER_TO_NAME_INFO(Header
)))
63 HeaderLocation
= NameInfo
;
65 if ((HandleInfo
= OBJECT_HEADER_TO_HANDLE_INFO(Header
)))
67 HeaderLocation
= HandleInfo
;
69 if ((QuotaInfo
= OBJECT_HEADER_TO_QUOTA_INFO(Header
)))
71 HeaderLocation
= QuotaInfo
;
74 /* Decrease the total */
75 InterlockedDecrement((PLONG
)&ObjectType
->TotalNumberOfObjects
);
77 /* Check if we have create info */
78 if (Header
->Flags
& OB_FLAG_CREATE_INFO
)
80 /* Double-check that it exists */
81 if (Header
->ObjectCreateInfo
)
84 ObpFreeObjectCreateInformation(Header
->ObjectCreateInfo
);
85 Header
->ObjectCreateInfo
= NULL
;
90 /* Check if it has a quota block */
91 if (Header
->QuotaBlockCharged
)
93 /* Check if we have quota information */
96 /* Get charges from quota information */
97 PagedPoolCharge
= QuotaInfo
->PagedPoolCharge
+
98 QuotaInfo
->SecurityDescriptorCharge
;
99 NonPagedPoolCharge
= QuotaInfo
->NonPagedPoolCharge
;
103 /* Get charges from object type */
104 PagedPoolCharge
= ObjectType
->TypeInfo
.DefaultPagedPoolCharge
;
105 NonPagedPoolCharge
= ObjectType
->
106 TypeInfo
.DefaultNonPagedPoolCharge
;
108 /* Add the SD charge too */
109 if (Header
->Flags
& OB_FLAG_SECURITY
) PagedPoolCharge
+= 2048;
112 /* Return the quota */
113 DPRINT("FIXME: Should return quotas: %lx %lx\n", PagedPoolCharge
, NonPagedPoolCharge
);
115 PsReturnSharedPoolQuota(ObjectHeader
->QuotaBlockCharged
,
123 /* Check if a handle database was active */
124 if ((HandleInfo
) && !(Header
->Flags
& OB_FLAG_SINGLE_PROCESS
))
127 ExFreePool(HandleInfo
->HandleCountDatabase
);
128 HandleInfo
->HandleCountDatabase
= NULL
;
131 /* Check if we have a name */
132 if ((NameInfo
) && (NameInfo
->Name
.Buffer
))
135 ExFreePool(NameInfo
->Name
.Buffer
);
136 NameInfo
->Name
.Buffer
= NULL
;
139 /* Catch invalid access */
140 Header
->Type
= (POBJECT_TYPE
)0xBAADB0B0;
142 /* Free the object using the same allocation tag */
143 ExFreePoolWithTag(HeaderLocation
, ObjectType
->Key
);
148 ObpDeleteObject(IN PVOID Object
,
149 IN BOOLEAN CalledFromWorkerThread
)
151 POBJECT_HEADER Header
;
152 POBJECT_TYPE ObjectType
;
153 POBJECT_HEADER_NAME_INFO NameInfo
;
154 POBJECT_HEADER_CREATOR_INFO CreatorInfo
;
158 /* Get the header and type */
159 Header
= OBJECT_TO_OBJECT_HEADER(Object
);
160 ObjectType
= Header
->Type
;
162 /* Get creator and name information */
163 NameInfo
= OBJECT_HEADER_TO_NAME_INFO(Header
);
164 CreatorInfo
= OBJECT_HEADER_TO_CREATOR_INFO(Header
);
166 /* Check if the object is on a type list */
167 if ((CreatorInfo
) && !(IsListEmpty(&CreatorInfo
->TypeList
)))
169 /* Lock the object type */
170 ObpEnterObjectTypeMutex(ObjectType
);
172 /* Remove the object from the type list */
173 RemoveEntryList(&CreatorInfo
->TypeList
);
175 /* Release the lock */
176 ObpLeaveObjectTypeMutex(ObjectType
);
179 /* Check if we have a name */
180 if ((NameInfo
) && (NameInfo
->Name
.Buffer
))
183 ExFreePool(NameInfo
->Name
.Buffer
);
184 RtlInitEmptyUnicodeString(&NameInfo
->Name
, NULL
, 0);
187 /* Check if we have a security descriptor */
188 if (Header
->SecurityDescriptor
)
190 /* Call the security procedure to delete it */
191 ObpCalloutStart(&CalloutIrql
);
192 ObjectType
->TypeInfo
.SecurityProcedure(Object
,
193 DeleteSecurityDescriptor
,
197 &Header
->SecurityDescriptor
,
200 ObpCalloutEnd(CalloutIrql
, "Security", ObjectType
, Object
);
203 /* Check if we have a delete procedure */
204 if (ObjectType
->TypeInfo
.DeleteProcedure
)
206 /* Save whether we were deleted from worker thread or not */
207 if (!CalledFromWorkerThread
) Header
->Flags
|= OB_FLAG_DEFER_DELETE
;
210 ObpCalloutStart(&CalloutIrql
);
211 ObjectType
->TypeInfo
.DeleteProcedure(Object
);
212 ObpCalloutEnd(CalloutIrql
, "Delete", ObjectType
, Object
);
215 /* Now de-allocate all object members */
216 ObpDeallocateObject(Object
);
221 ObpReapObject(IN PVOID Parameter
)
223 POBJECT_HEADER ReapObject
, NextObject
;
228 /* Get the reap object */
229 ReapObject
= InterlockedExchangePointer(&ObpReaperList
, (PVOID
)1);
231 /* Start deletion loop */
234 /* Get the next object */
235 NextObject
= ReapObject
->NextToFree
;
237 /* Delete the object */
238 ObpDeleteObject(&ReapObject
->Body
, TRUE
);
240 /* Move to the next one */
241 ReapObject
= NextObject
;
242 } while ((ReapObject
) && (ReapObject
!= (PVOID
)1));
243 } while ((ObpReaperList
!= (PVOID
)1) ||
244 (InterlockedCompareExchange((PLONG
)&ObpReaperList
, 0, 1) != 1));
248 * @name ObpSetPermanentObject
250 * The ObpSetPermanentObject routine makes an sets or clears the permanent
251 * flag of an object, thus making it either permanent or temporary.
254 * Pointer to the object to make permanent or temporary.
257 * Flag specifying which operation to perform.
261 * @remarks If the object is being made temporary, then it will be checked
262 * as a candidate for immediate removal from the namespace.
267 ObpSetPermanentObject(IN PVOID ObjectBody
,
268 IN BOOLEAN Permanent
)
270 POBJECT_HEADER ObjectHeader
;
273 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(ObjectBody
);
275 /* Acquire object lock */
276 ObpAcquireObjectLock(ObjectHeader
);
278 /* Check what we're doing to it */
281 /* Set it to permanent */
282 ObjectHeader
->Flags
|= OB_FLAG_PERMANENT
;
284 /* Release the lock */
285 ObpReleaseObjectLock(ObjectHeader
);
289 /* Remove the flag */
290 ObjectHeader
->Flags
&= ~OB_FLAG_PERMANENT
;
292 /* Release the lock */
293 ObpReleaseObjectLock(ObjectHeader
);
295 /* Check if we should delete the object now */
296 ObpDeleteNameCheck(ObjectBody
);
302 ObpAllocateObjectNameBuffer(IN ULONG Length
,
303 IN BOOLEAN UseLookaside
,
304 IN OUT PUNICODE_STRING ObjectName
)
309 /* Set the maximum length to the length plus the terminator */
310 MaximumLength
= Length
+ sizeof(UNICODE_NULL
);
312 /* Check if we should use the lookaside buffer */
313 if (!(UseLookaside
) || (MaximumLength
> OBP_NAME_LOOKASIDE_MAX_SIZE
))
315 /* Nope, allocate directly from pool */
316 /* Since we later use MaximumLength to detect that we're not allocating
317 * from a list, we need at least MaximumLength + sizeof(UNICODE_NULL)
320 * People do call this with UseLookasideList FALSE so the distinction
323 if (MaximumLength
<= OBP_NAME_LOOKASIDE_MAX_SIZE
)
325 MaximumLength
= OBP_NAME_LOOKASIDE_MAX_SIZE
+ sizeof(UNICODE_NULL
);
327 Buffer
= ExAllocatePoolWithTag(PagedPool
,
333 /* Allocate from the lookaside */
334 MaximumLength
= OBP_NAME_LOOKASIDE_MAX_SIZE
;
335 Buffer
= ObpAllocateObjectCreateInfoBuffer(LookasideNameBufferList
);
338 /* Setup the string */
339 ObjectName
->MaximumLength
= (USHORT
)MaximumLength
;
340 ObjectName
->Length
= (USHORT
)Length
;
341 ObjectName
->Buffer
= Buffer
;
347 ObpFreeObjectNameBuffer(IN PUNICODE_STRING Name
)
349 PVOID Buffer
= Name
->Buffer
;
351 /* We know this is a pool-allocation if the size doesn't match */
352 if (Name
->MaximumLength
!= OBP_NAME_LOOKASIDE_MAX_SIZE
)
354 /* Free it from the pool */
359 /* Otherwise, free from the lookaside */
360 ObpFreeCapturedAttributes(Buffer
, LookasideNameBufferList
);
366 ObpCaptureObjectName(IN OUT PUNICODE_STRING CapturedName
,
367 IN PUNICODE_STRING ObjectName
,
368 IN KPROCESSOR_MODE AccessMode
,
369 IN BOOLEAN UseLookaside
)
371 NTSTATUS Status
= STATUS_SUCCESS
;
373 PWCHAR StringBuffer
= NULL
;
374 UNICODE_STRING LocalName
;
377 /* Initialize the Input String */
378 RtlInitEmptyUnicodeString(CapturedName
, NULL
, 0);
380 /* Protect everything */
383 /* Check if we came from user mode */
384 if (AccessMode
!= KernelMode
)
386 /* First Probe the String */
387 LocalName
= ProbeForReadUnicodeString(ObjectName
);
388 ProbeForRead(LocalName
.Buffer
, LocalName
.Length
, sizeof(WCHAR
));
392 /* No probing needed */
393 LocalName
= *ObjectName
;
396 /* Make sure there really is a string */
397 if ((StringLength
= LocalName
.Length
))
399 /* Check that the size is a valid WCHAR multiple */
400 if ((StringLength
& (sizeof(WCHAR
) - 1)) ||
401 /* Check that the NULL-termination below will work */
402 (StringLength
== (MAXUSHORT
- sizeof(UNICODE_NULL
) + 1)))
404 /* PS: Please keep the checks above expanded for clarity */
405 Status
= STATUS_OBJECT_NAME_INVALID
;
409 /* Allocate the string buffer */
410 StringBuffer
= ObpAllocateObjectNameBuffer(StringLength
,
415 /* Set failure code */
416 Status
= STATUS_INSUFFICIENT_RESOURCES
;
421 RtlCopyMemory(StringBuffer
, LocalName
.Buffer
, StringLength
);
422 StringBuffer
[StringLength
/ sizeof(WCHAR
)] = UNICODE_NULL
;
427 _SEH2_EXCEPT(ExSystemExceptionFilter())
429 /* Handle exception and free the string buffer */
430 Status
= _SEH2_GetExceptionCode();
433 ObpFreeObjectNameBuffer(CapturedName
);
444 ObpCaptureObjectCreateInformation(IN POBJECT_ATTRIBUTES ObjectAttributes
,
445 IN KPROCESSOR_MODE AccessMode
,
446 IN BOOLEAN AllocateFromLookaside
,
447 IN POBJECT_CREATE_INFORMATION ObjectCreateInfo
,
448 OUT PUNICODE_STRING ObjectName
)
450 NTSTATUS Status
= STATUS_SUCCESS
;
451 PSECURITY_DESCRIPTOR SecurityDescriptor
;
452 PSECURITY_QUALITY_OF_SERVICE SecurityQos
;
453 PUNICODE_STRING LocalObjectName
= NULL
;
456 /* Zero out the Capture Data */
457 RtlZeroMemory(ObjectCreateInfo
, sizeof(OBJECT_CREATE_INFORMATION
));
459 /* SEH everything here for protection */
462 /* Check if we got attributes */
463 if (ObjectAttributes
)
465 /* Check if we're in user mode */
466 if (AccessMode
!= KernelMode
)
468 /* Probe the attributes */
469 ProbeForRead(ObjectAttributes
,
470 sizeof(OBJECT_ATTRIBUTES
),
474 /* Validate the Size and Attributes */
475 if ((ObjectAttributes
->Length
!= sizeof(OBJECT_ATTRIBUTES
)) ||
476 (ObjectAttributes
->Attributes
& ~OBJ_VALID_ATTRIBUTES
))
478 /* Invalid combination, fail */
479 _SEH2_YIELD(return STATUS_INVALID_PARAMETER
);
482 /* Set some Create Info */
483 ObjectCreateInfo
->RootDirectory
= ObjectAttributes
->RootDirectory
;
484 ObjectCreateInfo
->Attributes
= ObjectAttributes
->Attributes
;
485 LocalObjectName
= ObjectAttributes
->ObjectName
;
486 SecurityDescriptor
= ObjectAttributes
->SecurityDescriptor
;
487 SecurityQos
= ObjectAttributes
->SecurityQualityOfService
;
489 /* Check if we have a security descriptor */
490 if (SecurityDescriptor
)
493 Status
= SeCaptureSecurityDescriptor(SecurityDescriptor
,
499 if (!NT_SUCCESS(Status
))
501 /* Capture failed, quit */
502 ObjectCreateInfo
->SecurityDescriptor
= NULL
;
503 _SEH2_YIELD(return Status
);
506 /* Save the probe mode and security descriptor size */
507 ObjectCreateInfo
->SecurityDescriptorCharge
= 2048; /* FIXME */
508 ObjectCreateInfo
->ProbeMode
= AccessMode
;
511 /* Check if we have QoS */
514 /* Check if we came from user mode */
515 if (AccessMode
!= KernelMode
)
517 /* Validate the QoS */
518 ProbeForRead(SecurityQos
,
519 sizeof(SECURITY_QUALITY_OF_SERVICE
),
524 ObjectCreateInfo
->SecurityQualityOfService
= *SecurityQos
;
525 ObjectCreateInfo
->SecurityQos
=
526 &ObjectCreateInfo
->SecurityQualityOfService
;
531 /* We don't have a name */
532 LocalObjectName
= NULL
;
535 _SEH2_EXCEPT(ExSystemExceptionFilter())
537 /* Cleanup and return the exception code */
538 ObpReleaseObjectCreateInformation(ObjectCreateInfo
);
539 _SEH2_YIELD(return _SEH2_GetExceptionCode());
543 /* Now check if the Object Attributes had an Object Name */
546 Status
= ObpCaptureObjectName(ObjectName
,
549 AllocateFromLookaside
);
553 /* Clear the string */
554 RtlInitEmptyUnicodeString(ObjectName
, NULL
, 0);
556 /* He can't have specified a Root Directory */
557 if (ObjectCreateInfo
->RootDirectory
)
559 Status
= STATUS_OBJECT_NAME_INVALID
;
563 /* Cleanup if we failed */
564 if (!NT_SUCCESS(Status
))
566 ObpReleaseObjectCreateInformation(ObjectCreateInfo
);
569 /* Return status to caller */
575 ObFreeObjectCreateInfoBuffer(IN POBJECT_CREATE_INFORMATION ObjectCreateInfo
)
577 /* Call the macro. We use this function to isolate Ob internals from Io */
578 ObpFreeCapturedAttributes(ObjectCreateInfo
, LookasideCreateInfoList
);
583 ObpAllocateObject(IN POBJECT_CREATE_INFORMATION ObjectCreateInfo
,
584 IN PUNICODE_STRING ObjectName
,
585 IN POBJECT_TYPE ObjectType
,
587 IN KPROCESSOR_MODE PreviousMode
,
588 IN POBJECT_HEADER
*ObjectHeader
)
590 POBJECT_HEADER Header
;
591 ULONG QuotaSize
, HandleSize
, NameSize
, CreatorSize
;
592 POBJECT_HEADER_HANDLE_INFO HandleInfo
;
593 POBJECT_HEADER_NAME_INFO NameInfo
;
594 POBJECT_HEADER_CREATOR_INFO CreatorInfo
;
595 POBJECT_HEADER_QUOTA_INFO QuotaInfo
;
604 /* Check if we don't have an Object Type yet */
607 /* Use default tag and non-paged pool */
608 PoolType
= NonPagedPool
;
613 /* Use the pool and tag given */
614 PoolType
= ObjectType
->TypeInfo
.PoolType
;
615 Tag
= ObjectType
->Key
;
618 /* Check if we have no create information (ie: we're an object type) */
619 if (!ObjectCreateInfo
)
622 QuotaSize
= HandleSize
= 0;
623 NameSize
= sizeof(OBJECT_HEADER_NAME_INFO
);
624 CreatorSize
= sizeof(OBJECT_HEADER_CREATOR_INFO
);
628 /* Check if we have quota */
629 if ((((ObjectCreateInfo
->PagedPoolCharge
!=
630 ObjectType
->TypeInfo
.DefaultPagedPoolCharge
) ||
631 (ObjectCreateInfo
->NonPagedPoolCharge
!=
632 ObjectType
->TypeInfo
.DefaultNonPagedPoolCharge
) ||
633 (ObjectCreateInfo
->SecurityDescriptorCharge
> 2048)) &&
634 (PsGetCurrentProcess() != PsInitialSystemProcess
)) ||
635 (ObjectCreateInfo
->Attributes
& OBJ_EXCLUSIVE
))
638 QuotaSize
= sizeof(OBJECT_HEADER_QUOTA_INFO
);
639 ObpObjectsWithPoolQuota
++;
647 /* Check if we have a handle database */
648 if (ObjectType
->TypeInfo
.MaintainHandleCount
)
650 /* Set handle database size */
651 HandleSize
= sizeof(OBJECT_HEADER_HANDLE_INFO
);
652 ObpObjectsWithHandleDB
++;
660 /* Check if the Object has a name */
661 if (ObjectName
->Buffer
)
664 NameSize
= sizeof(OBJECT_HEADER_NAME_INFO
);
665 ObpObjectsWithName
++;
673 /* Check if the Object maintains type lists */
674 if (ObjectType
->TypeInfo
.MaintainTypeList
)
676 /* Set owner/creator size */
677 CreatorSize
= sizeof(OBJECT_HEADER_CREATOR_INFO
);
678 ObpObjectsWithCreatorInfo
++;
687 /* Set final header size */
688 FinalSize
= QuotaSize
+
692 FIELD_OFFSET(OBJECT_HEADER
, Body
);
694 /* Allocate memory for the Object and Header */
695 Header
= ExAllocatePoolWithTag(PoolType
, FinalSize
+ ObjectSize
, Tag
);
696 if (!Header
) return STATUS_INSUFFICIENT_RESOURCES
;
698 /* Check if we have a quota header */
701 /* Initialize quota info */
702 QuotaInfo
= (POBJECT_HEADER_QUOTA_INFO
)Header
;
703 QuotaInfo
->PagedPoolCharge
= ObjectCreateInfo
->PagedPoolCharge
;
704 QuotaInfo
->NonPagedPoolCharge
= ObjectCreateInfo
->NonPagedPoolCharge
;
705 QuotaInfo
->SecurityDescriptorCharge
= ObjectCreateInfo
->SecurityDescriptorCharge
;
706 QuotaInfo
->ExclusiveProcess
= NULL
;
707 Header
= (POBJECT_HEADER
)(QuotaInfo
+ 1);
710 /* Check if we have a handle database header */
713 /* Initialize Handle Info */
714 HandleInfo
= (POBJECT_HEADER_HANDLE_INFO
)Header
;
715 HandleInfo
->SingleEntry
.HandleCount
= 0;
716 Header
= (POBJECT_HEADER
)(HandleInfo
+ 1);
719 /* Check if we have a name header */
722 /* Initialize the Object Name Info */
723 NameInfo
= (POBJECT_HEADER_NAME_INFO
)Header
;
724 NameInfo
->Name
= *ObjectName
;
725 NameInfo
->Directory
= NULL
;
726 NameInfo
->QueryReferences
= 1;
728 /* Check if this is a call with the special protection flag */
729 if ((PreviousMode
== KernelMode
) &&
730 (ObjectCreateInfo
) &&
731 (ObjectCreateInfo
->Attributes
& 0x10000))
733 /* Set flag which will make the object protected from user-mode */
734 NameInfo
->QueryReferences
|= 0x40000000;
737 /* Set the header pointer */
738 Header
= (POBJECT_HEADER
)(NameInfo
+ 1);
741 /* Check if we have a creator header */
744 /* Initialize Creator Info */
745 CreatorInfo
= (POBJECT_HEADER_CREATOR_INFO
)Header
;
746 CreatorInfo
->CreatorBackTraceIndex
= 0;
747 CreatorInfo
->CreatorUniqueProcess
= PsGetCurrentProcessId();
748 InitializeListHead(&CreatorInfo
->TypeList
);
749 Header
= (POBJECT_HEADER
)(CreatorInfo
+ 1);
752 /* Check for quota information */
756 Header
->QuotaInfoOffset
= (UCHAR
)(QuotaSize
+
764 Header
->QuotaInfoOffset
= 0;
767 /* Check for handle information */
771 Header
->HandleInfoOffset
= (UCHAR
)(HandleSize
+
778 Header
->HandleInfoOffset
= 0;
781 /* Check for name information */
785 Header
->NameInfoOffset
= (UCHAR
)(NameSize
+ CreatorSize
);
790 Header
->NameInfoOffset
= 0;
793 /* Set the new object flag */
794 Header
->Flags
= OB_FLAG_CREATE_INFO
;
796 /* Remember if we have creator info */
797 if (CreatorSize
) Header
->Flags
|= OB_FLAG_CREATOR_INFO
;
799 /* Remember if we have handle info */
800 if (HandleSize
) Header
->Flags
|= OB_FLAG_SINGLE_PROCESS
;
802 /* Initialize the object header */
803 Header
->PointerCount
= 1;
804 Header
->HandleCount
= 0;
805 Header
->Type
= ObjectType
;
806 Header
->ObjectCreateInfo
= ObjectCreateInfo
;
807 Header
->SecurityDescriptor
= NULL
;
809 /* Check if this is a permanent object */
810 if ((ObjectCreateInfo
) && (ObjectCreateInfo
->Attributes
& OBJ_PERMANENT
))
812 /* Set the needed flag so we can check */
813 Header
->Flags
|= OB_FLAG_PERMANENT
;
816 /* Check if this is an exclusive object */
817 if ((ObjectCreateInfo
) && (ObjectCreateInfo
->Attributes
& OBJ_EXCLUSIVE
))
819 /* Set the needed flag so we can check */
820 Header
->Flags
|= OB_FLAG_EXCLUSIVE
;
823 /* Set kernel-mode flag */
824 if (PreviousMode
== KernelMode
) Header
->Flags
|= OB_FLAG_KERNEL_MODE
;
826 /* Check if we have a type */
829 /* Increase the number of objects of this type */
830 InterlockedIncrement((PLONG
)&ObjectType
->TotalNumberOfObjects
);
832 /* Update the high water */
833 ObjectType
->HighWaterNumberOfObjects
= max(ObjectType
->
834 TotalNumberOfObjects
,
836 HighWaterNumberOfObjects
);
840 *ObjectHeader
= Header
;
841 return STATUS_SUCCESS
;
846 ObQueryTypeInfo(IN POBJECT_TYPE ObjectType
,
847 OUT POBJECT_TYPE_INFORMATION ObjectTypeInfo
,
849 OUT PULONG ReturnLength
)
851 NTSTATUS Status
= STATUS_SUCCESS
;
857 /* Set return length aligned to 4-byte boundary */
858 *ReturnLength
+= sizeof(*ObjectTypeInfo
) +
859 ALIGN_UP(ObjectType
->Name
.MaximumLength
, ULONG
);
861 /* Check if thats too much though. */
862 if (Length
< *ReturnLength
)
864 _SEH2_YIELD(return STATUS_INFO_LENGTH_MISMATCH
);
868 ObjectTypeInfo
->TotalNumberOfHandles
=
869 ObjectType
->TotalNumberOfHandles
;
870 ObjectTypeInfo
->TotalNumberOfObjects
=
871 ObjectType
->TotalNumberOfObjects
;
872 ObjectTypeInfo
->HighWaterNumberOfHandles
=
873 ObjectType
->HighWaterNumberOfHandles
;
874 ObjectTypeInfo
->HighWaterNumberOfObjects
=
875 ObjectType
->HighWaterNumberOfObjects
;
876 ObjectTypeInfo
->PoolType
=
877 ObjectType
->TypeInfo
.PoolType
;
878 ObjectTypeInfo
->DefaultNonPagedPoolCharge
=
879 ObjectType
->TypeInfo
.DefaultNonPagedPoolCharge
;
880 ObjectTypeInfo
->DefaultPagedPoolCharge
=
881 ObjectType
->TypeInfo
.DefaultPagedPoolCharge
;
882 ObjectTypeInfo
->ValidAccessMask
=
883 ObjectType
->TypeInfo
.ValidAccessMask
;
884 ObjectTypeInfo
->SecurityRequired
=
885 ObjectType
->TypeInfo
.SecurityRequired
;
886 ObjectTypeInfo
->InvalidAttributes
=
887 ObjectType
->TypeInfo
.InvalidAttributes
;
888 ObjectTypeInfo
->GenericMapping
=
889 ObjectType
->TypeInfo
.GenericMapping
;
890 ObjectTypeInfo
->MaintainHandleCount
=
891 ObjectType
->TypeInfo
.MaintainHandleCount
;
893 /* Setup the name buffer */
894 InfoBuffer
= (PWSTR
)(ObjectTypeInfo
+ 1);
895 ObjectTypeInfo
->TypeName
.Buffer
= InfoBuffer
;
896 ObjectTypeInfo
->TypeName
.MaximumLength
= ObjectType
->Name
.MaximumLength
;
897 ObjectTypeInfo
->TypeName
.Length
= ObjectType
->Name
.Length
;
900 RtlCopyMemory(InfoBuffer
,
901 ObjectType
->Name
.Buffer
,
902 ObjectType
->Name
.Length
);
904 /* Null-terminate it */
905 (InfoBuffer
)[ObjectType
->Name
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
907 _SEH2_EXCEPT(ExSystemExceptionFilter())
909 /* Otherwise, get the exception code */
910 Status
= _SEH2_GetExceptionCode();
914 /* Return status to caller */
919 /* PUBLIC FUNCTIONS **********************************************************/
923 ObCreateObject(IN KPROCESSOR_MODE ProbeMode OPTIONAL
,
924 IN POBJECT_TYPE Type
,
925 IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL
,
926 IN KPROCESSOR_MODE AccessMode
,
927 IN OUT PVOID ParseContext OPTIONAL
,
929 IN ULONG PagedPoolCharge OPTIONAL
,
930 IN ULONG NonPagedPoolCharge OPTIONAL
,
934 POBJECT_CREATE_INFORMATION ObjectCreateInfo
;
935 UNICODE_STRING ObjectName
;
936 POBJECT_HEADER Header
;
938 /* Allocate a capture buffer */
939 ObjectCreateInfo
= ObpAllocateObjectCreateInfoBuffer(LookasideCreateInfoList
);
940 if (!ObjectCreateInfo
) return STATUS_INSUFFICIENT_RESOURCES
;
942 /* Capture all the info */
943 Status
= ObpCaptureObjectCreateInformation(ObjectAttributes
,
948 if (NT_SUCCESS(Status
))
950 /* Validate attributes */
951 if (Type
->TypeInfo
.InvalidAttributes
& ObjectCreateInfo
->Attributes
)
954 Status
= STATUS_INVALID_PARAMETER
;
958 /* Check if we have a paged charge */
959 if (!PagedPoolCharge
)
962 PagedPoolCharge
= Type
->TypeInfo
.DefaultPagedPoolCharge
;
965 /* Check for nonpaged charge */
966 if (!NonPagedPoolCharge
)
969 NonPagedPoolCharge
= Type
->TypeInfo
.DefaultNonPagedPoolCharge
;
972 /* Write the pool charges */
973 ObjectCreateInfo
->PagedPoolCharge
= PagedPoolCharge
;
974 ObjectCreateInfo
->NonPagedPoolCharge
= NonPagedPoolCharge
;
976 /* Allocate the Object */
977 Status
= ObpAllocateObject(ObjectCreateInfo
,
983 if (NT_SUCCESS(Status
))
985 /* Return the Object */
986 *Object
= &Header
->Body
;
988 /* Check if this is a permanent object */
989 if (Header
->Flags
& OB_FLAG_PERMANENT
)
991 /* Do the privilege check */
992 if (!SeSinglePrivilegeCheck(SeCreatePermanentPrivilege
,
996 ObpDeallocateObject(*Object
);
997 Status
= STATUS_PRIVILEGE_NOT_HELD
;
1006 /* Release the Capture Info, we don't need it */
1007 ObpFreeObjectCreateInformation(ObjectCreateInfo
);
1008 if (ObjectName
.Buffer
) ObpFreeObjectNameBuffer(&ObjectName
);
1011 /* We failed, so release the Buffer */
1012 ObpFreeCapturedAttributes(ObjectCreateInfo
, LookasideCreateInfoList
);
1018 ObCreateObjectType(IN PUNICODE_STRING TypeName
,
1019 IN POBJECT_TYPE_INITIALIZER ObjectTypeInitializer
,
1021 OUT POBJECT_TYPE
*ObjectType
)
1023 POBJECT_HEADER Header
;
1024 POBJECT_TYPE LocalObjectType
;
1028 OBP_LOOKUP_CONTEXT Context
;
1031 UNICODE_STRING ObjectName
;
1032 POBJECT_HEADER_CREATOR_INFO CreatorInfo
;
1034 /* Verify parameters */
1036 !(TypeName
->Length
) ||
1037 (TypeName
->Length
% sizeof(WCHAR
)) ||
1038 !(ObjectTypeInitializer
) ||
1039 (ObjectTypeInitializer
->Length
!= sizeof(*ObjectTypeInitializer
)) ||
1040 (ObjectTypeInitializer
->InvalidAttributes
& ~OBJ_VALID_ATTRIBUTES
) ||
1041 (ObjectTypeInitializer
->MaintainHandleCount
&&
1042 (!(ObjectTypeInitializer
->OpenProcedure
) &&
1043 !ObjectTypeInitializer
->CloseProcedure
)) ||
1044 ((!ObjectTypeInitializer
->UseDefaultObject
) &&
1045 (ObjectTypeInitializer
->PoolType
!= NonPagedPool
)))
1048 return STATUS_INVALID_PARAMETER
;
1051 /* Make sure the name doesn't have a separator */
1052 p
= TypeName
->Buffer
;
1053 i
= TypeName
->Length
/ sizeof(WCHAR
);
1056 /* Check for one and fail */
1057 if (*p
++ == OBJ_NAME_PATH_SEPARATOR
) return STATUS_OBJECT_NAME_INVALID
;
1060 /* Setup a lookup context */
1061 ObpInitializeLookupContext(&Context
);
1063 /* Check if we've already created the directory of types */
1064 if (ObpTypeDirectoryObject
)
1066 /* Acquire the directory lock */
1067 ObpAcquireDirectoryLockExclusive(ObpTypeDirectoryObject
, &Context
);
1070 if (ObpLookupEntryDirectory(ObpTypeDirectoryObject
,
1072 OBJ_CASE_INSENSITIVE
,
1076 /* We have already created it, so fail */
1077 ObpReleaseLookupContext(&Context
);
1078 return STATUS_OBJECT_NAME_COLLISION
;
1082 /* Now make a copy of the object name */
1083 ObjectName
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1084 TypeName
->MaximumLength
,
1086 if (!ObjectName
.Buffer
)
1088 /* Out of memory, fail */
1089 ObpReleaseLookupContext(&Context
);
1090 return STATUS_INSUFFICIENT_RESOURCES
;
1093 /* Set the length and copy the name */
1094 ObjectName
.MaximumLength
= TypeName
->MaximumLength
;
1095 RtlCopyUnicodeString(&ObjectName
, TypeName
);
1097 /* Allocate the Object */
1098 Status
= ObpAllocateObject(NULL
,
1101 sizeof(OBJECT_TYPE
),
1104 if (!NT_SUCCESS(Status
))
1106 /* Free the name and fail */
1107 ObpReleaseLookupContext(&Context
);
1108 ExFreePool(ObjectName
.Buffer
);
1112 /* Setup the flags and name */
1113 LocalObjectType
= (POBJECT_TYPE
)&Header
->Body
;
1114 LocalObjectType
->Name
= ObjectName
;
1115 Header
->Flags
|= OB_FLAG_KERNEL_MODE
| OB_FLAG_PERMANENT
;
1117 /* Clear accounting data */
1118 LocalObjectType
->TotalNumberOfObjects
=
1119 LocalObjectType
->TotalNumberOfHandles
=
1120 LocalObjectType
->HighWaterNumberOfObjects
=
1121 LocalObjectType
->HighWaterNumberOfHandles
= 0;
1123 /* Check if this is the first Object Type */
1124 if (!ObpTypeObjectType
)
1126 /* It is, so set this as the type object */
1127 ObpTypeObjectType
= LocalObjectType
;
1128 Header
->Type
= ObpTypeObjectType
;
1130 /* Set the hard-coded key and object count */
1131 LocalObjectType
->TotalNumberOfObjects
= 1;
1132 LocalObjectType
->Key
= 'TjbO';
1137 Tag
[0] = (CHAR
)TypeName
->Buffer
[0];
1138 Tag
[1] = (CHAR
)TypeName
->Buffer
[1];
1139 Tag
[2] = (CHAR
)TypeName
->Buffer
[2];
1140 Tag
[3] = (CHAR
)TypeName
->Buffer
[3];
1141 LocalObjectType
->Key
= *(PULONG
)Tag
;
1144 /* Set up the type information */
1145 LocalObjectType
->TypeInfo
= *ObjectTypeInitializer
;
1146 LocalObjectType
->TypeInfo
.PoolType
= ObjectTypeInitializer
->PoolType
;
1148 /* Check if we have to maintain a type list */
1149 if (NtGlobalFlag
& FLG_MAINTAIN_OBJECT_TYPELIST
)
1151 /* Enable support */
1152 LocalObjectType
->TypeInfo
.MaintainTypeList
= TRUE
;
1155 /* Calculate how much space our header'll take up */
1156 HeaderSize
= sizeof(OBJECT_HEADER
) +
1157 sizeof(OBJECT_HEADER_NAME_INFO
) +
1158 (ObjectTypeInitializer
->MaintainHandleCount
?
1159 sizeof(OBJECT_HEADER_HANDLE_INFO
) : 0);
1161 /* Check the pool type */
1162 if (ObjectTypeInitializer
->PoolType
== NonPagedPool
)
1164 /* Update the NonPaged Pool charge */
1165 LocalObjectType
->TypeInfo
.DefaultNonPagedPoolCharge
+= HeaderSize
;
1169 /* Update the Paged Pool charge */
1170 LocalObjectType
->TypeInfo
.DefaultPagedPoolCharge
+= HeaderSize
;
1173 /* All objects types need a security procedure */
1174 if (!ObjectTypeInitializer
->SecurityProcedure
)
1176 LocalObjectType
->TypeInfo
.SecurityProcedure
= SeDefaultObjectMethod
;
1179 /* Select the Wait Object */
1180 if (LocalObjectType
->TypeInfo
.UseDefaultObject
)
1182 /* Add the SYNCHRONIZE access mask since it's waitable */
1183 LocalObjectType
->TypeInfo
.ValidAccessMask
|= SYNCHRONIZE
;
1185 /* Use the "Default Object", a simple event */
1186 LocalObjectType
->DefaultObject
= &ObpDefaultObject
;
1188 /* The File Object gets an optimized hack so it can be waited on */
1189 else if ((TypeName
->Length
== 8) && !(wcscmp(TypeName
->Buffer
, L
"File")))
1191 /* Wait on the File Object's event directly */
1192 LocalObjectType
->DefaultObject
= (PVOID
)FIELD_OFFSET(FILE_OBJECT
,
1195 else if ((TypeName
->Length
== 24) && !(wcscmp(TypeName
->Buffer
, L
"WaitablePort")))
1197 /* Wait on the LPC Port's object directly */
1198 LocalObjectType
->DefaultObject
= (PVOID
)FIELD_OFFSET(LPCP_PORT_OBJECT
,
1203 /* No default Object */
1204 LocalObjectType
->DefaultObject
= NULL
;
1207 /* Initialize Object Type components */
1208 ExInitializeResourceLite(&LocalObjectType
->Mutex
);
1209 for (i
= 0; i
< 4; i
++)
1211 /* Initialize the object locks */
1212 ExInitializeResourceLite(&LocalObjectType
->ObjectLocks
[i
]);
1214 InitializeListHead(&LocalObjectType
->TypeList
);
1216 /* Lock the object type */
1217 ObpEnterObjectTypeMutex(LocalObjectType
);
1219 /* Get creator info and insert it into the type list */
1220 CreatorInfo
= OBJECT_HEADER_TO_CREATOR_INFO(Header
);
1221 if (CreatorInfo
) InsertTailList(&ObpTypeObjectType
->TypeList
,
1222 &CreatorInfo
->TypeList
);
1224 /* Set the index and the entry into the object type array */
1225 LocalObjectType
->Index
= ObpTypeObjectType
->TotalNumberOfObjects
;
1226 if (LocalObjectType
->Index
< 32)
1228 /* It fits, insert it */
1229 ObpObjectTypes
[LocalObjectType
->Index
- 1] = LocalObjectType
;
1232 /* Release the object type */
1233 ObpLeaveObjectTypeMutex(LocalObjectType
);
1235 /* Check if we're actually creating the directory object itself */
1236 if (!(ObpTypeDirectoryObject
) ||
1237 (ObpInsertEntryDirectory(ObpTypeDirectoryObject
, &Context
, Header
)))
1239 /* Check if the type directory exists */
1240 if (ObpTypeDirectoryObject
)
1243 ObReferenceObject(ObpTypeDirectoryObject
);
1246 /* Cleanup the lookup context */
1247 ObpReleaseLookupContext(&Context
);
1249 /* Return the object type and success */
1250 *ObjectType
= LocalObjectType
;
1251 return STATUS_SUCCESS
;
1254 /* If we got here, then we failed */
1255 ObpReleaseLookupContext(&Context
);
1256 return STATUS_INSUFFICIENT_RESOURCES
;
1261 ObpDeleteObjectType(IN PVOID Object
)
1264 POBJECT_TYPE ObjectType
= (PVOID
)Object
;
1266 /* Loop our locks */
1267 for (i
= 0; i
< 4; i
++)
1269 /* Delete each one */
1270 ExDeleteResourceLite(&ObjectType
->ObjectLocks
[i
]);
1273 /* Delete our main mutex */
1274 ExDeleteResourceLite(&ObjectType
->Mutex
);
1278 * @name ObMakeTemporaryObject
1281 * The ObMakeTemporaryObject routine <FILLMEIN>
1293 ObMakeTemporaryObject(IN PVOID ObjectBody
)
1297 /* Call the internal API */
1298 ObpSetPermanentObject(ObjectBody
, FALSE
);
1302 * @name NtMakeTemporaryObject
1305 * The NtMakeTemporaryObject routine <FILLMEIN>
1307 * @param ObjectHandle
1310 * @return STATUS_SUCCESS or appropriate error value.
1317 NtMakeTemporaryObject(IN HANDLE ObjectHandle
)
1323 /* Reference the object for DELETE access */
1324 Status
= ObReferenceObjectByHandle(ObjectHandle
,
1327 KeGetPreviousMode(),
1330 if (Status
!= STATUS_SUCCESS
) return Status
;
1332 /* Set it as temporary and dereference it */
1333 ObpSetPermanentObject(ObjectBody
, FALSE
);
1334 ObDereferenceObject(ObjectBody
);
1335 return STATUS_SUCCESS
;
1339 * @name NtMakePermanentObject
1342 * The NtMakePermanentObject routine <FILLMEIN>
1344 * @param ObjectHandle
1347 * @return STATUS_SUCCESS or appropriate error value.
1354 NtMakePermanentObject(IN HANDLE ObjectHandle
)
1358 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
1361 /* Make sure that the caller has SeCreatePermanentPrivilege */
1362 Status
= SeSinglePrivilegeCheck(SeCreatePermanentPrivilege
,
1364 if (!NT_SUCCESS(Status
)) return STATUS_PRIVILEGE_NOT_HELD
;
1366 /* Reference the object */
1367 Status
= ObReferenceObjectByHandle(ObjectHandle
,
1373 if (Status
!= STATUS_SUCCESS
) return Status
;
1375 /* Set it as permanent and dereference it */
1376 ObpSetPermanentObject(ObjectBody
, TRUE
);
1377 ObDereferenceObject(ObjectBody
);
1378 return STATUS_SUCCESS
;
1382 * @name NtQueryObject
1385 * The NtQueryObject routine <FILLMEIN>
1387 * @param ObjectHandle
1390 * @param ObjectInformationClass
1393 * @param ObjectInformation
1399 * @param ResultLength
1402 * @return STATUS_SUCCESS or appropriate error value.
1409 NtQueryObject(IN HANDLE ObjectHandle
,
1410 IN OBJECT_INFORMATION_CLASS ObjectInformationClass
,
1411 OUT PVOID ObjectInformation
,
1413 OUT PULONG ResultLength OPTIONAL
)
1415 OBJECT_HANDLE_INFORMATION HandleInfo
;
1416 POBJECT_HEADER ObjectHeader
= NULL
;
1417 POBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleFlags
;
1418 POBJECT_BASIC_INFORMATION BasicInfo
;
1420 PVOID Object
= NULL
;
1422 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
1425 /* Check if the caller is from user mode */
1426 if (PreviousMode
!= KernelMode
)
1428 /* Protect validation with SEH */
1431 /* Probe the input structure */
1432 ProbeForWrite(ObjectInformation
, Length
, sizeof(UCHAR
));
1434 /* If we have a result length, probe it too */
1435 if (ResultLength
) ProbeForWriteUlong(ResultLength
);
1437 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1439 /* Return the exception code */
1440 _SEH2_YIELD(return _SEH2_GetExceptionCode());
1446 * Make sure this isn't a generic type query, since the caller doesn't
1447 * have to give a handle for it
1449 if (ObjectInformationClass
!= ObjectTypesInformation
)
1451 /* Reference the object */
1452 Status
= ObReferenceObjectByHandle(ObjectHandle
,
1455 KeGetPreviousMode(),
1458 if (!NT_SUCCESS (Status
)) return Status
;
1460 /* Get the object header */
1461 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(Object
);
1466 /* Check the information class */
1467 switch (ObjectInformationClass
)
1470 case ObjectBasicInformation
:
1472 /* Validate length */
1473 InfoLength
= sizeof(OBJECT_BASIC_INFORMATION
);
1474 if (Length
!= sizeof(OBJECT_BASIC_INFORMATION
))
1477 Status
= STATUS_INFO_LENGTH_MISMATCH
;
1481 /* Fill out the basic information */
1482 BasicInfo
= (POBJECT_BASIC_INFORMATION
)ObjectInformation
;
1483 BasicInfo
->Attributes
= HandleInfo
.HandleAttributes
;
1484 BasicInfo
->GrantedAccess
= HandleInfo
.GrantedAccess
;
1485 BasicInfo
->HandleCount
= ObjectHeader
->HandleCount
;
1486 BasicInfo
->PointerCount
= ObjectHeader
->PointerCount
;
1488 /* Permanent/Exclusive Flags are NOT in Handle attributes! */
1489 if (ObjectHeader
->Flags
& OB_FLAG_EXCLUSIVE
)
1492 BasicInfo
->Attributes
|= OBJ_EXCLUSIVE
;
1494 if (ObjectHeader
->Flags
& OB_FLAG_PERMANENT
)
1497 BasicInfo
->Attributes
|= OBJ_PERMANENT
;
1500 /* Copy quota information */
1501 BasicInfo
->PagedPoolCharge
= 0; /* FIXME*/
1502 BasicInfo
->NonPagedPoolCharge
= 0; /* FIXME*/
1504 /* Copy name information */
1505 BasicInfo
->NameInfoSize
= 0; /* FIXME*/
1506 BasicInfo
->TypeInfoSize
= 0; /* FIXME*/
1508 /* Copy security information */
1509 BasicInfo
->SecurityDescriptorSize
= 0; /* FIXME*/
1511 /* Check if this is a symlink */
1512 if (ObjectHeader
->Type
== ObSymbolicLinkType
)
1514 /* Return the creation time */
1515 BasicInfo
->CreationTime
.QuadPart
=
1516 ((POBJECT_SYMBOLIC_LINK
)Object
)->CreationTime
.QuadPart
;
1520 /* Otherwise return 0 */
1521 BasicInfo
->CreationTime
.QuadPart
= (ULONGLONG
)0;
1524 /* Break out with success */
1525 Status
= STATUS_SUCCESS
;
1528 /* Name information */
1529 case ObjectNameInformation
:
1531 /* Call the helper and break out */
1532 Status
= ObQueryNameString(Object
,
1533 (POBJECT_NAME_INFORMATION
)
1539 /* Information about this type */
1540 case ObjectTypeInformation
:
1542 /* Call the helper and break out */
1543 Status
= ObQueryTypeInfo(ObjectHeader
->Type
,
1544 (POBJECT_TYPE_INFORMATION
)
1550 /* Information about all types */
1551 case ObjectTypesInformation
:
1552 DPRINT1("NOT IMPLEMENTED!\n");
1553 InfoLength
= Length
;
1554 Status
= STATUS_NOT_IMPLEMENTED
;
1557 /* Information about the handle flags */
1558 case ObjectHandleFlagInformation
:
1560 /* Validate length */
1561 InfoLength
= sizeof (OBJECT_HANDLE_ATTRIBUTE_INFORMATION
);
1562 if (Length
!= sizeof (OBJECT_HANDLE_ATTRIBUTE_INFORMATION
))
1564 Status
= STATUS_INFO_LENGTH_MISMATCH
;
1568 /* Get the structure */
1569 HandleFlags
= (POBJECT_HANDLE_ATTRIBUTE_INFORMATION
)
1573 HandleFlags
->Inherit
= HandleInfo
.HandleAttributes
& OBJ_INHERIT
;
1574 HandleFlags
->ProtectFromClose
= (HandleInfo
.HandleAttributes
&
1575 OBJ_PROTECT_CLOSE
) != 0;
1577 /* Break out with success */
1578 Status
= STATUS_SUCCESS
;
1585 InfoLength
= Length
;
1586 Status
= STATUS_INVALID_INFO_CLASS
;
1590 /* Check if the caller wanted the return length */
1593 /* Write the length */
1594 *ResultLength
= InfoLength
;
1597 _SEH2_EXCEPT(ExSystemExceptionFilter())
1599 /* Otherwise, get the exception code */
1600 Status
= _SEH2_GetExceptionCode();
1604 /* Dereference the object if we had referenced it */
1605 if (Object
) ObDereferenceObject (Object
);
1612 * @name NtSetInformationObject
1615 * The NtSetInformationObject routine <FILLMEIN>
1617 * @param ObjectHandle
1620 * @param ObjectInformationClass
1623 * @param ObjectInformation
1629 * @return STATUS_SUCCESS or appropriate error value.
1636 NtSetInformationObject(IN HANDLE ObjectHandle
,
1637 IN OBJECT_INFORMATION_CLASS ObjectInformationClass
,
1638 IN PVOID ObjectInformation
,
1642 OBP_SET_HANDLE_ATTRIBUTES_CONTEXT Context
;
1644 KAPC_STATE ApcState
;
1645 BOOLEAN AttachedToProcess
= FALSE
;
1648 /* Validate the information class */
1649 if (ObjectInformationClass
!= ObjectHandleFlagInformation
)
1652 return STATUS_INVALID_INFO_CLASS
;
1655 /* Validate the length */
1656 if (Length
!= sizeof (OBJECT_HANDLE_ATTRIBUTE_INFORMATION
))
1658 /* Invalid length */
1659 return STATUS_INFO_LENGTH_MISMATCH
;
1662 /* Save the previous mode */
1663 Context
.PreviousMode
= ExGetPreviousMode();
1665 /* Check if we were called from user mode */
1666 if (Context
.PreviousMode
!= KernelMode
)
1671 /* Probe and capture the attribute buffer */
1672 ProbeForRead(ObjectInformation
,
1673 sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION
),
1675 Context
.Information
= *(POBJECT_HANDLE_ATTRIBUTE_INFORMATION
)
1678 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1680 /* Return the exception code */
1681 _SEH2_YIELD(return _SEH2_GetExceptionCode());
1687 /* Just copy the buffer directly */
1688 Context
.Information
= *(POBJECT_HANDLE_ATTRIBUTE_INFORMATION
)
1692 /* Check if this is a kernel handle */
1693 if (ObIsKernelHandle(ObjectHandle
, Context
.PreviousMode
))
1695 /* Get the actual handle */
1696 ObjectHandle
= ObKernelHandleToHandle(ObjectHandle
);
1697 ObjectTable
= ObpKernelHandleTable
;
1699 /* Check if we're not in the system process */
1700 if (PsGetCurrentProcess() != PsInitialSystemProcess
)
1703 KeStackAttachProcess(&PsInitialSystemProcess
->Pcb
, &ApcState
);
1704 AttachedToProcess
= TRUE
;
1709 /* Use the current table */
1710 ObjectTable
= PsGetCurrentProcess()->ObjectTable
;
1713 /* Change the handle attributes */
1714 if (!ExChangeHandle(ObjectTable
,
1716 ObpSetHandleAttributes
,
1717 (ULONG_PTR
)&Context
))
1720 Status
= STATUS_ACCESS_DENIED
;
1725 Status
= STATUS_SUCCESS
;
1728 /* De-attach if we were attached, and return status */
1729 if (AttachedToProcess
) KeUnstackDetachProcess(&ApcState
);