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.

3323 lines
88 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. obhandle.c
  5. Abstract:
  6. Object handle routines
  7. Author:
  8. Steve Wood (stevewo) 31-Mar-1989
  9. Revision History:
  10. --*/
  11. #include "obp.h"
  12. //
  13. // Define logical sum of all generic accesses.
  14. //
  15. #define GENERIC_ACCESS (GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL)
  16. //
  17. // Define the mask for the trace index. The last bit is used for
  18. // OBJ_PROTECT_CLOSE bit
  19. //
  20. #define OBP_CREATOR_BACKTRACE_LIMIT 0x7fff
  21. #define OBP_CREATOR_BACKTRACE_INDEX_MASK OBP_CREATOR_BACKTRACE_LIMIT
  22. //
  23. // Define local prototypes
  24. //
  25. NTSTATUS
  26. ObpIncrementHandleDataBase (
  27. IN POBJECT_HEADER ObjectHeader,
  28. IN PEPROCESS Process,
  29. OUT PULONG NewProcessHandleCount
  30. );
  31. NTSTATUS
  32. ObpCaptureHandleInformation (
  33. IN OUT PSYSTEM_HANDLE_TABLE_ENTRY_INFO *HandleEntryInfo,
  34. IN HANDLE UniqueProcessId,
  35. IN PVOID HandleTableEntry,
  36. IN HANDLE HandleIndex,
  37. IN ULONG Length,
  38. IN OUT PULONG RequiredLength
  39. );
  40. NTSTATUS
  41. ObpCaptureHandleInformationEx (
  42. IN OUT PSYSTEM_HANDLE_TABLE_ENTRY_INFO_EX *HandleEntryInfo,
  43. IN HANDLE UniqueProcessId,
  44. IN PHANDLE_TABLE_ENTRY ObjectTableEntry,
  45. IN HANDLE HandleIndex,
  46. IN ULONG Length,
  47. IN OUT PULONG RequiredLength
  48. );
  49. USHORT
  50. ObpComputeGrantedAccessIndex (
  51. ACCESS_MASK GrantedAccess
  52. );
  53. ACCESS_MASK
  54. ObpTranslateGrantedAccessIndex (
  55. USHORT GrantedAccessIndex
  56. );
  57. USHORT
  58. RtlLogUmodeStackBackTrace(
  59. VOID
  60. );
  61. #ifdef ALLOC_PRAGMA
  62. #pragma alloc_text(PAGE,NtDuplicateObject)
  63. #pragma alloc_text(PAGE,ObGetHandleInformation)
  64. #pragma alloc_text(PAGE,ObGetHandleInformationEx)
  65. #pragma alloc_text(PAGE,ObpCaptureHandleInformation)
  66. #pragma alloc_text(PAGE,ObpCaptureHandleInformationEx)
  67. #pragma alloc_text(PAGE,ObpIncrementHandleDataBase)
  68. #pragma alloc_text(PAGE,ObpInsertHandleCount)
  69. #pragma alloc_text(PAGE,ObpIncrementHandleCount)
  70. #pragma alloc_text(PAGE,ObpIncrementUnnamedHandleCount)
  71. #pragma alloc_text(PAGE,ObpChargeQuotaForObject)
  72. #pragma alloc_text(PAGE,ObpDecrementHandleCount)
  73. #pragma alloc_text(PAGE,ObpCreateHandle)
  74. #pragma alloc_text(PAGE,ObpCreateUnnamedHandle)
  75. #pragma alloc_text(PAGE,ObpValidateDesiredAccess)
  76. #pragma alloc_text(PAGE,ObDuplicateObject)
  77. #pragma alloc_text(PAGE,ObReferenceProcessHandleTable)
  78. #pragma alloc_text(PAGE,ObDereferenceProcessHandleTable)
  79. #pragma alloc_text(PAGE,ObpComputeGrantedAccessIndex)
  80. #pragma alloc_text(PAGE,ObpTranslateGrantedAccessIndex)
  81. #pragma alloc_text(PAGE,ObGetProcessHandleCount)
  82. #endif
  83. PHANDLE_TABLE
  84. ObReferenceProcessHandleTable (
  85. PEPROCESS SourceProcess
  86. )
  87. /*++
  88. Routine Description:
  89. This function allows safe cross process handle table references. Table deletion
  90. at process exit waits for all outstanding references to finish. Once the table
  91. is marked deleted further references are denied byt this funtion.
  92. Arguments:
  93. SourceProcesss - Process whose handle table is to be referenced
  94. Return Value:
  95. Referenced handle table or NULL if the processes handle table has been or is in the
  96. process of being deleted.
  97. --*/
  98. {
  99. PHANDLE_TABLE ObjectTable;
  100. ObjectTable = NULL;
  101. if (ExAcquireRundownProtection (&SourceProcess->RundownProtect)) {
  102. ObjectTable = SourceProcess->ObjectTable;
  103. if (ObjectTable == NULL) {
  104. ExReleaseRundownProtection (&SourceProcess->RundownProtect);
  105. }
  106. }
  107. return ObjectTable;
  108. }
  109. VOID
  110. ObDereferenceProcessHandleTable (
  111. PEPROCESS SourceProcess
  112. )
  113. /*++
  114. Routine Description:
  115. This function removes the outstanding reference obtained via ObReferenceProcessHandleTable.
  116. Arguments:
  117. ObjectTable - Handle table to dereference
  118. Return Value:
  119. None.
  120. --*/
  121. {
  122. ExReleaseRundownProtection (&SourceProcess->RundownProtect);
  123. }
  124. ULONG
  125. ObGetProcessHandleCount (
  126. PEPROCESS Process
  127. )
  128. /*++
  129. Routine Description:
  130. This function returns the total number of open handles for a process.
  131. Arguments:
  132. Process - Process whose handle table is to be queried.
  133. Return Value:
  134. Count of open handles.
  135. --*/
  136. {
  137. PHANDLE_TABLE HandleTable;
  138. ULONG HandleCount;
  139. HandleTable = ObReferenceProcessHandleTable (Process);
  140. if ( HandleTable ) {
  141. HandleCount = HandleTable->HandleCount;
  142. ObDereferenceProcessHandleTable (Process);
  143. } else {
  144. HandleCount = 0;
  145. }
  146. return HandleCount;
  147. }
  148. NTSTATUS
  149. ObDuplicateObject (
  150. IN PEPROCESS SourceProcess,
  151. IN HANDLE SourceHandle,
  152. IN PEPROCESS TargetProcess OPTIONAL,
  153. OUT PHANDLE TargetHandle OPTIONAL,
  154. IN ACCESS_MASK DesiredAccess,
  155. IN ULONG HandleAttributes,
  156. IN ULONG Options,
  157. IN KPROCESSOR_MODE PreviousMode
  158. )
  159. /*++
  160. Routine Description:
  161. This function creates a handle that is a duplicate of the specified
  162. source handle. The source handle is evaluated in the context of the
  163. specified source process. The calling process must have
  164. PROCESS_DUP_HANDLE access to the source process. The duplicate
  165. handle is created with the specified attributes and desired access.
  166. The duplicate handle is created in the handle table of the specified
  167. target process. The calling process must have PROCESS_DUP_HANDLE
  168. access to the target process.
  169. Arguments:
  170. SourceProcess - Supplies a pointer to the source process for the
  171. handle being duplicated
  172. SourceHandle - Supplies the handle being duplicated
  173. TargetProcess - Optionally supplies a pointer to the target process
  174. that is to receive the new handle
  175. TargetHandle - Optionally returns a the new duplicated handle
  176. DesiredAccess - Desired access for the new handle
  177. HandleAttributes - Desired attributes for the new handle
  178. Options - Duplication options that control things like close source,
  179. same access, and same attributes.
  180. PreviousMode - Mode of caller
  181. Return Value:
  182. NTSTATUS - Status pf call
  183. --*/
  184. {
  185. NTSTATUS Status;
  186. PVOID SourceObject;
  187. POBJECT_HEADER ObjectHeader;
  188. POBJECT_TYPE ObjectType;
  189. BOOLEAN Attached;
  190. PHANDLE_TABLE SourceObjectTable, TargetObjectTable;
  191. HANDLE_TABLE_ENTRY ObjectTableEntry;
  192. OBJECT_HANDLE_INFORMATION HandleInformation;
  193. HANDLE NewHandle;
  194. ACCESS_STATE AccessState;
  195. AUX_ACCESS_DATA AuxData;
  196. ACCESS_MASK SourceAccess;
  197. ACCESS_MASK TargetAccess;
  198. ACCESS_MASK AuditMask = (ACCESS_MASK)0;
  199. PACCESS_STATE PassedAccessState = NULL;
  200. HANDLE_TABLE_ENTRY_INFO ObjectInfo;
  201. KAPC_STATE ApcState;
  202. if (ARGUMENT_PRESENT (TargetHandle)) {
  203. *TargetHandle = NULL;
  204. }
  205. //
  206. // If the caller is not asking for the same access then
  207. // validate the access they are requesting doesn't contain
  208. // any bad bits
  209. //
  210. if (!(Options & DUPLICATE_SAME_ACCESS)) {
  211. Status = ObpValidateDesiredAccess( DesiredAccess );
  212. if (!NT_SUCCESS( Status )) {
  213. return( Status );
  214. }
  215. }
  216. //
  217. // The Attached variable indicates if we needed to
  218. // attach to the source process because it was not the
  219. // current process.
  220. //
  221. Attached = FALSE;
  222. //
  223. // Lock down access to the process object tables
  224. //
  225. SourceObjectTable = ObReferenceProcessHandleTable (SourceProcess);
  226. //
  227. // Make sure the source process has an object table still
  228. //
  229. if ( SourceObjectTable == NULL ) {
  230. return STATUS_PROCESS_IS_TERMINATING;
  231. }
  232. //
  233. // The the input source handle get a pointer to the
  234. // source object itself, then detach from the process
  235. // if necessary and check if we were given a good
  236. // source handle.
  237. //
  238. Status = ObpReferenceProcessObjectByHandle (SourceHandle,
  239. SourceProcess,
  240. SourceObjectTable,
  241. PreviousMode,
  242. &SourceObject,
  243. &HandleInformation,
  244. &AuditMask);
  245. if (NT_SUCCESS( Status )) {
  246. if ((HandleInformation.HandleAttributes & OBJ_AUDIT_OBJECT_CLOSE) == 0) {
  247. AuditMask = 0;
  248. }
  249. }
  250. if (!NT_SUCCESS( Status )) {
  251. ObDereferenceProcessHandleTable (SourceProcess);
  252. return( Status );
  253. }
  254. //
  255. // We are all done if no target process handle was specified.
  256. // This is practially a noop because the only really end result
  257. // could be that we've closed the source handle.
  258. //
  259. if (!ARGUMENT_PRESENT( TargetProcess )) {
  260. //
  261. // If no TargetProcessHandle, then only possible option is to close
  262. // the source handle in the context of the source process.
  263. //
  264. if (!(Options & DUPLICATE_CLOSE_SOURCE)) {
  265. Status = STATUS_INVALID_PARAMETER;
  266. }
  267. if (Options & DUPLICATE_CLOSE_SOURCE) {
  268. KeStackAttachProcess( &SourceProcess->Pcb, &ApcState );
  269. NtClose( SourceHandle );
  270. KeUnstackDetachProcess (&ApcState);
  271. }
  272. ObDereferenceProcessHandleTable (SourceProcess);
  273. ObDereferenceObject( SourceObject );
  274. return( Status );
  275. }
  276. SourceAccess = HandleInformation.GrantedAccess;
  277. //
  278. // Make sure the target process has not exited
  279. //
  280. TargetObjectTable = ObReferenceProcessHandleTable (TargetProcess);
  281. if ( TargetObjectTable == NULL ) {
  282. if (Options & DUPLICATE_CLOSE_SOURCE) {
  283. KeStackAttachProcess( &SourceProcess->Pcb, &ApcState );
  284. NtClose( SourceHandle );
  285. KeUnstackDetachProcess (&ApcState);
  286. }
  287. ObDereferenceProcessHandleTable (SourceProcess);
  288. ObDereferenceObject( SourceObject );
  289. return STATUS_PROCESS_IS_TERMINATING;
  290. }
  291. //
  292. // If the specified target process is not the current process, attach
  293. // to the specified target process.
  294. //
  295. if (PsGetCurrentProcess() != TargetProcess) {
  296. KeStackAttachProcess( &TargetProcess->Pcb, &ApcState );
  297. Attached = TRUE;
  298. }
  299. //
  300. // Construct the proper desired access and attributes for the new handle
  301. //
  302. if (Options & DUPLICATE_SAME_ACCESS) {
  303. DesiredAccess = SourceAccess;
  304. }
  305. if (Options & DUPLICATE_SAME_ATTRIBUTES) {
  306. HandleAttributes = HandleInformation.HandleAttributes;
  307. } else {
  308. //
  309. // Always propogate auditing information.
  310. //
  311. HandleAttributes |= HandleInformation.HandleAttributes & OBJ_AUDIT_OBJECT_CLOSE;
  312. }
  313. //
  314. // Get the object header for the source object
  315. //
  316. ObjectHeader = OBJECT_TO_OBJECT_HEADER( SourceObject );
  317. ObjectType = ObjectHeader->Type;
  318. RtlZeroMemory( &ObjectTableEntry, sizeof( HANDLE_TABLE_ENTRY ) );
  319. ObjectTableEntry.Object = ObjectHeader;
  320. ObjectTableEntry.ObAttributes |= (HandleAttributes & OBJ_HANDLE_ATTRIBUTES);
  321. //
  322. // The following will be reset below if AVR is performed.
  323. //
  324. ObjectInfo.AuditMask = AuditMask;
  325. //
  326. // If any of the generic access bits are specified then map those to more
  327. // specific access bits
  328. //
  329. if ((DesiredAccess & GENERIC_ACCESS) != 0) {
  330. RtlMapGenericMask( &DesiredAccess,
  331. &ObjectType->TypeInfo.GenericMapping );
  332. }
  333. //
  334. // Make sure to preserve ACCESS_SYSTEM_SECURITY, which most likely is not
  335. // found in the ValidAccessMask
  336. //
  337. TargetAccess = DesiredAccess &
  338. (ObjectType->TypeInfo.ValidAccessMask | ACCESS_SYSTEM_SECURITY);
  339. //
  340. // If the access requested for the target is a superset of the
  341. // access allowed in the source, perform full AVR. If it is a
  342. // subset or equal, do not perform any access validation.
  343. //
  344. // Do not allow superset access if object type has a private security
  345. // method, as there is no means to call them in this case to do the
  346. // access check.
  347. //
  348. // If the AccessState is not passed to ObpIncrementHandleCount
  349. // there will be no AVR.
  350. //
  351. if (TargetAccess & ~SourceAccess) {
  352. if (ObjectType->TypeInfo.SecurityProcedure == SeDefaultObjectMethod) {
  353. Status = SeCreateAccessState( &AccessState,
  354. &AuxData,
  355. TargetAccess, // DesiredAccess
  356. &ObjectType->TypeInfo.GenericMapping );
  357. PassedAccessState = &AccessState;
  358. } else {
  359. Status = STATUS_ACCESS_DENIED;
  360. }
  361. } else {
  362. //
  363. // Do not perform AVR
  364. //
  365. PassedAccessState = NULL;
  366. Status = STATUS_SUCCESS;
  367. }
  368. //
  369. // Increment the new handle count and get a pointer to
  370. // the target processes object table
  371. //
  372. if ( NT_SUCCESS( Status )) {
  373. Status = ObpIncrementHandleCount( ObDuplicateHandle,
  374. PsGetCurrentProcess(), // this is already the target process
  375. SourceObject,
  376. ObjectType,
  377. PassedAccessState,
  378. PreviousMode,
  379. HandleAttributes );
  380. }
  381. if (Attached) {
  382. KeUnstackDetachProcess (&ApcState);
  383. Attached = FALSE;
  384. }
  385. if (Options & DUPLICATE_CLOSE_SOURCE) {
  386. KeStackAttachProcess( &SourceProcess->Pcb, &ApcState );
  387. NtClose( SourceHandle );
  388. KeUnstackDetachProcess(&ApcState);
  389. }
  390. if (!NT_SUCCESS( Status )) {
  391. if (PassedAccessState != NULL) {
  392. SeDeleteAccessState( PassedAccessState );
  393. }
  394. ObDereferenceProcessHandleTable (SourceProcess);
  395. ObDereferenceProcessHandleTable (TargetProcess);
  396. ObDereferenceObject( SourceObject );
  397. return( Status );
  398. }
  399. if ((PassedAccessState != NULL) && (PassedAccessState->GenerateOnClose == TRUE)) {
  400. //
  401. // If we performed AVR opening the handle, then mark the handle as needing
  402. // auditing when it's closed. Also save away the maximum audit mask
  403. // if one was created.
  404. //
  405. ObjectTableEntry.ObAttributes |= OBJ_AUDIT_OBJECT_CLOSE;
  406. ObjectInfo.AuditMask = ((PAUX_ACCESS_DATA)PassedAccessState->AuxData)->MaximumAuditMask;
  407. }
  408. #if i386
  409. if (NtGlobalFlag & FLG_KERNEL_STACK_TRACE_DB) {
  410. LONG StackTraceIndex;
  411. ObjectTableEntry.GrantedAccessIndex = ObpComputeGrantedAccessIndex( TargetAccess );
  412. if (PreviousMode == KernelMode) {
  413. StackTraceIndex = RtlLogStackBackTrace();
  414. } else {
  415. StackTraceIndex = RtlLogUmodeStackBackTrace();
  416. }
  417. //
  418. // Save the stack trace only if the index fits the CreatorBackTraceIndex
  419. // minus the ProtectOnClose bit
  420. //
  421. if (StackTraceIndex < OBP_CREATOR_BACKTRACE_LIMIT) {
  422. ObjectTableEntry.CreatorBackTraceIndex = (USHORT)StackTraceIndex;
  423. } else {
  424. ObjectTableEntry.CreatorBackTraceIndex = 0;
  425. }
  426. } else {
  427. ObjectTableEntry.GrantedAccess = TargetAccess;
  428. }
  429. #else
  430. ObjectTableEntry.GrantedAccess = TargetAccess;
  431. #endif // i386
  432. //
  433. // Now that we've constructed a new object table entry for the duplicated handle
  434. // we need to add it to the object table of the target process
  435. //
  436. ObpEncodeProtectClose(ObjectTableEntry);
  437. NewHandle = ExCreateHandle( TargetObjectTable, &ObjectTableEntry );
  438. if (NewHandle) {
  439. if ( ObjectInfo.AuditMask != 0 ) {
  440. //
  441. // Make sure it's the same object before setting the handle information.
  442. // The user may have closed it immediately after the ExCreateHandle call,
  443. // so at this time it may either be invalid or a completely different object.
  444. //
  445. PHANDLE_TABLE_ENTRY HandleTableEntry;
  446. PKTHREAD CurrentThread;
  447. CurrentThread = KeGetCurrentThread ();
  448. KeEnterCriticalRegionThread (CurrentThread);
  449. HandleTableEntry = ExMapHandleToPointer ( TargetObjectTable, NewHandle );
  450. if (HandleTableEntry != NULL) {
  451. if (((ULONG_PTR)(HandleTableEntry->Object) & ~OBJ_HANDLE_ATTRIBUTES) == (ULONG_PTR)ObjectHeader) {
  452. ExSetHandleInfo(TargetObjectTable, NewHandle, &ObjectInfo, TRUE);
  453. }
  454. ExUnlockHandleTableEntry( TargetObjectTable, HandleTableEntry );
  455. }
  456. KeLeaveCriticalRegionThread (CurrentThread);
  457. }
  458. //
  459. // We have a new handle to audit the creation of the new handle if
  460. // AVR was done. And set the optional output handle variable. Note
  461. // that if we reach here the status variable is already a success
  462. //
  463. if (PassedAccessState != NULL) {
  464. SeAuditHandleCreation( PassedAccessState, NewHandle );
  465. }
  466. if ( (ObjectTableEntry.ObAttributes & OBJ_AUDIT_OBJECT_CLOSE) &&
  467. (SeDetailedAuditingWithToken( PassedAccessState ? SeQuerySubjectContextToken( &PassedAccessState->SubjectSecurityContext ) : NULL )) ) {
  468. SeAuditHandleDuplication( SourceHandle,
  469. NewHandle,
  470. SourceProcess,
  471. TargetProcess );
  472. }
  473. } else {
  474. //
  475. // We didn't get a new handle to decrement the handle count dereference
  476. // the necessary objects, set the optional output variable and indicate
  477. // why we're failing
  478. //
  479. ObpDecrementHandleCount( TargetProcess,
  480. ObjectHeader,
  481. ObjectType,
  482. TargetAccess );
  483. ObDereferenceObject( SourceObject );
  484. Status = STATUS_INSUFFICIENT_RESOURCES;
  485. }
  486. if (ARGUMENT_PRESENT( TargetHandle )) {
  487. *TargetHandle = NewHandle;
  488. }
  489. //
  490. // Cleanup from our selfs and then return to our caller
  491. //
  492. if (PassedAccessState != NULL) {
  493. SeDeleteAccessState( PassedAccessState );
  494. }
  495. ObDereferenceProcessHandleTable (SourceProcess);
  496. ObDereferenceProcessHandleTable (TargetProcess);
  497. return( Status );
  498. }
  499. NTSTATUS
  500. NtDuplicateObject (
  501. IN HANDLE SourceProcessHandle,
  502. IN HANDLE SourceHandle,
  503. IN HANDLE TargetProcessHandle OPTIONAL,
  504. OUT PHANDLE TargetHandle OPTIONAL,
  505. IN ACCESS_MASK DesiredAccess,
  506. IN ULONG HandleAttributes,
  507. IN ULONG Options
  508. )
  509. /*++
  510. Routine Description:
  511. This function creates a handle that is a duplicate of the specified
  512. source handle. The source handle is evaluated in the context of the
  513. specified source process. The calling process must have
  514. PROCESS_DUP_HANDLE access to the source process. The duplicate
  515. handle is created with the specified attributes and desired access.
  516. The duplicate handle is created in the handle table of the specified
  517. target process. The calling process must have PROCESS_DUP_HANDLE
  518. access to the target process.
  519. Arguments:
  520. SourceProcessHandle - Supplies a handle to the source process for the
  521. handle being duplicated
  522. SourceHandle - Supplies the handle being duplicated
  523. TargetProcessHandle - Optionally supplies a handle to the target process
  524. that is to receive the new handle
  525. TargetHandle - Optionally returns a the new duplicated handle
  526. DesiredAccess - Desired access for the new handle
  527. HandleAttributes - Desired attributes for the new handle
  528. Options - Duplication options that control things like close source,
  529. same access, and same attributes.
  530. Return Value:
  531. TBS
  532. --*/
  533. {
  534. KPROCESSOR_MODE PreviousMode;
  535. NTSTATUS Status, Status1;
  536. PEPROCESS SourceProcess;
  537. PEPROCESS TargetProcess;
  538. HANDLE NewHandle = NULL;
  539. //
  540. // Get previous processor mode and probe output arguments if necessary.
  541. //
  542. PreviousMode = KeGetPreviousMode();
  543. if (ARGUMENT_PRESENT( TargetHandle ) && (PreviousMode != KernelMode)) {
  544. try {
  545. ProbeForWriteHandle( TargetHandle );
  546. *TargetHandle = NULL;
  547. } except( EXCEPTION_EXECUTE_HANDLER ) {
  548. return( GetExceptionCode() );
  549. }
  550. }
  551. //
  552. // Given the input source process handle get a pointer
  553. // to the source process object
  554. //
  555. Status = ObReferenceObjectByHandle( SourceProcessHandle,
  556. PROCESS_DUP_HANDLE,
  557. PsProcessType,
  558. PreviousMode,
  559. (PVOID *)&SourceProcess,
  560. NULL );
  561. if (!NT_SUCCESS( Status )) {
  562. return Status;
  563. }
  564. //
  565. // We are all done if no target process handle was specified.
  566. // This is practially a noop because the only really end result
  567. // could be that we've closed the source handle.
  568. //
  569. if (!ARGUMENT_PRESENT( TargetProcessHandle )) {
  570. //
  571. // If no TargetProcessHandle, then only possible option is to close
  572. // the source handle in the context of the source process.
  573. //
  574. TargetProcess = NULL;
  575. Status1 = STATUS_SUCCESS;
  576. } else {
  577. //
  578. // At this point the caller did specify for a target process
  579. // So from the target process handle get a pointer to the
  580. // target process object.
  581. //
  582. Status1 = ObReferenceObjectByHandle( TargetProcessHandle,
  583. PROCESS_DUP_HANDLE,
  584. PsProcessType,
  585. PreviousMode,
  586. (PVOID *)&TargetProcess,
  587. NULL );
  588. //
  589. // The original structure of Duplicate cased us to still close the input handle if the output process handle
  590. // was bad. We preserve that functionality.
  591. //
  592. if (!NT_SUCCESS (Status1)) {
  593. TargetProcess = NULL;
  594. }
  595. }
  596. Status = ObDuplicateObject (SourceProcess,
  597. SourceHandle,
  598. TargetProcess,
  599. &NewHandle,
  600. DesiredAccess,
  601. HandleAttributes,
  602. Options,
  603. PreviousMode );
  604. if (ARGUMENT_PRESENT( TargetHandle )) {
  605. try {
  606. *TargetHandle = NewHandle;
  607. } except( EXCEPTION_EXECUTE_HANDLER ) {
  608. //
  609. // Fall through, since we cannot undo what we have done.
  610. //
  611. }
  612. }
  613. ObDereferenceObject( SourceProcess );
  614. if ( TargetProcess != NULL) {
  615. ObDereferenceObject( TargetProcess );
  616. }
  617. if (!NT_SUCCESS (Status1)) {
  618. Status = Status1;
  619. }
  620. return( Status );
  621. }
  622. NTSTATUS
  623. ObGetHandleInformation (
  624. OUT PSYSTEM_HANDLE_INFORMATION HandleInformation,
  625. IN ULONG Length,
  626. OUT PULONG ReturnLength OPTIONAL
  627. )
  628. /*++
  629. Routine Description:
  630. This routine returns information about the specified handle.
  631. Arguments:
  632. HandleInformation - Supplies an array of handle information
  633. structures to fill in
  634. Length - Supplies the length the handle information array in bytes
  635. ReturnLength - Receives the number of bytes used by this call
  636. Return Value:
  637. An appropriate status value
  638. --*/
  639. {
  640. NTSTATUS Status;
  641. ULONG RequiredLength;
  642. PAGED_CODE();
  643. RequiredLength = FIELD_OFFSET( SYSTEM_HANDLE_INFORMATION, Handles );
  644. if (Length < RequiredLength) {
  645. return( STATUS_INFO_LENGTH_MISMATCH );
  646. }
  647. HandleInformation->NumberOfHandles = 0;
  648. //
  649. // For every handle in every handle table we'll be calling
  650. // our callback routine
  651. //
  652. Status = ExSnapShotHandleTables( ObpCaptureHandleInformation,
  653. HandleInformation,
  654. Length,
  655. &RequiredLength );
  656. if (ARGUMENT_PRESENT( ReturnLength )) {
  657. *ReturnLength = RequiredLength;
  658. }
  659. return( Status );
  660. }
  661. NTSTATUS
  662. ObGetHandleInformationEx (
  663. OUT PSYSTEM_HANDLE_INFORMATION_EX HandleInformation,
  664. IN ULONG Length,
  665. OUT PULONG ReturnLength OPTIONAL
  666. )
  667. /*++
  668. Routine Description:
  669. This routine returns information about the specified handle.
  670. Arguments:
  671. HandleInformation - Supplies an array of handle information
  672. structures to fill in
  673. Length - Supplies the length the handle information array in bytes
  674. ReturnLength - Receives the number of bytes used by this call
  675. Return Value:
  676. An appropriate status value
  677. --*/
  678. {
  679. NTSTATUS Status;
  680. ULONG RequiredLength;
  681. PAGED_CODE();
  682. RequiredLength = FIELD_OFFSET( SYSTEM_HANDLE_INFORMATION_EX, Handles );
  683. if (Length < RequiredLength) {
  684. return( STATUS_INFO_LENGTH_MISMATCH );
  685. }
  686. HandleInformation->NumberOfHandles = 0;
  687. //
  688. // For every handle in every handle table we'll be calling
  689. // our callback routine
  690. //
  691. Status = ExSnapShotHandleTablesEx( ObpCaptureHandleInformationEx,
  692. HandleInformation,
  693. Length,
  694. &RequiredLength );
  695. if (ARGUMENT_PRESENT( ReturnLength )) {
  696. *ReturnLength = RequiredLength;
  697. }
  698. return( Status );
  699. }
  700. NTSTATUS
  701. ObpCaptureHandleInformation (
  702. IN OUT PSYSTEM_HANDLE_TABLE_ENTRY_INFO *HandleEntryInfo,
  703. IN HANDLE UniqueProcessId,
  704. IN PHANDLE_TABLE_ENTRY ObjectTableEntry,
  705. IN HANDLE HandleIndex,
  706. IN ULONG Length,
  707. IN OUT PULONG RequiredLength
  708. )
  709. /*++
  710. Routine Description:
  711. This is the callback routine of ObGetHandleInformation
  712. Arguments:
  713. HandleEntryInfo - Supplies a pointer to the output buffer to receive
  714. the handle information
  715. UniqueProcessId - Supplies the process id of the caller
  716. ObjectTableEntry - Supplies the handle table entry that is being
  717. captured
  718. HandleIndex - Supplies the index for the preceding handle table entry
  719. Length - Specifies the length, in bytes, of the original user buffer
  720. RequiredLength - Specifies the length, in bytes, that has already been
  721. used in the buffer to store information. On return this receives
  722. the updated number of bytes being used.
  723. Note that the HandleEntryInfo does not necessarily point to the
  724. start of the original user buffer. It will have been offset by
  725. the feed-in RequiredLength value.
  726. Return Value:
  727. An appropriate status value
  728. --*/
  729. {
  730. NTSTATUS Status;
  731. POBJECT_HEADER ObjectHeader;
  732. //
  733. // Figure out who much size we really need to contain this extra record
  734. // and then check that it fits.
  735. //
  736. *RequiredLength += sizeof( SYSTEM_HANDLE_TABLE_ENTRY_INFO );
  737. if (Length < *RequiredLength) {
  738. Status = STATUS_INFO_LENGTH_MISMATCH;
  739. } else {
  740. //
  741. // Get the object header from the table entry and then copy over the information
  742. //
  743. ObjectHeader = (POBJECT_HEADER)(((ULONG_PTR)(ObjectTableEntry->Object)) & ~OBJ_HANDLE_ATTRIBUTES);
  744. (*HandleEntryInfo)->UniqueProcessId = (USHORT)((ULONG_PTR)UniqueProcessId);
  745. (*HandleEntryInfo)->HandleAttributes = (UCHAR)ObpGetHandleAttributes(ObjectTableEntry);
  746. (*HandleEntryInfo)->ObjectTypeIndex = (UCHAR)(ObjectHeader->Type->Index);
  747. (*HandleEntryInfo)->HandleValue = (USHORT)((ULONG_PTR)(HandleIndex));
  748. (*HandleEntryInfo)->Object = &ObjectHeader->Body;
  749. (*HandleEntryInfo)->CreatorBackTraceIndex = 0;
  750. #if i386
  751. if (NtGlobalFlag & FLG_KERNEL_STACK_TRACE_DB) {
  752. (*HandleEntryInfo)->CreatorBackTraceIndex = ObjectTableEntry->CreatorBackTraceIndex & OBP_CREATOR_BACKTRACE_INDEX_MASK;
  753. (*HandleEntryInfo)->GrantedAccess = ObpTranslateGrantedAccessIndex( ObjectTableEntry->GrantedAccessIndex );
  754. } else {
  755. (*HandleEntryInfo)->GrantedAccess = ObpDecodeGrantedAccess(ObjectTableEntry->GrantedAccess);
  756. }
  757. #else
  758. (*HandleEntryInfo)->GrantedAccess = ObpDecodeGrantedAccess(ObjectTableEntry->GrantedAccess);
  759. #endif // i386
  760. (*HandleEntryInfo)++;
  761. Status = STATUS_SUCCESS;
  762. }
  763. return( Status );
  764. }
  765. NTSTATUS
  766. ObpCaptureHandleInformationEx (
  767. IN OUT PSYSTEM_HANDLE_TABLE_ENTRY_INFO_EX *HandleEntryInfo,
  768. IN HANDLE UniqueProcessId,
  769. IN PHANDLE_TABLE_ENTRY ObjectTableEntry,
  770. IN HANDLE HandleIndex,
  771. IN ULONG Length,
  772. IN OUT PULONG RequiredLength
  773. )
  774. /*++
  775. Routine Description:
  776. This is the callback routine of ObGetHandleInformation
  777. Arguments:
  778. HandleEntryInfo - Supplies a pointer to the output buffer to receive
  779. the handle information
  780. UniqueProcessId - Supplies the process id of the caller
  781. ObjectTableEntry - Supplies the handle table entry that is being
  782. captured
  783. HandleIndex - Supplies the index for the preceding handle table entry
  784. Length - Specifies the length, in bytes, of the original user buffer
  785. RequiredLength - Specifies the length, in bytes, that has already been
  786. used in the buffer to store information. On return this receives
  787. the updated number of bytes being used.
  788. Note that the HandleEntryInfo does not necessarily point to the
  789. start of the original user buffer. It will have been offset by
  790. the feed-in RequiredLength value.
  791. Return Value:
  792. An appropriate status value
  793. --*/
  794. {
  795. NTSTATUS Status;
  796. POBJECT_HEADER ObjectHeader;
  797. //
  798. // Figure out who much size we really need to contain this extra record
  799. // and then check that it fits.
  800. //
  801. *RequiredLength += sizeof( SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX );
  802. if (Length < *RequiredLength) {
  803. Status = STATUS_INFO_LENGTH_MISMATCH;
  804. } else {
  805. //
  806. // Get the object header from the table entry and then copy over the information
  807. //
  808. ObjectHeader = (POBJECT_HEADER)(((ULONG_PTR)(ObjectTableEntry->Object)) & ~OBJ_HANDLE_ATTRIBUTES);
  809. (*HandleEntryInfo)->UniqueProcessId = (ULONG_PTR)UniqueProcessId;
  810. (*HandleEntryInfo)->HandleAttributes = (UCHAR)ObpGetHandleAttributes(ObjectTableEntry);
  811. (*HandleEntryInfo)->ObjectTypeIndex = (USHORT)(ObjectHeader->Type->Index);
  812. (*HandleEntryInfo)->HandleValue = (ULONG_PTR)(HandleIndex);
  813. (*HandleEntryInfo)->Object = &ObjectHeader->Body;
  814. (*HandleEntryInfo)->CreatorBackTraceIndex = 0;
  815. #if i386
  816. if (NtGlobalFlag & FLG_KERNEL_STACK_TRACE_DB) {
  817. (*HandleEntryInfo)->CreatorBackTraceIndex = ObjectTableEntry->CreatorBackTraceIndex & OBP_CREATOR_BACKTRACE_INDEX_MASK;
  818. (*HandleEntryInfo)->GrantedAccess = ObpTranslateGrantedAccessIndex( ObjectTableEntry->GrantedAccessIndex );
  819. } else {
  820. (*HandleEntryInfo)->GrantedAccess = ObpDecodeGrantedAccess(ObjectTableEntry->GrantedAccess);
  821. }
  822. #else
  823. (*HandleEntryInfo)->GrantedAccess = ObpDecodeGrantedAccess(ObjectTableEntry->GrantedAccess);
  824. #endif // i386
  825. (*HandleEntryInfo)++;
  826. Status = STATUS_SUCCESS;
  827. }
  828. return( Status );
  829. }
  830. POBJECT_HANDLE_COUNT_ENTRY
  831. ObpInsertHandleCount (
  832. POBJECT_HEADER ObjectHeader
  833. )
  834. /*++
  835. Routine Description:
  836. This function will increase the size of the handle database
  837. stored in the handle information of an object header. If
  838. necessary it will allocate new and free old handle databases.
  839. This routine should not be called if there is already free
  840. space in the handle table.
  841. Arguments:
  842. ObjectHeader - The object whose handle count is being incremented
  843. Return Value:
  844. The pointer to the next free handle count entry within the
  845. handle database.
  846. --*/
  847. {
  848. POBJECT_HEADER_HANDLE_INFO HandleInfo;
  849. POBJECT_HANDLE_COUNT_DATABASE OldHandleCountDataBase;
  850. POBJECT_HANDLE_COUNT_DATABASE NewHandleCountDataBase;
  851. POBJECT_HANDLE_COUNT_ENTRY FreeHandleCountEntry;
  852. ULONG CountEntries;
  853. ULONG OldSize;
  854. ULONG NewSize;
  855. OBJECT_HANDLE_COUNT_DATABASE SingleEntryDataBase;
  856. PAGED_CODE();
  857. //
  858. // Check if the object has any handle information
  859. //
  860. HandleInfo = OBJECT_HEADER_TO_HANDLE_INFO(ObjectHeader);
  861. if (HandleInfo == NULL) {
  862. return NULL;
  863. }
  864. //
  865. // The object does have some handle information. If it has
  866. // a single handle entry then we'll construct a local dummy
  867. // handle count database and come up with a new data base for
  868. // storing two entries.
  869. //
  870. if (ObjectHeader->Flags & OB_FLAG_SINGLE_HANDLE_ENTRY) {
  871. SingleEntryDataBase.CountEntries = 1;
  872. SingleEntryDataBase.HandleCountEntries[0] = HandleInfo->SingleEntry;
  873. OldHandleCountDataBase = &SingleEntryDataBase;
  874. OldSize = sizeof( SingleEntryDataBase );
  875. CountEntries = 2;
  876. NewSize = sizeof(OBJECT_HANDLE_COUNT_DATABASE) +
  877. ((CountEntries - 1) * sizeof( OBJECT_HANDLE_COUNT_ENTRY ));
  878. } else {
  879. //
  880. // The object already has multiple handles so we reference the
  881. // current handle database, and compute a new size bumped by four
  882. //
  883. OldHandleCountDataBase = HandleInfo->HandleCountDataBase;
  884. CountEntries = OldHandleCountDataBase->CountEntries;
  885. OldSize = sizeof(OBJECT_HANDLE_COUNT_DATABASE) +
  886. ((CountEntries - 1) * sizeof( OBJECT_HANDLE_COUNT_ENTRY));
  887. CountEntries += 4;
  888. NewSize = sizeof(OBJECT_HANDLE_COUNT_DATABASE) +
  889. ((CountEntries - 1) * sizeof( OBJECT_HANDLE_COUNT_ENTRY));
  890. }
  891. //
  892. // Now allocate pool for the new handle database.
  893. //
  894. NewHandleCountDataBase = ExAllocatePoolWithTag(PagedPool, NewSize,'dHbO');
  895. if (NewHandleCountDataBase == NULL) {
  896. return NULL;
  897. }
  898. //
  899. // Copy over the old database. Note that this might just copy our
  900. // local dummy entry for the single entry case
  901. //
  902. RtlCopyMemory(NewHandleCountDataBase, OldHandleCountDataBase, OldSize);
  903. //
  904. // If the previous mode was a single entry then remove that
  905. // indication otherwise free up with previous handle database
  906. //
  907. if (ObjectHeader->Flags & OB_FLAG_SINGLE_HANDLE_ENTRY) {
  908. ObjectHeader->Flags &= ~OB_FLAG_SINGLE_HANDLE_ENTRY;
  909. } else {
  910. ExFreePool( OldHandleCountDataBase );
  911. }
  912. //
  913. // Find the end of the new database that is used and zero out
  914. // the memory
  915. //
  916. FreeHandleCountEntry =
  917. (POBJECT_HANDLE_COUNT_ENTRY)((PCHAR)NewHandleCountDataBase + OldSize);
  918. RtlZeroMemory(FreeHandleCountEntry, NewSize - OldSize);
  919. //
  920. // Set the new database to the proper entry count and put it
  921. // all in the object
  922. //
  923. NewHandleCountDataBase->CountEntries = CountEntries;
  924. HandleInfo->HandleCountDataBase = NewHandleCountDataBase;
  925. //
  926. // And return to our caller
  927. //
  928. return FreeHandleCountEntry;
  929. }
  930. NTSTATUS
  931. ObpIncrementHandleDataBase (
  932. IN POBJECT_HEADER ObjectHeader,
  933. IN PEPROCESS Process,
  934. OUT PULONG NewProcessHandleCount
  935. )
  936. /*++
  937. Routine Description:
  938. This function increments the handle count database associated with the
  939. specified object for a specified process. This function is assuming that
  940. is called only for MaintainHandleCount == TRUE.
  941. Arguments:
  942. ObjectHeader - Supplies a pointer to the object.
  943. Process - Supplies a pointer to the process whose handle count is to be
  944. updated.
  945. NewProcessHandleCount - Supplies a pointer to a variable that receives
  946. the new handle count for the process.
  947. Return Value:
  948. An appropriate status value
  949. --*/
  950. {
  951. POBJECT_HEADER_HANDLE_INFO HandleInfo;
  952. POBJECT_HANDLE_COUNT_DATABASE HandleCountDataBase;
  953. POBJECT_HANDLE_COUNT_ENTRY HandleCountEntry;
  954. POBJECT_HANDLE_COUNT_ENTRY FreeHandleCountEntry;
  955. ULONG CountEntries;
  956. PAGED_CODE();
  957. //
  958. // Translate the object header to the handle information.
  959. // The HandleInfo can't be null because this function should be
  960. // called only if MaintainHandleCount == TRUE
  961. //
  962. HandleInfo = OBJECT_HEADER_TO_HANDLE_INFO(ObjectHeader);
  963. //
  964. // Check if the object has space for only a single handle
  965. //
  966. if (ObjectHeader->Flags & OB_FLAG_SINGLE_HANDLE_ENTRY) {
  967. //
  968. // If the single handle isn't in use then set the entry
  969. // and tell the caller there is only one handle
  970. //
  971. if (HandleInfo->SingleEntry.HandleCount == 0) {
  972. *NewProcessHandleCount = 1;
  973. HandleInfo->SingleEntry.HandleCount = 1;
  974. HandleInfo->SingleEntry.Process = Process;
  975. return STATUS_SUCCESS;
  976. //
  977. // If the single entry is for the same process as specified
  978. // then increment the count and we're done
  979. //
  980. } else if (HandleInfo->SingleEntry.Process == Process) {
  981. *NewProcessHandleCount = ++HandleInfo->SingleEntry.HandleCount;
  982. return STATUS_SUCCESS;
  983. //
  984. // Finally we have a object with a single handle entry already
  985. // in use, so we need to grow the handle database before
  986. // we can set this new value
  987. //
  988. } else {
  989. FreeHandleCountEntry = ObpInsertHandleCount( ObjectHeader );
  990. if (FreeHandleCountEntry == NULL) {
  991. return STATUS_INSUFFICIENT_RESOURCES;
  992. }
  993. FreeHandleCountEntry->Process = Process;
  994. FreeHandleCountEntry->HandleCount = 1;
  995. *NewProcessHandleCount = 1;
  996. return STATUS_SUCCESS;
  997. }
  998. }
  999. //
  1000. // The object does not contain a single entry, therefore we're
  1001. // assuming it already has a handle count database
  1002. //
  1003. HandleCountDataBase = HandleInfo->HandleCountDataBase;
  1004. FreeHandleCountEntry = NULL;
  1005. if (HandleCountDataBase != NULL) {
  1006. //
  1007. // Get the number of entries and a pointer to the first one
  1008. // in the handle database
  1009. //
  1010. CountEntries = HandleCountDataBase->CountEntries;
  1011. HandleCountEntry = &HandleCountDataBase->HandleCountEntries[ 0 ];
  1012. //
  1013. // For each entry in the handle database check for a process
  1014. // match and if so then increment the handle count and return
  1015. // to our caller. Otherwise if the entry is free then remember
  1016. // it so we can store to it later
  1017. //
  1018. while (CountEntries) {
  1019. if (HandleCountEntry->Process == Process) {
  1020. *NewProcessHandleCount = ++HandleCountEntry->HandleCount;
  1021. return STATUS_SUCCESS;
  1022. } else if (HandleCountEntry->HandleCount == 0) {
  1023. FreeHandleCountEntry = HandleCountEntry;
  1024. }
  1025. ++HandleCountEntry;
  1026. --CountEntries;
  1027. }
  1028. //
  1029. // If we did not find a free handle entry then we have to grow the
  1030. // handle database before we can set this new value
  1031. //
  1032. if (FreeHandleCountEntry == NULL) {
  1033. FreeHandleCountEntry = ObpInsertHandleCount( ObjectHeader );
  1034. if (FreeHandleCountEntry == NULL) {
  1035. return(STATUS_INSUFFICIENT_RESOURCES);
  1036. }
  1037. }
  1038. FreeHandleCountEntry->Process = Process;
  1039. FreeHandleCountEntry->HandleCount = 1;
  1040. *NewProcessHandleCount = 1;
  1041. }
  1042. return STATUS_SUCCESS;
  1043. }
  1044. NTSTATUS
  1045. ObpIncrementHandleCount (
  1046. OB_OPEN_REASON OpenReason,
  1047. PEPROCESS Process,
  1048. PVOID Object,
  1049. POBJECT_TYPE ObjectType,
  1050. PACCESS_STATE AccessState OPTIONAL,
  1051. KPROCESSOR_MODE AccessMode,
  1052. ULONG Attributes
  1053. )
  1054. /*++
  1055. Routine Description:
  1056. Increments the count of number of handles to the given object.
  1057. If the object is being opened or created, access validation and
  1058. auditing will be performed as appropriate.
  1059. Arguments:
  1060. OpenReason - Supplies the reason the handle count is being incremented.
  1061. Process - Pointer to the process in which the new handle will reside.
  1062. Object - Supplies a pointer to the body of the object.
  1063. ObjectType - Supplies the type of the object.
  1064. AccessState - Optional parameter supplying the current accumulated
  1065. security information describing the attempt to access the object.
  1066. AccessMode - Supplies the mode of the requestor.
  1067. Attributes - Desired attributes for the handle
  1068. Return Value:
  1069. An appropriate status value
  1070. --*/
  1071. {
  1072. NTSTATUS Status = STATUS_SUCCESS;
  1073. ULONG ProcessHandleCount;
  1074. BOOLEAN ExclusiveHandle;
  1075. POBJECT_HEADER_CREATOR_INFO CreatorInfo;
  1076. POBJECT_HEADER ObjectHeader;
  1077. BOOLEAN HasPrivilege = FALSE;
  1078. PRIVILEGE_SET Privileges;
  1079. BOOLEAN NewObject;
  1080. BOOLEAN HoldObjectTypeMutex = FALSE;
  1081. KPROCESSOR_MODE AccessCheckMode;
  1082. ULONG NewTotal;
  1083. PAGED_CODE();
  1084. ObpValidateIrql( "ObpIncrementHandleCount" );
  1085. //
  1086. // Get a pointer to the object header
  1087. //
  1088. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1089. //
  1090. // If the caller asked us to always do an access check then use user mode for previous mode
  1091. //
  1092. if (Attributes & OBJ_FORCE_ACCESS_CHECK) {
  1093. AccessCheckMode = UserMode;
  1094. } else {
  1095. AccessCheckMode = AccessMode;
  1096. }
  1097. ExclusiveHandle = FALSE;
  1098. HoldObjectTypeMutex = TRUE;
  1099. ObpLockObject( ObjectHeader );
  1100. try {
  1101. //
  1102. // Charge the user quota for the object
  1103. //
  1104. Status = ObpChargeQuotaForObject( ObjectHeader, ObjectType, &NewObject );
  1105. if (!NT_SUCCESS( Status )) {
  1106. leave;
  1107. }
  1108. //
  1109. // Check if the caller wants exlusive access and if so then
  1110. // make sure the attributes and header flags match up correctly
  1111. //
  1112. if (Attributes & OBJ_EXCLUSIVE) {
  1113. if ((Attributes & OBJ_INHERIT) ||
  1114. ((ObjectHeader->Flags & OB_FLAG_EXCLUSIVE_OBJECT) == 0)) {
  1115. Status = STATUS_INVALID_PARAMETER;
  1116. leave;
  1117. }
  1118. if (((OBJECT_HEADER_TO_EXCLUSIVE_PROCESS(ObjectHeader) == NULL) &&
  1119. (ObjectHeader->HandleCount != 0))
  1120. ||
  1121. ((OBJECT_HEADER_TO_EXCLUSIVE_PROCESS(ObjectHeader) != NULL) &&
  1122. (OBJECT_HEADER_TO_EXCLUSIVE_PROCESS(ObjectHeader) != PsGetCurrentProcess()))) {
  1123. Status = STATUS_ACCESS_DENIED;
  1124. leave;
  1125. }
  1126. ExclusiveHandle = TRUE;
  1127. //
  1128. // The user doesn't want exclusive access so now check to make sure
  1129. // the attriutes and header flags match up correctly
  1130. //
  1131. } else if ((ObjectHeader->Flags & OB_FLAG_EXCLUSIVE_OBJECT) &&
  1132. (OBJECT_HEADER_TO_EXCLUSIVE_PROCESS(ObjectHeader) != NULL)) {
  1133. Status = STATUS_ACCESS_DENIED;
  1134. leave;
  1135. }
  1136. //
  1137. // If handle count going from zero to one for an existing object that
  1138. // maintains a handle count database, but does not have an open procedure
  1139. // just a close procedure, then fail the call as they are trying to
  1140. // reopen an object by pointer and the close procedure will not know
  1141. // that the object has been 'recreated'
  1142. //
  1143. if ((ObjectHeader->HandleCount == 0) &&
  1144. (!NewObject) &&
  1145. (ObjectType->TypeInfo.MaintainHandleCount) &&
  1146. (ObjectType->TypeInfo.OpenProcedure == NULL) &&
  1147. (ObjectType->TypeInfo.CloseProcedure != NULL)) {
  1148. Status = STATUS_UNSUCCESSFUL;
  1149. leave;
  1150. }
  1151. if ((OpenReason == ObOpenHandle) ||
  1152. ((OpenReason == ObDuplicateHandle) && ARGUMENT_PRESENT(AccessState))) {
  1153. //
  1154. // Perform Access Validation to see if we can open this
  1155. // (already existing) object.
  1156. //
  1157. if (!ObCheckObjectAccess( Object,
  1158. AccessState,
  1159. TRUE,
  1160. AccessCheckMode,
  1161. &Status )) {
  1162. leave;
  1163. }
  1164. } else if ((OpenReason == ObCreateHandle)) {
  1165. //
  1166. // We are creating a new instance of this object type.
  1167. // A total of three audit messages may be generated:
  1168. //
  1169. // 1 - Audit the attempt to create an instance of this
  1170. // object type.
  1171. //
  1172. // 2 - Audit the successful creation.
  1173. //
  1174. // 3 - Audit the allocation of the handle.
  1175. //
  1176. //
  1177. // At this point, the RemainingDesiredAccess field in
  1178. // the AccessState may still contain either Generic access
  1179. // types, or MAXIMUM_ALLOWED. We will map the generics
  1180. // and substitute GenericAll for MAXIMUM_ALLOWED.
  1181. //
  1182. if ( AccessState->RemainingDesiredAccess & MAXIMUM_ALLOWED ) {
  1183. AccessState->RemainingDesiredAccess &= ~MAXIMUM_ALLOWED;
  1184. AccessState->RemainingDesiredAccess |= GENERIC_ALL;
  1185. }
  1186. if ((GENERIC_ACCESS & AccessState->RemainingDesiredAccess) != 0) {
  1187. RtlMapGenericMask( &AccessState->RemainingDesiredAccess,
  1188. &ObjectType->TypeInfo.GenericMapping );
  1189. }
  1190. //
  1191. // Since we are creating the object, we can give any access the caller
  1192. // wants. The only exception is ACCESS_SYSTEM_SECURITY, which requires
  1193. // a privilege.
  1194. //
  1195. if ( AccessState->RemainingDesiredAccess & ACCESS_SYSTEM_SECURITY ) {
  1196. //
  1197. // We could use SeSinglePrivilegeCheck here, but it
  1198. // captures the subject context again, and we don't
  1199. // want to do that in this path for performance reasons.
  1200. //
  1201. Privileges.PrivilegeCount = 1;
  1202. Privileges.Control = PRIVILEGE_SET_ALL_NECESSARY;
  1203. Privileges.Privilege[0].Luid = SeSecurityPrivilege;
  1204. Privileges.Privilege[0].Attributes = 0;
  1205. HasPrivilege = SePrivilegeCheck( &Privileges,
  1206. &AccessState->SubjectSecurityContext,
  1207. AccessCheckMode );
  1208. if (!HasPrivilege) {
  1209. SePrivilegedServiceAuditAlarm ( NULL,
  1210. &AccessState->SubjectSecurityContext,
  1211. &Privileges,
  1212. FALSE );
  1213. Status = STATUS_PRIVILEGE_NOT_HELD;
  1214. leave;
  1215. }
  1216. AccessState->RemainingDesiredAccess &= ~ACCESS_SYSTEM_SECURITY;
  1217. AccessState->PreviouslyGrantedAccess |= ACCESS_SYSTEM_SECURITY;
  1218. (VOID) SeAppendPrivileges( AccessState,
  1219. &Privileges );
  1220. }
  1221. }
  1222. if (ExclusiveHandle) {
  1223. OBJECT_HEADER_TO_QUOTA_INFO(ObjectHeader)->ExclusiveProcess = Process;
  1224. }
  1225. ObpIncrHandleCount( ObjectHeader );
  1226. ProcessHandleCount = 0;
  1227. //
  1228. // If the object type wants us to keep try of the handle counts
  1229. // then call our routine to do the work
  1230. //
  1231. if (ObjectType->TypeInfo.MaintainHandleCount) {
  1232. Status = ObpIncrementHandleDataBase( ObjectHeader,
  1233. Process,
  1234. &ProcessHandleCount );
  1235. if (!NT_SUCCESS(Status)) {
  1236. //
  1237. // The only thing we need to do is to remove the
  1238. // reference added before. The quota charge will be
  1239. // returned at object deletion
  1240. //
  1241. ObpDecrHandleCount( ObjectHeader );
  1242. leave;
  1243. }
  1244. }
  1245. ObpUnlockObject( ObjectHeader );
  1246. HoldObjectTypeMutex = FALSE;
  1247. //
  1248. // Set our preliminary status now to success because
  1249. // the call to the open procedure might change this
  1250. //
  1251. Status = STATUS_SUCCESS;
  1252. //
  1253. // If the object type has an open procedure
  1254. // then invoke that procedure
  1255. //
  1256. if (ObjectType->TypeInfo.OpenProcedure != NULL) {
  1257. #if DBG
  1258. KIRQL SaveIrql;
  1259. #endif
  1260. //
  1261. // Leave the object type mutex when call the OpenProcedure. If an exception
  1262. // while OpenProcedure the HoldObjectTypeMutex disable leaving the mutex
  1263. // on finally block
  1264. //
  1265. ObpBeginTypeSpecificCallOut( SaveIrql );
  1266. Status = (*ObjectType->TypeInfo.OpenProcedure)( OpenReason,
  1267. Process,
  1268. Object,
  1269. AccessState ?
  1270. AccessState->PreviouslyGrantedAccess :
  1271. 0,
  1272. ProcessHandleCount );
  1273. ObpEndTypeSpecificCallOut( SaveIrql, "Open", ObjectType, Object );
  1274. //
  1275. // Hold back the object type mutex and set the HoldObjectTypeMutex variable
  1276. // to allow releasing the mutex while leaving this procedure
  1277. //
  1278. if (!NT_SUCCESS(Status)) {
  1279. ObpLockObject( ObjectHeader );
  1280. HoldObjectTypeMutex = TRUE;
  1281. (VOID)ObpDecrHandleCount( ObjectHeader );
  1282. leave;
  1283. }
  1284. }
  1285. //
  1286. // Get the objects creator info block and insert it on the
  1287. // global list of objects for that type
  1288. //
  1289. if (OpenReason == ObCreateHandle) {
  1290. CreatorInfo = OBJECT_HEADER_TO_CREATOR_INFO( ObjectHeader );
  1291. if (CreatorInfo != NULL) {
  1292. ObpEnterObjectTypeMutex( ObjectType );
  1293. InsertTailList( &ObjectType->TypeList, &CreatorInfo->TypeList );
  1294. ObpLeaveObjectTypeMutex( ObjectType );
  1295. }
  1296. }
  1297. //
  1298. // Do some simple bookkeeping for the handle counts
  1299. // and then return to our caller
  1300. //
  1301. NewTotal = (ULONG) InterlockedIncrement((PLONG)&ObjectType->TotalNumberOfHandles);
  1302. //
  1303. // Note: The hightwather is only for bookeeping. We can do this w/o
  1304. // lock. In the worst case next time will be updated
  1305. //
  1306. if (NewTotal > ObjectType->HighWaterNumberOfHandles) {
  1307. ObjectType->HighWaterNumberOfHandles = NewTotal;
  1308. }
  1309. } finally {
  1310. if ( HoldObjectTypeMutex ) {
  1311. ObpUnlockObject( ObjectHeader );
  1312. }
  1313. }
  1314. return( Status );
  1315. }
  1316. NTSTATUS
  1317. ObpIncrementUnnamedHandleCount (
  1318. PACCESS_MASK DesiredAccess,
  1319. PEPROCESS Process,
  1320. PVOID Object,
  1321. POBJECT_TYPE ObjectType,
  1322. KPROCESSOR_MODE AccessMode,
  1323. ULONG Attributes
  1324. )
  1325. /*++
  1326. Routine Description:
  1327. Increments the count of number of handles to the given object.
  1328. Arguments:
  1329. Desired Access - Supplies the desired access to the object and receives
  1330. the assign access mask
  1331. Process - Pointer to the process in which the new handle will reside.
  1332. Object - Supplies a pointer to the body of the object.
  1333. ObjectType - Supplies the type of the object.
  1334. AccessMode - Supplies the mode of the requestor.
  1335. Attributes - Desired attributes for the handle
  1336. Return Value:
  1337. An appropriate status value
  1338. --*/
  1339. {
  1340. NTSTATUS Status = STATUS_SUCCESS;
  1341. BOOLEAN ExclusiveHandle;
  1342. POBJECT_HEADER_CREATOR_INFO CreatorInfo;
  1343. POBJECT_HEADER ObjectHeader;
  1344. BOOLEAN NewObject;
  1345. ULONG ProcessHandleCount;
  1346. BOOLEAN HoldObjectTypeMutex = FALSE;
  1347. ULONG NewTotal;
  1348. PAGED_CODE();
  1349. UNREFERENCED_PARAMETER (AccessMode);
  1350. ObpValidateIrql( "ObpIncrementUnnamedHandleCount" );
  1351. //
  1352. // Get a pointer to the object header
  1353. //
  1354. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1355. ExclusiveHandle = FALSE;
  1356. HoldObjectTypeMutex = TRUE;
  1357. ObpLockObject( ObjectHeader );
  1358. try {
  1359. //
  1360. // Charge the user quota for the object
  1361. //
  1362. Status = ObpChargeQuotaForObject( ObjectHeader, ObjectType, &NewObject );
  1363. if (!NT_SUCCESS( Status )) {
  1364. leave;
  1365. }
  1366. //
  1367. // Check if the caller wants exlusive access and if so then
  1368. // make sure the attributes and header flags match up correctly
  1369. //
  1370. if (Attributes & OBJ_EXCLUSIVE) {
  1371. if ((Attributes & OBJ_INHERIT) ||
  1372. ((ObjectHeader->Flags & OB_FLAG_EXCLUSIVE_OBJECT) == 0)) {
  1373. Status = STATUS_INVALID_PARAMETER;
  1374. leave;
  1375. }
  1376. if (((OBJECT_HEADER_TO_EXCLUSIVE_PROCESS(ObjectHeader) == NULL) &&
  1377. (ObjectHeader->HandleCount != 0))
  1378. ||
  1379. ((OBJECT_HEADER_TO_EXCLUSIVE_PROCESS(ObjectHeader) != NULL) &&
  1380. (OBJECT_HEADER_TO_EXCLUSIVE_PROCESS(ObjectHeader) != PsGetCurrentProcess()))) {
  1381. Status = STATUS_ACCESS_DENIED;
  1382. leave;
  1383. }
  1384. ExclusiveHandle = TRUE;
  1385. //
  1386. // The user doesn't want exclusive access so now check to make sure
  1387. // the attriutes and header flags match up correctly
  1388. //
  1389. } else if ((ObjectHeader->Flags & OB_FLAG_EXCLUSIVE_OBJECT) &&
  1390. (OBJECT_HEADER_TO_EXCLUSIVE_PROCESS(ObjectHeader) != NULL)) {
  1391. Status = STATUS_ACCESS_DENIED;
  1392. leave;
  1393. }
  1394. //
  1395. // If handle count going from zero to one for an existing object that
  1396. // maintains a handle count database, but does not have an open procedure
  1397. // just a close procedure, then fail the call as they are trying to
  1398. // reopen an object by pointer and the close procedure will not know
  1399. // that the object has been 'recreated'
  1400. //
  1401. if ((ObjectHeader->HandleCount == 0) &&
  1402. (!NewObject) &&
  1403. (ObjectType->TypeInfo.MaintainHandleCount) &&
  1404. (ObjectType->TypeInfo.OpenProcedure == NULL) &&
  1405. (ObjectType->TypeInfo.CloseProcedure != NULL)) {
  1406. Status = STATUS_UNSUCCESSFUL;
  1407. leave;
  1408. }
  1409. //
  1410. // If the user asked for the maximum allowed then remove the bit and
  1411. // or in generic all access
  1412. //
  1413. if ( *DesiredAccess & MAXIMUM_ALLOWED ) {
  1414. *DesiredAccess &= ~MAXIMUM_ALLOWED;
  1415. *DesiredAccess |= GENERIC_ALL;
  1416. }
  1417. // If the user asked for any generic bit then translate it to
  1418. // someone more appropriate for the object type
  1419. //
  1420. if ((GENERIC_ACCESS & *DesiredAccess) != 0) {
  1421. RtlMapGenericMask( DesiredAccess,
  1422. &ObjectType->TypeInfo.GenericMapping );
  1423. }
  1424. if (ExclusiveHandle) {
  1425. OBJECT_HEADER_TO_QUOTA_INFO(ObjectHeader)->ExclusiveProcess = Process;
  1426. }
  1427. ObpIncrHandleCount( ObjectHeader );
  1428. ProcessHandleCount = 0;
  1429. //
  1430. // If the object type wants us to keep try of the handle counts
  1431. // then call our routine to do the work
  1432. //
  1433. if (ObjectType->TypeInfo.MaintainHandleCount) {
  1434. Status = ObpIncrementHandleDataBase( ObjectHeader,
  1435. Process,
  1436. &ProcessHandleCount );
  1437. if (!NT_SUCCESS(Status)) {
  1438. //
  1439. // The only thing we need to do is to remove the
  1440. // reference added before. The quota charge will be
  1441. // returned at object deletion.
  1442. //
  1443. ObpDecrHandleCount( ObjectHeader );
  1444. leave;
  1445. }
  1446. }
  1447. ObpUnlockObject( ObjectHeader );
  1448. HoldObjectTypeMutex = FALSE;
  1449. //
  1450. // Set our preliminary status now to success because
  1451. // the call to the open procedure might change this
  1452. //
  1453. Status = STATUS_SUCCESS;
  1454. //
  1455. // If the object type has an open procedure
  1456. // then invoke that procedure
  1457. //
  1458. if (ObjectType->TypeInfo.OpenProcedure != NULL) {
  1459. #if DBG
  1460. KIRQL SaveIrql;
  1461. #endif
  1462. //
  1463. // Call the OpenProcedure. If an exception
  1464. // while OpenProcedure the HoldObjectTypeMutex disable leaving the mutex
  1465. // on finally block
  1466. //
  1467. ObpBeginTypeSpecificCallOut( SaveIrql );
  1468. Status = (*ObjectType->TypeInfo.OpenProcedure)( ObCreateHandle,
  1469. Process,
  1470. Object,
  1471. *DesiredAccess,
  1472. ProcessHandleCount );
  1473. ObpEndTypeSpecificCallOut( SaveIrql, "Open", ObjectType, Object );
  1474. //
  1475. // Hold back the object type mutex and set the HoldObjectTypeMutex variable
  1476. // to allow releasing the mutex while leaving this procedure
  1477. //
  1478. if (!NT_SUCCESS(Status)) {
  1479. ObpLockObject( ObjectHeader );
  1480. HoldObjectTypeMutex = TRUE;
  1481. (VOID)ObpDecrHandleCount( ObjectHeader );
  1482. leave;
  1483. }
  1484. }
  1485. //
  1486. // Get a pointer to the creator info block for the object and insert
  1487. // it on the global list of object for that type
  1488. //
  1489. CreatorInfo = OBJECT_HEADER_TO_CREATOR_INFO( ObjectHeader );
  1490. if (CreatorInfo != NULL) {
  1491. ObpEnterObjectTypeMutex( ObjectType );
  1492. InsertTailList( &ObjectType->TypeList, &CreatorInfo->TypeList );
  1493. ObpLeaveObjectTypeMutex( ObjectType );
  1494. }
  1495. //
  1496. // Do some simple bookkeeping for the handle counts
  1497. // and then return to our caller
  1498. //
  1499. NewTotal = (ULONG) InterlockedIncrement((PLONG)&ObjectType->TotalNumberOfHandles);
  1500. if (NewTotal > ObjectType->HighWaterNumberOfHandles) {
  1501. ObjectType->HighWaterNumberOfHandles = NewTotal;
  1502. }
  1503. } finally {
  1504. if ( HoldObjectTypeMutex ) {
  1505. ObpUnlockObject( ObjectHeader );
  1506. }
  1507. }
  1508. return( Status );
  1509. }
  1510. NTSTATUS
  1511. ObpChargeQuotaForObject (
  1512. IN POBJECT_HEADER ObjectHeader,
  1513. IN POBJECT_TYPE ObjectType,
  1514. OUT PBOOLEAN NewObject
  1515. )
  1516. /*++
  1517. Routine Description:
  1518. This routine charges quota against the current process for the new
  1519. object
  1520. Arguments:
  1521. ObjectHeader - Supplies a pointer to the new object being charged for
  1522. ObjectType - Supplies the type of the new object
  1523. NewObject - Returns true if the object is really new and false otherwise
  1524. Return Value:
  1525. An appropriate status value
  1526. --*/
  1527. {
  1528. POBJECT_HEADER_QUOTA_INFO QuotaInfo;
  1529. ULONG NonPagedPoolCharge;
  1530. ULONG PagedPoolCharge;
  1531. //
  1532. // Get a pointer to the quota block for this object
  1533. //
  1534. QuotaInfo = OBJECT_HEADER_TO_QUOTA_INFO( ObjectHeader );
  1535. *NewObject = FALSE;
  1536. //
  1537. // If the object is new then we have work to do otherwise
  1538. // we'll return with NewObject set to false
  1539. //
  1540. if (ObjectHeader->Flags & OB_FLAG_NEW_OBJECT) {
  1541. //
  1542. // Say the object now isn't new
  1543. //
  1544. ObjectHeader->Flags &= ~OB_FLAG_NEW_OBJECT;
  1545. //
  1546. // If there does exist a quota info structure for this
  1547. // object then calculate what our charge should be from
  1548. // the information stored in that structure
  1549. //
  1550. if (QuotaInfo != NULL) {
  1551. PagedPoolCharge = QuotaInfo->PagedPoolCharge +
  1552. QuotaInfo->SecurityDescriptorCharge;
  1553. NonPagedPoolCharge = QuotaInfo->NonPagedPoolCharge;
  1554. } else {
  1555. //
  1556. // There isn't any quota information so we're on our own
  1557. // Paged pool charge is the default for the object plus
  1558. // the security descriptor if present. Nonpaged pool charge
  1559. // is the default for the object.
  1560. //
  1561. PagedPoolCharge = ObjectType->TypeInfo.DefaultPagedPoolCharge;
  1562. if (ObjectHeader->SecurityDescriptor != NULL) {
  1563. ObjectHeader->Flags |= OB_FLAG_DEFAULT_SECURITY_QUOTA;
  1564. PagedPoolCharge += SE_DEFAULT_SECURITY_QUOTA;
  1565. }
  1566. NonPagedPoolCharge = ObjectType->TypeInfo.DefaultNonPagedPoolCharge;
  1567. }
  1568. //
  1569. // Now charge for the quota and make sure it succeeds
  1570. //
  1571. ObjectHeader->QuotaBlockCharged = (PVOID)PsChargeSharedPoolQuota( PsGetCurrentProcess(),
  1572. PagedPoolCharge,
  1573. NonPagedPoolCharge );
  1574. if (ObjectHeader->QuotaBlockCharged == NULL) {
  1575. return STATUS_QUOTA_EXCEEDED;
  1576. }
  1577. *NewObject = TRUE;
  1578. }
  1579. return STATUS_SUCCESS;
  1580. }
  1581. VOID
  1582. ObpDecrementHandleCount (
  1583. PEPROCESS Process,
  1584. POBJECT_HEADER ObjectHeader,
  1585. POBJECT_TYPE ObjectType,
  1586. ACCESS_MASK GrantedAccess
  1587. )
  1588. /*++
  1589. Routine Description:
  1590. This procedure decrements the handle count for the specified object
  1591. Arguments:
  1592. Process - Supplies the process where the handle existed
  1593. ObjectHeader - Supplies a pointer to the object header for the object
  1594. ObjectType - Supplies a type of the object
  1595. GrantedAccess - Supplies the current access mask to the object
  1596. Return Value:
  1597. None.
  1598. --*/
  1599. {
  1600. POBJECT_HEADER_HANDLE_INFO HandleInfo;
  1601. POBJECT_HANDLE_COUNT_DATABASE HandleCountDataBase;
  1602. POBJECT_HANDLE_COUNT_ENTRY HandleCountEntry;
  1603. PVOID Object;
  1604. ULONG CountEntries;
  1605. ULONG ProcessHandleCount;
  1606. ULONG_PTR SystemHandleCount;
  1607. LOGICAL HandleCountIsZero;
  1608. PAGED_CODE();
  1609. Object = (PVOID)&ObjectHeader->Body;
  1610. ProcessHandleCount = 0;
  1611. ObpLockObject( ObjectHeader );
  1612. SystemHandleCount = ObjectHeader->HandleCount;
  1613. //
  1614. // Decrement the handle count and it was one and it
  1615. // was an exclusive object then zero out the exclusive
  1616. // process
  1617. //
  1618. HandleCountIsZero = ObpDecrHandleCount( ObjectHeader );
  1619. if ( HandleCountIsZero &&
  1620. (ObjectHeader->Flags & OB_FLAG_EXCLUSIVE_OBJECT)) {
  1621. OBJECT_HEADER_TO_QUOTA_INFO( ObjectHeader )->ExclusiveProcess = NULL;
  1622. }
  1623. //
  1624. // If the object maintains a handle count database then
  1625. // search through the handle database and decrement
  1626. // the necessary information
  1627. //
  1628. if (ObjectType->TypeInfo.MaintainHandleCount) {
  1629. HandleInfo = OBJECT_HEADER_TO_HANDLE_INFO( ObjectHeader );
  1630. //
  1631. // Check if there is a single handle entry, then it better
  1632. // be ours
  1633. //
  1634. if (ObjectHeader->Flags & OB_FLAG_SINGLE_HANDLE_ENTRY) {
  1635. ASSERT(HandleInfo->SingleEntry.Process == Process);
  1636. ASSERT(HandleInfo->SingleEntry.HandleCount > 0);
  1637. ProcessHandleCount = HandleInfo->SingleEntry.HandleCount--;
  1638. HandleCountEntry = &HandleInfo->SingleEntry;
  1639. } else {
  1640. //
  1641. // Otherwise search the database for a process match
  1642. //
  1643. HandleCountDataBase = HandleInfo->HandleCountDataBase;
  1644. if (HandleCountDataBase != NULL) {
  1645. CountEntries = HandleCountDataBase->CountEntries;
  1646. HandleCountEntry = &HandleCountDataBase->HandleCountEntries[ 0 ];
  1647. while (CountEntries) {
  1648. if ((HandleCountEntry->HandleCount != 0) &&
  1649. (HandleCountEntry->Process == Process)) {
  1650. ProcessHandleCount = HandleCountEntry->HandleCount--;
  1651. break;
  1652. }
  1653. HandleCountEntry++;
  1654. CountEntries--;
  1655. }
  1656. }
  1657. else {
  1658. HandleCountEntry = NULL;
  1659. }
  1660. }
  1661. //
  1662. // Now if the process is giving up all handles to the object
  1663. // then zero out the handle count entry. For a single handle
  1664. // entry this is just the single entry in the header handle info
  1665. // structure
  1666. //
  1667. if (ProcessHandleCount == 1) {
  1668. HandleCountEntry->Process = NULL;
  1669. HandleCountEntry->HandleCount = 0;
  1670. }
  1671. }
  1672. ObpUnlockObject( ObjectHeader );
  1673. //
  1674. // If the Object Type has a Close Procedure, then release the type
  1675. // mutex before calling it, and then call ObpDeleteNameCheck without
  1676. // the mutex held.
  1677. //
  1678. if (ObjectType->TypeInfo.CloseProcedure) {
  1679. #if DBG
  1680. KIRQL SaveIrql;
  1681. #endif
  1682. ObpBeginTypeSpecificCallOut( SaveIrql );
  1683. (*ObjectType->TypeInfo.CloseProcedure)( Process,
  1684. Object,
  1685. GrantedAccess,
  1686. ProcessHandleCount,
  1687. SystemHandleCount );
  1688. ObpEndTypeSpecificCallOut( SaveIrql, "Close", ObjectType, Object );
  1689. }
  1690. ObpDeleteNameCheck( Object );
  1691. InterlockedDecrement((PLONG)&ObjectType->TotalNumberOfHandles);
  1692. return;
  1693. }
  1694. NTSTATUS
  1695. ObpCreateHandle (
  1696. IN OB_OPEN_REASON OpenReason,
  1697. IN PVOID Object,
  1698. IN POBJECT_TYPE ExpectedObjectType OPTIONAL,
  1699. IN PACCESS_STATE AccessState,
  1700. IN ULONG ObjectPointerBias OPTIONAL,
  1701. IN ULONG Attributes,
  1702. IN POBP_LOOKUP_CONTEXT LookupContext,
  1703. IN KPROCESSOR_MODE AccessMode,
  1704. OUT PVOID *ReferencedNewObject OPTIONAL,
  1705. OUT PHANDLE Handle
  1706. )
  1707. /*++
  1708. Routine Description:
  1709. This function creates a new handle to an existing object
  1710. Arguments:
  1711. OpenReason - The reason why we are doing this work
  1712. Object - A pointer to the body of the new object
  1713. ExpectedObjectType - Optionally Supplies the object type that
  1714. the caller is expecting
  1715. AccessState - Supplies the access state for the handle requested
  1716. by the caller
  1717. ObjectPointerBias - Optionally supplies a count of addition
  1718. increments we do to the pointer count for the object
  1719. Attributes - Desired attributes for the handle
  1720. DirectoryLocked - Indicates if the root directory mutex is already held
  1721. AccessMode - Supplies the mode of the requestor.
  1722. ReferencedNewObject - Optionally receives a pointer to the body
  1723. of the new object
  1724. Handle - Receives the new handle value
  1725. Return Value:
  1726. An appropriate status value
  1727. --*/
  1728. {
  1729. NTSTATUS Status;
  1730. POBJECT_HEADER ObjectHeader;
  1731. POBJECT_TYPE ObjectType;
  1732. PVOID ObjectTable;
  1733. HANDLE_TABLE_ENTRY ObjectTableEntry;
  1734. HANDLE NewHandle;
  1735. ACCESS_MASK DesiredAccess;
  1736. ACCESS_MASK GrantedAccess;
  1737. BOOLEAN AttachedToProcess = FALSE;
  1738. BOOLEAN KernelHandle = FALSE;
  1739. KAPC_STATE ApcState;
  1740. HANDLE_TABLE_ENTRY_INFO ObjectInfo;
  1741. PAGED_CODE();
  1742. ObpValidateIrql( "ObpCreateHandle" );
  1743. //
  1744. // Get a pointer to the object header and object type
  1745. //
  1746. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1747. ObjectType = ObjectHeader->Type;
  1748. //
  1749. // If the object type isn't what was expected then
  1750. // return an error to our caller, but first see if
  1751. // we should release the directory mutex
  1752. //
  1753. if ((ARGUMENT_PRESENT( ExpectedObjectType )) &&
  1754. (ObjectType != ExpectedObjectType )) {
  1755. if (LookupContext) {
  1756. ObpReleaseLookupContext( LookupContext );
  1757. }
  1758. return( STATUS_OBJECT_TYPE_MISMATCH );
  1759. }
  1760. //
  1761. // Set the first ulong of the object table entry to point
  1762. // back to the object header
  1763. //
  1764. ObjectTableEntry.Object = ObjectHeader;
  1765. //
  1766. // Now get a pointer to the object table for either the current process
  1767. // of the kernel handle table. OBJ_KERNEL_HANDLE dropped for user mode on capture.
  1768. //
  1769. if ((Attributes & OBJ_KERNEL_HANDLE) /* && (AccessMode == KernelMode) */) {
  1770. ObjectTable = ObpKernelHandleTable;
  1771. KernelHandle = TRUE;
  1772. //
  1773. // Go to the system process if we have to
  1774. //
  1775. if (PsGetCurrentProcess() != PsInitialSystemProcess) {
  1776. KeStackAttachProcess (&PsInitialSystemProcess->Pcb, &ApcState);
  1777. AttachedToProcess = TRUE;
  1778. }
  1779. } else {
  1780. ObjectTable = ObpGetObjectTable();
  1781. }
  1782. //
  1783. // ObpIncrementHandleCount will perform access checking on the
  1784. // object being opened as appropriate.
  1785. //
  1786. Status = ObpIncrementHandleCount( OpenReason,
  1787. PsGetCurrentProcess(),
  1788. Object,
  1789. ObjectType,
  1790. AccessState,
  1791. AccessMode,
  1792. Attributes );
  1793. if (AccessState->GenerateOnClose) {
  1794. Attributes |= OBJ_AUDIT_OBJECT_CLOSE;
  1795. }
  1796. //
  1797. // Or in some low order bits into the first ulong of the object
  1798. // table entry
  1799. //
  1800. ObjectTableEntry.ObAttributes |= (Attributes & OBJ_HANDLE_ATTRIBUTES);
  1801. //
  1802. // Merge both the remaining desired access and the currently
  1803. // granted access states into one mask and then compute
  1804. // the granted access
  1805. //
  1806. DesiredAccess = AccessState->RemainingDesiredAccess |
  1807. AccessState->PreviouslyGrantedAccess;
  1808. GrantedAccess = DesiredAccess &
  1809. (ObjectType->TypeInfo.ValidAccessMask | ACCESS_SYSTEM_SECURITY );
  1810. //
  1811. // AccessState->PreviouslyGrantedAccess is used for success audits in SeAuditHandleCreation() which uses it in calls
  1812. // to SepAdtPrivilegeObjectAuditAlarm() and SepAdtOpenObjectAuditAlarm(). Sanitize any bad bits from it.
  1813. //
  1814. AccessState->PreviouslyGrantedAccess = GrantedAccess;
  1815. //
  1816. // Compute and save away the audit mask for this object
  1817. // (if one exists). Note we only do this if the GenerateOnClose
  1818. // flag comes back in the access state, because this tells us
  1819. // that the audit is a result of what is in the SACL.
  1820. //
  1821. ObjectInfo.AuditMask = ((PAUX_ACCESS_DATA)AccessState->AuxData)->MaximumAuditMask;
  1822. //
  1823. // Unlock the directory if it is locked and make sure
  1824. // we've been successful so far
  1825. //
  1826. if (LookupContext) {
  1827. ObpReleaseLookupContext( LookupContext );
  1828. }
  1829. if (!NT_SUCCESS( Status )) {
  1830. //
  1831. // If we are attached to the system process then return
  1832. // back to our caller
  1833. //
  1834. if (AttachedToProcess) {
  1835. KeUnstackDetachProcess(&ApcState);
  1836. AttachedToProcess = FALSE;
  1837. }
  1838. return( Status );
  1839. }
  1840. //
  1841. // Bias the pointer count if that is what the caller wanted
  1842. //
  1843. if (ARGUMENT_PRESENT( ObjectPointerBias )) {
  1844. ObpIncrPointerCountEx (ObjectHeader, ObjectPointerBias);
  1845. }
  1846. //
  1847. // Set the granted access mask in the object table entry (second ulong)
  1848. //
  1849. #if i386
  1850. if (NtGlobalFlag & FLG_KERNEL_STACK_TRACE_DB) {
  1851. LONG StackTraceIndex;
  1852. ObjectTableEntry.GrantedAccessIndex = ObpComputeGrantedAccessIndex( GrantedAccess );
  1853. if (AccessMode == KernelMode) {
  1854. StackTraceIndex = RtlLogStackBackTrace();
  1855. } else {
  1856. StackTraceIndex = RtlLogUmodeStackBackTrace();
  1857. }
  1858. //
  1859. // Save the stack trace only if the index fits the CreatorBackTraceIndex
  1860. // minus the ProtectOnClose bit
  1861. //
  1862. if (StackTraceIndex < OBP_CREATOR_BACKTRACE_LIMIT) {
  1863. ObjectTableEntry.CreatorBackTraceIndex = (USHORT)StackTraceIndex;
  1864. } else {
  1865. ObjectTableEntry.CreatorBackTraceIndex = 0;
  1866. }
  1867. } else {
  1868. ObjectTableEntry.GrantedAccess = GrantedAccess;
  1869. }
  1870. #else
  1871. ObjectTableEntry.GrantedAccess = GrantedAccess;
  1872. #endif // i386
  1873. //
  1874. // Add this new object table entry to the object table for the process
  1875. //
  1876. ObpEncodeProtectClose(ObjectTableEntry);
  1877. NewHandle = ExCreateHandle( ObjectTable, &ObjectTableEntry );
  1878. //
  1879. // If we didn't get a handle then cleanup after ourselves and return
  1880. // the error to our caller
  1881. //
  1882. if (NewHandle == NULL) {
  1883. if (ARGUMENT_PRESENT( ObjectPointerBias )) {
  1884. ObpDecrPointerCountEx (ObjectHeader, ObjectPointerBias);
  1885. }
  1886. ObpDecrementHandleCount( PsGetCurrentProcess(),
  1887. ObjectHeader,
  1888. ObjectType,
  1889. GrantedAccess );
  1890. //
  1891. // If we are attached to the system process then return
  1892. // back to our caller
  1893. //
  1894. if (AttachedToProcess) {
  1895. KeUnstackDetachProcess(&ApcState);
  1896. AttachedToProcess = FALSE;
  1897. }
  1898. return( STATUS_INSUFFICIENT_RESOURCES );
  1899. }
  1900. if ( ObjectInfo.AuditMask != 0 ) {
  1901. PHANDLE_TABLE_ENTRY HandleTableEntry;
  1902. PKTHREAD CurrentThread;
  1903. CurrentThread = KeGetCurrentThread();
  1904. KeEnterCriticalRegionThread( CurrentThread );
  1905. //
  1906. // Make sure it's the same object before setting the handle information.
  1907. // The user may have closed it immediately after the ExCreateHandle call,
  1908. // so at this time it may either be invalid or a completely different object.
  1909. //
  1910. HandleTableEntry = ExMapHandleToPointer ( ObjectTable, NewHandle );
  1911. if (HandleTableEntry != NULL) {
  1912. if (((ULONG_PTR)(HandleTableEntry->Object) & ~OBJ_HANDLE_ATTRIBUTES) == (ULONG_PTR)ObjectHeader) {
  1913. ExSetHandleInfo(ObjectTable, NewHandle, &ObjectInfo, TRUE);
  1914. }
  1915. ExUnlockHandleTableEntry( ObjectTable, HandleTableEntry );
  1916. }
  1917. KeLeaveCriticalRegionThread( CurrentThread );
  1918. }
  1919. //
  1920. // We have a new Ex style handle now make it an ob style handle and also
  1921. // adjust for the kernel handle by setting the sign bit in the handle
  1922. // value
  1923. //
  1924. if (KernelHandle) {
  1925. NewHandle = EncodeKernelHandle( NewHandle );
  1926. }
  1927. *Handle = NewHandle;
  1928. //
  1929. // If requested, generate audit messages to indicate that a new handle
  1930. // has been allocated.
  1931. //
  1932. // This is the final security operation in the creation/opening of the
  1933. // object.
  1934. //
  1935. if ( AccessState->GenerateAudit ) {
  1936. SeAuditHandleCreation( AccessState,
  1937. *Handle );
  1938. }
  1939. if (OpenReason == ObCreateHandle) {
  1940. PAUX_ACCESS_DATA AuxData = AccessState->AuxData;
  1941. if ( ( AuxData->PrivilegesUsed != NULL) && (AuxData->PrivilegesUsed->PrivilegeCount > 0) ) {
  1942. SePrivilegeObjectAuditAlarm( *Handle,
  1943. &AccessState->SubjectSecurityContext,
  1944. GrantedAccess,
  1945. AuxData->PrivilegesUsed,
  1946. TRUE,
  1947. KeGetPreviousMode() );
  1948. }
  1949. }
  1950. //
  1951. // If the caller had a pointer bias and wanted the new reference object
  1952. // then return that value
  1953. //
  1954. if ((ARGUMENT_PRESENT( ObjectPointerBias )) &&
  1955. (ARGUMENT_PRESENT( ReferencedNewObject ))) {
  1956. *ReferencedNewObject = Object;
  1957. }
  1958. //
  1959. // If we are attached to the system process then return
  1960. // back to our caller
  1961. //
  1962. if (AttachedToProcess) {
  1963. KeUnstackDetachProcess(&ApcState);
  1964. AttachedToProcess = FALSE;
  1965. }
  1966. //
  1967. // And return to our caller
  1968. //
  1969. return( STATUS_SUCCESS );
  1970. }
  1971. NTSTATUS
  1972. ObpCreateUnnamedHandle (
  1973. IN PVOID Object,
  1974. IN ACCESS_MASK DesiredAccess,
  1975. IN ULONG ObjectPointerBias OPTIONAL,
  1976. IN ULONG Attributes,
  1977. IN KPROCESSOR_MODE AccessMode,
  1978. OUT PVOID *ReferencedNewObject OPTIONAL,
  1979. OUT PHANDLE Handle
  1980. )
  1981. /*++
  1982. Routine Description:
  1983. This function creates a new unnamed handle for an existing object
  1984. Arguments:
  1985. Object - A pointer to the body of the new object
  1986. DesiredAccess - Supplies the access mask being requsted
  1987. ObjectPointerBias - Optionally supplies a count of addition
  1988. increments we do to the pointer count for the object
  1989. Attributes - Desired attributes for the handle
  1990. AccessMode - Supplies the mode of the requestor.
  1991. ReferencedNewObject - Optionally receives a pointer to the body
  1992. of the new object
  1993. Handle - Receives the new handle value
  1994. Return Value:
  1995. An appropriate status value
  1996. --*/
  1997. {
  1998. NTSTATUS Status;
  1999. POBJECT_HEADER ObjectHeader;
  2000. POBJECT_TYPE ObjectType;
  2001. PVOID ObjectTable;
  2002. HANDLE_TABLE_ENTRY ObjectTableEntry;
  2003. HANDLE NewHandle;
  2004. ACCESS_MASK GrantedAccess;
  2005. BOOLEAN AttachedToProcess = FALSE;
  2006. BOOLEAN KernelHandle = FALSE;
  2007. KAPC_STATE ApcState;
  2008. PAGED_CODE();
  2009. ObpValidateIrql( "ObpCreateUnnamedHandle" );
  2010. //
  2011. // Get the object header and type for the new object
  2012. //
  2013. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  2014. ObjectType = ObjectHeader->Type;
  2015. //
  2016. // Set the first ulong of the object table entry to point
  2017. // to the object header and then or in the low order attribute
  2018. // bits
  2019. //
  2020. ObjectTableEntry.Object = ObjectHeader;
  2021. ObjectTableEntry.ObAttributes |= (Attributes & OBJ_HANDLE_ATTRIBUTES);
  2022. //
  2023. // Now get a pointer to the object table for either the current process
  2024. // of the kernel handle table
  2025. //
  2026. if ((Attributes & OBJ_KERNEL_HANDLE) /* && (AccessMode == KernelMode) */) {
  2027. ObjectTable = ObpKernelHandleTable;
  2028. KernelHandle = TRUE;
  2029. //
  2030. // Go to the system process if we have to
  2031. //
  2032. if (PsGetCurrentProcess() != PsInitialSystemProcess) {
  2033. KeStackAttachProcess (&PsInitialSystemProcess->Pcb, &ApcState);
  2034. AttachedToProcess = TRUE;
  2035. }
  2036. } else {
  2037. ObjectTable = ObpGetObjectTable();
  2038. }
  2039. //
  2040. // Increment the handle count, this routine also does the access
  2041. // check if necessary
  2042. //
  2043. Status = ObpIncrementUnnamedHandleCount( &DesiredAccess,
  2044. PsGetCurrentProcess(),
  2045. Object,
  2046. ObjectType,
  2047. AccessMode,
  2048. Attributes );
  2049. GrantedAccess = DesiredAccess &
  2050. (ObjectType->TypeInfo.ValidAccessMask | ACCESS_SYSTEM_SECURITY );
  2051. if (!NT_SUCCESS( Status )) {
  2052. //
  2053. // If we are attached to the system process then return
  2054. // back to our caller
  2055. //
  2056. if (AttachedToProcess) {
  2057. KeUnstackDetachProcess(&ApcState);
  2058. AttachedToProcess = FALSE;
  2059. }
  2060. return( Status );
  2061. }
  2062. //
  2063. // Bias the pointer count if that is what the caller wanted
  2064. //
  2065. if (ARGUMENT_PRESENT( ObjectPointerBias )) {
  2066. ObpIncrPointerCountEx (ObjectHeader, ObjectPointerBias);
  2067. }
  2068. //
  2069. // Set the granted access mask in the object table entry (second ulong)
  2070. //
  2071. #if i386
  2072. if (NtGlobalFlag & FLG_KERNEL_STACK_TRACE_DB) {
  2073. LONG StackTraceIndex;
  2074. ObjectTableEntry.GrantedAccessIndex = ObpComputeGrantedAccessIndex( GrantedAccess );
  2075. if (AccessMode == KernelMode) {
  2076. StackTraceIndex = RtlLogStackBackTrace();
  2077. } else {
  2078. StackTraceIndex = RtlLogUmodeStackBackTrace();
  2079. }
  2080. //
  2081. // Save the stack trace only if the index fits the CreatorBackTraceIndex
  2082. // minus the ProtectOnClose bit
  2083. //
  2084. if (StackTraceIndex < OBP_CREATOR_BACKTRACE_LIMIT) {
  2085. ObjectTableEntry.CreatorBackTraceIndex = (USHORT)StackTraceIndex;
  2086. } else {
  2087. ObjectTableEntry.CreatorBackTraceIndex = 0;
  2088. }
  2089. } else {
  2090. ObjectTableEntry.GrantedAccess = GrantedAccess;
  2091. }
  2092. #else
  2093. ObjectTableEntry.GrantedAccess = GrantedAccess;
  2094. #endif // i386
  2095. //
  2096. // Add this new object table entry to the object table for the process
  2097. //
  2098. ObpEncodeProtectClose(ObjectTableEntry);
  2099. NewHandle = ExCreateHandle( ObjectTable, &ObjectTableEntry );
  2100. //
  2101. // If we didn't get a handle then cleanup after ourselves and return
  2102. // the error to our caller
  2103. //
  2104. if (NewHandle == NULL) {
  2105. if (ARGUMENT_PRESENT( ObjectPointerBias )) {
  2106. ObpDecrPointerCountEx (ObjectHeader, ObjectPointerBias);
  2107. }
  2108. ObpDecrementHandleCount( PsGetCurrentProcess(),
  2109. ObjectHeader,
  2110. ObjectType,
  2111. GrantedAccess );
  2112. //
  2113. // If we are attached to the system process then return
  2114. // back to our caller
  2115. //
  2116. if (AttachedToProcess) {
  2117. KeUnstackDetachProcess(&ApcState);
  2118. AttachedToProcess = FALSE;
  2119. }
  2120. return( STATUS_INSUFFICIENT_RESOURCES );
  2121. }
  2122. //
  2123. // We have a new Ex style handle now make it an ob style handle and also
  2124. // adjust for the kernel handle by setting the sign bit in the handle
  2125. // value
  2126. //
  2127. if (KernelHandle) {
  2128. NewHandle = EncodeKernelHandle( NewHandle );
  2129. }
  2130. *Handle = NewHandle;
  2131. //
  2132. // If the caller had a pointer bias and wanted the new reference object
  2133. // then return that value
  2134. //
  2135. if ((ARGUMENT_PRESENT( ObjectPointerBias )) &&
  2136. (ARGUMENT_PRESENT( ReferencedNewObject ))) {
  2137. *ReferencedNewObject = Object;
  2138. }
  2139. //
  2140. // If we are attached to the system process then return
  2141. // back to our caller
  2142. //
  2143. if (AttachedToProcess) {
  2144. KeUnstackDetachProcess(&ApcState);
  2145. AttachedToProcess = FALSE;
  2146. }
  2147. return( STATUS_SUCCESS );
  2148. }
  2149. NTSTATUS
  2150. ObpValidateDesiredAccess (
  2151. IN ACCESS_MASK DesiredAccess
  2152. )
  2153. /*++
  2154. Routine Description:
  2155. This routine checks the input desired access mask to see that
  2156. some invalid bits are not set. The invalid bits are the top
  2157. two reserved bits and the top three standard rights bits.
  2158. See \nt\public\sdk\inc\ntseapi.h for more details.
  2159. Arguments:
  2160. DesiredAccess - Supplies the mask being checked
  2161. Return Value:
  2162. STATUS_ACCESS_DENIED if one or more of the wrongs bits are set and
  2163. STATUS_SUCCESS otherwise
  2164. --*/
  2165. {
  2166. if (DesiredAccess & 0x0CE00000) {
  2167. return( STATUS_ACCESS_DENIED );
  2168. } else {
  2169. return( STATUS_SUCCESS );
  2170. }
  2171. }
  2172. #if i386
  2173. //
  2174. // The following three variables are just performance counters
  2175. // for the following two routines
  2176. //
  2177. ULONG ObpXXX1;
  2178. ULONG ObpXXX2;
  2179. ULONG ObpXXX3;
  2180. USHORT
  2181. ObpComputeGrantedAccessIndex (
  2182. ACCESS_MASK GrantedAccess
  2183. )
  2184. /*++
  2185. Routine Description:
  2186. This routine takes a granted access and returns and index
  2187. back to our cache of granted access masks.
  2188. Arguments:
  2189. GrantedAccess - Supplies the access mask being added to the cache
  2190. Return Value:
  2191. USHORT - returns an index in the cache for the input granted access
  2192. --*/
  2193. {
  2194. ULONG GrantedAccessIndex, n;
  2195. PACCESS_MASK p;
  2196. PKTHREAD CurrentThread;
  2197. ObpXXX1 += 1;
  2198. //
  2199. // Lock the global data structure
  2200. //
  2201. CurrentThread = KeGetCurrentThread ();
  2202. KeEnterCriticalRegionThread (CurrentThread);
  2203. ExAcquirePushLockExclusive ( &ObpLock );
  2204. n = ObpCurCachedGrantedAccessIndex;
  2205. p = ObpCachedGrantedAccesses;
  2206. //
  2207. // For each index in our cache look for a match and if found
  2208. // then unlock the data structure and return that index
  2209. //
  2210. for (GrantedAccessIndex = 0; GrantedAccessIndex < n; GrantedAccessIndex++, p++ ) {
  2211. ObpXXX2 += 1;
  2212. if (*p == GrantedAccess) {
  2213. ExReleasePushLockExclusive ( &ObpLock );
  2214. KeLeaveCriticalRegionThread (CurrentThread);
  2215. return (USHORT)GrantedAccessIndex;
  2216. }
  2217. }
  2218. //
  2219. // We didn't find a match now see if we've maxed out the cache
  2220. //
  2221. if (ObpCurCachedGrantedAccessIndex == ObpMaxCachedGrantedAccessIndex) {
  2222. DbgPrint( "OB: GrantedAccess cache limit hit.\n" );
  2223. DbgBreakPoint();
  2224. }
  2225. //
  2226. // Set the granted access to the next free slot and increment the
  2227. // number used in the cache, release the lock, and return the
  2228. // new index to our caller
  2229. //
  2230. *p = GrantedAccess;
  2231. ObpCurCachedGrantedAccessIndex += 1;
  2232. ExReleasePushLockExclusive ( &ObpLock );
  2233. KeLeaveCriticalRegionThread (CurrentThread);
  2234. return (USHORT)GrantedAccessIndex;
  2235. }
  2236. ACCESS_MASK
  2237. ObpTranslateGrantedAccessIndex (
  2238. USHORT GrantedAccessIndex
  2239. )
  2240. /*++
  2241. Routine Description:
  2242. This routine takes as input a cache index and returns
  2243. the corresponding granted access mask
  2244. Arguments:
  2245. GrantedAccessIndex - Supplies the cache index to look up
  2246. Return Value:
  2247. ACCESS_MASK - Returns the corresponding desired access mask
  2248. --*/
  2249. {
  2250. ACCESS_MASK GrantedAccess = (ACCESS_MASK)0;
  2251. PKTHREAD CurrentThread;
  2252. ObpXXX3 += 1;
  2253. //
  2254. // Lock the global data structure
  2255. //
  2256. CurrentThread = KeGetCurrentThread ();
  2257. KeEnterCriticalRegionThread (CurrentThread);
  2258. ExAcquirePushLockExclusive ( &ObpLock );
  2259. //
  2260. // If the input index is within bounds then get the granted
  2261. // access
  2262. //
  2263. if (GrantedAccessIndex < ObpCurCachedGrantedAccessIndex) {
  2264. GrantedAccess = ObpCachedGrantedAccesses[ GrantedAccessIndex ];
  2265. }
  2266. //
  2267. // Release the lock and return the granted access to our caller
  2268. //
  2269. ExReleasePushLockExclusive ( &ObpLock );
  2270. KeLeaveCriticalRegionThread (CurrentThread);
  2271. return GrantedAccess;
  2272. }
  2273. #endif // i386