Respect OBJ_OPENIF flag in ObCreateObject
[reactos.git] / reactos / ntoskrnl / ob / object.c
index aa9bf1e..b128e9e 100644 (file)
@@ -1,14 +1,12 @@
 /* $Id$
  * 
- * COPYRIGHT:     See COPYING in the top level directory
- * PROJECT:       ReactOS kernel
- * FILE:          ntoskrnl/ob/object.c
- * PURPOSE:       Implements generic object managment functions
- * PROGRAMMERS    David Welch (welch@cwcom.net), Skywing (skywing@valhallalegends.com)
- * UPDATE HISTORY:
- *               10/06/98: Created
- *               09/13/03: Fixed various ObXxx routines to not call retention
- *                         checks directly at a raised IRQL.
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS kernel
+ * FILE:            ntoskrnl/ob/object.c
+ * PURPOSE:         Implements generic object managment functions
+ * 
+ * PROGRAMMERS:     David Welch (welch@cwcom.net)
+ *                  Skywing (skywing@valhallalegends.com)
  */
 
 /* INCLUDES *****************************************************************/
@@ -27,18 +25,6 @@ typedef struct _RETENTION_CHECK_PARAMS
 
 /* FUNCTIONS ************************************************************/
 
-PVOID HEADER_TO_BODY(POBJECT_HEADER obj)
-{
-  return(((char*)obj)+sizeof(OBJECT_HEADER)-sizeof(COMMON_BODY_HEADER));
-}
-
-
-POBJECT_HEADER BODY_TO_HEADER(PVOID body)
-{
-  PCOMMON_BODY_HEADER chdr = (PCOMMON_BODY_HEADER)body;
-  return(CONTAINING_RECORD((&(chdr->Type)),OBJECT_HEADER,Type));
-}
-
 NTSTATUS
 ObpCaptureObjectAttributes(IN POBJECT_ATTRIBUTES ObjectAttributes  OPTIONAL,
                            IN KPROCESSOR_MODE AccessMode,
@@ -49,24 +35,16 @@ ObpCaptureObjectAttributes(IN POBJECT_ATTRIBUTES ObjectAttributes  OPTIONAL,
 {
   OBJECT_ATTRIBUTES AttributesCopy;
   NTSTATUS Status = STATUS_SUCCESS;
-  
+
   /* at least one output parameter must be != NULL! */
-  ASSERT(((ULONG_PTR)CapturedObjectAttributes ^ (ULONG_PTR)ObjectName) != 0);
-  
+  ASSERT(CapturedObjectAttributes != NULL || ObjectName != NULL);
+
   if(ObjectAttributes == NULL)
   {
-failbasiccleanup:
-    if(ObjectName != NULL)
-    {
-      RtlInitUnicodeString(ObjectName, NULL);
-    }
-    if(CapturedObjectAttributes != NULL)
-    {
-      RtlZeroMemory(CapturedObjectAttributes, sizeof(CAPTURED_OBJECT_ATTRIBUTES));
-    }
-    return Status; /* STATUS_SUCCESS */
+    /* we're going to return STATUS_SUCCESS! */
+    goto failbasiccleanup;
   }
-  
+
   if(AccessMode != KernelMode)
   {
     _SEH_TRY
@@ -82,27 +60,37 @@ failbasiccleanup:
       Status = _SEH_GetExceptionCode();
     }
     _SEH_END;
-    
+
     if(!NT_SUCCESS(Status))
     {
-      return Status;
+      DPRINT1("ObpCaptureObjectAttributes failed to probe object attributes\n");
+      goto failbasiccleanup;
     }
   }
-  else if(AccessMode == KernelMode && !CaptureIfKernel)
+  else if(!CaptureIfKernel)
   {
-    if(ObjectAttributes->Length != sizeof(OBJECT_ATTRIBUTES))
+    if(ObjectAttributes->Length == sizeof(OBJECT_ATTRIBUTES))
     {
-      /* we don't have to capture any memory, the caller considers the passed data
-         as valid */
       if(ObjectName != NULL)
       {
-        *ObjectName = *ObjectAttributes->ObjectName;
+        /* we don't have to capture any memory, the caller considers the passed data
+           as valid */
+        if(ObjectAttributes->ObjectName != NULL)
+        {
+          *ObjectName = *ObjectAttributes->ObjectName;
+        }
+        else
+        {
+          ObjectName->Length = ObjectName->MaximumLength = 0;
+          ObjectName->Buffer = NULL;
+        }
       }
       if(CapturedObjectAttributes != NULL)
       {
         CapturedObjectAttributes->RootDirectory = ObjectAttributes->RootDirectory;
         CapturedObjectAttributes->Attributes = ObjectAttributes->Attributes;
         CapturedObjectAttributes->SecurityDescriptor = ObjectAttributes->SecurityDescriptor;
+        CapturedObjectAttributes->SecurityQualityOfService = ObjectAttributes->SecurityQualityOfService;
       }
 
       return STATUS_SUCCESS;
@@ -117,7 +105,7 @@ failbasiccleanup:
   {
     AttributesCopy = *ObjectAttributes;
   }
-  
+
   /* if Length isn't as expected, bail with an invalid parameter status code so
      the caller knows he passed garbage... */
   if(AttributesCopy.Length != sizeof(OBJECT_ATTRIBUTES))
@@ -125,7 +113,7 @@ failbasiccleanup:
     Status = STATUS_INVALID_PARAMETER;
     goto failbasiccleanup;
   }
-  
+
   if(CapturedObjectAttributes != NULL)
   {
     CapturedObjectAttributes->RootDirectory = AttributesCopy.RootDirectory;
@@ -148,14 +136,61 @@ failbasiccleanup:
     {
       CapturedObjectAttributes->SecurityDescriptor = NULL;
     }
+
+    if(AttributesCopy.SecurityQualityOfService != NULL)
+    {
+      SECURITY_QUALITY_OF_SERVICE SafeQoS;
+
+      _SEH_TRY
+      {
+        ProbeForRead(AttributesCopy.SecurityQualityOfService,
+                     sizeof(SECURITY_QUALITY_OF_SERVICE),
+                     sizeof(ULONG));
+        SafeQoS = *(PSECURITY_QUALITY_OF_SERVICE)AttributesCopy.SecurityQualityOfService;
+      }
+      _SEH_HANDLE
+      {
+        Status = _SEH_GetExceptionCode();
+      }
+      _SEH_END;
+
+      if(!NT_SUCCESS(Status))
+      {
+        DPRINT1("Unable to capture QoS!!!\n");
+        goto failcleanupsdescriptor;
+      }
+
+      if(SafeQoS.Length != sizeof(SECURITY_QUALITY_OF_SERVICE))
+      {
+        DPRINT1("Unable to capture QoS, wrong size!!!\n");
+        Status = STATUS_INVALID_PARAMETER;
+        goto failcleanupsdescriptor;
+      }
+
+      CapturedObjectAttributes->SecurityQualityOfService = ExAllocatePool(PoolType,
+                                                                          sizeof(SECURITY_QUALITY_OF_SERVICE));
+      if(CapturedObjectAttributes->SecurityQualityOfService != NULL)
+      {
+        *CapturedObjectAttributes->SecurityQualityOfService = SafeQoS;
+      }
+      else
+      {
+        Status = STATUS_INSUFFICIENT_RESOURCES;
+        goto failcleanupsdescriptor;
+      }
+    }
+    else
+    {
+      CapturedObjectAttributes->SecurityQualityOfService = NULL;
+    }
   }
-  
+
   if(ObjectName != NULL)
   {
     if(AttributesCopy.ObjectName != NULL)
     {
       UNICODE_STRING OriginalCopy;
-      
+
       if(AccessMode != KernelMode)
       {
         _SEH_TRY
@@ -169,7 +204,7 @@ failbasiccleanup:
           {
             ProbeForRead(OriginalCopy.Buffer,
                          OriginalCopy.Length,
-                         sizeof(ULONG));
+                         sizeof(WCHAR));
           }
         }
         _SEH_HANDLE
@@ -177,7 +212,7 @@ failbasiccleanup:
           Status = _SEH_GetExceptionCode();
         }
         _SEH_END;
-        
+
         if(NT_SUCCESS(Status))
         {
           if(OriginalCopy.Length > 0)
@@ -199,6 +234,11 @@ failbasiccleanup:
                 Status = _SEH_GetExceptionCode();
               }
               _SEH_END;
+
+              if(!NT_SUCCESS(Status))
+              {
+                DPRINT1("ObpCaptureObjectAttributes failed to copy the unicode string!\n");
+              }
             }
             else
             {
@@ -211,29 +251,15 @@ failbasiccleanup:
             Status = STATUS_OBJECT_NAME_INVALID;
           }
         }
-
-        /* handle failure */
-        if(!NT_SUCCESS(Status))
+        else
         {
-failallocatedcleanup:
-          if(ObjectName->Buffer)
-          {
-            ExFreePool(ObjectName->Buffer);
-          }
-          if(CapturedObjectAttributes != NULL)
-          {
-            /* cleanup allocated resources */
-            SeReleaseSecurityDescriptor(CapturedObjectAttributes->SecurityDescriptor,
-                                        AccessMode,
-                                        TRUE);
-          }
-          goto failbasiccleanup;
+          DPRINT1("ObpCaptureObjectAttributes failed to probe the object name UNICODE_STRING structure!\n");
         }
       }
       else /* AccessMode == KernelMode */
       {
         OriginalCopy = *AttributesCopy.ObjectName;
-        
+
         if(OriginalCopy.Length > 0)
         {
           ObjectName->MaximumLength = OriginalCopy.Length + sizeof(WCHAR);
@@ -254,22 +280,47 @@ failallocatedcleanup:
           /* if the caller specified a root directory, there must be an object name! */
           Status = STATUS_OBJECT_NAME_INVALID;
         }
-        
-        if(!NT_SUCCESS(Status))
-        {
-          goto failallocatedcleanup;
-        }
       }
     }
     else
     {
-      RtlInitUnicodeString(ObjectName, NULL);
+      ObjectName->Length = ObjectName->MaximumLength = 0;
+      ObjectName->Buffer = NULL;
     }
   }
