Leaked source code of windows server 2003
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.

345 lines
9.5 KiB

  1. /*++
  2. Copyright (c) 1989-1993 Microsoft Corporation
  3. Module Name:
  4. arcsec.c
  5. Abstract:
  6. This module contains subroutines for protecting the system
  7. partition on an ARC system.
  8. Author:
  9. Jim Kelly (JimK) 13-Jan-1993
  10. Environment:
  11. Kernel mode - system initialization
  12. Revision History:
  13. --*/
  14. #include "iomgr.h"
  15. //
  16. // Define procedures local to this module.
  17. //
  18. NTSTATUS
  19. IopApplySystemPartitionProt(
  20. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  21. );
  22. #ifdef ALLOC_PRAGMA
  23. #pragma alloc_text(INIT,IopProtectSystemPartition)
  24. #pragma alloc_text(INIT,IopApplySystemPartitionProt)
  25. #endif
  26. //
  27. // This name must match the name use by the DISK MANAGER utility.
  28. // The Disk Manager creates and sets the value of this registry
  29. // key. We only look at it.
  30. //
  31. #define IOP_SYSTEM_PART_PROT_KEY L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Lsa"
  32. #define IOP_SYSTEM_PART_PROT_VALUE L"Protect System Partition"
  33. BOOLEAN
  34. IopProtectSystemPartition(
  35. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  36. )
  37. /*++
  38. Routine Description:
  39. This routine assigns protection to the system partition of an
  40. ARC system, if necessary. If this is not an ARC system, or
  41. the system partition does not need to be protected, then this
  42. routine does nothing.
  43. Arguments:
  44. LoaderBlock - Supplies a pointer to the loader parameter block that was
  45. created by the OS Loader.
  46. Return Value:
  47. The function value is a BOOLEAN indicating whether or not protection
  48. has been appropriately applied. TRUE indicates no errors were
  49. encountered. FALSE indicates an error was encountered.
  50. --*/
  51. {
  52. //
  53. // We only entertain the possibility of assigning protection
  54. // to the system partition if we are an ARC system. For the
  55. // time being, the best way to determine if you are an ARC
  56. // system is to see if you aren't and X86 machine. DavidRo
  57. // believes that at some point in the future we will have
  58. // ARC compliant X86 machines. At that point in time, we
  59. // will need to change the following #ifdef's into something
  60. // that does a run-time determination.
  61. //
  62. #ifdef i386 // if (!ARC-Compliant system)
  63. UNREFERENCED_PARAMETER (LoaderBlock);
  64. //
  65. // Nothing to do for non-ARC systems
  66. //
  67. return(TRUE);
  68. #else // ARC-COMPLIANT system
  69. NTSTATUS status;
  70. NTSTATUS tmpStatus;
  71. HANDLE keyHandle;
  72. OBJECT_ATTRIBUTES objectAttributes;
  73. UNICODE_STRING keyName;
  74. UNICODE_STRING valueName;
  75. ULONG resultLength;
  76. ULONG keyBuffer[sizeof( KEY_VALUE_PARTIAL_INFORMATION ) + sizeof( ULONG )];
  77. PKEY_VALUE_PARTIAL_INFORMATION keyValue;
  78. //
  79. // This is an ARC system. Attempt to retrieve information from the registry
  80. // indicating whether or not we should protect the system partition.
  81. //
  82. RtlInitUnicodeString( &keyName, IOP_SYSTEM_PART_PROT_KEY );
  83. InitializeObjectAttributes( &objectAttributes,
  84. &keyName,
  85. OBJ_CASE_INSENSITIVE,
  86. NULL,
  87. NULL );
  88. status = NtOpenKey( &keyHandle, KEY_READ, &objectAttributes);
  89. if (NT_SUCCESS( status )) {
  90. keyValue = (PKEY_VALUE_PARTIAL_INFORMATION) &keyBuffer[0];
  91. RtlInitUnicodeString( &valueName, IOP_SYSTEM_PART_PROT_VALUE );
  92. status = NtQueryValueKey( keyHandle,
  93. &valueName,
  94. KeyValuePartialInformation,
  95. keyValue,
  96. sizeof( KEY_VALUE_PARTIAL_INFORMATION ) + sizeof( ULONG ),
  97. &resultLength );
  98. if (NT_SUCCESS( status )) {
  99. PBOOLEAN applyIt;
  100. //
  101. // The appropriate information was located in the registry. Now
  102. // determine whether or not is indicates that protection is to be
  103. // applied.
  104. //
  105. applyIt = &(keyValue->Data[0]);
  106. if (*applyIt) {
  107. status = IopApplySystemPartitionProt( LoaderBlock );
  108. }
  109. }
  110. tmpStatus = NtClose( keyHandle );
  111. ASSERT(NT_SUCCESS( tmpStatus ));
  112. }
  113. return TRUE;
  114. #endif // ARC-COMPLIANT system
  115. }
  116. NTSTATUS
  117. IopApplySystemPartitionProt(
  118. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  119. )
  120. /*++
  121. Routine Description:
  122. This routine applies protection to the system partition that
  123. prevents all users except administrators from accessing the
  124. partition.
  125. This routine is only used during system initialization.
  126. As such, all memory allocations are expected to succeed.
  127. Success is tested only with assertions.
  128. Arguments:
  129. LoaderBlock - Supplies a pointer to the loader parameter block that was
  130. created by the OS Loader.
  131. Return Value:
  132. The function value is the final status from attempting to set the system
  133. partition protection.
  134. --*/
  135. {
  136. NTSTATUS status;
  137. PACL dacl;
  138. SECURITY_DESCRIPTOR securityDescriptor;
  139. OBJECT_ATTRIBUTES objectAttributes;
  140. ULONG length;
  141. CHAR ArcNameFmt[12];
  142. ArcNameFmt[0] = '\\';
  143. ArcNameFmt[1] = 'A';
  144. ArcNameFmt[2] = 'r';
  145. ArcNameFmt[3] = 'c';
  146. ArcNameFmt[4] = 'N';
  147. ArcNameFmt[5] = 'a';
  148. ArcNameFmt[6] = 'm';
  149. ArcNameFmt[7] = 'e';
  150. ArcNameFmt[8] = '\\';
  151. ArcNameFmt[9] = '%';
  152. ArcNameFmt[10] = 's';
  153. ArcNameFmt[11] = '\0';
  154. ASSERT( ARGUMENT_PRESENT( LoaderBlock ) );
  155. ASSERT( ARGUMENT_PRESENT( LoaderBlock->ArcHalDeviceName ) );
  156. //
  157. // Build an appropriate discretionary ACL.
  158. //
  159. length = (ULONG) sizeof( ACL ) +
  160. ( 2 * ((ULONG) sizeof( ACCESS_ALLOWED_ACE ))) +
  161. SeLengthSid( SeLocalSystemSid ) +
  162. SeLengthSid( SeAliasAdminsSid ) +
  163. 8; // The 8 is just for good measure
  164. dacl = (PACL) ExAllocatePool( PagedPool, length );
  165. if (!dacl) {
  166. return STATUS_INSUFFICIENT_RESOURCES;
  167. }
  168. status = RtlCreateAcl( dacl, length, ACL_REVISION2 );
  169. if (NT_SUCCESS( status )) {
  170. status = RtlAddAccessAllowedAce( dacl,
  171. ACL_REVISION2,
  172. GENERIC_ALL,
  173. SeLocalSystemSid );
  174. if (NT_SUCCESS( status )) {
  175. status = RtlAddAccessAllowedAce( dacl,
  176. ACL_REVISION2,
  177. GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | READ_CONTROL,
  178. SeAliasAdminsSid );
  179. if (NT_SUCCESS( status )) {
  180. //
  181. // Put it in a security descriptor so that it may be applied to
  182. // the system partition device.
  183. //
  184. status = RtlCreateSecurityDescriptor( &securityDescriptor,
  185. SECURITY_DESCRIPTOR_REVISION );
  186. if (NT_SUCCESS( status )) {
  187. status = RtlSetDaclSecurityDescriptor( &securityDescriptor,
  188. TRUE,
  189. dacl,
  190. FALSE );
  191. }
  192. }
  193. }
  194. }
  195. if (!NT_SUCCESS( status )) {
  196. ExFreePool( dacl );
  197. return status;
  198. }
  199. //
  200. // Open the ARC boot device and apply the ACL.
  201. //
  202. {
  203. NTSTATUS tmpStatus;
  204. CHAR deviceNameBuffer[256];
  205. STRING deviceNameString;
  206. UNICODE_STRING deviceNameUnicodeString;
  207. HANDLE deviceHandle;
  208. IO_STATUS_BLOCK ioStatusBlock;
  209. //
  210. // Begin by formulating the ARC name of the boot device in the ARC
  211. // name space.
  212. //
  213. sprintf( deviceNameBuffer,
  214. ArcNameFmt,
  215. LoaderBlock->ArcHalDeviceName );
  216. RtlInitAnsiString( &deviceNameString, deviceNameBuffer );
  217. status = RtlAnsiStringToUnicodeString( &deviceNameUnicodeString,
  218. &deviceNameString,
  219. TRUE );
  220. if (NT_SUCCESS( status )) {
  221. InitializeObjectAttributes( &objectAttributes,
  222. &deviceNameUnicodeString,
  223. OBJ_CASE_INSENSITIVE,
  224. NULL,
  225. NULL );
  226. status = ZwOpenFile( &deviceHandle,
  227. WRITE_DAC,
  228. &objectAttributes,
  229. &ioStatusBlock,
  230. TRUE,
  231. 0 );
  232. RtlFreeUnicodeString( &deviceNameUnicodeString );
  233. if (NT_SUCCESS( status )) {
  234. //
  235. // Apply the ACL built above to the system partition device
  236. // object.
  237. //
  238. status = ZwSetSecurityObject( deviceHandle,
  239. DACL_SECURITY_INFORMATION,
  240. &securityDescriptor );
  241. tmpStatus = NtClose( deviceHandle );
  242. }
  243. }
  244. }
  245. //
  246. // Free the memory used to hold the ACL.
  247. //
  248. ExFreePool( dacl );
  249. return status;
  250. }