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.

578 lines
14 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. tseum.c
  5. Abstract:
  6. Test program for the security system
  7. Author:
  8. Gary Kimura [GaryKi] 20-Nov-1989
  9. Revision History:
  10. v3: robertre
  11. Updated ACL_REVISION
  12. --*/
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <nt.h>
  16. #include <ntrtl.h>
  17. #include "sep.h"
  18. #include "ttoken.c"
  19. VOID
  20. RtlDumpAcl(
  21. IN PACL Acl
  22. );
  23. main(
  24. int argc,
  25. char *argv[],
  26. char *envp[]
  27. )
  28. {
  29. HANDLE CurrentProcessHandle;
  30. NTSTATUS Status;
  31. ULONG i;
  32. VOID SeMain();
  33. CurrentProcessHandle = NtCurrentProcess();
  34. Status = STATUS_SUCCESS;
  35. DbgPrint( "Entering User Mode Test Program\n" );
  36. DbgPrint( "argc: %ld\n", argc );
  37. if (argv != NULL) {
  38. for (i=0; i<argc; i++) {
  39. DbgPrint( "argv[ %ld ]: %s\n", i, argv[ i ] );
  40. }
  41. }
  42. if (envp != NULL) {
  43. i = 0;
  44. while (*envp) {
  45. DbgPrint( "envp[ %02ld ]: %s\n", i++, *envp++ );
  46. }
  47. }
  48. SeMain();
  49. DbgPrint( "Exiting User Mode Test Program with Status = %lx\n", Status );
  50. NtTerminateProcess( CurrentProcessHandle, Status );
  51. }
  52. VOID
  53. SeMain(
  54. )
  55. {
  56. BOOLEAN TestCreateAcl();
  57. BOOLEAN TestQueryInformationAcl();
  58. BOOLEAN TestSetInformationAcl();
  59. BOOLEAN TestAddAce();
  60. BOOLEAN TestDeleteAce();
  61. BOOLEAN TestGetAce();
  62. BOOLEAN TestAccessCheck();
  63. BOOLEAN TestGenerateMessage();
  64. DbgPrint("Starting User Mode Security Test\n");
  65. if (!TestCreateAcl()) {
  66. DbgPrint("TestCreateAcl Error\n");
  67. return;
  68. }
  69. if (!TestQueryInformationAcl()) {
  70. DbgPrint("TestCreateAcl Error\n");
  71. return;
  72. }
  73. if (!TestSetInformationAcl()) {
  74. DbgPrint("TestCreateAcl Error\n");
  75. return;
  76. }
  77. if (!TestAddAce()) {
  78. DbgPrint("TestCreateAcl Error\n");
  79. return;
  80. }
  81. if (!TestDeleteAce()) {
  82. DbgPrint("TestCreateAcl Error\n");
  83. return;
  84. }
  85. if (!TestGetAce()) {
  86. DbgPrint("TestCreateAcl Error\n");
  87. return;
  88. }
  89. if (!TestAccessCheck()) {
  90. DbgPrint("TestCreateAcl Error\n");
  91. return;
  92. }
  93. if (!TestGenerateMessage()) {
  94. DbgPrint("TestCreateAcl Error\n");
  95. return;
  96. }
  97. DbgPrint("Ending User Mode Security Test\n");
  98. return;
  99. }
  100. BOOLEAN
  101. TestCreateAcl()
  102. {
  103. UCHAR Buffer[512];
  104. PACL Acl;
  105. NTSTATUS Status;
  106. Acl = (PACL)Buffer;
  107. //
  108. // Create a good large acl
  109. //
  110. if (!NT_SUCCESS(Status = RtlCreateAcl( Acl, 512, 1))) {
  111. DbgPrint("RtlCreateAcl Error large Acl : %8lx\n", Status);
  112. return FALSE;
  113. }
  114. //
  115. // Create a good small acl
  116. //
  117. if (!NT_SUCCESS(Status = RtlCreateAcl( Acl, sizeof(ACL), 1))) {
  118. DbgPrint("RtlCreateAcl Error small Acl : %8lx\n", Status);
  119. return FALSE;
  120. }
  121. //
  122. // Create a too small acl
  123. //
  124. if (NT_SUCCESS(Status = RtlCreateAcl( Acl, sizeof(ACL) - 1, 1))) {
  125. DbgPrint("RtlCreateAcl Error too small Acl : %8lx\n", Status);
  126. return FALSE;
  127. }
  128. //
  129. // Create a bad version acl
  130. //
  131. if (NT_SUCCESS(Status = RtlCreateAcl( Acl, 512, 2))) {
  132. DbgPrint("RtlCreateAcl Error bad version : %8lx\n", Status);
  133. return FALSE;
  134. }
  135. return TRUE;
  136. }
  137. BOOLEAN
  138. TestQueryInformationAcl()
  139. {
  140. UCHAR Buffer[512];
  141. PACL Acl;
  142. ACL_REVISION_INFORMATION AclRevisionInfo;
  143. ACL_SIZE_INFORMATION AclSizeInfo;
  144. NTSTATUS Status;
  145. Acl = (PACL)Buffer;
  146. BuildAcl( Fred, Acl, 512 );
  147. //
  148. // Query the revision information
  149. //
  150. if (!NT_SUCCESS(Status = RtlQueryInformationAcl( Acl,
  151. (PVOID)&AclRevisionInfo,
  152. sizeof(ACL_REVISION_INFORMATION),
  153. AclRevisionInformation))) {
  154. DbgPrint("RtlQueryInformationAcl revision info error : %8lx\n", Status);
  155. return FALSE;
  156. }
  157. if (AclRevisionInfo.AclRevision != ACL_REVISION) {
  158. DbgPrint("RtlAclRevision Error\n");
  159. return FALSE;
  160. }
  161. //
  162. // Query size information
  163. //
  164. if (!NT_SUCCESS(Status = RtlQueryInformationAcl( Acl,
  165. (PVOID)&AclSizeInfo,
  166. sizeof(ACL_SIZE_INFORMATION),
  167. AclSizeInformation))) {
  168. DbgPrint("RtlQueryInformationAcl size info Error : %8lx\n", Status);
  169. return FALSE;
  170. }
  171. if ((AclSizeInfo.AceCount != 6) ||
  172. (AclSizeInfo.AclBytesInUse != (sizeof(ACL)+6*sizeof(STANDARD_ACE))) ||
  173. (AclSizeInfo.AclBytesFree != 512 - AclSizeInfo.AclBytesInUse)) {
  174. DbgPrint("RtlAclSize Error\n");
  175. DbgPrint("AclSizeInfo.AceCount = %8lx\n", AclSizeInfo.AceCount);
  176. DbgPrint("AclSizeInfo.AclBytesInUse = %8lx\n", AclSizeInfo.AclBytesInUse);
  177. DbgPrint("AclSizeInfo.AclBytesFree = %8lx\n", AclSizeInfo.AclBytesFree);
  178. return FALSE;
  179. }
  180. return TRUE;
  181. }
  182. BOOLEAN
  183. TestSetInformationAcl()
  184. {
  185. UCHAR Buffer[512];
  186. PACL Acl;
  187. ACL_REVISION_INFORMATION AclRevisionInfo;
  188. NTSTATUS Status;
  189. Acl = (PACL)Buffer;
  190. BuildAcl( Fred, Acl, 512 );
  191. //
  192. // Set the revision information to the current revision level
  193. //
  194. AclRevisionInfo.AclRevision = ACL_REVISION;
  195. if (!NT_SUCCESS(Status = RtlSetInformationAcl( Acl,
  196. (PVOID)&AclRevisionInfo,
  197. sizeof(ACL_REVISION_INFORMATION),
  198. AclRevisionInformation))) {
  199. DbgPrint("RtlSetInformationAcl revision info error : %8lx\n", Status);
  200. return FALSE;
  201. }
  202. //
  203. // Set the revision information to something wrong
  204. //
  205. AclRevisionInfo.AclRevision = ACL_REVISION+1;
  206. if (NT_SUCCESS(Status = RtlSetInformationAcl( Acl,
  207. (PVOID)&AclRevisionInfo,
  208. sizeof(ACL_REVISION_INFORMATION),
  209. AclRevisionInformation))) {
  210. DbgPrint("RtlSetInformationAcl revision to wrong info error : %8lx\n", Status);
  211. return FALSE;
  212. }
  213. return TRUE;
  214. }
  215. BOOLEAN
  216. TestAddAce()
  217. {
  218. UCHAR AclBuffer[512];
  219. PACL Acl;
  220. STANDARD_ACE AceList[2];
  221. NTSTATUS Status;
  222. Acl = (PACL)AclBuffer;
  223. //
  224. // Create a good large acl
  225. //
  226. if (!NT_SUCCESS(Status = RtlCreateAcl( Acl, 512, 1))) {
  227. DbgPrint("RtlCreateAcl Error large Acl : %8lx\n", Status);
  228. return FALSE;
  229. }
  230. //
  231. // test add ace to add two aces to an empty acl
  232. //
  233. AceList[0].Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  234. AceList[0].Header.AceSize = sizeof(STANDARD_ACE);
  235. AceList[0].Header.InheritFlags = 0;
  236. AceList[0].Header.AceFlags = 0;
  237. AceList[0].Mask = 0x22222222;
  238. CopyGuid(&AceList[0].Guid, &FredGuid);
  239. AceList[1].Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  240. AceList[1].Header.AceSize = sizeof(STANDARD_ACE);
  241. AceList[1].Header.InheritFlags = 0;
  242. AceList[1].Header.AceFlags = 0;
  243. AceList[1].Mask = 0x44444444;
  244. CopyGuid(&AceList[1].Guid, &WilmaGuid);
  245. if (!NT_SUCCESS(Status = RtlAddAce( Acl,
  246. 1,
  247. 0,
  248. AceList,
  249. 2*sizeof(STANDARD_ACE)))) {
  250. DbgPrint("RtlAddAce to empty acl Error : %8lx\n", Status);
  251. return FALSE;
  252. }
  253. // RtlDumpAcl(Acl);
  254. //
  255. // test add ace to add one to the beginning of an acl
  256. //
  257. AceList[0].Header.AceType = SYSTEM_AUDIT_ACE_TYPE;
  258. AceList[0].Header.AceSize = sizeof(STANDARD_ACE);
  259. AceList[0].Header.InheritFlags = 0;
  260. AceList[0].Header.AceFlags = 0;
  261. AceList[0].Mask = 0x11111111;
  262. CopyGuid(&AceList[0].Guid, &PebblesGuid);
  263. if (!NT_SUCCESS(Status = RtlAddAce( Acl,
  264. 1,
  265. 0,
  266. AceList,
  267. sizeof(STANDARD_ACE)))) {
  268. DbgPrint("RtlAddAce to beginning of acl Error : %8lx\n", Status);
  269. return FALSE;
  270. }
  271. // RtlDumpAcl(Acl);
  272. //
  273. // test add ace to add one to the middle of an acl
  274. //
  275. AceList[0].Header.AceType = ACCESS_DENIED_ACE_TYPE;
  276. AceList[0].Header.AceSize = sizeof(STANDARD_ACE);
  277. AceList[0].Header.InheritFlags = 0;
  278. AceList[0].Header.AceFlags = 0;
  279. AceList[0].Mask = 0x33333333;
  280. CopyGuid(&AceList[0].Guid, &DinoGuid);
  281. if (!NT_SUCCESS(Status = RtlAddAce( Acl,
  282. 1,
  283. 2,
  284. AceList,
  285. sizeof(STANDARD_ACE)))) {
  286. DbgPrint("RtlAddAce to middle of acl Error : %8lx\n", Status);
  287. return FALSE;
  288. }
  289. // RtlDumpAcl(Acl);
  290. //
  291. // test add ace to add one to the end of an acl
  292. //
  293. AceList[0].Header.AceType = ACCESS_DENIED_ACE_TYPE;
  294. AceList[0].Header.AceSize = sizeof(STANDARD_ACE);
  295. AceList[0].Header.InheritFlags = 0;
  296. AceList[0].Header.AceFlags = 0;
  297. AceList[0].Mask = 0x55555555;
  298. CopyGuid(&AceList[0].Guid, &FlintstoneGuid);
  299. if (!NT_SUCCESS(Status = RtlAddAce( Acl,
  300. 1,
  301. MAXULONG,
  302. AceList,
  303. sizeof(STANDARD_ACE)))) {
  304. DbgPrint("RtlAddAce to end of an acl Error : %8lx\n", Status);
  305. return FALSE;
  306. }
  307. // RtlDumpAcl(Acl);
  308. return TRUE;
  309. }
  310. BOOLEAN
  311. TestDeleteAce()
  312. {
  313. UCHAR Buffer[512];
  314. PACL Acl;
  315. NTSTATUS Status;
  316. Acl = (PACL)Buffer;
  317. BuildAcl( Fred, Acl, 512 );
  318. //
  319. // test delete first ace
  320. //
  321. if (!NT_SUCCESS(Status = RtlDeleteAce(Acl, 0))) {
  322. DbgPrint("RtlDeleteAce first ace Error : %8lx\n", Status);
  323. return FALSE;
  324. }
  325. // RtlDumpAcl(Acl);
  326. //
  327. // test delete middle ace
  328. //
  329. if (!NT_SUCCESS(Status = RtlDeleteAce(Acl, 2))) {
  330. DbgPrint("RtlDeleteAce middle ace Error : %8lx\n", Status);
  331. return FALSE;
  332. }
  333. // RtlDumpAcl(Acl);
  334. //
  335. // test delete last ace
  336. //
  337. if (!NT_SUCCESS(Status = RtlDeleteAce(Acl, 3))) {
  338. DbgPrint("RtlDeleteAce last ace Error : %8lx\n", Status);
  339. return FALSE;
  340. }
  341. // RtlDumpAcl(Acl);
  342. return TRUE;
  343. }
  344. BOOLEAN
  345. TestGetAce()
  346. {
  347. UCHAR Buffer[512];
  348. PACL Acl;
  349. STANDARD_ACE Ace;
  350. NTSTATUS Status;
  351. Acl = (PACL)Buffer;
  352. BuildAcl( Fred, Acl, 512 );
  353. //
  354. // Get first ace
  355. //
  356. if (!NT_SUCCESS(Status = RtlGetAce(Acl, 0, (PVOID)&Ace))) {
  357. DbgPrint("RtlGetAce first ace error : %8lx\n", Status);
  358. return FALSE;
  359. }
  360. // RtlDumpAcl(Acl);
  361. //
  362. // Get middle ace
  363. //
  364. if (!NT_SUCCESS(Status = RtlGetAce(Acl, 3, (PVOID)&Ace))) {
  365. DbgPrint("RtlGetAce middle ace error : %8lx\n", Status);
  366. return FALSE;
  367. }
  368. // RtlDumpAcl(Acl);
  369. //
  370. // Get last ace
  371. //
  372. if (!NT_SUCCESS(Status = RtlGetAce(Acl, 5, (PVOID)&Ace))) {
  373. DbgPrint("RtlGetAce last ace error : %8lx\n", Status);
  374. return FALSE;
  375. }
  376. // RtlDumpAcl(Acl);
  377. return TRUE;
  378. }
  379. BOOLEAN
  380. TestAccessCheck()
  381. {
  382. UCHAR AclBuffer[1024];
  383. SECURITY_DESCRIPTOR SecurityDescriptor;
  384. PACL Acl;
  385. UCHAR TokenBuffer[512];
  386. PACCESS_TOKEN Token;
  387. NTSTATUS Status;
  388. Acl = (PACL)AclBuffer;
  389. BuildAcl( Fred, Acl, 1024 );
  390. Token = (PACCESS_TOKEN)TokenBuffer;
  391. BuildToken( Fred, Token );
  392. DiscretionarySecurityDescriptor( &SecurityDescriptor, Acl );
  393. //
  394. // Test should be successful based on aces
  395. //
  396. if (!NT_SUCCESS(Status = NtAccessCheck( &SecurityDescriptor,
  397. Token,
  398. 0x00000001,
  399. NULL ))) {
  400. DbgPrint("NtAccessCheck Error should allow access : %8lx\n", Status);
  401. return FALSE;
  402. }
  403. //
  404. // Test should be successful based on owner
  405. //
  406. if (!NT_SUCCESS(Status = NtAccessCheck( &SecurityDescriptor,
  407. Token,
  408. READ_CONTROL,
  409. &FredGuid ))) {
  410. DbgPrint("NtAccessCheck Error should allow owner : %8lx\n", Status);
  411. return FALSE;
  412. }
  413. //
  414. // Test should be unsuccessful based on aces
  415. //
  416. if (NT_SUCCESS(Status = NtAccessCheck( &SecurityDescriptor,
  417. Token,
  418. 0x0000000f,
  419. &FredGuid ))) {
  420. DbgPrint("NtAccessCheck Error shouldn't allow access : %8lx\n", Status);
  421. return FALSE;
  422. }
  423. //
  424. // Test should be unsuccessful based on non owner
  425. //
  426. if (NT_SUCCESS(Status = NtAccessCheck( &SecurityDescriptor,
  427. Token,
  428. READ_CONTROL,
  429. &BarneyGuid ))) {
  430. DbgPrint("NtAccessCheck Error shouldn't allow non owner access : %8lx\n", Status);
  431. return FALSE;
  432. }
  433. return TRUE;
  434. }
  435. BOOLEAN
  436. TestGenerateMessage()
  437. {
  438. return TRUE;
  439. }