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.

437 lines
12 KiB

  1. /*++
  2. Copyright (c) 2002 Microsoft Corporation
  3. Module Name:
  4. tunsd.c
  5. Abstract:
  6. utility routines to handle setting security descriptor on tunmp device.
  7. Environment:
  8. Kernel mode only.
  9. Revision History:
  10. alid 5/17/2002
  11. --*/
  12. #include <ntosp.h>
  13. #include <ntrtl.h>
  14. #define TUN_ALLOC_TAG 'untN'
  15. PACL NetConfigAcl = NULL;
  16. PSID NetConfigOpsSid = NULL;
  17. CHAR NetConfigSecurityDescriptor[SECURITY_DESCRIPTOR_MIN_LENGTH];
  18. PACL
  19. TunCreateAcl(
  20. BOOLEAN Admins,
  21. BOOLEAN LocalSystem,
  22. BOOLEAN LocalService,
  23. BOOLEAN NetworkService,
  24. BOOLEAN NetConfigOps,
  25. BOOLEAN Users,
  26. ACCESS_MASK AccessMask
  27. );
  28. NTSTATUS
  29. TunCreateGenericSD(
  30. PACL Acl,
  31. PCHAR AccessSecurityDescriptor
  32. );
  33. PACL
  34. TunCreateAcl(
  35. BOOLEAN Admins,
  36. BOOLEAN LocalSystem,
  37. BOOLEAN LocalService,
  38. BOOLEAN NetworkService,
  39. BOOLEAN NetConfigOps,
  40. BOOLEAN Users,
  41. ACCESS_MASK AccessMask
  42. )
  43. {
  44. PACL AccessDacl = NULL, pAcl = NULL;
  45. ULONG AclLength = 0;
  46. ULONG NetConfigOpsSidSize;
  47. SID_IDENTIFIER_AUTHORITY NetConfigOpsSidAuth = SECURITY_NT_AUTHORITY;
  48. PISID ISid;
  49. NTSTATUS Status;
  50. do
  51. {
  52. if (Admins)
  53. {
  54. AclLength += sizeof(ACL) +
  55. FIELD_OFFSET (ACCESS_ALLOWED_ACE, SidStart) +
  56. RtlLengthSid(SeExports->SeAliasAdminsSid);
  57. }
  58. if (LocalSystem)
  59. {
  60. AclLength += sizeof(ACL) +
  61. FIELD_OFFSET (ACCESS_ALLOWED_ACE, SidStart) +
  62. RtlLengthSid(SeExports->SeLocalSystemSid);
  63. }
  64. if (LocalService)
  65. {
  66. AclLength += sizeof(ACL) +
  67. FIELD_OFFSET (ACCESS_ALLOWED_ACE, SidStart) +
  68. RtlLengthSid(SeExports->SeLocalServiceSid);
  69. }
  70. if (NetworkService)
  71. {
  72. AclLength += sizeof(ACL) +
  73. FIELD_OFFSET (ACCESS_ALLOWED_ACE, SidStart) +
  74. RtlLengthSid(SeExports->SeNetworkServiceSid);
  75. }
  76. if (NetConfigOps)
  77. {
  78. NetConfigOpsSidSize = RtlLengthRequiredSid(2);
  79. NetConfigOpsSid = (PSID)ExAllocatePoolWithTag(PagedPool, NetConfigOpsSidSize, TUN_ALLOC_TAG);
  80. if (NULL == NetConfigOpsSid)
  81. {
  82. Status = STATUS_INSUFFICIENT_RESOURCES;
  83. break;
  84. }
  85. Status = RtlInitializeSid(NetConfigOpsSid, &NetConfigOpsSidAuth, 2);
  86. if (Status != STATUS_SUCCESS)
  87. {
  88. Status = STATUS_INSUFFICIENT_RESOURCES;
  89. break;
  90. }
  91. ISid = (PISID)(NetConfigOpsSid);
  92. ISid->SubAuthority[0] = SECURITY_BUILTIN_DOMAIN_RID;
  93. ISid->SubAuthority[1] = DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS;
  94. AclLength += sizeof(ACL) +
  95. FIELD_OFFSET (ACCESS_ALLOWED_ACE, SidStart) +
  96. RtlLengthSid(NetConfigOpsSid);
  97. }
  98. if (Users)
  99. {
  100. AclLength += sizeof(ACL) +
  101. FIELD_OFFSET (ACCESS_ALLOWED_ACE, SidStart) +
  102. RtlLengthSid(SeExports->SeAliasUsersSid);
  103. }
  104. AccessDacl = (PACL)ExAllocatePoolWithTag(PagedPool,
  105. AclLength,
  106. TUN_ALLOC_TAG);
  107. if (AccessDacl == NULL)
  108. {
  109. Status = STATUS_INSUFFICIENT_RESOURCES;
  110. break;
  111. }
  112. Status = RtlCreateAcl(AccessDacl,
  113. AclLength,
  114. ACL_REVISION2);
  115. if (!NT_SUCCESS(Status))
  116. {
  117. #if DBG
  118. DbgPrint("RtlCreateAcl failed, Status %lx.\n", Status);
  119. #endif
  120. break;
  121. }
  122. if (Admins)
  123. {
  124. Status = RtlAddAccessAllowedAce(
  125. AccessDacl,
  126. ACL_REVISION2,
  127. (STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL),
  128. SeExports->SeAliasAdminsSid
  129. );
  130. if (!NT_SUCCESS(Status))
  131. {
  132. #if DBG
  133. DbgPrint("RtlAddAccessAllowedAce failed, Status %lx.\n", Status);
  134. #endif
  135. break;
  136. }
  137. }
  138. if (LocalSystem)
  139. {
  140. Status = RtlAddAccessAllowedAce(
  141. AccessDacl,
  142. ACL_REVISION2,
  143. (STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL),
  144. SeExports->SeLocalSystemSid
  145. );
  146. if (!NT_SUCCESS(Status))
  147. {
  148. #if DBG
  149. DbgPrint("RtlAddAccessAllowedAce failed, Status %lx.\n", Status);
  150. #endif
  151. break;
  152. }
  153. }
  154. if (LocalService)
  155. {
  156. Status = RtlAddAccessAllowedAce(
  157. AccessDacl,
  158. ACL_REVISION2,
  159. (STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL),
  160. SeExports->SeLocalServiceSid
  161. );
  162. if (!NT_SUCCESS(Status))
  163. {
  164. #if DBG
  165. DbgPrint("RtlAddAccessAllowedAce failed, Status %lx.\n", Status);
  166. #endif
  167. break;
  168. }
  169. }
  170. if (NetworkService)
  171. {
  172. Status = RtlAddAccessAllowedAce(
  173. AccessDacl,
  174. ACL_REVISION2,
  175. (STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL),
  176. SeExports->SeNetworkServiceSid
  177. );
  178. if (!NT_SUCCESS(Status))
  179. {
  180. #if DBG
  181. DbgPrint("RtlAddAccessAllowedAce failed, Status %lx.\n", Status);
  182. #endif
  183. break;
  184. }
  185. }
  186. if (NetConfigOps)
  187. {
  188. Status = RtlAddAccessAllowedAce(
  189. AccessDacl,
  190. ACL_REVISION2,
  191. (STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL),
  192. NetConfigOpsSid
  193. );
  194. if (!NT_SUCCESS(Status))
  195. {
  196. #if DBG
  197. DbgPrint("RtlAddAccessAllowedAce failed, Status %lx.\n", Status);
  198. #endif
  199. break;
  200. }
  201. }
  202. if (Users)
  203. {
  204. Status = RtlAddAccessAllowedAce(
  205. AccessDacl,
  206. ACL_REVISION2,
  207. AccessMask,
  208. SeExports->SeAliasUsersSid
  209. );
  210. if (!NT_SUCCESS(Status))
  211. {
  212. DbgPrint("RtlAddAccessAllowedAce failed, Status %lx.\n", Status);
  213. break;
  214. }
  215. }
  216. pAcl = AccessDacl;
  217. }while (FALSE);
  218. if (pAcl == NULL)
  219. {
  220. if (AccessDacl)
  221. ExFreePool(AccessDacl);
  222. if (NetConfigOpsSid)
  223. {
  224. ExFreePool(NetConfigOpsSid);
  225. NetConfigOpsSid = NULL;
  226. }
  227. }
  228. return pAcl;
  229. }
  230. NTSTATUS
  231. TunCreateGenericSD(
  232. PACL Acl,
  233. PCHAR AccessSecurityDescriptor
  234. )
  235. /*++
  236. Routine Description:
  237. Creates the SD responsible for giving access to different users.
  238. Arguments:
  239. None.
  240. Return Value:
  241. STATUS_SUCCESS or an appropriate error code.
  242. --*/
  243. {
  244. PSECURITY_DESCRIPTOR AccessSd;
  245. NTSTATUS Status;
  246. if (Acl == NULL)
  247. return STATUS_UNSUCCESSFUL;
  248. do
  249. {
  250. AccessSd = AccessSecurityDescriptor;
  251. Status = RtlCreateSecurityDescriptor(
  252. AccessSd,
  253. SECURITY_DESCRIPTOR_REVISION1
  254. );
  255. if (!NT_SUCCESS(Status))
  256. {
  257. DbgPrint("RtlCreateSecurityDescriptor failed, Status %lx.\n", Status);
  258. break;
  259. }
  260. Status = RtlSetDaclSecurityDescriptor(
  261. AccessSd,
  262. TRUE, // DaclPresent
  263. Acl,
  264. FALSE // DaclDefaulted
  265. );
  266. if (!NT_SUCCESS(Status))
  267. {
  268. DbgPrint("RtlSetDaclSecurityDescriptor failed, Status %lx.\n", Status);
  269. break;
  270. }
  271. Status = RtlSetOwnerSecurityDescriptor(AccessSd,
  272. SeExports->SeAliasAdminsSid,
  273. FALSE);
  274. if (!NT_SUCCESS(Status))
  275. {
  276. DbgPrint("RtlSetOwnerSecurityDescriptor failed, Status %lx.\n", Status);
  277. break;
  278. }
  279. Status = RtlSetGroupSecurityDescriptor(AccessSd,
  280. SeExports->SeAliasAdminsSid,
  281. FALSE);
  282. if (!NT_SUCCESS(Status))
  283. {
  284. DbgPrint("RtlSetGroupSecurityDescriptor failed, Status %lx.\n", Status);
  285. break;
  286. }
  287. }while (FALSE);
  288. return (Status);
  289. }
  290. NTSTATUS
  291. TunCreateSD(
  292. VOID
  293. )
  294. {
  295. NTSTATUS Status;
  296. //
  297. // create an ACL for admin types
  298. //
  299. NetConfigAcl = TunCreateAcl(TRUE, // Admins
  300. TRUE, //LocalSystem
  301. TRUE, //LocalService
  302. TRUE, //NetworkService
  303. TRUE, //NetConfigOps
  304. FALSE, //Users
  305. GENERIC_READ | GENERIC_WRITE
  306. );
  307. if (NetConfigAcl != NULL)
  308. {
  309. Status = TunCreateGenericSD(NetConfigAcl, NetConfigSecurityDescriptor);
  310. }
  311. else
  312. {
  313. Status = STATUS_UNSUCCESSFUL;
  314. }
  315. return Status;
  316. }
  317. NTSTATUS
  318. TunSetSecurity(
  319. IN PDEVICE_OBJECT DeviceObject
  320. )
  321. {
  322. NTSTATUS Status;
  323. SECURITY_INFORMATION secInfo = OWNER_SECURITY_INFORMATION |
  324. GROUP_SECURITY_INFORMATION |
  325. DACL_SECURITY_INFORMATION;
  326. Status = ObSetSecurityObjectByPointer(DeviceObject,
  327. secInfo,
  328. NetConfigSecurityDescriptor);
  329. ASSERT(NT_SUCCESS(Status));
  330. return Status;
  331. }
  332. VOID
  333. TunDeleteSD(
  334. VOID
  335. )
  336. /*
  337. delete NetConfigAcl
  338. */
  339. {
  340. if (NetConfigAcl != NULL)
  341. {
  342. ExFreePool(NetConfigAcl);
  343. NetConfigAcl = NULL;
  344. }
  345. if (NetConfigOpsSid != NULL)
  346. {
  347. ExFreePool(NetConfigOpsSid);
  348. NetConfigOpsSid = NULL;
  349. }
  350. }