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.

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