Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
5.8 KiB

  1. /*++
  2. Copyright (c) 1999-2001 Microsoft Corporation
  3. Module Name:
  4. seutil.cxx
  5. Abstract:
  6. This module implements general security utilities.
  7. Author:
  8. Keith Moore (keithmo) 25-Mar-1999
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #ifdef ALLOC_PRAGMA
  13. #pragma alloc_text( PAGE, UlAssignSecurity )
  14. #pragma alloc_text( PAGE, UlDeassignSecurity )
  15. #pragma alloc_text( PAGE, UlAccessCheck )
  16. #endif // ALLOC_PRAGMA
  17. #if 0
  18. #endif
  19. //
  20. // Public functions.
  21. //
  22. /***************************************************************************++
  23. Routine Description:
  24. Assigns a new security descriptor.
  25. Arguments:
  26. pSecurityDescriptor - Supplies a pointer to the current security
  27. descriptor pointer. The current security descriptor pointer
  28. will be updated with the new security descriptor.
  29. pAccessState - Supplies the ACCESS_STATE structure containing
  30. the state of an access in progress.
  31. Return Value:
  32. NTSTATUS - Completion status.
  33. --***************************************************************************/
  34. NTSTATUS
  35. UlAssignSecurity(
  36. IN OUT PSECURITY_DESCRIPTOR *pSecurityDescriptor,
  37. IN PACCESS_STATE pAccessState
  38. )
  39. {
  40. NTSTATUS status;
  41. //
  42. // Sanity check.
  43. //
  44. PAGED_CODE();
  45. ASSERT( pSecurityDescriptor != NULL );
  46. ASSERT( pAccessState != NULL );
  47. //
  48. // Assign the security descriptor.
  49. //
  50. SeLockSubjectContext( &pAccessState->SubjectSecurityContext );
  51. status = SeAssignSecurity(
  52. NULL, // ParentDescriptor
  53. pAccessState->SecurityDescriptor,
  54. pSecurityDescriptor,
  55. FALSE, // IsDirectoryObject
  56. &pAccessState->SubjectSecurityContext,
  57. IoGetFileObjectGenericMapping(),
  58. PagedPool
  59. );
  60. SeUnlockSubjectContext( &pAccessState->SubjectSecurityContext );
  61. return status;
  62. } // UlAssignSecurity
  63. /***************************************************************************++
  64. Routine Description:
  65. Deletes a security descriptor.
  66. Arguments:
  67. pSecurityDescriptor - Supplies a pointer to the current security
  68. descriptor pointer. The current security descriptor pointer
  69. will be deleted.
  70. --***************************************************************************/
  71. VOID
  72. UlDeassignSecurity(
  73. IN OUT PSECURITY_DESCRIPTOR *pSecurityDescriptor
  74. )
  75. {
  76. //
  77. // Sanity check.
  78. //
  79. PAGED_CODE();
  80. ASSERT( pSecurityDescriptor != NULL );
  81. //
  82. // If there's a security descriptor present, free it.
  83. //
  84. if (*pSecurityDescriptor != NULL)
  85. {
  86. SeDeassignSecurity( pSecurityDescriptor );
  87. }
  88. } // UlDeassignSecurity
  89. /***************************************************************************++
  90. Routine Description:
  91. Determines if a user has access to the specified resource.
  92. Arguments:
  93. pSecurityDescriptor - Supplies the security descriptor protecting
  94. the resource.
  95. pAccessState - Supplies the ACCESS_STATE structure containing
  96. the state of an access in progress.
  97. DesiredAccess - Supplies an access mask describing the user's
  98. desired access to the resource. This mask is assumed to not
  99. contain generic access types.
  100. RequestorMode - Supplies the processor mode by which the access is
  101. being requested.
  102. pObjectName - Supplies the name of the object being referenced.
  103. Return Value:
  104. NTSTATUS - Completion status.
  105. --***************************************************************************/
  106. NTSTATUS
  107. UlAccessCheck(
  108. IN PSECURITY_DESCRIPTOR pSecurityDescriptor,
  109. IN PACCESS_STATE pAccessState,
  110. IN ACCESS_MASK DesiredAccess,
  111. IN KPROCESSOR_MODE RequestorMode,
  112. IN PWSTR pObjectName
  113. )
  114. {
  115. NTSTATUS status;
  116. BOOLEAN accessGranted;
  117. PPRIVILEGE_SET pPrivileges = NULL;
  118. ACCESS_MASK grantedAccess;
  119. UNICODE_STRING objectName;
  120. UNICODE_STRING typeName;
  121. //
  122. // Sanity check.
  123. //
  124. PAGED_CODE();
  125. ASSERT( pSecurityDescriptor != NULL );
  126. ASSERT( pAccessState != NULL );
  127. //
  128. // Perform the access check.
  129. //
  130. SeLockSubjectContext( &pAccessState->SubjectSecurityContext );
  131. accessGranted = SeAccessCheck(
  132. pSecurityDescriptor,
  133. &pAccessState->SubjectSecurityContext,
  134. TRUE, // SubjectContextLocked
  135. DesiredAccess,
  136. 0, // PreviouslyGrantedAccess
  137. &pPrivileges,
  138. IoGetFileObjectGenericMapping(),
  139. RequestorMode,
  140. &grantedAccess,
  141. &status
  142. );
  143. if (pPrivileges != NULL)
  144. {
  145. SeAppendPrivileges( pAccessState, pPrivileges );
  146. SeFreePrivileges( pPrivileges );
  147. }
  148. if (accessGranted)
  149. {
  150. pAccessState->PreviouslyGrantedAccess |= grantedAccess;
  151. pAccessState->RemainingDesiredAccess &= ~(grantedAccess | MAXIMUM_ALLOWED);
  152. }
  153. RtlInitUnicodeString( &typeName, L"Ul" );
  154. RtlInitUnicodeString( &objectName, pObjectName );
  155. SeOpenObjectAuditAlarm(
  156. &typeName,
  157. NULL, // Object
  158. &objectName,
  159. pSecurityDescriptor,
  160. pAccessState,
  161. FALSE, // ObjectCreated
  162. accessGranted,
  163. RequestorMode,
  164. &pAccessState->GenerateOnClose
  165. );
  166. SeUnlockSubjectContext( &pAccessState->SubjectSecurityContext );
  167. if (accessGranted)
  168. {
  169. status = STATUS_SUCCESS;
  170. }
  171. else
  172. {
  173. //
  174. // SeAccessCheck() should have set the completion status.
  175. //
  176. ASSERT( !NT_SUCCESS(status) );
  177. }
  178. return status;
  179. } // UlAccessCheck
  180. //
  181. // Private functions.
  182. //