#define NDEBUG
#include <internal/debug.h>
-#define EX_OBJ_TO_HDR(eob) ((POBJECT_HEADER)((ULONG_PTR)(eob) & \
- ~(EX_HANDLE_ENTRY_PROTECTFROMCLOSE | EX_HANDLE_ENTRY_INHERITABLE | \
- EX_HANDLE_ENTRY_AUDITONCLOSE)))
-#define EX_HTE_TO_HDR(hte) ((POBJECT_HEADER)((ULONG_PTR)((hte)->u1.Object) & \
- ~(EX_HANDLE_ENTRY_PROTECTFROMCLOSE | EX_HANDLE_ENTRY_INHERITABLE | \
- EX_HANDLE_ENTRY_AUDITONCLOSE)))
-
#define GENERIC_ANY (GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL)
/* GLOBALS *****************************************************************/
static VOID
ObpDecrementHandleCount(PVOID ObjectBody)
{
- POBJECT_HEADER ObjectHeader = BODY_TO_HEADER(ObjectBody);
+ PROS_OBJECT_HEADER ObjectHeader = BODY_TO_HEADER(ObjectBody);
LONG NewHandleCount = InterlockedDecrement(&ObjectHeader->HandleCount);
DPRINT("Header: %x\n", ObjectHeader);
DPRINT("NewHandleCount: %x\n", NewHandleCount);
{
/* the handle count should be decremented but we pass the previous value
to the callback */
- ObjectHeader->Type->TypeInfo.CloseProcedure(ObjectBody, NewHandleCount + 1);
+ ObjectHeader->Type->TypeInfo.CloseProcedure(NULL, ObjectBody, 0, NewHandleCount + 1, NewHandleCount + 1);
}
if(NewHandleCount == 0)
/* delete the object from the namespace when the last handle got closed.
Only do this if it's actually been inserted into the namespace and
if it's not a permanent object. */
- ObpRemoveEntryDirectory(ObjectHeader);
+ ObpRemoveEntryDirectory((PROS_OBJECT_HEADER)ObjectHeader);
}
/* remove the keep-alive reference */
NTSTATUS
+NTAPI
ObpQueryHandleAttributes(HANDLE Handle,
POBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo)
{
- PHANDLE_TABLE HandleTable;
PHANDLE_TABLE_ENTRY HandleTableEntry;
- LONG ExHandle;
+ PEPROCESS Process, CurrentProcess;
+ KAPC_STATE ApcState;
+ BOOLEAN AttachedToProcess = FALSE;
+ NTSTATUS Status = STATUS_SUCCESS;
PAGED_CODE();
- DPRINT("ObpQueryHandleAttributes(Handle %x)\n", Handle);
+ DPRINT("ObpQueryHandleAttributes(Handle %p)\n", Handle);
+ CurrentProcess = PsGetCurrentProcess();
+
+ KeEnterCriticalRegion();
if(ObIsKernelHandle(Handle, ExGetPreviousMode()))
{
- HandleTable = ObpKernelHandleTable;
- ExHandle = HANDLE_TO_EX_HANDLE(ObKernelHandleToHandle(Handle));
+ Process = PsInitialSystemProcess;
+ Handle = ObKernelHandleToHandle(Handle);
+
+ if (Process != CurrentProcess)
+ {
+ KeStackAttachProcess(&Process->Pcb,
+ &ApcState);
+ AttachedToProcess = TRUE;
+ }
}
else
{
- HandleTable = PsGetCurrentProcess()->ObjectTable;
- ExHandle = HANDLE_TO_EX_HANDLE(Handle);
+ Process = CurrentProcess;
}
- KeEnterCriticalRegion();
-
- HandleTableEntry = ExMapHandleToPointer(HandleTable,
- ExHandle);
- if (HandleTableEntry == NULL)
- {
- KeLeaveCriticalRegion();
- return STATUS_INVALID_HANDLE;
- }
+ HandleTableEntry = ExMapHandleToPointer(Process->ObjectTable,
+ Handle);
+ if (HandleTableEntry != NULL)
+ {
+ HandleInfo->Inherit = (HandleTableEntry->u1.ObAttributes & EX_HANDLE_ENTRY_INHERITABLE) != 0;
+ HandleInfo->ProtectFromClose = (HandleTableEntry->u1.ObAttributes & EX_HANDLE_ENTRY_PROTECTFROMCLOSE) != 0;
- HandleInfo->Inherit = (HandleTableEntry->u1.ObAttributes & EX_HANDLE_ENTRY_INHERITABLE) != 0;
- HandleInfo->ProtectFromClose = (HandleTableEntry->u1.ObAttributes & EX_HANDLE_ENTRY_PROTECTFROMCLOSE) != 0;
+ ExUnlockHandleTableEntry(Process->ObjectTable,
+ HandleTableEntry);
+ }
+ else
+ Status = STATUS_INVALID_HANDLE;
- ExUnlockHandleTableEntry(HandleTable,
- HandleTableEntry);
+ if (AttachedToProcess)
+ {
+ KeUnstackDetachProcess(&ApcState);
+ }
KeLeaveCriticalRegion();
- return STATUS_SUCCESS;
+ return Status;
}
NTSTATUS
+NTAPI
ObpSetHandleAttributes(HANDLE Handle,
POBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo)
{
- PHANDLE_TABLE HandleTable;
PHANDLE_TABLE_ENTRY HandleTableEntry;
- LONG ExHandle;
+ PEPROCESS Process, CurrentProcess;
+ KAPC_STATE ApcState;
+ BOOLEAN AttachedToProcess = FALSE;
+ NTSTATUS Status = STATUS_SUCCESS;
PAGED_CODE();
- DPRINT("ObpSetHandleAttributes(Handle %x)\n", Handle);
+ DPRINT("ObpSetHandleAttributes(Handle %p)\n", Handle);
+ CurrentProcess = PsGetCurrentProcess();
+
+ KeEnterCriticalRegion();
if(ObIsKernelHandle(Handle, ExGetPreviousMode()))
{
- HandleTable = ObpKernelHandleTable;
- ExHandle = HANDLE_TO_EX_HANDLE(ObKernelHandleToHandle(Handle));
+ Process = PsInitialSystemProcess;
+ Handle = ObKernelHandleToHandle(Handle);
+
+ if (Process != CurrentProcess)
+ {
+ KeStackAttachProcess(&Process->Pcb,
+ &ApcState);
+ AttachedToProcess = TRUE;
+ }
}
else
{
- HandleTable = PsGetCurrentProcess()->ObjectTable;
- ExHandle = HANDLE_TO_EX_HANDLE(Handle);
+ Process = CurrentProcess;
}
- KeEnterCriticalRegion();
+ HandleTableEntry = ExMapHandleToPointer(Process->ObjectTable,
+ Handle);
+ if (HandleTableEntry != NULL)
+ {
+ if (HandleInfo->Inherit)
+ HandleTableEntry->u1.ObAttributes |= EX_HANDLE_ENTRY_INHERITABLE;
+ else
+ HandleTableEntry->u1.ObAttributes &= ~EX_HANDLE_ENTRY_INHERITABLE;
- HandleTableEntry = ExMapHandleToPointer(HandleTable,
- ExHandle);
- if (HandleTableEntry == NULL)
- {
- KeLeaveCriticalRegion();
- return STATUS_INVALID_HANDLE;
- }
+ if (HandleInfo->ProtectFromClose)
+ HandleTableEntry->u1.ObAttributes |= EX_HANDLE_ENTRY_PROTECTFROMCLOSE;
+ else
+ HandleTableEntry->u1.ObAttributes &= ~EX_HANDLE_ENTRY_PROTECTFROMCLOSE;
- if (HandleInfo->Inherit)
- HandleTableEntry->u1.ObAttributes |= EX_HANDLE_ENTRY_INHERITABLE;
- else
- HandleTableEntry->u1.ObAttributes &= ~EX_HANDLE_ENTRY_INHERITABLE;
+ /* FIXME: Do we need to set anything in the object header??? */
- if (HandleInfo->ProtectFromClose)
- HandleTableEntry->u1.ObAttributes |= EX_HANDLE_ENTRY_PROTECTFROMCLOSE;
+ ExUnlockHandleTableEntry(Process->ObjectTable,
+ HandleTableEntry);
+ }
else
- HandleTableEntry->u1.ObAttributes &= ~EX_HANDLE_ENTRY_PROTECTFROMCLOSE;
-
- /* FIXME: Do we need to set anything in the object header??? */
+ Status = STATUS_INVALID_HANDLE;
- ExUnlockHandleTableEntry(HandleTable,
- HandleTableEntry);
+ if (AttachedToProcess)
+ {
+ KeUnstackDetachProcess(&ApcState);
+ }
KeLeaveCriticalRegion();
- return STATUS_SUCCESS;
+ return Status;
}
static NTSTATUS
-ObpDeleteHandle(PHANDLE_TABLE HandleTable,
- HANDLE Handle)
+ObpDeleteHandle(HANDLE Handle)
{
PHANDLE_TABLE_ENTRY HandleEntry;
PVOID Body;
- POBJECT_HEADER ObjectHeader;
- LONG ExHandle = HANDLE_TO_EX_HANDLE(Handle);
+ PROS_OBJECT_HEADER ObjectHeader;
+ PHANDLE_TABLE ObjectTable;
PAGED_CODE();
- DPRINT("ObpDeleteHandle(Handle %x)\n",Handle);
+ DPRINT("ObpDeleteHandle(Handle %p)\n",Handle);
+
+ ObjectTable = PsGetCurrentProcess()->ObjectTable;
KeEnterCriticalRegion();
- HandleEntry = ExMapHandleToPointer(HandleTable,
- ExHandle);
+ HandleEntry = ExMapHandleToPointer(ObjectTable,
+ Handle);
if(HandleEntry != NULL)
{
if(HandleEntry->u1.ObAttributes & EX_HANDLE_ENTRY_PROTECTFROMCLOSE)
{
- ExUnlockHandleTableEntry(HandleTable,
+ ExUnlockHandleTableEntry(ObjectTable,
HandleEntry);
KeLeaveCriticalRegion();
ObjectHeader = EX_HTE_TO_HDR(HandleEntry);
Body = &ObjectHeader->Body;
- ObpDecrementHandleCount(Body);
-
- /* destroy and unlock the handle entry */
- ExDestroyHandleByEntry(HandleTable,
+ /* destroy and unlock the handle entry */
+ ExDestroyHandleByEntry(ObjectTable,
HandleEntry,
- ExHandle);
+ Handle);
+
+ ObpDecrementHandleCount(Body);
KeLeaveCriticalRegion();
NTSTATUS
+NTAPI
ObDuplicateObject(PEPROCESS SourceProcess,
PEPROCESS TargetProcess,
HANDLE SourceHandle,
PHANDLE TargetHandle,
ACCESS_MASK DesiredAccess,
- BOOLEAN InheritHandle,
+ ULONG HandleAttributes,
ULONG Options)
{
- PHANDLE_TABLE SourceHandleTable;
PHANDLE_TABLE_ENTRY SourceHandleEntry;
HANDLE_TABLE_ENTRY NewHandleEntry;
+ BOOLEAN AttachedToProcess = FALSE;
PVOID ObjectBody;
- POBJECT_HEADER ObjectHeader;
- LONG ExTargetHandle;
- LONG ExSourceHandle;
+ PROS_OBJECT_HEADER ObjectHeader;
ULONG NewHandleCount;
+ HANDLE NewTargetHandle;
+ PEPROCESS CurrentProcess;
+ KAPC_STATE ApcState;
+ NTSTATUS Status = STATUS_SUCCESS;
PAGED_CODE();
- if(ObIsKernelHandle(SourceHandle, ExGetPreviousMode()))
+ if(SourceProcess == NULL ||
+ ObIsKernelHandle(SourceHandle, ExGetPreviousMode()))
{
- SourceHandleTable = ObpKernelHandleTable;
+ SourceProcess = PsInitialSystemProcess;
SourceHandle = ObKernelHandleToHandle(SourceHandle);
}
- else
- {
- SourceHandleTable = SourceProcess->ObjectTable;
- }
- ExSourceHandle = HANDLE_TO_EX_HANDLE(SourceHandle);
+ CurrentProcess = PsGetCurrentProcess();
KeEnterCriticalRegion();
- SourceHandleEntry = ExMapHandleToPointer(SourceHandleTable,
- ExSourceHandle);
+ if (SourceProcess != CurrentProcess)
+ {
+ KeStackAttachProcess(&SourceProcess->Pcb,
+ &ApcState);
+ AttachedToProcess = TRUE;
+ }
+ SourceHandleEntry = ExMapHandleToPointer(SourceProcess->ObjectTable,
+ SourceHandle);
if (SourceHandleEntry == NULL)
{
+ if (AttachedToProcess)
+ {
+ KeUnstackDetachProcess(&ApcState);
+ }
+
KeLeaveCriticalRegion();
return STATUS_INVALID_HANDLE;
}
ObjectBody = &ObjectHeader->Body;
NewHandleEntry.u1.Object = SourceHandleEntry->u1.Object;
- if(InheritHandle)
+ if(HandleAttributes & OBJ_INHERIT)
NewHandleEntry.u1.ObAttributes |= EX_HANDLE_ENTRY_INHERITABLE;
else
NewHandleEntry.u1.ObAttributes &= ~EX_HANDLE_ENTRY_INHERITABLE;
NewHandleCount = InterlockedIncrement(&ObjectHeader->HandleCount);
ASSERT(NewHandleCount >= 2);
- ExUnlockHandleTableEntry(SourceHandleTable,
+ ExUnlockHandleTableEntry(SourceProcess->ObjectTable,
SourceHandleEntry);
- KeLeaveCriticalRegion();
+ if (AttachedToProcess)
+ {
+ KeUnstackDetachProcess(&ApcState);
+ AttachedToProcess = FALSE;
+ }
+
+ if (TargetProcess != CurrentProcess)
+ {
+ KeStackAttachProcess(&TargetProcess->Pcb,
+ &ApcState);
+ AttachedToProcess = TRUE;
+ }
/* attempt to create the new handle */
- ExTargetHandle = ExCreateHandle(TargetProcess->ObjectTable,
- &NewHandleEntry);
- if (ExTargetHandle != EX_INVALID_HANDLE)
+ NewTargetHandle = ExCreateHandle(TargetProcess->ObjectTable,
+ &NewHandleEntry);
+ if (AttachedToProcess)
+ {
+ KeUnstackDetachProcess(&ApcState);
+ AttachedToProcess = FALSE;
+ }
+
+ if (NewTargetHandle != NULL)
{
if (Options & DUPLICATE_CLOSE_SOURCE)
{
- ObpDeleteHandle(SourceHandleTable,
- SourceHandle);
+ if (SourceProcess != CurrentProcess)
+ {
+ KeStackAttachProcess(&SourceProcess->Pcb,
+ &ApcState);
+ AttachedToProcess = TRUE;
+ }
+
+ /* delete the source handle */
+ ObpDeleteHandle(SourceHandle);
+
+ if (AttachedToProcess)
+ {
+ KeUnstackDetachProcess(&ApcState);
+ }
}
ObDereferenceObject(ObjectBody);
- *TargetHandle = EX_HANDLE_TO_HANDLE(ExTargetHandle);
-
- return STATUS_SUCCESS;
+ *TargetHandle = NewTargetHandle;
}
else
{
}
ObDereferenceObject(ObjectBody);
- return STATUS_UNSUCCESSFUL;
+ Status = STATUS_UNSUCCESSFUL;
}
+
+ KeLeaveCriticalRegion();
+
+ return Status;
}
/*
IN HANDLE TargetProcessHandle,
OUT PHANDLE TargetHandle OPTIONAL,
IN ACCESS_MASK DesiredAccess,
- IN ULONG InheritHandle,
+ IN ULONG HandleAttributes,
IN ULONG Options)
/*
* FUNCTION: Copies a handle from one process space to another
* TargetHandle (OUT) = Caller should supply storage for the
* duplicated handle.
* DesiredAccess = The desired access to the handle.
- * InheritHandle = Indicates wheter the new handle will be inheritable
- * or not.
+ * HandleAttributes = The desired handle attributes.
* Options = Specifies special actions upon duplicating the handle.
* Can be one of the values DUPLICATE_CLOSE_SOURCE |
* DUPLICATE_SAME_ACCESS. DUPLICATE_CLOSE_SOURCE specifies
{
PEPROCESS SourceProcess;
PEPROCESS TargetProcess;
+ PEPROCESS CurrentProcess;
HANDLE hTarget;
+ BOOLEAN AttachedToProcess = FALSE;
KPROCESSOR_MODE PreviousMode;
+ KAPC_STATE ApcState;
NTSTATUS Status = STATUS_SUCCESS;
PAGED_CODE();
{
_SEH_TRY
{
- ProbeForWrite(TargetHandle,
- sizeof(HANDLE),
- sizeof(ULONG));
+ ProbeForWriteHandle(TargetHandle);
}
_SEH_HANDLE
{
return(Status);
}
+ CurrentProcess = PsGetCurrentProcess();
+
/* Check for magic handle first */
if (SourceHandle == NtCurrentThread() ||
SourceHandle == NtCurrentProcess())
&ObjectType->TypeInfo.GenericMapping);
}
}
- Status = ObpCreateHandle(TargetProcess,
- ObjectBody,
+
+ if (TargetProcess != CurrentProcess)
+ {
+ KeStackAttachProcess(&TargetProcess->Pcb,
+ &ApcState);
+ AttachedToProcess = TRUE;
+ }
+
+ Status = ObpCreateHandle(ObjectBody,
DesiredAccess,
- InheritHandle,
+ HandleAttributes,
&hTarget);
+ if (AttachedToProcess)
+ {
+ KeUnstackDetachProcess(&ApcState);
+ AttachedToProcess = FALSE;
+ }
+
ObDereferenceObject(ObjectBody);
if (Options & DUPLICATE_CLOSE_SOURCE)
{
- ObpDeleteHandle(SourceProcess->ObjectTable,
- SourceHandle);
+ if (SourceProcess != CurrentProcess)
+ {
+ KeStackAttachProcess(&SourceProcess->Pcb,
+ &ApcState);
+ AttachedToProcess = TRUE;
+ }
+
+ ObpDeleteHandle(SourceHandle);
+
+ if (AttachedToProcess)
+ {
+ KeUnstackDetachProcess(&ApcState);
+ }
}
}
}
SourceHandle,
&hTarget,
DesiredAccess,
- InheritHandle,
+ HandleAttributes,
Options);
}
}
static VOID STDCALL
-DeleteHandleCallback(PHANDLE_TABLE HandleTable,
- PVOID Object,
- ULONG GrantedAccess,
- PVOID Context)
+SweepHandleCallback(PHANDLE_TABLE HandleTable,
+ PVOID Object,
+ ULONG GrantedAccess,
+ PVOID Context)
{
- POBJECT_HEADER ObjectHeader;
+ PROS_OBJECT_HEADER ObjectHeader;
PVOID ObjectBody;
PAGED_CODE();
PHANDLE_TABLE_ENTRY HandleTableEntry,
PVOID Context)
{
- POBJECT_HEADER ObjectHeader;
+ PROS_OBJECT_HEADER ObjectHeader;
BOOLEAN Ret = FALSE;
PAGED_CODE();
return Ret;
}
-VOID ObCreateHandleTable(PEPROCESS Parent,
+VOID
+NTAPI
+ObCreateHandleTable(PEPROCESS Parent,
BOOLEAN Inherit,
PEPROCESS Process)
/*
{
PAGED_CODE();
- ExDestroyHandleTable(Process->ObjectTable,
- DeleteHandleCallback,
- Process);
+ /* FIXME - Temporary hack: sweep and destroy here, needs to be fixed!!! */
+ ExSweepHandleTable(Process->ObjectTable,
+ SweepHandleCallback,
+ Process);
+ ExDestroyHandleTable(Process->ObjectTable);
+ Process->ObjectTable = NULL;
}
NTSTATUS
-ObpCreateHandle(PEPROCESS Process,
- PVOID ObjectBody,
- ACCESS_MASK GrantedAccess,
- BOOLEAN Inherit,
- PHANDLE HandleReturn)
+NTAPI
+ObpCreateHandle(PVOID ObjectBody,
+ ACCESS_MASK GrantedAccess,
+ ULONG HandleAttributes,
+ PHANDLE HandleReturn)
/*
* FUNCTION: Add a handle referencing an object
* ARGUMENTS:
*/
{
HANDLE_TABLE_ENTRY NewEntry;
- POBJECT_HEADER ObjectHeader;
- LONG ExHandle;
+ PEPROCESS Process, CurrentProcess;
+ PROS_OBJECT_HEADER ObjectHeader;
+ HANDLE Handle;
+ KAPC_STATE ApcState;
+ BOOLEAN AttachedToProcess = FALSE;
PAGED_CODE();
- DPRINT("ObpCreateHandle(Process %x, obj %x)\n",Process,ObjectBody);
+ DPRINT("ObpCreateHandle(obj %p)\n",ObjectBody);
- ASSERT(Process);
ASSERT(ObjectBody);
+ CurrentProcess = PsGetCurrentProcess();
+
ObjectHeader = BODY_TO_HEADER(ObjectBody);
+ /* check that this is a valid kernel pointer */
ASSERT((ULONG_PTR)ObjectHeader & EX_HANDLE_ENTRY_LOCKED);
if (GrantedAccess & MAXIMUM_ALLOWED)
}
NewEntry.u1.Object = ObjectHeader;
- if(Inherit)
+ if(HandleAttributes & OBJ_INHERIT)
NewEntry.u1.ObAttributes |= EX_HANDLE_ENTRY_INHERITABLE;
else
NewEntry.u1.ObAttributes &= ~EX_HANDLE_ENTRY_INHERITABLE;
NewEntry.u2.GrantedAccess = GrantedAccess;
- ExHandle = ExCreateHandle(Process->ObjectTable,
- &NewEntry);
- DPRINT("ObCreateHandle(0x%x)==0x%x [HT:0x%x]\n", ObjectHeader, *HandleReturn, Process->ObjectTable);
- if(ExHandle != EX_INVALID_HANDLE)
+ if ((HandleAttributes & OBJ_KERNEL_HANDLE) &&
+ ExGetPreviousMode == KernelMode)
+ {
+ Process = PsInitialSystemProcess;
+ if (Process != CurrentProcess)
+ {
+ KeStackAttachProcess(&Process->Pcb,
+ &ApcState);
+ AttachedToProcess = TRUE;
+ }
+ }
+ else
{
+ Process = CurrentProcess;
+ /* mask out the OBJ_KERNEL_HANDLE attribute */
+ HandleAttributes &= ~OBJ_KERNEL_HANDLE;
+ }
+
+ Handle = ExCreateHandle(Process->ObjectTable,
+ &NewEntry);
+
+ if (AttachedToProcess)
+ {
+ KeUnstackDetachProcess(&ApcState);
+ }
+
+ if(Handle != NULL)
+ {
+ if (HandleAttributes & OBJ_KERNEL_HANDLE)
+ {
+ /* mark the handle value */
+ Handle = ObMarkHandleAsKernelHandle(Handle);
+ }
+
if(InterlockedIncrement(&ObjectHeader->HandleCount) == 1)
{
- ObReferenceObjectByPointer(ObjectBody,
- 0,
- NULL,
- UserMode);
+ ObReferenceObject(ObjectBody);
}
- *HandleReturn = EX_HANDLE_TO_HANDLE(ExHandle);
+ *HandleReturn = Handle;
return STATUS_SUCCESS;
}
OUT PBOOLEAN GenerateOnClose)
{
PHANDLE_TABLE_ENTRY HandleEntry;
- PEPROCESS Process;
- LONG ExHandle = HANDLE_TO_EX_HANDLE(Handle);
+ PEPROCESS Process, CurrentProcess;
+ KAPC_STATE ApcState;
+ BOOLEAN AttachedToProcess = FALSE;
+ NTSTATUS Status = STATUS_SUCCESS;
PAGED_CODE();
- DPRINT("ObQueryObjectAuditingByHandle(Handle %x)\n", Handle);
+ DPRINT("ObQueryObjectAuditingByHandle(Handle %p)\n", Handle);
- Process = PsGetCurrentProcess();
+ CurrentProcess = PsGetCurrentProcess();
KeEnterCriticalRegion();
+ if(ObIsKernelHandle(Handle, ExGetPreviousMode()))
+ {
+ Process = PsInitialSystemProcess;
+ Handle = ObKernelHandleToHandle(Handle);
+
+ if (Process != CurrentProcess)
+ {
+ KeStackAttachProcess(&Process->Pcb,
+ &ApcState);
+ AttachedToProcess = TRUE;
+ }
+ }
+ else
+ Process = CurrentProcess;
+
HandleEntry = ExMapHandleToPointer(Process->ObjectTable,
- ExHandle);
+ Handle);
if(HandleEntry != NULL)
{
*GenerateOnClose = (HandleEntry->u1.ObAttributes & EX_HANDLE_ENTRY_AUDITONCLOSE) != 0;
ExUnlockHandleTableEntry(Process->ObjectTable,
HandleEntry);
+ }
+ else
+ Status = STATUS_INVALID_HANDLE;
- KeLeaveCriticalRegion();
-
- return STATUS_SUCCESS;
+ if (AttachedToProcess)
+ {
+ KeUnstackDetachProcess(&ApcState);
}
KeLeaveCriticalRegion();
- return STATUS_INVALID_HANDLE;
+ return Status;
}
POBJECT_HANDLE_INFORMATION HandleInformation)
{
PHANDLE_TABLE_ENTRY HandleEntry;
- POBJECT_HEADER ObjectHeader;
- PHANDLE_TABLE HandleTable;
+ PROS_OBJECT_HEADER ObjectHeader;
PVOID ObjectBody;
ACCESS_MASK GrantedAccess;
ULONG Attributes;
- LONG ExHandle;
+ PEPROCESS CurrentProcess, Process;
+ BOOLEAN AttachedToProcess = FALSE;
+ KAPC_STATE ApcState;
PAGED_CODE();
- DPRINT("ObReferenceObjectByHandle(Handle %x, DesiredAccess %x, "
- "ObjectType %x, AccessMode %d, Object %x)\n",Handle,DesiredAccess,
+ DPRINT("ObReferenceObjectByHandle(Handle %p, DesiredAccess %x, "
+ "ObjectType %p, AccessMode %d, Object %p)\n",Handle,DesiredAccess,
ObjectType,AccessMode,Object);
if (Handle == NULL)
{
return STATUS_INVALID_HANDLE;
}
+
+ CurrentProcess = PsGetCurrentProcess();
+
/*
* Handle special handle names
*/
if (Handle == NtCurrentProcess() &&
(ObjectType == PsProcessType || ObjectType == NULL))
{
- PEPROCESS CurrentProcess = PsGetCurrentProcess();
-
ObReferenceObject(CurrentProcess);
if (HandleInformation != NULL)
}
*Object = CurrentProcess;
- DPRINT("Referencing current process %x\n", CurrentProcess);
+ DPRINT("Referencing current process %p\n", CurrentProcess);
return STATUS_SUCCESS;
}
else if (Handle == NtCurrentProcess())
if(ObIsKernelHandle(Handle, AccessMode))
{
- HandleTable = ObpKernelHandleTable;
- ExHandle = HANDLE_TO_EX_HANDLE(ObKernelHandleToHandle(Handle));
+ Process = PsInitialSystemProcess;
+ Handle = ObKernelHandleToHandle(Handle);
}
else
{
- HandleTable = PsGetCurrentProcess()->ObjectTable;
- ExHandle = HANDLE_TO_EX_HANDLE(Handle);
+ Process = CurrentProcess;
}
KeEnterCriticalRegion();
- HandleEntry = ExMapHandleToPointer(HandleTable,
- ExHandle);
+ if (Process != CurrentProcess)
+ {
+ KeStackAttachProcess(&Process->Pcb,
+ &ApcState);
+ AttachedToProcess = TRUE;
+ }
+
+ HandleEntry = ExMapHandleToPointer(Process->ObjectTable,
+ Handle);
if (HandleEntry == NULL)
{
+ if (AttachedToProcess)
+ {
+ KeUnstackDetachProcess(&ApcState);
+ }
KeLeaveCriticalRegion();
- DPRINT("ExMapHandleToPointer() failed for handle 0x%x\n", Handle);
+ DPRINT("ExMapHandleToPointer() failed for handle 0x%p\n", Handle);
return(STATUS_INVALID_HANDLE);
}
ObjectHeader = EX_HTE_TO_HDR(HandleEntry);
ObjectBody = &ObjectHeader->Body;
- DPRINT("locked1: ObjectHeader: 0x%x [HT:0x%x]\n", ObjectHeader, HandleTable);
+ DPRINT("locked1: ObjectHeader: 0x%p [HT:0x%p]\n", ObjectHeader, Process->ObjectTable);
if (ObjectType != NULL && ObjectType != ObjectHeader->Type)
{
- DPRINT("ObjectType mismatch: %wZ vs %wZ (handle 0x%x)\n", &ObjectType->Name, ObjectHeader->Type ? &ObjectHeader->Type->Name : NULL, Handle);
+ DPRINT("ObjectType mismatch: %wZ vs %wZ (handle 0x%p)\n", &ObjectType->Name, ObjectHeader->Type ? &ObjectHeader->Type->Name : NULL, Handle);
- ExUnlockHandleTableEntry(HandleTable,
+ ExUnlockHandleTableEntry(Process->ObjectTable,
HandleEntry);
+ if (AttachedToProcess)
+ {
+ KeUnstackDetachProcess(&ApcState);
+ }
+
KeLeaveCriticalRegion();
return(STATUS_OBJECT_TYPE_MISMATCH);
rights than the handle can grant */
if(AccessMode != KernelMode && (~GrantedAccess & DesiredAccess))
{
- ExUnlockHandleTableEntry(HandleTable,
+ ExUnlockHandleTableEntry(Process->ObjectTable,
HandleEntry);
+ if (AttachedToProcess)
+ {
+ KeUnstackDetachProcess(&ApcState);
+ }
+
KeLeaveCriticalRegion();
DPRINT1("GrantedAccess: 0x%x, ~GrantedAccess: 0x%x, DesiredAccess: 0x%x, denied: 0x%x\n", GrantedAccess, ~GrantedAccess, DesiredAccess, ~GrantedAccess & DesiredAccess);
EX_HANDLE_ENTRY_INHERITABLE |
EX_HANDLE_ENTRY_AUDITONCLOSE);
- ExUnlockHandleTableEntry(HandleTable,
+ ExUnlockHandleTableEntry(Process->ObjectTable,
HandleEntry);
+ if (AttachedToProcess)
+ {
+ KeUnstackDetachProcess(&ApcState);
+ }
+
KeLeaveCriticalRegion();
if (HandleInformation != NULL)
NTSTATUS STDCALL
NtClose(IN HANDLE Handle)
{
- PHANDLE_TABLE HandleTable;
+ PEPROCESS Process, CurrentProcess;
+ BOOLEAN AttachedToProcess = FALSE;
+ KAPC_STATE ApcState;
NTSTATUS Status;
+ KPROCESSOR_MODE PreviousMode;
PAGED_CODE();
- if(ObIsKernelHandle(Handle, ExGetPreviousMode()))
+ PreviousMode = ExGetPreviousMode();
+ CurrentProcess = PsGetCurrentProcess();
+
+ if(ObIsKernelHandle(Handle, PreviousMode))
{
- HandleTable = ObpKernelHandleTable;
+ Process = PsInitialSystemProcess;
Handle = ObKernelHandleToHandle(Handle);
+
+ if (Process != CurrentProcess)
+ {
+ KeStackAttachProcess(&Process->Pcb,
+ &ApcState);
+ AttachedToProcess = TRUE;
+ }
}
else
+ Process = CurrentProcess;
+
+ Status = ObpDeleteHandle(Handle);
+
+ if (AttachedToProcess)
{
- HandleTable = PsGetCurrentProcess()->ObjectTable;
+ KeUnstackDetachProcess(&ApcState);
}
- Status = ObpDeleteHandle(HandleTable,
- Handle);
if (!NT_SUCCESS(Status))
{
- if((ExGetPreviousMode() != KernelMode) &&
- (PsGetCurrentProcess()->ExceptionPort))
+ if((PreviousMode != KernelMode) &&
+ (CurrentProcess->ExceptionPort))
{
KeRaiseUserException(Status);
}
OUT PHANDLE Handle)
{
POBJECT_CREATE_INFORMATION ObjectCreateInfo;
- POBJECT_HEADER Header;
+ PROS_OBJECT_HEADER Header;
POBJECT_HEADER_NAME_INFO ObjectNameInfo;
PVOID FoundObject = NULL;
- POBJECT_HEADER FoundHeader = NULL;
+ PROS_OBJECT_HEADER FoundHeader = NULL;
NTSTATUS Status = STATUS_SUCCESS;
UNICODE_STRING RemainingPath;
BOOLEAN ObjectAttached = FALSE;
PWSTR BufferPos = RemainingPath.Buffer;
ULONG Delta = 0;
- ObpAddEntryDirectory(FoundObject, Header, NULL);
+ ObpAddEntryDirectory(FoundObject, (PROS_OBJECT_HEADER)Header, NULL);
ObjectAttached = TRUE;
ObjectNameInfo = HEADER_TO_OBJECT_NAME(Header);
{
DPRINT("Calling %x\n", Header->Type->TypeInfo.OpenProcedure);
Status = Header->Type->TypeInfo.OpenProcedure(ObCreateHandle,
- &Header->Body,
NULL,
+ &Header->Body,
0,
0);
}
DPRINT("Create Failed\n");
if (ObjectAttached == TRUE)
{
- ObpRemoveEntryDirectory(Header);
+ ObpRemoveEntryDirectory((PROS_OBJECT_HEADER)Header);
}
if (FoundObject)
{
DPRINT("Creating handle\n");
if (Handle != NULL)
{
- Status = ObpCreateHandle(PsGetCurrentProcess(),
- &Header->Body,
+ Status = ObpCreateHandle(&Header->Body,
DesiredAccess,
- ObjectCreateInfo->Attributes & OBJ_INHERIT,
+ ObjectCreateInfo->Attributes,
Handle);
DPRINT("handle Created: %d. refcount. handlecount %d %d\n",
*Handle, Header->PointerCount, Header->HandleCount);
ULONG
+NTAPI
ObpGetHandleCountByHandleTable(PHANDLE_TABLE HandleTable)
{
return HandleTable->HandleCount;