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.

579 lines
16 KiB

  1. //////////////////////////////////////////////////////////////////////////
  2. // WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING //
  3. // //
  4. // This test file is not current with the security implementation. //
  5. // This file contains references to data types and APIs that do not //
  6. // exist. //
  7. // //
  8. //////////////////////////////////////////////////////////////////////////
  9. /*++
  10. Copyright (c) 1989 Microsoft Corporation
  11. Module Name:
  12. tse.c
  13. Abstract:
  14. Test program for the SE subcomponent of the NTOS project
  15. Author:
  16. Gary Kimura (garyki) 20-Nov-1989
  17. Revision History:
  18. --*/
  19. #include <stdio.h>
  20. #include "sep.h"
  21. #include <zwapi.h>
  22. BOOLEAN SeTest();
  23. #include "ttoken.c"
  24. int
  25. main(
  26. int argc,
  27. char *argv[]
  28. )
  29. {
  30. VOID KiSystemStartup();
  31. TestFunction = SeTest;
  32. KiSystemStartup();
  33. return( 0 );
  34. }
  35. BOOLEAN
  36. SeTest()
  37. {
  38. BOOLEAN TestMakeSystemToken();
  39. BOOLEAN TestTokenCopy();
  40. BOOLEAN TestTokenSize();
  41. BOOLEAN TestDefaultObjectMethod();
  42. BOOLEAN TestCaptureSecurityDescriptor();
  43. BOOLEAN TestAssignSecurity();
  44. BOOLEAN TestAccessCheck();
  45. BOOLEAN TestGenerateMessage();
  46. DbgPrint("Start SeTest()\n");
  47. if (!TestMakeSystemToken()) {
  48. DbgPrint("TestMakeSystemToken() Error\n");
  49. return FALSE;
  50. }
  51. if (!TestTokenCopy()) {
  52. DbgPrint("TestTokenCopy() Error\n");
  53. return FALSE;
  54. }
  55. if (!TestTokenSize()) {
  56. DbgPrint("TestTokenSize() Error\n");
  57. return FALSE;
  58. }
  59. if (!TestDefaultObjectMethod()) {
  60. DbgPrint("TestDefaultObjectMethod() Error\n");
  61. return FALSE;
  62. }
  63. if (!TestCaptureSecurityDescriptor()) {
  64. DbgPrint("TestCaptureSecurityDescriptor() Error\n");
  65. return FALSE;
  66. }
  67. if (!TestAssignSecurity()) {
  68. DbgPrint("TestAssignSecurity() Error\n");
  69. return FALSE;
  70. }
  71. if (!TestAccessCheck()) {
  72. DbgPrint("TestAccessCheck() Error\n");
  73. return FALSE;
  74. }
  75. if (!TestGenerateMessage()) {
  76. DbgPrint("TestGenerateMessage() Error\n");
  77. return FALSE;
  78. }
  79. DbgPrint("End SeTest()\n");
  80. return TRUE;
  81. }
  82. BOOLEAN
  83. TestMakeSystemToken()
  84. {
  85. PACCESS_TOKEN Token;
  86. //
  87. // Make the system token
  88. //
  89. Token = (PACCESS_TOKEN)SeMakeSystemToken( PagedPool );
  90. //
  91. // and check its contents
  92. //
  93. if (!SepIsSystemToken( Token, ((ACCESS_TOKEN *)Token)->Size )) {
  94. DbgPrint("SepIsSystemToken Error\n");
  95. return FALSE;
  96. }
  97. ExFreePool( Token );
  98. return TRUE;
  99. }
  100. BOOLEAN
  101. TestTokenCopy()
  102. {
  103. PACCESS_TOKEN InToken;
  104. PACCESS_TOKEN OutToken;
  105. NTSTATUS Status;
  106. ULONG i;
  107. //
  108. // Allocate Buffers, and build a token
  109. //
  110. InToken = (PACCESS_TOKEN)ExAllocatePool(PagedPool, 512);
  111. OutToken = (PACCESS_TOKEN)ExAllocatePool(PagedPool, 512);
  112. BuildToken( Fred, InToken );
  113. //
  114. // Make a copy of the token
  115. //
  116. if (!NT_SUCCESS(Status = SeTokenCopy( InToken, OutToken, 512))) {
  117. DbgPrint("SeTokenCopy Error: %8lx\n", Status);
  118. return FALSE;
  119. }
  120. //
  121. // check both tokens for equality
  122. //
  123. for (i = 0; i < ((ACCESS_TOKEN *)InToken)->Size; i += 1) {
  124. if (*((PUCHAR)InToken + 1) != *((PUCHAR)OutToken + 1)) {
  125. DbgPrint("Token copy error\n");
  126. return FALSE;
  127. }
  128. }
  129. ExFreePool( InToken );
  130. ExFreePool( OutToken );
  131. return TRUE;
  132. }
  133. BOOLEAN
  134. TestTokenSize()
  135. {
  136. PACCESS_TOKEN Token;
  137. //
  138. // Allocate and build a token
  139. //
  140. Token = (PACCESS_TOKEN)ExAllocatePool(PagedPool, 512);
  141. BuildToken( Wilma, Token );
  142. //
  143. // Get the token size
  144. //
  145. if (SeTokenSize(Token) != ((ACCESS_TOKEN *)Token)->Size) {
  146. DbgPrint("SeTokenSize error\n");
  147. return FALSE;
  148. }
  149. ExFreePool( Token );
  150. return TRUE;
  151. }
  152. BOOLEAN
  153. TestDefaultObjectMethod()
  154. {
  155. SECURITY_DESCRIPTOR SecurityDescriptor;
  156. PSECURITY_DESCRIPTOR Buffer;
  157. PACL Acl;
  158. NTSTATUS Status;
  159. PSECURITY_DESCRIPTOR ObjectsSecurityDescriptor;
  160. ULONG Length;
  161. Acl = (PACL)ExAllocatePool( PagedPool, 1024 );
  162. Buffer = (PSECURITY_DESCRIPTOR)ExAllocatePool( PagedPool, 1024 );
  163. BuildAcl( Fred, Acl, 1024 );
  164. DiscretionarySecurityDescriptor( &SecurityDescriptor, Acl );
  165. ObjectsSecurityDescriptor = NULL;
  166. //
  167. // Set the descriptor
  168. //
  169. if (!NT_SUCCESS(Status = SeDefaultObjectMethod( NULL,
  170. SetSecurityDescriptor,
  171. &SecurityDescriptor,
  172. 0,
  173. NULL,
  174. &ObjectsSecurityDescriptor,
  175. PagedPool ))) {
  176. DbgPrint("SeDefaultObjectMethod setting error: %8lx\n", Status );
  177. return FALSE;
  178. }
  179. //
  180. // Query the descriptor
  181. //
  182. if (!NT_SUCCESS(Status = SeDefaultObjectMethod( NULL,
  183. QuerySecurityDescriptor,
  184. Buffer,
  185. AllAclInformation,
  186. &Length,
  187. &ObjectsSecurityDescriptor,
  188. PagedPool ))) {
  189. DbgPrint("SeDefaultObjectMethod reading error: %8lx\n", Status );
  190. return FALSE;
  191. }
  192. //
  193. // Replace the descriptor
  194. //
  195. BuildAcl( Wilma, Acl, 1024 );
  196. if (!NT_SUCCESS(Status = SeDefaultObjectMethod( NULL,
  197. SetSecurityDescriptor,
  198. &SecurityDescriptor,
  199. AllAclInformation,
  200. &Length,
  201. &ObjectsSecurityDescriptor,
  202. PagedPool ))) {
  203. DbgPrint("SeDefaultObjectMethod reading error: %8lx\n", Status );
  204. return FALSE;
  205. }
  206. //
  207. // Delete the descriptor
  208. //
  209. if (!NT_SUCCESS(Status = SeDefaultObjectMethod( NULL,
  210. DeleteSecurityDescriptor,
  211. NULL,
  212. 0,
  213. NULL,
  214. &ObjectsSecurityDescriptor,
  215. PagedPool ))) {
  216. DbgPrint("SeDefaultObjectMethod deleting error: %8lx\n", Status );
  217. return FALSE;
  218. }
  219. ExFreePool(Acl);
  220. ExFreePool(Buffer);
  221. return TRUE;
  222. }
  223. BOOLEAN
  224. TestCaptureSecurityDescriptor()
  225. {
  226. SECURITY_DESCRIPTOR SecurityDescriptor;
  227. PACL Sacl;
  228. PACL Dacl;
  229. PSECURITY_DESCRIPTOR NewDescriptor;
  230. NTSTATUS Status;
  231. Sacl = (PACL)ExAllocatePool( PagedPool, 1024 );
  232. Dacl = (PACL)ExAllocatePool( PagedPool, 1024 );
  233. BuildAcl( Pebbles, Sacl, 1024 );
  234. BuildAcl( Barney, Dacl, 1024 );
  235. DiscretionarySecurityDescriptor( &SecurityDescriptor, Dacl );
  236. SecurityDescriptor.SecurityInformationClass = AllAclInformation;
  237. SecurityDescriptor.SystemAcl = Sacl;
  238. //
  239. // Capture kernel mode and don't force
  240. //
  241. if (!NT_SUCCESS(Status = SeCaptureSecurityDescriptor( &SecurityDescriptor,
  242. KernelMode,
  243. PagedPool,
  244. FALSE,
  245. &NewDescriptor ))) {
  246. DbgPrint("SeCapture Error, KernelMode, FALSE : %8lx\n", Status );
  247. return FALSE;
  248. }
  249. //
  250. // Capture kernel mode and force
  251. //
  252. if (!NT_SUCCESS(Status = SeCaptureSecurityDescriptor( &SecurityDescriptor,
  253. KernelMode,
  254. PagedPool,
  255. TRUE,
  256. &NewDescriptor ))) {
  257. DbgPrint("SeCapture Error, KernelMode, TRUE : %8lx\n", Status );
  258. return FALSE;
  259. } else {
  260. ExFreePool( NewDescriptor );
  261. }
  262. //
  263. // Capture user mode
  264. //
  265. if (!NT_SUCCESS(Status = SeCaptureSecurityDescriptor( &SecurityDescriptor,
  266. UserMode,
  267. PagedPool,
  268. TRUE,
  269. &NewDescriptor ))) {
  270. DbgPrint("SeCapture Error, UserMode, FALSE : %8lx\n", Status );
  271. return FALSE;
  272. } else {
  273. ExFreePool( NewDescriptor );
  274. }
  275. return TRUE;
  276. }
  277. BOOLEAN
  278. TestAssignSecurity()
  279. {
  280. SECURITY_DESCRIPTOR SecurityDescriptor;
  281. PACL Acl;
  282. PACCESS_TOKEN Token;
  283. GENERIC_MAPPING GenericMapping;
  284. PSECURITY_DESCRIPTOR NewDescriptor;
  285. NTSTATUS Status;
  286. Acl = (PACL)ExAllocatePool( PagedPool, 1024 );
  287. BuildAcl( Fred, Acl, 1024 );
  288. Token = (PACCESS_TOKEN)ExAllocatePool( PagedPool, 512 );
  289. BuildToken( Fred, Token );
  290. DiscretionarySecurityDescriptor( &SecurityDescriptor, Acl );
  291. //
  292. // Kernel mode, non dir, and no new
  293. //
  294. NewDescriptor = NULL;
  295. if (!NT_SUCCESS(Status = SeAssignSecurity( &SecurityDescriptor,
  296. &NewDescriptor,
  297. FALSE,
  298. Token,
  299. &GenericMapping,
  300. KernelMode ))) {
  301. DbgPrint("SeAssign Error NoNew, NoDir, Kernel : %8lx\n", Status );
  302. return FALSE;
  303. }
  304. //
  305. // Kernel mode, non dir, and new
  306. //
  307. if (!NT_SUCCESS(Status = SeAssignSecurity( &SecurityDescriptor,
  308. &NewDescriptor,
  309. FALSE,
  310. Token,
  311. &GenericMapping,
  312. KernelMode ))) {
  313. DbgPrint("SeAssign Error New, NoDir, Kernel : %8lx\n", Status );
  314. return FALSE;
  315. }
  316. //
  317. // Kernel mode, dir, and no new
  318. //
  319. NewDescriptor = NULL;
  320. if (!NT_SUCCESS(Status = SeAssignSecurity( &SecurityDescriptor,
  321. &NewDescriptor,
  322. TRUE,
  323. Token,
  324. &GenericMapping,
  325. KernelMode ))) {
  326. DbgPrint("SeAssign Error NoNew, Dir, Kernel : %8lx\n", Status );
  327. return FALSE;
  328. }
  329. //
  330. // Kernel mode, dir, and new
  331. //
  332. if (!NT_SUCCESS(Status = SeAssignSecurity( &SecurityDescriptor,
  333. &NewDescriptor,
  334. TRUE,
  335. Token,
  336. &GenericMapping,
  337. KernelMode ))) {
  338. DbgPrint("SeAssign Error New, Dir, Kernel : %8lx\n", Status );
  339. return FALSE;
  340. }
  341. //
  342. // User mode, non dir, and no new
  343. //
  344. NewDescriptor = NULL;
  345. if (!NT_SUCCESS(Status = SeAssignSecurity( &SecurityDescriptor,
  346. &NewDescriptor,
  347. FALSE,
  348. Token,
  349. &GenericMapping,
  350. UserMode ))) {
  351. DbgPrint("SeAssign Error NoNew, NoDir, User : %8lx\n", Status );
  352. return FALSE;
  353. }
  354. //
  355. // User mode, non dir, and new
  356. //
  357. if (!NT_SUCCESS(Status = SeAssignSecurity( &SecurityDescriptor,
  358. &NewDescriptor,
  359. FALSE,
  360. Token,
  361. &GenericMapping,
  362. UserMode ))) {
  363. DbgPrint("SeAssign Error New, NoDir, User : %8lx\n", Status );
  364. return FALSE;
  365. }
  366. //
  367. // User mode, dir, and no new
  368. //
  369. NewDescriptor = NULL;
  370. if (!NT_SUCCESS(Status = SeAssignSecurity( &SecurityDescriptor,
  371. &NewDescriptor,
  372. TRUE,
  373. Token,
  374. &GenericMapping,
  375. UserMode ))) {
  376. DbgPrint("SeAssign Error NoNew, Dir, User : %8lx\n", Status );
  377. return FALSE;
  378. }
  379. //
  380. // User mode, dir, and new
  381. //
  382. if (!NT_SUCCESS(Status = SeAssignSecurity( &SecurityDescriptor,
  383. &NewDescriptor,
  384. TRUE,
  385. Token,
  386. &GenericMapping,
  387. UserMode ))) {
  388. DbgPrint("SeAssign Error New, Dir, User : %8lx\n", Status );
  389. return FALSE;
  390. }
  391. return TRUE;
  392. }
  393. BOOLEAN
  394. TestAccessCheck()
  395. {
  396. SECURITY_DESCRIPTOR SecurityDescriptor;
  397. PACL Acl;
  398. PACCESS_TOKEN Token;
  399. Acl = (PACL)ExAllocatePool( PagedPool, 1024 );
  400. BuildAcl( Fred, Acl, 1024 );
  401. Token = (PACCESS_TOKEN)ExAllocatePool( PagedPool, 512 );
  402. BuildToken( Fred, Token );
  403. DiscretionarySecurityDescriptor( &SecurityDescriptor, Acl );
  404. //
  405. // Test should be successful based on aces
  406. //
  407. if (!SeAccessCheck( &SecurityDescriptor,
  408. Token,
  409. 0x00000001,
  410. NULL,
  411. UserMode )) {
  412. DbgPrint("SeAccessCheck Error should allow access\n");
  413. return FALSE;
  414. }
  415. //
  416. // Test should be successful based on owner
  417. //
  418. if (!SeAccessCheck( &SecurityDescriptor,
  419. Token,
  420. READ_CONTROL,
  421. &FredGuid,
  422. UserMode )) {
  423. DbgPrint("SeAccessCheck Error should allow owner\n");
  424. return FALSE;
  425. }
  426. //
  427. // Test should be unsuccessful based on aces
  428. //
  429. if (SeAccessCheck( &SecurityDescriptor,
  430. Token,
  431. 0x0000000f,
  432. &FredGuid,
  433. UserMode )) {
  434. DbgPrint("SeAccessCheck Error shouldn't allow access\n");
  435. return FALSE;
  436. }
  437. //
  438. // Test should be unsuccessful based on non owner
  439. //
  440. if (SeAccessCheck( &SecurityDescriptor,
  441. Token,
  442. READ_CONTROL,
  443. &BarneyGuid,
  444. UserMode )) {
  445. DbgPrint("SeAccessCheck Error shouldn't allow non owner access\n");
  446. return FALSE;
  447. }
  448. return TRUE;
  449. }
  450. BOOLEAN
  451. TestGenerateMessage()
  452. {
  453. return TRUE;
  454. }
  455.