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.

1468 lines
47 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. seglobal.c
  5. Abstract:
  6. This module contains the global variables used and exported by the security
  7. component.
  8. Author:
  9. Jim Kelly (JimK) 5-Aug-1990
  10. Environment:
  11. Kernel mode only.
  12. Revision History:
  13. --*/
  14. #include "pch.h"
  15. #pragma hdrstop
  16. #include "adt.h"
  17. VOID
  18. SepInitializePrivilegeSets( VOID );
  19. VOID
  20. SepInitSystemDacls( VOID );
  21. VOID
  22. SepInitProcessAuditSd( VOID );
  23. #ifdef ALLOC_PRAGMA
  24. #pragma alloc_text(INIT,SepVariableInitialization)
  25. #pragma alloc_text(INIT,SepInitSystemDacls)
  26. #pragma alloc_text(INIT,SepInitializePrivilegeSets)
  27. #pragma alloc_text(PAGE,SepAssemblePrivileges)
  28. #pragma alloc_text(INIT,SepInitializeWorkList)
  29. #endif
  30. #ifdef ALLOC_DATA_PRAGMA
  31. #pragma data_seg("PAGEDATA")
  32. #pragma const_seg("PAGECONST")
  33. #endif
  34. #ifdef SE_DIAGNOSTICS_ENABLED
  35. //
  36. // Used to control the active SE diagnostic support provided
  37. //
  38. ULONG SeGlobalFlag = 0;
  39. #endif // SE_DIAGNOSTICS_ENABLED
  40. ////////////////////////////////////////////////////////////////////////
  41. // //
  42. // Global, READ ONLY, Security variables //
  43. // //
  44. ////////////////////////////////////////////////////////////////////////
  45. //
  46. // Authentication ID and source name used for system processes
  47. //
  48. const TOKEN_SOURCE SeSystemTokenSource = {"*SYSTEM*", 0};
  49. const LUID SeSystemAuthenticationId = SYSTEM_LUID;
  50. const LUID SeAnonymousAuthenticationId = ANONYMOUS_LOGON_LUID;
  51. //
  52. // Universal well known SIDs
  53. //
  54. PSID SeNullSid = NULL;
  55. PSID SeWorldSid = NULL;
  56. PSID SeLocalSid = NULL;
  57. PSID SeCreatorOwnerSid = NULL;
  58. PSID SeCreatorGroupSid = NULL;
  59. PSID SeCreatorGroupServerSid = NULL;
  60. PSID SeCreatorOwnerServerSid = NULL;
  61. //
  62. // Sids defined by NT
  63. //
  64. PSID SeNtAuthoritySid = NULL;
  65. PSID SeDialupSid = NULL;
  66. PSID SeNetworkSid = NULL;
  67. PSID SeBatchSid = NULL;
  68. PSID SeInteractiveSid = NULL;
  69. PSID SeServiceSid = NULL;
  70. PSID SePrincipalSelfSid = NULL;
  71. PSID SeLocalSystemSid = NULL;
  72. PSID SeAuthenticatedUsersSid = NULL;
  73. PSID SeAliasAdminsSid = NULL;
  74. PSID SeRestrictedSid = NULL;
  75. PSID SeAliasUsersSid = NULL;
  76. PSID SeAliasGuestsSid = NULL;
  77. PSID SeAliasPowerUsersSid = NULL;
  78. PSID SeAliasAccountOpsSid = NULL;
  79. PSID SeAliasSystemOpsSid = NULL;
  80. PSID SeAliasPrintOpsSid = NULL;
  81. PSID SeAliasBackupOpsSid = NULL;
  82. PSID SeAnonymousLogonSid = NULL;
  83. PSID SeLocalServiceSid = NULL;
  84. PSID SeNetworkServiceSid = NULL;
  85. //
  86. // Well known tokens
  87. //
  88. PACCESS_TOKEN SeAnonymousLogonToken = NULL;
  89. PACCESS_TOKEN SeAnonymousLogonTokenNoEveryone = NULL;
  90. //
  91. // System default DACLs & Security Descriptors
  92. //
  93. // SePublicDefaultDacl - Protects objects so that WORLD:E, Admins:ALL, System: ALL.
  94. // Not inherited by sub-objects.
  95. //
  96. // SePublicDefaultUnrestrictedDacl - Protects objects so that WORLD:E, Admins:ALL, System: ALL, Restricted:E
  97. // Not inherited by sub-objects.
  98. //
  99. // SePublicOpenDacl - Protects so that WORLD can RWE and Admins: All.
  100. // Not inherited by sub-objects.
  101. //
  102. // SePublicOpenUnrestrictedDacl - Protects so that WORLD can RWE and Admins: All, Restricted:RE
  103. // Not inherited by sub-objects.
  104. //
  105. // SeSystemDefaultDacl - Protects objects so that SYSTEM & ADMIN can use them.
  106. // Not inherited by subobjects.
  107. //
  108. // SeUnrestrictedDacl - Protects objects so that everyone AND unrestricted have full control
  109. // Not inherited by subobjects.
  110. //
  111. PSECURITY_DESCRIPTOR SePublicDefaultSd = NULL;
  112. SECURITY_DESCRIPTOR SepPublicDefaultSd = {0};
  113. PSECURITY_DESCRIPTOR SePublicDefaultUnrestrictedSd = NULL;
  114. SECURITY_DESCRIPTOR SepPublicDefaultUnrestrictedSd = {0};
  115. PSECURITY_DESCRIPTOR SePublicOpenUnrestrictedSd = NULL;
  116. SECURITY_DESCRIPTOR SepPublicOpenUnrestrictedSd = {0};
  117. PSECURITY_DESCRIPTOR SePublicOpenSd = NULL;
  118. SECURITY_DESCRIPTOR SepPublicOpenSd = {0};
  119. PSECURITY_DESCRIPTOR SeSystemDefaultSd = NULL;
  120. SECURITY_DESCRIPTOR SepSystemDefaultSd = {0};
  121. PSECURITY_DESCRIPTOR SeUnrestrictedSd = NULL;
  122. SECURITY_DESCRIPTOR SepUnrestrictedSd = {0};
  123. //
  124. // security descriptor with a SACL to be used for adding
  125. // a SACL on system processes
  126. //
  127. PSECURITY_DESCRIPTOR SepProcessAuditSd = NULL;
  128. //
  129. // Access mask used for constructing the SACL in SepProcessAuditSd
  130. //
  131. ACCESS_MASK SepProcessAccessesToAudit = 0;
  132. //
  133. // security descriptor to check if a given token has any one of
  134. // following sids in it:
  135. // -- SeLocalSystemSid
  136. // -- SeLocalServiceSid
  137. // -- SeNetworkServiceSid
  138. //
  139. PSECURITY_DESCRIPTOR SepImportantProcessSd = NULL;
  140. //
  141. // used with SepImportantProcessSd
  142. //
  143. GENERIC_MAPPING GenericMappingForMembershipCheck = {
  144. STANDARD_RIGHTS_READ,
  145. STANDARD_RIGHTS_EXECUTE,
  146. STANDARD_RIGHTS_WRITE,
  147. STANDARD_RIGHTS_ALL };
  148. PACL SePublicDefaultDacl = NULL;
  149. PACL SePublicDefaultUnrestrictedDacl = NULL;
  150. PACL SePublicOpenDacl = NULL;
  151. PACL SePublicOpenUnrestrictedDacl = NULL;
  152. PACL SeSystemDefaultDacl = NULL;
  153. PACL SeUnrestrictedDacl = NULL;
  154. //
  155. // Sid of primary domain, and admin account in that domain
  156. //
  157. PSID SepPrimaryDomainSid = NULL;
  158. PSID SepPrimaryDomainAdminSid = NULL;
  159. //
  160. // Well known privilege values
  161. //
  162. LUID SeCreateTokenPrivilege = {0};
  163. LUID SeAssignPrimaryTokenPrivilege = {0};
  164. LUID SeLockMemoryPrivilege = {0};
  165. LUID SeIncreaseQuotaPrivilege = {0};
  166. LUID SeUnsolicitedInputPrivilege = {0};
  167. LUID SeTcbPrivilege = {0};
  168. LUID SeSecurityPrivilege = {0};
  169. LUID SeTakeOwnershipPrivilege = {0};
  170. LUID SeLoadDriverPrivilege = {0};
  171. LUID SeCreatePagefilePrivilege = {0};
  172. LUID SeIncreaseBasePriorityPrivilege = {0};
  173. LUID SeSystemProfilePrivilege = {0};
  174. LUID SeSystemtimePrivilege = {0};
  175. LUID SeProfileSingleProcessPrivilege = {0};
  176. LUID SeCreatePermanentPrivilege = {0};
  177. LUID SeBackupPrivilege = {0};
  178. LUID SeRestorePrivilege = {0};
  179. LUID SeShutdownPrivilege = {0};
  180. LUID SeDebugPrivilege = {0};
  181. LUID SeAuditPrivilege = {0};
  182. LUID SeSystemEnvironmentPrivilege = {0};
  183. LUID SeChangeNotifyPrivilege = {0};
  184. LUID SeRemoteShutdownPrivilege = {0};
  185. LUID SeUndockPrivilege = {0};
  186. LUID SeSyncAgentPrivilege = {0};
  187. LUID SeEnableDelegationPrivilege = {0};
  188. LUID SeManageVolumePrivilege = { 0 };
  189. //
  190. // Define the following structures for export from the kernel.
  191. // This will allow us to export pointers to these structures
  192. // rather than a pointer for each element in the structure.
  193. //
  194. PSE_EXPORTS SeExports = NULL;
  195. SE_EXPORTS SepExports = {0};
  196. static const SID_IDENTIFIER_AUTHORITY SepNullSidAuthority = SECURITY_NULL_SID_AUTHORITY;
  197. static const SID_IDENTIFIER_AUTHORITY SepWorldSidAuthority = SECURITY_WORLD_SID_AUTHORITY;
  198. static const SID_IDENTIFIER_AUTHORITY SepLocalSidAuthority = SECURITY_LOCAL_SID_AUTHORITY;
  199. static const SID_IDENTIFIER_AUTHORITY SepCreatorSidAuthority = SECURITY_CREATOR_SID_AUTHORITY;
  200. static const SID_IDENTIFIER_AUTHORITY SepNtAuthority = SECURITY_NT_AUTHORITY;
  201. //
  202. // Some variables we are going to use to help speed up access
  203. // checking.
  204. //
  205. static ULONG SinglePrivilegeSetSize = 0;
  206. static ULONG DoublePrivilegeSetSize = 0;
  207. static PPRIVILEGE_SET SepSystemSecurityPrivilegeSet = NULL;
  208. static PPRIVILEGE_SET SepTakeOwnershipPrivilegeSet = NULL;
  209. static PPRIVILEGE_SET SepDoublePrivilegeSet = NULL;
  210. //
  211. // Array containing information describing what is to be audited
  212. //
  213. SE_AUDITING_STATE SeAuditingState[POLICY_AUDIT_EVENT_TYPE_COUNT] =
  214. {
  215. { FALSE, FALSE },
  216. { FALSE, FALSE },
  217. { FALSE, FALSE },
  218. { FALSE, FALSE },
  219. { FALSE, FALSE },
  220. { FALSE, FALSE },
  221. { FALSE, FALSE },
  222. { FALSE, FALSE },
  223. { FALSE, FALSE }
  224. };
  225. //
  226. // Boolean indicating whether or not auditing is enabled for the system
  227. //
  228. BOOLEAN SepAdtAuditingEnabled = FALSE;
  229. //
  230. // Boolean to hold whether or not the user wants the system to crash when
  231. // an audit fails.
  232. //
  233. BOOLEAN SepCrashOnAuditFail = FALSE;
  234. //
  235. // Handle to the LSA process
  236. //
  237. HANDLE SepLsaHandle = NULL;
  238. //
  239. // Boolean indicating that we're auditing detailed events
  240. // such as process creation.
  241. //
  242. BOOLEAN SeDetailedAuditing = FALSE;
  243. #define SE_SUBSYSTEM_NAME L"Security"
  244. const UNICODE_STRING SeSubsystemName = {
  245. sizeof(SE_SUBSYSTEM_NAME) - sizeof(WCHAR),
  246. sizeof(SE_SUBSYSTEM_NAME),
  247. SE_SUBSYSTEM_NAME
  248. };
  249. //
  250. // Doubly linked list of work items queued to worker threads.
  251. //
  252. LIST_ENTRY SepLsaQueue = {NULL};
  253. //
  254. // Count to tell us how long the queue gets in SepRmCallLsa
  255. //
  256. ULONG SepLsaQueueLength = 0;
  257. #ifdef ALLOC_DATA_PRAGMA
  258. #pragma data_seg()
  259. #endif
  260. //
  261. // Mutex protecting the queue of work being passed to LSA
  262. //
  263. ERESOURCE SepLsaQueueLock = {0};
  264. SEP_WORK_ITEM SepExWorkItem = {0};
  265. ////////////////////////////////////////////////////////////////////////
  266. // //
  267. // Variable Initialization Routines //
  268. // //
  269. ////////////////////////////////////////////////////////////////////////
  270. BOOLEAN
  271. SepVariableInitialization()
  272. /*++
  273. Routine Description:
  274. This function initializes the global variables used by and exposed
  275. by security.
  276. Arguments:
  277. None.
  278. Return Value:
  279. TRUE if variables successfully initialized.
  280. FALSE if not successfully initialized.
  281. --*/
  282. {
  283. ULONG SidWithZeroSubAuthorities;
  284. ULONG SidWithOneSubAuthority;
  285. ULONG SidWithTwoSubAuthorities;
  286. ULONG SidWithThreeSubAuthorities;
  287. SID_IDENTIFIER_AUTHORITY NullSidAuthority;
  288. SID_IDENTIFIER_AUTHORITY WorldSidAuthority;
  289. SID_IDENTIFIER_AUTHORITY LocalSidAuthority;
  290. SID_IDENTIFIER_AUTHORITY CreatorSidAuthority;
  291. SID_IDENTIFIER_AUTHORITY SeNtAuthority;
  292. PAGED_CODE();
  293. NullSidAuthority = SepNullSidAuthority;
  294. WorldSidAuthority = SepWorldSidAuthority;
  295. LocalSidAuthority = SepLocalSidAuthority;
  296. CreatorSidAuthority = SepCreatorSidAuthority;
  297. SeNtAuthority = SepNtAuthority;
  298. //
  299. // The following SID sizes need to be allocated
  300. //
  301. SidWithZeroSubAuthorities = RtlLengthRequiredSid( 0 );
  302. SidWithOneSubAuthority = RtlLengthRequiredSid( 1 );
  303. SidWithTwoSubAuthorities = RtlLengthRequiredSid( 2 );
  304. SidWithThreeSubAuthorities = RtlLengthRequiredSid( 3 );
  305. //
  306. // Allocate and initialize the universal SIDs
  307. //
  308. SeNullSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  309. SeWorldSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  310. SeLocalSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  311. SeCreatorOwnerSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  312. SeCreatorGroupSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  313. SeCreatorOwnerServerSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  314. SeCreatorGroupServerSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  315. //
  316. // Fail initialization if we didn't get enough memory for the universal
  317. // SIDs.
  318. //
  319. if ( (SeNullSid == NULL) ||
  320. (SeWorldSid == NULL) ||
  321. (SeLocalSid == NULL) ||
  322. (SeCreatorOwnerSid == NULL) ||
  323. (SeCreatorGroupSid == NULL) ||
  324. (SeCreatorOwnerServerSid == NULL ) ||
  325. (SeCreatorGroupServerSid == NULL )
  326. ) {
  327. return( FALSE );
  328. }
  329. RtlInitializeSid( SeNullSid, &NullSidAuthority, 1 );
  330. RtlInitializeSid( SeWorldSid, &WorldSidAuthority, 1 );
  331. RtlInitializeSid( SeLocalSid, &LocalSidAuthority, 1 );
  332. RtlInitializeSid( SeCreatorOwnerSid, &CreatorSidAuthority, 1 );
  333. RtlInitializeSid( SeCreatorGroupSid, &CreatorSidAuthority, 1 );
  334. RtlInitializeSid( SeCreatorOwnerServerSid, &CreatorSidAuthority, 1 );
  335. RtlInitializeSid( SeCreatorGroupServerSid, &CreatorSidAuthority, 1 );
  336. *(RtlSubAuthoritySid( SeNullSid, 0 )) = SECURITY_NULL_RID;
  337. *(RtlSubAuthoritySid( SeWorldSid, 0 )) = SECURITY_WORLD_RID;
  338. *(RtlSubAuthoritySid( SeLocalSid, 0 )) = SECURITY_LOCAL_RID;
  339. *(RtlSubAuthoritySid( SeCreatorOwnerSid, 0 )) = SECURITY_CREATOR_OWNER_RID;
  340. *(RtlSubAuthoritySid( SeCreatorGroupSid, 0 )) = SECURITY_CREATOR_GROUP_RID;
  341. *(RtlSubAuthoritySid( SeCreatorOwnerServerSid, 0 )) = SECURITY_CREATOR_OWNER_SERVER_RID;
  342. *(RtlSubAuthoritySid( SeCreatorGroupServerSid, 0 )) = SECURITY_CREATOR_GROUP_SERVER_RID;
  343. //
  344. // Allocate and initialize the NT defined SIDs
  345. //
  346. SeNtAuthoritySid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithZeroSubAuthorities,'iSeS');
  347. SeDialupSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  348. SeNetworkSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  349. SeBatchSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  350. SeInteractiveSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  351. SeServiceSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  352. SePrincipalSelfSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  353. SeLocalSystemSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  354. SeAuthenticatedUsersSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  355. SeRestrictedSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  356. SeAnonymousLogonSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  357. SeLocalServiceSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  358. SeNetworkServiceSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithOneSubAuthority,'iSeS');
  359. SeAliasAdminsSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithTwoSubAuthorities,'iSeS');
  360. SeAliasUsersSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithTwoSubAuthorities,'iSeS');
  361. SeAliasGuestsSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithTwoSubAuthorities,'iSeS');
  362. SeAliasPowerUsersSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithTwoSubAuthorities,'iSeS');
  363. SeAliasAccountOpsSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithTwoSubAuthorities,'iSeS');
  364. SeAliasSystemOpsSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithTwoSubAuthorities,'iSeS');
  365. SeAliasPrintOpsSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithTwoSubAuthorities,'iSeS');
  366. SeAliasBackupOpsSid = (PSID)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,SidWithTwoSubAuthorities,'iSeS');
  367. //
  368. // Fail initialization if we didn't get enough memory for the NT SIDs.
  369. //
  370. if ( (SeNtAuthoritySid == NULL) ||
  371. (SeDialupSid == NULL) ||
  372. (SeNetworkSid == NULL) ||
  373. (SeBatchSid == NULL) ||
  374. (SeInteractiveSid == NULL) ||
  375. (SeServiceSid == NULL) ||
  376. (SePrincipalSelfSid == NULL) ||
  377. (SeLocalSystemSid == NULL) ||
  378. (SeAuthenticatedUsersSid == NULL) ||
  379. (SeRestrictedSid == NULL) ||
  380. (SeAnonymousLogonSid == NULL) ||
  381. (SeLocalServiceSid == NULL) ||
  382. (SeNetworkServiceSid == NULL) ||
  383. (SeAliasAdminsSid == NULL) ||
  384. (SeAliasUsersSid == NULL) ||
  385. (SeAliasGuestsSid == NULL) ||
  386. (SeAliasPowerUsersSid == NULL) ||
  387. (SeAliasAccountOpsSid == NULL) ||
  388. (SeAliasSystemOpsSid == NULL) ||
  389. (SeAliasPrintOpsSid == NULL) ||
  390. (SeAliasBackupOpsSid == NULL)
  391. ) {
  392. return( FALSE );
  393. }
  394. RtlInitializeSid( SeNtAuthoritySid, &SeNtAuthority, 0 );
  395. RtlInitializeSid( SeDialupSid, &SeNtAuthority, 1 );
  396. RtlInitializeSid( SeNetworkSid, &SeNtAuthority, 1 );
  397. RtlInitializeSid( SeBatchSid, &SeNtAuthority, 1 );
  398. RtlInitializeSid( SeInteractiveSid, &SeNtAuthority, 1 );
  399. RtlInitializeSid( SeServiceSid, &SeNtAuthority, 1 );
  400. RtlInitializeSid( SePrincipalSelfSid, &SeNtAuthority, 1 );
  401. RtlInitializeSid( SeLocalSystemSid, &SeNtAuthority, 1 );
  402. RtlInitializeSid( SeAuthenticatedUsersSid, &SeNtAuthority, 1 );
  403. RtlInitializeSid( SeRestrictedSid, &SeNtAuthority, 1 );
  404. RtlInitializeSid( SeAnonymousLogonSid, &SeNtAuthority, 1 );
  405. RtlInitializeSid( SeLocalServiceSid, &SeNtAuthority, 1 );
  406. RtlInitializeSid( SeNetworkServiceSid, &SeNtAuthority, 1 );
  407. RtlInitializeSid( SeAliasAdminsSid, &SeNtAuthority, 2);
  408. RtlInitializeSid( SeAliasUsersSid, &SeNtAuthority, 2);
  409. RtlInitializeSid( SeAliasGuestsSid, &SeNtAuthority, 2);
  410. RtlInitializeSid( SeAliasPowerUsersSid, &SeNtAuthority, 2);
  411. RtlInitializeSid( SeAliasAccountOpsSid, &SeNtAuthority, 2);
  412. RtlInitializeSid( SeAliasSystemOpsSid, &SeNtAuthority, 2);
  413. RtlInitializeSid( SeAliasPrintOpsSid, &SeNtAuthority, 2);
  414. RtlInitializeSid( SeAliasBackupOpsSid, &SeNtAuthority, 2);
  415. *(RtlSubAuthoritySid( SeDialupSid, 0 )) = SECURITY_DIALUP_RID;
  416. *(RtlSubAuthoritySid( SeNetworkSid, 0 )) = SECURITY_NETWORK_RID;
  417. *(RtlSubAuthoritySid( SeBatchSid, 0 )) = SECURITY_BATCH_RID;
  418. *(RtlSubAuthoritySid( SeInteractiveSid, 0 )) = SECURITY_INTERACTIVE_RID;
  419. *(RtlSubAuthoritySid( SeServiceSid, 0 )) = SECURITY_SERVICE_RID;
  420. *(RtlSubAuthoritySid( SePrincipalSelfSid, 0 )) = SECURITY_PRINCIPAL_SELF_RID;
  421. *(RtlSubAuthoritySid( SeLocalSystemSid, 0 )) = SECURITY_LOCAL_SYSTEM_RID;
  422. *(RtlSubAuthoritySid( SeAuthenticatedUsersSid, 0 )) = SECURITY_AUTHENTICATED_USER_RID;
  423. *(RtlSubAuthoritySid( SeRestrictedSid, 0 )) = SECURITY_RESTRICTED_CODE_RID;
  424. *(RtlSubAuthoritySid( SeAnonymousLogonSid, 0 )) = SECURITY_ANONYMOUS_LOGON_RID;
  425. *(RtlSubAuthoritySid( SeLocalServiceSid, 0 )) = SECURITY_LOCAL_SERVICE_RID;
  426. *(RtlSubAuthoritySid( SeNetworkServiceSid, 0 )) = SECURITY_NETWORK_SERVICE_RID;
  427. *(RtlSubAuthoritySid( SeAliasAdminsSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  428. *(RtlSubAuthoritySid( SeAliasUsersSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  429. *(RtlSubAuthoritySid( SeAliasGuestsSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  430. *(RtlSubAuthoritySid( SeAliasPowerUsersSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  431. *(RtlSubAuthoritySid( SeAliasAccountOpsSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  432. *(RtlSubAuthoritySid( SeAliasSystemOpsSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  433. *(RtlSubAuthoritySid( SeAliasPrintOpsSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  434. *(RtlSubAuthoritySid( SeAliasBackupOpsSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  435. *(RtlSubAuthoritySid( SeAliasAdminsSid, 1 )) = DOMAIN_ALIAS_RID_ADMINS;
  436. *(RtlSubAuthoritySid( SeAliasUsersSid, 1 )) = DOMAIN_ALIAS_RID_USERS;
  437. *(RtlSubAuthoritySid( SeAliasGuestsSid, 1 )) = DOMAIN_ALIAS_RID_GUESTS;
  438. *(RtlSubAuthoritySid( SeAliasPowerUsersSid, 1 )) = DOMAIN_ALIAS_RID_POWER_USERS;
  439. *(RtlSubAuthoritySid( SeAliasAccountOpsSid, 1 )) = DOMAIN_ALIAS_RID_ACCOUNT_OPS;
  440. *(RtlSubAuthoritySid( SeAliasSystemOpsSid, 1 )) = DOMAIN_ALIAS_RID_SYSTEM_OPS;
  441. *(RtlSubAuthoritySid( SeAliasPrintOpsSid, 1 )) = DOMAIN_ALIAS_RID_PRINT_OPS;
  442. *(RtlSubAuthoritySid( SeAliasBackupOpsSid, 1 )) = DOMAIN_ALIAS_RID_BACKUP_OPS;
  443. //
  444. // Initialize system default dacl
  445. //
  446. SepInitSystemDacls();
  447. //
  448. // Initialize the well known privilege values
  449. //
  450. SeCreateTokenPrivilege =
  451. RtlConvertLongToLuid(SE_CREATE_TOKEN_PRIVILEGE);
  452. SeAssignPrimaryTokenPrivilege =
  453. RtlConvertLongToLuid(SE_ASSIGNPRIMARYTOKEN_PRIVILEGE);
  454. SeLockMemoryPrivilege =
  455. RtlConvertLongToLuid(SE_LOCK_MEMORY_PRIVILEGE);
  456. SeIncreaseQuotaPrivilege =
  457. RtlConvertLongToLuid(SE_INCREASE_QUOTA_PRIVILEGE);
  458. SeUnsolicitedInputPrivilege =
  459. RtlConvertLongToLuid(SE_UNSOLICITED_INPUT_PRIVILEGE);
  460. SeTcbPrivilege =
  461. RtlConvertLongToLuid(SE_TCB_PRIVILEGE);
  462. SeSecurityPrivilege =
  463. RtlConvertLongToLuid(SE_SECURITY_PRIVILEGE);
  464. SeTakeOwnershipPrivilege =
  465. RtlConvertLongToLuid(SE_TAKE_OWNERSHIP_PRIVILEGE);
  466. SeLoadDriverPrivilege =
  467. RtlConvertLongToLuid(SE_LOAD_DRIVER_PRIVILEGE);
  468. SeCreatePagefilePrivilege =
  469. RtlConvertLongToLuid(SE_CREATE_PAGEFILE_PRIVILEGE);
  470. SeIncreaseBasePriorityPrivilege =
  471. RtlConvertLongToLuid(SE_INC_BASE_PRIORITY_PRIVILEGE);
  472. SeSystemProfilePrivilege =
  473. RtlConvertLongToLuid(SE_SYSTEM_PROFILE_PRIVILEGE);
  474. SeSystemtimePrivilege =
  475. RtlConvertLongToLuid(SE_SYSTEMTIME_PRIVILEGE);
  476. SeProfileSingleProcessPrivilege =
  477. RtlConvertLongToLuid(SE_PROF_SINGLE_PROCESS_PRIVILEGE);
  478. SeCreatePermanentPrivilege =
  479. RtlConvertLongToLuid(SE_CREATE_PERMANENT_PRIVILEGE);
  480. SeBackupPrivilege =
  481. RtlConvertLongToLuid(SE_BACKUP_PRIVILEGE);
  482. SeRestorePrivilege =
  483. RtlConvertLongToLuid(SE_RESTORE_PRIVILEGE);
  484. SeShutdownPrivilege =
  485. RtlConvertLongToLuid(SE_SHUTDOWN_PRIVILEGE);
  486. SeDebugPrivilege =
  487. RtlConvertLongToLuid(SE_DEBUG_PRIVILEGE);
  488. SeAuditPrivilege =
  489. RtlConvertLongToLuid(SE_AUDIT_PRIVILEGE);
  490. SeSystemEnvironmentPrivilege =
  491. RtlConvertLongToLuid(SE_SYSTEM_ENVIRONMENT_PRIVILEGE);
  492. SeChangeNotifyPrivilege =
  493. RtlConvertLongToLuid(SE_CHANGE_NOTIFY_PRIVILEGE);
  494. SeRemoteShutdownPrivilege =
  495. RtlConvertLongToLuid(SE_REMOTE_SHUTDOWN_PRIVILEGE);
  496. SeUndockPrivilege =
  497. RtlConvertLongToLuid(SE_UNDOCK_PRIVILEGE);
  498. SeSyncAgentPrivilege =
  499. RtlConvertLongToLuid(SE_SYNC_AGENT_PRIVILEGE);
  500. SeEnableDelegationPrivilege =
  501. RtlConvertLongToLuid(SE_ENABLE_DELEGATION_PRIVILEGE);
  502. SeManageVolumePrivilege =
  503. RtlConvertLongToLuid(SE_MANAGE_VOLUME_PRIVILEGE);
  504. //
  505. // Initialize the SeExports structure for exporting all
  506. // of the information we've created out of the kernel.
  507. //
  508. //
  509. // Package these together for export
  510. //
  511. SepExports.SeNullSid = SeNullSid;
  512. SepExports.SeWorldSid = SeWorldSid;
  513. SepExports.SeLocalSid = SeLocalSid;
  514. SepExports.SeCreatorOwnerSid = SeCreatorOwnerSid;
  515. SepExports.SeCreatorGroupSid = SeCreatorGroupSid;
  516. SepExports.SeNtAuthoritySid = SeNtAuthoritySid;
  517. SepExports.SeDialupSid = SeDialupSid;
  518. SepExports.SeNetworkSid = SeNetworkSid;
  519. SepExports.SeBatchSid = SeBatchSid;
  520. SepExports.SeInteractiveSid = SeInteractiveSid;
  521. SepExports.SeLocalSystemSid = SeLocalSystemSid;
  522. SepExports.SeAuthenticatedUsersSid = SeAuthenticatedUsersSid;
  523. SepExports.SeRestrictedSid = SeRestrictedSid;
  524. SepExports.SeAnonymousLogonSid = SeAnonymousLogonSid;
  525. SepExports.SeLocalServiceSid = SeLocalServiceSid;
  526. SepExports.SeNetworkServiceSid = SeNetworkServiceSid;
  527. SepExports.SeAliasAdminsSid = SeAliasAdminsSid;
  528. SepExports.SeAliasUsersSid = SeAliasUsersSid;
  529. SepExports.SeAliasGuestsSid = SeAliasGuestsSid;
  530. SepExports.SeAliasPowerUsersSid = SeAliasPowerUsersSid;
  531. SepExports.SeAliasAccountOpsSid = SeAliasAccountOpsSid;
  532. SepExports.SeAliasSystemOpsSid = SeAliasSystemOpsSid;
  533. SepExports.SeAliasPrintOpsSid = SeAliasPrintOpsSid;
  534. SepExports.SeAliasBackupOpsSid = SeAliasBackupOpsSid;
  535. SepExports.SeCreateTokenPrivilege = SeCreateTokenPrivilege;
  536. SepExports.SeAssignPrimaryTokenPrivilege = SeAssignPrimaryTokenPrivilege;
  537. SepExports.SeLockMemoryPrivilege = SeLockMemoryPrivilege;
  538. SepExports.SeIncreaseQuotaPrivilege = SeIncreaseQuotaPrivilege;
  539. SepExports.SeUnsolicitedInputPrivilege = SeUnsolicitedInputPrivilege;
  540. SepExports.SeTcbPrivilege = SeTcbPrivilege;
  541. SepExports.SeSecurityPrivilege = SeSecurityPrivilege;
  542. SepExports.SeTakeOwnershipPrivilege = SeTakeOwnershipPrivilege;
  543. SepExports.SeLoadDriverPrivilege = SeLoadDriverPrivilege;
  544. SepExports.SeCreatePagefilePrivilege = SeCreatePagefilePrivilege;
  545. SepExports.SeIncreaseBasePriorityPrivilege = SeIncreaseBasePriorityPrivilege;
  546. SepExports.SeSystemProfilePrivilege = SeSystemProfilePrivilege;
  547. SepExports.SeSystemtimePrivilege = SeSystemtimePrivilege;
  548. SepExports.SeProfileSingleProcessPrivilege = SeProfileSingleProcessPrivilege;
  549. SepExports.SeCreatePermanentPrivilege = SeCreatePermanentPrivilege;
  550. SepExports.SeBackupPrivilege = SeBackupPrivilege;
  551. SepExports.SeRestorePrivilege = SeRestorePrivilege;
  552. SepExports.SeShutdownPrivilege = SeShutdownPrivilege;
  553. SepExports.SeDebugPrivilege = SeDebugPrivilege;
  554. SepExports.SeAuditPrivilege = SeAuditPrivilege;
  555. SepExports.SeSystemEnvironmentPrivilege = SeSystemEnvironmentPrivilege;
  556. SepExports.SeChangeNotifyPrivilege = SeChangeNotifyPrivilege;
  557. SepExports.SeRemoteShutdownPrivilege = SeRemoteShutdownPrivilege;
  558. SepExports.SeUndockPrivilege = SeUndockPrivilege;
  559. SepExports.SeSyncAgentPrivilege = SeSyncAgentPrivilege;
  560. SepExports.SeEnableDelegationPrivilege = SeEnableDelegationPrivilege;
  561. SepExports.SeManageVolumePrivilege = SeManageVolumePrivilege;
  562. SeExports = &SepExports;
  563. //
  564. // Initialize frequently used privilege sets to speed up access
  565. // validation.
  566. //
  567. SepInitializePrivilegeSets();
  568. return TRUE;
  569. }
  570. VOID
  571. SepInitProcessAuditSd( VOID )
  572. /*++
  573. Routine Description:
  574. This function initializes SepProcessAuditSd -- a security descriptor
  575. that is used by SepAddSaclToProcess to add SACL to the existing
  576. security descriptor on a system process.
  577. A system process is defined as the one whose token has at least
  578. one of the following sids.
  579. -- SeLocalSystemSid
  580. -- SeLocalServiceSid
  581. -- SeNetworkServiceSid
  582. Arguments:
  583. None.
  584. Return Value:
  585. None.
  586. --*/
  587. {
  588. #define PROCESS_ACCESSES_TO_AUDIT ( PROCESS_CREATE_THREAD |\
  589. PROCESS_SET_INFORMATION |\
  590. PROCESS_SET_PORT |\
  591. PROCESS_SUSPEND_RESUME )
  592. NTSTATUS Status = STATUS_SUCCESS;
  593. ULONG AclLength, TotalSdLength;
  594. PISECURITY_DESCRIPTOR Sd = NULL;
  595. PACL Acl = NULL;
  596. //
  597. // free earlier instance if present
  598. //
  599. if ( SepProcessAuditSd != NULL ) {
  600. ExFreePool( SepProcessAuditSd );
  601. SepProcessAuditSd = NULL;
  602. }
  603. //DbgPrint("SepInitProcessAuditSd: SepProcessAccessesToAudit = %x\n", SepProcessAccessesToAudit);
  604. //
  605. // Don't initialize SeProcessAuditSd if SepProcessAccessesToAudit is 0
  606. // This effectively disables process access auditing
  607. //
  608. if ( SepProcessAccessesToAudit == 0 ) {
  609. goto Cleanup;
  610. }
  611. AclLength = (ULONG)sizeof(ACL) +
  612. ((ULONG)sizeof(SYSTEM_AUDIT_ACE) - sizeof(ULONG)) +
  613. SeLengthSid( SeWorldSid );
  614. TotalSdLength = sizeof(SECURITY_DESCRIPTOR) + AclLength;
  615. Sd = (PSECURITY_DESCRIPTOR) ExAllocatePoolWithTag(
  616. NonPagedPool,
  617. TotalSdLength,
  618. 'cAeS');
  619. if ( Sd == NULL ) {
  620. Status = STATUS_INSUFFICIENT_RESOURCES;
  621. goto Cleanup;
  622. }
  623. Acl = (PACL) (Sd + 1);
  624. Status = RtlCreateAcl( Acl, AclLength, ACL_REVISION2 );
  625. if ( NT_SUCCESS( Status )) {
  626. Status = RtlAddAuditAccessAce(
  627. Acl,
  628. ACL_REVISION2,
  629. SepProcessAccessesToAudit,
  630. SeWorldSid,
  631. TRUE,
  632. TRUE
  633. );
  634. if ( NT_SUCCESS( Status )) {
  635. Status = RtlCreateSecurityDescriptor( Sd,
  636. SECURITY_DESCRIPTOR_REVISION1 );
  637. if ( NT_SUCCESS( Status )) {
  638. Status = RtlSetSaclSecurityDescriptor( Sd,
  639. TRUE, Acl, FALSE );
  640. if ( NT_SUCCESS( Status )) {
  641. SepProcessAuditSd = Sd;
  642. }
  643. }
  644. }
  645. }
  646. ASSERT( NT_SUCCESS(Status) );
  647. if ( !NT_SUCCESS( Status )) {
  648. goto Cleanup;
  649. }
  650. //
  651. // create and initialize SepImportantProcessSd
  652. //
  653. AclLength = (ULONG)sizeof(ACL) +
  654. (3*((ULONG)sizeof(ACCESS_ALLOWED_ACE) - sizeof(ULONG))) +
  655. SeLengthSid( SeLocalSystemSid ) +
  656. SeLengthSid( SeLocalServiceSid ) +
  657. SeLengthSid( SeNetworkServiceSid );
  658. TotalSdLength = sizeof(SECURITY_DESCRIPTOR) + AclLength;
  659. Sd = (PSECURITY_DESCRIPTOR) ExAllocatePoolWithTag(
  660. NonPagedPool,
  661. TotalSdLength,
  662. 'cAeS');
  663. if ( Sd == NULL ) {
  664. Status = STATUS_INSUFFICIENT_RESOURCES;
  665. goto Cleanup;
  666. }
  667. Acl = (PACL) (Sd + 1);
  668. Status = RtlCreateAcl( Acl, AclLength, ACL_REVISION2 );
  669. if ( NT_SUCCESS( Status )) {
  670. Status = RtlAddAccessAllowedAce(
  671. Acl,
  672. ACL_REVISION2,
  673. SEP_QUERY_MEMBERSHIP,
  674. SeLocalSystemSid
  675. );
  676. if ( !NT_SUCCESS( Status )) {
  677. goto Cleanup;
  678. }
  679. Status = RtlAddAccessAllowedAce(
  680. Acl,
  681. ACL_REVISION2,
  682. SEP_QUERY_MEMBERSHIP,
  683. SeLocalServiceSid
  684. );
  685. if ( !NT_SUCCESS( Status )) {
  686. goto Cleanup;
  687. }
  688. Status = RtlAddAccessAllowedAce(
  689. Acl,
  690. ACL_REVISION2,
  691. SEP_QUERY_MEMBERSHIP,
  692. SeNetworkServiceSid
  693. );
  694. if ( !NT_SUCCESS( Status )) {
  695. goto Cleanup;
  696. }
  697. Status = RtlCreateSecurityDescriptor( Sd, SECURITY_DESCRIPTOR_REVISION1 );
  698. if ( NT_SUCCESS( Status )) {
  699. Status = RtlSetDaclSecurityDescriptor( Sd, TRUE, Acl, FALSE );
  700. if ( NT_SUCCESS( Status )) {
  701. SepImportantProcessSd = Sd;
  702. }
  703. }
  704. }
  705. Cleanup:
  706. if ( !NT_SUCCESS( Status )) {
  707. ASSERT( FALSE && L"SepInitProcessAuditSd failed" );
  708. //
  709. // this will bugcheck if SepCrashOnAuditFail is TRUE
  710. //
  711. SepAuditFailed();
  712. if ( Sd ) {
  713. ExFreePool( Sd );
  714. }
  715. }
  716. }
  717. VOID
  718. SepInitSystemDacls( VOID )
  719. /*++
  720. Routine Description:
  721. This function initializes the system's default dacls & security
  722. descriptors.
  723. Arguments:
  724. None.
  725. Return Value:
  726. None.
  727. --*/
  728. {
  729. NTSTATUS
  730. Status;
  731. ULONG
  732. PublicLength,
  733. PublicUnrestrictedLength,
  734. SystemLength,
  735. PublicOpenLength,
  736. UnrestrictedLength;
  737. PAGED_CODE();
  738. //
  739. // Set up a default ACLs
  740. //
  741. // Public: WORLD:execute, SYSTEM:all, ADMINS:all
  742. // PublicUnrestricted: WORLD:execute, SYSTEM:all, ADMINS:all, Restricted:execute
  743. // Public Open: WORLD:(Read|Write|Execute), ADMINS:(all), SYSTEM:all
  744. // System: SYSTEM:all, ADMINS:(read|execute|read_control)
  745. // Unrestricted: WORLD:(all), Restricted:(all)
  746. SystemLength = (ULONG)sizeof(ACL) +
  747. (2*((ULONG)sizeof(ACCESS_ALLOWED_ACE))) +
  748. SeLengthSid( SeLocalSystemSid ) +
  749. SeLengthSid( SeAliasAdminsSid ) +
  750. 8; // The 8 is just for good measure
  751. PublicLength = SystemLength +
  752. ((ULONG)sizeof(ACCESS_ALLOWED_ACE)) +
  753. SeLengthSid( SeWorldSid );
  754. PublicUnrestrictedLength = SystemLength +
  755. ((ULONG)sizeof(ACCESS_ALLOWED_ACE)) +
  756. SeLengthSid( SeRestrictedSid );
  757. PublicOpenLength = PublicLength;
  758. UnrestrictedLength = (ULONG)sizeof(ACL) +
  759. (2*((ULONG)sizeof(ACCESS_ALLOWED_ACE))) +
  760. SeLengthSid( SeWorldSid ) +
  761. SeLengthSid( SeRestrictedSid ) +
  762. 8; // The 8 is just for good measure
  763. SePublicDefaultDacl = (PACL)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE, PublicLength, 'cAeS');
  764. SePublicDefaultUnrestrictedDacl = (PACL)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE, PublicUnrestrictedLength, 'cAeS');
  765. SePublicOpenDacl = (PACL)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE, PublicOpenLength, 'cAeS');
  766. SePublicOpenUnrestrictedDacl = (PACL)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE, PublicUnrestrictedLength, 'cAeS');
  767. SeSystemDefaultDacl = (PACL)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE, SystemLength, 'cAeS');
  768. SeUnrestrictedDacl = (PACL)ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE, UnrestrictedLength, 'cAeS');
  769. ASSERT(SePublicDefaultDacl != NULL);
  770. ASSERT(SePublicDefaultUnrestrictedDacl != NULL);
  771. ASSERT(SePublicOpenDacl != NULL);
  772. ASSERT(SePublicOpenUnrestrictedDacl != NULL);
  773. ASSERT(SeSystemDefaultDacl != NULL);
  774. ASSERT(SeUnrestrictedDacl != NULL);
  775. Status = RtlCreateAcl( SePublicDefaultDacl, PublicLength, ACL_REVISION2);
  776. ASSERT( NT_SUCCESS(Status) );
  777. Status = RtlCreateAcl( SePublicDefaultUnrestrictedDacl, PublicUnrestrictedLength, ACL_REVISION2);
  778. ASSERT( NT_SUCCESS(Status) );
  779. Status = RtlCreateAcl( SePublicOpenDacl, PublicUnrestrictedLength, ACL_REVISION2);
  780. ASSERT( NT_SUCCESS(Status) );
  781. Status = RtlCreateAcl( SePublicOpenUnrestrictedDacl, PublicOpenLength, ACL_REVISION2);
  782. ASSERT( NT_SUCCESS(Status) );
  783. Status = RtlCreateAcl( SeSystemDefaultDacl, SystemLength, ACL_REVISION2);
  784. ASSERT( NT_SUCCESS(Status) );
  785. Status = RtlCreateAcl( SeUnrestrictedDacl, UnrestrictedLength, ACL_REVISION2);
  786. ASSERT( NT_SUCCESS(Status) );
  787. //
  788. // WORLD access (Public DACLs and Unrestricted only)
  789. //
  790. Status = RtlAddAccessAllowedAce (
  791. SePublicDefaultDacl,
  792. ACL_REVISION2,
  793. GENERIC_EXECUTE,
  794. SeWorldSid
  795. );
  796. ASSERT( NT_SUCCESS(Status) );
  797. Status = RtlAddAccessAllowedAce (
  798. SePublicDefaultUnrestrictedDacl,
  799. ACL_REVISION2,
  800. GENERIC_EXECUTE,
  801. SeWorldSid
  802. );
  803. ASSERT( NT_SUCCESS(Status) );
  804. Status = RtlAddAccessAllowedAce (
  805. SePublicOpenDacl,
  806. ACL_REVISION2,
  807. (GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE),
  808. SeWorldSid
  809. );
  810. ASSERT( NT_SUCCESS(Status) );
  811. Status = RtlAddAccessAllowedAce (
  812. SePublicOpenUnrestrictedDacl,
  813. ACL_REVISION2,
  814. (GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE),
  815. SeWorldSid
  816. );
  817. ASSERT( NT_SUCCESS(Status) );
  818. Status = RtlAddAccessAllowedAce (
  819. SeUnrestrictedDacl,
  820. ACL_REVISION2,
  821. GENERIC_ALL,
  822. SeWorldSid
  823. );
  824. ASSERT( NT_SUCCESS(Status) );
  825. //
  826. // SYSTEM access (PublicDefault, PublicOpen, and SystemDefault)
  827. //
  828. Status = RtlAddAccessAllowedAce (
  829. SePublicDefaultDacl,
  830. ACL_REVISION2,
  831. GENERIC_ALL,
  832. SeLocalSystemSid
  833. );
  834. ASSERT( NT_SUCCESS(Status) );
  835. Status = RtlAddAccessAllowedAce (
  836. SePublicDefaultUnrestrictedDacl,
  837. ACL_REVISION2,
  838. GENERIC_ALL,
  839. SeLocalSystemSid
  840. );
  841. ASSERT( NT_SUCCESS(Status) );
  842. Status = RtlAddAccessAllowedAce (
  843. SePublicOpenDacl,
  844. ACL_REVISION2,
  845. GENERIC_ALL,
  846. SeLocalSystemSid
  847. );
  848. ASSERT( NT_SUCCESS(Status) );
  849. Status = RtlAddAccessAllowedAce (
  850. SePublicOpenUnrestrictedDacl,
  851. ACL_REVISION2,
  852. GENERIC_ALL,
  853. SeLocalSystemSid
  854. );
  855. ASSERT( NT_SUCCESS(Status) );
  856. Status = RtlAddAccessAllowedAce (
  857. SeSystemDefaultDacl,
  858. ACL_REVISION2,
  859. GENERIC_ALL,
  860. SeLocalSystemSid
  861. );
  862. ASSERT( NT_SUCCESS(Status) );
  863. //
  864. // ADMINISTRATORS access (PublicDefault, PublicOpen, and SystemDefault)
  865. //
  866. Status = RtlAddAccessAllowedAce (
  867. SePublicDefaultDacl,
  868. ACL_REVISION2,
  869. GENERIC_ALL,
  870. SeAliasAdminsSid
  871. );
  872. ASSERT( NT_SUCCESS(Status) );
  873. Status = RtlAddAccessAllowedAce (
  874. SePublicDefaultUnrestrictedDacl,
  875. ACL_REVISION2,
  876. GENERIC_ALL,
  877. SeAliasAdminsSid
  878. );
  879. ASSERT( NT_SUCCESS(Status) );
  880. Status = RtlAddAccessAllowedAce (
  881. SePublicOpenDacl,
  882. ACL_REVISION2,
  883. GENERIC_ALL,
  884. SeAliasAdminsSid
  885. );
  886. ASSERT( NT_SUCCESS(Status) );
  887. Status = RtlAddAccessAllowedAce (
  888. SePublicOpenUnrestrictedDacl,
  889. ACL_REVISION2,
  890. GENERIC_ALL,
  891. SeAliasAdminsSid
  892. );
  893. ASSERT( NT_SUCCESS(Status) );
  894. Status = RtlAddAccessAllowedAce (
  895. SeSystemDefaultDacl,
  896. ACL_REVISION2,
  897. GENERIC_READ | GENERIC_EXECUTE | READ_CONTROL,
  898. SeAliasAdminsSid
  899. );
  900. ASSERT( NT_SUCCESS(Status) );
  901. //
  902. // RESTRICTED access (PublicDefaultUnrestricted and Unrestricted)
  903. //
  904. Status = RtlAddAccessAllowedAce (
  905. SePublicDefaultUnrestrictedDacl,
  906. ACL_REVISION2,
  907. GENERIC_EXECUTE,
  908. SeRestrictedSid
  909. );
  910. ASSERT( NT_SUCCESS(Status) );
  911. Status = RtlAddAccessAllowedAce (
  912. SePublicOpenUnrestrictedDacl,
  913. ACL_REVISION2,
  914. GENERIC_EXECUTE | GENERIC_READ,
  915. SeRestrictedSid
  916. );
  917. ASSERT( NT_SUCCESS(Status) );
  918. Status = RtlAddAccessAllowedAce (
  919. SeUnrestrictedDacl,
  920. ACL_REVISION2,
  921. GENERIC_ALL,
  922. SeRestrictedSid
  923. );
  924. ASSERT( NT_SUCCESS(Status) );
  925. //
  926. // Now initialize security descriptors
  927. // that export this protection
  928. //
  929. SePublicDefaultSd = (PSECURITY_DESCRIPTOR)&SepPublicDefaultSd;
  930. Status = RtlCreateSecurityDescriptor(
  931. SePublicDefaultSd,
  932. SECURITY_DESCRIPTOR_REVISION1
  933. );
  934. ASSERT( NT_SUCCESS(Status) );
  935. Status = RtlSetDaclSecurityDescriptor(
  936. SePublicDefaultSd,
  937. TRUE, // DaclPresent
  938. SePublicDefaultDacl,
  939. FALSE // DaclDefaulted
  940. );
  941. ASSERT( NT_SUCCESS(Status) );
  942. SePublicDefaultUnrestrictedSd = (PSECURITY_DESCRIPTOR)&SepPublicDefaultUnrestrictedSd;
  943. Status = RtlCreateSecurityDescriptor(
  944. SePublicDefaultUnrestrictedSd,
  945. SECURITY_DESCRIPTOR_REVISION1
  946. );
  947. ASSERT( NT_SUCCESS(Status) );
  948. Status = RtlSetDaclSecurityDescriptor(
  949. SePublicDefaultUnrestrictedSd,
  950. TRUE, // DaclPresent
  951. SePublicDefaultUnrestrictedDacl,
  952. FALSE // DaclDefaulted
  953. );
  954. ASSERT( NT_SUCCESS(Status) );
  955. SePublicOpenSd = (PSECURITY_DESCRIPTOR)&SepPublicOpenSd;
  956. Status = RtlCreateSecurityDescriptor(
  957. SePublicOpenSd,
  958. SECURITY_DESCRIPTOR_REVISION1
  959. );
  960. ASSERT( NT_SUCCESS(Status) );
  961. Status = RtlSetDaclSecurityDescriptor(
  962. SePublicOpenSd,
  963. TRUE, // DaclPresent
  964. SePublicOpenDacl,
  965. FALSE // DaclDefaulted
  966. );
  967. ASSERT( NT_SUCCESS(Status) );
  968. SePublicOpenUnrestrictedSd = (PSECURITY_DESCRIPTOR)&SepPublicOpenUnrestrictedSd;
  969. Status = RtlCreateSecurityDescriptor(
  970. SePublicOpenUnrestrictedSd,
  971. SECURITY_DESCRIPTOR_REVISION1
  972. );
  973. ASSERT( NT_SUCCESS(Status) );
  974. Status = RtlSetDaclSecurityDescriptor(
  975. SePublicOpenUnrestrictedSd,
  976. TRUE, // DaclPresent
  977. SePublicOpenUnrestrictedDacl,
  978. FALSE // DaclDefaulted
  979. );
  980. ASSERT( NT_SUCCESS(Status) );
  981. SeSystemDefaultSd = (PSECURITY_DESCRIPTOR)&SepSystemDefaultSd;
  982. Status = RtlCreateSecurityDescriptor(
  983. SeSystemDefaultSd,
  984. SECURITY_DESCRIPTOR_REVISION1
  985. );
  986. ASSERT( NT_SUCCESS(Status) );
  987. Status = RtlSetDaclSecurityDescriptor(
  988. SeSystemDefaultSd,
  989. TRUE, // DaclPresent
  990. SeSystemDefaultDacl,
  991. FALSE // DaclDefaulted
  992. );
  993. ASSERT( NT_SUCCESS(Status) );
  994. SeUnrestrictedSd = (PSECURITY_DESCRIPTOR)&SepUnrestrictedSd;
  995. Status = RtlCreateSecurityDescriptor(
  996. SeUnrestrictedSd,
  997. SECURITY_DESCRIPTOR_REVISION1
  998. );
  999. ASSERT( NT_SUCCESS(Status) );
  1000. Status = RtlSetDaclSecurityDescriptor(
  1001. SeUnrestrictedSd,
  1002. TRUE, // DaclPresent
  1003. SeUnrestrictedDacl,
  1004. FALSE // DaclDefaulted
  1005. );
  1006. ASSERT( NT_SUCCESS(Status) );
  1007. return;
  1008. }
  1009. VOID
  1010. SepInitializePrivilegeSets( VOID )
  1011. /*++
  1012. Routine Description:
  1013. This routine is called once during system initialization to pre-allocate
  1014. and initialize some commonly used privilege sets.
  1015. Arguments:
  1016. None
  1017. Return Value:
  1018. None.
  1019. --*/
  1020. {
  1021. PAGED_CODE();
  1022. SinglePrivilegeSetSize = sizeof( PRIVILEGE_SET );
  1023. DoublePrivilegeSetSize = sizeof( PRIVILEGE_SET ) +
  1024. (ULONG)sizeof( LUID_AND_ATTRIBUTES );
  1025. SepSystemSecurityPrivilegeSet = ExAllocatePoolWithTag( PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE, SinglePrivilegeSetSize, 'rPeS' );
  1026. SepTakeOwnershipPrivilegeSet = ExAllocatePoolWithTag( PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE, SinglePrivilegeSetSize, 'rPeS' );
  1027. SepDoublePrivilegeSet = ExAllocatePoolWithTag( PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE, DoublePrivilegeSetSize, 'rPeS' );
  1028. SepSystemSecurityPrivilegeSet->PrivilegeCount = 1;
  1029. SepSystemSecurityPrivilegeSet->Control = 0;
  1030. SepSystemSecurityPrivilegeSet->Privilege[0].Luid = SeSecurityPrivilege;
  1031. SepSystemSecurityPrivilegeSet->Privilege[0].Attributes = SE_PRIVILEGE_USED_FOR_ACCESS;
  1032. SepTakeOwnershipPrivilegeSet->PrivilegeCount = 1;
  1033. SepTakeOwnershipPrivilegeSet->Control = 0;
  1034. SepTakeOwnershipPrivilegeSet->Privilege[0].Luid = SeTakeOwnershipPrivilege;
  1035. SepTakeOwnershipPrivilegeSet->Privilege[0].Attributes = SE_PRIVILEGE_USED_FOR_ACCESS;
  1036. SepDoublePrivilegeSet->PrivilegeCount = 2;
  1037. SepDoublePrivilegeSet->Control = 0;
  1038. SepDoublePrivilegeSet->Privilege[0].Luid = SeSecurityPrivilege;
  1039. SepDoublePrivilegeSet->Privilege[0].Attributes = SE_PRIVILEGE_USED_FOR_ACCESS;
  1040. SepDoublePrivilegeSet->Privilege[1].Luid = SeTakeOwnershipPrivilege;
  1041. SepDoublePrivilegeSet->Privilege[1].Attributes = SE_PRIVILEGE_USED_FOR_ACCESS;
  1042. }
  1043. VOID
  1044. SepAssemblePrivileges(
  1045. IN ULONG PrivilegeCount,
  1046. IN BOOLEAN SystemSecurity,
  1047. IN BOOLEAN WriteOwner,
  1048. OUT PPRIVILEGE_SET *Privileges
  1049. )
  1050. /*++
  1051. Routine Description:
  1052. This routine takes the results of the various privilege checks
  1053. in SeAccessCheck and returns an appropriate privilege set.
  1054. Arguments:
  1055. PrivilegeCount - The number of privileges granted.
  1056. SystemSecurity - Provides a boolean indicating whether to put
  1057. SeSecurityPrivilege into the output privilege set.
  1058. WriteOwner - Provides a boolean indicating whether to put
  1059. SeTakeOwnershipPrivilege into the output privilege set.
  1060. Privileges - Supplies a pointer that will return the privilege
  1061. set. Should be freed with ExFreePool when no longer needed.
  1062. Return Value:
  1063. None.
  1064. --*/
  1065. {
  1066. PPRIVILEGE_SET PrivilegeSet;
  1067. ULONG SizeRequired;
  1068. PAGED_CODE();
  1069. ASSERT( (PrivilegeCount != 0) && (PrivilegeCount <= 2) );
  1070. if ( !ARGUMENT_PRESENT( Privileges ) ) {
  1071. return;
  1072. }
  1073. if ( PrivilegeCount == 1 ) {
  1074. SizeRequired = SinglePrivilegeSetSize;
  1075. if ( SystemSecurity ) {
  1076. PrivilegeSet = SepSystemSecurityPrivilegeSet;
  1077. } else {
  1078. ASSERT( WriteOwner );
  1079. PrivilegeSet = SepTakeOwnershipPrivilegeSet;
  1080. }
  1081. } else {
  1082. SizeRequired = DoublePrivilegeSetSize;
  1083. PrivilegeSet = SepDoublePrivilegeSet;
  1084. }
  1085. *Privileges = ExAllocatePoolWithTag( PagedPool, SizeRequired, 'rPeS' );
  1086. if ( *Privileges != NULL ) {
  1087. RtlCopyMemory (
  1088. *Privileges,
  1089. PrivilegeSet,
  1090. SizeRequired
  1091. );
  1092. }
  1093. }
  1094. BOOLEAN
  1095. SepInitializeWorkList(
  1096. VOID
  1097. )
  1098. /*++
  1099. Routine Description:
  1100. Initializes the mutex and list head used to queue work from the
  1101. Executive to LSA. This mechanism operates on top of the normal ExWorkerThread
  1102. mechanism by capturing the first thread to perform LSA work and keeping it
  1103. until all the current work is done.
  1104. The reduces the number of worker threads that are blocked on I/O to LSA.
  1105. Arguments:
  1106. None.
  1107. Return Value:
  1108. TRUE if successful, FALSE otherwise.
  1109. --*/
  1110. {
  1111. PAGED_CODE();
  1112. ExInitializeResourceLite(&SepLsaQueueLock);
  1113. InitializeListHead(&SepLsaQueue);
  1114. return( TRUE );
  1115. }