Source code of Windows XP (NT5)
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.

2720 lines
68 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. obref.c
  5. Abstract:
  6. Object open API
  7. Author:
  8. Steve Wood (stevewo) 31-Mar-1989
  9. Revision History:
  10. --*/
  11. #include "obp.h"
  12. #undef ObReferenceObjectByHandle
  13. #ifdef ALLOC_PRAGMA
  14. #pragma alloc_text(INIT,ObpInitStackTrace)
  15. #pragma alloc_text(PAGE,ObOpenObjectByName)
  16. #pragma alloc_text(PAGE,ObOpenObjectByPointer)
  17. #pragma alloc_text(PAGE,ObReferenceObjectByHandle)
  18. #pragma alloc_text(PAGE,ObReferenceObjectByName)
  19. #pragma alloc_text(PAGE,ObReferenceFileObjectForWrite)
  20. #pragma alloc_text(PAGE,ObpProcessRemoveObjectQueue)
  21. #pragma alloc_text(PAGE,ObpRemoveObjectRoutine)
  22. #pragma alloc_text(PAGE,ObpDeleteNameCheck)
  23. #pragma alloc_text(PAGE,ObpAuditObjectAccess)
  24. #pragma alloc_text(PAGE,ObIsObjectDeletionInline)
  25. #pragma alloc_text(PAGE,ObAuditObjectAccess)
  26. #endif
  27. //
  28. //
  29. // Stack Trace code
  30. //
  31. //
  32. ULONG ObpTraceNoDeregister = 0;
  33. WCHAR ObpTracePoolTagsBuffer[128] = { 0 };
  34. ULONG ObpTracePoolTagsLength = sizeof(ObpTracePoolTagsBuffer);
  35. ULONG ObpTracePoolTags[16];
  36. BOOLEAN ObpTraceEnabled = FALSE;
  37. #ifdef POOL_TAGGING
  38. #define OBTRACE_OBJECTBUCKETS 401 // # of buckets in the object hash table (a prime)
  39. #define OBTRACE_STACKS 14747 // max # of unique stack traces (a prime)
  40. #define OBTRACE_STACKSPEROBJECT 32768 // max number of object references
  41. #define OBTRACE_TRACEDEPTH 16 // depth of stack traces
  42. const ObpObjectBuckets = OBTRACE_OBJECTBUCKETS;
  43. const ObpMaxStacks = OBTRACE_STACKS;
  44. const ObpStacksPerObject = OBTRACE_STACKSPEROBJECT;
  45. const ObpTraceDepth = OBTRACE_TRACEDEPTH;
  46. //
  47. // Object reference stacktrace structure
  48. //
  49. typedef struct _OBJECT_REF_TRACE {
  50. PVOID StackTrace[OBTRACE_TRACEDEPTH];
  51. } OBJECT_REF_TRACE, *POBJECT_REF_TRACE;
  52. typedef struct _OBJECT_REF_STACK_INFO {
  53. USHORT Sequence;
  54. USHORT Index;
  55. } OBJECT_REF_STACK_INFO, *POBJECT_REF_STACK_INFO;
  56. //
  57. // Object reference info structure
  58. //
  59. typedef struct _OBJECT_REF_INFO {
  60. POBJECT_HEADER ObjectHeader;
  61. PVOID NextRef;
  62. UCHAR ImageFileName[16];
  63. ULONG NextPos;
  64. OBJECT_REF_STACK_INFO StackInfo[OBTRACE_STACKSPEROBJECT];
  65. } OBJECT_REF_INFO, *POBJECT_REF_INFO;
  66. //
  67. // The stack hash table, and the object hash table
  68. //
  69. OBJECT_REF_TRACE *ObpStackTable = NULL;
  70. POBJECT_REF_INFO *ObpObjectTable = NULL;
  71. //
  72. // Some statistics
  73. //
  74. ULONG ObpNumStackTraces;
  75. ULONG ObpNumTracedObjects;
  76. ULONG ObpStackSequence;
  77. //
  78. // Spin lock for object tracing
  79. //
  80. KSPIN_LOCK ObpStackTraceLock;
  81. #define OBTRACE_HASHOBJECT(x) (((((ULONG)(ULONG_PTR)(&(x)->Body)) >> 4) & 0xfffff) % OBTRACE_OBJECTBUCKETS)
  82. POBJECT_REF_INFO
  83. ObpGetObjectRefInfo (
  84. POBJECT_HEADER ObjectHeader
  85. )
  86. /*++
  87. Routine Description:
  88. This routine returns a pointer to the OBJECT_REF_INFO for the
  89. specified object, or NULL, if it doesn't exist.
  90. Arguments:
  91. ObjectHeader - Pointer to the object header
  92. Return Value:
  93. The pointer to a OBJECT_REF_INFO object for the specified object.
  94. --*/
  95. {
  96. POBJECT_REF_INFO ObjectRefInfo = ObpObjectTable[OBTRACE_HASHOBJECT(ObjectHeader)];
  97. while (ObjectRefInfo && ObjectRefInfo->ObjectHeader != ObjectHeader) {
  98. ObjectRefInfo = (POBJECT_REF_INFO)ObjectRefInfo->NextRef;
  99. }
  100. return ObjectRefInfo;
  101. }
  102. ULONG
  103. ObpGetTraceIndex (
  104. POBJECT_REF_TRACE Trace
  105. )
  106. /*++
  107. Routine Description:
  108. This routine returns the index of 'Trace' in the stack
  109. trace hash table (ObpStackTable). If Trace does not exist
  110. in the table, it is added, and the new index is returned.
  111. Arguments:
  112. Trace - Pointer to a stack trace to find in the table
  113. Return Value:
  114. The index of Trace in ObpStackTable
  115. --*/
  116. {
  117. ULONG_PTR Value = 0;
  118. ULONG Index;
  119. PUSHORT Key;
  120. ULONG Hash;
  121. //
  122. // Determine the hash value for the stack trace
  123. //
  124. Key = (PUSHORT)Trace->StackTrace;
  125. for (Index = 0; Index < sizeof(Trace->StackTrace) / sizeof(*Key); Index += 2) {
  126. Value += Key[Index] ^ Key[Index + 1];
  127. }
  128. Hash = ((ULONG)Value) % OBTRACE_STACKS;
  129. //
  130. // Look up the trace at that index (linear probing)
  131. //
  132. while (ObpStackTable[Hash].StackTrace[0] != NULL &&
  133. RtlCompareMemory(&ObpStackTable[Hash], Trace, sizeof(OBJECT_REF_TRACE)) != sizeof(OBJECT_REF_TRACE)) {
  134. Hash = (Hash + 1) % OBTRACE_STACKS;
  135. if (Hash == ((ULONG)Value) % OBTRACE_STACKS) {
  136. return OBTRACE_STACKS;
  137. }
  138. }
  139. //
  140. // If the trace doesn't already exist in the table, add it.
  141. //
  142. if (ObpStackTable[Hash].StackTrace[0] == NULL) {
  143. RtlCopyMemory(&ObpStackTable[Hash], Trace, sizeof(OBJECT_REF_TRACE));
  144. ObpNumStackTraces++;
  145. }
  146. return Hash;
  147. }
  148. VOID
  149. ObpInitStackTrace()
  150. /*++
  151. Routine Description:
  152. Initialize the ob ref/deref stack-tracing code.
  153. Arguments:
  154. Return Value:
  155. --*/
  156. {
  157. ULONG i,j;
  158. KeInitializeSpinLock( &ObpStackTraceLock );
  159. RtlZeroMemory(ObpTracePoolTags, sizeof(ObpTracePoolTags));
  160. ObpStackSequence = 0;
  161. ObpNumStackTraces = 0;
  162. ObpNumTracedObjects = 0;
  163. ObpTraceEnabled = FALSE;
  164. //
  165. // Loop through the ObpTracePoolTagsBuffer string, and convert it to
  166. // an array of pool tags.
  167. //
  168. // The string should be in the form "Tag1;Tag2;Tag3; ..."
  169. //
  170. for (i = 0; i < sizeof(ObpTracePoolTags) / sizeof(ULONG); i++) {
  171. for (j = 0; j < 4; j++) {
  172. ObpTracePoolTags[i] = (ObpTracePoolTags[i] << 8) | ObpTracePoolTagsBuffer[5*i+(3-j)];
  173. }
  174. }
  175. //
  176. // If object tracing was turned on via the registry key, then we
  177. // need to allocate memory for the tables. If the memory allocations
  178. // fail, we turn off tracing by clearing the pool tag array.
  179. //
  180. if (ObpTracePoolTags[0] != 0) {
  181. ObpStackTable = ExAllocatePoolWithTag( NonPagedPool,
  182. OBTRACE_STACKS * sizeof(OBJECT_REF_TRACE),
  183. 'TSbO' );
  184. if (ObpStackTable != NULL) {
  185. RtlZeroMemory(ObpStackTable, OBTRACE_STACKS * sizeof(OBJECT_REF_TRACE));
  186. ObpObjectTable = ExAllocatePoolWithTag( NonPagedPool,
  187. OBTRACE_OBJECTBUCKETS * sizeof(POBJECT_REF_INFO),
  188. 'TSbO' );
  189. if (ObpObjectTable != NULL) {
  190. RtlZeroMemory(ObpObjectTable, OBTRACE_OBJECTBUCKETS * sizeof(POBJECT_REF_INFO));
  191. ObpTraceEnabled = TRUE;
  192. } else {
  193. ExFreePoolWithTag( ObpStackTable, 'TSbO' );
  194. ObpStackTable = NULL;
  195. RtlZeroMemory(ObpTracePoolTags, sizeof(ObpTracePoolTags));
  196. }
  197. } else {
  198. RtlZeroMemory(ObpTracePoolTags, sizeof(ObpTracePoolTags));
  199. }
  200. }
  201. }
  202. BOOLEAN
  203. ObpIsObjectTraced (
  204. POBJECT_HEADER ObjectHeader
  205. )
  206. /*++
  207. Routine Description:
  208. This routine determines if an object should have its references
  209. and dereferences traced.
  210. Arguments:
  211. ObjectHeader - The object to check
  212. Return Value:
  213. TRUE, if the object should be traced, and FALSE, otherwise
  214. --*/
  215. {
  216. ULONG i;
  217. if (ObjectHeader != NULL) {
  218. //
  219. // Loop through the ObpTracePoolTags array, and return true if
  220. // the object type key matches one of them.
  221. //
  222. for (i = 0; i < sizeof(ObpTracePoolTags) / sizeof(ULONG); i++) {
  223. if (ObjectHeader->Type->Key == ObpTracePoolTags[i]) {
  224. return TRUE;
  225. }
  226. }
  227. }
  228. return FALSE;
  229. }
  230. VOID
  231. ObpRegisterObject (
  232. POBJECT_HEADER ObjectHeader
  233. )
  234. /*++
  235. Routine Description:
  236. This routine is called once for each object that is created.
  237. It determines if the object should be traced, and if so, adds
  238. it to the hash table.
  239. Arguments:
  240. ObjectHeader - The object to register
  241. Return Value:
  242. --*/
  243. {
  244. KIRQL OldIrql;
  245. POBJECT_REF_INFO ObjectRefInfo = NULL;
  246. //
  247. // Are we tracing this object?
  248. //
  249. if (ObpIsObjectTraced( ObjectHeader )) {
  250. ExAcquireSpinLock( &ObpStackTraceLock, &OldIrql );
  251. ObjectRefInfo = ObpGetObjectRefInfo(ObjectHeader);
  252. if (ObjectRefInfo == NULL) {
  253. //
  254. // Allocate a new OBJECT_REF_INFO for the object
  255. //
  256. ObjectRefInfo = ExAllocatePoolWithTag( NonPagedPool,
  257. sizeof(OBJECT_REF_INFO),
  258. 'TSbO' );
  259. if (ObjectRefInfo != NULL) {
  260. //
  261. // Place the object into the hash table (at the beginning of the bucket)
  262. //
  263. ObjectRefInfo->NextRef = ObpObjectTable[OBTRACE_HASHOBJECT(ObjectHeader)];
  264. ObpObjectTable[OBTRACE_HASHOBJECT(ObjectHeader)] = ObjectRefInfo;
  265. } else {
  266. DbgPrint( "ObpRegisterObject - ExAllocatePoolWithTag failed.\n" );
  267. }
  268. }
  269. if (ObjectRefInfo != NULL) {
  270. ObpNumTracedObjects++;
  271. //
  272. // Initialize the OBJECT_REF_INFO
  273. //
  274. ObjectRefInfo->ObjectHeader = ObjectHeader;
  275. RtlCopyMemory( ObjectRefInfo->ImageFileName,
  276. PsGetCurrentProcess()->ImageFileName,
  277. sizeof(ObjectRefInfo->ImageFileName) );
  278. ObjectRefInfo->NextPos = 0;
  279. RtlZeroMemory( ObjectRefInfo->StackInfo,
  280. sizeof(ObjectRefInfo->StackInfo) );
  281. }
  282. ExReleaseSpinLock( &ObpStackTraceLock, OldIrql );
  283. }
  284. }
  285. VOID
  286. ObpDeregisterObject (
  287. POBJECT_HEADER ObjectHeader
  288. )
  289. /*++
  290. Routine Description:
  291. This routine is called once for each object that is deleted.
  292. It determines if the object is traced, and if so, deletes
  293. it from the hash table.
  294. Arguments:
  295. ObjectHeader - The object to deregister
  296. Return Value:
  297. --*/
  298. {
  299. KIRQL OldIrql;
  300. POBJECT_REF_INFO ObjectRefInfo = NULL;
  301. //
  302. // Are we tracing this object?
  303. //
  304. if (ObpIsObjectTraced( ObjectHeader )) {
  305. ExAcquireSpinLock( &ObpStackTraceLock, &OldIrql );
  306. ObjectRefInfo = ObpObjectTable[OBTRACE_HASHOBJECT(ObjectHeader)];
  307. if (ObjectRefInfo != NULL) {
  308. //
  309. // Remove the entry from the list
  310. //
  311. if (ObjectRefInfo->ObjectHeader == ObjectHeader) {
  312. ObpObjectTable[OBTRACE_HASHOBJECT(ObjectHeader)] = ObjectRefInfo->NextRef;
  313. } else {
  314. POBJECT_REF_INFO PrevObjectRefInfo;
  315. do {
  316. PrevObjectRefInfo = ObjectRefInfo;
  317. ObjectRefInfo = ObjectRefInfo->NextRef;
  318. } while (ObjectRefInfo && ObjectRefInfo->ObjectHeader != ObjectHeader);
  319. if (ObjectRefInfo && ObjectRefInfo->ObjectHeader == ObjectHeader) {
  320. PrevObjectRefInfo->NextRef = ObjectRefInfo->NextRef;
  321. }
  322. }
  323. }
  324. //
  325. // Free the object we just removed from the list
  326. //
  327. if (ObjectRefInfo != NULL) {
  328. ExFreePoolWithTag( ObjectRefInfo, 'TSbO' );
  329. }
  330. ExReleaseSpinLock( &ObpStackTraceLock, OldIrql );
  331. }
  332. }
  333. VOID
  334. ObpPushStackInfo (
  335. POBJECT_HEADER ObjectHeader,
  336. BOOLEAN IsRef
  337. )
  338. /*++
  339. Routine Description:
  340. This routine is called each time an object is referenced or
  341. dereferenced. It determines if the object is traced, and if
  342. so, adds the necessary trace to the object reference info.
  343. Arguments:
  344. ObjectHeader - The object to trace.
  345. IsRef - TRUE if this is a ref, FALSE if a deref
  346. Return Value:
  347. --*/
  348. {
  349. KIRQL OldIrql;
  350. POBJECT_REF_INFO ObjectInfo;
  351. //
  352. // Are we tracing this object?
  353. //
  354. if (ObpIsObjectTraced( ObjectHeader )) {
  355. ExAcquireSpinLock( &ObpStackTraceLock, &OldIrql );
  356. ObjectInfo = ObpGetObjectRefInfo( ObjectHeader );
  357. if (ObjectInfo) {
  358. OBJECT_REF_TRACE Stack = { 0 };
  359. ULONG StackIndex;
  360. ULONG CapturedTraces;
  361. //
  362. // Capture the stack trace
  363. //
  364. CapturedTraces = RtlCaptureStackBackTrace( 1, OBTRACE_TRACEDEPTH, Stack.StackTrace, &StackIndex );
  365. if (CapturedTraces >= 1) {
  366. //
  367. // Get the table index for the trace
  368. //
  369. StackIndex = ObpGetTraceIndex( &Stack );
  370. if (StackIndex < OBTRACE_STACKS) {
  371. //
  372. // Add new reference info to the object
  373. //
  374. if (ObjectInfo->NextPos < OBTRACE_STACKSPEROBJECT) {
  375. ObjectInfo->StackInfo[ObjectInfo->NextPos].Index = (USHORT)StackIndex | (IsRef ? 0x8000 : 0);
  376. ObpStackSequence++;
  377. ObjectInfo->StackInfo[ObjectInfo->NextPos].Sequence = (USHORT)ObpStackSequence;
  378. ObjectInfo->NextPos++;
  379. }
  380. } else {
  381. DbgPrint( "ObpPushStackInfo -- ObpStackTable overflow!\n" );
  382. }
  383. }
  384. }
  385. ExReleaseSpinLock( &ObpStackTraceLock, OldIrql );
  386. }
  387. }
  388. #endif //POOL_TAGGING
  389. //
  390. //
  391. // End Stack trace code
  392. //
  393. typedef struct _OB_TEMP_BUFFER {
  394. ACCESS_STATE LocalAccessState;
  395. OBJECT_CREATE_INFORMATION ObjectCreateInfo;
  396. OBP_LOOKUP_CONTEXT LookupContext;
  397. AUX_ACCESS_DATA AuxData;
  398. } OB_TEMP_BUFFER, *POB_TEMP_BUFFER;
  399. NTSTATUS
  400. ObOpenObjectByName (
  401. IN POBJECT_ATTRIBUTES ObjectAttributes,
  402. IN POBJECT_TYPE ObjectType OPTIONAL,
  403. IN KPROCESSOR_MODE AccessMode,
  404. IN OUT PACCESS_STATE AccessState OPTIONAL,
  405. IN ACCESS_MASK DesiredAccess OPTIONAL,
  406. IN OUT PVOID ParseContext OPTIONAL,
  407. OUT PHANDLE Handle
  408. )
  409. /*++
  410. Routine Description:
  411. This function opens an object with full access validation and auditing.
  412. Soon after entering we capture the SubjectContext for the caller. This
  413. context must remain captured until auditing is complete, and passed to
  414. any routine that may have to do access checking or auditing.
  415. Arguments:
  416. ObjectAttributes - Supplies a pointer to the object attributes.
  417. ObjectType - Supplies an optional pointer to the object type descriptor.
  418. AccessMode - Supplies the processor mode of the access.
  419. AccessState - Supplies an optional pointer to the current access status
  420. describing already granted access types, the privileges used to get
  421. them, and any access types yet to be granted.
  422. DesiredAcess - Supplies the desired access to the object.
  423. ParseContext - Supplies an optional pointer to parse context.
  424. Handle - Supplies a pointer to a variable that receives the handle value.
  425. Return Value:
  426. If the object is successfully opened, then a handle for the object is
  427. created and a success status is returned. Otherwise, an error status is
  428. returned.
  429. --*/
  430. {
  431. NTSTATUS Status;
  432. NTSTATUS HandleStatus;
  433. PVOID ExistingObject;
  434. HANDLE NewHandle;
  435. BOOLEAN DirectoryLocked;
  436. OB_OPEN_REASON OpenReason;
  437. POBJECT_HEADER ObjectHeader;
  438. UNICODE_STRING CapturedObjectName;
  439. PGENERIC_MAPPING GenericMapping;
  440. PAGED_CODE();
  441. ObpValidateIrql("ObOpenObjectByName");
  442. //
  443. // If the object attributes are not specified, then return an error.
  444. //
  445. *Handle = NULL;
  446. if (!ARGUMENT_PRESENT(ObjectAttributes)) {
  447. Status = STATUS_INVALID_PARAMETER;
  448. } else {
  449. POB_TEMP_BUFFER TempBuffer;
  450. TempBuffer = ExAllocatePoolWithTag( NonPagedPool,
  451. sizeof(OB_TEMP_BUFFER),
  452. 'tSbO'
  453. );
  454. if (TempBuffer == NULL) {
  455. return STATUS_INSUFFICIENT_RESOURCES;
  456. }
  457. //
  458. // Capture the object creation information.
  459. //
  460. Status = ObpCaptureObjectCreateInformation( ObjectType,
  461. AccessMode,
  462. AccessMode,
  463. ObjectAttributes,
  464. &CapturedObjectName,
  465. &TempBuffer->ObjectCreateInfo,
  466. TRUE );
  467. //
  468. // If the object creation information is successfully captured,
  469. // then generate the access state.
  470. //
  471. if (NT_SUCCESS(Status)) {
  472. if (!ARGUMENT_PRESENT(AccessState)) {
  473. //
  474. // If an object type descriptor is specified, then use
  475. // associated generic mapping. Otherwise, use no generic
  476. // mapping.
  477. //
  478. GenericMapping = NULL;
  479. if (ARGUMENT_PRESENT(ObjectType)) {
  480. GenericMapping = &ObjectType->TypeInfo.GenericMapping;
  481. }
  482. AccessState = &TempBuffer->LocalAccessState;
  483. Status = SeCreateAccessState( &TempBuffer->LocalAccessState,
  484. &TempBuffer->AuxData,
  485. DesiredAccess,
  486. GenericMapping );
  487. if (!NT_SUCCESS(Status)) {
  488. goto FreeCreateInfo;
  489. }
  490. }
  491. //
  492. // If there is a security descriptor specified in the object
  493. // attributes, then capture it in the access state.
  494. //
  495. if (TempBuffer->ObjectCreateInfo.SecurityDescriptor != NULL) {
  496. AccessState->SecurityDescriptor = TempBuffer->ObjectCreateInfo.SecurityDescriptor;
  497. }
  498. //
  499. // Validate the access state.
  500. //
  501. Status = ObpValidateAccessMask(AccessState);
  502. //
  503. // If the access state is valid, then lookup the object by
  504. // name.
  505. //
  506. if (NT_SUCCESS(Status)) {
  507. Status = ObpLookupObjectName( TempBuffer->ObjectCreateInfo.RootDirectory,
  508. &CapturedObjectName,
  509. TempBuffer->ObjectCreateInfo.Attributes,
  510. ObjectType,
  511. AccessMode,
  512. ParseContext,
  513. TempBuffer->ObjectCreateInfo.SecurityQos,
  514. NULL,
  515. AccessState,
  516. &TempBuffer->LookupContext,
  517. &ExistingObject );
  518. //
  519. // If the object was successfully looked up, then attempt
  520. // to create or open a handle.
  521. //
  522. if (NT_SUCCESS(Status)) {
  523. ObjectHeader = OBJECT_TO_OBJECT_HEADER(ExistingObject);
  524. //
  525. // If the object is being created, then the operation
  526. // must be a open-if operation. Otherwise, a handle to
  527. // an object is being opened.
  528. //
  529. if (ObjectHeader->Flags & OB_FLAG_NEW_OBJECT) {
  530. OpenReason = ObCreateHandle;
  531. if (ObjectHeader->ObjectCreateInfo != NULL) {
  532. ObpFreeObjectCreateInformation(ObjectHeader->ObjectCreateInfo);
  533. ObjectHeader->ObjectCreateInfo = NULL;
  534. }
  535. } else {
  536. OpenReason = ObOpenHandle;
  537. }
  538. //
  539. // If any of the object attributes are invalid, then
  540. // return an error status.
  541. //
  542. if (ObjectHeader->Type->TypeInfo.InvalidAttributes & TempBuffer->ObjectCreateInfo.Attributes) {
  543. Status = STATUS_INVALID_PARAMETER;
  544. ObpReleaseLookupContext( &TempBuffer->LookupContext );
  545. } else {
  546. //
  547. // The status returned by the object lookup routine
  548. // must be returned if the creation of a handle is
  549. // successful. Otherwise, the handle creation status
  550. // is returned.
  551. //
  552. HandleStatus = ObpCreateHandle( OpenReason,
  553. ExistingObject,
  554. ObjectType,
  555. AccessState,
  556. 0,
  557. TempBuffer->ObjectCreateInfo.Attributes,
  558. &TempBuffer->LookupContext,
  559. AccessMode,
  560. (PVOID *)NULL,
  561. &NewHandle );
  562. if (!NT_SUCCESS(HandleStatus)) {
  563. ObDereferenceObject(ExistingObject);
  564. Status = HandleStatus;
  565. } else {
  566. *Handle = NewHandle;
  567. }
  568. }
  569. } else {
  570. ObpReleaseLookupContext( &TempBuffer->LookupContext );
  571. }
  572. }
  573. //
  574. // If the access state was generated, then delete the access
  575. // state.
  576. //
  577. if (AccessState == &TempBuffer->LocalAccessState) {
  578. SeDeleteAccessState(AccessState);
  579. }
  580. //
  581. // Free the create information.
  582. //
  583. FreeCreateInfo:
  584. ObpReleaseObjectCreateInformation(&TempBuffer->ObjectCreateInfo);
  585. if (CapturedObjectName.Buffer != NULL) {
  586. ObpFreeObjectNameBuffer(&CapturedObjectName);
  587. }
  588. }
  589. ExFreePool(TempBuffer);
  590. }
  591. return Status;
  592. }
  593. NTSTATUS
  594. ObOpenObjectByPointer (
  595. IN PVOID Object,
  596. IN ULONG HandleAttributes,
  597. IN PACCESS_STATE PassedAccessState OPTIONAL,
  598. IN ACCESS_MASK DesiredAccess,
  599. IN POBJECT_TYPE ObjectType,
  600. IN KPROCESSOR_MODE AccessMode,
  601. OUT PHANDLE Handle
  602. )
  603. /*++
  604. Routine Description:
  605. This routine opens an object referenced by a pointer.
  606. Arguments:
  607. Object - A pointer to the object being opened.
  608. HandleAttributes - The desired attributes for the handle, such
  609. as OBJ_INHERIT, OBJ_PERMANENT, OBJ_EXCLUSIVE, OBJ_CASE_INSENSITIVE,
  610. OBJ_OPENIF, and OBJ_OPENLINK
  611. PassedAccessState - Supplies an optional pointer to the current access
  612. status describing already granted access types, the privileges used
  613. to get them, and any access types yet to be granted.
  614. DesiredAcess - Supplies the desired access to the object.
  615. ObjectType - Supplies the type of the object being opened
  616. AccessMode - Supplies the processor mode of the access.
  617. Handle - Supplies a pointer to a variable that receives the handle value.
  618. Return Value:
  619. An appropriate NTSTATUS value
  620. --*/
  621. {
  622. NTSTATUS Status;
  623. HANDLE NewHandle;
  624. POBJECT_HEADER ObjectHeader;
  625. ACCESS_STATE LocalAccessState;
  626. PACCESS_STATE AccessState = NULL;
  627. AUX_ACCESS_DATA AuxData;
  628. PAGED_CODE();
  629. ObpValidateIrql( "ObOpenObjectByPointer" );
  630. //
  631. // First increment the pointer count for the object. This routine
  632. // also checks the object types
  633. //
  634. Status = ObReferenceObjectByPointer( Object,
  635. 0,
  636. ObjectType,
  637. AccessMode );
  638. if (NT_SUCCESS( Status )) {
  639. //
  640. // Get the object header for the input object body
  641. //
  642. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  643. //
  644. // If the caller did not pass in an access state then
  645. // we will create a new one based on the desired access
  646. // and the object types generic mapping
  647. //
  648. if (!ARGUMENT_PRESENT( PassedAccessState )) {
  649. Status = SeCreateAccessState( &LocalAccessState,
  650. &AuxData,
  651. DesiredAccess,
  652. &ObjectHeader->Type->TypeInfo.GenericMapping );
  653. if (!NT_SUCCESS( Status )) {
  654. ObDereferenceObject( Object );
  655. return(Status);
  656. }
  657. AccessState = &LocalAccessState;
  658. //
  659. // Otherwise the caller did specify an access state so
  660. // we use the one passed in.
  661. //
  662. } else {
  663. AccessState = PassedAccessState;
  664. }
  665. //
  666. // Make sure the caller is asking for handle attributes that are
  667. // valid for the given object type
  668. //
  669. if (ObjectHeader->Type->TypeInfo.InvalidAttributes & HandleAttributes) {
  670. if (AccessState == &LocalAccessState) {
  671. SeDeleteAccessState( AccessState );
  672. }
  673. ObDereferenceObject( Object );
  674. return( STATUS_INVALID_PARAMETER );
  675. }
  676. //
  677. // We've referenced the object and have an access state to give
  678. // the new handle so now create a new handle for the object.
  679. //
  680. Status = ObpCreateHandle( ObOpenHandle,
  681. Object,
  682. ObjectType,
  683. AccessState,
  684. 0,
  685. HandleAttributes,
  686. NULL,
  687. AccessMode,
  688. (PVOID *)NULL,
  689. &NewHandle );
  690. if (!NT_SUCCESS( Status )) {
  691. ObDereferenceObject( Object );
  692. }
  693. }
  694. //
  695. // If we successfully opened by object and created a new handle
  696. // then set the output variable correctly
  697. //
  698. if (NT_SUCCESS( Status )) {
  699. *Handle = NewHandle;
  700. } else {
  701. *Handle = NULL;
  702. }
  703. //
  704. // Check if we used our own access state and now need to cleanup
  705. //
  706. if (AccessState == &LocalAccessState) {
  707. SeDeleteAccessState( AccessState );
  708. }
  709. //
  710. // And return to our caller
  711. //
  712. return( Status );
  713. }
  714. NTSTATUS
  715. ObReferenceObjectByHandle (
  716. IN HANDLE Handle,
  717. IN ACCESS_MASK DesiredAccess,
  718. IN POBJECT_TYPE ObjectType OPTIONAL,
  719. IN KPROCESSOR_MODE AccessMode,
  720. OUT PVOID *Object,
  721. OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL
  722. )
  723. /*++
  724. Routine Description:
  725. Given a handle to an object this routine returns a pointer
  726. to the body of the object with proper ref counts
  727. Arguments:
  728. Handle - Supplies a handle to the object being referenced. It can
  729. also be the result of NtCurrentProcess or NtCurrentThread
  730. DesiredAccess - Supplies the access being requested by the caller
  731. ObjectType - Optionally supplies the type of the object we
  732. are expecting
  733. AccessMode - Supplies the processor mode of the access
  734. Object - Receives a pointer to the object body if the operation
  735. is successful
  736. HandleInformation - Optionally receives information regarding the
  737. input handle.
  738. Return Value:
  739. An appropriate NTSTATUS value
  740. --*/
  741. {
  742. ACCESS_MASK GrantedAccess;
  743. PHANDLE_TABLE HandleTable;
  744. POBJECT_HEADER ObjectHeader;
  745. PHANDLE_TABLE_ENTRY ObjectTableEntry;
  746. PEPROCESS Process;
  747. NTSTATUS Status;
  748. PETHREAD Thread;
  749. ObpValidateIrql("ObReferenceObjectByHandle");
  750. Thread = PsGetCurrentThread ();
  751. *Object = NULL;
  752. //
  753. // Check is this handle is a kernel handle or one of the two builtin pseudo handles
  754. //
  755. if ((LONG)(ULONG_PTR) Handle < 0) {
  756. //
  757. // If the handle is equal to the current process handle and the object
  758. // type is NULL or type process, then attempt to translate a handle to
  759. // the current process. Otherwise, check if the handle is the current
  760. // thread handle.
  761. //
  762. if (Handle == NtCurrentProcess()) {
  763. if ((ObjectType == PsProcessType) || (ObjectType == NULL)) {
  764. Process = PsGetCurrentProcessByThread(Thread);
  765. GrantedAccess = Process->GrantedAccess;
  766. if ((SeComputeDeniedAccesses(GrantedAccess, DesiredAccess) == 0) ||
  767. (AccessMode == KernelMode)) {
  768. ObjectHeader = OBJECT_TO_OBJECT_HEADER(Process);
  769. if (ARGUMENT_PRESENT(HandleInformation)) {
  770. HandleInformation->GrantedAccess = GrantedAccess;
  771. HandleInformation->HandleAttributes = 0;
  772. }
  773. ObpIncrPointerCount(ObjectHeader);
  774. *Object = Process;
  775. ASSERT( *Object != NULL );
  776. Status = STATUS_SUCCESS;
  777. } else {
  778. Status = STATUS_ACCESS_DENIED;
  779. }
  780. } else {
  781. Status = STATUS_OBJECT_TYPE_MISMATCH;
  782. }
  783. return Status;
  784. //
  785. // If the handle is equal to the current thread handle and the object
  786. // type is NULL or type thread, then attempt to translate a handle to
  787. // the current thread. Otherwise, the we'll try and translate the
  788. // handle
  789. //
  790. } else if (Handle == NtCurrentThread()) {
  791. if ((ObjectType == PsThreadType) || (ObjectType == NULL)) {
  792. GrantedAccess = Thread->GrantedAccess;
  793. if ((SeComputeDeniedAccesses(GrantedAccess, DesiredAccess) == 0) ||
  794. (AccessMode == KernelMode)) {
  795. ObjectHeader = OBJECT_TO_OBJECT_HEADER(Thread);
  796. if (ARGUMENT_PRESENT(HandleInformation)) {
  797. HandleInformation->GrantedAccess = GrantedAccess;
  798. HandleInformation->HandleAttributes = 0;
  799. }
  800. ObpIncrPointerCount(ObjectHeader);
  801. *Object = Thread;
  802. ASSERT( *Object != NULL );
  803. Status = STATUS_SUCCESS;
  804. } else {
  805. Status = STATUS_ACCESS_DENIED;
  806. }
  807. } else {
  808. Status = STATUS_OBJECT_TYPE_MISMATCH;
  809. }
  810. return Status;
  811. } else if (AccessMode == KernelMode) {
  812. //
  813. // Make the handle look like a regular handle
  814. //
  815. Handle = DecodeKernelHandle( Handle );
  816. //
  817. // The global kernel handle table
  818. //
  819. HandleTable = ObpKernelHandleTable;
  820. } else {
  821. //
  822. // The previous mode was user for this kernel handle value. Reject it here.
  823. //
  824. return STATUS_INVALID_HANDLE;
  825. }
  826. } else {
  827. HandleTable = PsGetCurrentProcessByThread(Thread)->ObjectTable;
  828. }
  829. ASSERT(HandleTable != NULL);
  830. //
  831. // Protect this thread from being suspended while we hold the handle table entry lock
  832. //
  833. KeEnterCriticalRegionThread(&Thread->Tcb);
  834. //
  835. // Translate the specified handle to an object table index.
  836. //
  837. ObjectTableEntry = ExMapHandleToPointerEx ( HandleTable, Handle, AccessMode );
  838. //
  839. // Make sure the object table entry really does exist
  840. //
  841. if (ObjectTableEntry != NULL) {
  842. ObjectHeader = (POBJECT_HEADER)(((ULONG_PTR)(ObjectTableEntry->Object)) & ~OBJ_HANDLE_ATTRIBUTES);
  843. if ((ObjectHeader->Type == ObjectType) || (ObjectType == NULL)) {
  844. #if i386
  845. if (NtGlobalFlag & FLG_KERNEL_STACK_TRACE_DB) {
  846. if ((AccessMode != KernelMode) || ARGUMENT_PRESENT(HandleInformation)) {
  847. GrantedAccess = ObpTranslateGrantedAccessIndex( ObjectTableEntry->GrantedAccessIndex );
  848. }
  849. } else {
  850. GrantedAccess = ObpDecodeGrantedAccess(ObjectTableEntry->GrantedAccess);
  851. }
  852. #else
  853. GrantedAccess = ObpDecodeGrantedAccess(ObjectTableEntry->GrantedAccess);
  854. #endif // i386
  855. if ((SeComputeDeniedAccesses(GrantedAccess, DesiredAccess) == 0) ||
  856. (AccessMode == KernelMode)) {
  857. PHANDLE_TABLE_ENTRY_INFO ObjectInfo;
  858. ObjectInfo = ExGetHandleInfo(HandleTable, Handle, TRUE);
  859. //
  860. // Access to the object is allowed. Return the handle
  861. // information is requested, increment the object
  862. // pointer count, unlock the handle table and return
  863. // a success status.
  864. //
  865. // Note that this is the only successful return path
  866. // out of this routine if the user did not specify
  867. // the current process or current thread in the input
  868. // handle.
  869. //
  870. if (ARGUMENT_PRESENT(HandleInformation)) {
  871. HandleInformation->GrantedAccess = GrantedAccess;
  872. HandleInformation->HandleAttributes = ObpGetHandleAttributes(ObjectTableEntry);
  873. }
  874. //
  875. // If this object was audited when it was opened, it may
  876. // be necessary to generate an audit now. Check the audit
  877. // mask that was saved when the handle was created.
  878. //
  879. // It is safe to do this check in a non-atomic fashion,
  880. // because bits will never be added to this mask once it is
  881. // created.
  882. //
  883. if ( (ObjectTableEntry->ObAttributes & OBJ_AUDIT_OBJECT_CLOSE) &&
  884. (ObjectInfo != NULL) &&
  885. (ObjectInfo->AuditMask != 0) &&
  886. (DesiredAccess != 0) &&
  887. (AccessMode != KernelMode)) {
  888. ObpAuditObjectAccess( Handle, ObjectInfo, &ObjectHeader->Type->Name, DesiredAccess );
  889. }
  890. ObpIncrPointerCount(ObjectHeader);
  891. ExUnlockHandleTableEntry( HandleTable, ObjectTableEntry );
  892. KeLeaveCriticalRegionThread(&Thread->Tcb);
  893. *Object = &ObjectHeader->Body;
  894. ASSERT( *Object != NULL );
  895. return STATUS_SUCCESS;
  896. } else {
  897. Status = STATUS_ACCESS_DENIED;
  898. }
  899. } else {
  900. Status = STATUS_OBJECT_TYPE_MISMATCH;
  901. }
  902. ExUnlockHandleTableEntry( HandleTable, ObjectTableEntry );
  903. } else {
  904. Status = STATUS_INVALID_HANDLE;
  905. }
  906. KeLeaveCriticalRegionThread(&Thread->Tcb);
  907. return Status;
  908. }
  909. NTSTATUS
  910. ObReferenceFileObjectForWrite(
  911. IN HANDLE Handle,
  912. IN KPROCESSOR_MODE AccessMode,
  913. OUT PVOID *FileObject,
  914. OUT POBJECT_HANDLE_INFORMATION HandleInformation
  915. )
  916. /*++
  917. Routine Description:
  918. Given a handle to a file object this routine returns a pointer
  919. to the body of the object with proper ref counts and auditing. This
  920. routine is meant to solve a very particular handle reference issue with
  921. file object access auditing. Do not call this unless you understand exactly
  922. what you are doing.
  923. Arguments:
  924. Handle - Supplies a handle to the IoFileObjectType being referenced.
  925. AccessMode - Supplies the processor mode of the access
  926. FileObject - Receives a pointer to the object body if the operation
  927. is successful
  928. HandleInformation - receives information regarding the input handle.
  929. Return Value:
  930. An appropriate NTSTATUS value
  931. --*/
  932. {
  933. ACCESS_MASK GrantedAccess;
  934. ACCESS_MASK DesiredAccess;
  935. PHANDLE_TABLE HandleTable;
  936. POBJECT_HEADER ObjectHeader;
  937. PHANDLE_TABLE_ENTRY ObjectTableEntry;
  938. PEPROCESS Process;
  939. NTSTATUS Status;
  940. PETHREAD Thread;
  941. PHANDLE_TABLE_ENTRY_INFO ObjectInfo;
  942. ObpValidateIrql("ObReferenceFileObjectForWrite");
  943. Thread = PsGetCurrentThread ();
  944. //
  945. // Check is this handle is a kernel handle
  946. //
  947. if ((LONG)(ULONG_PTR) Handle < 0) {
  948. if ((AccessMode == KernelMode) && (Handle != NtCurrentProcess()) && (Handle != NtCurrentThread())) {
  949. //
  950. // Make the handle look like a regular handle
  951. //
  952. Handle = DecodeKernelHandle( Handle );
  953. //
  954. // The global kernel handle table
  955. //
  956. HandleTable = ObpKernelHandleTable;
  957. } else {
  958. //
  959. // The previous mode was user for this kernel handle value, or it was a builtin handle. Reject it here.
  960. //
  961. return STATUS_INVALID_HANDLE;
  962. }
  963. } else {
  964. HandleTable = PsGetCurrentProcessByThread(Thread)->ObjectTable;
  965. }
  966. ASSERT(HandleTable != NULL);
  967. //
  968. // Protect this thread from being suspended while we hold the handle table entry lock
  969. //
  970. KeEnterCriticalRegionThread(&Thread->Tcb);
  971. //
  972. // Translate the specified handle to an object table index.
  973. //
  974. ObjectTableEntry = ExMapHandleToPointerEx ( HandleTable, Handle, AccessMode );
  975. //
  976. // Make sure the object table entry really does exist
  977. //
  978. if (ObjectTableEntry != NULL) {
  979. ObjectHeader = (POBJECT_HEADER)(((ULONG_PTR)(ObjectTableEntry->Object)) & ~OBJ_HANDLE_ATTRIBUTES);
  980. if (NT_SUCCESS(IoComputeDesiredAccessFileObject((PFILE_OBJECT)&ObjectHeader->Body, &DesiredAccess))) {
  981. #if i386
  982. if (NtGlobalFlag & FLG_KERNEL_STACK_TRACE_DB) {
  983. GrantedAccess = ObpTranslateGrantedAccessIndex( ObjectTableEntry->GrantedAccessIndex );
  984. } else {
  985. GrantedAccess = ObpDecodeGrantedAccess(ObjectTableEntry->GrantedAccess);
  986. }
  987. #else
  988. GrantedAccess = ObpDecodeGrantedAccess(ObjectTableEntry->GrantedAccess);
  989. #endif // i386
  990. ObjectInfo = ExGetHandleInfo(HandleTable, Handle, TRUE);
  991. //
  992. // Access to the object is allowed. Return the handle
  993. // information, increment the object pointer count,
  994. // compute correct access, audit, unlock the handle
  995. // table and return a success status.
  996. //
  997. // Note that this is the only successful return path
  998. // out of this routine.
  999. //
  1000. HandleInformation->GrantedAccess = GrantedAccess;
  1001. HandleInformation->HandleAttributes = ObpGetHandleAttributes(ObjectTableEntry);
  1002. //
  1003. // Check to ensure that the caller has either WRITE_DATA or APPEND_DATA
  1004. // access to the file. If not, cleanup and return an access denied
  1005. // error status value. Note that if this is a pipe then the APPEND_DATA
  1006. // access check may not be made since this access code is overlaid with
  1007. // CREATE_PIPE_INSTANCE access.
  1008. //
  1009. if (SeComputeGrantedAccesses( GrantedAccess, DesiredAccess )) {
  1010. //
  1011. // If this object was audited when it was opened, it may
  1012. // be necessary to generate an audit now. Check the audit
  1013. // mask that was saved when the handle was created.
  1014. //
  1015. // It is safe to do this check in a non-atomic fashion,
  1016. // because bits will never be added to this mask once it is
  1017. // created.
  1018. //
  1019. if ( (ObjectTableEntry->ObAttributes & OBJ_AUDIT_OBJECT_CLOSE) &&
  1020. (ObjectInfo != NULL) &&
  1021. (ObjectInfo->AuditMask != 0) &&
  1022. (DesiredAccess != 0) &&
  1023. (AccessMode != KernelMode)) {
  1024. ObpAuditObjectAccess( Handle, ObjectInfo, &ObjectHeader->Type->Name, DesiredAccess );
  1025. }
  1026. ObpIncrPointerCount(ObjectHeader);
  1027. ExUnlockHandleTableEntry( HandleTable, ObjectTableEntry );
  1028. KeLeaveCriticalRegionThread(&Thread->Tcb);
  1029. *FileObject = &ObjectHeader->Body;
  1030. ASSERT( *FileObject != NULL );
  1031. return STATUS_SUCCESS;
  1032. } else {
  1033. Status = STATUS_ACCESS_DENIED;
  1034. }
  1035. } else {
  1036. Status = STATUS_OBJECT_TYPE_MISMATCH;
  1037. }
  1038. ExUnlockHandleTableEntry( HandleTable, ObjectTableEntry );
  1039. } else {
  1040. Status = STATUS_INVALID_HANDLE;
  1041. }
  1042. KeLeaveCriticalRegionThread(&Thread->Tcb);
  1043. //
  1044. // No handle translation is possible. Set the object address to NULL
  1045. // and return an error status.
  1046. //
  1047. *FileObject = NULL;
  1048. return Status;
  1049. }
  1050. VOID
  1051. ObAuditObjectAccess(
  1052. IN HANDLE Handle,
  1053. IN POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL,
  1054. IN KPROCESSOR_MODE AccessMode,
  1055. IN ACCESS_MASK DesiredAccess
  1056. )
  1057. /*++
  1058. Routine Description:
  1059. This routine will determine if it is necessary to audit the operation being
  1060. performed on the passed handle. If so, it will clear the bits in the handle
  1061. and generate the appropriate audit before returning.
  1062. The bits in the handle's audit mask are cleared in an atomic way so that
  1063. multiple threads coming through this code do not generate more than one
  1064. audit for the same operation.
  1065. Arguments:
  1066. Handle - Supplies the handle being accessed.
  1067. AccessMode - The mode (kernel or user) that originated the handle.
  1068. DesiredAccess - Supplies the access mask describing how the handle is being used
  1069. in this operation.
  1070. Return Value:
  1071. None.
  1072. --*/
  1073. {
  1074. PHANDLE_TABLE HandleTable;
  1075. PHANDLE_TABLE_ENTRY ObjectTableEntry;
  1076. POBJECT_HEADER ObjectHeader;
  1077. ACCESS_MASK GrantedAccess;
  1078. PKTHREAD CurrentThread;
  1079. //
  1080. // Exit fast if we have nothing to do.
  1081. //
  1082. if (ARGUMENT_PRESENT(HandleInformation)) {
  1083. if (!(HandleInformation->HandleAttributes & OBJ_AUDIT_OBJECT_CLOSE)) {
  1084. return;
  1085. }
  1086. }
  1087. //
  1088. // Do not currently support this on kernel mode
  1089. // handles.
  1090. //
  1091. if (AccessMode == KernelMode) {
  1092. return;
  1093. }
  1094. HandleTable = ObpGetObjectTable();
  1095. ASSERT(HandleTable != NULL);
  1096. //
  1097. // Translate the specified handle to an object table index.
  1098. //
  1099. CurrentThread = KeGetCurrentThread ();
  1100. KeEnterCriticalRegionThread (CurrentThread);
  1101. ObjectTableEntry = ExMapHandleToPointer( HandleTable, Handle );
  1102. //
  1103. // Make sure the object table entry really does exist
  1104. //
  1105. if (ObjectTableEntry != NULL) {
  1106. PHANDLE_TABLE_ENTRY_INFO ObjectInfo;
  1107. ObjectInfo = ExGetHandleInfo(HandleTable, Handle, TRUE);
  1108. ObjectHeader = (POBJECT_HEADER)(((ULONG_PTR)(ObjectTableEntry->Object)) & ~OBJ_HANDLE_ATTRIBUTES);
  1109. //
  1110. // If this object was audited when it was opened, it may
  1111. // be necessary to generate an audit now. Check the audit
  1112. // mask that was saved when the handle was created.
  1113. //
  1114. // It is safe to do this check in a non-atomic fashion,
  1115. // because bits will never be added to this mask once it is
  1116. // created.
  1117. //
  1118. // Note: is OBJ_AUDIT_OBJECT_CLOSE in ObAttributes kept in synch with
  1119. // HandleAttributes?
  1120. //
  1121. if ( (ObjectTableEntry->ObAttributes & OBJ_AUDIT_OBJECT_CLOSE) &&
  1122. (ObjectInfo != NULL) &&
  1123. (ObjectInfo->AuditMask != 0) &&
  1124. (DesiredAccess != 0)) {
  1125. ObpAuditObjectAccess( Handle, ObjectInfo, &ObjectHeader->Type->Name, DesiredAccess );
  1126. }
  1127. ExUnlockHandleTableEntry( HandleTable, ObjectTableEntry );
  1128. }
  1129. KeLeaveCriticalRegionThread (CurrentThread);
  1130. }
  1131. VOID
  1132. ObpAuditObjectAccess(
  1133. IN HANDLE Handle,
  1134. IN PHANDLE_TABLE_ENTRY_INFO ObjectTableEntryInfo,
  1135. IN PUNICODE_STRING ObjectTypeName,
  1136. IN ACCESS_MASK DesiredAccess
  1137. )
  1138. /*++
  1139. Routine Description:
  1140. This routine will determine if it is necessary to audit the operation being
  1141. performed on the passed handle. If so, it will clear the bits in the handle
  1142. and generate the appropriate audit before returning.
  1143. The bits in the handle's audit mask are cleared in an atomic way so that
  1144. multiple threads coming through this code do not generate more than one
  1145. audit for the same operation.
  1146. Arguments:
  1147. Handle - Supplies the handle being accessed.
  1148. ObjectTableEntry - Supplies the object table entry for the handle passed in the
  1149. first parameter.
  1150. DesiredAccess - Supplies the access mask describing how the handle is being used
  1151. in this operation.
  1152. Return Value:
  1153. None.
  1154. --*/
  1155. {
  1156. ACCESS_MASK t1, t2, r;
  1157. ACCESS_MASK BitsToAudit;
  1158. //
  1159. // Determine if this access is to
  1160. // be audited, and if so, clear the bits
  1161. // in the ObjectTableEntry.
  1162. //
  1163. while (ObjectTableEntryInfo->AuditMask != 0) {
  1164. t1 = ObjectTableEntryInfo->AuditMask;
  1165. t2 = t1 & ~DesiredAccess;
  1166. if (t2 != t1) {
  1167. r = InterlockedCompareExchange(&ObjectTableEntryInfo->AuditMask, t2, t1);
  1168. if (r == t1) {
  1169. //
  1170. // AuditMask was == t1, so AuditMask is now == t2
  1171. // it worked, r contains what was in AuditMask, which
  1172. // we can examine safely.
  1173. //
  1174. BitsToAudit = r & DesiredAccess;
  1175. //
  1176. // Generate audit here
  1177. //
  1178. if (BitsToAudit != 0) {
  1179. SeOperationAuditAlarm( NULL,
  1180. Handle,
  1181. ObjectTypeName,
  1182. BitsToAudit,
  1183. NULL
  1184. );
  1185. }
  1186. return;
  1187. }
  1188. //
  1189. // else, somebody changed it, go around for another try
  1190. //
  1191. } else {
  1192. //
  1193. // There are no bits in the AuditMask that we
  1194. // want to audit here, just leave.
  1195. //
  1196. return;
  1197. }
  1198. }
  1199. }
  1200. NTSTATUS
  1201. ObReferenceObjectByName (
  1202. IN PUNICODE_STRING ObjectName,
  1203. IN ULONG Attributes,
  1204. IN PACCESS_STATE AccessState OPTIONAL,
  1205. IN ACCESS_MASK DesiredAccess OPTIONAL,
  1206. IN POBJECT_TYPE ObjectType,
  1207. IN KPROCESSOR_MODE AccessMode,
  1208. IN OUT PVOID ParseContext OPTIONAL,
  1209. OUT PVOID *Object
  1210. )
  1211. /*++
  1212. Routine Description:
  1213. Given a name of an object this routine returns a pointer
  1214. to the body of the object with proper ref counts
  1215. Arguments:
  1216. ObjectName - Supplies the name of the object being referenced
  1217. Attributes - Supplies the desired handle attributes
  1218. AccessState - Supplies an optional pointer to the current access
  1219. status describing already granted access types, the privileges used
  1220. to get them, and any access types yet to be granted.
  1221. DesiredAccess - Optionally supplies the desired access to the
  1222. for the object
  1223. ObjectType - Specifies the object type according to the caller
  1224. AccessMode - Supplies the processor mode of the access
  1225. ParseContext - Optionally supplies a context to pass down to the
  1226. parse routine
  1227. Object - Receives a pointer to the referenced object body
  1228. Return Value:
  1229. An appropriate NTSTATUS value
  1230. --*/
  1231. {
  1232. UNICODE_STRING CapturedObjectName;
  1233. PVOID ExistingObject;
  1234. ACCESS_STATE LocalAccessState;
  1235. AUX_ACCESS_DATA AuxData;
  1236. NTSTATUS Status;
  1237. OBP_LOOKUP_CONTEXT LookupContext;
  1238. PAGED_CODE();
  1239. ObpValidateIrql("ObReferenceObjectByName");
  1240. //
  1241. // If the object name descriptor is not specified, or the object name
  1242. // length is zero (tested after capture), then the object name is
  1243. // invalid.
  1244. //
  1245. if (ObjectName == NULL) {
  1246. return STATUS_OBJECT_NAME_INVALID;
  1247. }
  1248. //
  1249. // Capture the object name.
  1250. //
  1251. Status = ObpCaptureObjectName( AccessMode,
  1252. ObjectName,
  1253. &CapturedObjectName,
  1254. TRUE );
  1255. if (NT_SUCCESS(Status)) {
  1256. //
  1257. // No buffer has been allocated for a zero length name so no free
  1258. // needed
  1259. //
  1260. if (CapturedObjectName.Length == 0) {
  1261. return STATUS_OBJECT_NAME_INVALID;
  1262. }
  1263. //
  1264. // If the access state is not specified, then create the access
  1265. // state.
  1266. //
  1267. if (!ARGUMENT_PRESENT(AccessState)) {
  1268. AccessState = &LocalAccessState;
  1269. Status = SeCreateAccessState( &LocalAccessState,
  1270. &AuxData,
  1271. DesiredAccess,
  1272. &ObjectType->TypeInfo.GenericMapping );
  1273. if (!NT_SUCCESS(Status)) {
  1274. goto FreeBuffer;
  1275. }
  1276. }
  1277. //
  1278. // Lookup object by name.
  1279. //
  1280. Status = ObpLookupObjectName( NULL,
  1281. &CapturedObjectName,
  1282. Attributes,
  1283. ObjectType,
  1284. AccessMode,
  1285. ParseContext,
  1286. NULL,
  1287. NULL,
  1288. AccessState,
  1289. &LookupContext,
  1290. &ExistingObject );
  1291. //
  1292. // If the directory is returned locked, then unlock it.
  1293. //
  1294. ObpReleaseLookupContext( &LookupContext );
  1295. //
  1296. // If the lookup was successful, then return the existing
  1297. // object if access is allowed. Otherwise, return NULL.
  1298. //
  1299. *Object = NULL;
  1300. if (NT_SUCCESS(Status)) {
  1301. if (ObpCheckObjectReference( ExistingObject,
  1302. AccessState,
  1303. FALSE,
  1304. AccessMode,
  1305. &Status )) {
  1306. *Object = ExistingObject;
  1307. }
  1308. }
  1309. //
  1310. // If the access state was generated, then delete the access
  1311. // state.
  1312. //
  1313. if (AccessState == &LocalAccessState) {
  1314. SeDeleteAccessState(AccessState);
  1315. }
  1316. //
  1317. // Free the object name buffer.
  1318. //
  1319. FreeBuffer:
  1320. ObpFreeObjectNameBuffer(&CapturedObjectName);
  1321. }
  1322. return Status;
  1323. }
  1324. NTSTATUS
  1325. ObReferenceObjectByPointer (
  1326. IN PVOID Object,
  1327. IN ACCESS_MASK DesiredAccess,
  1328. IN POBJECT_TYPE ObjectType,
  1329. IN KPROCESSOR_MODE AccessMode
  1330. )
  1331. /*++
  1332. Routine Description:
  1333. This routine adds another reference count to an object denoted by
  1334. a pointer to the object body
  1335. Arguments:
  1336. Object - Supplies a pointer to the object being referenced
  1337. DesiredAccess - Specifies the desired access for the reference
  1338. ObjectType - Specifies the object type according to the caller
  1339. AccessMode - Supplies the processor mode of the access
  1340. Return Value:
  1341. STATUS_SUCCESS if successful and STATUS_OBJECT_TYPE_MISMATCH otherwise
  1342. --*/
  1343. {
  1344. POBJECT_HEADER ObjectHeader;
  1345. //
  1346. // Translate the pointer to the object body to a pointer to the
  1347. // object header
  1348. //
  1349. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1350. //
  1351. // If the specified object type does not match and either the caller is
  1352. // not kernel mode or it is not a symbolic link object then it is an
  1353. // error
  1354. //
  1355. if ((ObjectHeader->Type != ObjectType) && (AccessMode != KernelMode ||
  1356. ObjectType == ObpSymbolicLinkObjectType)) {
  1357. return( STATUS_OBJECT_TYPE_MISMATCH );
  1358. }
  1359. //
  1360. // Otherwise increment the pointer count and return success to
  1361. // our caller
  1362. //
  1363. ObpIncrPointerCount( ObjectHeader );
  1364. return( STATUS_SUCCESS );
  1365. }
  1366. VOID
  1367. ObpDeferObjectDeletion (
  1368. IN POBJECT_HEADER ObjectHeader
  1369. )
  1370. {
  1371. PVOID OldValue;
  1372. //
  1373. // Push this object on the list. If we make an empty to non-empty
  1374. // transition then we may have to start a worker thread.
  1375. //
  1376. while (1) {
  1377. OldValue = ObpRemoveObjectList;
  1378. ObjectHeader->NextToFree = OldValue;
  1379. if (InterlockedCompareExchangePointer (&ObpRemoveObjectList,
  1380. ObjectHeader,
  1381. OldValue) == OldValue) {
  1382. break;
  1383. }
  1384. }
  1385. if (OldValue == NULL) {
  1386. //
  1387. // If we have to start the worker thread then go ahead
  1388. // and enqueue the work item
  1389. //
  1390. ExQueueWorkItem( &ObpRemoveObjectWorkItem, CriticalWorkQueue );
  1391. }
  1392. }
  1393. LONG
  1394. FASTCALL
  1395. ObfReferenceObject (
  1396. IN PVOID Object
  1397. )
  1398. /*++
  1399. Routine Description:
  1400. This function increments the reference count for an object.
  1401. N.B. This function should be used to increment the reference count
  1402. when the accessing mode is kernel or the objct type is known.
  1403. Arguments:
  1404. Object - Supplies a pointer to the object whose reference count is
  1405. incremented.
  1406. Return Value:
  1407. None.
  1408. --*/
  1409. {
  1410. POBJECT_HEADER ObjectHeader;
  1411. LONG RetVal;
  1412. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1413. RetVal = ObpIncrPointerCount( ObjectHeader );
  1414. ASSERT (RetVal != 1);
  1415. return RetVal;
  1416. }
  1417. LONG
  1418. FASTCALL
  1419. ObReferenceObjectEx (
  1420. IN PVOID Object,
  1421. IN ULONG Count
  1422. )
  1423. /*++
  1424. Routine Description:
  1425. This function increments the reference count for an object by the specified amount.
  1426. Arguments:
  1427. Object - Supplies a pointer to the object whose reference count is
  1428. incremented.
  1429. Count - Amount to increment by
  1430. Return Value:
  1431. LONG - New value of count
  1432. --*/
  1433. {
  1434. POBJECT_HEADER ObjectHeader;
  1435. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1436. return ObpIncrPointerCountEx (ObjectHeader, Count);
  1437. }
  1438. LONG
  1439. FASTCALL
  1440. ObDereferenceObjectEx (
  1441. IN PVOID Object,
  1442. IN ULONG Count
  1443. )
  1444. /*++
  1445. Routine Description:
  1446. This function decrements the reference count for an object by the specified amount.
  1447. Arguments:
  1448. Object - Supplies a pointer to the object whose reference count is
  1449. incremented.
  1450. Count - Amount to decrement by
  1451. Return Value:
  1452. LONG - New value of count
  1453. --*/
  1454. {
  1455. POBJECT_HEADER ObjectHeader;
  1456. LONG Result;
  1457. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1458. Result = ObpDecrPointerCountEx (ObjectHeader, Count);
  1459. if (Result == 0) {
  1460. ObpDeferObjectDeletion (ObjectHeader);
  1461. }
  1462. return Result;
  1463. }
  1464. BOOLEAN
  1465. FASTCALL
  1466. ObReferenceObjectSafe (
  1467. IN PVOID Object
  1468. )
  1469. /*++
  1470. Routine Description:
  1471. This function increments the reference count for an object. It returns
  1472. FALSE if the object is being deleted or TRUE if it's safe to use the object further
  1473. Arguments:
  1474. Object - Supplies a pointer to the object whose reference count is
  1475. incremented.
  1476. Return Value:
  1477. TRUE - The object was successfuly referenced and safe to use
  1478. FALSE - The object is being deleted
  1479. --*/
  1480. {
  1481. POBJECT_HEADER ObjectHeader;
  1482. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1483. if (ObpSafeInterlockedIncrement(&ObjectHeader->PointerCount)) {
  1484. #ifdef POOL_TAGGING
  1485. if(ObpTraceEnabled) {
  1486. ObpPushStackInfo(ObjectHeader, TRUE);
  1487. }
  1488. #endif // POOL_TAGGING
  1489. return TRUE;
  1490. }
  1491. return FALSE;
  1492. }
  1493. LONG
  1494. FASTCALL
  1495. ObfDereferenceObject (
  1496. IN PVOID Object
  1497. )
  1498. /*++
  1499. Routine Description:
  1500. This routine decrments the refernce count of the specified object and
  1501. does whatever cleanup there is if the count goes to zero.
  1502. Arguments:
  1503. Object - Supplies a pointer to the body of the object being dereferenced
  1504. Return Value:
  1505. None.
  1506. --*/
  1507. {
  1508. POBJECT_HEADER ObjectHeader;
  1509. POBJECT_TYPE ObjectType;
  1510. KIRQL OldIrql;
  1511. BOOLEAN StartWorkerThread;
  1512. LONG Result;
  1513. //
  1514. // Translate a pointer to the object body to a pointer to the object
  1515. // header.
  1516. //
  1517. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1518. #if DBG
  1519. {
  1520. POBJECT_HEADER_NAME_INFO NameInfo;
  1521. NameInfo = OBJECT_HEADER_TO_NAME_INFO( ObjectHeader );
  1522. if (NameInfo) {
  1523. InterlockedDecrement(&NameInfo->DbgDereferenceCount) ;
  1524. }
  1525. }
  1526. #endif
  1527. //
  1528. // Decrement the point count and if the result is now then
  1529. // there is extra work to do
  1530. //
  1531. ObjectType = ObjectHeader->Type;
  1532. Result = ObpDecrPointerCount( ObjectHeader );
  1533. if (Result == 0) {
  1534. //
  1535. // Find out the level we're at and the object type
  1536. //
  1537. OldIrql = KeGetCurrentIrql();
  1538. ASSERT(ObjectHeader->HandleCount == 0);
  1539. //
  1540. // If we're at the passive level then go ahead and delete the
  1541. // object now.
  1542. //
  1543. if (OldIrql == PASSIVE_LEVEL) {
  1544. #ifdef POOL_TAGGING
  1545. //
  1546. // The object is going away, so we deregister it.
  1547. //
  1548. if (ObpTraceEnabled && !ObpTraceNoDeregister) {
  1549. ObpDeregisterObject( ObjectHeader );
  1550. }
  1551. #endif //POOL_TAGGING
  1552. ObpRemoveObjectRoutine( Object, FALSE );
  1553. return Result;
  1554. } else {
  1555. //
  1556. // Objects can't be deleted from an IRQL above PASSIVE_LEVEL.
  1557. // So queue the delete operation.
  1558. //
  1559. ObpDeferObjectDeletion (ObjectHeader);
  1560. }
  1561. }
  1562. return Result;
  1563. }
  1564. VOID
  1565. ObDereferenceObjectDeferDelete (
  1566. IN PVOID Object
  1567. )
  1568. {
  1569. POBJECT_HEADER ObjectHeader;
  1570. LONG Result;
  1571. #if DBG
  1572. POBJECT_HEADER_NAME_INFO NameInfo;
  1573. #endif
  1574. //
  1575. // Translate a pointer to the object body to a pointer to the object
  1576. // header.
  1577. //
  1578. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1579. #if DBG
  1580. NameInfo = OBJECT_HEADER_TO_NAME_INFO( ObjectHeader );
  1581. if (NameInfo) {
  1582. InterlockedDecrement(&NameInfo->DbgDereferenceCount) ;
  1583. }
  1584. #endif
  1585. //
  1586. // Decrement the point count and if the result is now then
  1587. // there is extra work to do
  1588. //
  1589. Result = ObpDecrPointerCount( ObjectHeader );
  1590. if (Result == 0) {
  1591. ObpDeferObjectDeletion (ObjectHeader);
  1592. }
  1593. }
  1594. VOID
  1595. ObpProcessRemoveObjectQueue (
  1596. PVOID Parameter
  1597. )
  1598. /*++
  1599. Routine Description:
  1600. This is the work routine for the remove object work queue. Its
  1601. job is to remove and process items from the remove object queue.
  1602. Arguments:
  1603. Parameter - Ignored
  1604. Return Value:
  1605. None.
  1606. --*/
  1607. {
  1608. POBJECT_HEADER ObjectHeader, FirstObject, NextObject;
  1609. //
  1610. // Process the list of defered delete objects.
  1611. // The list head serves two purposes. First it maintains
  1612. // the list of objects we need to delete and second
  1613. // it signals that this thread is active.
  1614. // While we are processing the latest list we leave the
  1615. // header as the value 1. This will never be an object address
  1616. // as the bottom bits should be clear for an object.
  1617. //
  1618. while (1) {
  1619. ObjectHeader = InterlockedExchangePointer (&ObpRemoveObjectList,
  1620. (PVOID) 1);
  1621. while (1) {
  1622. #ifdef POOL_TAGGING
  1623. if (ObpTraceEnabled && !ObpTraceNoDeregister) {
  1624. ObpDeregisterObject( ObjectHeader );
  1625. }
  1626. #endif
  1627. NextObject = ObjectHeader->NextToFree;
  1628. ObpRemoveObjectRoutine( &ObjectHeader->Body, TRUE );
  1629. ObjectHeader = NextObject;
  1630. if (ObjectHeader == NULL || ObjectHeader == (PVOID) 1) {
  1631. break;
  1632. }
  1633. }
  1634. if (ObpRemoveObjectList == (PVOID) 1 &&
  1635. InterlockedCompareExchangePointer (&ObpRemoveObjectList,
  1636. NULL,
  1637. (PVOID) 1) == (PVOID) 1) {
  1638. break;
  1639. }
  1640. }
  1641. }
  1642. VOID
  1643. ObpRemoveObjectRoutine (
  1644. IN PVOID Object,
  1645. IN BOOLEAN CalledOnWorkerThread
  1646. )
  1647. /*++
  1648. Routine Description:
  1649. This routine is used to delete an object whose reference count has
  1650. gone to zero.
  1651. Arguments:
  1652. Object - Supplies a pointer to the body of the object being deleted
  1653. CalledOnWorkerThread - TRUE if called on worker thread, FALSE if called in
  1654. the context of the ObDereferenceObject.
  1655. Return Value:
  1656. None.
  1657. --*/
  1658. {
  1659. NTSTATUS Status;
  1660. POBJECT_HEADER ObjectHeader;
  1661. POBJECT_TYPE ObjectType;
  1662. POBJECT_HEADER_CREATOR_INFO CreatorInfo;
  1663. POBJECT_HEADER_NAME_INFO NameInfo;
  1664. PAGED_CODE();
  1665. ObpValidateIrql( "ObpRemoveObjectRoutine" );
  1666. //
  1667. // Retrieve an object header from the object body, and also get
  1668. // the object type, creator and name info if available
  1669. //
  1670. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1671. ObjectType = ObjectHeader->Type;
  1672. CreatorInfo = OBJECT_HEADER_TO_CREATOR_INFO( ObjectHeader );
  1673. NameInfo = OBJECT_HEADER_TO_NAME_INFO( ObjectHeader );
  1674. //
  1675. // If there is a creator info record and we are on the list
  1676. // for the object type then remove this object from the list
  1677. //
  1678. if (CreatorInfo != NULL && !IsListEmpty( &CreatorInfo->TypeList )) {
  1679. //
  1680. // Get exclusive access to the object type object
  1681. //
  1682. ObpEnterObjectTypeMutex( ObjectType );
  1683. RemoveEntryList( &CreatorInfo->TypeList );
  1684. //
  1685. // We are done with the object type object so we can now release it
  1686. //
  1687. ObpLeaveObjectTypeMutex( ObjectType );
  1688. }
  1689. //
  1690. // If there is a name info record and the name buffer is not null
  1691. // then free the buffer and zero out the name record
  1692. //
  1693. if (NameInfo != NULL && NameInfo->Name.Buffer != NULL) {
  1694. ExFreePool( NameInfo->Name.Buffer );
  1695. NameInfo->Name.Buffer = NULL;
  1696. NameInfo->Name.Length = 0;
  1697. NameInfo->Name.MaximumLength = 0;
  1698. }
  1699. //
  1700. // Security descriptor deletion must precede the
  1701. // call to the object's DeleteProcedure. Check if we have
  1702. // a security descriptor and if so then call the routine
  1703. // to delete the security descritpor.
  1704. //
  1705. if (ObjectHeader->SecurityDescriptor != NULL) {
  1706. KIRQL SaveIrql;
  1707. ObpBeginTypeSpecificCallOut( SaveIrql );
  1708. Status = (ObjectType->TypeInfo.SecurityProcedure)( Object,
  1709. DeleteSecurityDescriptor,
  1710. NULL, NULL, NULL,
  1711. &ObjectHeader->SecurityDescriptor,
  1712. 0,
  1713. NULL );
  1714. ObpEndTypeSpecificCallOut( SaveIrql, "Security", ObjectType, Object );
  1715. }
  1716. //
  1717. // Now if there is a delete callback for the object type invoke
  1718. // the routine
  1719. //
  1720. if (ObjectType->TypeInfo.DeleteProcedure) {
  1721. KIRQL SaveIrql;
  1722. ObpBeginTypeSpecificCallOut( SaveIrql );
  1723. if (!CalledOnWorkerThread) {
  1724. ObjectHeader->Flags |= OB_FLAG_DELETED_INLINE;
  1725. }
  1726. (*(ObjectType->TypeInfo.DeleteProcedure))(Object);
  1727. ObpEndTypeSpecificCallOut( SaveIrql, "Delete", ObjectType, Object );
  1728. }
  1729. //
  1730. // Finally return the object back to pool including releasing any quota
  1731. // charges
  1732. //
  1733. ObpFreeObject( Object );
  1734. }
  1735. VOID
  1736. ObpDeleteNameCheck (
  1737. IN PVOID Object
  1738. )
  1739. /*++
  1740. Routine Description:
  1741. This routine removes the name of an object from its parent directory
  1742. Arguments:
  1743. Object - Supplies a pointer to the object body whose name is being checked
  1744. TypeMutexHeld - Indicates if the lock on object type is being held by the
  1745. caller
  1746. Return Value:
  1747. None.
  1748. --*/
  1749. {
  1750. POBJECT_HEADER ObjectHeader;
  1751. POBJECT_TYPE ObjectType;
  1752. POBJECT_HEADER_NAME_INFO NameInfo;
  1753. POBJECT_HEADER_CREATOR_INFO CreatorInfo;
  1754. PVOID DirObject;
  1755. OBP_LOOKUP_CONTEXT LookupContext;
  1756. PAGED_CODE();
  1757. ObpValidateIrql( "ObpDeleteNameCheck" );
  1758. //
  1759. // Translate the object body to an object header also get
  1760. // the object type and name info if present
  1761. //
  1762. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1763. NameInfo = ObpReferenceNameInfo( ObjectHeader );
  1764. ObjectType = ObjectHeader->Type;
  1765. //
  1766. // Make sure that the object has a zero handle count, has a non
  1767. // empty name buffer, and is not a permanent object
  1768. //
  1769. if ((ObjectHeader->HandleCount == 0) &&
  1770. (NameInfo != NULL) &&
  1771. (NameInfo->Name.Length != 0) &&
  1772. (!(ObjectHeader->Flags & OB_FLAG_PERMANENT_OBJECT)) &&
  1773. (NameInfo->Directory != NULL)) {
  1774. ObpInitializeLookupContext(&LookupContext);
  1775. ObpLockLookupContext ( &LookupContext, NameInfo->Directory );
  1776. DirObject = NULL;
  1777. //
  1778. // Check that the object we is still in the directory otherwise
  1779. // then is nothing for us to remove
  1780. //
  1781. if (Object == ObpLookupDirectoryEntry( NameInfo->Directory,
  1782. &NameInfo->Name,
  1783. 0,
  1784. FALSE,
  1785. &LookupContext )) {
  1786. //
  1787. // Now reacquire the lock on the object type and
  1788. // check check the handle count again. If it is still
  1789. // zero then we can do the actual delete name operation
  1790. //
  1791. //
  1792. // Delete the directory entry, if the entry is still there
  1793. //
  1794. ObpLockObject( ObjectHeader );
  1795. if (ObjectHeader->HandleCount == 0 &&
  1796. (ObjectHeader->Flags & OB_FLAG_PERMANENT_OBJECT) == 0) {
  1797. KIRQL SaveIrql;
  1798. //
  1799. // Delete the directory entry
  1800. //
  1801. ObpDeleteDirectoryEntry( &LookupContext );
  1802. //
  1803. // If this is a symbolic link object then we also need to
  1804. // delete the symbolic link
  1805. //
  1806. if (ObjectType == ObpSymbolicLinkObjectType) {
  1807. ObpDeleteSymbolicLinkName( (POBJECT_SYMBOLIC_LINK)Object );
  1808. }
  1809. DirObject = NameInfo->Directory;
  1810. }
  1811. ObpUnlockObject( ObjectHeader );
  1812. }
  1813. ObpReleaseLookupContext( &LookupContext );
  1814. //
  1815. // If there is a directory object for the name then decrement
  1816. // its reference count for it and for the object
  1817. //
  1818. if (DirObject != NULL) {
  1819. //
  1820. // Dereference the name twice: one because we referenced it to
  1821. // saftely access the name info, and the second deref is because
  1822. // we want a deletion for the NameInfo
  1823. //
  1824. ObpDereferenceNameInfo(NameInfo);
  1825. ObpDereferenceNameInfo(NameInfo);
  1826. ObDereferenceObject( Object );
  1827. }
  1828. } else {
  1829. ObpDereferenceNameInfo(NameInfo);
  1830. }
  1831. return;
  1832. }
  1833. //
  1834. // Thunks to support standard call callers
  1835. //
  1836. #ifdef ObDereferenceObject
  1837. #undef ObDereferenceObject
  1838. #endif
  1839. LONG
  1840. ObDereferenceObject (
  1841. IN PVOID Object
  1842. )
  1843. /*++
  1844. Routine Description:
  1845. This is really just a thunk for the Obf version of the dereference
  1846. routine
  1847. Arguments:
  1848. Object - Supplies a pointer to the body of the object being dereferenced
  1849. Return Value:
  1850. None.
  1851. --*/
  1852. {
  1853. return ObfDereferenceObject (Object) ;
  1854. }
  1855. BOOLEAN
  1856. ObIsObjectDeletionInline(
  1857. IN PVOID Object
  1858. )
  1859. /*++
  1860. Routine Description:
  1861. This is available only of object DeleteProcedure callbacks. It allows the
  1862. callback to determine whether the stack on which it is invoked is
  1863. Arguments:
  1864. Object - Supplies a pointer to the body of the object being deleted
  1865. Return Value:
  1866. TRUE if the deletion procedure is being invoked on the same stack as the
  1867. ObDereferenceObject, and FALSE if the procedure is being invoked from a
  1868. queued work item.
  1869. --*/
  1870. {
  1871. POBJECT_HEADER ObjectHeader;
  1872. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1873. return ((ObjectHeader->Flags & OB_FLAG_DELETED_INLINE) != 0);
  1874. }