2
0

RawSecurityDescriptorTest.cs 14 KB

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