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.

1853 lines
54 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. obse.c
  5. Abstract:
  6. Object Security API calls
  7. Author:
  8. Steve Wood (stevewo) 31-Mar-1989
  9. Revision History:
  10. --*/
  11. #include "obp.h"
  12. #if defined(ALLOC_PRAGMA)
  13. #pragma alloc_text(PAGE,NtSetSecurityObject)
  14. #pragma alloc_text(PAGE,NtQuerySecurityObject)
  15. #pragma alloc_text(PAGE,ObAssignObjectSecurityDescriptor)
  16. #pragma alloc_text(PAGE,ObAssignSecurity)
  17. #pragma alloc_text(PAGE,ObCheckCreateObjectAccess)
  18. #pragma alloc_text(PAGE,ObCheckObjectAccess)
  19. #pragma alloc_text(PAGE,ObpCheckObjectReference)
  20. #pragma alloc_text(PAGE,ObpCheckTraverseAccess)
  21. #pragma alloc_text(PAGE,ObGetObjectSecurity)
  22. #pragma alloc_text(PAGE,ObSetSecurityDescriptorInfo)
  23. #pragma alloc_text(PAGE,ObQuerySecurityDescriptorInfo)
  24. #pragma alloc_text(PAGE,ObReleaseObjectSecurity)
  25. #pragma alloc_text(PAGE,ObValidateSecurityQuota)
  26. #pragma alloc_text(PAGE,ObpValidateAccessMask)
  27. #pragma alloc_text(PAGE,ObSetSecurityObjectByPointer)
  28. #endif
  29. ULONG ObpDefaultSecurityDescriptorLength = 256;
  30. NTSTATUS
  31. NtSetSecurityObject (
  32. IN HANDLE Handle,
  33. IN SECURITY_INFORMATION SecurityInformation,
  34. IN PSECURITY_DESCRIPTOR SecurityDescriptor
  35. )
  36. /*++
  37. Routine Description:
  38. This routine is used to invoke an object's security routine. It
  39. is used to set the object's security state.
  40. Arguments:
  41. Handle - Supplies the handle for the object being modified
  42. SecurityInformation - Indicates the type of information we are
  43. interested in setting. e.g., owner, group, dacl, or sacl.
  44. SecurityDescriptor - Supplies the security descriptor for the
  45. object being modified.
  46. Return Value:
  47. An appropriate NTSTATUS value
  48. --*/
  49. {
  50. NTSTATUS Status;
  51. PVOID Object;
  52. ACCESS_MASK DesiredAccess;
  53. OBJECT_HANDLE_INFORMATION HandleInformation;
  54. KPROCESSOR_MODE RequestorMode;
  55. SECURITY_DESCRIPTOR_RELATIVE *CapturedDescriptor;
  56. PAGED_CODE();
  57. //
  58. // Make sure the passed security descriptor is really there.
  59. // SeCaptureSecurityDescriptor doesn't mind being passed a NULL
  60. // SecurityDescriptor, and will just return NULL back.
  61. //
  62. if (!ARGUMENT_PRESENT( SecurityDescriptor )) {
  63. return( STATUS_ACCESS_VIOLATION );
  64. }
  65. //
  66. // Establish the accesses needed to the object based upon the
  67. // security information being modified.
  68. //
  69. SeSetSecurityAccessMask( SecurityInformation, &DesiredAccess );
  70. Status = ObReferenceObjectByHandle( Handle,
  71. DesiredAccess,
  72. NULL,
  73. RequestorMode = KeGetPreviousMode(),
  74. &Object,
  75. &HandleInformation );
  76. if (NT_SUCCESS( Status )) {
  77. //
  78. // Probe and capture the input security descriptor, and return
  79. // right away if it is ill-formed.
  80. //
  81. // Because the security descriptor is always captured the returned
  82. // security descriptor is in self-relative format.
  83. //
  84. Status = SeCaptureSecurityDescriptor( SecurityDescriptor,
  85. RequestorMode,
  86. PagedPool,
  87. TRUE,
  88. (PSECURITY_DESCRIPTOR *)&CapturedDescriptor );
  89. if (NT_SUCCESS( Status )) {
  90. //
  91. // Now check for a valid combination of what the user wants to set
  92. // and what was supplied in the input security descriptor. If the
  93. // caller wants to set the owner then the owner field of the
  94. // security descriptor better not be null, likewise for the group
  95. // setting. If anything is missing we'll return and error.
  96. //
  97. ASSERT(CapturedDescriptor->Control & SE_SELF_RELATIVE);
  98. if (((SecurityInformation & OWNER_SECURITY_INFORMATION) &&
  99. (CapturedDescriptor->Owner == 0))
  100. ||
  101. ((SecurityInformation & GROUP_SECURITY_INFORMATION) &&
  102. (CapturedDescriptor->Group == 0))) {
  103. SeReleaseSecurityDescriptor( (PSECURITY_DESCRIPTOR)CapturedDescriptor,
  104. RequestorMode,
  105. TRUE );
  106. ObDereferenceObject( Object );
  107. return( STATUS_INVALID_SECURITY_DESCR );
  108. }
  109. Status = ObSetSecurityObjectByPointer( Object,
  110. SecurityInformation,
  111. CapturedDescriptor );
  112. SeReleaseSecurityDescriptor( (PSECURITY_DESCRIPTOR)CapturedDescriptor,
  113. RequestorMode,
  114. TRUE );
  115. }
  116. ObDereferenceObject( Object );
  117. }
  118. return( Status );
  119. }
  120. NTSTATUS
  121. ObSetSecurityObjectByPointer (
  122. IN PVOID Object,
  123. IN SECURITY_INFORMATION SecurityInformation,
  124. IN PSECURITY_DESCRIPTOR SecurityDescriptor
  125. )
  126. /*++
  127. Routine Description:
  128. This routine is used to invoke an object's security routine. It
  129. is used to set the object's security state.
  130. This routine is accessible only to the kernel and assumes that all
  131. necessary validation of parameters has been done by the caller.
  132. Arguments:
  133. Object - Supplies the pointer for the object being modified
  134. SecurityInformation - Indicates the type of information we are
  135. interested in setting. e.g., owner, group, dacl, or sacl.
  136. SecurityDescriptor - Supplies the security descriptor for the
  137. object being modified.
  138. Return Value:
  139. An appropriate NTSTATUS value
  140. --*/
  141. {
  142. NTSTATUS Status;
  143. POBJECT_HEADER ObjectHeader;
  144. POBJECT_TYPE ObjectType;
  145. PAGED_CODE();
  146. // DbgPrint("ObSetSecurityObjectByPointer called for object %#08lx with info "
  147. // "%x and descriptor %#08lx\n",
  148. // Object, SecurityInformation, SecurityDescriptor);
  149. //
  150. // Map the object body to an object header and the corresponding
  151. // object type
  152. //
  153. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  154. ObjectType = ObjectHeader->Type;
  155. //
  156. // Make sure the passed security descriptor is really there.
  157. //
  158. ASSERT(ARGUMENT_PRESENT( SecurityDescriptor ));
  159. //
  160. // Now invoke the security procedure call back to set the security
  161. // descriptor for the object
  162. //
  163. Status = (ObjectType->TypeInfo.SecurityProcedure)
  164. ( Object,
  165. SetSecurityDescriptor,
  166. &SecurityInformation,
  167. SecurityDescriptor,
  168. NULL,
  169. &ObjectHeader->SecurityDescriptor,
  170. ObjectType->TypeInfo.PoolType,
  171. &ObjectType->TypeInfo.GenericMapping );
  172. // DbgPrint("ObSetSecurityObjectByPointer: object security routine returned "
  173. // "%#08lx\n", Status);
  174. return( Status );
  175. }
  176. NTSTATUS
  177. NtQuerySecurityObject (
  178. IN HANDLE Handle,
  179. IN SECURITY_INFORMATION SecurityInformation,
  180. OUT PSECURITY_DESCRIPTOR SecurityDescriptor,
  181. IN ULONG Length,
  182. OUT PULONG LengthNeeded
  183. )
  184. /*++
  185. Routine Description:
  186. This routine is used to query the security descriptor for an
  187. object.
  188. Arguments:
  189. Handle - Supplies the handle for the object being investigated
  190. SecurityInformation - Indicates the type of information we are
  191. interested in getting. e.g., owner, group, dacl, or sacl.
  192. SecurityDescriptor - Supplies a pointer to where the information
  193. should be returned
  194. Length - Supplies the size, in bytes, of the output buffer
  195. LengthNeeded - Receives the length, in bytes, needed to store
  196. the output security descriptor
  197. Return Value:
  198. An appropriate NTSTATUS value
  199. --*/
  200. {
  201. NTSTATUS Status;
  202. PVOID Object;
  203. ACCESS_MASK DesiredAccess;
  204. OBJECT_HANDLE_INFORMATION HandleInformation;
  205. KPROCESSOR_MODE RequestorMode;
  206. POBJECT_HEADER ObjectHeader;
  207. POBJECT_TYPE ObjectType;
  208. PAGED_CODE();
  209. //
  210. // Probe output parameters
  211. //
  212. RequestorMode = KeGetPreviousMode();
  213. if (RequestorMode != KernelMode) {
  214. try {
  215. ProbeForWriteUlong( LengthNeeded );
  216. ProbeForWrite( SecurityDescriptor, Length, sizeof(ULONG) );
  217. } except(EXCEPTION_EXECUTE_HANDLER) {
  218. return GetExceptionCode();
  219. }
  220. }
  221. //
  222. // Establish the accesses needed to the object based upon the
  223. // security information being queried
  224. //
  225. SeQuerySecurityAccessMask( SecurityInformation, &DesiredAccess );
  226. Status = ObReferenceObjectByHandle( Handle,
  227. DesiredAccess,
  228. NULL,
  229. RequestorMode,
  230. &Object,
  231. &HandleInformation );
  232. if (!NT_SUCCESS( Status )) {
  233. return( Status );
  234. }
  235. //
  236. // Map the object body to an object header and the corresponding
  237. // object type
  238. //
  239. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  240. ObjectType = ObjectHeader->Type;
  241. //
  242. // Invoke the object type's security callback routine to query
  243. // the object. This routine is assumed to have a try-except around
  244. // the setting of the output security descriptor
  245. //
  246. Status = (ObjectType->TypeInfo.SecurityProcedure)( Object,
  247. QuerySecurityDescriptor,
  248. &SecurityInformation,
  249. SecurityDescriptor,
  250. &Length,
  251. &ObjectHeader->SecurityDescriptor,
  252. ObjectType->TypeInfo.PoolType,
  253. &ObjectType->TypeInfo.GenericMapping );
  254. //
  255. // Indicate the length needed for the security descriptor. This
  256. // will be set even if the callback failed so the caller will know
  257. // the number of bytes necessary
  258. //
  259. try {
  260. *LengthNeeded = Length;
  261. } except(EXCEPTION_EXECUTE_HANDLER) {
  262. ObDereferenceObject( Object );
  263. return(GetExceptionCode());
  264. }
  265. //
  266. // And return to our caller
  267. //
  268. ObDereferenceObject( Object );
  269. return( Status );
  270. }
  271. BOOLEAN
  272. ObCheckObjectAccess (
  273. IN PVOID Object,
  274. IN OUT PACCESS_STATE AccessState,
  275. IN BOOLEAN TypeMutexLocked,
  276. IN KPROCESSOR_MODE AccessMode,
  277. OUT PNTSTATUS AccessStatus
  278. )
  279. /*++
  280. Routine Description:
  281. This routine performs access validation on the passed object. The
  282. remaining desired access mask is extracted from the AccessState
  283. parameter and passes to the appropriate security routine to perform the
  284. access check.
  285. If the access attempt is successful, SeAccessCheck returns a mask
  286. containing the granted accesses. The bits in this mask are turned
  287. on in the PreviouslyGrantedAccess field of the AccessState, and
  288. are turned off in the RemainingDesiredAccess field.
  289. Arguments:
  290. Object - The object being examined.
  291. AccessState - The ACCESS_STATE structure containing accumulated
  292. information about the current attempt to gain access to the object.
  293. TypeMutexLocked - Indicates whether the type mutex for this object's
  294. type is locked. The type mutex is used to protect the object's
  295. security descriptor from being modified while it is being accessed.
  296. AccessMode - The previous processor mode.
  297. AccessStatus - Pointer to a variable to return the status code of the
  298. access attempt. In the case of failure this status code must be
  299. propagated back to the user.
  300. Return Value:
  301. BOOLEAN - TRUE if access is allowed and FALSE otherwise
  302. --*/
  303. {
  304. ACCESS_MASK GrantedAccess = 0;
  305. BOOLEAN AccessAllowed;
  306. BOOLEAN MemoryAllocated;
  307. NTSTATUS Status;
  308. PSECURITY_DESCRIPTOR SecurityDescriptor = NULL;
  309. POBJECT_HEADER ObjectHeader;
  310. POBJECT_TYPE ObjectType;
  311. PPRIVILEGE_SET Privileges = NULL;
  312. PAGED_CODE();
  313. UNREFERENCED_PARAMETER (TypeMutexLocked);
  314. //
  315. // Map the object body to an object header and the
  316. // corresponding object type
  317. //
  318. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  319. ObjectType = ObjectHeader->Type;
  320. //
  321. // Obtain the object's security descriptor
  322. //
  323. Status = ObGetObjectSecurity( Object,
  324. &SecurityDescriptor,
  325. &MemoryAllocated );
  326. //
  327. // If we failed in getting the security descriptor then
  328. // put the object type lock back where it was and return
  329. // the error back to our caller
  330. //
  331. if (!NT_SUCCESS( Status )) {
  332. *AccessStatus = Status;
  333. return( FALSE );
  334. } else {
  335. //
  336. // Otherwise we've been successful at getting the
  337. // object's security descriptor, but now make sure
  338. // it is not null.
  339. if (SecurityDescriptor == NULL) {
  340. *AccessStatus = Status;
  341. return(TRUE);
  342. }
  343. }
  344. //
  345. // We have a non-null security descriptor so now
  346. // lock the caller's tokens until after auditing has been
  347. // performed.
  348. //
  349. SeLockSubjectContext( &AccessState->SubjectSecurityContext );
  350. //
  351. // Do the access check, and if we have some privileges then
  352. // put those in the access state too.
  353. //
  354. AccessAllowed = SeAccessCheck( SecurityDescriptor,
  355. &AccessState->SubjectSecurityContext,
  356. TRUE, // Tokens are locked
  357. AccessState->RemainingDesiredAccess,
  358. AccessState->PreviouslyGrantedAccess,
  359. &Privileges,
  360. &ObjectType->TypeInfo.GenericMapping,
  361. AccessMode,
  362. &GrantedAccess,
  363. AccessStatus );
  364. if (Privileges != NULL) {
  365. Status = SeAppendPrivileges( AccessState,
  366. Privileges );
  367. SeFreePrivileges( Privileges );
  368. }
  369. //
  370. // If we were granted access then set that fact into
  371. // what we've been granted and remove it from what remains
  372. // to be granted.
  373. //
  374. if (AccessAllowed) {
  375. AccessState->PreviouslyGrantedAccess |= GrantedAccess;
  376. AccessState->RemainingDesiredAccess &= ~(GrantedAccess | MAXIMUM_ALLOWED);
  377. }
  378. //
  379. // Audit the attempt to open the object, audit
  380. // the creation of its handle later.
  381. //
  382. if ( SecurityDescriptor != NULL ) {
  383. SeOpenObjectAuditAlarm( &ObjectType->Name,
  384. Object,
  385. NULL, // AbsoluteObjectName
  386. SecurityDescriptor,
  387. AccessState,
  388. FALSE, // ObjectCreated (FALSE, only open here)
  389. AccessAllowed,
  390. AccessMode,
  391. &AccessState->GenerateOnClose );
  392. }
  393. SeUnlockSubjectContext( &AccessState->SubjectSecurityContext );
  394. //
  395. // Free the security descriptor before returning to
  396. // our caller
  397. //
  398. ObReleaseObjectSecurity( SecurityDescriptor,
  399. MemoryAllocated );
  400. return( AccessAllowed );
  401. }
  402. BOOLEAN
  403. ObpCheckObjectReference (
  404. IN PVOID Object,
  405. IN OUT PACCESS_STATE AccessState,
  406. IN BOOLEAN TypeMutexLocked,
  407. IN KPROCESSOR_MODE AccessMode,
  408. OUT PNTSTATUS AccessStatus
  409. )
  410. /*++
  411. Routine Description:
  412. The routine performs access validation on the passed object. The
  413. remaining desired access mask is extracted from the AccessState
  414. parameter and passes to the appropriate security routine to
  415. perform the access check.
  416. If the access attempt is successful, SeAccessCheck returns a mask
  417. containing the granted accesses. The bits in this mask are turned
  418. on in the PreviouslyGrantedAccess field of the AccessState, and
  419. are turned off in the RemainingDesiredAccess field.
  420. This routine differs from ObpCheckObjectAccess in that it calls
  421. a different audit routine.
  422. Arguments:
  423. Object - The object being examined.
  424. AccessState - The ACCESS_STATE structure containing accumulated
  425. information about the current attempt to gain access to the object.
  426. TypeMutexLocked - Indicates whether the type mutex for this object's
  427. type is locked. The type mutex is used to protect the object's
  428. security descriptor from being modified while it is being accessed.
  429. AccessMode - The previous processor mode.
  430. AccessStatus - Pointer to a variable to return the status code of the
  431. access attempt. In the case of failure this status code must be
  432. propagated back to the user.
  433. Return Value:
  434. BOOLEAN - TRUE if access is allowed and FALSE otherwise
  435. --*/
  436. {
  437. BOOLEAN AccessAllowed;
  438. ACCESS_MASK GrantedAccess = 0;
  439. BOOLEAN MemoryAllocated;
  440. PSECURITY_DESCRIPTOR SecurityDescriptor;
  441. NTSTATUS Status;
  442. POBJECT_HEADER ObjectHeader;
  443. POBJECT_TYPE ObjectType;
  444. PPRIVILEGE_SET Privileges = NULL;
  445. PAGED_CODE();
  446. UNREFERENCED_PARAMETER (TypeMutexLocked);
  447. //
  448. // Map the object body to an object header and the
  449. // corresponding object type
  450. //
  451. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  452. ObjectType = ObjectHeader->Type;
  453. //
  454. // Obtain the object's security descriptor
  455. //
  456. Status = ObGetObjectSecurity( Object,
  457. &SecurityDescriptor,
  458. &MemoryAllocated );
  459. //
  460. // If we failed in getting the security descriptor then
  461. // put the object type lock back where it was and return
  462. // the error back to our caller
  463. //
  464. if (!NT_SUCCESS( Status )) {
  465. *AccessStatus = Status;
  466. return( FALSE );
  467. }
  468. //
  469. // Lock the caller's tokens until after auditing has been
  470. // performed.
  471. //
  472. SeLockSubjectContext( &AccessState->SubjectSecurityContext );
  473. //
  474. // Do the access check, and if we have some privileges then
  475. // put those in the access state too.
  476. //
  477. AccessAllowed = SeAccessCheck( SecurityDescriptor,
  478. &AccessState->SubjectSecurityContext,
  479. TRUE, // Tokens are locked
  480. AccessState->RemainingDesiredAccess,
  481. AccessState->PreviouslyGrantedAccess,
  482. &Privileges,
  483. &ObjectType->TypeInfo.GenericMapping,
  484. AccessMode,
  485. &GrantedAccess,
  486. AccessStatus );
  487. if (AccessAllowed) {
  488. AccessState->PreviouslyGrantedAccess |= GrantedAccess;
  489. AccessState->RemainingDesiredAccess &= ~GrantedAccess;
  490. }
  491. //
  492. // If we have a security descriptor then call the security routine
  493. // to audit this reference and then unlock the caller's token
  494. //
  495. if ( SecurityDescriptor != NULL ) {
  496. SeObjectReferenceAuditAlarm( &AccessState->OperationID,
  497. Object,
  498. SecurityDescriptor,
  499. &AccessState->SubjectSecurityContext,
  500. AccessState->RemainingDesiredAccess | AccessState->PreviouslyGrantedAccess,
  501. ((PAUX_ACCESS_DATA)(AccessState->AuxData))->PrivilegesUsed,
  502. AccessAllowed,
  503. AccessMode );
  504. }
  505. SeUnlockSubjectContext( &AccessState->SubjectSecurityContext );
  506. //
  507. // Finally free the security descriptor
  508. // and return to our caller
  509. //
  510. ObReleaseObjectSecurity( SecurityDescriptor,
  511. MemoryAllocated );
  512. return( AccessAllowed );
  513. }
  514. BOOLEAN
  515. ObpCheckTraverseAccess (
  516. IN PVOID DirectoryObject,
  517. IN ACCESS_MASK TraverseAccess,
  518. IN PACCESS_STATE AccessState,
  519. IN BOOLEAN TypeMutexLocked,
  520. IN KPROCESSOR_MODE PreviousMode,
  521. OUT PNTSTATUS AccessStatus
  522. )
  523. /*++
  524. Routine Description:
  525. This routine checks for traverse access to the given directory object.
  526. Note that the contents of the AccessState structure are not
  527. modified, since it is assumed that this access check is incidental
  528. to another access operation.
  529. Arguments:
  530. DirectoryObject - The object body of the object being examined.
  531. TraverseAccess - The desired access to the object, most likely DIRECTORY
  532. TRAVERSE access.
  533. AccessState - Checks for traverse access will typically be incidental
  534. to some other access attempt. Information on the current state of
  535. that access attempt is required so that the constituent access
  536. attempts may be associated with each other in the audit log.
  537. TypeMutexLocked - Indicates whether the type mutex for this object's
  538. type is locked. The type mutex is used to protect the object's
  539. security descriptor from being modified while it is being accessed.
  540. PreviousMode - The previous processor mode.
  541. AccessStatus - Pointer to a variable to return the status code of the
  542. access attempt. In the case of failure this status code must be
  543. propagated back to the user.
  544. Return Value:
  545. BOOLEAN - TRUE if access is allowed and FALSE otherwise. AccessStatus
  546. contains the status code to be passed back to the caller. It is not
  547. correct to simply pass back STATUS_ACCESS_DENIED, since this will have
  548. to change with the advent of mandatory access control.
  549. --*/
  550. {
  551. BOOLEAN AccessAllowed;
  552. ACCESS_MASK GrantedAccess = 0;
  553. PSECURITY_DESCRIPTOR SecurityDescriptor;
  554. BOOLEAN MemoryAllocated;
  555. NTSTATUS Status;
  556. POBJECT_HEADER ObjectHeader;
  557. POBJECT_TYPE ObjectType;
  558. PPRIVILEGE_SET Privileges = NULL;
  559. PAGED_CODE();
  560. UNREFERENCED_PARAMETER (TypeMutexLocked);
  561. //
  562. // Map the object body to an object header and corresponding
  563. // object type
  564. //
  565. ObjectHeader = OBJECT_TO_OBJECT_HEADER( DirectoryObject );
  566. ObjectType = ObjectHeader->Type;
  567. //
  568. // Obtain the object's security descriptor and make it was
  569. // successful
  570. //
  571. Status = ObGetObjectSecurity( DirectoryObject,
  572. &SecurityDescriptor,
  573. &MemoryAllocated );
  574. if (!NT_SUCCESS( Status )) {
  575. *AccessStatus = Status;
  576. return( FALSE );
  577. }
  578. if (!SeFastTraverseCheck( SecurityDescriptor,
  579. AccessState,
  580. DIRECTORY_TRAVERSE,
  581. PreviousMode )) {
  582. //
  583. // SeFastTraverseCheck could be modified to tell us that
  584. // no one has any access to this directory. However,
  585. // we're going to have to fail this entire call if
  586. // that is the case, so we really don't need to worry
  587. // all that much about making it blindingly fast.
  588. //
  589. // The world does not have traverse access and we have
  590. // the client's access state so lock down the client's
  591. // token and then do the access check, appending privileges
  592. // if present. The access check will give the answer
  593. // we return back to our caller
  594. //
  595. SeLockSubjectContext( &AccessState->SubjectSecurityContext );
  596. AccessAllowed = SeAccessCheck( SecurityDescriptor,
  597. &AccessState->SubjectSecurityContext,
  598. TRUE, // Tokens are locked
  599. TraverseAccess,
  600. 0,
  601. &Privileges,
  602. &ObjectType->TypeInfo.GenericMapping,
  603. PreviousMode,
  604. &GrantedAccess,
  605. AccessStatus );
  606. if (Privileges != NULL) {
  607. Status = SeAppendPrivileges( AccessState,
  608. Privileges );
  609. SeFreePrivileges( Privileges );
  610. }
  611. //
  612. // If the client's token is locked then now we can unlock it
  613. //
  614. SeUnlockSubjectContext( &AccessState->SubjectSecurityContext );
  615. } else {
  616. //
  617. // At this point the world has traverse access
  618. //
  619. AccessAllowed = TRUE;
  620. }
  621. //
  622. // Finally free the security descriptor
  623. // and then return to our caller
  624. //
  625. ObReleaseObjectSecurity( SecurityDescriptor,
  626. MemoryAllocated );
  627. return( AccessAllowed );
  628. }
  629. BOOLEAN
  630. ObCheckCreateObjectAccess (
  631. IN PVOID DirectoryObject,
  632. IN ACCESS_MASK CreateAccess,
  633. IN PACCESS_STATE AccessState,
  634. IN PUNICODE_STRING ComponentName,
  635. IN BOOLEAN TypeMutexLocked,
  636. IN KPROCESSOR_MODE PreviousMode,
  637. OUT PNTSTATUS AccessStatus
  638. )
  639. /*++
  640. Routine Description:
  641. This routine checks to see if we are allowed to create an object in the
  642. given directory, and performs auditing as appropriate.
  643. Arguments:
  644. DirectoryObject - The directory object being examined.
  645. CreateAccess - The access mask corresponding to create access for
  646. this directory type.
  647. AccessState - Checks for traverse access will typically be incidental
  648. to some other access attempt. Information on the current state of
  649. that access attempt is required so that the constituent access
  650. attempts may be associated with each other in the audit log.
  651. ComponentName - Pointer to a Unicode string containing the name of
  652. the object being created.
  653. TypeMutexLocked - Indicates whether the type mutex for this object's
  654. type is locked. The type mutex is used to protect the object's
  655. security descriptor from being modified while it is being accessed.
  656. PreviousMode - The previous processor mode.
  657. AccessStatus - Pointer to a variable to return the status code of the
  658. access attempt. In the case of failure this status code must be
  659. propagated back to the user.
  660. Return Value:
  661. BOOLEAN - TRUE if access is allowed and FALSE otherwise. AccessStatus
  662. contains the status code to be passed back to the caller. It is not
  663. correct to simply pass back STATUS_ACCESS_DENIED, since this will have
  664. to change with the advent of mandatory access control.
  665. --*/
  666. {
  667. BOOLEAN AccessAllowed;
  668. ACCESS_MASK GrantedAccess = 0;
  669. PSECURITY_DESCRIPTOR SecurityDescriptor;
  670. BOOLEAN MemoryAllocated;
  671. NTSTATUS Status;
  672. POBJECT_HEADER ObjectHeader;
  673. POBJECT_TYPE ObjectType;
  674. PPRIVILEGE_SET Privileges = NULL;
  675. PAGED_CODE();
  676. UNREFERENCED_PARAMETER (ComponentName);
  677. UNREFERENCED_PARAMETER (TypeMutexLocked);
  678. //
  679. // Map the object body to its object header and corresponding
  680. // object type
  681. //
  682. ObjectHeader = OBJECT_TO_OBJECT_HEADER( DirectoryObject );
  683. ObjectType = ObjectHeader->Type;
  684. //
  685. // Obtain the object's security descriptor and make it was
  686. // successful
  687. //
  688. Status = ObGetObjectSecurity( DirectoryObject,
  689. &SecurityDescriptor,
  690. &MemoryAllocated );
  691. if (!NT_SUCCESS( Status )) {
  692. *AccessStatus = Status;
  693. return( FALSE );
  694. }
  695. //
  696. // lock the caller's tokens until after auditing has been
  697. // performed.
  698. //
  699. SeLockSubjectContext( &AccessState->SubjectSecurityContext );
  700. //
  701. // if we have a security descriptor then do an access
  702. // check to see if access is allowed and set in the
  703. // privileges if necessary
  704. //
  705. if (SecurityDescriptor != NULL) {
  706. AccessAllowed = SeAccessCheck( SecurityDescriptor,
  707. &AccessState->SubjectSecurityContext,
  708. TRUE, // Tokens are locked
  709. CreateAccess,
  710. 0,
  711. &Privileges,
  712. &ObjectType->TypeInfo.GenericMapping,
  713. PreviousMode,
  714. &GrantedAccess,
  715. AccessStatus );
  716. if (Privileges != NULL) {
  717. Status = SeAppendPrivileges( AccessState,
  718. Privileges );
  719. SeFreePrivileges( Privileges );
  720. }
  721. //
  722. // This is wrong, but leave for reference.
  723. //
  724. // if (AccessAllowed) {
  725. //
  726. // AccessState->PreviouslyGrantedAccess |= GrantedAccess;
  727. // AccessState->RemainingDesiredAccess &= ~GrantedAccess;
  728. // }
  729. //
  730. } else {
  731. //
  732. // At this point there is not a security descriptor
  733. // so we'll assume access is allowed
  734. //
  735. AccessAllowed = TRUE;
  736. }
  737. //
  738. // Free the caller's token and if the caller didn't have the
  739. // object type locked we need to free it.
  740. //
  741. SeUnlockSubjectContext( &AccessState->SubjectSecurityContext );
  742. //
  743. // Finally free the security descriptor
  744. // and return to our caller
  745. //
  746. ObReleaseObjectSecurity( SecurityDescriptor,
  747. MemoryAllocated );
  748. return( AccessAllowed );
  749. }
  750. NTSTATUS
  751. ObAssignObjectSecurityDescriptor (
  752. IN PVOID Object,
  753. IN PSECURITY_DESCRIPTOR SecurityDescriptor OPTIONAL,
  754. IN POOL_TYPE PoolType // This field is currently ignored.
  755. )
  756. /*++
  757. Routine Description:
  758. Takes a pointer to an object and sets the SecurityDescriptor field
  759. in the object's header.
  760. Arguments:
  761. Object - Supplies a pointer to the object
  762. SecurityDescriptor - Supplies a pointer to the security descriptor
  763. to be assigned to the object. This pointer may be null if there
  764. is no security on the object.
  765. PoolType - Supplies the type of pool memory used to allocate the
  766. security descriptor. This field is currently ignored.
  767. Return Value:
  768. An appropriate NTSTATUS value.
  769. --*/
  770. {
  771. NTSTATUS Status;
  772. PSECURITY_DESCRIPTOR OutputSecurityDescriptor;
  773. POBJECT_HEADER ObjectHeader;
  774. PAGED_CODE();
  775. UNREFERENCED_PARAMETER (PoolType);
  776. //
  777. // If the security descriptor isn't supplied then we set the
  778. // object header's security descriptor to null and return
  779. // to our caller
  780. //
  781. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  782. if (!ARGUMENT_PRESENT(SecurityDescriptor)) {
  783. ExFastRefInitialize ((PEX_FAST_REF) &ObjectHeader->SecurityDescriptor, NULL);
  784. return( STATUS_SUCCESS );
  785. }
  786. //
  787. // Log the new security descriptor into our security database and
  788. // get back the real security descriptor to use
  789. //
  790. Status = ObLogSecurityDescriptor( SecurityDescriptor,
  791. &OutputSecurityDescriptor,
  792. ExFastRefGetAdditionalReferenceCount () + 1 );
  793. //
  794. // If we've been successful so far then set the object's
  795. // security descriptor to the newly allocated one.
  796. //
  797. if (NT_SUCCESS(Status)) {
  798. ExFreePool (SecurityDescriptor);
  799. ASSERT (OutputSecurityDescriptor);
  800. __assume (OutputSecurityDescriptor);
  801. //
  802. // Initialize a fast reference structure with zero additional references
  803. //
  804. ExFastRefInitialize ((PEX_FAST_REF) &ObjectHeader->SecurityDescriptor, OutputSecurityDescriptor);
  805. }
  806. //
  807. // And return to our caller
  808. //
  809. return( Status );
  810. }
  811. NTSTATUS
  812. ObGetObjectSecurity (
  813. IN PVOID Object,
  814. OUT PSECURITY_DESCRIPTOR *SecurityDescriptor,
  815. OUT PBOOLEAN MemoryAllocated
  816. )
  817. /*++
  818. Routine Description:
  819. Given an object, this routine will find its security descriptor.
  820. It will do this by calling the object's security method.
  821. It is possible for an object not to have a security descriptor
  822. at all. Unnamed objects such as events that can only be referenced
  823. by a handle are an example of an object that does not have a
  824. security descriptor.
  825. Arguments:
  826. Object - Supplies the object body being queried.
  827. SecurityDescriptor - Returns a pointer to the object's security
  828. descriptor.
  829. MemoryAllocated - indicates whether we had to allocate pool
  830. memory to hold the security descriptor or not. This should
  831. be passed back into ObReleaseObjectSecurity.
  832. Return Value:
  833. STATUS_SUCCESS - The operation was successful. Note that the
  834. operation may be successful and still return a NULL security
  835. descriptor.
  836. STATUS_INSUFFICIENT_RESOURCES - Insufficient memory was available
  837. to satisfy the request.
  838. --*/
  839. {
  840. SECURITY_INFORMATION SecurityInformation;
  841. ULONG Length = ObpDefaultSecurityDescriptorLength;
  842. NTSTATUS Status;
  843. POBJECT_TYPE ObjectType;
  844. POBJECT_HEADER ObjectHeader;
  845. #if DBG
  846. KIRQL SaveIrql;
  847. #endif
  848. PAGED_CODE();
  849. //
  850. // Map the object body to its object header and corresponding
  851. // object type
  852. //
  853. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  854. ObjectType = ObjectHeader->Type;
  855. //
  856. // If the object is one that uses the default object method,
  857. // its security descriptor is contained in ob's security
  858. // descriptor cache.
  859. //
  860. // Reference it so that it doesn't go away out from under us.
  861. //
  862. if (ObpCentralizedSecurity(ObjectType)) {
  863. *SecurityDescriptor = ObpReferenceSecurityDescriptor( ObjectHeader );
  864. *MemoryAllocated = FALSE;
  865. return( STATUS_SUCCESS );
  866. }
  867. //
  868. // Request a complete security descriptor
  869. //
  870. SecurityInformation = OWNER_SECURITY_INFORMATION |
  871. GROUP_SECURITY_INFORMATION |
  872. DACL_SECURITY_INFORMATION |
  873. SACL_SECURITY_INFORMATION;
  874. //
  875. // We don't know exactly how large is the SD, but we try with the largest
  876. // size we get so far. In general the SD will be released after
  877. // the access is checked. It shouldn't be then a problem of an extra pool usage
  878. // because this oversizing
  879. //
  880. *SecurityDescriptor = ExAllocatePoolWithTag( PagedPool, Length, 'qSbO' );
  881. if (*SecurityDescriptor == NULL) {
  882. return( STATUS_INSUFFICIENT_RESOURCES );
  883. }
  884. *MemoryAllocated = TRUE;
  885. //
  886. // The security method will return an absolute format
  887. // security descriptor that just happens to be in a self
  888. // contained buffer (not to be confused with a self-relative
  889. // security descriptor).
  890. //
  891. ObpBeginTypeSpecificCallOut( SaveIrql );
  892. Status = (*ObjectType->TypeInfo.SecurityProcedure)( Object,
  893. QuerySecurityDescriptor,
  894. &SecurityInformation,
  895. *SecurityDescriptor,
  896. &Length,
  897. &ObjectHeader->SecurityDescriptor,
  898. ObjectType->TypeInfo.PoolType,
  899. &ObjectType->TypeInfo.GenericMapping );
  900. ObpEndTypeSpecificCallOut( SaveIrql, "Security", ObjectType, Object );
  901. if (Status == STATUS_BUFFER_TOO_SMALL) {
  902. //
  903. // The SD is larger than we tried first time. We need to allocate an other
  904. // buffer and try again with this size
  905. //
  906. ExFreePool( *SecurityDescriptor );
  907. *MemoryAllocated = FALSE;
  908. //
  909. // Save the new largest size
  910. //
  911. ObpDefaultSecurityDescriptorLength = Length;
  912. // DbgPrint( "ObpDefaultSecurityDescriptorLength increased to %ld\n",
  913. // ObpDefaultSecurityDescriptorLength);
  914. //
  915. // Now that we know how large the security descriptor is we
  916. // can allocate space for it
  917. //
  918. *SecurityDescriptor = ExAllocatePoolWithTag( PagedPool, Length, 'qSbO' );
  919. if (*SecurityDescriptor == NULL) {
  920. return( STATUS_INSUFFICIENT_RESOURCES );
  921. }
  922. *MemoryAllocated = TRUE;
  923. //
  924. // The security method will return an absolute format
  925. // security descriptor that just happens to be in a self
  926. // contained buffer (not to be confused with a self-relative
  927. // security descriptor).
  928. //
  929. ObpBeginTypeSpecificCallOut( SaveIrql );
  930. Status = (*ObjectType->TypeInfo.SecurityProcedure)( Object,
  931. QuerySecurityDescriptor,
  932. &SecurityInformation,
  933. *SecurityDescriptor,
  934. &Length,
  935. &ObjectHeader->SecurityDescriptor,
  936. ObjectType->TypeInfo.PoolType,
  937. &ObjectType->TypeInfo.GenericMapping );
  938. ObpEndTypeSpecificCallOut( SaveIrql, "Security", ObjectType, Object );
  939. }
  940. if (!NT_SUCCESS( Status )) {
  941. ExFreePool( *SecurityDescriptor );
  942. *MemoryAllocated = FALSE;
  943. }
  944. return( Status );
  945. }
  946. VOID
  947. ObReleaseObjectSecurity (
  948. IN PSECURITY_DESCRIPTOR SecurityDescriptor,
  949. IN BOOLEAN MemoryAllocated
  950. )
  951. /*++
  952. Routine Description:
  953. This function will free up any memory associated with a queried
  954. security descriptor. This undoes the function ObGetObjectSecurity
  955. Arguments:
  956. SecurityDescriptor - Supplies a pointer to the security descriptor
  957. to be freed.
  958. MemoryAllocated - Supplies whether or not we should free the
  959. memory pointed to by SecurityDescriptor.
  960. Return Value:
  961. None.
  962. --*/
  963. {
  964. PAGED_CODE();
  965. //
  966. // Check if there is a security descriptor to actually free
  967. //
  968. if ( SecurityDescriptor != NULL ) {
  969. //
  970. // If ObGetObjectSecurity allocated memory then we
  971. // need to free it. Otherwise what the earlier routine did
  972. // was reference the object to keep the security descriptor
  973. // to keep it from going away
  974. //
  975. if (MemoryAllocated) {
  976. ExFreePool( SecurityDescriptor );
  977. } else {
  978. ObDereferenceSecurityDescriptor( SecurityDescriptor, 1);
  979. }
  980. }
  981. }
  982. NTSTATUS
  983. ObValidateSecurityQuota (
  984. IN PVOID Object,
  985. IN ULONG NewSize
  986. )
  987. /*++
  988. Routine Description:
  989. This routine will check to see if the new security information
  990. is larger than is allowed by the object's pre-allocated quota.
  991. Arguments:
  992. Object - Supplies a pointer to the object whose information is to be
  993. modified.
  994. NewSize - Supplies the size of the proposed new security
  995. information.
  996. Return Value:
  997. STATUS_SUCCESS - New size is within alloted quota.
  998. STATUS_QUOTA_EXCEEDED - The desired adjustment would have exceeded
  999. the permitted security quota for this object.
  1000. --*/
  1001. {
  1002. POBJECT_HEADER ObjectHeader;
  1003. POBJECT_HEADER_QUOTA_INFO QuotaInfo;
  1004. PAGED_CODE();
  1005. //
  1006. // Map the object body to its object header and corresponding
  1007. // quota information block
  1008. //
  1009. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1010. //
  1011. // If we never charged quota originaly then don't worry about it now.
  1012. //
  1013. if (ObjectHeader->QuotaBlockCharged == (PEPROCESS_QUOTA_BLOCK) 1) {
  1014. return( STATUS_SUCCESS );
  1015. }
  1016. QuotaInfo = OBJECT_HEADER_TO_QUOTA_INFO( ObjectHeader );
  1017. //
  1018. // If there isn't any quota info and the new size is greater
  1019. // then the default security quota then if the object uses
  1020. // the default value then we've exceeded quota otherwise
  1021. // let the caller get the quota
  1022. //
  1023. if ((QuotaInfo == NULL) && (NewSize > SE_DEFAULT_SECURITY_QUOTA)) {
  1024. if (!(ObjectHeader->Flags & OB_FLAG_DEFAULT_SECURITY_QUOTA)) {
  1025. return( STATUS_SUCCESS );
  1026. }
  1027. return( STATUS_QUOTA_EXCEEDED );
  1028. //
  1029. // If the quota is not null and the new size is greater than the
  1030. // allowed quota charge then if the charge is zero we grant the
  1031. // request otherwise we've exceeded quota.
  1032. //
  1033. } else if ((QuotaInfo != NULL) && (NewSize > QuotaInfo->SecurityDescriptorCharge)) {
  1034. if (QuotaInfo->SecurityDescriptorCharge == 0) {
  1035. //
  1036. // Should really charge quota here.
  1037. //
  1038. // QuotaInfo->SecurityDescriptorCharge = SeComputeSecurityQuota( NewSize );
  1039. return( STATUS_SUCCESS );
  1040. }
  1041. return( STATUS_QUOTA_EXCEEDED );
  1042. //
  1043. // Otherwise we have two cases. (1) there isn't any quota info but
  1044. // the size is within limits or (2) there is a quota info block and
  1045. // the size is within the specified security descriptor charge so
  1046. // return success to our caller
  1047. //
  1048. } else {
  1049. return( STATUS_SUCCESS );
  1050. }
  1051. }
  1052. NTSTATUS
  1053. ObAssignSecurity (
  1054. IN PACCESS_STATE AccessState,
  1055. IN PSECURITY_DESCRIPTOR ParentDescriptor OPTIONAL,
  1056. IN PVOID Object,
  1057. IN POBJECT_TYPE ObjectType
  1058. )
  1059. /*++
  1060. Routine Description:
  1061. This routine will assign a security descriptor to a newly created object.
  1062. It assumes that the AccessState parameter contains a captured security
  1063. descriptor.
  1064. Arguments:
  1065. AccessState - The AccessState containing the security information
  1066. for this object creation.
  1067. ParentDescriptor - The security descriptor from the parent object, if
  1068. available.
  1069. Object - A pointer to the object being created.
  1070. ObjectType - Supplies the type of object being created.
  1071. Return Value:
  1072. STATUS_SUCCESS - indicates the operation was successful.
  1073. STATUS_INVALID_OWNER - The owner SID provided as the owner of the
  1074. target security descriptor is not one the caller is authorized
  1075. to assign as the owner of an object.
  1076. STATUS_PRIVILEGE_NOT_HELD - The caller does not have the privilege
  1077. necessary to explicitly assign the specified system ACL.
  1078. SeSecurityPrivilege privilege is needed to explicitly assign
  1079. system ACLs to objects.
  1080. --*/
  1081. {
  1082. PSECURITY_DESCRIPTOR NewDescriptor = NULL;
  1083. NTSTATUS Status;
  1084. #if DBG
  1085. KIRQL SaveIrql;
  1086. #endif
  1087. PAGED_CODE();
  1088. //
  1089. // SeAssignSecurity will construct the final version
  1090. // of the security descriptor
  1091. //
  1092. Status = SeAssignSecurity( ParentDescriptor,
  1093. AccessState->SecurityDescriptor,
  1094. &NewDescriptor,
  1095. (BOOLEAN)(ObjectType == ObpDirectoryObjectType),
  1096. &AccessState->SubjectSecurityContext,
  1097. &ObjectType->TypeInfo.GenericMapping,
  1098. PagedPool );
  1099. if (!NT_SUCCESS( Status )) {
  1100. return( Status );
  1101. }
  1102. ObpBeginTypeSpecificCallOut( SaveIrql );
  1103. //
  1104. // Now invoke the security method callback to finish
  1105. // the assignment.
  1106. //
  1107. Status = (*ObjectType->TypeInfo.SecurityProcedure)( Object,
  1108. AssignSecurityDescriptor,
  1109. NULL,
  1110. NewDescriptor,
  1111. NULL,
  1112. NULL,
  1113. PagedPool,
  1114. &ObjectType->TypeInfo.GenericMapping );
  1115. ObpEndTypeSpecificCallOut( SaveIrql, "Security", ObjectType, Object );
  1116. if (!NT_SUCCESS( Status )) {
  1117. //
  1118. // The attempt to assign the security descriptor to the object
  1119. // failed. Free the space used by the new security descriptor.
  1120. //
  1121. SeDeassignSecurity( &NewDescriptor );
  1122. }
  1123. //
  1124. // And return to our caller
  1125. //
  1126. return( Status );
  1127. }
  1128. NTSTATUS
  1129. ObQuerySecurityDescriptorInfo(
  1130. IN PVOID Object,
  1131. IN PSECURITY_INFORMATION SecurityInformation,
  1132. OUT PSECURITY_DESCRIPTOR SecurityDescriptor,
  1133. IN OUT PULONG Length,
  1134. IN PSECURITY_DESCRIPTOR *ObjectsSecurityDescriptor
  1135. )
  1136. /*++
  1137. Routine Description:
  1138. This routine will extract the desired information from the
  1139. passed security descriptor and return the information in
  1140. the passed buffer as a security descriptor in self-relative
  1141. format.
  1142. This routine assumes that all parameters are captured and
  1143. safe to reference.
  1144. Arguments:
  1145. Object - Object that is having its security queried
  1146. SecurityInformation - Specifies what information is being queried.
  1147. SecurityDescriptor - Supplies the buffer to output the requested
  1148. information into.
  1149. This buffer has been probed only to the size indicated by
  1150. the Length parameter. Since it still points into user space,
  1151. it must always be accessed in a try clause.
  1152. Length - Supplies the address of a variable containing the length of
  1153. the security descriptor buffer. Upon return this variable will
  1154. contain the length needed to store the requested information.
  1155. ObjectsSecurityDescriptor - Supplies the address of a pointer to
  1156. the objects security descriptor. The passed security descriptor
  1157. must be in self-relative format.
  1158. Return Value:
  1159. NTSTATUS - STATUS_SUCCESS if successful and an appropriate error value
  1160. otherwise
  1161. --*/
  1162. {
  1163. NTSTATUS Status;
  1164. POBJECT_HEADER ObjectHeader;
  1165. PSECURITY_DESCRIPTOR ReferencedSecurityDescriptor;
  1166. PAGED_CODE();
  1167. UNREFERENCED_PARAMETER (ObjectsSecurityDescriptor);
  1168. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1169. //
  1170. // Reference the security descriptor
  1171. //
  1172. ReferencedSecurityDescriptor = ObpReferenceSecurityDescriptor( ObjectHeader );
  1173. Status = SeQuerySecurityDescriptorInfo( SecurityInformation,
  1174. SecurityDescriptor,
  1175. Length,
  1176. &ReferencedSecurityDescriptor
  1177. );
  1178. if (ReferencedSecurityDescriptor != NULL) {
  1179. ObDereferenceSecurityDescriptor ( ReferencedSecurityDescriptor, 1 );
  1180. }
  1181. return( Status );
  1182. }
  1183. NTSTATUS
  1184. ObSetSecurityDescriptorInfo (
  1185. IN PVOID Object,
  1186. IN PSECURITY_INFORMATION SecurityInformation,
  1187. IN OUT PSECURITY_DESCRIPTOR SecurityDescriptor,
  1188. IN OUT PSECURITY_DESCRIPTOR *ObjectsSecurityDescriptor,
  1189. IN POOL_TYPE PoolType,
  1190. IN PGENERIC_MAPPING GenericMapping
  1191. )
  1192. /*++
  1193. Routine Description:
  1194. Sets the security descriptor on an already secure object.
  1195. Arguments:
  1196. Object - Pointer to the object being modified.
  1197. SecurityInformation - Describes which information in the SecurityDescriptor parameter
  1198. is relevent.
  1199. SecurityDescriptor - Provides the new security information.
  1200. ObjectsSecurityDescriptor - Provides/returns the object's security descriptor.
  1201. PoolType - The pool the ObjectSecurityDescriptor is allocated from.
  1202. GenericMapping - Supplies the generic mapping for the object.
  1203. Return Value:
  1204. An appropriate status value
  1205. --*/
  1206. {
  1207. PSECURITY_DESCRIPTOR OldDescriptor;
  1208. PSECURITY_DESCRIPTOR NewDescriptor;
  1209. PSECURITY_DESCRIPTOR CachedDescriptor;
  1210. NTSTATUS Status;
  1211. POBJECT_HEADER ObjectHeader;
  1212. EX_FAST_REF OldRef;
  1213. PAGED_CODE();
  1214. //
  1215. // Check the rest of our input and call the default set security
  1216. // method. Also make sure no one is modifying the security descriptor
  1217. // while we're looking at it.
  1218. //
  1219. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1220. //
  1221. // In order to preserve some protected fields in the SD (like the SACL) we need to make sure that only one
  1222. // thread updates it at any one time. If we didn't do this another modification could wipe out a SACL
  1223. // an administrator was adding.
  1224. //
  1225. while (1) {
  1226. //
  1227. // Reference the security descriptor
  1228. //
  1229. OldDescriptor = ObpReferenceSecurityDescriptor( ObjectHeader );
  1230. NewDescriptor = OldDescriptor;
  1231. Status = SeSetSecurityDescriptorInfo( Object,
  1232. SecurityInformation,
  1233. SecurityDescriptor,
  1234. &NewDescriptor,
  1235. PoolType,
  1236. GenericMapping );
  1237. //
  1238. // If we successfully set the new security descriptor then we
  1239. // need to log it in our database and get yet another pointer
  1240. // to the finaly security descriptor
  1241. //
  1242. if ( NT_SUCCESS( Status )) {
  1243. Status = ObLogSecurityDescriptor( NewDescriptor,
  1244. &CachedDescriptor,
  1245. ExFastRefGetAdditionalReferenceCount () + 1 );
  1246. ExFreePool( NewDescriptor );
  1247. if ( NT_SUCCESS( Status )) {
  1248. //
  1249. // Now we need to see if anyone else update this security descriptor inside the
  1250. // gap where we didn't hold the lock. If they did then we just try it all again.
  1251. //
  1252. OldRef = ExFastRefCompareSwapObject ((PEX_FAST_REF)ObjectsSecurityDescriptor,
  1253. CachedDescriptor,
  1254. OldDescriptor);
  1255. if (ExFastRefEqualObjects (OldRef, OldDescriptor)) {
  1256. //
  1257. // The swap occured ok. We must now flush any slow refers out of the slow ref path before
  1258. // dereferencing the object. We do this by obtaining and dropping the object lock.
  1259. //
  1260. ObpLockObject( ObjectHeader );
  1261. ObpUnlockObject( ObjectHeader );
  1262. //
  1263. // If there was an original object then we need to work out how many
  1264. // cached references there were (if any) and return them.
  1265. //
  1266. ObDereferenceSecurityDescriptor( OldDescriptor, ExFastRefGetUnusedReferences (OldRef) + 2 );
  1267. break;
  1268. } else {
  1269. ObDereferenceSecurityDescriptor( OldDescriptor, 1 );
  1270. ObDereferenceSecurityDescriptor( CachedDescriptor, ExFastRefGetAdditionalReferenceCount () + 1);
  1271. }
  1272. } else {
  1273. //
  1274. // Dereference old SecurityDescriptor
  1275. //
  1276. ObDereferenceSecurityDescriptor( OldDescriptor, 1 );
  1277. break;
  1278. }
  1279. } else {
  1280. //
  1281. // Dereference old SecurityDescriptor
  1282. //
  1283. if (OldDescriptor != NULL) {
  1284. ObDereferenceSecurityDescriptor( OldDescriptor, 1 );
  1285. }
  1286. break;
  1287. }
  1288. }
  1289. //
  1290. // And return to our caller
  1291. //
  1292. return( Status );
  1293. }
  1294. NTSTATUS
  1295. ObpValidateAccessMask (
  1296. PACCESS_STATE AccessState
  1297. )
  1298. /*++
  1299. Routine Description:
  1300. Checks the desired access mask of a passed object against the
  1301. passed security descriptor.
  1302. Arguments:
  1303. AccessState - A pointer to the AccessState for the pending operation.
  1304. Return Value:
  1305. Only returns STATUS_SUCCESS
  1306. --*/
  1307. {
  1308. SECURITY_DESCRIPTOR *SecurityDescriptor = AccessState->SecurityDescriptor;
  1309. PAGED_CODE();
  1310. //
  1311. // First make sure the access state has a security descriptor. If there
  1312. // is one and it has a system acl and the previously granted access did
  1313. // not include system security then add the fact that we want system
  1314. // security to the remaining desired access state.
  1315. //
  1316. if (SecurityDescriptor != NULL) {
  1317. if ( SecurityDescriptor->Control & SE_SACL_PRESENT ) {
  1318. if ( !(AccessState->PreviouslyGrantedAccess & ACCESS_SYSTEM_SECURITY)) {
  1319. AccessState->RemainingDesiredAccess |= ACCESS_SYSTEM_SECURITY;
  1320. }
  1321. }
  1322. }
  1323. return( STATUS_SUCCESS );
  1324. }