3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ob/object.c
6 * PURPOSE: Implements generic object managment functions
8 * PROGRAMMERS: David Welch (welch@cwcom.net)
9 * Skywing (skywing@valhallalegends.com)
12 /* INCLUDES *****************************************************************/
16 #include <internal/debug.h>
19 typedef struct _RETENTION_CHECK_PARAMS
21 WORK_QUEUE_ITEM WorkItem
;
22 POBJECT_HEADER ObjectHeader
;
23 } RETENTION_CHECK_PARAMS
, *PRETENTION_CHECK_PARAMS
;
25 /* TEMPORARY HACK. DO NOT REMOVE -- Alex */
28 ExpDesktopCreate(PVOID ObjectBody
,
31 struct _OBJECT_ATTRIBUTES
* ObjectAttributes
);
32 /* FUNCTIONS ************************************************************/
35 ObpCaptureObjectAttributes(IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL
,
36 IN KPROCESSOR_MODE AccessMode
,
37 IN POOL_TYPE PoolType
,
38 IN BOOLEAN CaptureIfKernel
,
39 OUT PCAPTURED_OBJECT_ATTRIBUTES CapturedObjectAttributes OPTIONAL
,
40 OUT PUNICODE_STRING ObjectName OPTIONAL
)
42 OBJECT_ATTRIBUTES AttributesCopy
;
43 NTSTATUS Status
= STATUS_SUCCESS
;
45 /* at least one output parameter must be != NULL! */
46 ASSERT(CapturedObjectAttributes
!= NULL
|| ObjectName
!= NULL
);
48 if(ObjectAttributes
== NULL
)
50 /* we're going to return STATUS_SUCCESS! */
51 goto failbasiccleanup
;
54 if(AccessMode
!= KernelMode
)
56 RtlZeroMemory(&AttributesCopy
, sizeof(AttributesCopy
));
60 ProbeForRead(ObjectAttributes
,
61 sizeof(ObjectAttributes
),
63 /* make a copy on the stack */
64 AttributesCopy
= *ObjectAttributes
;
68 Status
= _SEH_GetExceptionCode();
72 if(!NT_SUCCESS(Status
))
74 DPRINT1("ObpCaptureObjectAttributes failed to probe object attributes\n");
75 goto failbasiccleanup
;
78 else if(!CaptureIfKernel
)
80 if(ObjectAttributes
->Length
== sizeof(OBJECT_ATTRIBUTES
))
82 if(ObjectName
!= NULL
)
84 /* we don't have to capture any memory, the caller considers the passed data
86 if(ObjectAttributes
->ObjectName
!= NULL
)
88 *ObjectName
= *ObjectAttributes
->ObjectName
;
92 ObjectName
->Length
= ObjectName
->MaximumLength
= 0;
93 ObjectName
->Buffer
= NULL
;
96 if(CapturedObjectAttributes
!= NULL
)
98 CapturedObjectAttributes
->RootDirectory
= ObjectAttributes
->RootDirectory
;
99 CapturedObjectAttributes
->Attributes
= ObjectAttributes
->Attributes
;
100 CapturedObjectAttributes
->SecurityDescriptor
= ObjectAttributes
->SecurityDescriptor
;
101 CapturedObjectAttributes
->SecurityQualityOfService
= ObjectAttributes
->SecurityQualityOfService
;
104 return STATUS_SUCCESS
;
108 Status
= STATUS_INVALID_PARAMETER
;
109 goto failbasiccleanup
;
114 AttributesCopy
= *ObjectAttributes
;
117 /* if Length isn't as expected, bail with an invalid parameter status code so
118 the caller knows he passed garbage... */
119 if(AttributesCopy
.Length
!= sizeof(OBJECT_ATTRIBUTES
))
121 Status
= STATUS_INVALID_PARAMETER
;
122 goto failbasiccleanup
;
125 if(CapturedObjectAttributes
!= NULL
)
127 CapturedObjectAttributes
->RootDirectory
= AttributesCopy
.RootDirectory
;
128 CapturedObjectAttributes
->Attributes
= AttributesCopy
.Attributes
;
130 if(AttributesCopy
.SecurityDescriptor
!= NULL
)
132 Status
= SeCaptureSecurityDescriptor(AttributesCopy
.SecurityDescriptor
,
136 &CapturedObjectAttributes
->SecurityDescriptor
);
137 if(!NT_SUCCESS(Status
))
139 DPRINT1("Unable to capture the security descriptor!!!\n");
140 goto failbasiccleanup
;
145 CapturedObjectAttributes
->SecurityDescriptor
= NULL
;
148 if(AttributesCopy
.SecurityQualityOfService
!= NULL
)
150 SECURITY_QUALITY_OF_SERVICE SafeQoS
;
152 RtlZeroMemory(&SafeQoS
, sizeof(SafeQoS
));
156 ProbeForRead(AttributesCopy
.SecurityQualityOfService
,
157 sizeof(SECURITY_QUALITY_OF_SERVICE
),
159 SafeQoS
= *(PSECURITY_QUALITY_OF_SERVICE
)AttributesCopy
.SecurityQualityOfService
;
163 Status
= _SEH_GetExceptionCode();
167 if(!NT_SUCCESS(Status
))
169 DPRINT1("Unable to capture QoS!!!\n");
170 goto failcleanupsdescriptor
;
173 if(SafeQoS
.Length
!= sizeof(SECURITY_QUALITY_OF_SERVICE
))
175 DPRINT1("Unable to capture QoS, wrong size!!!\n");
176 Status
= STATUS_INVALID_PARAMETER
;
177 goto failcleanupsdescriptor
;
180 CapturedObjectAttributes
->SecurityQualityOfService
= ExAllocatePool(PoolType
,
181 sizeof(SECURITY_QUALITY_OF_SERVICE
));
182 if(CapturedObjectAttributes
->SecurityQualityOfService
!= NULL
)
184 *CapturedObjectAttributes
->SecurityQualityOfService
= SafeQoS
;
188 Status
= STATUS_INSUFFICIENT_RESOURCES
;
189 goto failcleanupsdescriptor
;
194 CapturedObjectAttributes
->SecurityQualityOfService
= NULL
;
198 if(ObjectName
!= NULL
)
200 if(AttributesCopy
.ObjectName
!= NULL
)
202 UNICODE_STRING OriginalCopy
;
204 if(AccessMode
!= KernelMode
)
206 RtlZeroMemory(&OriginalCopy
, sizeof(OriginalCopy
));
210 /* probe the ObjectName structure and make a local stack copy of it */
211 ProbeForRead(AttributesCopy
.ObjectName
,
212 sizeof(UNICODE_STRING
),
214 OriginalCopy
= *AttributesCopy
.ObjectName
;
215 if(OriginalCopy
.Length
> 0)
217 ProbeForRead(OriginalCopy
.Buffer
,
224 Status
= _SEH_GetExceptionCode();
228 if(NT_SUCCESS(Status
))
230 if(OriginalCopy
.Length
> 0)
232 ObjectName
->MaximumLength
= OriginalCopy
.Length
+ sizeof(WCHAR
);
233 ObjectName
->Buffer
= ExAllocatePool(PoolType
,
234 ObjectName
->MaximumLength
);
235 if(ObjectName
->Buffer
!= NULL
)
239 /* no need to probe OriginalCopy.Buffer again, we already did that
240 when capturing the UNICODE_STRING structure itself */
241 RtlCopyMemory(ObjectName
->Buffer
, OriginalCopy
.Buffer
, OriginalCopy
.Length
);
242 ObjectName
->Buffer
[OriginalCopy
.Length
/ sizeof(WCHAR
)] = L
'\0';
246 Status
= _SEH_GetExceptionCode();
250 if(!NT_SUCCESS(Status
))
252 DPRINT1("ObpCaptureObjectAttributes failed to copy the unicode string!\n");
257 Status
= STATUS_INSUFFICIENT_RESOURCES
;
260 else if(AttributesCopy
.RootDirectory
!= NULL
/* && OriginalCopy.Length == 0 */)
262 /* if the caller specified a root directory, there must be an object name! */
263 Status
= STATUS_OBJECT_NAME_INVALID
;
268 DPRINT1("ObpCaptureObjectAttributes failed to probe the object name UNICODE_STRING structure!\n");
271 else /* AccessMode == KernelMode */
273 OriginalCopy
= *AttributesCopy
.ObjectName
;
275 if(OriginalCopy
.Length
> 0)
277 ObjectName
->MaximumLength
= OriginalCopy
.Length
+ sizeof(WCHAR
);
278 ObjectName
->Buffer
= ExAllocatePool(PoolType
,
279 ObjectName
->MaximumLength
);
280 if(ObjectName
->Buffer
!= NULL
)
282 RtlCopyMemory(ObjectName
->Buffer
, OriginalCopy
.Buffer
, OriginalCopy
.Length
);
283 ObjectName
->Buffer
[OriginalCopy
.Length
/ sizeof(WCHAR
)] = L
'\0';
287 Status
= STATUS_INSUFFICIENT_RESOURCES
;
290 else if(AttributesCopy
.RootDirectory
!= NULL
/* && OriginalCopy.Length == 0 */)
292 /* if the caller specified a root directory, there must be an object name! */
293 Status
= STATUS_OBJECT_NAME_INVALID
;
299 ObjectName
->Length
= ObjectName
->MaximumLength
= 0;
300 ObjectName
->Buffer
= NULL
;
304 if(!NT_SUCCESS(Status
))
306 if(ObjectName
->Buffer
)
308 ExFreePool(ObjectName
->Buffer
);
311 failcleanupsdescriptor
:
312 if(CapturedObjectAttributes
!= NULL
)
314 /* cleanup allocated resources */
315 SeReleaseSecurityDescriptor(CapturedObjectAttributes
->SecurityDescriptor
,
321 if(ObjectName
!= NULL
)
323 ObjectName
->Length
= ObjectName
->MaximumLength
= 0;
324 ObjectName
->Buffer
= NULL
;
326 if(CapturedObjectAttributes
!= NULL
)
328 RtlZeroMemory(CapturedObjectAttributes
, sizeof(CAPTURED_OBJECT_ATTRIBUTES
));
337 ObpReleaseObjectAttributes(IN PCAPTURED_OBJECT_ATTRIBUTES CapturedObjectAttributes OPTIONAL
,
338 IN PUNICODE_STRING ObjectName OPTIONAL
,
339 IN KPROCESSOR_MODE AccessMode
,
340 IN BOOLEAN CaptureIfKernel
)
342 /* WARNING - You need to pass the same parameters to this function as you passed
343 to ObpCaptureObjectAttributes() to avoid memory leaks */
344 if(AccessMode
!= KernelMode
|| CaptureIfKernel
)
346 if(CapturedObjectAttributes
!= NULL
)
348 if(CapturedObjectAttributes
->SecurityDescriptor
!= NULL
)
350 ExFreePool(CapturedObjectAttributes
->SecurityDescriptor
);
351 CapturedObjectAttributes
->SecurityDescriptor
= NULL
;
353 if(CapturedObjectAttributes
->SecurityQualityOfService
!= NULL
)
355 ExFreePool(CapturedObjectAttributes
->SecurityQualityOfService
);
356 CapturedObjectAttributes
->SecurityQualityOfService
= NULL
;
359 if(ObjectName
!= NULL
&&
360 ObjectName
->Length
> 0)
362 ExFreePool(ObjectName
->Buffer
);
368 /**********************************************************************
380 * Pointer to a unicode string that will contain the
381 * remaining path if the function returns successfully.
382 * The caller must free the buffer after use by calling
383 * RtlFreeUnicodeString ().
386 * Optional pointer to an object type. This is used to
387 * descide if a symbolic link object will be parsed or not.
392 ObFindObject(POBJECT_ATTRIBUTES ObjectAttributes
,
393 PVOID
* ReturnedObject
,
394 PUNICODE_STRING RemainingPath
,
395 POBJECT_TYPE ObjectType
)
400 POBJECT_HEADER CurrentHeader
;
403 UNICODE_STRING PathString
;
405 PUNICODE_STRING ObjectName
;
409 DPRINT("ObFindObject(ObjectAttributes %x, ReturnedObject %x, "
410 "RemainingPath %x)\n",ObjectAttributes
,ReturnedObject
,RemainingPath
);
411 DPRINT("ObjectAttributes->ObjectName %wZ\n",
412 ObjectAttributes
->ObjectName
);
414 RtlInitUnicodeString (RemainingPath
, NULL
);
416 if (ObjectAttributes
->RootDirectory
== NULL
)
418 ObReferenceObjectByPointer(NameSpaceRoot
,
422 CurrentObject
= NameSpaceRoot
;
426 Status
= ObReferenceObjectByHandle(ObjectAttributes
->RootDirectory
,
432 if (!NT_SUCCESS(Status
))
438 ObjectName
= ObjectAttributes
->ObjectName
;
439 if (ObjectName
->Length
== 0 ||
440 ObjectName
->Buffer
[0] == UNICODE_NULL
)
442 *ReturnedObject
= CurrentObject
;
443 return STATUS_SUCCESS
;
446 if (ObjectAttributes
->RootDirectory
== NULL
&&
447 ObjectName
->Buffer
[0] != L
'\\')
449 ObDereferenceObject (CurrentObject
);
450 return STATUS_UNSUCCESSFUL
;
453 /* Create a zero-terminated copy of the object name */
454 PathString
.Length
= ObjectName
->Length
;
455 PathString
.MaximumLength
= ObjectName
->Length
+ sizeof(WCHAR
);
456 PathString
.Buffer
= ExAllocatePool (NonPagedPool
,
457 PathString
.MaximumLength
);
458 if (PathString
.Buffer
== NULL
)
460 ObDereferenceObject (CurrentObject
);
461 return STATUS_INSUFFICIENT_RESOURCES
;
464 RtlCopyMemory (PathString
.Buffer
,
467 PathString
.Buffer
[PathString
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
469 current
= PathString
.Buffer
;
471 RootObject
= CurrentObject
;
472 Attributes
= ObjectAttributes
->Attributes
;
473 if (ObjectType
== ObSymbolicLinkType
)
474 Attributes
|= OBJ_OPENLINK
;
478 DPRINT("current %S\n",current
);
479 CurrentHeader
= BODY_TO_HEADER(CurrentObject
);
481 DPRINT("Current ObjectType %wZ\n",
482 &CurrentHeader
->ObjectType
->TypeName
);
484 if (CurrentHeader
->ObjectType
->Parse
== NULL
)
486 DPRINT("Current object can't parse\n");
489 Status
= CurrentHeader
->ObjectType
->Parse(CurrentObject
,
494 if (Status
== STATUS_REPARSE
)
496 /* reparse the object path */
497 NextObject
= NameSpaceRoot
;
498 current
= PathString
.Buffer
;
500 ObReferenceObjectByPointer(NextObject
,
506 if (NextObject
== NULL
)
510 ObDereferenceObject(CurrentObject
);
511 CurrentObject
= NextObject
;
515 RtlpCreateUnicodeString (RemainingPath
, current
, NonPagedPool
);
516 RtlFreeUnicodeString (&PathString
);
517 *ReturnedObject
= CurrentObject
;
519 return STATUS_SUCCESS
;
523 /**********************************************************************
525 * ObQueryNameString@16
536 ObQueryNameString (IN PVOID Object
,
537 OUT POBJECT_NAME_INFORMATION ObjectNameInfo
,
539 OUT PULONG ReturnLength
)
541 POBJECT_NAME_INFORMATION LocalInfo
;
542 POBJECT_HEADER ObjectHeader
;
543 ULONG LocalReturnLength
;
550 if (Length
< sizeof(OBJECT_NAME_INFORMATION
) + sizeof(WCHAR
))
551 return STATUS_INVALID_BUFFER_SIZE
;
553 ObjectNameInfo
->Name
.MaximumLength
= (USHORT
)(Length
- sizeof(OBJECT_NAME_INFORMATION
));
554 ObjectNameInfo
->Name
.Length
= 0;
555 ObjectNameInfo
->Name
.Buffer
=
556 (PWCHAR
)((ULONG_PTR
)ObjectNameInfo
+ sizeof(OBJECT_NAME_INFORMATION
));
557 ObjectNameInfo
->Name
.Buffer
[0] = 0;
559 ObjectHeader
= BODY_TO_HEADER(Object
);
561 if (ObjectHeader
->ObjectType
!= NULL
&&
562 ObjectHeader
->ObjectType
->QueryName
!= NULL
)
564 DPRINT ("Calling %x\n", ObjectHeader
->ObjectType
->QueryName
);
565 Status
= ObjectHeader
->ObjectType
->QueryName (Object
,
570 else if (ObjectHeader
->Name
.Length
> 0 && ObjectHeader
->Name
.Buffer
!= NULL
)
572 DPRINT ("Object does not have a 'QueryName' function\n");
574 if (ObjectHeader
->Parent
== NameSpaceRoot
)
576 DPRINT ("Reached the root directory\n");
577 ObjectNameInfo
->Name
.Length
= 0;
578 ObjectNameInfo
->Name
.Buffer
[0] = 0;
579 Status
= STATUS_SUCCESS
;
581 else if (ObjectHeader
->Parent
!= NULL
)
583 LocalInfo
= ExAllocatePool (NonPagedPool
,
584 sizeof(OBJECT_NAME_INFORMATION
) +
585 MAX_PATH
* sizeof(WCHAR
));
586 if (LocalInfo
== NULL
)
587 return STATUS_INSUFFICIENT_RESOURCES
;
589 Status
= ObQueryNameString (ObjectHeader
->Parent
,
591 MAX_PATH
* sizeof(WCHAR
),
593 if (!NT_SUCCESS (Status
))
595 ExFreePool (LocalInfo
);
599 Status
= RtlAppendUnicodeStringToString (&ObjectNameInfo
->Name
,
602 ExFreePool (LocalInfo
);
604 if (!NT_SUCCESS (Status
))
608 DPRINT ("Object path %wZ\n", &ObjectHeader
->Name
);
609 Status
= RtlAppendUnicodeToString (&ObjectNameInfo
->Name
,
611 if (!NT_SUCCESS (Status
))
614 Status
= RtlAppendUnicodeStringToString (&ObjectNameInfo
->Name
,
615 &ObjectHeader
->Name
);
619 DPRINT ("Object is unnamed\n");
621 ObjectNameInfo
->Name
.MaximumLength
= 0;
622 ObjectNameInfo
->Name
.Length
= 0;
623 ObjectNameInfo
->Name
.Buffer
= NULL
;
625 Status
= STATUS_SUCCESS
;
628 if (NT_SUCCESS (Status
))
630 ObjectNameInfo
->Name
.MaximumLength
=
631 (ObjectNameInfo
->Name
.Length
) ? ObjectNameInfo
->Name
.Length
+ sizeof(WCHAR
) : 0;
633 sizeof(OBJECT_NAME_INFORMATION
) + ObjectNameInfo
->Name
.MaximumLength
;
634 DPRINT ("Returned object path: %wZ\n", &ObjectNameInfo
->Name
);
641 /**********************************************************************
655 ObCreateObject (IN KPROCESSOR_MODE ObjectAttributesAccessMode OPTIONAL
,
656 IN POBJECT_TYPE Type
,
657 IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL
,
658 IN KPROCESSOR_MODE AccessMode
,
659 IN OUT PVOID ParseContext OPTIONAL
,
661 IN ULONG PagedPoolCharge OPTIONAL
,
662 IN ULONG NonPagedPoolCharge OPTIONAL
,
666 UNICODE_STRING RemainingPath
;
667 POBJECT_HEADER Header
;
668 POBJECT_HEADER ParentHeader
= NULL
;
670 BOOLEAN ObjectAttached
= FALSE
;
672 PSECURITY_DESCRIPTOR NewSecurityDescriptor
= NULL
;
673 SECURITY_SUBJECT_CONTEXT SubjectContext
;
677 if(ObjectAttributesAccessMode
== UserMode
&& ObjectAttributes
!= NULL
)
679 Status
= STATUS_SUCCESS
;
682 ProbeForRead(ObjectAttributes
,
683 sizeof(OBJECT_ATTRIBUTES
),
688 Status
= _SEH_GetExceptionCode();
692 if(!NT_SUCCESS(Status
))
698 DPRINT("ObCreateObject(Type %p ObjectAttributes %p, Object %p)\n",
699 Type
, ObjectAttributes
, Object
);
703 DPRINT1("Invalid object type!\n");
704 return STATUS_INVALID_PARAMETER
;
707 if (ObjectAttributes
!= NULL
&&
708 ObjectAttributes
->ObjectName
!= NULL
&&
709 ObjectAttributes
->ObjectName
->Buffer
!= NULL
)
711 Status
= ObFindObject(ObjectAttributes
,
715 if (!NT_SUCCESS(Status
))
717 DPRINT1("ObFindObject() failed! (Status 0x%x)\n", Status
);
722 ParentHeader
= BODY_TO_HEADER(Parent
);
725 RemainingPath
.Buffer
== NULL
)
727 if (ParentHeader
->ObjectType
!= Type
728 || !(ObjectAttributes
->Attributes
& OBJ_OPENIF
))
730 ObDereferenceObject(Parent
);
731 return STATUS_OBJECT_NAME_COLLISION
;
734 return STATUS_OBJECT_EXISTS
;
739 RtlInitUnicodeString(&RemainingPath
, NULL
);
742 DPRINT("Allocating memory\n");
743 Header
= (POBJECT_HEADER
)ExAllocatePoolWithTag(NonPagedPool
,
744 OBJECT_ALLOC_SIZE(ObjectSize
),
746 if (Header
== NULL
) {
747 DPRINT1("Not enough memory!\n");
748 return STATUS_INSUFFICIENT_RESOURCES
;
751 RtlZeroMemory(Header
, OBJECT_ALLOC_SIZE(ObjectSize
));
753 /* Initialize the object header */
754 DPRINT("Initalizing header 0x%x (%wZ)\n", Header
, &Type
->TypeName
);
755 Header
->HandleCount
= 0;
756 Header
->RefCount
= 1;
757 Header
->ObjectType
= Type
;
758 if (ObjectAttributes
!= NULL
&&
759 ObjectAttributes
->Attributes
& OBJ_PERMANENT
)
761 Header
->Permanent
= TRUE
;
765 Header
->Permanent
= FALSE
;
768 if (ObjectAttributes
!= NULL
&&
769 ObjectAttributes
->Attributes
& OBJ_INHERIT
)
771 Header
->Inherit
= TRUE
;
775 Header
->Inherit
= FALSE
;
778 RtlInitUnicodeString(&(Header
->Name
),NULL
);
780 DPRINT("Getting Parent and adding entry\n");
781 if (ParentHeader
!= NULL
&&
782 ParentHeader
->ObjectType
== ObDirectoryType
&&
783 RemainingPath
.Buffer
!= NULL
)
785 NamePtr
= RemainingPath
.Buffer
;
786 if (*NamePtr
== L
'\\')
789 ObpAddEntryDirectory(Parent
,
793 ObjectAttached
= TRUE
;
796 if ((Header
->ObjectType
== IoFileObjectType
) ||
797 (Header
->ObjectType
== ExDesktopObjectType
) ||
798 (Header
->ObjectType
->Open
!= NULL
))
800 DPRINT("About to call Open Routine\n");
801 if (Header
->ObjectType
== IoFileObjectType
)
803 /* TEMPORARY HACK. DO NOT TOUCH -- Alex */
804 DPRINT("Calling IopCreateFile\n");
805 Status
= IopCreateFile(HEADER_TO_BODY(Header
),
807 RemainingPath
.Buffer
,
810 else if (Header
->ObjectType
== ExDesktopObjectType
)
812 /* TEMPORARY HACK. DO NOT TOUCH -- Alex */
813 DPRINT("Calling ExpDesktopCreate\n");
814 Status
= ExpDesktopCreate(HEADER_TO_BODY(Header
),
816 RemainingPath
.Buffer
,
819 else if (Header
->ObjectType
->Open
!= NULL
)
821 DPRINT("Calling %x\n", Header
->ObjectType
->Open
);
822 Status
= Header
->ObjectType
->Open(ObCreateHandle
,
823 HEADER_TO_BODY(Header
),
829 if (!NT_SUCCESS(Status
))
831 if (ObjectAttached
== TRUE
)
833 ObpRemoveEntryDirectory(Header
);
837 ObDereferenceObject(Parent
);
839 RtlFreeUnicodeString(&Header
->Name
);
840 RtlFreeUnicodeString(&RemainingPath
);
842 DPRINT("Create Failed\n");
847 RtlFreeUnicodeString(&RemainingPath
);
849 SeCaptureSubjectContext(&SubjectContext
);
851 DPRINT("Security Assignment in progress\n");
852 /* Build the new security descriptor */
853 Status
= SeAssignSecurity((ParentHeader
!= NULL
) ? ParentHeader
->SecurityDescriptor
: NULL
,
854 (ObjectAttributes
!= NULL
) ? ObjectAttributes
->SecurityDescriptor
: NULL
,
855 &NewSecurityDescriptor
,
856 (Header
->ObjectType
== ObDirectoryType
),
858 Header
->ObjectType
->Mapping
,
860 if (NT_SUCCESS(Status
))
862 DPRINT("NewSecurityDescriptor %p\n", NewSecurityDescriptor
);
864 if (Header
->ObjectType
->Security
!= NULL
)
866 /* Call the security method */
867 Status
= Header
->ObjectType
->Security(HEADER_TO_BODY(Header
),
868 AssignSecurityDescriptor
,
870 NewSecurityDescriptor
,
875 /* Assign the security descriptor to the object header */
876 Status
= ObpAddSecurityDescriptor(NewSecurityDescriptor
,
877 &Header
->SecurityDescriptor
);
878 DPRINT("Object security descriptor %p\n", Header
->SecurityDescriptor
);
881 /* Release the new security descriptor */
882 SeDeassignSecurity(&NewSecurityDescriptor
);
885 DPRINT("Security Complete\n");
886 SeReleaseSubjectContext(&SubjectContext
);
890 *Object
= HEADER_TO_BODY(Header
);
894 return STATUS_SUCCESS
;
899 * FUNCTION: Increments the pointer reference count for a given object
901 * ObjectBody = Object's body
902 * DesiredAccess = Desired access to the object
903 * ObjectType = Points to the object type structure
904 * AccessMode = Type of access check to perform
910 ObReferenceObjectByPointer(IN PVOID Object
,
911 IN ACCESS_MASK DesiredAccess
,
912 IN POBJECT_TYPE ObjectType
,
913 IN KPROCESSOR_MODE AccessMode
)
915 POBJECT_HEADER Header
;
917 /* NOTE: should be possible to reference an object above APC_LEVEL! */
919 DPRINT("ObReferenceObjectByPointer(Object %x, ObjectType %x)\n",
922 Header
= BODY_TO_HEADER(Object
);
924 if (ObjectType
!= NULL
&& Header
->ObjectType
!= ObjectType
)
926 DPRINT("Failed %x (type was %x %S) should be %x %S\n",
929 Header
->ObjectType
->TypeName
.Buffer
,
931 ObjectType
->TypeName
.Buffer
);
932 return(STATUS_UNSUCCESSFUL
);
934 if (Header
->ObjectType
== PsProcessType
)
936 DPRINT("Ref p 0x%x refcount %d type %x ",
937 Object
, Header
->RefCount
, PsProcessType
);
938 DPRINT("eip %x\n", ((PULONG
)&Object
)[-1]);
940 if (Header
->ObjectType
== PsThreadType
)
942 DPRINT("Deref t 0x%x with refcount %d type %x ",
943 Object
, Header
->RefCount
, PsThreadType
);
944 DPRINT("eip %x\n", ((PULONG
)&Object
)[-1]);
947 if (Header
->RefCount
== 0 && !Header
->Permanent
)
949 if (Header
->ObjectType
== PsProcessType
)
951 return STATUS_PROCESS_IS_TERMINATING
;
953 if (Header
->ObjectType
== PsThreadType
)
955 return STATUS_THREAD_IS_TERMINATING
;
957 return(STATUS_UNSUCCESSFUL
);
960 if (1 == InterlockedIncrement(&Header
->RefCount
) && !Header
->Permanent
)
965 return(STATUS_SUCCESS
);
973 ObOpenObjectByPointer(IN POBJECT Object
,
974 IN ULONG HandleAttributes
,
975 IN PACCESS_STATE PassedAccessState
,
976 IN ACCESS_MASK DesiredAccess
,
977 IN POBJECT_TYPE ObjectType
,
978 IN KPROCESSOR_MODE AccessMode
,
985 DPRINT("ObOpenObjectByPointer()\n");
987 Status
= ObReferenceObjectByPointer(Object
,
991 if (!NT_SUCCESS(Status
))
996 Status
= ObpCreateHandle(PsGetCurrentProcess(),
999 (BOOLEAN
)(HandleAttributes
& OBJ_INHERIT
),
1002 ObDereferenceObject(Object
);
1004 return STATUS_SUCCESS
;
1009 ObpDeleteObject(POBJECT_HEADER Header
)
1011 DPRINT("ObpDeleteObject(Header %p)\n", Header
);
1012 if (KeGetCurrentIrql() != PASSIVE_LEVEL
)
1014 DPRINT("ObpDeleteObject called at an unsupported IRQL. Use ObpDeleteObjectDpcLevel instead.\n");
1018 if (Header
->SecurityDescriptor
!= NULL
)
1020 ObpRemoveSecurityDescriptor(Header
->SecurityDescriptor
);
1023 if (Header
->ObjectType
!= NULL
&&
1024 Header
->ObjectType
->Delete
!= NULL
)
1026 Header
->ObjectType
->Delete(HEADER_TO_BODY(Header
));
1029 if (Header
->Name
.Buffer
!= NULL
)
1031 ObpRemoveEntryDirectory(Header
);
1032 RtlFreeUnicodeString(&Header
->Name
);
1035 DPRINT("ObPerformRetentionChecks() = Freeing object\n");
1038 return(STATUS_SUCCESS
);
1043 ObpDeleteObjectWorkRoutine (IN PVOID Parameter
)
1045 PRETENTION_CHECK_PARAMS Params
= (PRETENTION_CHECK_PARAMS
)Parameter
;
1046 /* ULONG Tag; */ /* See below */
1049 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL
); /* We need PAGED_CODE somewhere... */
1051 /* Turn this on when we have ExFreePoolWithTag
1052 Tag = Params->ObjectHeader->ObjectType->Tag; */
1053 ObpDeleteObject(Params
->ObjectHeader
);
1055 /* ExFreePoolWithTag(Params, Tag); */
1060 ObpDeleteObjectDpcLevel(IN POBJECT_HEADER ObjectHeader
,
1061 IN LONG OldRefCount
)
1064 if (ObjectHeader
->RefCount
< 0)
1066 CPRINT("Object %p/%p has invalid reference count (%d)\n",
1067 ObjectHeader
, HEADER_TO_BODY(ObjectHeader
),
1068 ObjectHeader
->RefCount
);
1072 if (ObjectHeader
->HandleCount
< 0)
1074 CPRINT("Object %p/%p has invalid handle count (%d)\n",
1075 ObjectHeader
, HEADER_TO_BODY(ObjectHeader
),
1076 ObjectHeader
->HandleCount
);
1082 switch (KeGetCurrentIrql ())
1085 return ObpDeleteObject (ObjectHeader
);
1088 case DISPATCH_LEVEL
:
1090 PRETENTION_CHECK_PARAMS Params
;
1093 We use must succeed pool here because if the allocation fails
1094 then we leak memory.
1096 Params
= (PRETENTION_CHECK_PARAMS
)
1097 ExAllocatePoolWithTag(NonPagedPoolMustSucceed
,
1098 sizeof(RETENTION_CHECK_PARAMS
),
1099 ObjectHeader
->ObjectType
->Tag
);
1100 Params
->ObjectHeader
= ObjectHeader
;
1101 ExInitializeWorkItem(&Params
->WorkItem
,
1102 ObpDeleteObjectWorkRoutine
,
1104 ExQueueWorkItem(&Params
->WorkItem
,
1107 return STATUS_PENDING
;
1110 DPRINT("ObpDeleteObjectDpcLevel called at unsupported "
1111 "IRQL %u!\n", KeGetCurrentIrql());
1113 return STATUS_UNSUCCESSFUL
;
1116 return STATUS_SUCCESS
;
1120 /**********************************************************************
1122 * ObfReferenceObject@4
1125 * Increments a given object's reference count and performs
1129 * ObjectBody = Body of the object.
1137 ObfReferenceObject(IN PVOID Object
)
1139 POBJECT_HEADER Header
;
1143 Header
= BODY_TO_HEADER(Object
);
1145 /* No one should be referencing an object once we are deleting it. */
1146 if (InterlockedIncrement(&Header
->RefCount
) == 1 && !Header
->Permanent
)
1153 /**********************************************************************
1155 * ObfDereferenceObject@4
1158 * Decrements a given object's reference count and performs
1162 * ObjectBody = Body of the object.
1170 ObfDereferenceObject(IN PVOID Object
)
1172 POBJECT_HEADER Header
;
1178 /* Extract the object header. */
1179 Header
= BODY_TO_HEADER(Object
);
1180 Permanent
= Header
->Permanent
;
1183 Drop our reference and get the new count so we can tell if this was the
1186 NewRefCount
= InterlockedDecrement(&Header
->RefCount
);
1187 DPRINT("ObfDereferenceObject(0x%x)==%d (%wZ)\n", Object
, NewRefCount
, &Header
->ObjectType
->TypeName
);
1188 ASSERT(NewRefCount
>= 0);
1190 /* Check whether the object can now be deleted. */
1191 if (NewRefCount
== 0 &&
1194 ObpDeleteObjectDpcLevel(Header
, NewRefCount
);
1200 ObInitializeFastReference(IN PEX_FAST_REF FastRef
,
1203 /* FIXME: Fast Referencing is Unimplemented */
1204 FastRef
->Object
= Object
;
1210 ObFastReferenceObject(IN PEX_FAST_REF FastRef
)
1212 /* FIXME: Fast Referencing is Unimplemented */
1214 /* Do a normal Reference */
1215 ObReferenceObject(FastRef
->Object
);
1217 /* Return the Object */
1218 return FastRef
->Object
;
1223 ObFastDereferenceObject(IN PEX_FAST_REF FastRef
,
1226 /* FIXME: Fast Referencing is Unimplemented */
1228 /* Do a normal Dereference */
1229 ObDereferenceObject(FastRef
->Object
);
1234 ObFastReplaceObject(IN PEX_FAST_REF FastRef
,
1237 PVOID OldObject
= FastRef
->Object
;
1239 /* FIXME: Fast Referencing is Unimplemented */
1240 FastRef
->Object
= Object
;
1242 /* Do a normal Dereference */
1243 ObDereferenceObject(OldObject
);
1245 /* Return old Object*/
1249 /**********************************************************************
1251 * ObGetObjectPointerCount@4
1254 * Retrieves the pointer(reference) count of the given object.
1257 * ObjectBody = Body of the object.
1265 ObGetObjectPointerCount(PVOID Object
)
1267 POBJECT_HEADER Header
;
1272 Header
= BODY_TO_HEADER(Object
);
1274 return Header
->RefCount
;
1278 /**********************************************************************
1280 * ObGetObjectHandleCount@4
1283 * Retrieves the handle count of the given object.
1286 * ObjectBody = Body of the object.
1292 ObGetObjectHandleCount(PVOID Object
)
1294 POBJECT_HEADER Header
;
1299 Header
= BODY_TO_HEADER(Object
);
1301 return Header
->HandleCount
;
1305 /**********************************************************************
1307 * ObDereferenceObject@4
1310 * Decrements a given object's reference count and performs
1314 * ObjectBody = Body of the object.
1322 #ifdef ObDereferenceObject
1323 #undef ObDereferenceObject
1327 ObDereferenceObject(IN PVOID Object
)
1329 ObfDereferenceObject(Object
);