-  
+
+  if(!NT_SUCCESS(Status))
+  {
+    if(ObjectName->Buffer)
+    {
+      ExFreePool(ObjectName->Buffer);
+    }
+
+failcleanupsdescriptor:
+    if(CapturedObjectAttributes != NULL)
+    {
+      /* cleanup allocated resources */
+      SeReleaseSecurityDescriptor(CapturedObjectAttributes->SecurityDescriptor,
+                                  AccessMode,
+                                  TRUE);
+    }
+
+failbasiccleanup:
+    if(ObjectName != NULL)
+    {
+      ObjectName->Length = ObjectName->MaximumLength = 0;
+      ObjectName->Buffer = NULL;
+    }
+    if(CapturedObjectAttributes != NULL)
+    {
+      RtlZeroMemory(CapturedObjectAttributes, sizeof(CAPTURED_OBJECT_ATTRIBUTES));
+    }
+  }
+
   return Status;
 }
 
+
 VOID
 ObpReleaseObjectAttributes(IN PCAPTURED_OBJECT_ATTRIBUTES CapturedObjectAttributes  OPTIONAL,
                            IN PUNICODE_STRING ObjectName  OPTIONAL,
@@ -278,14 +329,20 @@ ObpReleaseObjectAttributes(IN PCAPTURED_OBJECT_ATTRIBUTES CapturedObjectAttribut
 {
   /* WARNING - You need to pass the same parameters to this function as you passed
                to ObpCaptureObjectAttributes() to avoid memory leaks */
-  if(AccessMode != KernelMode ||
-     (AccessMode == KernelMode && CaptureIfKernel))
+  if(AccessMode != KernelMode || CaptureIfKernel)
   {
-    if(CapturedObjectAttributes != NULL &&
-       CapturedObjectAttributes->SecurityDescriptor != NULL)
+    if(CapturedObjectAttributes != NULL)
     {
-      ExFreePool(CapturedObjectAttributes->SecurityDescriptor);
-      CapturedObjectAttributes->SecurityDescriptor = NULL;
+      if(CapturedObjectAttributes->SecurityDescriptor != NULL)
+      {
+        ExFreePool(CapturedObjectAttributes->SecurityDescriptor);
+        CapturedObjectAttributes->SecurityDescriptor = NULL;
+      }
+      if(CapturedObjectAttributes->SecurityQualityOfService != NULL)
+      {
+        ExFreePool(CapturedObjectAttributes->SecurityQualityOfService);
+        CapturedObjectAttributes->SecurityQualityOfService = NULL;
+      }
     }
     if(ObjectName != NULL &&
        ObjectName->Length > 0)
@@ -334,6 +391,8 @@ ObFindObject(POBJECT_ATTRIBUTES ObjectAttributes,
   UNICODE_STRING PathString;
   ULONG Attributes;
   PUNICODE_STRING ObjectName;
+  
+  PAGED_CODE();
 
   DPRINT("ObFindObject(ObjectAttributes %x, ReturnedObject %x, "
         "RemainingPath %x)\n",ObjectAttributes,ReturnedObject,RemainingPath);
@@ -353,7 +412,7 @@ ObFindObject(POBJECT_ATTRIBUTES ObjectAttributes,
   else
     {
       Status = ObReferenceObjectByHandle(ObjectAttributes->RootDirectory,
-                                        DIRECTORY_TRAVERSE,
+                                        0,
                                         NULL,
                                         UserMode,
                                         &CurrentObject,
@@ -441,7 +500,7 @@ ObFindObject(POBJECT_ATTRIBUTES ObjectAttributes,
     }
 
   if (current)
-     RtlCreateUnicodeString (RemainingPath, current);
+     RtlpCreateUnicodeString (RemainingPath, current, NonPagedPool);
   RtlFreeUnicodeString (&PathString);
   *ReturnedObject = CurrentObject;
 
@@ -471,6 +530,8 @@ ObQueryNameString (IN PVOID Object,
   POBJECT_HEADER ObjectHeader;
   ULONG LocalReturnLength;
   NTSTATUS Status;
+  
+  PAGED_CODE();
 
   *ReturnLength = 0;
 
@@ -599,7 +660,7 @@ ObCreateObject (IN KPROCESSOR_MODE ObjectAttributesAccessMode OPTIONAL,
   PSECURITY_DESCRIPTOR NewSecurityDescriptor = NULL;
   SECURITY_SUBJECT_CONTEXT SubjectContext;
 
-  ASSERT_IRQL(APC_LEVEL);
+  PAGED_CODE();
   
   if(ObjectAttributesAccessMode == UserMode && ObjectAttributes != NULL)
   {
@@ -644,6 +705,22 @@ ObCreateObject (IN KPROCESSOR_MODE ObjectAttributesAccessMode OPTIONAL,
          DPRINT1("ObFindObject() failed! (Status 0x%x)\n", Status);
          return Status;
        }
+      if (Parent != NULL)
+        {
+          ParentHeader = BODY_TO_HEADER(Parent);
+        }
+      if (ParentHeader &&
+         RemainingPath.Buffer == NULL)
+        {
+         if (ParentHeader->ObjectType != Type
+               || !(ObjectAttributes->Attributes & OBJ_OPENIF))
+           {
+              ObDereferenceObject(Parent);
+             return STATUS_OBJECT_NAME_COLLISION;
+           }
+         *Object = Parent;
+          return STATUS_OBJECT_EXISTS;
+       }
     }
   else
     {
@@ -661,7 +738,7 @@ ObCreateObject (IN KPROCESSOR_MODE ObjectAttributesAccessMode OPTIONAL,
   RtlZeroMemory(Header, OBJECT_ALLOC_SIZE(ObjectSize));
 
   /* Initialize the object header */
-  DPRINT("Initalizing header\n");
+  DPRINT("Initalizing header 0x%x (%wZ)\n", Header, &Type->TypeName);
   Header->HandleCount = 0;
   Header->RefCount = 1;
   Header->ObjectType = Type;
@@ -688,11 +765,6 @@ ObCreateObject (IN KPROCESSOR_MODE ObjectAttributesAccessMode OPTIONAL,
   RtlInitUnicodeString(&(Header->Name),NULL);
 
   DPRINT("Getting Parent and adding entry\n");
-  if (Parent != NULL)
-    {
-      ParentHeader = BODY_TO_HEADER(Parent);
-    }
-
   if (ParentHeader != NULL &&
       ParentHeader->ObjectType == ObDirectoryType &&
       RemainingPath.Buffer != NULL)
@@ -802,6 +874,8 @@ ObReferenceObjectByPointer(IN PVOID Object,
                           IN KPROCESSOR_MODE AccessMode)
 {
    POBJECT_HEADER Header;
+   
+   /* NOTE: should be possible to reference an object above APC_LEVEL! */
 
    DPRINT("ObReferenceObjectByPointer(Object %x, ObjectType %x)\n",
          Object,ObjectType);
@@ -831,7 +905,7 @@ ObReferenceObjectByPointer(IN PVOID Object,
        DPRINT("eip %x\n", ((PULONG)&Object)[-1]);
      }
  
-   if (Header->CloseInProcess)
+   if (Header->RefCount == 0 && !Header->Permanent)
    {
       if (Header->ObjectType == PsProcessType)
         {
@@ -844,7 +918,10 @@ ObReferenceObjectByPointer(IN PVOID Object,
       return(STATUS_UNSUCCESSFUL);
    }
 
-   InterlockedIncrement(&Header->RefCount);
+   if (1 == InterlockedIncrement(&Header->RefCount) && !Header->Permanent)
+   {
+      KEBUGCHECK(0);
+   }
    
    return(STATUS_SUCCESS);
 }
@@ -864,6 +941,8 @@ ObOpenObjectByPointer(IN POBJECT Object,
 {
    NTSTATUS Status;
    
+   PAGED_CODE();
+   
    DPRINT("ObOpenObjectByPointer()\n");
    
    Status = ObReferenceObjectByPointer(Object,
@@ -958,12 +1037,6 @@ ObpDeleteObjectDpcLevel(IN POBJECT_HEADER ObjectHeader,
       KEBUGCHECK(0);
     }
 
-  if (ObjectHeader->CloseInProcess)
-    {
-      KEBUGCHECK(0);
-      return STATUS_UNSUCCESSFUL;
-    }
-  ObjectHeader->CloseInProcess = TRUE;
   
   switch (KeGetCurrentIrql ())
     {
@@ -1029,12 +1102,11 @@ ObfReferenceObject(IN PVOID Object)
   Header = BODY_TO_HEADER(Object);
 
   /* No one should be referencing an object once we are deleting it. */
-  if (Header->CloseInProcess)
-    {
-      KEBUGCHECK(0);
-    }
+  if (InterlockedIncrement(&Header->RefCount) == 1 && !Header->Permanent)
+  {
+     KEBUGCHECK(0);
+  }
 
-  (VOID)InterlockedIncrement(&Header->RefCount);
 }
 
 
@@ -1060,25 +1132,23 @@ ObfDereferenceObject(IN PVOID Object)
   POBJECT_HEADER Header;
   LONG NewRefCount;
   BOOL Permanent;
-  ULONG HandleCount;
 
   ASSERT(Object);
 
   /* Extract the object header. */
   Header = BODY_TO_HEADER(Object);
   Permanent = Header->Permanent;
-  HandleCount = Header->HandleCount;
 
   /* 
      Drop our reference and get the new count so we can tell if this was the
      last reference.
   */
   NewRefCount = InterlockedDecrement(&Header->RefCount);
+  DPRINT("ObfDereferenceObject(0x%x)==%d (%wZ)\n", Object, NewRefCount, &Header->ObjectType->TypeName);
   ASSERT(NewRefCount >= 0);
 
   /* Check whether the object can now be deleted. */
   if (NewRefCount == 0 &&
-      HandleCount == 0 &&
       !Permanent)
     {
       ObpDeleteObjectDpcLevel(Header, NewRefCount);
@@ -1105,6 +1175,8 @@ ULONG STDCALL
 ObGetObjectPointerCount(PVOID Object)
 {
   POBJECT_HEADER Header;
+  
+  PAGED_CODE();
 
   ASSERT(Object);
   Header = BODY_TO_HEADER(Object);
@@ -1130,6 +1202,8 @@ ULONG
 ObGetObjectHandleCount(PVOID Object)
 {
   POBJECT_HEADER Header;
+  
+  PAGED_CODE();
 
   ASSERT(Object);
   Header = BODY_TO_HEADER(Object);