2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ob/obname.c
5 * PURPOSE: Manages all functions related to the Object Manager name-
6 * space, such as finding objects or querying their names.
7 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 * Thomas Weidenmueller (w3seek@reactos.org)
12 /* INCLUDES ******************************************************************/
18 BOOLEAN ObpCaseInsensitive
= TRUE
;
19 POBJECT_DIRECTORY ObpRootDirectoryObject
;
20 POBJECT_DIRECTORY ObpTypeDirectoryObject
;
22 /* DOS Device Prefix \??\ and \?? */
23 ALIGNEDNAME ObpDosDevicesShortNamePrefix
= {{L
'\\',L
'?',L
'?',L
'\\'}};
24 ALIGNEDNAME ObpDosDevicesShortNameRoot
= {{L
'\\',L
'?',L
'?',L
'\0'}};
25 UNICODE_STRING ObpDosDevicesShortName
=
27 sizeof(ObpDosDevicesShortNamePrefix
),
28 sizeof(ObpDosDevicesShortNamePrefix
),
29 (PWSTR
)&ObpDosDevicesShortNamePrefix
32 WCHAR ObpUnsecureGlobalNamesBuffer
[128] = {0};
33 ULONG ObpUnsecureGlobalNamesLength
= sizeof(ObpUnsecureGlobalNamesBuffer
);
35 /* PRIVATE FUNCTIONS *********************************************************/
40 ObpGetDosDevicesProtection(OUT PSECURITY_DESCRIPTOR SecurityDescriptor
)
46 /* Initialize the SD */
47 Status
= RtlCreateSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
48 ASSERT(NT_SUCCESS(Status
));
50 if (ObpProtectionMode
& 1)
52 AclSize
= sizeof(ACL
) +
53 sizeof(ACE
) + RtlLengthSid(SeWorldSid
) +
54 sizeof(ACE
) + RtlLengthSid(SeLocalSystemSid
) +
55 sizeof(ACE
) + RtlLengthSid(SeWorldSid
) +
56 sizeof(ACE
) + RtlLengthSid(SeAliasAdminsSid
) +
57 sizeof(ACE
) + RtlLengthSid(SeLocalSystemSid
) +
58 sizeof(ACE
) + RtlLengthSid(SeCreatorOwnerSid
);
60 /* Allocate the ACL */
61 Dacl
= ExAllocatePoolWithTag(PagedPool
, AclSize
, 'lcaD');
64 return STATUS_INSUFFICIENT_RESOURCES
;
67 /* Initialize the DACL */
68 Status
= RtlCreateAcl(Dacl
, AclSize
, ACL_REVISION
);
69 ASSERT(NT_SUCCESS(Status
));
72 Status
= RtlAddAccessAllowedAce(Dacl
,
74 GENERIC_READ
| GENERIC_EXECUTE
,
76 ASSERT(NT_SUCCESS(Status
));
78 Status
= RtlAddAccessAllowedAce(Dacl
,
82 ASSERT(NT_SUCCESS(Status
));
84 Status
= RtlAddAccessAllowedAceEx(Dacl
,
86 INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
| OBJECT_INHERIT_ACE
,
89 ASSERT(NT_SUCCESS(Status
));
91 Status
= RtlAddAccessAllowedAceEx(Dacl
,
93 INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
| OBJECT_INHERIT_ACE
,
96 ASSERT(NT_SUCCESS(Status
));
98 Status
= RtlAddAccessAllowedAceEx(Dacl
,
100 INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
| OBJECT_INHERIT_ACE
,
103 ASSERT(NT_SUCCESS(Status
));
105 Status
= RtlAddAccessAllowedAceEx(Dacl
,
107 INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
| OBJECT_INHERIT_ACE
,
110 ASSERT(NT_SUCCESS(Status
));
114 AclSize
= sizeof(ACL
) +
115 sizeof(ACE
) + RtlLengthSid(SeLocalSystemSid
) +
116 sizeof(ACE
) + RtlLengthSid(SeWorldSid
) +
117 sizeof(ACE
) + RtlLengthSid(SeLocalSystemSid
);
119 /* Allocate the ACL */
120 Dacl
= ExAllocatePoolWithTag(PagedPool
, AclSize
, 'lcaD');
123 return STATUS_INSUFFICIENT_RESOURCES
;
126 /* Initialize the DACL */
127 Status
= RtlCreateAcl(Dacl
, AclSize
, ACL_REVISION
);
128 ASSERT(NT_SUCCESS(Status
));
131 Status
= RtlAddAccessAllowedAce(Dacl
,
133 GENERIC_READ
| GENERIC_EXECUTE
| GENERIC_WRITE
,
135 ASSERT(NT_SUCCESS(Status
));
137 Status
= RtlAddAccessAllowedAce(Dacl
,
141 ASSERT(NT_SUCCESS(Status
));
143 Status
= RtlAddAccessAllowedAceEx(Dacl
,
145 INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
| OBJECT_INHERIT_ACE
,
148 ASSERT(NT_SUCCESS(Status
));
151 /* Attach the DACL to the SD */
152 Status
= RtlSetDaclSecurityDescriptor(SecurityDescriptor
, TRUE
, Dacl
, FALSE
);
153 ASSERT(NT_SUCCESS(Status
));
155 return STATUS_SUCCESS
;
161 ObpFreeDosDevicesProtection(OUT PSECURITY_DESCRIPTOR SecurityDescriptor
)
165 BOOLEAN DaclPresent
, DaclDefaulted
;
167 Status
= RtlGetDaclSecurityDescriptor(SecurityDescriptor
, &DaclPresent
, &Dacl
, &DaclDefaulted
);
168 ASSERT(NT_SUCCESS(Status
));
170 ASSERT(Dacl
!= NULL
);
171 ExFreePoolWithTag(Dacl
, 'lcaD');
177 ObpCreateDosDevicesDirectory(VOID
)
179 OBJECT_ATTRIBUTES ObjectAttributes
;
180 UNICODE_STRING RootName
, TargetName
, LinkName
;
181 HANDLE Handle
, SymHandle
;
182 SECURITY_DESCRIPTOR DosDevicesSD
;
185 /* Create a custom security descriptor for the global DosDevices directory */
186 Status
= ObpGetDosDevicesProtection(&DosDevicesSD
);
187 if (!NT_SUCCESS(Status
))
190 /* Create the global DosDevices directory \?? */
191 RtlInitUnicodeString(&RootName
, L
"\\GLOBAL??");
192 InitializeObjectAttributes(&ObjectAttributes
,
197 Status
= NtCreateDirectoryObject(&Handle
,
198 DIRECTORY_ALL_ACCESS
,
200 if (!NT_SUCCESS(Status
))
203 /* Create the system device map */
204 Status
= ObSetDeviceMap(NULL
, Handle
);
205 if (!NT_SUCCESS(Status
))
208 /*********************************************\
209 |*** HACK until we support device mappings ***|
210 |*** Add a symlink \??\ <--> \GLOBAL??\ ***|
211 \*********************************************/
212 RtlInitUnicodeString(&LinkName
, L
"\\??");
213 InitializeObjectAttributes(&ObjectAttributes
,
218 Status
= NtCreateSymbolicLinkObject(&SymHandle
,
219 SYMBOLIC_LINK_ALL_ACCESS
,
222 if (NT_SUCCESS(Status
)) NtClose(SymHandle
);
223 /*********************************************\
224 \*********************************************/
226 // FIXME: Create a device mapping for the global \?? directory
229 * Initialize the \??\GLOBALROOT symbolic link
230 * pointing to the root directory \ .
232 RtlInitUnicodeString(&LinkName
, L
"GLOBALROOT");
233 RtlInitUnicodeString(&TargetName
, L
"");
234 InitializeObjectAttributes(&ObjectAttributes
,
239 Status
= NtCreateSymbolicLinkObject(&SymHandle
,
240 SYMBOLIC_LINK_ALL_ACCESS
,
243 if (NT_SUCCESS(Status
)) NtClose(SymHandle
);
246 * Initialize the \??\Global symbolic link pointing to the global
247 * DosDevices directory \?? . It is used to access the global \??
248 * by user-mode components which, by default, use a per-session
249 * DosDevices directory.
251 RtlInitUnicodeString(&LinkName
, L
"Global");
252 InitializeObjectAttributes(&ObjectAttributes
,
257 Status
= NtCreateSymbolicLinkObject(&SymHandle
,
258 SYMBOLIC_LINK_ALL_ACCESS
,
261 if (NT_SUCCESS(Status
)) NtClose(SymHandle
);
263 /* Close the directory handle */
265 if (!NT_SUCCESS(Status
))
269 * Initialize the \DosDevices symbolic link pointing to the global
270 * DosDevices directory \?? , for backward compatibility with
271 * Windows NT-2000 systems.
273 RtlCreateUnicodeString(&LinkName
, L
"\\DosDevices");
274 RtlInitUnicodeString(&RootName
, (PCWSTR
)&ObpDosDevicesShortNameRoot
);
275 InitializeObjectAttributes(&ObjectAttributes
,
280 Status
= NtCreateSymbolicLinkObject(&SymHandle
,
281 SYMBOLIC_LINK_ALL_ACCESS
,
284 if (NT_SUCCESS(Status
)) NtClose(SymHandle
);
287 ObpFreeDosDevicesProtection(&DosDevicesSD
);
294 * @name ObpDeleteNameCheck
296 * The ObpDeleteNameCheck routine checks if a named object should be
297 * removed from the object directory namespace.
300 * Pointer to the object to check for possible removal.
304 * @remarks An object is removed if the following 4 criteria are met:
305 * 1) The object has 0 handles open
306 * 2) The object is in the directory namespace and has a name
307 * 3) The object is not permanent
312 ObpDeleteNameCheck(IN PVOID Object
)
314 POBJECT_HEADER ObjectHeader
;
315 OBP_LOOKUP_CONTEXT Context
;
316 POBJECT_HEADER_NAME_INFO ObjectNameInfo
;
317 POBJECT_TYPE ObjectType
;
318 PVOID Directory
= NULL
;
320 /* Get object structures */
321 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(Object
);
322 ObjectNameInfo
= ObpReferenceNameInfo(ObjectHeader
);
323 ObjectType
= ObjectHeader
->Type
;
326 * Check if the handle count is 0, if the object is named,
327 * and if the object isn't a permanent object.
329 if (!(ObjectHeader
->HandleCount
) &&
331 (ObjectNameInfo
->Name
.Length
) &&
332 (ObjectNameInfo
->Directory
) &&
333 !(ObjectHeader
->Flags
& OB_FLAG_PERMANENT
))
335 /* Setup a lookup context */
336 ObpInitializeLookupContext(&Context
);
338 /* Lock the directory */
339 ObpAcquireDirectoryLockExclusive(ObjectNameInfo
->Directory
, &Context
);
342 Object
= ObpLookupEntryDirectory(ObjectNameInfo
->Directory
,
343 &ObjectNameInfo
->Name
,
349 /* Lock the object */
350 ObpAcquireObjectLock(ObjectHeader
);
352 /* Make sure we can still delete the object */
353 if (!(ObjectHeader
->HandleCount
) &&
354 !(ObjectHeader
->Flags
& OB_FLAG_PERMANENT
))
356 /* First delete it from the directory */
357 ObpDeleteEntryDirectory(&Context
);
359 /* Check if this is a symbolic link */
360 if (ObjectType
== ObpSymbolicLinkObjectType
)
362 /* Remove internal name */
363 ObpDeleteSymbolicLinkName(Object
);
366 /* Check if the kernel exclusive is set */
367 ObjectNameInfo
= OBJECT_HEADER_TO_NAME_INFO(ObjectHeader
);
368 if ((ObjectNameInfo
) &&
369 (ObjectNameInfo
->QueryReferences
& OB_FLAG_KERNEL_EXCLUSIVE
))
371 /* Remove protection flag */
372 InterlockedExchangeAdd((PLONG
)&ObjectNameInfo
->QueryReferences
,
373 -OB_FLAG_KERNEL_EXCLUSIVE
);
376 /* Get the directory */
377 Directory
= ObjectNameInfo
->Directory
;
380 /* Release the lock */
381 ObpReleaseObjectLock(ObjectHeader
);
384 /* Cleanup after lookup */
385 ObpReleaseLookupContext(&Context
);
387 /* Remove another query reference since we added one on top */
388 ObpDereferenceNameInfo(ObjectNameInfo
);
390 /* Check if we were inserted in a directory */
393 /* We were, so first remove the extra reference we had added */
394 ObpDereferenceNameInfo(ObjectNameInfo
);
396 /* Now dereference the object as well */
397 ObDereferenceObject(Object
);
402 /* Remove the reference we added */
403 ObpDereferenceNameInfo(ObjectNameInfo
);
409 ObpIsUnsecureName(IN PUNICODE_STRING ObjectName
,
410 IN BOOLEAN CaseInSensitive
)
413 PWSTR UnsecureBuffer
;
414 UNICODE_STRING UnsecureName
;
416 /* No unsecure names known, quit */
417 if (ObpUnsecureGlobalNamesBuffer
[0] == UNICODE_NULL
)
422 /* By default, we have a secure name */
424 /* We will browse the whole string */
425 UnsecureBuffer
= &ObpUnsecureGlobalNamesBuffer
[0];
428 /* Initialize the unicode string */
429 RtlInitUnicodeString(&UnsecureName
, UnsecureBuffer
);
430 /* We're at the end of the multisz string! */
431 if (UnsecureName
.Length
== 0)
437 * Does the unsecure name prefix the object name?
438 * If so, that's an unsecure name, and return so
440 if (RtlPrefixUnicodeString(&UnsecureName
, ObjectName
, CaseInSensitive
))
447 * Move to the next string. As a reminder, ObpUnsecureGlobalNamesBuffer is
448 * a multisz, so we move the string next to the current UNICODE_NULL char
450 UnsecureBuffer
= (PWSTR
)((ULONG_PTR
)UnsecureBuffer
+ UnsecureName
.Length
+ sizeof(UNICODE_NULL
));
453 /* Return our findings */
459 ObpLookupObjectName(IN HANDLE RootHandle OPTIONAL
,
460 IN OUT PUNICODE_STRING ObjectName
,
462 IN POBJECT_TYPE ObjectType
,
463 IN KPROCESSOR_MODE AccessMode
,
464 IN OUT PVOID ParseContext
,
465 IN PSECURITY_QUALITY_OF_SERVICE SecurityQos OPTIONAL
,
466 IN PVOID InsertObject OPTIONAL
,
467 IN OUT PACCESS_STATE AccessState
,
468 OUT POBP_LOOKUP_CONTEXT LookupContext
,
469 OUT PVOID
*FoundObject
)
472 POBJECT_HEADER ObjectHeader
;
473 UNICODE_STRING ComponentName
, RemainingName
;
474 BOOLEAN Reparse
= FALSE
, SymLink
= FALSE
;
475 POBJECT_DIRECTORY Directory
= NULL
, ParentDirectory
= NULL
, RootDirectory
;
476 POBJECT_DIRECTORY ReferencedDirectory
= NULL
, ReferencedParentDirectory
= NULL
;
478 OB_PARSE_METHOD ParseRoutine
;
480 KPROCESSOR_MODE AccessCheckMode
;
482 POBJECT_HEADER_NAME_INFO ObjectNameInfo
;
483 ULONG MaxReparse
= 30;
485 OBTRACE(OB_NAMESPACE_DEBUG
,
486 "%s - Finding Object: %wZ. Expecting: %p\n",
491 /* Initialize starting state */
492 ObpInitializeLookupContext(LookupContext
);
494 Status
= STATUS_SUCCESS
;
497 /* Check if case-insensitivity is checked */
498 if (ObpCaseInsensitive
)
500 /* Check if the object type requests this */
501 if (!(ObjectType
) || (ObjectType
->TypeInfo
.CaseInsensitive
))
503 /* Add the flag to disable case sensitivity */
504 Attributes
|= OBJ_CASE_INSENSITIVE
;
508 /* Check if this is a access checks are being forced */
509 AccessCheckMode
= (Attributes
& OBJ_FORCE_ACCESS_CHECK
) ?
510 UserMode
: AccessMode
;
512 /* Check if we got a Root Directory */
515 /* We did. Reference it */
516 Status
= ObReferenceObjectByHandle(RootHandle
,
520 (PVOID
*)&RootDirectory
,
522 if (!NT_SUCCESS(Status
)) return Status
;
525 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(RootDirectory
);
527 /* The name cannot start with a separator, unless this is a file */
528 if ((ObjectName
->Buffer
) &&
529 (ObjectName
->Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
) &&
530 (ObjectHeader
->Type
!= IoFileObjectType
))
532 /* The syntax is bad, so fail this request */
533 ObDereferenceObject(RootDirectory
);
534 return STATUS_OBJECT_PATH_SYNTAX_BAD
;
537 /* Don't parse a Directory */
538 if (ObjectHeader
->Type
!= ObpDirectoryObjectType
)
540 /* Make sure the Object Type has a parse routine */
541 ParseRoutine
= ObjectHeader
->Type
->TypeInfo
.ParseProcedure
;
544 /* We can't parse a name if we don't have a parse routine */
545 ObDereferenceObject(RootDirectory
);
546 return STATUS_INVALID_HANDLE
;
549 /* Set default parse count */
555 /* Start with the full name */
556 RemainingName
= *ObjectName
;
558 /* Call the Parse Procedure */
559 ObpCalloutStart(&CalloutIrql
);
560 Status
= ParseRoutine(RootDirectory
,
570 ObpCalloutEnd(CalloutIrql
, "Parse", ObjectHeader
->Type
, Object
);
572 /* Check for success or failure, so not reparse */
573 if ((Status
!= STATUS_REPARSE
) &&
574 (Status
!= STATUS_REPARSE_OBJECT
))
576 /* Check for failure */
577 if (!NT_SUCCESS(Status
))
579 /* Parse routine might not have cleared this, do it */
584 /* Modify status to reflect failure inside Ob */
585 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
588 /* We're done, return the status and object */
589 *FoundObject
= Object
;
590 ObDereferenceObject(RootDirectory
);
593 else if ((!ObjectName
->Length
) ||
594 (!ObjectName
->Buffer
) ||
595 (ObjectName
->Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
))
597 /* Reparsed to the root directory, so start over */
598 ObDereferenceObject(RootDirectory
);
599 RootDirectory
= ObpRootDirectoryObject
;
601 /* Don't use this anymore, since we're starting at root */
605 else if (--MaxReparse
)
607 /* Try reparsing again */
612 /* Reparsed too many times */
613 ObDereferenceObject(RootDirectory
);
615 /* Return the object and normalized status */
616 *FoundObject
= Object
;
617 if (!Object
) Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
622 else if (!(ObjectName
->Length
) || !(ObjectName
->Buffer
))
624 /* Just return the Root Directory if we didn't get a name */
625 Status
= ObReferenceObjectByPointer(RootDirectory
,
629 if (NT_SUCCESS(Status
)) Object
= RootDirectory
;
631 /* Remove the first reference we added and return the object */
632 ObDereferenceObject(RootDirectory
);
633 *FoundObject
= Object
;
639 /* We did not get a Root Directory, so use the root */
640 RootDirectory
= ObpRootDirectoryObject
;
642 /* It must start with a path separator */
643 if (!(ObjectName
->Length
) ||
644 !(ObjectName
->Buffer
) ||
645 (ObjectName
->Buffer
[0] != OBJ_NAME_PATH_SEPARATOR
))
647 /* This name is invalid, so fail */
648 return STATUS_OBJECT_PATH_SYNTAX_BAD
;
651 /* Check if the name is only the path separator */
652 if (ObjectName
->Length
== sizeof(OBJ_NAME_PATH_SEPARATOR
))
654 /* So the caller only wants the root directory; do we have one? */
657 /* This must be the first time we're creating it... right? */
660 /* Yes, so return it to ObInsert so that it can create it */
661 Status
= ObReferenceObjectByPointer(InsertObject
,
665 if (NT_SUCCESS(Status
)) *FoundObject
= InsertObject
;
670 /* This should never really happen */
672 return STATUS_INVALID_PARAMETER
;
677 /* We do have the root directory, so just return it */
678 Status
= ObReferenceObjectByPointer(RootDirectory
,
682 if (NT_SUCCESS(Status
)) *FoundObject
= RootDirectory
;
689 /* FIXME: Check if we have a device map */
691 /* Check if this is a possible DOS name */
692 if (!((ULONG_PTR
)(ObjectName
->Buffer
) & 7))
695 * This could be one. Does it match the prefix?
696 * Note that as an optimization, the match is done as 64-bit
697 * compare since the prefix is "\??\" which is exactly 8 bytes.
699 * In the second branch, we test for "\??" which is also valid.
700 * This time, we use a 32-bit compare followed by a Unicode
701 * character compare (16-bit), since the sum is 6 bytes.
703 if ((ObjectName
->Length
>= ObpDosDevicesShortName
.Length
) &&
704 (*(PULONGLONG
)(ObjectName
->Buffer
) ==
705 ObpDosDevicesShortNamePrefix
.Alignment
.QuadPart
))
709 else if ((ObjectName
->Length
== ObpDosDevicesShortName
.Length
-
711 (*(PULONG
)(ObjectName
->Buffer
) ==
712 ObpDosDevicesShortNameRoot
.Alignment
.LowPart
) &&
713 (*((PWCHAR
)(ObjectName
->Buffer
) + 2) ==
714 (WCHAR
)(ObpDosDevicesShortNameRoot
.Alignment
.HighPart
)))
722 /* Check if we were reparsing a symbolic link */
731 while (Reparse
&& MaxReparse
)
734 RemainingName
= *ObjectName
;
736 /* Disable reparsing again */
739 /* Start parse loop */
745 /* Check if the name starts with a path separator */
746 if ((RemainingName
.Length
) &&
747 (RemainingName
.Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
))
749 /* Skip the path separator */
750 RemainingName
.Buffer
++;
751 RemainingName
.Length
-= sizeof(OBJ_NAME_PATH_SEPARATOR
);
754 /* Find the next Part Name */
755 ComponentName
= RemainingName
;
756 while (RemainingName
.Length
)
758 /* Break if we found the \ ending */
759 if (RemainingName
.Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
) break;
762 RemainingName
.Buffer
++;
763 RemainingName
.Length
-= sizeof(OBJ_NAME_PATH_SEPARATOR
);
766 /* Get its size and make sure it's valid */
767 ComponentName
.Length
-= RemainingName
.Length
;
768 if (!ComponentName
.Length
)
770 /* Invalid size, fail */
771 Status
= STATUS_OBJECT_NAME_INVALID
;
775 /* Check if we're in the root */
776 if (!Directory
) Directory
= RootDirectory
;
778 /* Check if this is a user-mode call that needs to traverse */
779 if ((AccessCheckMode
!= KernelMode
) &&
780 !(AccessState
->Flags
& TOKEN_HAS_TRAVERSE_PRIVILEGE
))
782 /* We shouldn't have referenced a directory yet */
783 ASSERT(ReferencedDirectory
== NULL
);
785 /* Reference the directory */
786 ObReferenceObject(Directory
);
787 ReferencedDirectory
= Directory
;
789 /* Check if we have a parent directory */
792 /* Check for traverse access */
793 if (!ObpCheckTraverseAccess(ParentDirectory
,
800 /* We don't have it, fail */
806 /* Check if we don't have a remaining name yet */
807 if (!RemainingName
.Length
)
809 /* Check if we don't have a referenced directory yet */
810 if (!ReferencedDirectory
)
813 ObReferenceObject(Directory
);
814 ReferencedDirectory
= Directory
;
817 /* Check if we are inserting an object */
820 /* Lock the directory */
821 ObpAcquireDirectoryLockExclusive(Directory
, LookupContext
);
826 Object
= ObpLookupEntryDirectory(Directory
,
829 InsertObject
? FALSE
: TRUE
,
833 /* We didn't find it... do we still have a path? */
834 if (RemainingName
.Length
)
836 /* Then tell the caller the path wasn't found */
837 Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
840 else if (!InsertObject
)
842 /* Otherwise, we have a path, but the name isn't valid */
843 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
847 /* Check create access for the object */
848 if (!ObCheckCreateObjectAccess(Directory
,
849 ObjectType
== ObpDirectoryObjectType
?
850 DIRECTORY_CREATE_SUBDIRECTORY
:
851 DIRECTORY_CREATE_OBJECT
,
858 /* We don't have create access, fail */
862 /* Get the object header */
863 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(InsertObject
);
866 * Deny object creation if:
867 * That's a section object or a symbolic link
868 * Which isn't in the same section that root directory
869 * That doesn't have the SeCreateGlobalPrivilege
870 * And that is not a known unsecure name
872 if (RootDirectory
->SessionId
!= -1)
874 if (ObjectHeader
->Type
== MmSectionObjectType
||
875 ObjectHeader
->Type
== ObpSymbolicLinkObjectType
)
877 if (RootDirectory
->SessionId
!= PsGetCurrentProcessSessionId() &&
878 !SeSinglePrivilegeCheck(SeCreateGlobalPrivilege
, AccessCheckMode
) &&
879 !ObpIsUnsecureName(&ComponentName
, BooleanFlagOn(Attributes
, OBJ_CASE_INSENSITIVE
)))
881 Status
= STATUS_ACCESS_DENIED
;
887 /* Create Object Name */
888 NewName
= ExAllocatePoolWithTag(PagedPool
,
889 ComponentName
.Length
,
892 !(ObpInsertEntryDirectory(Directory
,
896 /* Either couldn't allocate the name, or insert failed */
897 if (NewName
) ExFreePoolWithTag(NewName
, OB_NAME_TAG
);
899 /* Fail due to memory reasons */
900 Status
= STATUS_INSUFFICIENT_RESOURCES
;
904 /* Reference newly to be inserted object */
905 ObReferenceObject(InsertObject
);
907 /* Get the name information */
908 ObjectNameInfo
= OBJECT_HEADER_TO_NAME_INFO(ObjectHeader
);
910 /* Reference the directory */
911 ObReferenceObject(Directory
);
914 RtlCopyMemory(NewName
,
915 ComponentName
.Buffer
,
916 ComponentName
.Length
);
918 /* Check if we had an old name */
919 if (ObjectNameInfo
->Name
.Buffer
)
922 ExFreePoolWithTag(ObjectNameInfo
->Name
.Buffer
, OB_NAME_TAG
);
926 ObjectNameInfo
->Name
.Buffer
= NewName
;
927 ObjectNameInfo
->Name
.Length
= ComponentName
.Length
;
928 ObjectNameInfo
->Name
.MaximumLength
= ComponentName
.Length
;
930 /* Return Status and the Expected Object */
931 Status
= STATUS_SUCCESS
;
932 Object
= InsertObject
;
934 /* Get out of here */
939 /* We found it, so now get its header */
940 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(Object
);
943 * Check for a parse Procedure, but don't bother to parse for an insert
944 * unless it's a Symbolic Link, in which case we MUST parse
946 ParseRoutine
= ObjectHeader
->Type
->TypeInfo
.ParseProcedure
;
947 if ((ParseRoutine
) &&
948 (!(InsertObject
) || (ParseRoutine
== ObpParseSymbolicLink
)))
950 /* Use the Root Directory next time */
953 /* Increment the pointer count */
954 InterlockedExchangeAddSizeT(&ObjectHeader
->PointerCount
, 1);
956 /* Cleanup from the first lookup */
957 ObpReleaseLookupContext(LookupContext
);
959 /* Check if we have a referenced directory */
960 if (ReferencedDirectory
)
962 /* We do, dereference it */
963 ObDereferenceObject(ReferencedDirectory
);
964 ReferencedDirectory
= NULL
;
967 /* Check if we have a referenced parent directory */
968 if (ReferencedParentDirectory
)
970 /* We do, dereference it */
971 ObDereferenceObject(ReferencedParentDirectory
);
972 ReferencedParentDirectory
= NULL
;
975 /* Call the Parse Procedure */
976 ObpCalloutStart(&CalloutIrql
);
977 Status
= ParseRoutine(Object
,
987 ObpCalloutEnd(CalloutIrql
, "Parse", ObjectHeader
->Type
, Object
);
989 /* Remove our extra reference */
990 ObDereferenceObject(&ObjectHeader
->Body
);
992 /* Check if we have to reparse */
993 if ((Status
== STATUS_REPARSE
) ||
994 (Status
== STATUS_REPARSE_OBJECT
))
1005 /* Start over from root if we got sent back there */
1006 if ((Status
== STATUS_REPARSE_OBJECT
) ||
1007 (ObjectName
->Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
))
1009 /* Check if we got a root directory */
1012 /* Stop using it, because we have a new directory now */
1013 ObDereferenceObject(RootDirectory
);
1018 ParentDirectory
= NULL
;
1019 RootDirectory
= ObpRootDirectoryObject
;
1021 /* Check for reparse status */
1022 if (Status
== STATUS_REPARSE_OBJECT
)
1024 /* Don't reparse again */
1027 /* Did we actually get an object to which to reparse? */
1030 /* We didn't, so set a failure status */
1031 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
1035 /* We did, so we're free to parse the new object */
1041 /* This is a symbolic link */
1046 else if (RootDirectory
== ObpRootDirectoryObject
)
1048 /* We got STATUS_REPARSE but are at the Root Directory */
1050 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
1054 else if (!NT_SUCCESS(Status
))
1061 /* We didn't reparse but we didn't find the Object Either */
1062 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
1065 /* Break out of the loop */
1070 /* No parse routine...do we still have a remaining name? */
1071 if (!RemainingName
.Length
)
1073 /* Are we creating an object? */
1076 /* Check if this is a user-mode call that needs to traverse */
1077 if ((AccessCheckMode
!= KernelMode
) &&
1078 !(AccessState
->Flags
& TOKEN_HAS_TRAVERSE_PRIVILEGE
))
1080 /* Check if we can get it */
1081 if (!ObpCheckTraverseAccess(Directory
,
1088 /* We don't have access, fail */
1094 /* Reference the Object */
1095 Status
= ObReferenceObjectByPointer(Object
,
1099 if (!NT_SUCCESS(Status
)) Object
= NULL
;
1102 /* And get out of the reparse loop */
1107 /* We still have a name; check if this is a directory object */
1108 if (ObjectHeader
->Type
== ObpDirectoryObjectType
)
1110 /* Check if we have a referenced parent directory */
1111 if (ReferencedParentDirectory
)
1113 /* Dereference it */
1114 ObDereferenceObject(ReferencedParentDirectory
);
1117 /* Restart the lookup from this directory */
1118 ReferencedParentDirectory
= ReferencedDirectory
;
1119 ParentDirectory
= Directory
;
1121 ReferencedDirectory
= NULL
;
1125 /* We still have a name, but no parse routine for it */
1126 Status
= STATUS_OBJECT_TYPE_MISMATCH
;
1135 /* Check if we failed */
1136 if (!NT_SUCCESS(Status
))
1138 /* Cleanup after lookup */
1139 ObpReleaseLookupContext(LookupContext
);
1142 /* Check if we have a device map and dereference it if so */
1143 //if (DeviceMap) ObfDereferenceDeviceMap(DeviceMap);
1145 /* Check if we have a referenced directory and dereference it if so */
1146 if (ReferencedDirectory
) ObDereferenceObject(ReferencedDirectory
);
1148 /* Check if we have a referenced parent directory */
1149 if (ReferencedParentDirectory
)
1151 /* We do, dereference it */
1152 ObDereferenceObject(ReferencedParentDirectory
);
1155 /* Set the found object and check if we got one */
1156 *FoundObject
= Object
;
1159 /* Nothing was found. Did we reparse or get success? */
1160 if ((Status
== STATUS_REPARSE
) || (NT_SUCCESS(Status
)))
1162 /* Set correct failure */
1163 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
1167 /* Check if we had a root directory */
1168 if (RootHandle
) ObDereferenceObject(RootDirectory
);
1170 /* Return status to caller */
1171 OBTRACE(OB_NAMESPACE_DEBUG
,
1172 "%s - Found Object: %p. Expected: %p\n",
1179 /* PUBLIC FUNCTIONS *********************************************************/
1183 ObQueryNameString(IN PVOID Object
,
1184 OUT POBJECT_NAME_INFORMATION ObjectNameInfo
,
1186 OUT PULONG ReturnLength
)
1188 POBJECT_HEADER_NAME_INFO LocalInfo
;
1189 POBJECT_HEADER ObjectHeader
;
1190 POBJECT_DIRECTORY ParentDirectory
;
1193 BOOLEAN ObjectIsNamed
;
1194 NTSTATUS Status
= STATUS_SUCCESS
;
1196 /* Get the Kernel Meta-Structures */
1197 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(Object
);
1198 LocalInfo
= OBJECT_HEADER_TO_NAME_INFO(ObjectHeader
);
1200 /* Check if a Query Name Procedure is available */
1201 if (ObjectHeader
->Type
->TypeInfo
.QueryNameProcedure
)
1203 /* Call the procedure inside SEH */
1204 ObjectIsNamed
= ((LocalInfo
) && (LocalInfo
->Name
.Length
> 0));
1208 Status
= ObjectHeader
->Type
->TypeInfo
.QueryNameProcedure(Object
,
1215 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1217 /* Return the exception code */
1218 Status
= _SEH2_GetExceptionCode();
1225 /* Check if the object doesn't even have a name */
1226 if (!(LocalInfo
) || !(LocalInfo
->Name
.Buffer
))
1228 Status
= STATUS_SUCCESS
;
1232 /* We're returning the name structure */
1233 *ReturnLength
= sizeof(OBJECT_NAME_INFORMATION
);
1235 /* Check if we were given enough space */
1236 if (*ReturnLength
> Length
)
1238 Status
= STATUS_INFO_LENGTH_MISMATCH
;
1242 /* Return an empty buffer */
1243 RtlInitEmptyUnicodeString(&ObjectNameInfo
->Name
, NULL
, 0);
1246 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1248 /* Return the exception code */
1249 Status
= _SEH2_GetExceptionCode();
1257 * Find the size needed for the name. We won't do
1258 * this during the Name Creation loop because we want
1259 * to let the caller know that the buffer isn't big
1260 * enough right at the beginning, not work our way through
1261 * and find out at the end
1265 if (Object
== ObpRootDirectoryObject
)
1267 /* Size of the '\' string */
1268 NameSize
= sizeof(OBJ_NAME_PATH_SEPARATOR
);
1272 /* Get the Object Directory and add name of Object */
1273 ParentDirectory
= LocalInfo
->Directory
;
1274 NameSize
= sizeof(OBJ_NAME_PATH_SEPARATOR
) + LocalInfo
->Name
.Length
;
1276 /* Loop inside the directory to get the top-most one (meaning root) */
1277 while ((ParentDirectory
!= ObpRootDirectoryObject
) && (ParentDirectory
))
1279 /* Get the Name Information */
1280 LocalInfo
= OBJECT_HEADER_TO_NAME_INFO(
1281 OBJECT_TO_OBJECT_HEADER(ParentDirectory
));
1283 /* Add the size of the Directory Name */
1284 if (LocalInfo
&& LocalInfo
->Directory
)
1286 /* Size of the '\' string + Directory Name */
1287 NameSize
+= sizeof(OBJ_NAME_PATH_SEPARATOR
) +
1288 LocalInfo
->Name
.Length
;
1290 /* Move to next parent Directory */
1291 ParentDirectory
= LocalInfo
->Directory
;
1295 /* Directory with no name. We append "...\" */
1296 NameSize
+= sizeof(L
"...") + sizeof(OBJ_NAME_PATH_SEPARATOR
);
1302 /* Finally, add the name of the structure and the null char */
1303 *ReturnLength
= NameSize
+
1304 sizeof(OBJECT_NAME_INFORMATION
) +
1305 sizeof(UNICODE_NULL
);
1307 /* Check if we were given enough space */
1308 if (*ReturnLength
> Length
) _SEH2_YIELD(return STATUS_INFO_LENGTH_MISMATCH
);
1311 * Now we will actually create the name. We work backwards because
1312 * it's easier to start off from the Name we have and walk up the
1313 * parent directories. We use the same logic as Name Length calculation.
1315 LocalInfo
= OBJECT_HEADER_TO_NAME_INFO(ObjectHeader
);
1316 ObjectName
= (PWCH
)((ULONG_PTR
)ObjectNameInfo
+ *ReturnLength
);
1317 *--ObjectName
= UNICODE_NULL
;
1319 /* Check if the object is actually the Root directory */
1320 if (Object
== ObpRootDirectoryObject
)
1322 /* This is already the Root Directory, return "\\" */
1323 *--ObjectName
= OBJ_NAME_PATH_SEPARATOR
;
1324 ObjectNameInfo
->Name
.Length
= (USHORT
)NameSize
;
1325 ObjectNameInfo
->Name
.MaximumLength
= (USHORT
)(NameSize
+
1326 sizeof(UNICODE_NULL
));
1327 ObjectNameInfo
->Name
.Buffer
= ObjectName
;
1328 Status
= STATUS_SUCCESS
;
1332 /* Start by adding the Object's Name */
1333 ObjectName
= (PWCH
)((ULONG_PTR
)ObjectName
-
1334 LocalInfo
->Name
.Length
);
1335 RtlCopyMemory(ObjectName
,
1336 LocalInfo
->Name
.Buffer
,
1337 LocalInfo
->Name
.Length
);
1339 /* Now parse the Parent directories until we reach the top */
1340 ParentDirectory
= LocalInfo
->Directory
;
1341 while ((ParentDirectory
!= ObpRootDirectoryObject
) && (ParentDirectory
))
1343 /* Get the name information */
1344 LocalInfo
= OBJECT_HEADER_TO_NAME_INFO(
1345 OBJECT_TO_OBJECT_HEADER(ParentDirectory
));
1348 *(--ObjectName
) = OBJ_NAME_PATH_SEPARATOR
;
1350 /* Add the Parent Directory's Name */
1351 if (LocalInfo
&& LocalInfo
->Name
.Buffer
)
1354 ObjectName
= (PWCH
)((ULONG_PTR
)ObjectName
-
1355 LocalInfo
->Name
.Length
);
1356 RtlCopyMemory(ObjectName
,
1357 LocalInfo
->Name
.Buffer
,
1358 LocalInfo
->Name
.Length
);
1360 /* Move to next parent */
1361 ParentDirectory
= LocalInfo
->Directory
;
1365 /* Directory without a name, we add "..." */
1366 ObjectName
= (PWCH
)((ULONG_PTR
)ObjectName
-
1368 sizeof(UNICODE_NULL
));
1369 RtlCopyMemory(ObjectName
, L
"...", sizeof(L
"..."));
1374 /* Add Root Directory Name */
1375 *(--ObjectName
) = OBJ_NAME_PATH_SEPARATOR
;
1376 ObjectNameInfo
->Name
.Length
= (USHORT
)NameSize
;
1377 ObjectNameInfo
->Name
.MaximumLength
=
1378 (USHORT
)(NameSize
+ sizeof(UNICODE_NULL
));
1379 ObjectNameInfo
->Name
.Buffer
= ObjectName
;
1382 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1384 /* Return the exception code */
1385 Status
= _SEH2_GetExceptionCode();
1389 /* Return success */