I have the following problem:
Given an object handle, an object pointer (pointer and handle refer to the same object), and the pid of the process that owns the handle, I want to be able to access the object pointer safely. Note that the object handle and pointer come from a call to ZwQuerySystemInformation(SystemHandleInformation), therefore they may refer to an object that does not exist anymore and the process with the given pid may not exist either.
My current solution is as follows:
- Get a handle to the owner process
- Duplicate the handle into the current process
- Call ObReferenceObjectByPointer using the originally supplied object pointer
If all 3 steps succeed I assume that it is OK to use the object file pointer. Once I am done with the file pointer I clean by:
- ObDereferenceObject on the object pointer
- Close the duplicated handle (I have to play some tricks to make sure the handle can be closed)
- Close the owner process handle
This code ran successfully for several weeks until today. I got SYSTEM_THREAD_EXCEPTION_NOT_HANDLED (STATUS_ACCESS_VIOLATION) when trying to perform step 1 of the dereferencing sequence. Here is the stack trace (obtained by “!analyze -v”):
nt!ObpRemoveObjectRoutine+0xc4
nt!ObfDereferenceObject+0x67
nt!VerifierObfDereferenceObject+0xa
mydriver!DereferenceObject+0x15
mydriver!MakeWrappers+0x5dc
mydriver!DiscoverObjectsAndMakeWrappers+0x30b
mydriver!DriverEntry+0x3af
nt!IopLoadDriver+0x689
nt!IopLoadUnloadDriver+0x45
nt!ExpWorkerThread+0xeb
nt!PspSystemThreadStartup+0x2e
nt!KiThreadStartup+0x16
The actual exception seems to be thrown on a different thread . Its stack trace is (obtained by “k”):
nt!KeBugCheckEx+0x1b
nt!PspUnhandledExceptionInSystemThread+0x1a
nt!PspSystemThreadStartup+0x56
nt!KiThreadStartup+0x16
The exception details are:
EXCEPTION_RECORD: f78f6a24 – (.exr 0xfffffffff78f6a24)
ExceptionAddress: 80929979 (nt!ObpRemoveObjectRoutine+0x000000c4)
ExceptionCode: c0000005 (Access violation)
ExceptionFlags: 00000000
NumberParameters: 2
Parameter[0]: 00000000
Parameter[1]: 00000098
Attempt to read from address 00000098
My questions are:
-
What is wrong with my approach? I have my handles to the process and the object and managed to increment the refcount on the file object. Why would decrementing the refcount on the file object cause this error?
-
Are there another approache to achieve what I am trying to do?
Thank you in advance,
–aydan