preliminary comment out the self-modifying code for RtlPrefetchMemoryNonTemporal
[reactos.git] / reactos / ntoskrnl / se / semgr.c
index 626c8e5..7b10fae 100644 (file)
@@ -1,12 +1,11 @@
-/* $Id: semgr.c,v 1.48 2004/10/22 20:48:00 ekohl Exp $
+/* $Id$
  *
- * COPYRIGHT:         See COPYING in the top level directory
- * PROJECT:           ReactOS kernel
- * PURPOSE:           Security manager
- * FILE:              kernel/se/semgr.c
- * PROGRAMER:         ?
- * REVISION HISTORY:
- *                 26/07/98: Added stubs for security functions
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS kernel
+ * FILE:            ntoskrnl/se/semgr.c
+ * PURPOSE:         Security manager
+ *
+ * PROGRAMMERS:     No programmer listed.
  */
 
 /* INCLUDES *****************************************************************/
 #define NDEBUG
 #include <internal/debug.h>
 
-#define TAG_SXPT   TAG('S', 'X', 'P', 'T')
-
-
 /* GLOBALS ******************************************************************/
 
-PSE_EXPORTS EXPORTED SeExports = NULL;
+PSE_EXPORTS SeExports = NULL;
+SE_EXPORTS SepExports;
 
 static ERESOURCE SepSubjectContextLock;
 
@@ -29,10 +26,17 @@ static ERESOURCE SepSubjectContextLock;
 
 static BOOLEAN SepInitExports(VOID);
 
+#if defined (ALLOC_PRAGMA)
+#pragma alloc_text(INIT, SeInit1)
+#pragma alloc_text(INIT, SeInit2)
+#pragma alloc_text(INIT, SepInitExports)
+#endif
 
 /* FUNCTIONS ****************************************************************/
 
-BOOLEAN INIT_FUNCTION
+BOOLEAN 
+INIT_FUNCTION
+NTAPI
 SeInit1(VOID)
 {
   SepInitLuid();
@@ -58,7 +62,9 @@ SeInit1(VOID)
 }
 
 
