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.

1267 lines
38 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. ctaccess.c
  5. Abstract:
  6. Common access validation test routines
  7. These routines are used in both kernel and user mode tests.
  8. This test assumes the security runtime library routines are
  9. functioning correctly.
  10. Author:
  11. Robert Reichel (robertre) 12/14/90
  12. Environment:
  13. Test of access validation routines
  14. Revision History:
  15. v1: robertre
  16. Created
  17. --*/
  18. #include "tsecomm.c" // Mode dependent macros and routines.
  19. //
  20. // Define the local macros and procedure for this module
  21. //
  22. //
  23. // Return a pointer to the first Ace in an Acl (even if the Acl is empty).
  24. //
  25. // PACE_HEADER
  26. // FirstAce (
  27. // IN PACL Acl
  28. // );
  29. //
  30. #define FirstAce(Acl) ((PVOID)((PUCHAR)(Acl) + sizeof(ACL)))
  31. //
  32. // Return a pointer to the next Ace in a sequence (even if the input
  33. // Ace is the one in the sequence).
  34. //
  35. // PACE_HEADER
  36. // NextAce (
  37. // IN PACE_HEADER Ace
  38. // );
  39. //
  40. #define NextAce(Ace) ((PVOID)((PUCHAR)(Ace) + ((PACE_HEADER)(Ace))->AceSize))
  41. VOID
  42. DumpAcl (
  43. IN PACL Acl
  44. );
  45. ////////////////////////////////////////////////////////////////
  46. // //
  47. // Module wide variables //
  48. // //
  49. ////////////////////////////////////////////////////////////////
  50. #define DEFAULT_DACL_LENGTH (1024L)
  51. #define GROUP_IDS_LENGTH (1024L)
  52. #define NEW_GROUP_STATE_LENGTH (1024L)
  53. #define PRIVILEGES_LENGTH (128L)
  54. #define TOO_BIG_ACL_SIZE (2048L)
  55. //
  56. // definitions related to TokenWithGroups
  57. //
  58. #define FLINTSTONE_INDEX (0L)
  59. #define CHILD_INDEX (1L)
  60. #define NEANDERTHOL_INDEX (2L)
  61. #define WORLD_INDEX (3L)
  62. #define GROUP_COUNT (4L)
  63. //
  64. // Definitions related to TokenWithPrivileges
  65. //
  66. #define UNSOLICITED_INDEX (0L)
  67. #define SECURITY_INDEX (1L)
  68. #define PRIVILEGE_COUNT (2L)
  69. //
  70. // Access types
  71. //
  72. #define SET_WIDGET_COLOR 0x00000001
  73. #define SET_WIDGET_SIZE 0x00000002
  74. #define GET_WIDGET_COLOR 0x00000004
  75. #define GET_WIDGET_SIZE 0x00000008
  76. #define START_WIDGET 0x00000010
  77. #define STOP_WIDGET 0x00000020
  78. #define GIVE_WIDGET 0x00000040
  79. #define TAKE_WIDGET 0x00000080
  80. NTSTATUS Status;
  81. HANDLE SimpleToken;
  82. HANDLE TokenWithGroups;
  83. HANDLE TokenWithDefaultOwner;
  84. HANDLE TokenWithPrivileges;
  85. HANDLE TokenWithDefaultDacl;
  86. HANDLE Token;
  87. HANDLE ImpersonationToken;
  88. HANDLE PrimaryToken;
  89. HANDLE AnonymousToken;
  90. OBJECT_ATTRIBUTES PrimaryTokenAttributes;
  91. PSECURITY_DESCRIPTOR PrimarySecurityDescriptor;
  92. SECURITY_QUALITY_OF_SERVICE PrimarySecurityQos;
  93. OBJECT_ATTRIBUTES ImpersonationTokenAttributes;
  94. PSECURITY_DESCRIPTOR ImpersonationSecurityDescriptor;
  95. SECURITY_QUALITY_OF_SERVICE ImpersonationSecurityQos;
  96. OBJECT_ATTRIBUTES AnonymousTokenAttributes;
  97. PSECURITY_DESCRIPTOR AnonymousSecurityDescriptor;
  98. SECURITY_QUALITY_OF_SERVICE AnonymousSecurityQos;
  99. ULONG DisabledGroupAttributes;
  100. ULONG OptionalGroupAttributes;
  101. ULONG NormalGroupAttributes;
  102. ULONG OwnerGroupAttributes;
  103. ULONG LengthAvailable;
  104. ULONG CurrentLength;
  105. TIME_FIELDS TempTimeFields = {3000, 1, 1, 1, 1, 1, 1, 1};
  106. LARGE_INTEGER NoExpiration;
  107. LUID DummyAuthenticationId;
  108. LUID SystemAuthenticationId = SYSTEM_LUID;
  109. TOKEN_SOURCE TestSource = {"SE: TEST", 0};
  110. PSID Owner;
  111. PSID Group;
  112. PACL Dacl;
  113. PSID TempOwner;
  114. PSID TempGroup;
  115. PACL TempDacl;
  116. ////////////////////////////////////////////////////////////////
  117. // //
  118. // Initialization Routine //
  119. // //
  120. ////////////////////////////////////////////////////////////////
  121. BOOLEAN
  122. TestTokenInitialize()
  123. {
  124. TSeVariableInitialization(); // Initialize global variables
  125. DisabledGroupAttributes = (SE_GROUP_ENABLED_BY_DEFAULT);
  126. OptionalGroupAttributes = (SE_GROUP_ENABLED_BY_DEFAULT |
  127. SE_GROUP_ENABLED
  128. );
  129. NormalGroupAttributes = (SE_GROUP_MANDATORY |
  130. SE_GROUP_ENABLED_BY_DEFAULT |
  131. SE_GROUP_ENABLED
  132. );
  133. OwnerGroupAttributes = (SE_GROUP_MANDATORY |
  134. SE_GROUP_ENABLED_BY_DEFAULT |
  135. SE_GROUP_ENABLED |
  136. SE_GROUP_OWNER
  137. );
  138. PrimarySecurityDescriptor =
  139. (PSECURITY_DESCRIPTOR)TstAllocatePool( PagedPool, 1024 );
  140. InitializeObjectAttributes(
  141. &PrimaryTokenAttributes,
  142. NULL,
  143. OBJ_INHERIT,
  144. NULL,
  145. NULL
  146. );
  147. ImpersonationSecurityDescriptor =
  148. (PSECURITY_DESCRIPTOR)TstAllocatePool( PagedPool, 1024 );
  149. ImpersonationSecurityQos.Length = (ULONG)sizeof(SECURITY_QUALITY_OF_SERVICE);
  150. ImpersonationSecurityQos.ImpersonationLevel = SecurityImpersonation;
  151. ImpersonationSecurityQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
  152. ImpersonationSecurityQos.EffectiveOnly = FALSE;
  153. InitializeObjectAttributes(
  154. &ImpersonationTokenAttributes,
  155. NULL,
  156. OBJ_INHERIT,
  157. NULL,
  158. NULL
  159. );
  160. ImpersonationTokenAttributes.SecurityQualityOfService =
  161. &ImpersonationSecurityQos;
  162. AnonymousSecurityDescriptor =
  163. (PSECURITY_DESCRIPTOR)TstAllocatePool( PagedPool, 1024 );
  164. AnonymousSecurityQos.Length = (ULONG)sizeof(SECURITY_QUALITY_OF_SERVICE);
  165. AnonymousSecurityQos.ImpersonationLevel = SecurityAnonymous;
  166. AnonymousSecurityQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
  167. AnonymousSecurityQos.EffectiveOnly = FALSE;
  168. InitializeObjectAttributes(
  169. &AnonymousTokenAttributes,
  170. NULL,
  171. OBJ_INHERIT,
  172. NULL,
  173. NULL
  174. );
  175. AnonymousTokenAttributes.SecurityQualityOfService =
  176. &AnonymousSecurityQos;
  177. //
  178. // Build an ACL for use.
  179. //
  180. Dacl = (PACL)TstAllocatePool( PagedPool, 256 );
  181. Dacl->AclRevision=ACL_REVISION;
  182. Dacl->Sbz1=0;
  183. Dacl->Sbz2=0;
  184. Dacl->AclSize=256;
  185. Dacl->AceCount=0;
  186. //
  187. // Set up expiration times
  188. //
  189. TempTimeFields.Year = 3000;
  190. TempTimeFields.Month = 1;
  191. TempTimeFields.Day = 1;
  192. TempTimeFields.Hour = 1;
  193. TempTimeFields.Minute = 1;
  194. TempTimeFields.Second = 1;
  195. TempTimeFields.Milliseconds = 1;
  196. TempTimeFields.Weekday = 1;
  197. RtlTimeFieldsToTime( &TempTimeFields, &NoExpiration );
  198. //
  199. // Use a dummy authentication ID for a while.
  200. //
  201. DummyAuthenticationId = FredLuid;
  202. //
  203. // Use a token source specific to security test
  204. //
  205. NtAllocateLocallyUniqueId( &(TestSource.SourceIdentifier) );
  206. DbgPrint("Done.\n");
  207. return TRUE;
  208. }
  209. BOOLEAN
  210. CreateDAclToken()
  211. {
  212. BOOLEAN CompletionStatus = TRUE;
  213. TOKEN_USER UserId;
  214. TOKEN_PRIMARY_GROUP PrimaryGroup;
  215. PTOKEN_GROUPS GroupIds;
  216. PTOKEN_PRIVILEGES Privileges;
  217. TOKEN_DEFAULT_DACL DefaultDacl;
  218. TOKEN_OWNER Owner;
  219. PSECURITY_DESCRIPTOR Widget1SecurityDescriptor;
  220. NTSTATUS AccessStatus;
  221. ACCESS_MASK GrantedAccess;
  222. PACCESS_ALLOWED_ACE AllowBarneySetColor;
  223. PACCESS_ALLOWED_ACE AllowFredSetColor;
  224. PACCESS_DENIED_ACE DenyPebblesSetColor;
  225. PACCESS_ALLOWED_ACE AllowPebblesSetColor;
  226. PACCESS_DENIED_ACE DenyFredSetColor;
  227. PACCESS_ALLOWED_ACE AllowBarneySetSize;
  228. PACCESS_ALLOWED_ACE AllowPebblesSetSize;
  229. PACCESS_ALLOWED_ACE AllowPebblesGetColor;
  230. PACCESS_ALLOWED_ACE AllowPebblesGetSize;
  231. USHORT AllowBarneySetColorLength;
  232. USHORT AllowFredSetColorLength;
  233. USHORT DenyPebblesSetColorLength;
  234. USHORT AllowPebblesSetColorLength;
  235. USHORT DenyFredSetColorLength;
  236. USHORT AllowBarneySetSizeLength;
  237. USHORT AllowPebblesSetSizeLength;
  238. USHORT AllowPebblesGetColorLength;
  239. USHORT AllowPebblesGetSizeLength;
  240. DbgPrint("\n");
  241. GroupIds = (PTOKEN_GROUPS)TstAllocatePool( PagedPool,
  242. GROUP_IDS_LENGTH
  243. );
  244. Privileges = (PTOKEN_PRIVILEGES)TstAllocatePool( PagedPool,
  245. PRIVILEGES_LENGTH
  246. );
  247. DefaultDacl.DefaultDacl = (PACL)TstAllocatePool( PagedPool,
  248. DEFAULT_DACL_LENGTH
  249. );
  250. //
  251. // Create a token with default DACL
  252. //
  253. DbgPrint("Se: Create Token With Default Dacl ... ");
  254. GroupIds->GroupCount = GROUP_COUNT;
  255. GroupIds->Groups[FLINTSTONE_INDEX].Sid = FlintstoneSid;
  256. GroupIds->Groups[CHILD_INDEX].Sid = ChildSid;
  257. GroupIds->Groups[NEANDERTHOL_INDEX].Sid = NeandertholSid;
  258. GroupIds->Groups[WORLD_INDEX].Sid = WorldSid;
  259. GroupIds->Groups[FLINTSTONE_INDEX].Attributes = OwnerGroupAttributes;
  260. GroupIds->Groups[CHILD_INDEX].Attributes = OptionalGroupAttributes;
  261. GroupIds->Groups[NEANDERTHOL_INDEX].Attributes = OptionalGroupAttributes;
  262. GroupIds->Groups[WORLD_INDEX].Attributes = NormalGroupAttributes;
  263. UserId.User.Sid = PebblesSid;
  264. UserId.User.Attributes = 0;
  265. Owner.Owner = FlintstoneSid;
  266. Privileges->PrivilegeCount = PRIVILEGE_COUNT;
  267. Privileges->Privileges[UNSOLICITED_INDEX].Luid = UnsolicitedInputPrivilege;
  268. Privileges->Privileges[SECURITY_INDEX].Luid = SecurityPrivilege;
  269. Privileges->Privileges[UNSOLICITED_INDEX].Attributes = 0;
  270. Privileges->Privileges[SECURITY_INDEX].Attributes = 0;
  271. PrimaryGroup.PrimaryGroup = FlintstoneSid;
  272. Status = RtlCreateAcl( DefaultDacl.DefaultDacl, DEFAULT_DACL_LENGTH, ACL_REVISION);
  273. ASSERT(NT_SUCCESS(Status) );
  274. Status = NtCreateToken(
  275. &PrimaryToken, // Handle
  276. (TOKEN_ALL_ACCESS), // DesiredAccess
  277. &PrimaryTokenAttributes, // ObjectAttributes
  278. TokenPrimary, // TokenType
  279. &DummyAuthenticationId, // Authentication LUID
  280. &NoExpiration, // Expiration Time
  281. &UserId, // Owner ID
  282. GroupIds, // Group IDs
  283. Privileges, // Privileges
  284. &Owner, // Owner
  285. &PrimaryGroup, // Primary Group
  286. &DefaultDacl, // Default Dacl
  287. &TestSource // TokenSource
  288. );
  289. if (NT_SUCCESS(Status)) {
  290. DbgPrint("Succeeded.\n");
  291. } else {
  292. DbgPrint("********** Failed ************\n");
  293. DbgPrint("Status is: 0x%lx \n", Status);
  294. CompletionStatus = FALSE;
  295. }
  296. ASSERT(NT_SUCCESS(Status));
  297. //
  298. // Create an impersonation token, Impersonation level = Impersonation
  299. //
  300. DbgPrint("Se: Create an impersonation token ... ");
  301. GroupIds->GroupCount = GROUP_COUNT;
  302. GroupIds->Groups[FLINTSTONE_INDEX].Sid = FlintstoneSid;
  303. GroupIds->Groups[CHILD_INDEX].Sid = ChildSid;
  304. GroupIds->Groups[NEANDERTHOL_INDEX].Sid = NeandertholSid;
  305. GroupIds->Groups[WORLD_INDEX].Sid = WorldSid;
  306. GroupIds->Groups[FLINTSTONE_INDEX].Attributes = OwnerGroupAttributes;
  307. GroupIds->Groups[CHILD_INDEX].Attributes = OptionalGroupAttributes;
  308. GroupIds->Groups[NEANDERTHOL_INDEX].Attributes = OptionalGroupAttributes;
  309. GroupIds->Groups[WORLD_INDEX].Attributes = NormalGroupAttributes;
  310. UserId.User.Sid = PebblesSid;
  311. UserId.User.Attributes = 0;
  312. Owner.Owner = FlintstoneSid;
  313. Privileges->PrivilegeCount = PRIVILEGE_COUNT;
  314. Privileges->Privileges[UNSOLICITED_INDEX].Luid = UnsolicitedInputPrivilege;
  315. Privileges->Privileges[SECURITY_INDEX].Luid = SecurityPrivilege;
  316. Privileges->Privileges[UNSOLICITED_INDEX].Attributes = 0;
  317. Privileges->Privileges[SECURITY_INDEX].Attributes = 0;
  318. PrimaryGroup.PrimaryGroup = FlintstoneSid;
  319. Status = RtlCreateAcl( DefaultDacl.DefaultDacl, DEFAULT_DACL_LENGTH, ACL_REVISION);
  320. ASSERT(NT_SUCCESS(Status) );
  321. Status = NtCreateToken(
  322. &ImpersonationToken, // Handle
  323. (TOKEN_ALL_ACCESS), // DesiredAccess
  324. &ImpersonationTokenAttributes, // ObjectAttributes
  325. TokenImpersonation, // TokenType
  326. &DummyAuthenticationId, // Authentication LUID
  327. &NoExpiration, // Expiration Time
  328. &UserId, // Owner ID
  329. GroupIds, // Group IDs
  330. Privileges, // Privileges
  331. &Owner, // Owner
  332. &PrimaryGroup, // Primary Group
  333. &DefaultDacl, // Default Dacl
  334. &TestSource // TokenSource
  335. );
  336. if (NT_SUCCESS(Status)) {
  337. DbgPrint("Succeeded.\n");
  338. } else {
  339. DbgPrint("********** Failed ************\n");
  340. DbgPrint("Status is: 0x%lx \n", Status);
  341. CompletionStatus = FALSE;
  342. }
  343. ASSERT(NT_SUCCESS(Status));
  344. //
  345. // Attach tokens to process
  346. //
  347. NtSetInformationProcess(
  348. NtCurrentProcess(),
  349. ProcessAccessToken,
  350. &PrimaryToken,
  351. sizeof( PHANDLE ));
  352. NtSetInformationThread(
  353. NtCurrentThread(),
  354. ThreadImpersonationToken,
  355. &ImpersonationToken,
  356. sizeof( PHANDLE ));
  357. // Create some ACEs
  358. // AllowBarneySetColor
  359. AllowBarneySetColorLength = (USHORT)(sizeof( ACCESS_ALLOWED_ACE ) - sizeof( ULONG ) +
  360. SeLengthSid( BarneySid ));
  361. AllowBarneySetColor = (PVOID) TstAllocatePool ( PagedPool, AllowBarneySetColorLength );
  362. AllowBarneySetColor->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  363. AllowBarneySetColor->Header.AceSize = AllowBarneySetColorLength;
  364. AllowBarneySetColor->Header.AceFlags = 0;
  365. AllowBarneySetColor->Mask = SET_WIDGET_COLOR;
  366. RtlCopySid(
  367. SeLengthSid( BarneySid ),
  368. &(AllowBarneySetColor->SidStart),
  369. BarneySid );
  370. // DenyPebblesSetColor
  371. DenyPebblesSetColorLength = (USHORT)(sizeof( ACCESS_DENIED_ACE ) - sizeof( ULONG ) +
  372. SeLengthSid( BarneySid ));
  373. DenyPebblesSetColor = (PVOID) TstAllocatePool ( PagedPool, DenyPebblesSetColorLength );
  374. DenyPebblesSetColor->Header.AceType = ACCESS_DENIED_ACE_TYPE;
  375. DenyPebblesSetColor->Header.AceSize = DenyPebblesSetColorLength;
  376. DenyPebblesSetColor->Header.AceFlags = 0;
  377. DenyPebblesSetColor->Mask = SET_WIDGET_COLOR;
  378. RtlCopySid(
  379. SeLengthSid( PebblesSid ),
  380. &(DenyPebblesSetColor->SidStart),
  381. PebblesSid );
  382. // AllowFredSetColor
  383. AllowFredSetColorLength = (USHORT)(sizeof( ACCESS_ALLOWED_ACE ) - sizeof( ULONG ) +
  384. SeLengthSid( FredSid ));
  385. AllowFredSetColor = (PVOID) TstAllocatePool ( PagedPool, AllowFredSetColorLength );
  386. AllowFredSetColor->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  387. AllowFredSetColor->Header.AceSize = AllowFredSetColorLength;
  388. AllowFredSetColor->Header.AceFlags = 0;
  389. AllowFredSetColor->Mask = SET_WIDGET_COLOR;
  390. RtlCopySid(
  391. SeLengthSid( FredSid ),
  392. &(AllowFredSetColor->SidStart),
  393. FredSid );
  394. // AllowPebblesSetColor
  395. AllowPebblesSetColorLength = (USHORT)(sizeof( ACCESS_ALLOWED_ACE ) - sizeof( ULONG ) +
  396. SeLengthSid( PebblesSid ));
  397. AllowPebblesSetColor = (PVOID) TstAllocatePool ( PagedPool, AllowPebblesSetColorLength );
  398. AllowPebblesSetColor->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  399. AllowPebblesSetColor->Header.AceSize = AllowPebblesSetColorLength;
  400. AllowPebblesSetColor->Header.AceFlags = 0;
  401. AllowPebblesSetColor->Mask = SET_WIDGET_COLOR;
  402. RtlCopySid(
  403. SeLengthSid( PebblesSid ),
  404. &(AllowPebblesSetColor->SidStart),
  405. PebblesSid );
  406. // DenyFredSetColor
  407. DenyFredSetColorLength = (USHORT)(sizeof( ACCESS_DENIED_ACE ) - sizeof( ULONG ) +
  408. SeLengthSid( FredSid ));
  409. DenyFredSetColor = (PVOID) TstAllocatePool ( PagedPool, DenyFredSetColorLength );
  410. DenyFredSetColor->Header.AceType = ACCESS_DENIED_ACE_TYPE;
  411. DenyFredSetColor->Header.AceSize = DenyFredSetColorLength;
  412. DenyFredSetColor->Header.AceFlags = 0;
  413. DenyFredSetColor->Mask = SET_WIDGET_COLOR;
  414. RtlCopySid(
  415. SeLengthSid( FredSid ),
  416. &(DenyFredSetColor->SidStart),
  417. FredSid );
  418. // AllowBarneySetSize
  419. AllowBarneySetSizeLength = (USHORT)(sizeof( ACCESS_ALLOWED_ACE ) - sizeof( ULONG ) +
  420. SeLengthSid( BarneySid ));
  421. AllowBarneySetSize = (PVOID) TstAllocatePool ( PagedPool, AllowBarneySetSizeLength );
  422. AllowBarneySetSize->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  423. AllowBarneySetSize->Header.AceSize = AllowBarneySetSizeLength;
  424. AllowBarneySetSize->Header.AceFlags = 0;
  425. AllowBarneySetSize->Mask = SET_WIDGET_SIZE;
  426. RtlCopySid(
  427. SeLengthSid( BarneySid ),
  428. &(AllowBarneySetSize->SidStart),
  429. BarneySid );
  430. // AllowPebblesSetSize
  431. AllowPebblesSetSizeLength = (USHORT)(sizeof( ACCESS_ALLOWED_ACE ) - sizeof( ULONG ) +
  432. SeLengthSid( PebblesSid ));
  433. AllowPebblesSetSize = (PVOID) TstAllocatePool ( PagedPool, AllowPebblesSetSizeLength );
  434. AllowPebblesSetSize->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  435. AllowPebblesSetSize->Header.AceSize = AllowPebblesSetSizeLength;
  436. AllowPebblesSetSize->Header.AceFlags = 0;
  437. AllowPebblesSetSize->Mask = SET_WIDGET_SIZE;
  438. RtlCopySid(
  439. SeLengthSid( PebblesSid ),
  440. &(AllowPebblesSetSize->SidStart),
  441. PebblesSid );
  442. // AllowPebblesGetSize
  443. AllowPebblesGetSizeLength = (USHORT)(sizeof( ACCESS_ALLOWED_ACE ) - sizeof( ULONG ) +
  444. SeLengthSid( PebblesSid ));
  445. AllowPebblesGetSize = (PVOID) TstAllocatePool ( PagedPool, AllowPebblesGetSizeLength );
  446. AllowPebblesGetSize->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  447. AllowPebblesGetSize->Header.AceSize = AllowPebblesGetSizeLength;
  448. AllowPebblesGetSize->Header.AceFlags = 0;
  449. AllowPebblesGetSize->Mask = SET_WIDGET_SIZE;
  450. RtlCopySid(
  451. SeLengthSid( PebblesSid ),
  452. &(AllowPebblesGetSize->SidStart),
  453. PebblesSid );
  454. // AllowPebblesGetColor
  455. AllowPebblesGetColorLength = (USHORT)(sizeof( ACCESS_ALLOWED_ACE ) - sizeof( ULONG ) +
  456. SeLengthSid( PebblesSid ));
  457. AllowPebblesGetColor = (PVOID) TstAllocatePool ( PagedPool, AllowPebblesGetColorLength );
  458. AllowPebblesGetColor->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  459. AllowPebblesGetColor->Header.AceSize = AllowPebblesGetColorLength;
  460. AllowPebblesGetColor->Header.AceFlags = 0;
  461. AllowPebblesGetColor->Mask = SET_WIDGET_COLOR;
  462. RtlCopySid(
  463. SeLengthSid( PebblesSid ),
  464. &(AllowPebblesGetColor->SidStart),
  465. PebblesSid );
  466. //
  467. // Create some ACLs that we can put into a Security Descriptor
  468. //
  469. DbgBreakPoint();
  470. //
  471. // Dacl
  472. //
  473. // +----------------+ +----------------+ +----------------+
  474. // | 1st ACE | | 2nd ACE | | 3rd ACE |
  475. // +----------------+ +----------------+ +----------------+
  476. // | AccessAllowed | | AccessDenied | | AccessAllowed |
  477. // +----------------+ +----------------+ +----------------+
  478. // | BARNEY | | PEBBLES | | FRED |
  479. // +----------------+ +----------------+ +----------------+
  480. // | SetWidgeColor | | SetWidgeColor | | SetWidgeColor |
  481. // +----------------+ +----------------+ +----------------+
  482. //
  483. Dacl = (PACL) TstAllocatePool ( PagedPool, 2048 );
  484. RtlCreateAcl( Dacl, 2048, ACL_REVISION);
  485. RtlAddAce ( Dacl,
  486. ACL_REVISION,
  487. 0,
  488. AllowBarneySetColor,
  489. AllowBarneySetColorLength );
  490. RtlAddAce ( Dacl,
  491. ACL_REVISION,
  492. 1,
  493. DenyPebblesSetColor,
  494. DenyPebblesSetColorLength );
  495. RtlAddAce ( Dacl,
  496. ACL_REVISION,
  497. 2,
  498. DenyFredSetColor,
  499. AllowFredSetColorLength );
  500. DumpAcl (Dacl);
  501. // Create a security descriptor
  502. //
  503. // Owner = Pebbles
  504. // Group = Flintstone
  505. // Dacl = Dacl
  506. // Sacl = NULL
  507. //
  508. Widget1SecurityDescriptor =
  509. (PSECURITY_DESCRIPTOR)TstAllocatePool( PagedPool, 1024 );
  510. RtlCreateSecurityDescriptor( Widget1SecurityDescriptor,
  511. 1 );
  512. RtlSetOwnerSecurityDescriptor( Widget1SecurityDescriptor,
  513. PebblesSid,
  514. FALSE );
  515. RtlSetGroupSecurityDescriptor( Widget1SecurityDescriptor,
  516. FlintstoneSid,
  517. FALSE );
  518. RtlSetDaclSecurityDescriptor( Widget1SecurityDescriptor,
  519. TRUE,
  520. Dacl,
  521. FALSE );
  522. RtlSetSaclSecurityDescriptor( Widget1SecurityDescriptor,
  523. FALSE,
  524. NULL,
  525. NULL );
  526. // See if Pebbles is allowed SET_WIDGET_COLOR (should be denied)
  527. Status = NtAccessCheck( Widget1SecurityDescriptor,
  528. PrimaryToken,
  529. (ACCESS_MASK) SET_WIDGET_COLOR,
  530. &GrantedAccess,
  531. &AccessStatus );
  532. // DbgBreakPoint();
  533. ASSERT(NT_SUCCESS(Status));
  534. ASSERT(!NT_SUCCESS(AccessStatus));
  535. ASSERT(GrantedAccess == NULL);
  536. // Update Dacl to be the following:
  537. //
  538. // Dacl2
  539. //
  540. // +----------------+ +----------------+ +----------------+
  541. // | 1st ACE | | 2nd ACE | | 3rd ACE |
  542. // +----------------+ +----------------+ +----------------+
  543. // | AccessAllowed | | AccessAllowed | | AccessDenied |
  544. // +----------------+ +----------------+ +----------------+
  545. // | BARNEY | | PEBBLES | | FRED |
  546. // +----------------+ +----------------+ +----------------+
  547. // | SetWidgeColor | | SetWidgeColor | | SetWidgeColor |
  548. // +----------------+ +----------------+ +----------------+
  549. //
  550. // Delete 2nd Ace
  551. RtlDeleteAce (Dacl, 1);
  552. RtlAddAce ( Dacl,
  553. ACL_REVISION,
  554. 1,
  555. AllowPebblesSetColor,
  556. AllowPebblesSetColorLength );
  557. RtlDeleteAce ( Dacl, 2 );
  558. RtlAddAce ( Dacl,
  559. ACL_REVISION,
  560. 1,
  561. DenyFredSetColor,
  562. DenyFredSetColorLength );
  563. // Change the security descriptor to use updated Dacl
  564. //
  565. // Owner = Pebbles
  566. // Group = Flintstone
  567. // Dacl = Dacl2
  568. // Sacl = NULL
  569. //
  570. RtlSetDaclSecurityDescriptor( Widget1SecurityDescriptor,
  571. TRUE,
  572. Dacl,
  573. FALSE );
  574. // See if Pebbles is allowed SET_WIDGET_COLOR (should be permitted)
  575. Status = NtAccessCheck( Widget1SecurityDescriptor,
  576. PrimaryToken,
  577. (ACCESS_MASK) SET_WIDGET_COLOR,
  578. &GrantedAccess,
  579. &AccessStatus );
  580. ASSERT(NT_SUCCESS(Status));
  581. ASSERT(NT_SUCCESS(AccessStatus));
  582. ASSERT(GrantedAccess == (ACCESS_MASK)SET_WIDGET_COLOR);
  583. //
  584. // Dacl3
  585. //
  586. // +----------------+ +----------------+ +----------------+
  587. // | 1st ACE | | 2nd ACE | | 3rd ACE |
  588. // +----------------+ +----------------+ +----------------+
  589. // | AccessAllowed | | AccessAllowed | | AccessDenied |
  590. // +----------------+ +----------------+ +----------------+
  591. // | BARNEY | | PEBBLES | | FRED |
  592. // +----------------+ +----------------+ +----------------+
  593. // | SetWidgeColor | | SetWidgeColor | | SetWidgeColor |
  594. // +----------------+ +----------------+ +----------------+
  595. //
  596. // +----------------+ +----------------+
  597. // | 4th ACE | | 5th ACE |
  598. // +----------------+ +----------------+
  599. // | AccessAllowed | | AccessAllowed |
  600. // +----------------+ +----------------+
  601. // | BARNEY | | PEBBLES |
  602. // +----------------+ +----------------+
  603. // | SetWidgeSize | | SetWidgeSize |
  604. // +----------------+ +----------------+
  605. //
  606. RtlAddAce ( Dacl,
  607. ACL_REVISION,
  608. MAXULONG,
  609. AllowBarneySetSize,
  610. AllowBarneySetSizeLength );
  611. RtlAddAce ( Dacl,
  612. ACL_REVISION,
  613. MAXULONG,
  614. AllowPebblesSetSize,
  615. AllowPebblesSetSizeLength );
  616. // Change the security descriptor to use Dacl3
  617. //
  618. // Owner = Pebbles
  619. // Group = Flintstone
  620. // Dacl = Dacl3
  621. // Sacl = NULL
  622. //
  623. RtlSetDaclSecurityDescriptor( Widget1SecurityDescriptor,
  624. TRUE,
  625. Dacl,
  626. FALSE );
  627. // Request MAXIMUM_ACCESS for Pebbles. Should get back SetWidgetSize
  628. // and SetWidgetColor
  629. Status = NtAccessCheck( Widget1SecurityDescriptor,
  630. PrimaryToken,
  631. (ACCESS_MASK) MAXIMUM_ALLOWED,
  632. &GrantedAccess,
  633. &AccessStatus );
  634. ASSERT(NT_SUCCESS(Status));
  635. ASSERT(NT_SUCCESS(AccessStatus));
  636. ASSERT(GrantedAccess == (ACCESS_MASK) (SET_WIDGET_COLOR | SET_WIDGET_SIZE));
  637. //
  638. // Dacl4
  639. //
  640. // +----------------+ +----------------+ +----------------+
  641. // | 1st ACE | | 2nd ACE | | 3rd ACE |
  642. // +----------------+ +----------------+ +----------------+
  643. // | AccessAllowed | | AccessAllowed | | AccessDenied |
  644. // +----------------+ +----------------+ +----------------+
  645. // | BARNEY | | PEBBLES | | FRED |
  646. // +----------------+ +----------------+ +----------------+
  647. // | SetWidgeColor | | SetWidgeColor | | SetWidgeColor |
  648. // +----------------+ +----------------+ +----------------+
  649. //
  650. // +----------------+ +----------------+ +----------------+
  651. // | 4th ACE | | 5th ACE | | 6th ACE |
  652. // +----------------+ +----------------+ +----------------+
  653. // | AccessAllowed | | AccessAllowed | | AccessDenied |
  654. // +----------------+ +----------------+ +----------------+
  655. // | BARNEY | | PEBBLES | | PEBBLES |
  656. // +----------------+ +----------------+ +----------------+
  657. // | SetWidgeSize | | SetWidgeSize | | SetWidgeColor |
  658. // +----------------+ +----------------+ +----------------+
  659. //
  660. RtlAddAce ( Dacl,
  661. ACL_REVISION,
  662. MAXULONG,
  663. DenyPebblesSetColor,
  664. DenyPebblesSetColorLength );
  665. RtlSetDaclSecurityDescriptor( Widget1SecurityDescriptor,
  666. TRUE,
  667. Dacl,
  668. FALSE );
  669. // Request MAXIMUM_ACCESS for Pebbles. Should get back SetWidgetSize
  670. // and SetWidgetColor
  671. Status = NtAccessCheck( Widget1SecurityDescriptor,
  672. PrimaryToken,
  673. (ACCESS_MASK) MAXIMUM_ALLOWED,
  674. &GrantedAccess,
  675. &AccessStatus );
  676. ASSERT(NT_SUCCESS(Status));
  677. ASSERT(NT_SUCCESS(AccessStatus));
  678. ASSERT(GrantedAccess == (ACCESS_MASK) (SET_WIDGET_COLOR | SET_WIDGET_SIZE));
  679. //
  680. // Dacl5
  681. //
  682. // +----------------+ +----------------+ +----------------+
  683. // | 1st ACE | | 2nd ACE | | 3rd ACE |
  684. // +----------------+ +----------------+ +----------------+
  685. // | AccessAllowed | | AccessDenied | | AccessDenied |
  686. // +----------------+ +----------------+ +----------------+
  687. // | BARNEY | | PEBBLES | | FRED |
  688. // +----------------+ +----------------+ +----------------+
  689. // | SetWidgeColor | | SetWidgeColor | | SetWidgeColor |
  690. // +----------------+ +----------------+ +----------------+
  691. //
  692. // +----------------+ +----------------+ +----------------+
  693. // | 4th ACE | | 5th ACE | | 6th ACE |
  694. // +----------------+ +----------------+ +----------------+
  695. // | AccessAllowed | | AccessAllowed | | AccessAllowed |
  696. // +----------------+ +----------------+ +----------------+
  697. // | BARNEY | | PEBBLES | | PEBBLES |
  698. // +----------------+ +----------------+ +----------------+
  699. // | SetWidgeSize | | SetWidgeSize | | SetWidgeColor |
  700. // +----------------+ +----------------+ +----------------+
  701. //
  702. RtlDeleteAce (Dacl, 1);
  703. RtlAddAce ( Dacl,
  704. ACL_REVISION,
  705. 1,
  706. DenyPebblesSetColor,
  707. DenyPebblesSetColorLength );
  708. RtlDeleteAce (Dacl, 5);
  709. RtlAddAce ( Dacl,
  710. ACL_REVISION,
  711. MAXULONG,
  712. AllowPebblesSetColor,
  713. AllowPebblesSetColorLength );
  714. DumpAcl ( Dacl );
  715. RtlSetDaclSecurityDescriptor( Widget1SecurityDescriptor,
  716. TRUE,
  717. Dacl,
  718. FALSE );
  719. // Request MAXIMUM_ACCESS for Pebbles. Should get back SetWidgetSize
  720. Status = NtAccessCheck( Widget1SecurityDescriptor,
  721. PrimaryToken,
  722. (ACCESS_MASK) MAXIMUM_ALLOWED,
  723. &GrantedAccess,
  724. &AccessStatus );
  725. ASSERT(NT_SUCCESS(Status));
  726. ASSERT(NT_SUCCESS(AccessStatus));
  727. ASSERT(GrantedAccess == (ACCESS_MASK) SET_WIDGET_SIZE);
  728. //
  729. // Dacl6
  730. //
  731. // +----------------+ +----------------+ +----------------+
  732. // | 1st ACE | | 2nd ACE | | 3rd ACE |
  733. // +----------------+ +----------------+ +----------------+
  734. // | AccessAllowed | | AccessDenied | | AccessDenied |
  735. // +----------------+ +----------------+ +----------------+
  736. // | BARNEY | | PEBBLES | | FRED |
  737. // +----------------+ +----------------+ +----------------+
  738. // | SetWidgeColor | | SetWidgeColor | | SetWidgeColor |
  739. // +----------------+ +----------------+ +----------------+
  740. //
  741. // +----------------+ +----------------+ +----------------+
  742. // | 4th ACE | | 5th ACE | | 6th ACE |
  743. // +----------------+ +----------------+ +----------------+
  744. // | AccessAllowed | | AccessAllowed | | AccessAllowed |
  745. // +----------------+ +----------------+ +----------------+
  746. // | BARNEY | | PEBBLES | | PEBBLES |
  747. // +----------------+ +----------------+ +----------------+
  748. // | SetWidgeSize | | SetWidgeSize | | SetWidgeColor |
  749. // +----------------+ +----------------+ +----------------+
  750. //
  751. // +----------------+ +----------------+
  752. // | 7th ACE | | 8th ACE |
  753. // +----------------+ +----------------+
  754. // | AccessAllowed | | AccessAllowed |
  755. // +----------------+ +----------------+
  756. // | PEBBLES | | PEBBLES |
  757. // +----------------+ +----------------+
  758. // | GetWidgeSize | | GetWidgeColor |
  759. // +----------------+ +----------------+
  760. //
  761. RtlAddAce ( Dacl,
  762. ACL_REVISION,
  763. MAXULONG,
  764. AllowPebblesGetSize,
  765. AllowPebblesGetSizeLength );
  766. RtlAddAce ( Dacl,
  767. ACL_REVISION,
  768. MAXULONG,
  769. AllowPebblesGetColor,
  770. AllowPebblesGetColorLength );
  771. DumpAcl ( Dacl );
  772. RtlSetDaclSecurityDescriptor( Widget1SecurityDescriptor,
  773. TRUE,
  774. Dacl,
  775. FALSE );
  776. // Request MAXIMUM_ACCESS for Pebbles. Should get back SetWidgetSize
  777. Status = NtAccessCheck( Widget1SecurityDescriptor,
  778. PrimaryToken,
  779. (ACCESS_MASK) MAXIMUM_ALLOWED,
  780. &GrantedAccess,
  781. &AccessStatus );
  782. ASSERT(NT_SUCCESS(Status));
  783. ASSERT(NT_SUCCESS(AccessStatus));
  784. ASSERT(GrantedAccess == (ACCESS_MASK) SET_WIDGET_SIZE);
  785. return(TRUE);
  786. }
  787. /////////////////////////////////////////////////////////////////////
  788. // //
  789. // //
  790. // Main test entry point //
  791. // //
  792. // //
  793. /////////////////////////////////////////////////////////////////////
  794. BOOLEAN
  795. CTAccess()
  796. {
  797. BOOLEAN Result = TRUE;
  798. if (!TSeVariableInitialization()) {
  799. DbgPrint("Se: Failed to initialize global test variables.\n");
  800. return FALSE;
  801. }
  802. DbgPrint("Se: Initialization...");
  803. TestTokenInitialize();
  804. CreateDAclToken();
  805. }
  806. //
  807. // Debug support routine
  808. //
  809. typedef struct _STANDARD_ACE {
  810. ACE_HEADER Header;
  811. ACCESS_MASK Mask;
  812. PSID Sid;
  813. } STANDARD_ACE;
  814. typedef STANDARD_ACE *PSTANDARD_ACE;
  815. VOID
  816. DumpAcl (
  817. IN PACL Acl
  818. )
  819. /*++
  820. Routine Description:
  821. This routine dumps via (DbgPrint) an Acl for debug purposes. It is
  822. specialized to dump standard aces.
  823. Arguments:
  824. Acl - Supplies the Acl to dump
  825. Return Value:
  826. None
  827. --*/
  828. {
  829. ULONG i;
  830. PSTANDARD_ACE Ace;
  831. DbgPrint("DumpAcl @ %8lx", Acl);
  832. //
  833. // Check if the Acl is null
  834. //
  835. if (Acl == NULL) {
  836. return;
  837. }
  838. //
  839. // Dump the Acl header
  840. //
  841. DbgPrint(" Revision: %02x", Acl->AclRevision);
  842. DbgPrint(" Size: %04x", Acl->AclSize);
  843. DbgPrint(" AceCount: %04x\n", Acl->AceCount);
  844. //
  845. // Now for each Ace we want do dump it
  846. //
  847. for (i = 0, Ace = FirstAce(Acl);
  848. i < Acl->AceCount;
  849. i++, Ace = NextAce(Ace) ) {
  850. //
  851. // print out the ace header
  852. //
  853. DbgPrint(" AceHeader: %08lx ", *(PULONG)Ace);
  854. //
  855. // special case on the standard ace types
  856. //
  857. if ((Ace->Header.AceType == ACCESS_ALLOWED_ACE_TYPE) ||
  858. (Ace->Header.AceType == ACCESS_DENIED_ACE_TYPE) ||
  859. (Ace->Header.AceType == SYSTEM_AUDIT_ACE_TYPE) ||
  860. (Ace->Header.AceType == SYSTEM_ALARM_ACE_TYPE)) {
  861. //
  862. // The following array is indexed by ace types and must
  863. // follow the allowed, denied, audit, alarm seqeuence
  864. //
  865. static PCHAR AceTypes[] = { "Access Allowed",
  866. "Access Denied ",
  867. "System Audit ",
  868. "System Alarm "
  869. };
  870. DbgPrint(AceTypes[Ace->Header.AceType]);
  871. DbgPrint("\nAccess Mask: %08lx ", Ace->Mask);
  872. } else {
  873. DbgPrint("Unknown Ace Type\n");
  874. }
  875. DbgPrint("\n");
  876. DbgPrint("AceSize = %d\n",Ace->Header.AceSize);
  877. DbgPrint("Ace Flags = ");
  878. if (Ace->Header.AceFlags & OBJECT_INHERIT_ACE) {
  879. DbgPrint("OBJECT_INHERIT_ACE\n");
  880. DbgPrint(" ");
  881. }
  882. if (Ace->Header.AceFlags & CONTAINER_INHERIT_ACE) {
  883. DbgPrint("CONTAINER_INHERIT_ACE\n");
  884. DbgPrint(" ");
  885. }
  886. if (Ace->Header.AceFlags & NO_PROPAGATE_INHERIT_ACE) {
  887. DbgPrint("NO_PROPAGATE_INHERIT_ACE\n");
  888. DbgPrint(" ");
  889. }
  890. if (Ace->Header.AceFlags & INHERIT_ONLY_ACE) {
  891. DbgPrint("INHERIT_ONLY_ACE\n");
  892. DbgPrint(" ");
  893. }
  894. if (Ace->Header.AceFlags & SUCCESSFUL_ACCESS_ACE_FLAG) {
  895. DbgPrint("SUCCESSFUL_ACCESS_ACE_FLAG\n");
  896. DbgPrint(" ");
  897. }
  898. if (Ace->Header.AceFlags & FAILED_ACCESS_ACE_FLAG) {
  899. DbgPrint("FAILED_ACCESS_ACE_FLAG\n");
  900. DbgPrint(" ");
  901. }
  902. DbgPrint("\n");
  903. }
  904. }
  905.