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.

535 lines
15 KiB

  1. /*************************************************************************
  2. *
  3. * file.c
  4. *
  5. * Process the security on a file
  6. *
  7. * Copyright Microsoft, 1998
  8. *
  9. *
  10. *
  11. *************************************************************************/
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include <process.h>
  18. #include <winsta.h>
  19. #include <syslib.h>
  20. #include "security.h"
  21. #if DBG
  22. ULONG
  23. DbgPrint(
  24. PCH Format,
  25. ...
  26. );
  27. #ifdef FULL_DEBUG
  28. #define DBGPRINT(x) DbgPrint x
  29. #else
  30. #define DBGPRINT(x)
  31. #endif
  32. #if DBGTRACE
  33. #define TRACE0(x) DbgPrint x
  34. #define TRACE1(x) DbgPrint x
  35. #else
  36. #define TRACE0(x)
  37. #define TRACE1(x)
  38. #endif
  39. #else
  40. #define DBGPRINT(x)
  41. #define TRACE0(x)
  42. #define TRACE1(x)
  43. #endif
  44. // External data
  45. // data.c
  46. extern ACCESS_MASK DeniedAccess;
  47. // security.c
  48. extern PSID SeCreatorOwnerSid;
  49. extern PSID SeCreatorGroupSid;
  50. FILE_RESULT
  51. xxxSetFileSecurity(
  52. PWCHAR pFile
  53. );
  54. /*****************************************************************************
  55. *
  56. * xxxProcessFile
  57. *
  58. * Process the given file for access security holes
  59. *
  60. * ENTRY:
  61. * Param1 (input/output)
  62. * Comments
  63. *
  64. * EXIT:
  65. * STATUS_SUCCESS - no error
  66. *
  67. ****************************************************************************/
  68. FILE_RESULT
  69. xxxProcessFile(
  70. PWCHAR pFile,
  71. PWIN32_FIND_DATAW p,
  72. DWORD Level,
  73. DWORD Index
  74. )
  75. {
  76. FILE_RESULT rc;
  77. rc = xxxSetFileSecurity( pFile );
  78. return( rc );
  79. }
  80. /*****************************************************************************
  81. *
  82. * xxxSetFileSecurity
  83. *
  84. * Set the security properties for the given file
  85. *
  86. * ENTRY:
  87. * Param1 (input/output)
  88. * Comments
  89. *
  90. * EXIT:
  91. * STATUS_SUCCESS - no error
  92. *
  93. ****************************************************************************/
  94. FILE_RESULT
  95. xxxSetFileSecurity(
  96. PWCHAR pFile
  97. )
  98. {
  99. BOOL rc;
  100. BOOL DaclPresent;
  101. BOOL Default;
  102. BOOL OwnerDefaulted;
  103. BOOL GroupDefaulted;
  104. FILE_RESULT Result, ReturnResult;
  105. DWORD Size, Index;
  106. PACL pACL = NULL;
  107. PVOID pAce = NULL;
  108. SECURITY_INFORMATION Info = 0;
  109. DWORD Error;
  110. PSECURITY_DESCRIPTOR pSelfSd = NULL;
  111. // Absolute SD values
  112. PSECURITY_DESCRIPTOR pAbsSd = NULL;
  113. DWORD AbsSdSize = 0;
  114. PACL pAbsAcl = NULL;
  115. DWORD AbsAclSize = 0;
  116. PACL pAbsSacl = NULL;
  117. DWORD AbsSaclSize = 0;
  118. PSID pAbsOwner = NULL;
  119. DWORD AbsOwnerSize = 0;
  120. PSID pAbsGroup = NULL;
  121. DWORD AbsGroupSize = 0;
  122. DBGPRINT(( "entering xxxSetFileSecurity(pFile=%ws)\n", pFile ));
  123. /*
  124. * Get the files current security descriptor
  125. */
  126. Size = 0;
  127. rc = GetFileSecurityW(
  128. pFile,
  129. DACL_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION,
  130. NULL, // pSelfSd
  131. 0,
  132. &Size
  133. );
  134. if( rc ) {
  135. Error = GetLastError();
  136. ReportFileResult(
  137. FileAccessErrorUserFormat,
  138. 0, // Access
  139. pFile,
  140. NULL, // pAccountName
  141. NULL, // pDomainName
  142. "%d has no DACL",
  143. Error
  144. );
  145. DBGPRINT(( "leaving xxxSetFileSecurity(1); returning=FileAccessError\n" ));
  146. return( FileAccessError );
  147. }
  148. else {
  149. pSelfSd = LocalAlloc( LMEM_FIXED, Size );
  150. if( pSelfSd == NULL ) {
  151. ReportFileResult(
  152. FileAccessErrorUserFormat,
  153. 0, // Access
  154. pFile,
  155. NULL, // pAccountName
  156. NULL, // pDomainName
  157. "Out of memory skipped entry"
  158. );
  159. DBGPRINT(( "leaving xxxSetFileSecurity(2); returning=0\n" ));
  160. return( FALSE );
  161. }
  162. rc = GetFileSecurityW(
  163. pFile,
  164. DACL_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION,
  165. pSelfSd,
  166. Size,
  167. &Size
  168. );
  169. if( !rc ) {
  170. Error = GetLastError();
  171. ReportFileResult(
  172. FileAccessErrorUserFormat,
  173. 0, // Access
  174. pFile,
  175. NULL, // pAccountName
  176. NULL, // pDomainName
  177. "%d Could not get DACL",
  178. Error
  179. );
  180. LocalFree( pSelfSd );
  181. DBGPRINT(( "leaving xxxSetFileSecurity(3); returning=FileAccessError\n" ));
  182. return( FileAccessError );
  183. }
  184. }
  185. //
  186. // Now convert the self relative SD to an absolute one.
  187. //
  188. rc = MakeAbsoluteSD (
  189. pSelfSd,
  190. pAbsSd,
  191. &AbsSdSize,
  192. pAbsAcl,
  193. &AbsAclSize,
  194. pAbsSacl,
  195. &AbsSaclSize,
  196. pAbsOwner,
  197. &AbsOwnerSize,
  198. pAbsGroup,
  199. &AbsGroupSize
  200. );
  201. if( !rc ) {
  202. Error = GetLastError();
  203. if( Error != ERROR_INSUFFICIENT_BUFFER ) {
  204. ReportFileResult(
  205. FileAccessErrorUserFormat,
  206. 0, // Access
  207. pFile,
  208. NULL, // pAccountName
  209. NULL, // pDomainName
  210. "%d converting SECURITY_DESCRIPTOR",
  211. Error
  212. );
  213. LocalFree( pSelfSd );
  214. DBGPRINT(( "leaving xxxSetFileSecurity(4); returning=FileAccessError\n" ));
  215. return( FileAccessError );
  216. }
  217. // Allocate buffers and now really get the SD
  218. pAbsSd = LocalAlloc( LMEM_FIXED, AbsSdSize );
  219. pAbsAcl = LocalAlloc( LMEM_FIXED, AbsAclSize );
  220. pAbsSacl = LocalAlloc( LMEM_FIXED, AbsSaclSize );
  221. pAbsOwner = LocalAlloc( LMEM_FIXED, AbsOwnerSize );
  222. pAbsGroup = LocalAlloc( LMEM_FIXED, AbsGroupSize );
  223. if( !( pAbsSd && pAbsAcl && pAbsSacl && pAbsOwner && pAbsGroup ) ) {
  224. ReportFileResult(
  225. FileAccessErrorUserFormat,
  226. 0, // Access
  227. pFile,
  228. NULL, // pAccountName
  229. NULL, // pDomainName
  230. "Allocating memory"
  231. );
  232. if( pAbsSd ) LocalFree( pAbsSd );
  233. if( pAbsAcl ) LocalFree( pAbsAcl );
  234. if( pAbsSacl ) LocalFree( pAbsSacl );
  235. if( pAbsOwner ) LocalFree( pAbsOwner );
  236. if( pAbsGroup ) LocalFree( pAbsGroup );
  237. LocalFree( pSelfSd );
  238. DBGPRINT(( "leaving xxxSetFileSecurity(5); returning=FileAccessError\n" ));
  239. return( FileAccessError );
  240. }
  241. // Try it again
  242. rc = MakeAbsoluteSD (
  243. pSelfSd,
  244. pAbsSd,
  245. &AbsSdSize,
  246. pAbsAcl,
  247. &AbsAclSize,
  248. pAbsSacl,
  249. &AbsSaclSize,
  250. pAbsOwner,
  251. &AbsOwnerSize,
  252. pAbsGroup,
  253. &AbsGroupSize
  254. );
  255. if( !rc ) {
  256. Error = GetLastError();
  257. ReportFileResult(
  258. FileAccessErrorUserFormat,
  259. 0, // Access
  260. pFile,
  261. NULL, // pAccountName
  262. NULL, // pDomainName
  263. "%d Making ABSOLUTE SD",
  264. Error
  265. );
  266. if( pAbsSd ) LocalFree( pAbsSd );
  267. if( pAbsAcl ) LocalFree( pAbsAcl );
  268. if( pAbsSacl ) LocalFree( pAbsSacl );
  269. if( pAbsOwner ) LocalFree( pAbsOwner );
  270. if( pAbsGroup ) LocalFree( pAbsGroup );
  271. LocalFree( pSelfSd );
  272. DBGPRINT(( "leaving xxxSetFileSecurity(6); returning=FileAccessError\n" ));
  273. return( FileAccessError );
  274. }
  275. }
  276. //
  277. // Get our new trusted ACL
  278. //
  279. pACL = GetSecureAcl();
  280. if( pACL == NULL ) {
  281. ReportFileResult(
  282. FileAccessErrorUserFormat,
  283. 0, // Access
  284. pFile,
  285. NULL, // pAccountName
  286. NULL, // pDomainName
  287. "Could not get New ACL"
  288. );
  289. if( pAbsSd ) LocalFree( pAbsSd );
  290. if( pAbsAcl ) LocalFree( pAbsAcl );
  291. if( pAbsSacl ) LocalFree( pAbsSacl );
  292. if( pAbsOwner ) LocalFree( pAbsOwner );
  293. if( pAbsGroup ) LocalFree( pAbsGroup );
  294. LocalFree( pSelfSd );
  295. DBGPRINT(( "leaving xxxSetFileSecurity(7); returning=FileAccessError\n" ));
  296. return( FileAccessError );
  297. }
  298. //
  299. // Now set the trusted ACL onto the security descriptor
  300. //
  301. rc = SetSecurityDescriptorDacl(
  302. pAbsSd,
  303. TRUE, // DACL present
  304. pACL,
  305. FALSE // Not default
  306. );
  307. if( !rc ) {
  308. Error = GetLastError();
  309. ReportFileResult(
  310. FileAccessErrorUserFormat,
  311. 0, // Access
  312. pFile,
  313. NULL, // pAccountName
  314. NULL, // pDomainName
  315. "Could not set new ACL in Security Descriptor %d",
  316. Error
  317. );
  318. if( pAbsSd ) LocalFree( pAbsSd );
  319. if( pAbsAcl ) LocalFree( pAbsAcl );
  320. if( pAbsSacl ) LocalFree( pAbsSacl );
  321. if( pAbsOwner ) LocalFree( pAbsOwner );
  322. if( pAbsGroup ) LocalFree( pAbsGroup );
  323. LocalFree( pSelfSd );
  324. DBGPRINT(( "leaving xxxSetFileSecurity(8); returning=FileAccessError\n" ));
  325. return( FileAccessError );
  326. }
  327. Info |= DACL_SECURITY_INFORMATION;
  328. //
  329. // If the owner is not one of the admins, we will grab
  330. // it and local admin will now own it
  331. //
  332. if( pAbsOwner && !IsAllowSid( pAbsOwner ) ) {
  333. // Make the local admin own it
  334. rc = SetSecurityDescriptorOwner(
  335. pAbsSd,
  336. GetAdminSid(),
  337. FALSE // Not defaulted
  338. );
  339. if( !rc ) {
  340. Error = GetLastError();
  341. ReportFileResult(
  342. FileAccessErrorUserFormat,
  343. 0, // Access
  344. pFile,
  345. NULL, // pAccountName
  346. NULL, // pDomainName
  347. "Could not set file owner %d",
  348. Error
  349. );
  350. if( pAbsSd ) LocalFree( pAbsSd );
  351. if( pAbsAcl ) LocalFree( pAbsAcl );
  352. if( pAbsSacl ) LocalFree( pAbsSacl );
  353. if( pAbsOwner ) LocalFree( pAbsOwner );
  354. if( pAbsGroup ) LocalFree( pAbsGroup );
  355. LocalFree( pSelfSd );
  356. DBGPRINT(( "leaving xxxSetFileSecurity(9); returning=FileAccessError\n" ));
  357. return( FileAccessError );
  358. }
  359. else {
  360. Info |= OWNER_SECURITY_INFORMATION;
  361. }
  362. }
  363. #ifdef notdef // WWM - don't worry about the group
  364. if( pAbsGroup && !IsAllowSid( pAbsGroup ) ) {
  365. // Make the local admin group own it
  366. rc = SetSecurityDescriptorGroup(
  367. pAbsSd,
  368. GetLocalAdminGroupSid(),
  369. FALSE // Not defaulted
  370. );
  371. if( !rc ) {
  372. Error = GetLastError();
  373. ReportFileResult(
  374. FileAccessErrorUserFormat,
  375. 0, // Access
  376. pFile,
  377. NULL, // pAccountName
  378. NULL, // pDomainName
  379. "Could not set file group %d",
  380. Error
  381. );
  382. if( pAbsSd ) LocalFree( pAbsSd );
  383. if( pAbsAcl ) LocalFree( pAbsAcl );
  384. if( pAbsSacl ) LocalFree( pAbsSacl );
  385. if( pAbsOwner ) LocalFree( pAbsOwner );
  386. if( pAbsGroup ) LocalFree( pAbsGroup );
  387. LocalFree( pSelfSd );
  388. DBGPRINT(( "leaving xxxSetFileSecurity(10); returning=FileAccessError\n" ));
  389. return( FileAccessError );
  390. }
  391. else {
  392. Info |= GROUP_SECURITY_INFORMATION;
  393. }
  394. }
  395. #endif
  396. //
  397. // Now set the new security descriptor onto the file
  398. //
  399. rc = SetFileSecurityW(
  400. pFile,
  401. Info,
  402. pAbsSd
  403. );
  404. if( !rc ) {
  405. Error = GetLastError();
  406. ReportFileResult(
  407. FileAccessErrorUserFormat,
  408. 0, // Access
  409. pFile,
  410. NULL, // pAccountName
  411. NULL, // pDomainName
  412. "Could not set new Security Descriptor %d",
  413. Error
  414. );
  415. if( pAbsSd ) LocalFree( pAbsSd );
  416. if( pAbsAcl ) LocalFree( pAbsAcl );
  417. if( pAbsSacl ) LocalFree( pAbsSacl );
  418. if( pAbsOwner ) LocalFree( pAbsOwner );
  419. if( pAbsGroup ) LocalFree( pAbsGroup );
  420. LocalFree( pSelfSd );
  421. DBGPRINT(( "leaving xxxSetFileSecurity(11); returning=FileAccessError\n" ));
  422. return( FileAccessError );
  423. }
  424. if( pAbsSd ) LocalFree( pAbsSd );
  425. if( pAbsAcl ) LocalFree( pAbsAcl );
  426. if( pAbsSacl ) LocalFree( pAbsSacl );
  427. if( pAbsOwner ) LocalFree( pAbsOwner );
  428. if( pAbsGroup ) LocalFree( pAbsGroup );
  429. LocalFree( pSelfSd );
  430. DBGPRINT(( "leaving xxxSetFileSecurity(12); returning=FileOk\n" ));
  431. return( FileOk );
  432. }
  433. #ifdef notdef
  434. //
  435. // Get the owner SID
  436. //
  437. rc = GetSecurityDescriptorOwner(
  438. pSelfSd,
  439. &Owner,
  440. &OwnerDefaulted
  441. );
  442. if( !rc ) {
  443. // No owner info
  444. Owner = NULL;
  445. }
  446. //
  447. // Get the group SID
  448. //
  449. rc = GetSecurityDescriptorGroup(
  450. pSelfSd,
  451. &Group,
  452. &GroupDefaulted
  453. );
  454. if( !rc ) {
  455. // No group info
  456. Group = NULL;
  457. }
  458. #endif