RawSecurityDescriptorTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // RawSecurityDescriptorTest.cs - NUnit Test Cases for RawSecurityDescriptor
  3. //
  4. // Author:
  5. // Kenneth Bell
  6. //
  7. using System;
  8. using System.Security.AccessControl;
  9. using System.Security.Principal;
  10. using NUnit.Framework;
  11. namespace MonoTests.System.Security.AccessControl {
  12. [TestFixture]
  13. public class RawSecurityDescriptorTest {
  14. private void CheckSddlConstructor (string sddl, byte[] expectedBinary)
  15. {
  16. RawSecurityDescriptor sd = new RawSecurityDescriptor (sddl);
  17. Assert.GreaterOrEqual (sd.BinaryLength, 0);
  18. byte[] buffer = new byte[sd.BinaryLength];
  19. sd.GetBinaryForm (buffer, 0);
  20. Assert.AreEqual (expectedBinary, buffer);
  21. }
  22. private void CheckBinaryConstructor (string expectedSddl, byte[] binary)
  23. {
  24. RawSecurityDescriptor sd = new RawSecurityDescriptor (binary, 0);
  25. Assert.AreEqual (sd.BinaryLength, binary.Length);
  26. Assert.AreEqual (expectedSddl, sd.GetSddlForm (AccessControlSections.All));
  27. }
  28. private void CheckRoundTrip (string sddl)
  29. {
  30. RawSecurityDescriptor sd = new RawSecurityDescriptor (sddl);
  31. byte[] buffer = new byte[sd.BinaryLength];
  32. sd.GetBinaryForm (buffer, 0);
  33. sd = new RawSecurityDescriptor (buffer, 0);
  34. Assert.AreEqual (sddl, sd.GetSddlForm (AccessControlSections.All));
  35. }
  36. [Test]
  37. public void ConstructorEmptyString ()
  38. {
  39. byte[] sdBinary = new byte[] {
  40. 0x01, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  42. CheckSddlConstructor ("", sdBinary);
  43. }
  44. [Test]
  45. public void ConstructorString ()
  46. {
  47. byte[] sdBinary = new byte[] {
  48. 0x01, 0x00, 0x04, 0x80, 0x14, 0x00, 0x00, 0x00, 0x24, 0x00,
  49. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00,
  50. 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
  51. 0x00, 0x00, 0x21, 0x02, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00,
  52. 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x20, 0x02,
  53. 0x00, 0x00, 0x02, 0x00, 0x1C, 0x00, 0x01, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0x14, 0x00, 0x3F, 0x00, 0x0E, 0x10, 0x01, 0x01,
  55. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  56. CheckSddlConstructor ("O:BUG:BAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)", sdBinary);
  57. CheckSddlConstructor ("G:BAO:BUD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)", sdBinary);
  58. CheckSddlConstructor ("G:BAD:(A; ;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)O:BU", sdBinary);
  59. CheckSddlConstructor ("O:buG:baD:(a;;rpwpccdclcswrcwdwoga;;;s-1-0-0)", sdBinary);
  60. sdBinary = new byte[] {
  61. 0x01, 0x00, 0x00, 0x80, 0x14, 0x00, 0x00, 0x00, 0x24, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63. 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
  64. 0x00, 0x00, 0x21, 0x02, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x20, 0x02,
  66. 0x00, 0x00 };
  67. CheckSddlConstructor ("O:BUG:BA", sdBinary);
  68. sdBinary = new byte[] {
  69. 0x01, 0x00, 0x04, 0x80, 0x14, 0x00, 0x00, 0x00, 0x24, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00,
  71. 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
  72. 0x00, 0x00, 0x21, 0x02, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00,
  73. 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x20, 0x02,
  74. 0x00, 0x00, 0x04, 0x00, 0x40, 0x00, 0x01, 0x00, 0x00, 0x00,
  75. 0x05, 0x00, 0x38, 0x00, 0x3F, 0x00, 0x0E, 0x10, 0x03, 0x00,
  76. 0x00, 0x00, 0x53, 0x1A, 0x72, 0xAB, 0x2F, 0x1E, 0xD0, 0x11,
  77. 0x98, 0x19, 0x00, 0xAA, 0x00, 0x40, 0x52, 0x9B, 0x53, 0x1A,
  78. 0x72, 0xAB, 0x2F, 0x1E, 0xD0, 0x11, 0x98, 0x19, 0x00, 0xAA,
  79. 0x00, 0x40, 0x52, 0x9B, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  80. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  81. CheckSddlConstructor ("O:BUG:BAD:(OA;;RPWPCCDCLCSWRCWDWOGA;ab721a53-1e2f-11d0-9819-00aa0040529b;ab721a53-1e2f-11d0-9819-00aa0040529b;S-1-0-0)", sdBinary);
  82. }
  83. [Test]
  84. public void ConstructorBinary ()
  85. {
  86. byte[] sdBinary = new byte[] {
  87. 0x01, 0x00, 0x04, 0x80, 0x14, 0x00, 0x00, 0x00, 0x24, 0x00,
  88. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00,
  89. 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
  90. 0x00, 0x00, 0x21, 0x02, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00,
  91. 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x20, 0x02,
  92. 0x00, 0x00, 0x02, 0x00, 0x1C, 0x00, 0x01, 0x00, 0x00, 0x00,
  93. 0x00, 0x00, 0x14, 0x00, 0x3F, 0x00, 0x0E, 0x10, 0x01, 0x01,
  94. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  95. CheckBinaryConstructor ("O:BUG:BAD:(A;;CCDCLCSWRPWPRCWDWOGA;;;S-1-0-0)", sdBinary);
  96. sdBinary = new byte[] {
  97. 0x01, 0x00, 0x00, 0x80, 0x14, 0x00, 0x00, 0x00, 0x24, 0x00,
  98. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99. 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
  100. 0x00, 0x00, 0x21, 0x02, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00,
  101. 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x20, 0x02,
  102. 0x00, 0x00 };
  103. CheckBinaryConstructor ("O:BUG:BA", sdBinary);
  104. sdBinary = new byte[] {
  105. 0x01, 0x00, 0x04, 0x80, 0x14, 0x00, 0x00, 0x00, 0x24, 0x00,
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00,
  107. 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
  108. 0x00, 0x00, 0x21, 0x02, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x20, 0x02,
  110. 0x00, 0x00, 0x04, 0x00, 0x40, 0x00, 0x01, 0x00, 0x00, 0x00,
  111. 0x05, 0x00, 0x38, 0x00, 0x3F, 0x00, 0x0E, 0x10, 0x03, 0x00,
  112. 0x00, 0x00, 0x53, 0x1A, 0x72, 0xAB, 0x2F, 0x1E, 0xD0, 0x11,
  113. 0x98, 0x19, 0x00, 0xAA, 0x00, 0x40, 0x52, 0x9B, 0x53, 0x1A,
  114. 0x72, 0xAB, 0x2F, 0x1E, 0xD0, 0x11, 0x98, 0x19, 0x00, 0xAA,
  115. 0x00, 0x40, 0x52, 0x9B, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  117. CheckBinaryConstructor ("O:BUG:BAD:(OA;;CCDCLCSWRPWPRCWDWOGA;ab721a53-1e2f-11d0-9819-00aa0040529b;ab721a53-1e2f-11d0-9819-00aa0040529b;S-1-0-0)", sdBinary);
  118. }
  119. [Test]
  120. public void FlagMismatch ()
  121. {
  122. // Check setting DACL-present flag on empty SD
  123. RawSecurityDescriptor sd = new RawSecurityDescriptor ("");
  124. Assert.AreEqual (20, sd.BinaryLength);
  125. sd.SetFlags (ControlFlags.DiscretionaryAclPresent);
  126. Assert.AreEqual (20, sd.BinaryLength);
  127. byte[] buffer = new byte[sd.BinaryLength];
  128. sd.GetBinaryForm (buffer, 0);
  129. byte[] sdBinary = new byte[] {
  130. 0x01, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  132. Assert.AreEqual (sdBinary, buffer);
  133. // Check unsetting DACL-present flag on SD with DACL
  134. sd = new RawSecurityDescriptor ("O:BUG:BAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)");
  135. Assert.AreEqual (80, sd.BinaryLength);
  136. sd.SetFlags (sd.ControlFlags & ~ControlFlags.DiscretionaryAclPresent);
  137. Assert.AreEqual (ControlFlags.SelfRelative, sd.ControlFlags);
  138. Assert.AreEqual (52, sd.BinaryLength);
  139. buffer = new byte[sd.BinaryLength];
  140. sd.GetBinaryForm (buffer, 0);
  141. sdBinary = new byte[] {
  142. 0x01, 0x00, 0x00, 0x80, 0x14, 0x00, 0x00, 0x00, 0x24, 0x00,
  143. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  144. 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
  145. 0x00, 0x00, 0x21, 0x02, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00,
  146. 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x20, 0x02,
  147. 0x00, 0x00 };
  148. Assert.AreEqual (sdBinary, buffer);
  149. }
  150. [Test]
  151. public void GetBinaryForm ()
  152. {
  153. RawSecurityDescriptor sd = new RawSecurityDescriptor ("");
  154. sd.Owner = new SecurityIdentifier (WellKnownSidType.BuiltinUsersSid, null);
  155. sd.Group = new SecurityIdentifier (WellKnownSidType.BuiltinAdministratorsSid, null);
  156. sd.DiscretionaryAcl = new RawAcl (1, 0);
  157. sd.SystemAcl = new RawAcl (1, 0);
  158. sd.SetFlags (sd.ControlFlags | ControlFlags.DiscretionaryAclPresent | ControlFlags.SystemAclPresent);
  159. // Empty ACL form
  160. byte[] buffer = new byte[sd.BinaryLength];
  161. sd.GetBinaryForm (buffer, 0);
  162. byte[] sdBinary = new byte[] {
  163. 0x01, 0x00, 0x14, 0x80, 0x14, 0x00, 0x00, 0x00, 0x24, 0x00,
  164. 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00,
  165. 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
  166. 0x00, 0x00, 0x21, 0x02, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00,
  167. 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x20, 0x02,
  168. 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  169. 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 };
  170. Assert.AreEqual (sdBinary, buffer);
  171. // Add an ACE to the DACL
  172. SecurityIdentifier builtInAdmins = new SecurityIdentifier (WellKnownSidType.BuiltinAdministratorsSid, null);
  173. CommonAce ace = new CommonAce (AceFlags.None, AceQualifier.AccessAllowed, 0x7FFFFFFF, builtInAdmins, false, null);
  174. sd.DiscretionaryAcl.InsertAce (0, ace);
  175. buffer = new byte[sd.BinaryLength];
  176. sd.GetBinaryForm (buffer, 0);
  177. sdBinary = new byte[] {
  178. 0x01, 0x00, 0x14, 0x80, 0x14, 0x00, 0x00, 0x00, 0x24, 0x00,
  179. 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00,
  180. 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
  181. 0x00, 0x00, 0x21, 0x02, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00,
  182. 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x20, 0x02,
  183. 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  184. 0x01, 0x00, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  185. 0x18, 0x00, 0xFF, 0xFF, 0xFF, 0x7F, 0x01, 0x02, 0x00, 0x00,
  186. 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x20, 0x02,
  187. 0x00, 0x00 };
  188. Assert.AreEqual (sdBinary, buffer);
  189. // This time with an Object ACE
  190. ObjectAce objectAce = new ObjectAce (AceFlags.Inherited, AceQualifier.AccessAllowed, 0x12345678, builtInAdmins, ObjectAceFlags.ObjectAceTypePresent | ObjectAceFlags.InheritedObjectAceTypePresent, new Guid ("189c0dc7-b849-4dea-93a5-6d4cb8857a5c"), new Guid ("53b4a3d4-fe39-468b-bc60-b4fcba772fa5"), false, null);
  191. sd.DiscretionaryAcl = new RawAcl (2, 0);
  192. sd.DiscretionaryAcl.InsertAce (0, objectAce);
  193. buffer = new byte[sd.BinaryLength];
  194. sd.GetBinaryForm (buffer, 0);
  195. sdBinary = new byte[] {
  196. 0x01, 0x00, 0x14, 0x80, 0x14, 0x00, 0x00, 0x00, 0x24, 0x00,
  197. 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00,
  198. 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
  199. 0x00, 0x00, 0x21, 0x02, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00,
  200. 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x20, 0x02,
  201. 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  202. 0x02, 0x00, 0x44, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x10,
  203. 0x3C, 0x00, 0x78, 0x56, 0x34, 0x12, 0x03, 0x00, 0x00, 0x00,
  204. 0xC7, 0x0D, 0x9C, 0x18, 0x49, 0xB8, 0xEA, 0x4D, 0x93, 0xA5,
  205. 0x6D, 0x4C, 0xB8, 0x85, 0x7A, 0x5C, 0xD4, 0xA3, 0xB4, 0x53,
  206. 0x39, 0xFE, 0x8B, 0x46, 0xBC, 0x60, 0xB4, 0xFC, 0xBA, 0x77,
  207. 0x2F, 0xA5, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
  208. 0x20, 0x00, 0x00, 0x00, 0x20, 0x02, 0x00, 0x00 };
  209. Assert.AreEqual (sdBinary, buffer);
  210. }
  211. [Test]
  212. public void GetSddlForm ()
  213. {
  214. RawSecurityDescriptor sd = new RawSecurityDescriptor ("");
  215. Assert.AreEqual ("", sd.GetSddlForm (AccessControlSections.All));
  216. // Ask for part of SD that isn't represented
  217. sd.Owner = new SecurityIdentifier (WellKnownSidType.BuiltinUsersSid, null);
  218. sd.Group = new SecurityIdentifier (WellKnownSidType.BuiltinAdministratorsSid, null);
  219. Assert.AreEqual ("", sd.GetSddlForm (AccessControlSections.Access));
  220. // Empty ACL form
  221. sd.DiscretionaryAcl = new RawAcl (2, 0);
  222. sd.SystemAcl = new RawAcl (1, 0);
  223. sd.SetFlags (sd.ControlFlags | ControlFlags.DiscretionaryAclPresent | ControlFlags.SystemAclPresent);
  224. Assert.AreEqual ("O:BUG:BAD:S:", sd.GetSddlForm (AccessControlSections.All));
  225. // Add an ACE to the DACL
  226. SecurityIdentifier builtInAdmins = new SecurityIdentifier (WellKnownSidType.BuiltinAdministratorsSid, null);
  227. CommonAce ace = new CommonAce (AceFlags.None, AceQualifier.AccessAllowed, 0x7FFFFFFF, builtInAdmins, false, null);
  228. sd.DiscretionaryAcl.InsertAce (0, ace);
  229. Assert.AreEqual ("O:BUG:BAD:(A;;0x7fffffff;;;BA)S:", sd.GetSddlForm (AccessControlSections.All));
  230. // Add second ACE to the DACL
  231. SecurityIdentifier randomUser = new SecurityIdentifier ("S-1-5-21-324-23423-234-334");
  232. ace = new CommonAce (AceFlags.Inherited | AceFlags.ContainerInherit, AceQualifier.AccessDenied, 0x12345678, randomUser, true, null);
  233. sd.DiscretionaryAcl.InsertAce (0, ace);
  234. Assert.AreEqual ("O:BUD:(XD;CIID;0x12345678;;;S-1-5-21-324-23423-234-334)(A;;0x7fffffff;;;BA)", sd.GetSddlForm (AccessControlSections.Owner | AccessControlSections.Access));
  235. // DACL & SACL flags
  236. sd.SetFlags (sd.ControlFlags | ControlFlags.DiscretionaryAclProtected | ControlFlags.DiscretionaryAclAutoInherited | ControlFlags.DiscretionaryAclAutoInheritRequired | ControlFlags.SystemAclAutoInherited);
  237. sd.DiscretionaryAcl = new RawAcl (1, 0);
  238. ace = new CommonAce (AceFlags.None, AceQualifier.AccessAllowed, 0x7FFFFFFF, builtInAdmins, false, null);
  239. sd.DiscretionaryAcl.InsertAce (0, ace);
  240. Assert.AreEqual ("O:BUG:BAD:PARAI(A;;0x7fffffff;;;BA)S:AI", sd.GetSddlForm (AccessControlSections.All));
  241. sd.SetFlags (sd.ControlFlags | ControlFlags.ServerSecurity | ControlFlags.DiscretionaryAclDefaulted);
  242. Assert.AreEqual ("O:BUG:BAD:PARAI(A;;0x7fffffff;;;BA)S:AI", sd.GetSddlForm (AccessControlSections.All));
  243. }
  244. [Test]
  245. public void RoundTrip ()
  246. {
  247. CheckRoundTrip ("O:BUG:BAD:(A;;CCDCLCSWRPWPRCWDWOGA;;;S-1-0-0)");
  248. CheckRoundTrip ("O:BUG:BAD:(A;;KR;;;S-1-0-0)");
  249. CheckRoundTrip ("O:BUG:BAD:(OA;;CCDCLCSWRPWPRCWDWOGA;ab721a53-1e2f-11d0-9819-00aa0040529b;ab721a53-1e2f-11d0-9819-00aa0040529b;S-1-0-0)");
  250. CheckRoundTrip ("O:BUG:BAD:(A;;CCDCLCSWRPRC;;;S-1-0-0)");
  251. CheckRoundTrip ("O:SYG:BAD:(A;;0x12019f;;;SY)(A;;0x12019f;;;BA)");
  252. CheckRoundTrip ("O:SYG:BAD:(A;OICINPIOID;0x12019f;;;SY)");
  253. CheckRoundTrip ("O:SYG:BAS:(AU;SAFA;0x12019f;;;SY)");
  254. }
  255. }
  256. }