-BOOLEAN INIT_FUNCTION
+BOOLEAN
+INIT_FUNCTION
+NTAPI
 SeInit2(VOID)
 {
   SepInitializeTokenImplementation();
@@ -68,6 +74,7 @@ SeInit2(VOID)
 
 
 BOOLEAN
+NTAPI
 SeInitSRM(VOID)
 {
   OBJECT_ATTRIBUTES ObjectAttributes;
@@ -84,7 +91,7 @@ SeInitSRM(VOID)
                             OBJ_PERMANENT,
                             0,
                             NULL);
-  Status = NtCreateDirectoryObject(&DirectoryHandle,
+  Status = ZwCreateDirectoryObject(&DirectoryHandle,
                                   DIRECTORY_ALL_ACCESS,
                                   &ObjectAttributes);
   if (!NT_SUCCESS(Status))
@@ -101,10 +108,10 @@ SeInitSRM(VOID)
                             OBJ_PERMANENT,
                             DirectoryHandle,
                             SePublicDefaultSd);
-  Status = NtCreateEvent(&EventHandle,
+  Status = ZwCreateEvent(&EventHandle,
                         EVENT_ALL_ACCESS,
                         &ObjectAttributes,
-                        FALSE,
+                        SynchronizationEvent,
                         FALSE);
   if (!NT_SUCCESS(Status))
     {
@@ -113,8 +120,8 @@ SeInitSRM(VOID)
       return FALSE;
     }
 
-  NtClose(EventHandle);
-  NtClose(DirectoryHandle);
+  ZwClose(EventHandle);
+  ZwClose(DirectoryHandle);
 
   /* FIXME: Create SRM port and listener thread */
 
@@ -125,56 +132,58 @@ SeInitSRM(VOID)
 static BOOLEAN INIT_FUNCTION
 SepInitExports(VOID)
 {
-  SeExports = ExAllocatePoolWithTag(NonPagedPool,
-                                   sizeof(SE_EXPORTS),
-                                   TAG_SXPT);
-  if (SeExports == NULL)
-    return FALSE;
-
-  SeExports->SeCreateTokenPrivilege = SeCreateTokenPrivilege;
-  SeExports->SeAssignPrimaryTokenPrivilege = SeAssignPrimaryTokenPrivilege;
-  SeExports->SeLockMemoryPrivilege = SeLockMemoryPrivilege;
-  SeExports->SeIncreaseQuotaPrivilege = SeIncreaseQuotaPrivilege;
-  SeExports->SeUnsolicitedInputPrivilege = SeUnsolicitedInputPrivilege;
-  SeExports->SeTcbPrivilege = SeTcbPrivilege;
-  SeExports->SeSecurityPrivilege = SeSecurityPrivilege;
-  SeExports->SeTakeOwnershipPrivilege = SeTakeOwnershipPrivilege;
-  SeExports->SeLoadDriverPrivilege = SeLoadDriverPrivilege;
-  SeExports->SeCreatePagefilePrivilege = SeCreatePagefilePrivilege;
-  SeExports->SeIncreaseBasePriorityPrivilege = SeIncreaseBasePriorityPrivilege;
-  SeExports->SeSystemProfilePrivilege = SeSystemProfilePrivilege;
-  SeExports->SeSystemtimePrivilege = SeSystemtimePrivilege;
-  SeExports->SeProfileSingleProcessPrivilege = SeProfileSingleProcessPrivilege;
-  SeExports->SeCreatePermanentPrivilege = SeCreatePermanentPrivilege;
-  SeExports->SeBackupPrivilege = SeBackupPrivilege;
-  SeExports->SeRestorePrivilege = SeRestorePrivilege;
-  SeExports->SeShutdownPrivilege = SeShutdownPrivilege;
-  SeExports->SeDebugPrivilege = SeDebugPrivilege;
-  SeExports->SeAuditPrivilege = SeAuditPrivilege;
-  SeExports->SeSystemEnvironmentPrivilege = SeSystemEnvironmentPrivilege;
-  SeExports->SeChangeNotifyPrivilege = SeChangeNotifyPrivilege;
-  SeExports->SeRemoteShutdownPrivilege = SeRemoteShutdownPrivilege;
-
-  SeExports->SeNullSid = SeNullSid;
-  SeExports->SeWorldSid = SeWorldSid;
-  SeExports->SeLocalSid = SeLocalSid;
-  SeExports->SeCreatorOwnerSid = SeCreatorOwnerSid;
-  SeExports->SeCreatorGroupSid = SeCreatorGroupSid;
-  SeExports->SeNtAuthoritySid = SeNtAuthoritySid;
-  SeExports->SeDialupSid = SeDialupSid;
-  SeExports->SeNetworkSid = SeNetworkSid;
-  SeExports->SeBatchSid = SeBatchSid;
-  SeExports->SeInteractiveSid = SeInteractiveSid;
-  SeExports->SeLocalSystemSid = SeLocalSystemSid;
-  SeExports->SeAliasAdminsSid = SeAliasAdminsSid;
-  SeExports->SeAliasUsersSid = SeAliasUsersSid;
-  SeExports->SeAliasGuestsSid = SeAliasGuestsSid;
-  SeExports->SeAliasPowerUsersSid = SeAliasPowerUsersSid;
-  SeExports->SeAliasAccountOpsSid = SeAliasAccountOpsSid;
-  SeExports->SeAliasSystemOpsSid = SeAliasSystemOpsSid;
-  SeExports->SeAliasPrintOpsSid = SeAliasPrintOpsSid;
-  SeExports->SeAliasBackupOpsSid = SeAliasBackupOpsSid;
-
+  SepExports.SeCreateTokenPrivilege = SeCreateTokenPrivilege;
+  SepExports.SeAssignPrimaryTokenPrivilege = SeAssignPrimaryTokenPrivilege;
+  SepExports.SeLockMemoryPrivilege = SeLockMemoryPrivilege;
+  SepExports.SeIncreaseQuotaPrivilege = SeIncreaseQuotaPrivilege;
+  SepExports.SeUnsolicitedInputPrivilege = SeUnsolicitedInputPrivilege;
+  SepExports.SeTcbPrivilege = SeTcbPrivilege;
+  SepExports.SeSecurityPrivilege = SeSecurityPrivilege;
+  SepExports.SeTakeOwnershipPrivilege = SeTakeOwnershipPrivilege;
+  SepExports.SeLoadDriverPrivilege = SeLoadDriverPrivilege;
+  SepExports.SeCreatePagefilePrivilege = SeCreatePagefilePrivilege;
+  SepExports.SeIncreaseBasePriorityPrivilege = SeIncreaseBasePriorityPrivilege;
+  SepExports.SeSystemProfilePrivilege = SeSystemProfilePrivilege;
+  SepExports.SeSystemtimePrivilege = SeSystemtimePrivilege;
+  SepExports.SeProfileSingleProcessPrivilege = SeProfileSingleProcessPrivilege;
+  SepExports.SeCreatePermanentPrivilege = SeCreatePermanentPrivilege;
+  SepExports.SeBackupPrivilege = SeBackupPrivilege;
+  SepExports.SeRestorePrivilege = SeRestorePrivilege;
+  SepExports.SeShutdownPrivilege = SeShutdownPrivilege;
+  SepExports.SeDebugPrivilege = SeDebugPrivilege;
+  SepExports.SeAuditPrivilege = SeAuditPrivilege;
+  SepExports.SeSystemEnvironmentPrivilege = SeSystemEnvironmentPrivilege;
+  SepExports.SeChangeNotifyPrivilege = SeChangeNotifyPrivilege;
+  SepExports.SeRemoteShutdownPrivilege = SeRemoteShutdownPrivilege;
+
+  SepExports.SeNullSid = SeNullSid;
+  SepExports.SeWorldSid = SeWorldSid;
+  SepExports.SeLocalSid = SeLocalSid;
+  SepExports.SeCreatorOwnerSid = SeCreatorOwnerSid;
+  SepExports.SeCreatorGroupSid = SeCreatorGroupSid;
+  SepExports.SeNtAuthoritySid = SeNtAuthoritySid;
+  SepExports.SeDialupSid = SeDialupSid;
+  SepExports.SeNetworkSid = SeNetworkSid;
+  SepExports.SeBatchSid = SeBatchSid;
+  SepExports.SeInteractiveSid = SeInteractiveSid;
+  SepExports.SeLocalSystemSid = SeLocalSystemSid;
+  SepExports.SeAliasAdminsSid = SeAliasAdminsSid;
+  SepExports.SeAliasUsersSid = SeAliasUsersSid;
+  SepExports.SeAliasGuestsSid = SeAliasGuestsSid;
+  SepExports.SeAliasPowerUsersSid = SeAliasPowerUsersSid;
+  SepExports.SeAliasAccountOpsSid = SeAliasAccountOpsSid;
+  SepExports.SeAliasSystemOpsSid = SeAliasSystemOpsSid;
+  SepExports.SeAliasPrintOpsSid = SeAliasPrintOpsSid;
+  SepExports.SeAliasBackupOpsSid = SeAliasBackupOpsSid;
+  SepExports.SeAuthenticatedUsersSid = SeAuthenticatedUsersSid;
+  SepExports.SeRestrictedSid = SeRestrictedSid;
+  SepExports.SeAnonymousLogonSid = SeAnonymousLogonSid;
+
+  SepExports.SeUndockPrivilege = SeUndockPrivilege;
+  SepExports.SeSyncAgentPrivilege = SeSyncAgentPrivilege;
+  SepExports.SeEnableDelegationPrivilege = SeEnableDelegationPrivilege;
+  
+  SeExports = &SepExports;
   return TRUE;
 }
 
@@ -189,20 +198,223 @@ VOID SepDeReferenceLogonSession(PLUID AuthenticationId)
    UNIMPLEMENTED;
 }
 
+NTSTATUS
+STDCALL
+SeDefaultObjectMethod(PVOID Object,
+                      SECURITY_OPERATION_CODE OperationType,
+                      SECURITY_INFORMATION SecurityInformation,
+                      PSECURITY_DESCRIPTOR _SecurityDescriptor,
+                      PULONG ReturnLength,
+                      PSECURITY_DESCRIPTOR *OldSecurityDescriptor,
+                      POOL_TYPE PoolType,
+                      PGENERIC_MAPPING GenericMapping)
+{
+  PISECURITY_DESCRIPTOR ObjectSd;
+  PISECURITY_DESCRIPTOR NewSd;
+  PISECURITY_DESCRIPTOR SecurityDescriptor = _SecurityDescriptor;
+  POBJECT_HEADER Header = BODY_TO_HEADER(Object);
+  PSID Owner = 0;
+  PSID Group = 0;
+  PACL Dacl = 0;
+  PACL Sacl = 0;
+  ULONG OwnerLength = 0;
+  ULONG GroupLength = 0;
+  ULONG DaclLength = 0;
+  ULONG SaclLength = 0;
+  ULONG Control = 0;
+  ULONG_PTR Current;
+  NTSTATUS Status;
 
+    if (OperationType == SetSecurityDescriptor)
+    {
+        ObjectSd = Header->SecurityDescriptor;
 
-/*
- * @unimplemented
- */
-NTSTATUS STDCALL
-NtAllocateUuids(PULARGE_INTEGER Time,
-               PULONG Range,
-               PULONG Sequence)
-{
-  UNIMPLEMENTED;
-  return(STATUS_NOT_IMPLEMENTED);
-}
+      /* Get owner and owner size */
+      if (SecurityInformation & OWNER_SECURITY_INFORMATION)
+       {
+         if (SecurityDescriptor->Owner != NULL)
+           {
+               if( SecurityDescriptor->Control & SE_SELF_RELATIVE )
+                   Owner = (PSID)((ULONG_PTR)SecurityDescriptor->Owner +
+                                  (ULONG_PTR)SecurityDescriptor);
+               else
+                   Owner = (PSID)SecurityDescriptor->Owner;
+               OwnerLength = ROUND_UP(RtlLengthSid(Owner), 4);
+           }
+         Control |= (SecurityDescriptor->Control & SE_OWNER_DEFAULTED);
+       }
+      else
+       {
+         if (ObjectSd->Owner != NULL)
+         {
+             Owner = (PSID)((ULONG_PTR)ObjectSd->Owner + (ULONG_PTR)ObjectSd);
+             OwnerLength = ROUND_UP(RtlLengthSid(Owner), 4);
+         }
+         Control |= (ObjectSd->Control & SE_OWNER_DEFAULTED);
+       }
 
+      /* Get group and group size */
+      if (SecurityInformation & GROUP_SECURITY_INFORMATION)
+       {
+         if (SecurityDescriptor->Group != NULL)
+           {
+               if( SecurityDescriptor->Control & SE_SELF_RELATIVE )
+                   Group = (PSID)((ULONG_PTR)SecurityDescriptor->Group +
+                                  (ULONG_PTR)SecurityDescriptor);
+               else
+                   Group = (PSID)SecurityDescriptor->Group;
+               GroupLength = ROUND_UP(RtlLengthSid(Group), 4);
+           }
+         Control |= (SecurityDescriptor->Control & SE_GROUP_DEFAULTED);
+       }
+      else
+       {
+         if (ObjectSd->Group != NULL)
+           {
+             Group = (PSID)((ULONG_PTR)ObjectSd->Group + (ULONG_PTR)ObjectSd);
+             GroupLength = ROUND_UP(RtlLengthSid(Group), 4);
+           }
+         Control |= (ObjectSd->Control & SE_GROUP_DEFAULTED);
+       }
+
+      /* Get DACL and DACL size */
+      if (SecurityInformation & DACL_SECURITY_INFORMATION)
+       {
+         if ((SecurityDescriptor->Control & SE_DACL_PRESENT) &&
+             (SecurityDescriptor->Dacl != NULL))
+           {
+               if( SecurityDescriptor->Control & SE_SELF_RELATIVE )
+                   Dacl = (PACL)((ULONG_PTR)SecurityDescriptor->Dacl +
+                                 (ULONG_PTR)SecurityDescriptor);
+               else
+                   Dacl = (PACL)SecurityDescriptor->Dacl;
+
+             DaclLength = ROUND_UP((ULONG)Dacl->AclSize, 4);
+           }
+         Control |= (SecurityDescriptor->Control & (SE_DACL_DEFAULTED | SE_DACL_PRESENT));
+       }
+      else
+       {
+         if ((ObjectSd->Control & SE_DACL_PRESENT) &&
+             (ObjectSd->Dacl != NULL))
+           {
+             Dacl = (PACL)((ULONG_PTR)ObjectSd->Dacl + (ULONG_PTR)ObjectSd);
+             DaclLength = ROUND_UP((ULONG)Dacl->AclSize, 4);
+           }
+         Control |= (ObjectSd->Control & (SE_DACL_DEFAULTED | SE_DACL_PRESENT));
+       }
+
+      /* Get SACL and SACL size */
+      if (SecurityInformation & SACL_SECURITY_INFORMATION)
+       {
+         if ((SecurityDescriptor->Control & SE_SACL_PRESENT) &&
+             (SecurityDescriptor->Sacl != NULL))
+           {
+               if( SecurityDescriptor->Control & SE_SELF_RELATIVE )
+                   Sacl = (PACL)((ULONG_PTR)SecurityDescriptor->Sacl +
+                                 (ULONG_PTR)SecurityDescriptor);
+               else
+                   Sacl = (PACL)SecurityDescriptor->Sacl;
+               SaclLength = ROUND_UP((ULONG)Sacl->AclSize, 4);
+           }
+         Control |= (SecurityDescriptor->Control & (SE_SACL_DEFAULTED | SE_SACL_PRESENT));
+       }
+      else
+       {
+         if ((ObjectSd->Control & SE_SACL_PRESENT) &&
+             (ObjectSd->Sacl != NULL))
+           {
+             Sacl = (PACL)((ULONG_PTR)ObjectSd->Sacl + (ULONG_PTR)ObjectSd);
+             SaclLength = ROUND_UP((ULONG)Sacl->AclSize, 4);
+           }
+         Control |= (ObjectSd->Control & (SE_SACL_DEFAULTED | SE_SACL_PRESENT));
+       }
+
+      NewSd = ExAllocatePool(NonPagedPool,
+                            sizeof(SECURITY_DESCRIPTOR) + OwnerLength + GroupLength +
+                            DaclLength + SaclLength);
+      if (NewSd == NULL)
+       {
+         ObDereferenceObject(Object);
+         return STATUS_INSUFFICIENT_RESOURCES;
+       }
+
+      RtlCreateSecurityDescriptor(NewSd,
+                                 SECURITY_DESCRIPTOR_REVISION1);
+      /* We always build a self-relative descriptor */
+      NewSd->Control = Control | SE_SELF_RELATIVE;
+
+      Current = (ULONG_PTR)NewSd + sizeof(SECURITY_DESCRIPTOR);
+
+      if (OwnerLength != 0)
+       {
+         RtlCopyMemory((PVOID)Current,
+                       Owner,
+                       OwnerLength);
+         NewSd->Owner = (PSID)(Current - (ULONG_PTR)NewSd);
+         Current += OwnerLength;
+       }
+
+      if (GroupLength != 0)
+       {
+         RtlCopyMemory((PVOID)Current,
+                       Group,
+                       GroupLength);
+         NewSd->Group = (PSID)(Current - (ULONG_PTR)NewSd);
+         Current += GroupLength;
+       }
+
+      if (DaclLength != 0)
+       {
+         RtlCopyMemory((PVOID)Current,
+                       Dacl,
+                       DaclLength);
+         NewSd->Dacl = (PACL)(Current - (ULONG_PTR)NewSd);
+         Current += DaclLength;
+       }
+
+      if (SaclLength != 0)
+       {
+         RtlCopyMemory((PVOID)Current,
+                       Sacl,
+                       SaclLength);
+         NewSd->Sacl = (PACL)(Current - (ULONG_PTR)NewSd);
+         Current += SaclLength;
+       }
+
+      /* Add the new SD */
+      Status = ObpAddSecurityDescriptor(NewSd,
+                                       &Header->SecurityDescriptor);
+      if (NT_SUCCESS(Status))
+       {
+         /* Remove the old security descriptor */
+         ObpRemoveSecurityDescriptor(ObjectSd);
+       }
+      else
+       {
+         /* Restore the old security descriptor */
+         Header->SecurityDescriptor = ObjectSd;
+       }
+
+      ExFreePool(NewSd);
+    }
+    else if (OperationType == QuerySecurityDescriptor)
+    {
+        Status = SeQuerySecurityDescriptorInfo(&SecurityInformation,
+                                               SecurityDescriptor,
+                                               ReturnLength,
+                                               &Header->SecurityDescriptor);
+    }
+    else if (OperationType == AssignSecurityDescriptor)
+    {
+      /* Assign the security descriptor to the object header */
+      Status = ObpAddSecurityDescriptor(SecurityDescriptor,
+                                                       &Header->SecurityDescriptor);
+    }
+
+
+    return STATUS_SUCCESS;
+}
 
 /*
  * @implemented
@@ -214,6 +426,8 @@ SeCaptureSubjectContext(OUT PSECURITY_SUBJECT_CONTEXT SubjectContext)
   BOOLEAN CopyOnOpen;
   BOOLEAN EffectiveOnly;
 
+  PAGED_CODE();
+
   Thread = PsGetCurrentThread();
   if (Thread == NULL)
     {
@@ -225,7 +439,7 @@ SeCaptureSubjectContext(OUT PSECURITY_SUBJECT_CONTEXT SubjectContext)
   else
     {
       SubjectContext->ProcessAuditId = Thread->ThreadsProcess;
-      SubjectContext->ClientToken = 
+      SubjectContext->ClientToken =
        PsReferenceImpersonationToken(Thread,
                                      &CopyOnOpen,
                                      &EffectiveOnly,
@@ -241,6 +455,9 @@ SeCaptureSubjectContext(OUT PSECURITY_SUBJECT_CONTEXT SubjectContext)
 VOID STDCALL
 SeLockSubjectContext(IN PSECURITY_SUBJECT_CONTEXT SubjectContext)
 {
+  PAGED_CODE();
+
+  KeEnterCriticalRegion();
   ExAcquireResourceExclusiveLite(&SepSubjectContextLock, TRUE);
 }
 
@@ -251,7 +468,10 @@ SeLockSubjectContext(IN PSECURITY_SUBJECT_CONTEXT SubjectContext)
 VOID STDCALL
 SeUnlockSubjectContext(IN PSECURITY_SUBJECT_CONTEXT SubjectContext)
 {
+  PAGED_CODE();
+
   ExReleaseResourceLite(&SepSubjectContextLock);
+  KeLeaveCriticalRegion();
 }
 
 
@@ -261,6 +481,8 @@ SeUnlockSubjectContext(IN PSECURITY_SUBJECT_CONTEXT SubjectContext)
 VOID STDCALL
 SeReleaseSubjectContext(IN PSECURITY_SUBJECT_CONTEXT SubjectContext)
 {
+  PAGED_CODE();
+
   if (SubjectContext->PrimaryToken != NULL)
     {
       ObDereferenceObject(SubjectContext->PrimaryToken);
@@ -279,6 +501,8 @@ SeReleaseSubjectContext(IN PSECURITY_SUBJECT_CONTEXT SubjectContext)
 NTSTATUS STDCALL
 SeDeassignSecurity(PSECURITY_DESCRIPTOR *SecurityDescriptor)
 {
+  PAGED_CODE();
+
   if (*SecurityDescriptor != NULL)
     {
       ExFreePool(*SecurityDescriptor);
@@ -311,28 +535,30 @@ SeAssignSecurityEx(IN PSECURITY_DESCRIPTOR ParentDescriptor OPTIONAL,
 /*
  * FUNCTION: Creates a security descriptor for a new object.
  * ARGUMENTS:
- *         ParentDescriptor = 
- *         ExplicitDescriptor = 
- *         NewDescriptor = 
- *         IsDirectoryObject = 
- *         SubjectContext = 
- *         GeneralMapping = 
- *         PoolType = 
+ *         ParentDescriptor =
+ *         ExplicitDescriptor =
+ *         NewDescriptor =
+ *         IsDirectoryObject =
+ *         SubjectContext =
+ *         GeneralMapping =
+ *         PoolType =
  * RETURNS: Status
  *
  * @implemented
  */
 NTSTATUS STDCALL
-SeAssignSecurity(PSECURITY_DESCRIPTOR ParentDescriptor OPTIONAL,
-                PSECURITY_DESCRIPTOR ExplicitDescriptor OPTIONAL,
+SeAssignSecurity(PSECURITY_DESCRIPTOR _ParentDescriptor OPTIONAL,
+                PSECURITY_DESCRIPTOR _ExplicitDescriptor OPTIONAL,
                 PSECURITY_DESCRIPTOR *NewDescriptor,
                 BOOLEAN IsDirectoryObject,
                 PSECURITY_SUBJECT_CONTEXT SubjectContext,
                 PGENERIC_MAPPING GenericMapping,
                 POOL_TYPE PoolType)
 {
-  PSECURITY_DESCRIPTOR Descriptor;
-  PACCESS_TOKEN Token;
+  PISECURITY_DESCRIPTOR ParentDescriptor = _ParentDescriptor;
+  PISECURITY_DESCRIPTOR ExplicitDescriptor = _ExplicitDescriptor;
+  PISECURITY_DESCRIPTOR Descriptor;
+  PTOKEN Token;
   ULONG OwnerLength = 0;
   ULONG GroupLength = 0;
   ULONG DaclLength = 0;
@@ -345,6 +571,8 @@ SeAssignSecurity(PSECURITY_DESCRIPTOR ParentDescriptor OPTIONAL,
   PACL Dacl = NULL;
   PACL Sacl = NULL;
 
+  PAGED_CODE();
+
   /* Lock subject context */
   SeLockSubjectContext(SubjectContext);
 
@@ -363,9 +591,11 @@ SeAssignSecurity(PSECURITY_DESCRIPTOR ParentDescriptor OPTIONAL,
     {
       DPRINT("Use explicit owner sid!\n");
       Owner = ExplicitDescriptor->Owner;
+      
       if (ExplicitDescriptor->Control & SE_SELF_RELATIVE)
        {
          Owner = (PSID)(((ULONG_PTR)Owner) + (ULONG_PTR)ExplicitDescriptor);
+
        }
     }
   else
@@ -490,20 +720,18 @@ SeAssignSecurity(PSECURITY_DESCRIPTOR ParentDescriptor OPTIONAL,
 
 
   /* Allocate and initialize the new security descriptor */
-  Length = sizeof(SECURITY_DESCRIPTOR) + 
+  Length = sizeof(SECURITY_DESCRIPTOR) +
       OwnerLength + GroupLength + DaclLength + SaclLength;
 
-  DPRINT("L: sizeof(SECURITY_DESCRIPTOR) %d OwnerLength %d GroupLength %d DaclLength %d SaclLength %d\n", 
+  DPRINT("L: sizeof(SECURITY_DESCRIPTOR) %d OwnerLength %d GroupLength %d DaclLength %d SaclLength %d\n",
         sizeof(SECURITY_DESCRIPTOR),
         OwnerLength,
         GroupLength,
         DaclLength,
         SaclLength);
 
-  Descriptor = ExAllocatePool(NonPagedPool,
+  Descriptor = ExAllocatePool(PagedPool,
                              Length);
-  RtlZeroMemory( Descriptor, Length );
-
   if (Descriptor == NULL)
     {
       DPRINT1("ExAlloctePool() failed\n");
@@ -511,6 +739,7 @@ SeAssignSecurity(PSECURITY_DESCRIPTOR ParentDescriptor OPTIONAL,
       return STATUS_INSUFFICIENT_RESOURCES;
     }
 
+  RtlZeroMemory( Descriptor, Length );
   RtlCreateSecurityDescriptor(Descriptor,
                              SECURITY_DESCRIPTOR_REVISION);
 
@@ -569,10 +798,13 @@ SeAssignSecurity(PSECURITY_DESCRIPTOR ParentDescriptor OPTIONAL,
 
 
 static BOOLEAN
-SepSidInToken(PACCESS_TOKEN Token,
+SepSidInToken(PACCESS_TOKEN _Token,
              PSID Sid)
 {
   ULONG i;
+  PTOKEN Token = (PTOKEN)_Token;
+
+  PAGED_CODE();
 
   if (Token->UserAndGroupCount == 0)
   {
@@ -637,6 +869,8 @@ SeAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor,
   PSID Sid;
   NTSTATUS Status;
 
+  PAGED_CODE();
+
   CurrentAccess = PreviouslyGrantedAccess;
 
   if (SubjectContextLocked == FALSE)
@@ -805,9 +1039,11 @@ NtAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor,
 {
   SECURITY_SUBJECT_CONTEXT SubjectSecurityContext;
   KPROCESSOR_MODE PreviousMode;
-  PACCESS_TOKEN Token;
+  PTOKEN Token;
   NTSTATUS Status;
 
+  PAGED_CODE();
+
   DPRINT("NtAccessCheck() called\n");
 
   PreviousMode = KeGetPreviousMode();