2006-05-24 22:07:11 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Kernel
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE: ntoskrnl/ob/obhandle.c
|
|
|
|
* PURPOSE: Manages all functions related to the Object Manager handle
|
|
|
|
* implementation, including creating and destroying handles
|
|
|
|
* and/or handle tables, duplicating objects, and setting the
|
|
|
|
* permanent or temporary flags.
|
|
|
|
* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
|
|
|
|
* Eric Kohl
|
|
|
|
* Thomas Weidenmueller (w3seek@reactos.org)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
|
|
|
#include <ntoskrnl.h>
|
|
|
|
#define NDEBUG
|
2007-02-14 17:51:48 +00:00
|
|
|
#include <debug.h>
|
2006-05-24 22:07:11 +00:00
|
|
|
|
|
|
|
PHANDLE_TABLE ObpKernelHandleTable = NULL;
|
2007-02-14 17:51:48 +00:00
|
|
|
ULONG ObpAccessProtectCloseBit = MAXIMUM_ALLOWED;
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2009-08-24 18:19:53 +00:00
|
|
|
#define TAG_OB_HANDLE 'dHbO'
|
2007-01-08 17:56:56 +00:00
|
|
|
|
2006-05-24 22:07:11 +00:00
|
|
|
/* PRIVATE FUNCTIONS *********************************************************/
|
|
|
|
|
2007-01-10 03:35:59 +00:00
|
|
|
PHANDLE_TABLE
|
|
|
|
NTAPI
|
|
|
|
ObReferenceProcessHandleTable(IN PEPROCESS Process)
|
|
|
|
{
|
|
|
|
PHANDLE_TABLE HandleTable = NULL;
|
|
|
|
|
|
|
|
/* Lock the process */
|
|
|
|
if (ExAcquireRundownProtection(&Process->RundownProtect))
|
|
|
|
{
|
|
|
|
/* Get the handle table */
|
|
|
|
HandleTable = Process->ObjectTable;
|
|
|
|
if (!HandleTable)
|
|
|
|
{
|
|
|
|
/* No table, release the lock */
|
|
|
|
ExReleaseRundownProtection(&Process->RundownProtect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the handle table */
|
|
|
|
return HandleTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
ObDereferenceProcessHandleTable(IN PEPROCESS Process)
|
|
|
|
{
|
|
|
|
/* Release the process lock */
|
|
|
|
ExReleaseRundownProtection(&Process->RundownProtect);
|
|
|
|
}
|
|
|
|
|
2009-01-28 10:32:43 +00:00
|
|
|
ULONG
|
|
|
|
NTAPI
|
|
|
|
ObGetProcessHandleCount(IN PEPROCESS Process)
|
|
|
|
{
|
|
|
|
ULONG HandleCount;
|
|
|
|
PHANDLE_TABLE HandleTable;
|
|
|
|
|
|
|
|
ASSERT(Process);
|
|
|
|
|
|
|
|
/* Ensure the handle table doesn't go away while we use it */
|
|
|
|
HandleTable = ObReferenceProcessHandleTable(Process);
|
|
|
|
|
|
|
|
if (HandleTable != NULL)
|
|
|
|
{
|
|
|
|
/* Count the number of handles the process has */
|
|
|
|
HandleCount = HandleTable->HandleCount;
|
|
|
|
|
|
|
|
/* Let the handle table go */
|
|
|
|
ObDereferenceProcessHandleTable(Process);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No handle table, no handles */
|
|
|
|
HandleCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HandleCount;
|
|
|
|
}
|
|
|
|
|
2007-01-10 03:35:59 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
ObpReferenceProcessObjectByHandle(IN HANDLE Handle,
|
|
|
|
IN PEPROCESS Process,
|
|
|
|
IN PHANDLE_TABLE HandleTable,
|
|
|
|
IN KPROCESSOR_MODE AccessMode,
|
|
|
|
OUT PVOID *Object,
|
2007-01-15 18:37:53 +00:00
|
|
|
OUT POBJECT_HANDLE_INFORMATION HandleInformation,
|
|
|
|
OUT PACCESS_MASK AuditMask)
|
2007-01-10 03:35:59 +00:00
|
|
|
{
|
|
|
|
PHANDLE_TABLE_ENTRY HandleEntry;
|
|
|
|
POBJECT_HEADER ObjectHeader;
|
|
|
|
ACCESS_MASK GrantedAccess;
|
|
|
|
ULONG Attributes;
|
2007-01-15 18:37:53 +00:00
|
|
|
PETHREAD Thread = PsGetCurrentThread();
|
2007-01-10 03:35:59 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
/* Assume failure */
|
|
|
|
*Object = NULL;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-09-30 08:32:34 +00:00
|
|
|
/* Check if this is a special handle */
|
|
|
|
if (HandleToLong(Handle) < 0)
|
2007-01-10 03:35:59 +00:00
|
|
|
{
|
2007-09-30 08:32:34 +00:00
|
|
|
/* Check if the caller wants the current process */
|
|
|
|
if (Handle == NtCurrentProcess())
|
|
|
|
{
|
|
|
|
/* Return handle info */
|
|
|
|
HandleInformation->HandleAttributes = 0;
|
|
|
|
HandleInformation->GrantedAccess = Process->GrantedAccess;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-09-30 08:32:34 +00:00
|
|
|
/* No audit mask */
|
|
|
|
*AuditMask = 0;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-09-30 08:32:34 +00:00
|
|
|
/* Reference ourselves */
|
|
|
|
ObjectHeader = OBJECT_TO_OBJECT_HEADER(Process);
|
|
|
|
InterlockedIncrement(&ObjectHeader->PointerCount);
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-09-30 08:32:34 +00:00
|
|
|
/* Return the pointer */
|
|
|
|
*Object = Process;
|
|
|
|
ASSERT(*Object != NULL);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-09-30 08:32:34 +00:00
|
|
|
/* Check if the caller wants the current thread */
|
|
|
|
if (Handle == NtCurrentThread())
|
|
|
|
{
|
|
|
|
/* Return handle information */
|
|
|
|
HandleInformation->HandleAttributes = 0;
|
|
|
|
HandleInformation->GrantedAccess = Thread->GrantedAccess;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-09-30 08:32:34 +00:00
|
|
|
/* Reference ourselves */
|
|
|
|
ObjectHeader = OBJECT_TO_OBJECT_HEADER(Thread);
|
|
|
|
InterlockedExchangeAdd(&ObjectHeader->PointerCount, 1);
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-09-30 08:32:34 +00:00
|
|
|
/* No audit mask */
|
|
|
|
*AuditMask = 0;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-09-30 08:32:34 +00:00
|
|
|
/* Return the pointer */
|
|
|
|
*Object = Thread;
|
|
|
|
ASSERT(*Object != NULL);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-09-30 08:32:34 +00:00
|
|
|
/* This is a kernel handle... do we have access? */
|
|
|
|
if (AccessMode == KernelMode)
|
|
|
|
{
|
|
|
|
/* Use the kernel handle table and get the actual handle value */
|
|
|
|
Handle = ObKernelHandleToHandle(Handle);
|
|
|
|
HandleTable = ObpKernelHandleTable;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This is an illegal attempt to access a kernel handle */
|
|
|
|
return STATUS_INVALID_HANDLE;
|
|
|
|
}
|
2007-01-10 03:35:59 +00:00
|
|
|
}
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-01-10 03:35:59 +00:00
|
|
|
/* Enter a critical region while we touch the handle table */
|
|
|
|
ASSERT(HandleTable != NULL);
|
|
|
|
KeEnterCriticalRegion();
|
|
|
|
|
|
|
|
/* Get the handle entry */
|
|
|
|
HandleEntry = ExMapHandleToPointer(HandleTable, Handle);
|
|
|
|
if (HandleEntry)
|
|
|
|
{
|
|
|
|
/* Get the object header and validate the type*/
|
2007-01-22 08:15:17 +00:00
|
|
|
ObjectHeader = ObpGetHandleObject(HandleEntry);
|
2007-01-10 03:35:59 +00:00
|
|
|
|
|
|
|
/* Get the granted access and validate it */
|
|
|
|
GrantedAccess = HandleEntry->GrantedAccess;
|
|
|
|
|
|
|
|
/* Mask out the internal attributes */
|
2007-01-22 08:15:17 +00:00
|
|
|
Attributes = HandleEntry->ObAttributes & OBJ_HANDLE_ATTRIBUTES;
|
2007-01-10 03:35:59 +00:00
|
|
|
|
|
|
|
/* Fill out the information */
|
|
|
|
HandleInformation->HandleAttributes = Attributes;
|
|
|
|
HandleInformation->GrantedAccess = GrantedAccess;
|
|
|
|
|
2007-01-15 18:37:53 +00:00
|
|
|
/* No audit mask (FIXME!) */
|
|
|
|
*AuditMask = 0;
|
|
|
|
|
2007-01-10 03:35:59 +00:00
|
|
|
/* Return the pointer */
|
|
|
|
*Object = &ObjectHeader->Body;
|
|
|
|
|
|
|
|
/* Add a reference */
|
|
|
|
InterlockedExchangeAdd(&ObjectHeader->PointerCount, 1);
|
|
|
|
|
|
|
|
/* Unlock the handle */
|
|
|
|
ExUnlockHandleTableEntry(HandleTable, HandleEntry);
|
|
|
|
KeLeaveCriticalRegion();
|
|
|
|
|
|
|
|
/* Return success */
|
|
|
|
ASSERT(*Object != NULL);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Invalid handle */
|
|
|
|
Status = STATUS_INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return failure status */
|
|
|
|
KeLeaveCriticalRegion();
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2007-01-08 19:40:10 +00:00
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
ObpEnumFindHandleProcedure(IN PHANDLE_TABLE_ENTRY HandleEntry,
|
|
|
|
IN HANDLE Handle,
|
|
|
|
IN PVOID Context)
|
|
|
|
{
|
2007-01-22 09:57:10 +00:00
|
|
|
POBJECT_HEADER ObjectHeader;
|
|
|
|
ACCESS_MASK GrantedAccess;
|
|
|
|
ULONG HandleAttributes;
|
|
|
|
POBP_FIND_HANDLE_DATA FindData = Context;
|
|
|
|
|
|
|
|
/* Get the object header */
|
|
|
|
ObjectHeader = ObpGetHandleObject(HandleEntry);
|
|
|
|
|
|
|
|
/* Make sure it's valid and matching */
|
|
|
|
if ((FindData->ObjectHeader) && (FindData->ObjectHeader != ObjectHeader))
|
|
|
|
{
|
|
|
|
/* No match, fail */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now attempt to match the object type */
|
|
|
|
if ((FindData->ObjectType) && (FindData->ObjectType != ObjectHeader->Type))
|
|
|
|
{
|
|
|
|
/* No match, fail */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have extra information */
|
|
|
|
if (FindData->HandleInformation)
|
|
|
|
{
|
|
|
|
/* Get the granted access and attributes */
|
|
|
|
GrantedAccess = HandleEntry->GrantedAccess;
|
|
|
|
HandleAttributes = HandleEntry->ObAttributes & OBJ_HANDLE_ATTRIBUTES;
|
|
|
|
|
|
|
|
/* Attempt to match them */
|
|
|
|
if ((FindData->HandleInformation->HandleAttributes != HandleAttributes) ||
|
|
|
|
(FindData->HandleInformation->GrantedAccess != GrantedAccess))
|
|
|
|
{
|
|
|
|
/* No match, fail */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have a match */
|
|
|
|
return TRUE;
|
2007-01-08 19:40:10 +00:00
|
|
|
}
|
|
|
|
|
2007-01-08 17:56:56 +00:00
|
|
|
POBJECT_HANDLE_COUNT_ENTRY
|
|
|
|
NTAPI
|
|
|
|
ObpInsertHandleCount(IN POBJECT_HEADER ObjectHeader)
|
|
|
|
{
|
|
|
|
POBJECT_HEADER_HANDLE_INFO HandleInfo;
|
|
|
|
POBJECT_HANDLE_COUNT_ENTRY FreeEntry;
|
|
|
|
POBJECT_HANDLE_COUNT_DATABASE HandleDatabase, OldHandleDatabase;
|
|
|
|
ULONG i;
|
|
|
|
ULONG Size, OldSize;
|
|
|
|
OBJECT_HANDLE_COUNT_DATABASE SingleDatabase;
|
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Get the handle info */
|
|
|
|
HandleInfo = OBJECT_HEADER_TO_HANDLE_INFO(ObjectHeader);
|
|
|
|
if (!HandleInfo) return NULL;
|
|
|
|
|
|
|
|
/* Check if we only have one entry */
|
|
|
|
if (ObjectHeader->Flags & OB_FLAG_SINGLE_PROCESS)
|
|
|
|
{
|
|
|
|
/* Fill out the single entry */
|
|
|
|
SingleDatabase.CountEntries = 1;
|
|
|
|
SingleDatabase.HandleCountEntries[0] = HandleInfo->SingleEntry;
|
|
|
|
|
|
|
|
/* Use this as the old size */
|
|
|
|
OldHandleDatabase = &SingleDatabase;
|
|
|
|
OldSize = sizeof(SingleDatabase);
|
|
|
|
|
|
|
|
/* Now we'll have two entries, and an entire DB */
|
|
|
|
i = 2;
|
|
|
|
Size = sizeof(OBJECT_HANDLE_COUNT_DATABASE) +
|
2007-02-14 17:51:48 +00:00
|
|
|
((i - 1) * sizeof(OBJECT_HANDLE_COUNT_ENTRY));
|
2007-01-08 17:56:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We already have a DB, get the information from it */
|
|
|
|
OldHandleDatabase = HandleInfo->HandleCountDatabase;
|
|
|
|
i = OldHandleDatabase->CountEntries;
|
|
|
|
OldSize = sizeof(OBJECT_HANDLE_COUNT_DATABASE) +
|
|
|
|
((i - 1) * sizeof(OBJECT_HANDLE_COUNT_ENTRY));
|
|
|
|
|
|
|
|
/* Add 4 more entries */
|
|
|
|
i += 4;
|
2007-08-15 05:30:32 +00:00
|
|
|
Size = OldSize + (4 * sizeof(OBJECT_HANDLE_COUNT_ENTRY));
|
2007-01-08 17:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the DB */
|
|
|
|
HandleDatabase = ExAllocatePoolWithTag(PagedPool, Size, TAG_OB_HANDLE);
|
|
|
|
if (!HandleDatabase) return NULL;
|
|
|
|
|
|
|
|
/* Copy the old database */
|
2007-01-15 18:37:53 +00:00
|
|
|
RtlCopyMemory(HandleDatabase, OldHandleDatabase, OldSize);
|
2007-01-08 17:56:56 +00:00
|
|
|
|
|
|
|
/* Check if we he had a single entry before */
|
|
|
|
if (ObjectHeader->Flags & OB_FLAG_SINGLE_PROCESS)
|
|
|
|
{
|
|
|
|
/* Now we have more */
|
|
|
|
ObjectHeader->Flags &= ~OB_FLAG_SINGLE_PROCESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise we had a DB, free it */
|
2008-08-31 15:29:21 +00:00
|
|
|
ExFreePoolWithTag(OldHandleDatabase, TAG_OB_HANDLE);
|
2007-01-08 17:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the end of the copy and zero out the new data */
|
|
|
|
FreeEntry = (PVOID)((ULONG_PTR)HandleDatabase + OldSize);
|
|
|
|
RtlZeroMemory(FreeEntry, Size - OldSize);
|
|
|
|
|
|
|
|
/* Set the new information and return the free entry */
|
|
|
|
HandleDatabase->CountEntries = i;
|
|
|
|
HandleInfo->HandleCountDatabase = HandleDatabase;
|
|
|
|
return FreeEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
ObpIncrementHandleDataBase(IN POBJECT_HEADER ObjectHeader,
|
|
|
|
IN PEPROCESS Process,
|
|
|
|
IN OUT PULONG NewProcessHandleCount)
|
|
|
|
{
|
|
|
|
POBJECT_HEADER_HANDLE_INFO HandleInfo;
|
|
|
|
POBJECT_HANDLE_COUNT_ENTRY HandleEntry, FreeEntry = NULL;
|
|
|
|
POBJECT_HANDLE_COUNT_DATABASE HandleDatabase;
|
|
|
|
ULONG i;
|
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Get the handle info and check if we only have one entry */
|
|
|
|
HandleInfo = OBJECT_HEADER_TO_HANDLE_INFO(ObjectHeader);
|
|
|
|
if (ObjectHeader->Flags & OB_FLAG_SINGLE_PROCESS)
|
|
|
|
{
|
|
|
|
/* Check if the entry is free */
|
|
|
|
if (!HandleInfo->SingleEntry.HandleCount)
|
|
|
|
{
|
|
|
|
/* Add ours */
|
|
|
|
HandleInfo->SingleEntry.HandleCount = 1;
|
|
|
|
HandleInfo->SingleEntry.Process = Process;
|
|
|
|
|
|
|
|
/* Return success and 1 handle */
|
|
|
|
*NewProcessHandleCount = 1;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
else if (HandleInfo->SingleEntry.Process == Process)
|
|
|
|
{
|
|
|
|
/* Busy entry, but same process */
|
|
|
|
*NewProcessHandleCount = ++HandleInfo->SingleEntry.HandleCount;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Insert a new entry */
|
|
|
|
FreeEntry = ObpInsertHandleCount(ObjectHeader);
|
|
|
|
if (!FreeEntry) return STATUS_INSUFFICIENT_RESOURCES;
|
2007-08-19 05:11:54 +00:00
|
|
|
ASSERT(!FreeEntry->Process);
|
|
|
|
ASSERT(!FreeEntry->HandleCount);
|
2007-01-08 17:56:56 +00:00
|
|
|
|
|
|
|
/* Fill it out */
|
|
|
|
FreeEntry->Process = Process;
|
|
|
|
FreeEntry->HandleCount = 1;
|
|
|
|
|
|
|
|
/* Return success and 1 handle */
|
|
|
|
*NewProcessHandleCount = 1;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have a database instead */
|
|
|
|
HandleDatabase = HandleInfo->HandleCountDatabase;
|
|
|
|
if (HandleDatabase)
|
|
|
|
{
|
|
|
|
/* Get the entries and loop them */
|
|
|
|
i = HandleDatabase->CountEntries;
|
|
|
|
HandleEntry = &HandleDatabase->HandleCountEntries[0];
|
|
|
|
while (i)
|
|
|
|
{
|
|
|
|
/* Check if this is a match */
|
|
|
|
if (HandleEntry->Process == Process)
|
|
|
|
{
|
|
|
|
/* Found it, get the process handle count */
|
|
|
|
*NewProcessHandleCount = ++HandleEntry->HandleCount;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
else if (!HandleEntry->HandleCount)
|
|
|
|
{
|
|
|
|
/* Found a free entry */
|
|
|
|
FreeEntry = HandleEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep looping */
|
|
|
|
HandleEntry++;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we couldn't find a free entry */
|
|
|
|
if (!FreeEntry)
|
|
|
|
{
|
|
|
|
/* Allocate one */
|
|
|
|
FreeEntry = ObpInsertHandleCount(ObjectHeader);
|
|
|
|
if (!FreeEntry) return STATUS_INSUFFICIENT_RESOURCES;
|
2007-08-19 05:11:54 +00:00
|
|
|
ASSERT(!FreeEntry->Process);
|
|
|
|
ASSERT(!FreeEntry->HandleCount);
|
2007-01-08 17:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill out the entry */
|
|
|
|
FreeEntry->Process = Process;
|
|
|
|
FreeEntry->HandleCount = 1;
|
|
|
|
*NewProcessHandleCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return success if we got here */
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-06-08 20:58:21 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
ObpChargeQuotaForObject(IN POBJECT_HEADER ObjectHeader,
|
2007-01-08 08:03:47 +00:00
|
|
|
IN POBJECT_TYPE ObjectType,
|
|
|
|
OUT PBOOLEAN NewObject)
|
2006-06-08 20:58:21 +00:00
|
|
|
{
|
|
|
|
POBJECT_HEADER_QUOTA_INFO ObjectQuota;
|
|
|
|
ULONG PagedPoolCharge, NonPagedPoolCharge;
|
|
|
|
|
|
|
|
/* Get quota information */
|
|
|
|
ObjectQuota = OBJECT_HEADER_TO_QUOTA_INFO(ObjectHeader);
|
2007-01-08 08:03:47 +00:00
|
|
|
*NewObject = FALSE;
|
2006-06-08 20:58:21 +00:00
|
|
|
|
|
|
|
/* Check if this is a new object */
|
|
|
|
if (ObjectHeader->Flags & OB_FLAG_CREATE_INFO)
|
|
|
|
{
|
|
|
|
/* Remove the flag */
|
|
|
|
ObjectHeader->Flags &= ~ OB_FLAG_CREATE_INFO;
|
|
|
|
if (ObjectQuota)
|
|
|
|
{
|
|
|
|
/* We have a quota, get the charges */
|
|
|
|
PagedPoolCharge = ObjectQuota->PagedPoolCharge;
|
|
|
|
NonPagedPoolCharge = ObjectQuota->NonPagedPoolCharge;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Get it from the object type */
|
|
|
|
PagedPoolCharge = ObjectType->TypeInfo.DefaultPagedPoolCharge;
|
|
|
|
NonPagedPoolCharge = ObjectType->TypeInfo.DefaultNonPagedPoolCharge;
|
|
|
|
}
|
|
|
|
|
2007-02-14 17:51:48 +00:00
|
|
|
/* Charge the quota */
|
|
|
|
ObjectHeader->QuotaBlockCharged = (PVOID)1;
|
[NTOS]: Remove useless variables in kernel code that were set, but never actually used (dead code, tests, copy/pasters). If a variable was set but not used because of missing/#if'ed out code, a note was added instead.
[NTOS]: In the process, fix bugs in the Event dispatcher code that used Win32 EVENT_TYPE instead of NT KOBJECTS enumeration.
[NTOS]: Fix a bug in ObpInsertHandleCount, where the object access check was being done with the previous mode, instead of honoring the probe mode, which is defined by OBJ_FORCE_ACCESS_CHECK.
[NTOS]: Fix a bug in a section function which was always returning STATUS_SUCCESS, now it returns the result of the previous Status = function assignment. If this isn't desired, then don't check for the Status anymore.
[NTOS]: Note that MDL code does not support SkipBytes argument. If it is used, MDL could be invalid.
[NTOS]: Add checks for VerifierAllocation and set it when needed (WIP).
[NTOS]: Clarify what _WORKING_LINKER_ is, and the legal risks in continuing to use a linker that builds non-Microsoft drivers when used with headers whose EULA specify that they can only be used for Microsoft drivers.
svn path=/trunk/; revision=48692
2010-09-04 08:17:17 +00:00
|
|
|
DPRINT("FIXME: Should charge: %lx %lx\n", PagedPoolCharge, NonPagedPoolCharge);
|
2007-02-14 17:51:48 +00:00
|
|
|
#if 0
|
|
|
|
PsChargeSharedPoolQuota(PsGetCurrentProcess(),
|
|
|
|
PagedPoolCharge,
|
|
|
|
NonPagedPoolCharge);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Check if we don't have a quota block */
|
|
|
|
if (!ObjectHeader->QuotaBlockCharged) return STATUS_QUOTA_EXCEEDED;
|
|
|
|
|
|
|
|
/* Now set the flag */
|
|
|
|
*NewObject = TRUE;
|
2006-06-08 20:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return success */
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-09-24 17:32:14 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
ObpValidateAccessMask(IN PACCESS_STATE AccessState)
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-06-06 05:49:28 +00:00
|
|
|
/*++
|
|
|
|
* @name ObpDecrementHandleCount
|
|
|
|
*
|
|
|
|
* The ObpDecrementHandleCount routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param ObjectBody
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param Process
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param GrantedAccess
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
2006-05-24 22:07:11 +00:00
|
|
|
NTAPI
|
2006-06-06 05:49:28 +00:00
|
|
|
ObpDecrementHandleCount(IN PVOID ObjectBody,
|
|
|
|
IN PEPROCESS Process,
|
2007-02-14 17:51:48 +00:00
|
|
|
IN ACCESS_MASK GrantedAccess,
|
|
|
|
IN POBJECT_TYPE ObjectType)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-06 05:49:28 +00:00
|
|
|
POBJECT_HEADER ObjectHeader;
|
|
|
|
LONG SystemHandleCount, ProcessHandleCount;
|
2006-10-19 02:54:48 +00:00
|
|
|
LONG NewCount;
|
2007-01-08 08:03:47 +00:00
|
|
|
KIRQL CalloutIrql;
|
2007-01-08 17:56:56 +00:00
|
|
|
POBJECT_HEADER_HANDLE_INFO HandleInfo;
|
|
|
|
POBJECT_HANDLE_COUNT_ENTRY HandleEntry;
|
|
|
|
POBJECT_HANDLE_COUNT_DATABASE HandleDatabase;
|
|
|
|
ULONG i;
|
2007-01-15 18:37:53 +00:00
|
|
|
PAGED_CODE();
|
2006-06-06 05:49:28 +00:00
|
|
|
|
|
|
|
/* Get the object type and header */
|
|
|
|
ObjectHeader = OBJECT_TO_OBJECT_HEADER(ObjectBody);
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
2008-09-12 14:10:27 +00:00
|
|
|
"%s - Decrementing count for: %p. HC PC %lx %lx\n",
|
2006-06-07 05:04:43 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
ObjectBody,
|
|
|
|
ObjectHeader->HandleCount,
|
|
|
|
ObjectHeader->PointerCount);
|
2006-06-06 05:49:28 +00:00
|
|
|
|
2008-04-01 20:07:36 +00:00
|
|
|
/* Lock the object */
|
|
|
|
ObpAcquireObjectLock(ObjectHeader);
|
2006-10-19 02:54:48 +00:00
|
|
|
|
2007-01-08 08:03:47 +00:00
|
|
|
/* Set default counts */
|
2006-06-06 05:49:28 +00:00
|
|
|
SystemHandleCount = ObjectHeader->HandleCount;
|
|
|
|
ProcessHandleCount = 0;
|
|
|
|
|
|
|
|
/* Decrement the handle count */
|
2006-10-19 02:54:48 +00:00
|
|
|
NewCount = InterlockedDecrement(&ObjectHeader->HandleCount);
|
|
|
|
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Check if we're out of handles and this was an exclusive object */
|
|
|
|
if (!(NewCount) && (ObjectHeader->Flags & OB_FLAG_EXCLUSIVE))
|
2006-10-19 02:54:48 +00:00
|
|
|
{
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Clear the exclusive flag */
|
|
|
|
OBJECT_HEADER_TO_QUOTA_INFO(ObjectHeader)->ExclusiveProcess = NULL;
|
2006-10-19 02:54:48 +00:00
|
|
|
}
|
|
|
|
|
2007-01-08 08:03:47 +00:00
|
|
|
/* Is the object type keeping track of handles? */
|
|
|
|
if (ObjectType->TypeInfo.MaintainHandleCount)
|
|
|
|
{
|
2007-01-08 17:56:56 +00:00
|
|
|
/* Get handle information */
|
|
|
|
HandleInfo = OBJECT_HEADER_TO_HANDLE_INFO(ObjectHeader);
|
|
|
|
|
|
|
|
/* Check if there's only a single entry */
|
|
|
|
if (ObjectHeader->Flags & OB_FLAG_SINGLE_PROCESS)
|
|
|
|
{
|
|
|
|
/* It should be us */
|
|
|
|
ASSERT(HandleInfo->SingleEntry.Process == Process);
|
|
|
|
ASSERT(HandleInfo->SingleEntry.HandleCount > 0);
|
|
|
|
|
|
|
|
/* Get the handle counts */
|
|
|
|
ProcessHandleCount = HandleInfo->SingleEntry.HandleCount--;
|
|
|
|
HandleEntry = &HandleInfo->SingleEntry;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, get the database */
|
|
|
|
HandleDatabase = HandleInfo->HandleCountDatabase;
|
|
|
|
if (HandleDatabase)
|
|
|
|
{
|
|
|
|
/* Get the entries and loop them */
|
|
|
|
i = HandleDatabase->CountEntries;
|
|
|
|
HandleEntry = &HandleDatabase->HandleCountEntries[0];
|
|
|
|
while (i)
|
|
|
|
{
|
|
|
|
/* Check if this is a match */
|
|
|
|
if ((HandleEntry->HandleCount) &&
|
|
|
|
(HandleEntry->Process == Process))
|
|
|
|
{
|
|
|
|
/* Found it, get the process handle count */
|
|
|
|
ProcessHandleCount = HandleEntry->HandleCount--;
|
2007-08-19 05:11:54 +00:00
|
|
|
break;
|
2007-01-08 17:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep looping */
|
|
|
|
HandleEntry++;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
2007-02-14 17:51:48 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No database, so no entry */
|
|
|
|
HandleEntry = NULL;
|
|
|
|
}
|
2007-01-08 17:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if this is the last handle */
|
|
|
|
if (ProcessHandleCount == 1)
|
|
|
|
{
|
|
|
|
/* Then clear the entry */
|
|
|
|
HandleEntry->Process = NULL;
|
|
|
|
HandleEntry->HandleCount = 0;
|
|
|
|
}
|
2007-01-08 08:03:47 +00:00
|
|
|
}
|
|
|
|
|
2006-10-19 02:54:48 +00:00
|
|
|
/* Release the lock */
|
2008-04-01 20:07:36 +00:00
|
|
|
ObpReleaseObjectLock(ObjectHeader);
|
2006-06-06 05:49:28 +00:00
|
|
|
|
|
|
|
/* Check if we have a close procedure */
|
|
|
|
if (ObjectType->TypeInfo.CloseProcedure)
|
|
|
|
{
|
|
|
|
/* Call it */
|
2007-01-08 08:03:47 +00:00
|
|
|
ObpCalloutStart(&CalloutIrql);
|
2006-06-06 05:49:28 +00:00
|
|
|
ObjectType->TypeInfo.CloseProcedure(Process,
|
|
|
|
ObjectBody,
|
|
|
|
GrantedAccess,
|
|
|
|
ProcessHandleCount,
|
|
|
|
SystemHandleCount);
|
2007-01-08 08:03:47 +00:00
|
|
|
ObpCalloutEnd(CalloutIrql, "Close", ObjectType, ObjectBody);
|
2006-06-06 05:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we should delete the object */
|
|
|
|
ObpDeleteNameCheck(ObjectBody);
|
|
|
|
|
|
|
|
/* Decrease the total number of handles for this type */
|
2006-10-20 13:19:13 +00:00
|
|
|
InterlockedDecrement((PLONG)&ObjectType->TotalNumberOfHandles);
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
2008-09-12 14:10:27 +00:00
|
|
|
"%s - Decremented count for: %p. HC PC %lx %lx\n",
|
2006-06-07 05:04:43 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
ObjectBody,
|
|
|
|
ObjectHeader->HandleCount,
|
|
|
|
ObjectHeader->PointerCount);
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-06 06:12:09 +00:00
|
|
|
/*++
|
2006-06-08 18:09:01 +00:00
|
|
|
* @name ObpCloseHandleTableEntry
|
2006-06-06 06:12:09 +00:00
|
|
|
*
|
2006-06-08 18:09:01 +00:00
|
|
|
* The ObpCloseHandleTableEntry routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param HandleTable
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param HandleEntry
|
|
|
|
* <FILLMEIN>.
|
2006-06-06 06:12:09 +00:00
|
|
|
*
|
|
|
|
* @param Handle
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
2006-06-08 18:09:01 +00:00
|
|
|
* @param AccessMode
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param IgnoreHandleProtection
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
2006-06-06 06:12:09 +00:00
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2006-06-08 18:09:01 +00:00
|
|
|
ObpCloseHandleTableEntry(IN PHANDLE_TABLE HandleTable,
|
|
|
|
IN PHANDLE_TABLE_ENTRY HandleEntry,
|
|
|
|
IN HANDLE Handle,
|
|
|
|
IN KPROCESSOR_MODE AccessMode,
|
|
|
|
IN BOOLEAN IgnoreHandleProtection)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-06 05:49:28 +00:00
|
|
|
PVOID Body;
|
2006-06-06 06:12:09 +00:00
|
|
|
POBJECT_TYPE ObjectType;
|
2006-06-06 05:49:28 +00:00
|
|
|
POBJECT_HEADER ObjectHeader;
|
|
|
|
ACCESS_MASK GrantedAccess;
|
2007-01-08 08:03:47 +00:00
|
|
|
KIRQL CalloutIrql;
|
2006-06-06 05:49:28 +00:00
|
|
|
PAGED_CODE();
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-08 18:09:01 +00:00
|
|
|
/* Get the object data */
|
2007-01-22 08:15:17 +00:00
|
|
|
ObjectHeader = ObpGetHandleObject(HandleEntry);
|
2006-06-08 18:09:01 +00:00
|
|
|
ObjectType = ObjectHeader->Type;
|
|
|
|
Body = &ObjectHeader->Body;
|
|
|
|
GrantedAccess = HandleEntry->GrantedAccess;
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
2008-09-12 14:10:27 +00:00
|
|
|
"%s - Closing handle: %lx for %p. HC PC %lx %lx\n",
|
2006-06-08 18:09:01 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
Handle,
|
|
|
|
Body,
|
|
|
|
ObjectHeader->HandleCount,
|
|
|
|
ObjectHeader->PointerCount);
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-08 18:09:01 +00:00
|
|
|
/* Check if the object has an Okay To Close procedure */
|
|
|
|
if (ObjectType->TypeInfo.OkayToCloseProcedure)
|
2006-06-06 05:49:28 +00:00
|
|
|
{
|
2006-06-08 18:09:01 +00:00
|
|
|
/* Call it and check if it's not letting us close it */
|
2007-01-08 08:03:47 +00:00
|
|
|
ObpCalloutStart(&CalloutIrql);
|
2006-06-08 18:09:01 +00:00
|
|
|
if (!ObjectType->TypeInfo.OkayToCloseProcedure(PsGetCurrentProcess(),
|
|
|
|
Body,
|
2006-06-11 06:52:56 +00:00
|
|
|
Handle,
|
|
|
|
AccessMode))
|
2006-06-08 18:09:01 +00:00
|
|
|
{
|
|
|
|
/* Fail */
|
2007-01-08 08:03:47 +00:00
|
|
|
ObpCalloutEnd(CalloutIrql, "NtClose", ObjectType, Body);
|
2006-06-08 18:09:01 +00:00
|
|
|
ExUnlockHandleTableEntry(HandleTable, HandleEntry);
|
|
|
|
return STATUS_HANDLE_NOT_CLOSABLE;
|
|
|
|
}
|
2007-01-08 08:03:47 +00:00
|
|
|
|
|
|
|
/* Success, validate callout retrn */
|
|
|
|
ObpCalloutEnd(CalloutIrql, "NtClose", ObjectType, Body);
|
2006-06-08 18:09:01 +00:00
|
|
|
}
|
2006-06-06 06:12:09 +00:00
|
|
|
|
2006-06-08 18:09:01 +00:00
|
|
|
/* The callback allowed us to close it, but does the handle itself? */
|
2007-01-22 08:15:17 +00:00
|
|
|
if ((HandleEntry->ObAttributes & OBJ_PROTECT_CLOSE) &&
|
2006-06-08 18:09:01 +00:00
|
|
|
!(IgnoreHandleProtection))
|
|
|
|
{
|
|
|
|
/* It doesn't, are we from user mode? */
|
|
|
|
if (AccessMode != KernelMode)
|
2006-06-06 05:49:28 +00:00
|
|
|
{
|
2006-06-08 18:09:01 +00:00
|
|
|
/* We are! Unlock the entry */
|
|
|
|
ExUnlockHandleTableEntry(HandleTable, HandleEntry);
|
|
|
|
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Make sure we have a debug port */
|
|
|
|
if (PsGetCurrentProcess()->DebugPort)
|
2006-06-08 18:09:01 +00:00
|
|
|
{
|
|
|
|
/* Raise an exception */
|
|
|
|
return KeRaiseUserException(STATUS_HANDLE_NOT_CLOSABLE);
|
|
|
|
}
|
|
|
|
else
|
2006-06-06 06:12:09 +00:00
|
|
|
{
|
2007-01-08 08:03:47 +00:00
|
|
|
/* Return the error instead */
|
2006-06-06 06:12:09 +00:00
|
|
|
return STATUS_HANDLE_NOT_CLOSABLE;
|
|
|
|
}
|
|
|
|
}
|
2007-01-15 18:37:53 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, bugcheck the OS */
|
2008-08-22 00:26:52 +00:00
|
|
|
KeBugCheckEx(INVALID_KERNEL_HANDLE, (ULONG_PTR)Handle, 0, 0, 0);
|
2007-01-15 18:37:53 +00:00
|
|
|
}
|
2006-06-08 18:09:01 +00:00
|
|
|
}
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-08 18:09:01 +00:00
|
|
|
/* Destroy and unlock the handle entry */
|
2007-01-22 08:15:17 +00:00
|
|
|
ExDestroyHandle(HandleTable, Handle, HandleEntry);
|
2006-06-06 05:49:28 +00:00
|
|
|
|
2006-06-08 18:09:01 +00:00
|
|
|
/* Now decrement the handle count */
|
2007-02-14 17:51:48 +00:00
|
|
|
ObpDecrementHandleCount(Body,
|
|
|
|
PsGetCurrentProcess(),
|
|
|
|
GrantedAccess,
|
|
|
|
ObjectType);
|
2006-06-06 06:12:09 +00:00
|
|
|
|
2006-06-08 18:09:01 +00:00
|
|
|
/* Dereference the object as well */
|
2006-08-06 21:15:55 +00:00
|
|
|
ObDereferenceObject(Body);
|
2006-06-08 18:09:01 +00:00
|
|
|
|
|
|
|
/* Return to caller */
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
2008-09-12 14:10:27 +00:00
|
|
|
"%s - Closed handle: %lx for %p. HC PC %lx %lx\n",
|
2006-06-08 18:09:01 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
Handle,
|
|
|
|
Body,
|
|
|
|
ObjectHeader->HandleCount,
|
|
|
|
ObjectHeader->PointerCount);
|
|
|
|
return STATUS_SUCCESS;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-08 06:31:16 +00:00
|
|
|
/*++
|
|
|
|
* @name ObpIncrementHandleCount
|
|
|
|
*
|
|
|
|
* The ObpIncrementHandleCount routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param Object
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param AccessState
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param AccessMode
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param HandleAttributes
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param Process
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param OpenReason
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
2006-06-07 23:59:15 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
ObpIncrementHandleCount(IN PVOID Object,
|
2008-11-26 18:56:41 +00:00
|
|
|
IN PACCESS_STATE AccessState OPTIONAL,
|
2006-06-07 23:59:15 +00:00
|
|
|
IN KPROCESSOR_MODE AccessMode,
|
|
|
|
IN ULONG HandleAttributes,
|
|
|
|
IN PEPROCESS Process,
|
|
|
|
IN OB_OPEN_REASON OpenReason)
|
|
|
|
{
|
|
|
|
POBJECT_HEADER ObjectHeader;
|
|
|
|
POBJECT_TYPE ObjectType;
|
|
|
|
ULONG ProcessHandleCount;
|
2006-06-08 20:58:21 +00:00
|
|
|
NTSTATUS Status;
|
2007-01-08 08:03:47 +00:00
|
|
|
PEPROCESS ExclusiveProcess;
|
|
|
|
BOOLEAN Exclusive = FALSE, NewObject;
|
|
|
|
POBJECT_HEADER_CREATOR_INFO CreatorInfo;
|
|
|
|
KIRQL CalloutIrql;
|
2007-01-15 18:37:53 +00:00
|
|
|
KPROCESSOR_MODE ProbeMode;
|
|
|
|
ULONG Total;
|
2007-02-14 17:51:48 +00:00
|
|
|
PAGED_CODE();
|
2006-06-07 23:59:15 +00:00
|
|
|
|
|
|
|
/* Get the object header and type */
|
|
|
|
ObjectHeader = OBJECT_TO_OBJECT_HEADER(Object);
|
|
|
|
ObjectType = ObjectHeader->Type;
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
2008-09-12 14:10:27 +00:00
|
|
|
"%s - Incrementing count for: %p. Reason: %lx. HC PC %lx %lx\n",
|
2006-06-07 23:59:15 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
Object,
|
|
|
|
OpenReason,
|
|
|
|
ObjectHeader->HandleCount,
|
|
|
|
ObjectHeader->PointerCount);
|
|
|
|
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Check if caller is forcing user mode */
|
|
|
|
if (HandleAttributes & OBJ_FORCE_ACCESS_CHECK)
|
|
|
|
{
|
|
|
|
/* Force it */
|
|
|
|
ProbeMode = UserMode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Keep original setting */
|
|
|
|
ProbeMode = AccessMode;
|
|
|
|
}
|
|
|
|
|
2008-04-01 20:07:36 +00:00
|
|
|
/* Lock the object */
|
|
|
|
ObpAcquireObjectLock(ObjectHeader);
|
2006-10-19 02:54:48 +00:00
|
|
|
|
2006-06-08 20:58:21 +00:00
|
|
|
/* Charge quota and remove the creator info flag */
|
2007-01-08 08:03:47 +00:00
|
|
|
Status = ObpChargeQuotaForObject(ObjectHeader, ObjectType, &NewObject);
|
2006-06-08 20:58:21 +00:00
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
|
|
|
|
2007-01-08 08:03:47 +00:00
|
|
|
/* Check if the open is exclusive */
|
|
|
|
if (HandleAttributes & OBJ_EXCLUSIVE)
|
|
|
|
{
|
|
|
|
/* Check if the object allows this, or if the inherit flag was given */
|
|
|
|
if ((HandleAttributes & OBJ_INHERIT) ||
|
|
|
|
!(ObjectHeader->Flags & OB_FLAG_EXCLUSIVE))
|
|
|
|
{
|
|
|
|
/* Incorrect attempt */
|
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
goto Quickie;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have access to it */
|
|
|
|
ExclusiveProcess = OBJECT_HEADER_TO_EXCLUSIVE_PROCESS(ObjectHeader);
|
|
|
|
if ((!(ExclusiveProcess) && (ObjectHeader->HandleCount)) ||
|
|
|
|
((ExclusiveProcess) && (ExclusiveProcess != PsGetCurrentProcess())))
|
|
|
|
{
|
|
|
|
/* This isn't the right process */
|
|
|
|
Status = STATUS_ACCESS_DENIED;
|
|
|
|
goto Quickie;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now you got exclusive access */
|
|
|
|
Exclusive = TRUE;
|
|
|
|
}
|
|
|
|
else if ((ObjectHeader->Flags & OB_FLAG_EXCLUSIVE) &&
|
|
|
|
(OBJECT_HEADER_TO_EXCLUSIVE_PROCESS(ObjectHeader)))
|
|
|
|
{
|
|
|
|
/* Caller didn't want exclusive access, but the object is exclusive */
|
|
|
|
Status = STATUS_ACCESS_DENIED;
|
|
|
|
goto Quickie;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if this is an object that went from 0 handles back to existence,
|
|
|
|
* but doesn't have an open procedure, only a close procedure. This means
|
|
|
|
* that it will never realize that the object is back alive, so we must
|
|
|
|
* fail the request.
|
|
|
|
*/
|
|
|
|
if (!(ObjectHeader->HandleCount) &&
|
|
|
|
!(NewObject) &&
|
|
|
|
(ObjectType->TypeInfo.MaintainHandleCount) &&
|
|
|
|
!(ObjectType->TypeInfo.OpenProcedure) &&
|
|
|
|
(ObjectType->TypeInfo.CloseProcedure))
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
Status = STATUS_UNSUCCESSFUL;
|
|
|
|
goto Quickie;
|
|
|
|
}
|
|
|
|
|
2006-06-07 23:59:15 +00:00
|
|
|
/* Check if we're opening an existing handle */
|
2007-01-15 18:37:53 +00:00
|
|
|
if ((OpenReason == ObOpenHandle) ||
|
|
|
|
((OpenReason == ObDuplicateHandle) && (AccessState)))
|
2006-06-07 23:59:15 +00:00
|
|
|
{
|
2006-06-29 05:05:27 +00:00
|
|
|
/* Validate the caller's access to this object */
|
2006-06-07 23:59:15 +00:00
|
|
|
if (!ObCheckObjectAccess(Object,
|
|
|
|
AccessState,
|
|
|
|
TRUE,
|
[NTOS]: Remove useless variables in kernel code that were set, but never actually used (dead code, tests, copy/pasters). If a variable was set but not used because of missing/#if'ed out code, a note was added instead.
[NTOS]: In the process, fix bugs in the Event dispatcher code that used Win32 EVENT_TYPE instead of NT KOBJECTS enumeration.
[NTOS]: Fix a bug in ObpInsertHandleCount, where the object access check was being done with the previous mode, instead of honoring the probe mode, which is defined by OBJ_FORCE_ACCESS_CHECK.
[NTOS]: Fix a bug in a section function which was always returning STATUS_SUCCESS, now it returns the result of the previous Status = function assignment. If this isn't desired, then don't check for the Status anymore.
[NTOS]: Note that MDL code does not support SkipBytes argument. If it is used, MDL could be invalid.
[NTOS]: Add checks for VerifierAllocation and set it when needed (WIP).
[NTOS]: Clarify what _WORKING_LINKER_ is, and the legal risks in continuing to use a linker that builds non-Microsoft drivers when used with headers whose EULA specify that they can only be used for Microsoft drivers.
svn path=/trunk/; revision=48692
2010-09-04 08:17:17 +00:00
|
|
|
ProbeMode,
|
2006-06-07 23:59:15 +00:00
|
|
|
&Status))
|
|
|
|
{
|
2006-06-29 05:05:27 +00:00
|
|
|
/* Access was denied, so fail */
|
2007-01-08 08:03:47 +00:00
|
|
|
goto Quickie;
|
2006-06-07 23:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (OpenReason == ObCreateHandle)
|
|
|
|
{
|
|
|
|
/* Convert MAXIMUM_ALLOWED to GENERIC_ALL */
|
|
|
|
if (AccessState->RemainingDesiredAccess & MAXIMUM_ALLOWED)
|
|
|
|
{
|
|
|
|
/* Mask out MAXIMUM_ALLOWED and stick GENERIC_ALL instead */
|
|
|
|
AccessState->RemainingDesiredAccess &= ~MAXIMUM_ALLOWED;
|
|
|
|
AccessState->RemainingDesiredAccess |= GENERIC_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have to map the GENERIC mask */
|
|
|
|
if (AccessState->RemainingDesiredAccess & GENERIC_ACCESS)
|
|
|
|
{
|
|
|
|
/* Map it to the correct access masks */
|
|
|
|
RtlMapGenericMask(&AccessState->RemainingDesiredAccess,
|
|
|
|
&ObjectType->TypeInfo.GenericMapping);
|
|
|
|
}
|
2007-01-08 08:03:47 +00:00
|
|
|
|
|
|
|
/* Check if the caller is trying to access system security */
|
|
|
|
if (AccessState->RemainingDesiredAccess & ACCESS_SYSTEM_SECURITY)
|
|
|
|
{
|
|
|
|
/* FIXME: TODO */
|
|
|
|
DPRINT1("ACCESS_SYSTEM_SECURITY not validated!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if this is an exclusive handle */
|
|
|
|
if (Exclusive)
|
|
|
|
{
|
|
|
|
/* Save the owner process */
|
|
|
|
OBJECT_HEADER_TO_QUOTA_INFO(ObjectHeader)->ExclusiveProcess = Process;
|
2006-06-07 23:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Increase the handle count */
|
2006-06-14 04:02:02 +00:00
|
|
|
InterlockedIncrement(&ObjectHeader->HandleCount);
|
2007-01-08 08:03:47 +00:00
|
|
|
ProcessHandleCount = 0;
|
|
|
|
|
|
|
|
/* Check if we have a handle database */
|
|
|
|
if (ObjectType->TypeInfo.MaintainHandleCount)
|
|
|
|
{
|
2007-01-08 17:56:56 +00:00
|
|
|
/* Increment the handle database */
|
|
|
|
Status = ObpIncrementHandleDataBase(ObjectHeader,
|
|
|
|
Process,
|
|
|
|
&ProcessHandleCount);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* FIXME: This should never happen for now */
|
|
|
|
DPRINT1("Unhandled case\n");
|
2008-08-24 15:48:05 +00:00
|
|
|
ASSERT(FALSE);
|
2007-01-08 17:56:56 +00:00
|
|
|
goto Quickie;
|
|
|
|
}
|
2007-01-08 08:03:47 +00:00
|
|
|
}
|
2006-06-07 23:59:15 +00:00
|
|
|
|
2006-10-19 02:54:48 +00:00
|
|
|
/* Release the lock */
|
2008-04-01 20:07:36 +00:00
|
|
|
ObpReleaseObjectLock(ObjectHeader);
|
2006-10-19 02:54:48 +00:00
|
|
|
|
2006-06-07 23:59:15 +00:00
|
|
|
/* Check if we have an open procedure */
|
2007-01-08 08:03:47 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
2006-06-07 23:59:15 +00:00
|
|
|
if (ObjectType->TypeInfo.OpenProcedure)
|
|
|
|
{
|
|
|
|
/* Call it */
|
2007-01-08 08:03:47 +00:00
|
|
|
ObpCalloutStart(&CalloutIrql);
|
|
|
|
Status = ObjectType->TypeInfo.OpenProcedure(OpenReason,
|
|
|
|
Process,
|
|
|
|
Object,
|
2007-01-15 18:37:53 +00:00
|
|
|
AccessState ?
|
|
|
|
AccessState->
|
|
|
|
PreviouslyGrantedAccess :
|
|
|
|
0,
|
2007-01-08 08:03:47 +00:00
|
|
|
ProcessHandleCount);
|
|
|
|
ObpCalloutEnd(CalloutIrql, "Open", ObjectType, Object);
|
|
|
|
|
|
|
|
/* Check if the open procedure failed */
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* FIXME: This should never happen for now */
|
|
|
|
DPRINT1("Unhandled case\n");
|
2008-08-24 15:48:05 +00:00
|
|
|
ASSERT(FALSE);
|
2007-01-08 08:03:47 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Check if this is a create operation */
|
|
|
|
if (OpenReason == ObCreateHandle)
|
2007-01-08 08:03:47 +00:00
|
|
|
{
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Check if we have creator info */
|
|
|
|
CreatorInfo = OBJECT_HEADER_TO_CREATOR_INFO(ObjectHeader);
|
|
|
|
if (CreatorInfo)
|
|
|
|
{
|
|
|
|
/* We do, acquire the lock */
|
|
|
|
ObpEnterObjectTypeMutex(ObjectType);
|
2007-01-08 08:03:47 +00:00
|
|
|
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Insert us on the list */
|
|
|
|
InsertTailList(&ObjectType->TypeList, &CreatorInfo->TypeList);
|
2007-01-08 08:03:47 +00:00
|
|
|
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Release the lock */
|
|
|
|
ObpLeaveObjectTypeMutex(ObjectType);
|
|
|
|
}
|
2006-06-07 23:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Increase total number of handles */
|
2007-01-15 18:37:53 +00:00
|
|
|
Total = InterlockedIncrement((PLONG)&ObjectType->TotalNumberOfHandles);
|
|
|
|
if (Total > ObjectType->HighWaterNumberOfHandles)
|
2007-01-08 08:03:47 +00:00
|
|
|
{
|
|
|
|
/* Fixup count */
|
2007-01-15 18:37:53 +00:00
|
|
|
ObjectType->HighWaterNumberOfHandles = Total;
|
2007-01-08 08:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Trace call and return */
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
2008-09-12 14:10:27 +00:00
|
|
|
"%s - Incremented count for: %p. Reason: %lx HC PC %lx %lx\n",
|
2006-06-07 23:59:15 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
Object,
|
|
|
|
OpenReason,
|
|
|
|
ObjectHeader->HandleCount,
|
|
|
|
ObjectHeader->PointerCount);
|
2007-01-08 08:03:47 +00:00
|
|
|
return Status;
|
|
|
|
|
|
|
|
Quickie:
|
|
|
|
/* Release lock and return */
|
2008-04-01 20:07:36 +00:00
|
|
|
ObpReleaseObjectLock(ObjectHeader);
|
2007-01-08 08:03:47 +00:00
|
|
|
return Status;
|
2006-06-07 23:59:15 +00:00
|
|
|
}
|
|
|
|
|
2006-06-10 00:41:16 +00:00
|
|
|
/*++
|
|
|
|
* @name ObpIncrementUnnamedHandleCount
|
|
|
|
*
|
|
|
|
* The ObpIncrementUnnamedHandleCount routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param Object
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param AccessState
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param AccessMode
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param HandleAttributes
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param Process
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param OpenReason
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
ObpIncrementUnnamedHandleCount(IN PVOID Object,
|
|
|
|
IN PACCESS_MASK DesiredAccess,
|
|
|
|
IN KPROCESSOR_MODE AccessMode,
|
|
|
|
IN ULONG HandleAttributes,
|
|
|
|
IN PEPROCESS Process)
|
|
|
|
{
|
|
|
|
POBJECT_HEADER ObjectHeader;
|
|
|
|
POBJECT_TYPE ObjectType;
|
|
|
|
ULONG ProcessHandleCount;
|
|
|
|
NTSTATUS Status;
|
2007-01-08 08:03:47 +00:00
|
|
|
PEPROCESS ExclusiveProcess;
|
|
|
|
BOOLEAN Exclusive = FALSE, NewObject;
|
|
|
|
POBJECT_HEADER_CREATOR_INFO CreatorInfo;
|
|
|
|
KIRQL CalloutIrql;
|
2007-01-15 18:37:53 +00:00
|
|
|
ULONG Total;
|
2006-06-10 00:41:16 +00:00
|
|
|
|
|
|
|
/* Get the object header and type */
|
|
|
|
ObjectHeader = OBJECT_TO_OBJECT_HEADER(Object);
|
|
|
|
ObjectType = ObjectHeader->Type;
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
2008-09-12 14:10:27 +00:00
|
|
|
"%s - Incrementing count for: %p. UNNAMED. HC PC %lx %lx\n",
|
2006-06-10 00:41:16 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
Object,
|
|
|
|
ObjectHeader->HandleCount,
|
|
|
|
ObjectHeader->PointerCount);
|
|
|
|
|
2008-05-14 20:59:43 +00:00
|
|
|
/* Lock the object */
|
2008-04-01 20:07:36 +00:00
|
|
|
ObpAcquireObjectLock(ObjectHeader);
|
2006-10-19 02:54:48 +00:00
|
|
|
|
2006-06-10 00:41:16 +00:00
|
|
|
/* Charge quota and remove the creator info flag */
|
2007-01-08 08:03:47 +00:00
|
|
|
Status = ObpChargeQuotaForObject(ObjectHeader, ObjectType, &NewObject);
|
2006-06-10 00:41:16 +00:00
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
|
|
|
|
2007-01-08 08:03:47 +00:00
|
|
|
/* Check if the open is exclusive */
|
|
|
|
if (HandleAttributes & OBJ_EXCLUSIVE)
|
|
|
|
{
|
|
|
|
/* Check if the object allows this, or if the inherit flag was given */
|
|
|
|
if ((HandleAttributes & OBJ_INHERIT) ||
|
|
|
|
!(ObjectHeader->Flags & OB_FLAG_EXCLUSIVE))
|
|
|
|
{
|
|
|
|
/* Incorrect attempt */
|
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
goto Quickie;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have access to it */
|
|
|
|
ExclusiveProcess = OBJECT_HEADER_TO_EXCLUSIVE_PROCESS(ObjectHeader);
|
|
|
|
if ((!(ExclusiveProcess) && (ObjectHeader->HandleCount)) ||
|
|
|
|
((ExclusiveProcess) && (ExclusiveProcess != PsGetCurrentProcess())))
|
|
|
|
{
|
|
|
|
/* This isn't the right process */
|
|
|
|
Status = STATUS_ACCESS_DENIED;
|
|
|
|
goto Quickie;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now you got exclusive access */
|
|
|
|
Exclusive = TRUE;
|
|
|
|
}
|
|
|
|
else if ((ObjectHeader->Flags & OB_FLAG_EXCLUSIVE) &&
|
|
|
|
(OBJECT_HEADER_TO_EXCLUSIVE_PROCESS(ObjectHeader)))
|
|
|
|
{
|
|
|
|
/* Caller didn't want exclusive access, but the object is exclusive */
|
|
|
|
Status = STATUS_ACCESS_DENIED;
|
|
|
|
goto Quickie;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if this is an object that went from 0 handles back to existence,
|
|
|
|
* but doesn't have an open procedure, only a close procedure. This means
|
|
|
|
* that it will never realize that the object is back alive, so we must
|
|
|
|
* fail the request.
|
|
|
|
*/
|
|
|
|
if (!(ObjectHeader->HandleCount) &&
|
|
|
|
!(NewObject) &&
|
|
|
|
(ObjectType->TypeInfo.MaintainHandleCount) &&
|
|
|
|
!(ObjectType->TypeInfo.OpenProcedure) &&
|
|
|
|
(ObjectType->TypeInfo.CloseProcedure))
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
Status = STATUS_UNSUCCESSFUL;
|
|
|
|
goto Quickie;
|
|
|
|
}
|
|
|
|
|
2006-06-10 00:41:16 +00:00
|
|
|
/* Convert MAXIMUM_ALLOWED to GENERIC_ALL */
|
|
|
|
if (*DesiredAccess & MAXIMUM_ALLOWED)
|
|
|
|
{
|
|
|
|
/* Mask out MAXIMUM_ALLOWED and stick GENERIC_ALL instead */
|
|
|
|
*DesiredAccess &= ~MAXIMUM_ALLOWED;
|
|
|
|
*DesiredAccess |= GENERIC_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have to map the GENERIC mask */
|
|
|
|
if (*DesiredAccess & GENERIC_ACCESS)
|
|
|
|
{
|
|
|
|
/* Map it to the correct access masks */
|
|
|
|
RtlMapGenericMask(DesiredAccess,
|
|
|
|
&ObjectType->TypeInfo.GenericMapping);
|
|
|
|
}
|
|
|
|
|
2007-01-08 08:03:47 +00:00
|
|
|
/* Check if this is an exclusive handle */
|
|
|
|
if (Exclusive)
|
|
|
|
{
|
|
|
|
/* Save the owner process */
|
|
|
|
OBJECT_HEADER_TO_QUOTA_INFO(ObjectHeader)->ExclusiveProcess = Process;
|
|
|
|
}
|
|
|
|
|
2006-06-10 00:41:16 +00:00
|
|
|
/* Increase the handle count */
|
2006-06-14 04:02:02 +00:00
|
|
|
InterlockedIncrement(&ObjectHeader->HandleCount);
|
2007-01-08 08:03:47 +00:00
|
|
|
ProcessHandleCount = 0;
|
2006-06-10 00:41:16 +00:00
|
|
|
|
2007-01-08 08:03:47 +00:00
|
|
|
/* Check if we have a handle database */
|
|
|
|
if (ObjectType->TypeInfo.MaintainHandleCount)
|
|
|
|
{
|
2007-01-08 17:56:56 +00:00
|
|
|
/* Increment the handle database */
|
|
|
|
Status = ObpIncrementHandleDataBase(ObjectHeader,
|
|
|
|
Process,
|
|
|
|
&ProcessHandleCount);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* FIXME: This should never happen for now */
|
|
|
|
DPRINT1("Unhandled case\n");
|
2008-08-24 15:48:05 +00:00
|
|
|
ASSERT(FALSE);
|
2007-01-08 17:56:56 +00:00
|
|
|
goto Quickie;
|
|
|
|
}
|
2007-01-08 08:03:47 +00:00
|
|
|
}
|
2006-10-19 02:54:48 +00:00
|
|
|
|
2007-01-08 08:03:47 +00:00
|
|
|
/* Release the lock */
|
2008-04-01 20:07:36 +00:00
|
|
|
ObpReleaseObjectLock(ObjectHeader);
|
2006-06-10 00:41:16 +00:00
|
|
|
|
|
|
|
/* Check if we have an open procedure */
|
2007-01-08 08:03:47 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
2006-06-10 00:41:16 +00:00
|
|
|
if (ObjectType->TypeInfo.OpenProcedure)
|
|
|
|
{
|
|
|
|
/* Call it */
|
2007-01-08 08:03:47 +00:00
|
|
|
ObpCalloutStart(&CalloutIrql);
|
|
|
|
Status = ObjectType->TypeInfo.OpenProcedure(ObCreateHandle,
|
|
|
|
Process,
|
|
|
|
Object,
|
|
|
|
*DesiredAccess,
|
|
|
|
ProcessHandleCount);
|
|
|
|
ObpCalloutEnd(CalloutIrql, "Open", ObjectType, Object);
|
|
|
|
|
|
|
|
/* Check if the open procedure failed */
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* FIXME: This should never happen for now */
|
|
|
|
DPRINT1("Unhandled case\n");
|
2008-08-24 15:48:05 +00:00
|
|
|
ASSERT(FALSE);
|
2007-01-08 08:03:47 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have creator info */
|
|
|
|
CreatorInfo = OBJECT_HEADER_TO_CREATOR_INFO(ObjectHeader);
|
|
|
|
if (CreatorInfo)
|
|
|
|
{
|
|
|
|
/* We do, acquire the lock */
|
|
|
|
ObpEnterObjectTypeMutex(ObjectType);
|
|
|
|
|
|
|
|
/* Insert us on the list */
|
|
|
|
InsertTailList(&ObjectType->TypeList, &CreatorInfo->TypeList);
|
|
|
|
|
|
|
|
/* Release the lock */
|
|
|
|
ObpLeaveObjectTypeMutex(ObjectType);
|
2006-06-10 00:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Increase total number of handles */
|
2007-01-15 18:37:53 +00:00
|
|
|
Total = InterlockedIncrement((PLONG)&ObjectType->TotalNumberOfHandles);
|
|
|
|
if (Total > ObjectType->HighWaterNumberOfHandles)
|
2007-01-08 08:03:47 +00:00
|
|
|
{
|
|
|
|
/* Fixup count */
|
2007-01-15 18:37:53 +00:00
|
|
|
ObjectType->HighWaterNumberOfHandles = Total;
|
2007-01-08 08:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Trace call and return */
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
2008-09-12 14:10:27 +00:00
|
|
|
"%s - Incremented count for: %p. UNNAMED HC PC %lx %lx\n",
|
2006-06-10 00:41:16 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
Object,
|
|
|
|
ObjectHeader->HandleCount,
|
|
|
|
ObjectHeader->PointerCount);
|
2007-01-08 08:03:47 +00:00
|
|
|
return Status;
|
|
|
|
|
|
|
|
Quickie:
|
|
|
|
/* Release lock and return */
|
2008-04-01 20:07:36 +00:00
|
|
|
ObpReleaseObjectLock(ObjectHeader);
|
2007-01-08 08:03:47 +00:00
|
|
|
return Status;
|
2006-06-10 00:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name ObpCreateUnnamedHandle
|
|
|
|
*
|
|
|
|
* The ObpCreateUnnamedHandle routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param Object
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param DesiredAccess
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param AdditionalReferences
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param HandleAttributes
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param AccessMode
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param ReturnedObject
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param ReturnedHandle
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
ObpCreateUnnamedHandle(IN PVOID Object,
|
|
|
|
IN ACCESS_MASK DesiredAccess,
|
|
|
|
IN ULONG AdditionalReferences,
|
|
|
|
IN ULONG HandleAttributes,
|
|
|
|
IN KPROCESSOR_MODE AccessMode,
|
|
|
|
OUT PVOID *ReturnedObject,
|
|
|
|
OUT PHANDLE ReturnedHandle)
|
|
|
|
{
|
|
|
|
HANDLE_TABLE_ENTRY NewEntry;
|
|
|
|
POBJECT_HEADER ObjectHeader;
|
|
|
|
HANDLE Handle;
|
|
|
|
KAPC_STATE ApcState;
|
2007-01-09 17:18:22 +00:00
|
|
|
BOOLEAN AttachedToProcess = FALSE, KernelHandle = FALSE;
|
2006-06-10 00:41:16 +00:00
|
|
|
PVOID HandleTable;
|
|
|
|
NTSTATUS Status;
|
2007-01-09 17:18:22 +00:00
|
|
|
ACCESS_MASK GrantedAccess;
|
|
|
|
POBJECT_TYPE ObjectType;
|
2006-06-10 00:41:16 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Get the object header and type */
|
|
|
|
ObjectHeader = OBJECT_TO_OBJECT_HEADER(Object);
|
2007-01-09 17:18:22 +00:00
|
|
|
ObjectType = ObjectHeader->Type;
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
2008-09-12 14:10:27 +00:00
|
|
|
"%s - Creating handle for: %p. UNNAMED. HC PC %lx %lx\n",
|
2006-06-10 00:41:16 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
Object,
|
|
|
|
ObjectHeader->HandleCount,
|
|
|
|
ObjectHeader->PointerCount);
|
|
|
|
|
2007-08-11 09:04:17 +00:00
|
|
|
/* Save the object header */
|
|
|
|
NewEntry.Object = ObjectHeader;
|
|
|
|
|
|
|
|
/* Mask out the internal attributes */
|
|
|
|
NewEntry.ObAttributes |= HandleAttributes & OBJ_HANDLE_ATTRIBUTES;
|
|
|
|
|
2006-06-10 00:41:16 +00:00
|
|
|
/* Check if this is a kernel handle */
|
2007-01-15 18:37:53 +00:00
|
|
|
if (HandleAttributes & OBJ_KERNEL_HANDLE)
|
2006-06-10 00:41:16 +00:00
|
|
|
{
|
|
|
|
/* Set the handle table */
|
|
|
|
HandleTable = ObpKernelHandleTable;
|
2007-01-09 17:18:22 +00:00
|
|
|
KernelHandle = TRUE;
|
2006-06-10 00:41:16 +00:00
|
|
|
|
|
|
|
/* Check if we're not in the system process */
|
|
|
|
if (PsGetCurrentProcess() != PsInitialSystemProcess)
|
|
|
|
{
|
|
|
|
/* Attach to the system process */
|
|
|
|
KeStackAttachProcess(&PsInitialSystemProcess->Pcb, &ApcState);
|
|
|
|
AttachedToProcess = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Get the current handle table */
|
|
|
|
HandleTable = PsGetCurrentProcess()->ObjectTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the handle count */
|
|
|
|
Status = ObpIncrementUnnamedHandleCount(Object,
|
|
|
|
&DesiredAccess,
|
|
|
|
AccessMode,
|
|
|
|
HandleAttributes,
|
|
|
|
PsGetCurrentProcess());
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We failed (meaning security failure, according to NT Internals)
|
|
|
|
* detach and return
|
|
|
|
*/
|
|
|
|
if (AttachedToProcess) KeUnstackDetachProcess(&ApcState);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2007-01-09 17:18:22 +00:00
|
|
|
/* Remove what's not in the valid access mask */
|
|
|
|
GrantedAccess = DesiredAccess & (ObjectType->TypeInfo.ValidAccessMask |
|
|
|
|
ACCESS_SYSTEM_SECURITY);
|
2006-06-10 00:41:16 +00:00
|
|
|
|
2006-06-27 01:16:17 +00:00
|
|
|
/* Handle extra references */
|
|
|
|
if (AdditionalReferences)
|
|
|
|
{
|
2007-01-09 17:18:22 +00:00
|
|
|
/* Add them to the header */
|
|
|
|
InterlockedExchangeAdd(&ObjectHeader->PointerCount,
|
|
|
|
AdditionalReferences);
|
2006-06-27 01:16:17 +00:00
|
|
|
}
|
|
|
|
|
2007-01-09 17:18:22 +00:00
|
|
|
/* Save the access mask */
|
|
|
|
NewEntry.GrantedAccess = GrantedAccess;
|
|
|
|
|
2006-06-10 00:41:16 +00:00
|
|
|
/*
|
|
|
|
* Create the actual handle. We'll need to do this *after* calling
|
|
|
|
* ObpIncrementHandleCount to make sure that Object Security is valid
|
|
|
|
* (specified in Gl00my documentation on Ob)
|
|
|
|
*/
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
|
|
|
"%s - Handle Properties: [%p-%lx-%lx]\n",
|
2006-06-10 00:41:16 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
NewEntry.Object, NewEntry.ObAttributes & 3, NewEntry.GrantedAccess);
|
|
|
|
Handle = ExCreateHandle(HandleTable, &NewEntry);
|
|
|
|
|
|
|
|
/* Make sure we got a handle */
|
|
|
|
if (Handle)
|
|
|
|
{
|
|
|
|
/* Check if this was a kernel handle */
|
2007-01-09 17:18:22 +00:00
|
|
|
if (KernelHandle) Handle = ObMarkHandleAsKernelHandle(Handle);
|
2006-06-10 00:41:16 +00:00
|
|
|
|
|
|
|
/* Return handle and object */
|
|
|
|
*ReturnedHandle = Handle;
|
2007-01-09 17:18:22 +00:00
|
|
|
|
|
|
|
/* Return the new object only if caller wanted it biased */
|
|
|
|
if ((AdditionalReferences) && (ReturnedObject))
|
|
|
|
{
|
|
|
|
/* Return it */
|
|
|
|
*ReturnedObject = Object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Detach if needed */
|
|
|
|
if (AttachedToProcess) KeUnstackDetachProcess(&ApcState);
|
|
|
|
|
|
|
|
/* Trace and return */
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
2008-09-12 14:10:27 +00:00
|
|
|
"%s - Returning Handle: %lx HC PC %lx %lx\n",
|
2006-06-10 00:41:16 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
Handle,
|
|
|
|
ObjectHeader->HandleCount,
|
|
|
|
ObjectHeader->PointerCount);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-06-27 01:16:17 +00:00
|
|
|
/* Handle extra references */
|
2007-01-15 18:37:53 +00:00
|
|
|
if (AdditionalReferences)
|
2006-06-27 01:16:17 +00:00
|
|
|
{
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Dereference it as many times as required */
|
2007-01-09 17:18:22 +00:00
|
|
|
InterlockedExchangeAdd(&ObjectHeader->PointerCount,
|
2007-08-04 08:49:47 +00:00
|
|
|
-(LONG)AdditionalReferences);
|
2006-06-27 01:16:17 +00:00
|
|
|
}
|
|
|
|
|
2006-06-10 00:41:16 +00:00
|
|
|
/* Decrement the handle count and detach */
|
|
|
|
ObpDecrementHandleCount(&ObjectHeader->Body,
|
|
|
|
PsGetCurrentProcess(),
|
2007-02-14 17:51:48 +00:00
|
|
|
GrantedAccess,
|
|
|
|
ObjectType);
|
2007-01-09 17:18:22 +00:00
|
|
|
|
|
|
|
/* Detach and fail */
|
|
|
|
if (AttachedToProcess) KeUnstackDetachProcess(&ApcState);
|
2006-06-10 00:41:16 +00:00
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2006-06-08 06:31:16 +00:00
|
|
|
/*++
|
|
|
|
* @name ObpCreateHandle
|
|
|
|
*
|
|
|
|
* The ObpCreateHandle routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param OpenReason
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param Object
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param Type
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param AccessState
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param AdditionalReferences
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param HandleAttributes
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param AccessMode
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param ReturnedObject
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param ReturnedHandle
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
2007-01-09 17:18:22 +00:00
|
|
|
* @remarks Cleans up the Lookup Context on return.
|
2006-06-08 06:31:16 +00:00
|
|
|
*
|
|
|
|
*--*/
|
2006-06-06 21:02:55 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2006-06-08 06:31:16 +00:00
|
|
|
ObpCreateHandle(IN OB_OPEN_REASON OpenReason,
|
2006-06-07 06:15:59 +00:00
|
|
|
IN PVOID Object,
|
2006-06-07 05:37:04 +00:00
|
|
|
IN POBJECT_TYPE Type OPTIONAL,
|
|
|
|
IN PACCESS_STATE AccessState,
|
|
|
|
IN ULONG AdditionalReferences,
|
|
|
|
IN ULONG HandleAttributes,
|
2007-01-08 08:03:47 +00:00
|
|
|
IN POBP_LOOKUP_CONTEXT Context,
|
2006-06-07 05:37:04 +00:00
|
|
|
IN KPROCESSOR_MODE AccessMode,
|
|
|
|
OUT PVOID *ReturnedObject,
|
|
|
|
OUT PHANDLE ReturnedHandle)
|
2006-06-06 21:02:55 +00:00
|
|
|
{
|
|
|
|
HANDLE_TABLE_ENTRY NewEntry;
|
|
|
|
POBJECT_HEADER ObjectHeader;
|
|
|
|
HANDLE Handle;
|
|
|
|
KAPC_STATE ApcState;
|
2007-01-09 17:18:22 +00:00
|
|
|
BOOLEAN AttachedToProcess = FALSE, KernelHandle = FALSE;
|
2006-06-07 23:59:15 +00:00
|
|
|
POBJECT_TYPE ObjectType;
|
|
|
|
PVOID HandleTable;
|
|
|
|
NTSTATUS Status;
|
2007-01-09 17:18:22 +00:00
|
|
|
ACCESS_MASK DesiredAccess, GrantedAccess;
|
2008-06-16 08:54:21 +00:00
|
|
|
PAUX_ACCESS_DATA AuxData;
|
2006-06-07 23:59:15 +00:00
|
|
|
PAGED_CODE();
|
2006-06-06 21:02:55 +00:00
|
|
|
|
2006-06-07 06:15:59 +00:00
|
|
|
/* Get the object header and type */
|
|
|
|
ObjectHeader = OBJECT_TO_OBJECT_HEADER(Object);
|
|
|
|
ObjectType = ObjectHeader->Type;
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
2008-09-12 14:10:27 +00:00
|
|
|
"%s - Creating handle for: %p. Reason: %lx. HC PC %lx %lx\n",
|
2006-06-07 06:15:59 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
Object,
|
|
|
|
OpenReason,
|
|
|
|
ObjectHeader->HandleCount,
|
|
|
|
ObjectHeader->PointerCount);
|
2006-06-06 21:02:55 +00:00
|
|
|
|
2006-06-07 06:15:59 +00:00
|
|
|
/* Check if the types match */
|
2007-01-09 17:18:22 +00:00
|
|
|
if ((Type) && (ObjectType != Type))
|
|
|
|
{
|
|
|
|
/* They don't, cleanup */
|
2008-06-18 12:28:29 +00:00
|
|
|
if (Context) ObpReleaseLookupContext(Context);
|
2007-01-09 17:18:22 +00:00
|
|
|
return STATUS_OBJECT_TYPE_MISMATCH;
|
|
|
|
}
|
2006-06-06 21:02:55 +00:00
|
|
|
|
2007-08-04 08:49:47 +00:00
|
|
|
/* Save the object header */
|
|
|
|
NewEntry.Object = ObjectHeader;
|
|
|
|
|
2006-06-07 06:15:59 +00:00
|
|
|
/* Check if this is a kernel handle */
|
2007-01-15 18:37:53 +00:00
|
|
|
if (HandleAttributes & OBJ_KERNEL_HANDLE)
|
2006-06-06 21:02:55 +00:00
|
|
|
{
|
2006-06-07 06:15:59 +00:00
|
|
|
/* Set the handle table */
|
|
|
|
HandleTable = ObpKernelHandleTable;
|
2007-01-09 17:18:22 +00:00
|
|
|
KernelHandle = TRUE;
|
2006-06-06 21:02:55 +00:00
|
|
|
|
2006-06-07 06:15:59 +00:00
|
|
|
/* Check if we're not in the system process */
|
|
|
|
if (PsGetCurrentProcess() != PsInitialSystemProcess)
|
2006-06-06 21:02:55 +00:00
|
|
|
{
|
2006-06-07 06:15:59 +00:00
|
|
|
/* Attach to the system process */
|
|
|
|
KeStackAttachProcess(&PsInitialSystemProcess->Pcb, &ApcState);
|
2006-06-06 21:02:55 +00:00
|
|
|
AttachedToProcess = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-06-07 06:15:59 +00:00
|
|
|
/* Get the current handle table */
|
|
|
|
HandleTable = PsGetCurrentProcess()->ObjectTable;
|
2006-06-06 21:02:55 +00:00
|
|
|
}
|
|
|
|
|
2006-06-07 23:59:15 +00:00
|
|
|
/* Increment the handle count */
|
|
|
|
Status = ObpIncrementHandleCount(Object,
|
|
|
|
AccessState,
|
|
|
|
AccessMode,
|
|
|
|
HandleAttributes,
|
|
|
|
PsGetCurrentProcess(),
|
|
|
|
OpenReason);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2006-06-06 21:02:55 +00:00
|
|
|
{
|
2006-06-07 06:15:59 +00:00
|
|
|
/*
|
2006-06-07 23:59:15 +00:00
|
|
|
* We failed (meaning security failure, according to NT Internals)
|
|
|
|
* detach and return
|
2006-06-07 06:15:59 +00:00
|
|
|
*/
|
2008-06-18 12:28:29 +00:00
|
|
|
if (Context) ObpReleaseLookupContext(Context);
|
2006-06-07 23:59:15 +00:00
|
|
|
if (AttachedToProcess) KeUnstackDetachProcess(&ApcState);
|
|
|
|
return Status;
|
2006-06-07 06:15:59 +00:00
|
|
|
}
|
|
|
|
|
2007-01-09 17:18:22 +00:00
|
|
|
/* Check if we are doing audits on close */
|
|
|
|
if (AccessState->GenerateOnClose)
|
|
|
|
{
|
|
|
|
/* Force the attribute on */
|
2007-08-11 09:04:17 +00:00
|
|
|
HandleAttributes |= OBJ_AUDIT_OBJECT_CLOSE;
|
2007-01-09 17:18:22 +00:00
|
|
|
}
|
2006-06-07 06:15:59 +00:00
|
|
|
|
|
|
|
/* Mask out the internal attributes */
|
2007-08-11 09:04:17 +00:00
|
|
|
NewEntry.ObAttributes |= (HandleAttributes & OBJ_HANDLE_ATTRIBUTES);
|
2006-06-07 06:15:59 +00:00
|
|
|
|
2007-01-09 17:18:22 +00:00
|
|
|
/* Get the original desired access */
|
|
|
|
DesiredAccess = AccessState->RemainingDesiredAccess |
|
|
|
|
AccessState->PreviouslyGrantedAccess;
|
|
|
|
|
|
|
|
/* Remove what's not in the valid access mask */
|
|
|
|
GrantedAccess = DesiredAccess & (ObjectType->TypeInfo.ValidAccessMask |
|
|
|
|
ACCESS_SYSTEM_SECURITY);
|
|
|
|
|
|
|
|
/* Update the value in the access state */
|
|
|
|
AccessState->PreviouslyGrantedAccess = GrantedAccess;
|
|
|
|
|
|
|
|
/* Get the auxiliary data */
|
|
|
|
AuxData = AccessState->AuxData;
|
2006-06-07 06:15:59 +00:00
|
|
|
|
2006-06-27 01:16:17 +00:00
|
|
|
/* Handle extra references */
|
|
|
|
if (AdditionalReferences)
|
|
|
|
{
|
2007-01-09 17:18:22 +00:00
|
|
|
/* Add them to the header */
|
|
|
|
InterlockedExchangeAdd(&ObjectHeader->PointerCount, AdditionalReferences);
|
2006-06-27 01:16:17 +00:00
|
|
|
}
|
|
|
|
|
2007-01-09 17:18:22 +00:00
|
|
|
/* Now we can release the object */
|
2008-06-18 12:28:29 +00:00
|
|
|
if (Context) ObpReleaseLookupContext(Context);
|
2007-01-09 17:18:22 +00:00
|
|
|
|
|
|
|
/* Save the access mask */
|
|
|
|
NewEntry.GrantedAccess = GrantedAccess;
|
|
|
|
|
2006-06-07 06:15:59 +00:00
|
|
|
/*
|
|
|
|
* Create the actual handle. We'll need to do this *after* calling
|
|
|
|
* ObpIncrementHandleCount to make sure that Object Security is valid
|
|
|
|
* (specified in Gl00my documentation on Ob)
|
|
|
|
*/
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
|
|
|
"%s - Handle Properties: [%p-%lx-%lx]\n",
|
2006-06-07 06:15:59 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
NewEntry.Object, NewEntry.ObAttributes & 3, NewEntry.GrantedAccess);
|
|
|
|
Handle = ExCreateHandle(HandleTable, &NewEntry);
|
|
|
|
|
2006-06-07 23:59:15 +00:00
|
|
|
/* Make sure we got a handle */
|
|
|
|
if (Handle)
|
2006-06-07 06:15:59 +00:00
|
|
|
{
|
2007-01-09 17:18:22 +00:00
|
|
|
/* Check if this was a kernel handle */
|
|
|
|
if (KernelHandle) Handle = ObMarkHandleAsKernelHandle(Handle);
|
|
|
|
|
|
|
|
/* Return it */
|
2006-06-07 05:37:04 +00:00
|
|
|
*ReturnedHandle = Handle;
|
2007-01-09 17:18:22 +00:00
|
|
|
|
|
|
|
/* Check if we need to generate on audit */
|
|
|
|
if (AccessState->GenerateAudit)
|
|
|
|
{
|
|
|
|
/* Audit the handle creation */
|
|
|
|
//SeAuditHandleCreation(AccessState, Handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if this was a create */
|
|
|
|
if (OpenReason == ObCreateHandle)
|
|
|
|
{
|
|
|
|
/* Check if we need to audit the privileges */
|
|
|
|
if ((AuxData->PrivilegeSet) &&
|
|
|
|
(AuxData->PrivilegeSet->PrivilegeCount))
|
|
|
|
{
|
|
|
|
/* Do the audit */
|
|
|
|
#if 0
|
|
|
|
SePrivilegeObjectAuditAlarm(Handle,
|
|
|
|
&AccessState->
|
|
|
|
SubjectSecurityContext,
|
|
|
|
GrantedAccess,
|
|
|
|
AuxData->PrivilegeSet,
|
|
|
|
TRUE,
|
|
|
|
ExGetPreviousMode());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the new object only if caller wanted it biased */
|
|
|
|
if ((AdditionalReferences) && (ReturnedObject))
|
|
|
|
{
|
|
|
|
/* Return it */
|
|
|
|
*ReturnedObject = Object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Detach if needed */
|
|
|
|
if (AttachedToProcess) KeUnstackDetachProcess(&ApcState);
|
|
|
|
|
|
|
|
/* Trace and return */
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
2008-09-12 14:10:27 +00:00
|
|
|
"%s - Returning Handle: %lx HC PC %lx %lx\n",
|
2006-06-07 06:15:59 +00:00
|
|
|
__FUNCTION__,
|
2006-06-07 23:59:15 +00:00
|
|
|
Handle,
|
2006-06-07 06:15:59 +00:00
|
|
|
ObjectHeader->HandleCount,
|
|
|
|
ObjectHeader->PointerCount);
|
2006-06-06 21:02:55 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-01-09 17:18:22 +00:00
|
|
|
/* Decrement the handle count and detach */
|
|
|
|
ObpDecrementHandleCount(&ObjectHeader->Body,
|
|
|
|
PsGetCurrentProcess(),
|
2007-02-14 17:51:48 +00:00
|
|
|
GrantedAccess,
|
|
|
|
ObjectType);
|
2007-01-09 17:18:22 +00:00
|
|
|
|
2006-06-27 01:16:17 +00:00
|
|
|
/* Handle extra references */
|
2007-01-15 18:37:53 +00:00
|
|
|
if (AdditionalReferences)
|
2007-01-09 17:18:22 +00:00
|
|
|
{
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Check how many extra references were added */
|
|
|
|
if (AdditionalReferences > 1)
|
|
|
|
{
|
|
|
|
/* Dereference it many times */
|
|
|
|
InterlockedExchangeAdd(&ObjectHeader->PointerCount,
|
2007-08-04 08:49:47 +00:00
|
|
|
-(LONG)(AdditionalReferences - 1));
|
2007-01-15 18:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Dereference the object one last time */
|
2007-01-09 17:18:22 +00:00
|
|
|
ObDereferenceObject(Object);
|
|
|
|
}
|
2006-06-27 01:16:17 +00:00
|
|
|
|
2007-01-09 17:18:22 +00:00
|
|
|
/* Detach if necessary and fail */
|
|
|
|
if (AttachedToProcess) KeUnstackDetachProcess(&ApcState);
|
2006-06-07 06:15:59 +00:00
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
2006-06-06 21:02:55 +00:00
|
|
|
}
|
|
|
|
|
2006-06-08 18:24:37 +00:00
|
|
|
/*++
|
|
|
|
* @name ObpCloseHandle
|
|
|
|
*
|
|
|
|
* The ObpCloseHandle routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param Handle
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param AccessMode
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
2006-06-08 18:09:01 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
ObpCloseHandle(IN HANDLE Handle,
|
|
|
|
IN KPROCESSOR_MODE AccessMode)
|
|
|
|
{
|
|
|
|
PVOID HandleTable;
|
|
|
|
BOOLEAN AttachedToProcess = FALSE;
|
|
|
|
KAPC_STATE ApcState;
|
|
|
|
PHANDLE_TABLE_ENTRY HandleTableEntry;
|
|
|
|
NTSTATUS Status;
|
2007-02-14 17:51:48 +00:00
|
|
|
PEPROCESS Process = PsGetCurrentProcess();
|
2006-06-08 18:09:01 +00:00
|
|
|
PAGED_CODE();
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
|
|
|
"%s - Closing handle: %lx\n", __FUNCTION__, Handle);
|
2006-06-08 18:09:01 +00:00
|
|
|
|
|
|
|
/* Check if we're dealing with a kernel handle */
|
|
|
|
if (ObIsKernelHandle(Handle, AccessMode))
|
|
|
|
{
|
|
|
|
/* Use the kernel table and convert the handle */
|
|
|
|
HandleTable = ObpKernelHandleTable;
|
|
|
|
Handle = ObKernelHandleToHandle(Handle);
|
|
|
|
|
|
|
|
/* Check if we're not in the system process */
|
2007-02-14 17:51:48 +00:00
|
|
|
if (Process != PsInitialSystemProcess)
|
2006-06-08 18:09:01 +00:00
|
|
|
{
|
|
|
|
/* Attach to the system process */
|
|
|
|
KeStackAttachProcess(&PsInitialSystemProcess->Pcb, &ApcState);
|
|
|
|
AttachedToProcess = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Use the process's handle table */
|
2007-02-14 17:51:48 +00:00
|
|
|
HandleTable = Process->ObjectTable;
|
2006-06-08 18:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Enter a critical region to protect handle access */
|
|
|
|
KeEnterCriticalRegion();
|
|
|
|
|
|
|
|
/* Get the handle entry */
|
|
|
|
HandleTableEntry = ExMapHandleToPointer(HandleTable, Handle);
|
|
|
|
if (HandleTableEntry)
|
|
|
|
{
|
|
|
|
/* Now close the entry */
|
|
|
|
Status = ObpCloseHandleTableEntry(HandleTable,
|
|
|
|
HandleTableEntry,
|
|
|
|
Handle,
|
|
|
|
AccessMode,
|
|
|
|
FALSE);
|
|
|
|
|
|
|
|
/* We can quit the critical region now */
|
|
|
|
KeLeaveCriticalRegion();
|
|
|
|
|
|
|
|
/* Detach and return success */
|
|
|
|
if (AttachedToProcess) KeUnstackDetachProcess(&ApcState);
|
2008-05-14 20:59:43 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
2006-06-08 18:09:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We failed, quit the critical region */
|
|
|
|
KeLeaveCriticalRegion();
|
|
|
|
|
|
|
|
/* Detach */
|
|
|
|
if (AttachedToProcess) KeUnstackDetachProcess(&ApcState);
|
|
|
|
|
2007-02-14 17:51:48 +00:00
|
|
|
/* Check if we have a valid handle that's not the process or thread */
|
|
|
|
if ((Handle) &&
|
|
|
|
(Handle != NtCurrentProcess()) &&
|
|
|
|
(Handle != NtCurrentThread()))
|
2006-06-08 18:09:01 +00:00
|
|
|
{
|
2007-02-14 17:51:48 +00:00
|
|
|
/* Check if we came from user mode */
|
|
|
|
if (AccessMode != KernelMode)
|
|
|
|
{
|
|
|
|
/* Check if we have no debug port */
|
|
|
|
if (Process->DebugPort)
|
|
|
|
{
|
|
|
|
/* Make sure we're not attached */
|
|
|
|
if (!KeIsAttachedProcess())
|
|
|
|
{
|
|
|
|
/* Raise an exception */
|
|
|
|
return KeRaiseUserException(STATUS_INVALID_HANDLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This is kernel mode. Check if we're exiting */
|
|
|
|
if (!(PsIsThreadTerminating(PsGetCurrentThread())) &&
|
|
|
|
(Process->Peb))
|
|
|
|
{
|
|
|
|
/* Check if the debugger is enabled */
|
|
|
|
if (KdDebuggerEnabled)
|
|
|
|
{
|
|
|
|
/* Bugcheck */
|
2008-08-24 15:48:05 +00:00
|
|
|
KeBugCheckEx(INVALID_KERNEL_HANDLE, (ULONG_PTR)Handle, 1, 0, 0);
|
2007-02-14 17:51:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-06-08 18:09:01 +00:00
|
|
|
}
|
2007-02-14 17:51:48 +00:00
|
|
|
|
|
|
|
/* Set invalid status */
|
|
|
|
Status = STATUS_INVALID_HANDLE;
|
2006-06-08 18:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return status */
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
|
|
|
"%s - Closed handle: %lx S: %lx\n",
|
2006-06-08 18:09:01 +00:00
|
|
|
__FUNCTION__, Handle, Status);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2006-06-06 05:49:28 +00:00
|
|
|
/*++
|
|
|
|
* @name ObpSetHandleAttributes
|
|
|
|
*
|
|
|
|
* The ObpSetHandleAttributes routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param HandleTableEntry
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param Context
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
2006-06-05 00:04:36 +00:00
|
|
|
BOOLEAN
|
2006-05-24 22:07:11 +00:00
|
|
|
NTAPI
|
2007-01-22 08:15:17 +00:00
|
|
|
ObpSetHandleAttributes(IN OUT PHANDLE_TABLE_ENTRY HandleTableEntry,
|
|
|
|
IN ULONG_PTR Context)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2007-01-22 08:15:17 +00:00
|
|
|
POBP_SET_HANDLE_ATTRIBUTES_CONTEXT SetHandleInfo = (PVOID)Context;
|
|
|
|
POBJECT_HEADER ObjectHeader = ObpGetHandleObject(HandleTableEntry);
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-05 00:04:36 +00:00
|
|
|
/* Check if making the handle inheritable */
|
|
|
|
if (SetHandleInfo->Information.Inherit)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-29 05:05:27 +00:00
|
|
|
/* Check if inheriting is not supported for this object */
|
|
|
|
if (ObjectHeader->Type->TypeInfo.InvalidAttributes & OBJ_INHERIT)
|
|
|
|
{
|
|
|
|
/* Fail without changing anything */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the flag */
|
2007-01-22 08:15:17 +00:00
|
|
|
HandleTableEntry->ObAttributes |= OBJ_INHERIT;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-06-05 00:04:36 +00:00
|
|
|
/* Otherwise this implies we're removing the flag */
|
2007-01-22 08:15:17 +00:00
|
|
|
HandleTableEntry->ObAttributes &= ~OBJ_INHERIT;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-05 00:04:36 +00:00
|
|
|
/* Check if making the handle protected */
|
|
|
|
if (SetHandleInfo->Information.ProtectFromClose)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-05 00:04:36 +00:00
|
|
|
/* Set the flag */
|
2007-02-14 17:51:48 +00:00
|
|
|
HandleTableEntry->GrantedAccess |= ObpAccessProtectCloseBit;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-06-05 00:04:36 +00:00
|
|
|
/* Otherwise, remove it */
|
2007-02-14 17:51:48 +00:00
|
|
|
HandleTableEntry->GrantedAccess &= ~ObpAccessProtectCloseBit;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-05 00:04:36 +00:00
|
|
|
/* Return success */
|
|
|
|
return TRUE;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-08 06:31:16 +00:00
|
|
|
/*++
|
|
|
|
* @name ObpCloseHandleCallback
|
|
|
|
*
|
|
|
|
* The ObpCloseHandleCallback routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param HandleTable
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param Object
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param GrantedAccess
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param Context
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
2007-02-14 17:51:48 +00:00
|
|
|
BOOLEAN
|
2006-06-08 18:09:01 +00:00
|
|
|
NTAPI
|
|
|
|
ObpCloseHandleCallback(IN PHANDLE_TABLE_ENTRY HandleTableEntry,
|
|
|
|
IN HANDLE Handle,
|
|
|
|
IN PVOID Context)
|
|
|
|
{
|
|
|
|
POBP_CLOSE_HANDLE_CONTEXT CloseContext = (POBP_CLOSE_HANDLE_CONTEXT)Context;
|
|
|
|
|
|
|
|
/* Simply decrement the handle count */
|
2006-06-10 00:41:16 +00:00
|
|
|
ObpCloseHandleTableEntry(CloseContext->HandleTable,
|
2006-06-08 18:09:01 +00:00
|
|
|
HandleTableEntry,
|
|
|
|
Handle,
|
2006-06-10 00:41:16 +00:00
|
|
|
CloseContext->AccessMode,
|
2006-06-08 18:09:01 +00:00
|
|
|
TRUE);
|
2007-02-14 17:51:48 +00:00
|
|
|
return TRUE;
|
2006-06-06 21:02:55 +00:00
|
|
|
}
|
|
|
|
|
2006-06-08 06:31:16 +00:00
|
|
|
/*++
|
|
|
|
* @name ObpDuplicateHandleCallback
|
|
|
|
*
|
|
|
|
* The ObpDuplicateHandleCallback routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param HandleTable
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param HandleTableEntry
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param Context
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
2006-06-06 21:02:55 +00:00
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
2007-01-22 08:15:17 +00:00
|
|
|
ObpDuplicateHandleCallback(IN PEPROCESS Process,
|
|
|
|
IN PHANDLE_TABLE HandleTable,
|
|
|
|
IN PHANDLE_TABLE_ENTRY OldEntry,
|
|
|
|
IN PHANDLE_TABLE_ENTRY HandleTableEntry)
|
2006-06-06 21:02:55 +00:00
|
|
|
{
|
|
|
|
POBJECT_HEADER ObjectHeader;
|
|
|
|
BOOLEAN Ret = FALSE;
|
2006-06-07 05:04:43 +00:00
|
|
|
ACCESS_STATE AccessState;
|
2006-06-07 23:59:15 +00:00
|
|
|
NTSTATUS Status;
|
2006-06-06 21:02:55 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Make sure that the handle is inheritable */
|
2007-01-22 08:15:17 +00:00
|
|
|
Ret = (HandleTableEntry->ObAttributes & OBJ_INHERIT) != 0;
|
2006-06-27 01:16:17 +00:00
|
|
|
if (Ret)
|
2006-06-06 21:02:55 +00:00
|
|
|
{
|
2006-06-07 05:37:04 +00:00
|
|
|
/* Get the object header */
|
2007-01-22 08:15:17 +00:00
|
|
|
ObjectHeader = ObpGetHandleObject(HandleTableEntry);
|
|
|
|
|
|
|
|
/* Increment the pointer count */
|
|
|
|
InterlockedIncrement(&ObjectHeader->PointerCount);
|
|
|
|
|
|
|
|
/* Release the handle lock */
|
|
|
|
ExUnlockHandleTableEntry(HandleTable, OldEntry);
|
2006-06-07 05:37:04 +00:00
|
|
|
|
2006-06-07 05:04:43 +00:00
|
|
|
/* Setup the access state */
|
|
|
|
AccessState.PreviouslyGrantedAccess = HandleTableEntry->GrantedAccess;
|
|
|
|
|
2006-06-07 23:59:15 +00:00
|
|
|
/* Call the shared routine for incrementing handles */
|
|
|
|
Status = ObpIncrementHandleCount(&ObjectHeader->Body,
|
|
|
|
&AccessState,
|
|
|
|
KernelMode,
|
2008-07-14 17:42:22 +00:00
|
|
|
HandleTableEntry->ObAttributes & OBJ_HANDLE_ATTRIBUTES,
|
2007-01-22 08:15:17 +00:00
|
|
|
Process,
|
2006-06-07 23:59:15 +00:00
|
|
|
ObInheritHandle);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Return failure */
|
2007-01-22 08:15:17 +00:00
|
|
|
ObDereferenceObject(&ObjectHeader->Body);
|
2006-06-07 23:59:15 +00:00
|
|
|
Ret = FALSE;
|
|
|
|
}
|
2007-01-22 08:15:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Release the handle lock */
|
|
|
|
ExUnlockHandleTableEntry(HandleTable, OldEntry);
|
2006-06-06 21:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return duplication result */
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2006-07-09 18:54:13 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
ObClearProcessHandleTable(IN PEPROCESS Process)
|
|
|
|
{
|
|
|
|
/* FIXME */
|
|
|
|
}
|
|
|
|
|
2006-06-08 06:31:16 +00:00
|
|
|
/*++
|
|
|
|
* @name ObpCreateHandleTable
|
|
|
|
*
|
|
|
|
* The ObpCreateHandleTable routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param Parent
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param Process
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
2006-06-06 21:02:55 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
- PS_UNKNOWN_VALUE determines if the process should be created with large pages: rename to PS_LARGE_PAGES
- Do Address Space initialization as per "Windows Internals II" slides, either for the Boot, System, Cloned User or New User Process cases.
- Rename MmCreateProcessAddressSpace to MmInitializeProcessAddressSpace, and MmCopyMmInfo to MmCreateProcessAddressSpace. What ReactOS did is correct as per "Windows Internals II", but the names were inverted.
- Clone the Object Table if we are the boot process, and only initialize part of the address space (since we don't need things like guard page, TEB, etc), however, do initialize and map the shared user data section.
- Make the initial system process and idle process share the same page directory instead of creating a new one.
- Use the same priority class as the parent process, if the process was in the idle or below-normal priority class.
- Only duplicate handles if the caller requested it, instead of always duplicating the process's handles!
- Generate a null image file name for system processes.
- Rename ObpCreateHandleTable to ObInitProcess and better handle race and out-of-memory conditions. Detect if auditing required, but don't do anything about it.
- Initialize the Idle/System process address space much earlier in the boot process, in MmInitSystem.
Thanks to Alex for providing various information, and answering all my questions.
svn path=/trunk/; revision=29223
2007-09-26 20:55:26 +00:00
|
|
|
ObInitProcess(IN PEPROCESS Parent OPTIONAL,
|
|
|
|
IN PEPROCESS Process)
|
2006-06-06 21:02:55 +00:00
|
|
|
{
|
- PS_UNKNOWN_VALUE determines if the process should be created with large pages: rename to PS_LARGE_PAGES
- Do Address Space initialization as per "Windows Internals II" slides, either for the Boot, System, Cloned User or New User Process cases.
- Rename MmCreateProcessAddressSpace to MmInitializeProcessAddressSpace, and MmCopyMmInfo to MmCreateProcessAddressSpace. What ReactOS did is correct as per "Windows Internals II", but the names were inverted.
- Clone the Object Table if we are the boot process, and only initialize part of the address space (since we don't need things like guard page, TEB, etc), however, do initialize and map the shared user data section.
- Make the initial system process and idle process share the same page directory instead of creating a new one.
- Use the same priority class as the parent process, if the process was in the idle or below-normal priority class.
- Only duplicate handles if the caller requested it, instead of always duplicating the process's handles!
- Generate a null image file name for system processes.
- Rename ObpCreateHandleTable to ObInitProcess and better handle race and out-of-memory conditions. Detect if auditing required, but don't do anything about it.
- Initialize the Idle/System process address space much earlier in the boot process, in MmInitSystem.
Thanks to Alex for providing various information, and answering all my questions.
svn path=/trunk/; revision=29223
2007-09-26 20:55:26 +00:00
|
|
|
PHANDLE_TABLE ParentTable, ObjectTable;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
- PS_UNKNOWN_VALUE determines if the process should be created with large pages: rename to PS_LARGE_PAGES
- Do Address Space initialization as per "Windows Internals II" slides, either for the Boot, System, Cloned User or New User Process cases.
- Rename MmCreateProcessAddressSpace to MmInitializeProcessAddressSpace, and MmCopyMmInfo to MmCreateProcessAddressSpace. What ReactOS did is correct as per "Windows Internals II", but the names were inverted.
- Clone the Object Table if we are the boot process, and only initialize part of the address space (since we don't need things like guard page, TEB, etc), however, do initialize and map the shared user data section.
- Make the initial system process and idle process share the same page directory instead of creating a new one.
- Use the same priority class as the parent process, if the process was in the idle or below-normal priority class.
- Only duplicate handles if the caller requested it, instead of always duplicating the process's handles!
- Generate a null image file name for system processes.
- Rename ObpCreateHandleTable to ObInitProcess and better handle race and out-of-memory conditions. Detect if auditing required, but don't do anything about it.
- Initialize the Idle/System process address space much earlier in the boot process, in MmInitSystem.
Thanks to Alex for providing various information, and answering all my questions.
svn path=/trunk/; revision=29223
2007-09-26 20:55:26 +00:00
|
|
|
/* Check for a parent */
|
2006-06-06 21:02:55 +00:00
|
|
|
if (Parent)
|
|
|
|
{
|
- PS_UNKNOWN_VALUE determines if the process should be created with large pages: rename to PS_LARGE_PAGES
- Do Address Space initialization as per "Windows Internals II" slides, either for the Boot, System, Cloned User or New User Process cases.
- Rename MmCreateProcessAddressSpace to MmInitializeProcessAddressSpace, and MmCopyMmInfo to MmCreateProcessAddressSpace. What ReactOS did is correct as per "Windows Internals II", but the names were inverted.
- Clone the Object Table if we are the boot process, and only initialize part of the address space (since we don't need things like guard page, TEB, etc), however, do initialize and map the shared user data section.
- Make the initial system process and idle process share the same page directory instead of creating a new one.
- Use the same priority class as the parent process, if the process was in the idle or below-normal priority class.
- Only duplicate handles if the caller requested it, instead of always duplicating the process's handles!
- Generate a null image file name for system processes.
- Rename ObpCreateHandleTable to ObInitProcess and better handle race and out-of-memory conditions. Detect if auditing required, but don't do anything about it.
- Initialize the Idle/System process address space much earlier in the boot process, in MmInitSystem.
Thanks to Alex for providing various information, and answering all my questions.
svn path=/trunk/; revision=29223
2007-09-26 20:55:26 +00:00
|
|
|
/* Reference the parent's table */
|
|
|
|
ParentTable = ObReferenceProcessHandleTable(Parent);
|
|
|
|
if (!ParentTable) return STATUS_PROCESS_IS_TERMINATING;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
- PS_UNKNOWN_VALUE determines if the process should be created with large pages: rename to PS_LARGE_PAGES
- Do Address Space initialization as per "Windows Internals II" slides, either for the Boot, System, Cloned User or New User Process cases.
- Rename MmCreateProcessAddressSpace to MmInitializeProcessAddressSpace, and MmCopyMmInfo to MmCreateProcessAddressSpace. What ReactOS did is correct as per "Windows Internals II", but the names were inverted.
- Clone the Object Table if we are the boot process, and only initialize part of the address space (since we don't need things like guard page, TEB, etc), however, do initialize and map the shared user data section.
- Make the initial system process and idle process share the same page directory instead of creating a new one.
- Use the same priority class as the parent process, if the process was in the idle or below-normal priority class.
- Only duplicate handles if the caller requested it, instead of always duplicating the process's handles!
- Generate a null image file name for system processes.
- Rename ObpCreateHandleTable to ObInitProcess and better handle race and out-of-memory conditions. Detect if auditing required, but don't do anything about it.
- Initialize the Idle/System process address space much earlier in the boot process, in MmInitSystem.
Thanks to Alex for providing various information, and answering all my questions.
svn path=/trunk/; revision=29223
2007-09-26 20:55:26 +00:00
|
|
|
/* Duplicate it */
|
|
|
|
ObjectTable = ExDupHandleTable(Process,
|
|
|
|
ParentTable,
|
2006-06-06 21:02:55 +00:00
|
|
|
ObpDuplicateHandleCallback,
|
2007-01-22 08:15:17 +00:00
|
|
|
OBJ_INHERIT);
|
2006-06-06 21:02:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
- PS_UNKNOWN_VALUE determines if the process should be created with large pages: rename to PS_LARGE_PAGES
- Do Address Space initialization as per "Windows Internals II" slides, either for the Boot, System, Cloned User or New User Process cases.
- Rename MmCreateProcessAddressSpace to MmInitializeProcessAddressSpace, and MmCopyMmInfo to MmCreateProcessAddressSpace. What ReactOS did is correct as per "Windows Internals II", but the names were inverted.
- Clone the Object Table if we are the boot process, and only initialize part of the address space (since we don't need things like guard page, TEB, etc), however, do initialize and map the shared user data section.
- Make the initial system process and idle process share the same page directory instead of creating a new one.
- Use the same priority class as the parent process, if the process was in the idle or below-normal priority class.
- Only duplicate handles if the caller requested it, instead of always duplicating the process's handles!
- Generate a null image file name for system processes.
- Rename ObpCreateHandleTable to ObInitProcess and better handle race and out-of-memory conditions. Detect if auditing required, but don't do anything about it.
- Initialize the Idle/System process address space much earlier in the boot process, in MmInitSystem.
Thanks to Alex for providing various information, and answering all my questions.
svn path=/trunk/; revision=29223
2007-09-26 20:55:26 +00:00
|
|
|
/* Otherwise just create a new table */
|
|
|
|
ParentTable = NULL;
|
|
|
|
ObjectTable = ExCreateHandleTable(Process);
|
|
|
|
}
|
2007-10-19 23:21:45 +00:00
|
|
|
|
- PS_UNKNOWN_VALUE determines if the process should be created with large pages: rename to PS_LARGE_PAGES
- Do Address Space initialization as per "Windows Internals II" slides, either for the Boot, System, Cloned User or New User Process cases.
- Rename MmCreateProcessAddressSpace to MmInitializeProcessAddressSpace, and MmCopyMmInfo to MmCreateProcessAddressSpace. What ReactOS did is correct as per "Windows Internals II", but the names were inverted.
- Clone the Object Table if we are the boot process, and only initialize part of the address space (since we don't need things like guard page, TEB, etc), however, do initialize and map the shared user data section.
- Make the initial system process and idle process share the same page directory instead of creating a new one.
- Use the same priority class as the parent process, if the process was in the idle or below-normal priority class.
- Only duplicate handles if the caller requested it, instead of always duplicating the process's handles!
- Generate a null image file name for system processes.
- Rename ObpCreateHandleTable to ObInitProcess and better handle race and out-of-memory conditions. Detect if auditing required, but don't do anything about it.
- Initialize the Idle/System process address space much earlier in the boot process, in MmInitSystem.
Thanks to Alex for providing various information, and answering all my questions.
svn path=/trunk/; revision=29223
2007-09-26 20:55:26 +00:00
|
|
|
/* Make sure we have a table */
|
|
|
|
if (ObjectTable)
|
|
|
|
{
|
2007-10-19 23:21:45 +00:00
|
|
|
/* Associate it */
|
- PS_UNKNOWN_VALUE determines if the process should be created with large pages: rename to PS_LARGE_PAGES
- Do Address Space initialization as per "Windows Internals II" slides, either for the Boot, System, Cloned User or New User Process cases.
- Rename MmCreateProcessAddressSpace to MmInitializeProcessAddressSpace, and MmCopyMmInfo to MmCreateProcessAddressSpace. What ReactOS did is correct as per "Windows Internals II", but the names were inverted.
- Clone the Object Table if we are the boot process, and only initialize part of the address space (since we don't need things like guard page, TEB, etc), however, do initialize and map the shared user data section.
- Make the initial system process and idle process share the same page directory instead of creating a new one.
- Use the same priority class as the parent process, if the process was in the idle or below-normal priority class.
- Only duplicate handles if the caller requested it, instead of always duplicating the process's handles!
- Generate a null image file name for system processes.
- Rename ObpCreateHandleTable to ObInitProcess and better handle race and out-of-memory conditions. Detect if auditing required, but don't do anything about it.
- Initialize the Idle/System process address space much earlier in the boot process, in MmInitSystem.
Thanks to Alex for providing various information, and answering all my questions.
svn path=/trunk/; revision=29223
2007-09-26 20:55:26 +00:00
|
|
|
Process->ObjectTable = ObjectTable;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
- PS_UNKNOWN_VALUE determines if the process should be created with large pages: rename to PS_LARGE_PAGES
- Do Address Space initialization as per "Windows Internals II" slides, either for the Boot, System, Cloned User or New User Process cases.
- Rename MmCreateProcessAddressSpace to MmInitializeProcessAddressSpace, and MmCopyMmInfo to MmCreateProcessAddressSpace. What ReactOS did is correct as per "Windows Internals II", but the names were inverted.
- Clone the Object Table if we are the boot process, and only initialize part of the address space (since we don't need things like guard page, TEB, etc), however, do initialize and map the shared user data section.
- Make the initial system process and idle process share the same page directory instead of creating a new one.
- Use the same priority class as the parent process, if the process was in the idle or below-normal priority class.
- Only duplicate handles if the caller requested it, instead of always duplicating the process's handles!
- Generate a null image file name for system processes.
- Rename ObpCreateHandleTable to ObInitProcess and better handle race and out-of-memory conditions. Detect if auditing required, but don't do anything about it.
- Initialize the Idle/System process address space much earlier in the boot process, in MmInitSystem.
Thanks to Alex for providing various information, and answering all my questions.
svn path=/trunk/; revision=29223
2007-09-26 20:55:26 +00:00
|
|
|
/* Check for auditing */
|
|
|
|
if (SeDetailedAuditingWithToken(NULL))
|
|
|
|
{
|
|
|
|
/* FIXME: TODO */
|
|
|
|
DPRINT1("Need auditing!\n");
|
|
|
|
}
|
2007-10-19 23:21:45 +00:00
|
|
|
|
- PS_UNKNOWN_VALUE determines if the process should be created with large pages: rename to PS_LARGE_PAGES
- Do Address Space initialization as per "Windows Internals II" slides, either for the Boot, System, Cloned User or New User Process cases.
- Rename MmCreateProcessAddressSpace to MmInitializeProcessAddressSpace, and MmCopyMmInfo to MmCreateProcessAddressSpace. What ReactOS did is correct as per "Windows Internals II", but the names were inverted.
- Clone the Object Table if we are the boot process, and only initialize part of the address space (since we don't need things like guard page, TEB, etc), however, do initialize and map the shared user data section.
- Make the initial system process and idle process share the same page directory instead of creating a new one.
- Use the same priority class as the parent process, if the process was in the idle or below-normal priority class.
- Only duplicate handles if the caller requested it, instead of always duplicating the process's handles!
- Generate a null image file name for system processes.
- Rename ObpCreateHandleTable to ObInitProcess and better handle race and out-of-memory conditions. Detect if auditing required, but don't do anything about it.
- Initialize the Idle/System process address space much earlier in the boot process, in MmInitSystem.
Thanks to Alex for providing various information, and answering all my questions.
svn path=/trunk/; revision=29223
2007-09-26 20:55:26 +00:00
|
|
|
/* Get rid of the old table now */
|
|
|
|
if (ParentTable) ObDereferenceProcessHandleTable(Parent);
|
2007-10-19 23:21:45 +00:00
|
|
|
|
- PS_UNKNOWN_VALUE determines if the process should be created with large pages: rename to PS_LARGE_PAGES
- Do Address Space initialization as per "Windows Internals II" slides, either for the Boot, System, Cloned User or New User Process cases.
- Rename MmCreateProcessAddressSpace to MmInitializeProcessAddressSpace, and MmCopyMmInfo to MmCreateProcessAddressSpace. What ReactOS did is correct as per "Windows Internals II", but the names were inverted.
- Clone the Object Table if we are the boot process, and only initialize part of the address space (since we don't need things like guard page, TEB, etc), however, do initialize and map the shared user data section.
- Make the initial system process and idle process share the same page directory instead of creating a new one.
- Use the same priority class as the parent process, if the process was in the idle or below-normal priority class.
- Only duplicate handles if the caller requested it, instead of always duplicating the process's handles!
- Generate a null image file name for system processes.
- Rename ObpCreateHandleTable to ObInitProcess and better handle race and out-of-memory conditions. Detect if auditing required, but don't do anything about it.
- Initialize the Idle/System process address space much earlier in the boot process, in MmInitSystem.
Thanks to Alex for providing various information, and answering all my questions.
svn path=/trunk/; revision=29223
2007-09-26 20:55:26 +00:00
|
|
|
/* We are done */
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
Process->ObjectTable = NULL;
|
|
|
|
if (ParentTable) ObDereferenceProcessHandleTable(Parent);
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
2006-06-06 21:02:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-08 06:31:16 +00:00
|
|
|
/*++
|
|
|
|
* @name ObKillProcess
|
|
|
|
*
|
|
|
|
* The ObKillProcess routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param Process
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
2006-06-06 21:02:55 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
ObKillProcess(IN PEPROCESS Process)
|
|
|
|
{
|
2007-01-10 03:35:59 +00:00
|
|
|
PHANDLE_TABLE HandleTable;
|
2006-06-08 18:09:01 +00:00
|
|
|
OBP_CLOSE_HANDLE_CONTEXT Context;
|
2007-01-10 03:35:59 +00:00
|
|
|
BOOLEAN HardErrors;
|
2006-06-06 21:02:55 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Wait for process rundown and then complete it */
|
2007-01-10 03:35:59 +00:00
|
|
|
ExWaitForRundownProtectionRelease(&Process->RundownProtect);
|
2007-01-15 18:37:53 +00:00
|
|
|
ExRundownCompleted(&Process->RundownProtect);
|
2007-01-10 03:35:59 +00:00
|
|
|
|
|
|
|
/* Get the object table */
|
|
|
|
HandleTable = Process->ObjectTable;
|
|
|
|
if (!HandleTable) return;
|
|
|
|
|
|
|
|
/* Disable hard errors while we close handles */
|
|
|
|
HardErrors = IoSetThreadHardErrorMode(FALSE);
|
|
|
|
|
2006-06-06 21:02:55 +00:00
|
|
|
/* Enter a critical region */
|
|
|
|
KeEnterCriticalRegion();
|
|
|
|
|
2006-06-08 18:09:01 +00:00
|
|
|
/* Fill out the context */
|
|
|
|
Context.AccessMode = KernelMode;
|
|
|
|
Context.HandleTable = HandleTable;
|
|
|
|
|
2006-06-06 21:02:55 +00:00
|
|
|
/* Sweep the handle table to close all handles */
|
2006-06-08 18:09:01 +00:00
|
|
|
ExSweepHandleTable(HandleTable,
|
2006-06-06 21:02:55 +00:00
|
|
|
ObpCloseHandleCallback,
|
2006-06-15 13:16:29 +00:00
|
|
|
&Context);
|
2007-09-30 08:32:34 +00:00
|
|
|
ASSERT(HandleTable->HandleCount == 0);
|
2006-06-06 21:02:55 +00:00
|
|
|
|
2007-01-10 03:35:59 +00:00
|
|
|
/* Leave the critical region */
|
2006-06-06 21:02:55 +00:00
|
|
|
KeLeaveCriticalRegion();
|
|
|
|
|
2007-01-10 03:35:59 +00:00
|
|
|
/* Re-enable hard errors */
|
|
|
|
IoSetThreadHardErrorMode(HardErrors);
|
|
|
|
|
|
|
|
/* Destroy the object table */
|
2006-06-06 21:02:55 +00:00
|
|
|
Process->ObjectTable = NULL;
|
2007-01-22 08:15:17 +00:00
|
|
|
ExDestroyHandleTable(HandleTable, NULL);
|
2006-06-06 21:02:55 +00:00
|
|
|
}
|
|
|
|
|
2006-05-24 22:07:11 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2006-06-11 06:52:56 +00:00
|
|
|
ObDuplicateObject(IN PEPROCESS SourceProcess,
|
|
|
|
IN HANDLE SourceHandle,
|
|
|
|
IN PEPROCESS TargetProcess OPTIONAL,
|
|
|
|
IN PHANDLE TargetHandle OPTIONAL,
|
|
|
|
IN ACCESS_MASK DesiredAccess,
|
|
|
|
IN ULONG HandleAttributes,
|
|
|
|
IN ULONG Options,
|
|
|
|
IN KPROCESSOR_MODE PreviousMode)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
|
|
|
HANDLE_TABLE_ENTRY NewHandleEntry;
|
|
|
|
BOOLEAN AttachedToProcess = FALSE;
|
2006-06-11 06:52:56 +00:00
|
|
|
PVOID SourceObject;
|
2006-05-24 22:07:11 +00:00
|
|
|
POBJECT_HEADER ObjectHeader;
|
2006-06-11 06:52:56 +00:00
|
|
|
POBJECT_TYPE ObjectType;
|
|
|
|
HANDLE NewHandle;
|
2006-05-24 22:07:11 +00:00
|
|
|
KAPC_STATE ApcState;
|
2007-01-10 03:35:59 +00:00
|
|
|
NTSTATUS Status;
|
2006-06-11 06:52:56 +00:00
|
|
|
ACCESS_MASK TargetAccess, SourceAccess;
|
|
|
|
ACCESS_STATE AccessState;
|
|
|
|
PACCESS_STATE PassedAccessState = NULL;
|
2008-06-16 08:54:21 +00:00
|
|
|
AUX_ACCESS_DATA AuxData;
|
2007-01-10 03:35:59 +00:00
|
|
|
PHANDLE_TABLE HandleTable;
|
2006-06-11 06:52:56 +00:00
|
|
|
OBJECT_HANDLE_INFORMATION HandleInformation;
|
2007-01-15 18:37:53 +00:00
|
|
|
ULONG AuditMask;
|
2006-05-24 22:07:11 +00:00
|
|
|
PAGED_CODE();
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
|
|
|
"%s - Duplicating handle: %lx for %p into %p\n",
|
2006-06-11 06:52:56 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
SourceHandle,
|
|
|
|
SourceProcess,
|
|
|
|
TargetProcess);
|
|
|
|
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Assume failure */
|
|
|
|
if (TargetHandle) *TargetHandle = NULL;
|
|
|
|
|
2007-01-10 03:35:59 +00:00
|
|
|
/* Check if we're not duplicating the same access */
|
|
|
|
if (!(Options & DUPLICATE_SAME_ACCESS))
|
2006-06-11 06:52:56 +00:00
|
|
|
{
|
2007-01-10 03:35:59 +00:00
|
|
|
/* Validate the desired access */
|
|
|
|
Status = STATUS_SUCCESS; //ObpValidateDesiredAccess(DesiredAccess);
|
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
2006-06-11 06:52:56 +00:00
|
|
|
}
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2007-01-10 03:35:59 +00:00
|
|
|
/* Reference the object table */
|
|
|
|
HandleTable = ObReferenceProcessHandleTable(SourceProcess);
|
|
|
|
if (!HandleTable) return STATUS_PROCESS_IS_TERMINATING;
|
2006-06-11 06:52:56 +00:00
|
|
|
|
2007-01-10 03:35:59 +00:00
|
|
|
/* Reference the process object */
|
|
|
|
Status = ObpReferenceProcessObjectByHandle(SourceHandle,
|
2007-01-15 18:37:53 +00:00
|
|
|
SourceProcess,
|
2007-01-10 03:35:59 +00:00
|
|
|
HandleTable,
|
|
|
|
PreviousMode,
|
|
|
|
&SourceObject,
|
2007-01-15 18:37:53 +00:00
|
|
|
&HandleInformation,
|
|
|
|
&AuditMask);
|
2007-01-10 03:35:59 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2007-01-10 03:35:59 +00:00
|
|
|
/* Fail */
|
|
|
|
ObDereferenceProcessHandleTable(SourceProcess);
|
|
|
|
return Status;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
2007-02-14 17:51:48 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Check if we have to don't have to audit object close */
|
|
|
|
if (!(HandleInformation.HandleAttributes & OBJ_AUDIT_OBJECT_CLOSE))
|
|
|
|
{
|
|
|
|
/* Then there is no audit mask */
|
|
|
|
AuditMask = 0;
|
|
|
|
}
|
|
|
|
}
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2007-01-10 03:35:59 +00:00
|
|
|
/* Check if there's no target process */
|
|
|
|
if (!TargetProcess)
|
|
|
|
{
|
|
|
|
/* Check if the caller wanted actual duplication */
|
|
|
|
if (!(Options & DUPLICATE_CLOSE_SOURCE))
|
|
|
|
{
|
|
|
|
/* Invalid request */
|
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, do the attach */
|
|
|
|
KeStackAttachProcess(&SourceProcess->Pcb, &ApcState);
|
|
|
|
|
|
|
|
/* Close the handle and detach */
|
|
|
|
NtClose(SourceHandle);
|
|
|
|
KeUnstackDetachProcess(&ApcState);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return */
|
|
|
|
ObDereferenceProcessHandleTable(SourceProcess);
|
|
|
|
ObDereferenceObject(SourceObject);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the target handle table */
|
|
|
|
HandleTable = ObReferenceProcessHandleTable(TargetProcess);
|
|
|
|
if (!HandleTable)
|
|
|
|
{
|
|
|
|
/* Check if the caller wanted us to close the handle */
|
|
|
|
if (Options & DUPLICATE_CLOSE_SOURCE)
|
|
|
|
{
|
|
|
|
/* Do the attach */
|
|
|
|
KeStackAttachProcess(&SourceProcess->Pcb, &ApcState);
|
|
|
|
|
|
|
|
/* Close the handle and detach */
|
|
|
|
NtClose(SourceHandle);
|
|
|
|
KeUnstackDetachProcess(&ApcState);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return */
|
|
|
|
ObDereferenceProcessHandleTable(SourceProcess);
|
|
|
|
ObDereferenceObject(SourceObject);
|
|
|
|
return STATUS_PROCESS_IS_TERMINATING;
|
|
|
|
}
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Get the source access */
|
|
|
|
SourceAccess = HandleInformation.GrantedAccess;
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Check if we're not in the target process */
|
|
|
|
if (TargetProcess != PsGetCurrentProcess())
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Attach to it */
|
|
|
|
KeStackAttachProcess(&TargetProcess->Pcb, &ApcState);
|
2006-05-24 22:07:11 +00:00
|
|
|
AttachedToProcess = TRUE;
|
|
|
|
}
|
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Check if we're duplicating the attributes */
|
|
|
|
if (Options & DUPLICATE_SAME_ATTRIBUTES)
|
|
|
|
{
|
|
|
|
/* Duplicate them */
|
|
|
|
HandleAttributes = HandleInformation.HandleAttributes;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
2007-02-14 17:51:48 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Don't allow caller to bypass auditing */
|
|
|
|
HandleAttributes |= HandleInformation.HandleAttributes &
|
|
|
|
OBJ_AUDIT_OBJECT_CLOSE;
|
|
|
|
}
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Check if we're duplicating the access */
|
|
|
|
if (Options & DUPLICATE_SAME_ACCESS) DesiredAccess = SourceAccess;
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Get object data */
|
|
|
|
ObjectHeader = OBJECT_TO_OBJECT_HEADER(SourceObject);
|
|
|
|
ObjectType = ObjectHeader->Type;
|
|
|
|
|
|
|
|
/* Fill out the entry */
|
2007-02-14 17:51:48 +00:00
|
|
|
RtlZeroMemory(&NewHandleEntry, sizeof(HANDLE_TABLE_ENTRY));
|
2006-06-11 06:52:56 +00:00
|
|
|
NewHandleEntry.Object = ObjectHeader;
|
2007-08-11 09:04:17 +00:00
|
|
|
NewHandleEntry.ObAttributes |= (HandleAttributes & OBJ_HANDLE_ATTRIBUTES);
|
2006-06-11 06:52:56 +00:00
|
|
|
|
|
|
|
/* Check if we're using a generic mask */
|
|
|
|
if (DesiredAccess & GENERIC_ACCESS)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Map it */
|
2007-01-10 03:35:59 +00:00
|
|
|
RtlMapGenericMask(&DesiredAccess,
|
|
|
|
&ObjectType->TypeInfo.GenericMapping);
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
2006-06-11 06:52:56 +00:00
|
|
|
|
2007-02-14 17:51:48 +00:00
|
|
|
/* Set the target access, always propagate ACCESS_SYSTEM_SECURITY */
|
|
|
|
TargetAccess = DesiredAccess & (ObjectType->TypeInfo.ValidAccessMask |
|
|
|
|
ACCESS_SYSTEM_SECURITY);
|
2006-06-11 06:52:56 +00:00
|
|
|
NewHandleEntry.GrantedAccess = TargetAccess;
|
|
|
|
|
|
|
|
/* Check if we're asking for new access */
|
|
|
|
if (TargetAccess & ~SourceAccess)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* We are. We need the security procedure to validate this */
|
|
|
|
if (ObjectType->TypeInfo.SecurityProcedure == SeDefaultObjectMethod)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Use our built-in access state */
|
|
|
|
PassedAccessState = &AccessState;
|
|
|
|
Status = SeCreateAccessState(&AccessState,
|
|
|
|
&AuxData,
|
|
|
|
TargetAccess,
|
|
|
|
&ObjectType->TypeInfo.GenericMapping);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise we can't allow this privilege elevation */
|
|
|
|
Status = STATUS_ACCESS_DENIED;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-11 06:52:56 +00:00
|
|
|
else
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* We don't need an access state */
|
|
|
|
Status = STATUS_SUCCESS;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Make sure the access state was created OK */
|
|
|
|
if (NT_SUCCESS(Status))
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Add a new handle */
|
|
|
|
Status = ObpIncrementHandleCount(SourceObject,
|
|
|
|
PassedAccessState,
|
|
|
|
PreviousMode,
|
|
|
|
HandleAttributes,
|
|
|
|
PsGetCurrentProcess(),
|
|
|
|
ObDuplicateHandle);
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Check if we were attached */
|
2006-05-24 22:07:11 +00:00
|
|
|
if (AttachedToProcess)
|
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* We can safely detach now */
|
2006-05-24 22:07:11 +00:00
|
|
|
KeUnstackDetachProcess(&ApcState);
|
|
|
|
AttachedToProcess = FALSE;
|
|
|
|
}
|
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Check if we have to close the source handle */
|
|
|
|
if (Options & DUPLICATE_CLOSE_SOURCE)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Attach and close */
|
|
|
|
KeStackAttachProcess(&SourceProcess->Pcb, &ApcState);
|
|
|
|
NtClose(SourceHandle);
|
|
|
|
KeUnstackDetachProcess(&ApcState);
|
|
|
|
}
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Check if we had an access state */
|
|
|
|
if (PassedAccessState) SeDeleteAccessState(PassedAccessState);
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Now check if incrementing actually failed */
|
|
|
|
if (!NT_SUCCESS(Status))
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Dereference handle tables */
|
|
|
|
ObDereferenceProcessHandleTable(SourceProcess);
|
|
|
|
ObDereferenceProcessHandleTable(TargetProcess);
|
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Dereference the source object */
|
|
|
|
ObDereferenceObject(SourceObject);
|
|
|
|
return Status;
|
|
|
|
}
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Now create the handle */
|
|
|
|
NewHandle = ExCreateHandle(HandleTable, &NewHandleEntry);
|
|
|
|
if (!NewHandle)
|
|
|
|
{
|
|
|
|
/* Undo the increment */
|
|
|
|
ObpDecrementHandleCount(SourceObject,
|
|
|
|
TargetProcess,
|
2007-02-14 17:51:48 +00:00
|
|
|
TargetAccess,
|
|
|
|
ObjectType);
|
2006-06-11 06:52:56 +00:00
|
|
|
|
|
|
|
/* Deference the object and set failure status */
|
|
|
|
ObDereferenceObject(SourceObject);
|
|
|
|
Status = STATUS_INSUFFICIENT_RESOURCES;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Return the handle */
|
|
|
|
if (TargetHandle) *TargetHandle = NewHandle;
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2007-01-10 03:35:59 +00:00
|
|
|
/* Dereference handle tables */
|
|
|
|
ObDereferenceProcessHandleTable(SourceProcess);
|
|
|
|
ObDereferenceProcessHandleTable(TargetProcess);
|
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Return status */
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
|
|
|
"%s - Duplicated handle: %lx for %p into %p. Source: %p HC PC %lx %lx\n",
|
2006-06-11 06:52:56 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
NewHandle,
|
|
|
|
SourceProcess,
|
|
|
|
TargetProcess,
|
|
|
|
SourceObject,
|
|
|
|
ObjectHeader->PointerCount,
|
|
|
|
ObjectHeader->HandleCount);
|
2006-05-24 22:07:11 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PUBLIC FUNCTIONS *********************************************************/
|
|
|
|
|
2006-06-08 06:31:16 +00:00
|
|
|
/*++
|
|
|
|
* @name ObOpenObjectByName
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The ObOpenObjectByName routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param ObjectAttributes
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param ObjectType
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param AccessMode
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param PassedAccessState
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param DesiredAccess
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param ParseContext
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param Handle
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
2006-05-25 04:17:29 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2006-05-24 22:07:11 +00:00
|
|
|
ObOpenObjectByName(IN POBJECT_ATTRIBUTES ObjectAttributes,
|
|
|
|
IN POBJECT_TYPE ObjectType,
|
|
|
|
IN KPROCESSOR_MODE AccessMode,
|
|
|
|
IN PACCESS_STATE PassedAccessState,
|
2006-05-25 20:50:58 +00:00
|
|
|
IN ACCESS_MASK DesiredAccess,
|
|
|
|
IN OUT PVOID ParseContext,
|
2006-05-24 22:07:11 +00:00
|
|
|
OUT PHANDLE Handle)
|
|
|
|
{
|
|
|
|
PVOID Object = NULL;
|
|
|
|
UNICODE_STRING ObjectName;
|
|
|
|
NTSTATUS Status;
|
2006-06-07 05:37:04 +00:00
|
|
|
POBJECT_HEADER ObjectHeader;
|
2006-05-25 04:17:29 +00:00
|
|
|
PGENERIC_MAPPING GenericMapping = NULL;
|
2006-06-07 05:37:04 +00:00
|
|
|
OB_OPEN_REASON OpenReason;
|
2007-01-08 08:03:47 +00:00
|
|
|
POB_TEMP_BUFFER TempBuffer;
|
2006-05-24 22:07:11 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Assume failure */
|
|
|
|
*Handle = NULL;
|
|
|
|
|
2006-06-27 03:06:26 +00:00
|
|
|
/* Check if we didn't get any Object Attributes */
|
|
|
|
if (!ObjectAttributes)
|
|
|
|
{
|
|
|
|
/* Fail with special status code */
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2007-01-08 08:03:47 +00:00
|
|
|
/* Allocate the temporary buffer */
|
|
|
|
TempBuffer = ExAllocatePoolWithTag(NonPagedPool,
|
|
|
|
sizeof(OB_TEMP_BUFFER),
|
|
|
|
TAG_OB_TEMP_STORAGE);
|
|
|
|
if (!TempBuffer) return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
|
2006-05-24 22:07:11 +00:00
|
|
|
/* Capture all the info */
|
2008-06-18 12:28:29 +00:00
|
|
|
Status = ObpCaptureObjectCreateInformation(ObjectAttributes,
|
|
|
|
AccessMode,
|
|
|
|
TRUE,
|
|
|
|
&TempBuffer->ObjectCreateInfo,
|
|
|
|
&ObjectName);
|
2007-01-15 18:37:53 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Fail */
|
2008-08-31 15:29:21 +00:00
|
|
|
ExFreePoolWithTag(TempBuffer, TAG_OB_TEMP_STORAGE);
|
2007-01-15 18:37:53 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-05-25 04:17:29 +00:00
|
|
|
/* Check if we didn't get an access state */
|
|
|
|
if (!PassedAccessState)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-05-25 04:17:29 +00:00
|
|
|
/* Try to get the generic mapping if we can */
|
|
|
|
if (ObjectType) GenericMapping = &ObjectType->TypeInfo.GenericMapping;
|
|
|
|
|
|
|
|
/* Use our built-in access state */
|
2007-01-08 08:03:47 +00:00
|
|
|
PassedAccessState = &TempBuffer->LocalAccessState;
|
|
|
|
Status = SeCreateAccessState(&TempBuffer->LocalAccessState,
|
|
|
|
&TempBuffer->AuxData,
|
2006-05-25 04:17:29 +00:00
|
|
|
DesiredAccess,
|
|
|
|
GenericMapping);
|
|
|
|
if (!NT_SUCCESS(Status)) goto Quickie;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-05-25 04:17:29 +00:00
|
|
|
/* Get the security descriptor */
|
2007-01-08 08:03:47 +00:00
|
|
|
if (TempBuffer->ObjectCreateInfo.SecurityDescriptor)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-05-25 04:17:29 +00:00
|
|
|
/* Save it in the access state */
|
|
|
|
PassedAccessState->SecurityDescriptor =
|
2007-01-08 08:03:47 +00:00
|
|
|
TempBuffer->ObjectCreateInfo.SecurityDescriptor;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
2006-05-25 04:17:29 +00:00
|
|
|
|
|
|
|
/* Now do the lookup */
|
2007-01-08 08:03:47 +00:00
|
|
|
Status = ObpLookupObjectName(TempBuffer->ObjectCreateInfo.RootDirectory,
|
|
|
|
&ObjectName,
|
|
|
|
TempBuffer->ObjectCreateInfo.Attributes,
|
|
|
|
ObjectType,
|
|
|
|
AccessMode,
|
|
|
|
ParseContext,
|
|
|
|
TempBuffer->ObjectCreateInfo.SecurityQos,
|
|
|
|
NULL,
|
|
|
|
PassedAccessState,
|
|
|
|
&TempBuffer->LookupContext,
|
|
|
|
&Object);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Cleanup after lookup */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpReleaseLookupContext(&TempBuffer->LookupContext);
|
2007-01-08 08:03:47 +00:00
|
|
|
goto Cleanup;
|
|
|
|
}
|
2006-05-25 04:17:29 +00:00
|
|
|
|
2006-06-07 05:37:04 +00:00
|
|
|
/* Check if this object has create information */
|
|
|
|
ObjectHeader = OBJECT_TO_OBJECT_HEADER(Object);
|
|
|
|
if (ObjectHeader->Flags & OB_FLAG_CREATE_INFO)
|
|
|
|
{
|
|
|
|
/* Then we are creating a new handle */
|
|
|
|
OpenReason = ObCreateHandle;
|
2006-06-08 20:58:21 +00:00
|
|
|
|
|
|
|
/* Check if we still have create info */
|
|
|
|
if (ObjectHeader->ObjectCreateInfo)
|
|
|
|
{
|
|
|
|
/* Free it */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpFreeObjectCreateInformation(ObjectHeader->
|
2006-10-19 03:09:19 +00:00
|
|
|
ObjectCreateInfo);
|
|
|
|
ObjectHeader->ObjectCreateInfo = NULL;
|
2006-06-08 20:58:21 +00:00
|
|
|
}
|
2006-06-07 05:37:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, we are merely opening it */
|
|
|
|
OpenReason = ObOpenHandle;
|
|
|
|
}
|
|
|
|
|
2007-01-08 08:03:47 +00:00
|
|
|
/* Check if we have invalid object attributes */
|
|
|
|
if (ObjectHeader->Type->TypeInfo.InvalidAttributes &
|
|
|
|
TempBuffer->ObjectCreateInfo.Attributes)
|
|
|
|
{
|
|
|
|
/* Set failure code */
|
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
2007-01-09 09:07:36 +00:00
|
|
|
|
|
|
|
/* Cleanup after lookup */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpReleaseLookupContext(&TempBuffer->LookupContext);
|
2007-01-15 18:37:53 +00:00
|
|
|
|
|
|
|
/* Dereference the object */
|
|
|
|
ObDereferenceObject(Object);
|
2007-01-08 08:03:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Create the actual handle now */
|
|
|
|
Status = ObpCreateHandle(OpenReason,
|
|
|
|
Object,
|
|
|
|
ObjectType,
|
|
|
|
PassedAccessState,
|
|
|
|
0,
|
|
|
|
TempBuffer->ObjectCreateInfo.Attributes,
|
|
|
|
&TempBuffer->LookupContext,
|
|
|
|
AccessMode,
|
|
|
|
NULL,
|
|
|
|
Handle);
|
|
|
|
if (!NT_SUCCESS(Status)) ObDereferenceObject(Object);
|
|
|
|
}
|
2006-05-24 22:07:11 +00:00
|
|
|
|
|
|
|
Cleanup:
|
2006-05-25 04:17:29 +00:00
|
|
|
/* Delete the access state */
|
2007-01-08 08:03:47 +00:00
|
|
|
if (PassedAccessState == &TempBuffer->LocalAccessState)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-05-25 04:17:29 +00:00
|
|
|
SeDeleteAccessState(PassedAccessState);
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-05-25 04:17:29 +00:00
|
|
|
Quickie:
|
2007-01-08 08:03:47 +00:00
|
|
|
/* Release the object attributes and temporary buffer */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpReleaseObjectCreateInformation(&TempBuffer->ObjectCreateInfo);
|
2007-01-08 21:02:22 +00:00
|
|
|
if (ObjectName.Buffer) ObpFreeObjectNameBuffer(&ObjectName);
|
2008-08-31 15:29:21 +00:00
|
|
|
ExFreePoolWithTag(TempBuffer, TAG_OB_TEMP_STORAGE);
|
2007-01-08 08:03:47 +00:00
|
|
|
|
|
|
|
/* Return status */
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
2006-06-27 01:16:17 +00:00
|
|
|
"%s - returning Object %p with PC S: %lx %lx\n",
|
2006-06-07 05:04:43 +00:00
|
|
|
__FUNCTION__,
|
2006-06-27 01:16:17 +00:00
|
|
|
Object,
|
|
|
|
Object ? OBJECT_TO_OBJECT_HEADER(Object)->PointerCount : -1,
|
2006-06-07 05:04:43 +00:00
|
|
|
Status);
|
2006-05-24 22:07:11 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2006-06-08 06:31:16 +00:00
|
|
|
/*++
|
|
|
|
* @name ObOpenObjectByPointer
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The ObOpenObjectByPointer routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param Object
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param HandleAttributes
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param PassedAccessState
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param DesiredAccess
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param ObjectType
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param AccessMode
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param Handle
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
2006-05-25 04:17:29 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2006-05-24 22:07:11 +00:00
|
|
|
ObOpenObjectByPointer(IN PVOID Object,
|
|
|
|
IN ULONG HandleAttributes,
|
|
|
|
IN PACCESS_STATE PassedAccessState,
|
|
|
|
IN ACCESS_MASK DesiredAccess,
|
|
|
|
IN POBJECT_TYPE ObjectType,
|
|
|
|
IN KPROCESSOR_MODE AccessMode,
|
|
|
|
OUT PHANDLE Handle)
|
|
|
|
{
|
2006-06-30 21:34:53 +00:00
|
|
|
POBJECT_HEADER Header;
|
2006-05-24 22:07:11 +00:00
|
|
|
NTSTATUS Status;
|
2006-06-07 05:04:43 +00:00
|
|
|
ACCESS_STATE AccessState;
|
2008-06-16 08:54:21 +00:00
|
|
|
AUX_ACCESS_DATA AuxData;
|
2006-05-24 22:07:11 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Assume failure */
|
|
|
|
*Handle = NULL;
|
2006-06-30 21:34:53 +00:00
|
|
|
|
2006-05-25 04:17:29 +00:00
|
|
|
/* Reference the object */
|
2006-05-24 22:07:11 +00:00
|
|
|
Status = ObReferenceObjectByPointer(Object,
|
2006-05-25 04:17:29 +00:00
|
|
|
0,
|
|
|
|
ObjectType,
|
|
|
|
AccessMode);
|
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Get the Header Info */
|
|
|
|
Header = OBJECT_TO_OBJECT_HEADER(Object);
|
|
|
|
|
2006-06-07 05:04:43 +00:00
|
|
|
/* Check if we didn't get an access state */
|
|
|
|
if (!PassedAccessState)
|
|
|
|
{
|
|
|
|
/* Use our built-in access state */
|
|
|
|
PassedAccessState = &AccessState;
|
|
|
|
Status = SeCreateAccessState(&AccessState,
|
|
|
|
&AuxData,
|
|
|
|
DesiredAccess,
|
2006-06-30 21:34:53 +00:00
|
|
|
&Header->Type->TypeInfo.GenericMapping);
|
2006-06-07 05:04:43 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
ObDereferenceObject(Object);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-15 18:37:53 +00:00
|
|
|
/* Check if we have invalid object attributes */
|
|
|
|
if (Header->Type->TypeInfo.InvalidAttributes & HandleAttributes)
|
|
|
|
{
|
|
|
|
/* Delete the access state */
|
|
|
|
if (PassedAccessState == &AccessState)
|
|
|
|
{
|
|
|
|
SeDeleteAccessState(PassedAccessState);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dereference the object */
|
|
|
|
ObDereferenceObject(Object);
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2006-05-25 04:17:29 +00:00
|
|
|
/* Create the handle */
|
2006-06-07 05:37:04 +00:00
|
|
|
Status = ObpCreateHandle(ObOpenHandle,
|
|
|
|
Object,
|
|
|
|
ObjectType,
|
2006-06-07 05:04:43 +00:00
|
|
|
PassedAccessState,
|
2006-06-07 05:37:04 +00:00
|
|
|
0,
|
2006-05-25 04:17:29 +00:00
|
|
|
HandleAttributes,
|
2007-01-08 08:03:47 +00:00
|
|
|
NULL,
|
2006-06-07 05:37:04 +00:00
|
|
|
AccessMode,
|
|
|
|
NULL,
|
2006-05-25 04:17:29 +00:00
|
|
|
Handle);
|
2006-06-14 04:02:02 +00:00
|
|
|
if (!NT_SUCCESS(Status)) ObDereferenceObject(Object);
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-07 05:04:43 +00:00
|
|
|
/* Delete the access state */
|
|
|
|
if (PassedAccessState == &AccessState)
|
|
|
|
{
|
|
|
|
SeDeleteAccessState(PassedAccessState);
|
|
|
|
}
|
|
|
|
|
2006-06-07 05:37:04 +00:00
|
|
|
/* Return */
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
|
|
|
"%s - returning Object with PC S: %lx %lx\n",
|
2006-06-07 05:04:43 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
OBJECT_TO_OBJECT_HEADER(Object)->PointerCount,
|
|
|
|
Status);
|
2006-05-25 04:17:29 +00:00
|
|
|
return Status;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2007-01-08 19:40:10 +00:00
|
|
|
/*++
|
|
|
|
* @name ObFindHandleForObject
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The ObFindHandleForObject routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param Process
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param Object
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param ObjectType
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param HandleInformation
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param HandleReturn
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
2006-05-24 22:07:11 +00:00
|
|
|
ObFindHandleForObject(IN PEPROCESS Process,
|
|
|
|
IN PVOID Object,
|
|
|
|
IN POBJECT_TYPE ObjectType,
|
|
|
|
IN POBJECT_HANDLE_INFORMATION HandleInformation,
|
2007-01-08 19:40:10 +00:00
|
|
|
OUT PHANDLE Handle)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2007-01-08 19:40:10 +00:00
|
|
|
OBP_FIND_HANDLE_DATA FindData;
|
|
|
|
BOOLEAN Result = FALSE;
|
2007-02-14 17:51:48 +00:00
|
|
|
PVOID ObjectTable;
|
2007-01-08 19:40:10 +00:00
|
|
|
|
|
|
|
/* Make sure we have an object table */
|
2007-02-14 17:51:48 +00:00
|
|
|
ObjectTable = ObReferenceProcessHandleTable(Process);
|
|
|
|
if (ObjectTable)
|
2007-01-08 19:40:10 +00:00
|
|
|
{
|
|
|
|
/* Check if we have an object */
|
|
|
|
if (Object)
|
|
|
|
{
|
|
|
|
/* Set its header */
|
|
|
|
FindData.ObjectHeader = OBJECT_TO_OBJECT_HEADER(Object);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, no object to match*/
|
|
|
|
FindData.ObjectHeader = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set other information */
|
|
|
|
FindData.ObjectType = ObjectType;
|
|
|
|
FindData.HandleInformation = HandleInformation;
|
|
|
|
|
|
|
|
/* Enumerate the handle table */
|
|
|
|
if (ExEnumHandleTable(Process->ObjectTable,
|
|
|
|
ObpEnumFindHandleProcedure,
|
|
|
|
&FindData,
|
|
|
|
Handle))
|
|
|
|
{
|
|
|
|
/* Set success */
|
|
|
|
Result = TRUE;
|
|
|
|
}
|
2007-02-14 17:51:48 +00:00
|
|
|
|
|
|
|
/* Let go of the table */
|
|
|
|
ObDereferenceProcessHandleTable(Process);
|
2007-01-08 19:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the result */
|
|
|
|
return Result;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-08 06:31:16 +00:00
|
|
|
/*++
|
|
|
|
* @name ObInsertObject
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The ObInsertObject routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param Object
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param PassedAccessState
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param DesiredAccess
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param AdditionalReferences
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param ReferencedObject
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param Handle
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
2006-06-08 02:56:59 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2006-05-24 22:07:11 +00:00
|
|
|
ObInsertObject(IN PVOID Object,
|
2007-01-08 08:03:47 +00:00
|
|
|
IN PACCESS_STATE AccessState OPTIONAL,
|
2006-05-24 22:07:11 +00:00
|
|
|
IN ACCESS_MASK DesiredAccess,
|
2007-01-08 08:03:47 +00:00
|
|
|
IN ULONG ObjectPointerBias,
|
|
|
|
OUT PVOID *NewObject OPTIONAL,
|
2006-05-24 22:07:11 +00:00
|
|
|
OUT PHANDLE Handle)
|
|
|
|
{
|
|
|
|
POBJECT_CREATE_INFORMATION ObjectCreateInfo;
|
2007-01-08 08:03:47 +00:00
|
|
|
POBJECT_HEADER ObjectHeader;
|
2006-06-08 02:56:59 +00:00
|
|
|
POBJECT_TYPE ObjectType;
|
2007-01-08 08:03:47 +00:00
|
|
|
PUNICODE_STRING ObjectName;
|
|
|
|
PVOID InsertObject;
|
|
|
|
PSECURITY_DESCRIPTOR ParentDescriptor = NULL;
|
|
|
|
BOOLEAN SdAllocated = FALSE;
|
2006-05-28 19:05:19 +00:00
|
|
|
POBJECT_HEADER_NAME_INFO ObjectNameInfo;
|
2007-01-08 08:03:47 +00:00
|
|
|
OBP_LOOKUP_CONTEXT Context;
|
|
|
|
ACCESS_STATE LocalAccessState;
|
2008-06-16 08:54:21 +00:00
|
|
|
AUX_ACCESS_DATA AuxData;
|
2007-01-08 08:03:47 +00:00
|
|
|
OB_OPEN_REASON OpenReason;
|
|
|
|
KPROCESSOR_MODE PreviousMode;
|
|
|
|
NTSTATUS Status = STATUS_SUCCESS, RealStatus;
|
2007-01-09 08:38:07 +00:00
|
|
|
BOOLEAN IsNewObject;
|
2006-05-24 22:07:11 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
2007-01-08 08:03:47 +00:00
|
|
|
/* Get the Header */
|
|
|
|
ObjectHeader = OBJECT_TO_OBJECT_HEADER(Object);
|
|
|
|
|
|
|
|
/* Detect invalid insert */
|
|
|
|
if (!(ObjectHeader->Flags & OB_FLAG_CREATE_INFO))
|
|
|
|
{
|
|
|
|
/* Display warning and break into debugger */
|
|
|
|
DPRINT1("OB: Attempting to insert existing object %08x\n", Object);
|
|
|
|
DbgBreakPoint();
|
|
|
|
|
|
|
|
/* Allow debugger to continue */
|
|
|
|
ObDereferenceObject(Object);
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the create and name info, as well as the object type */
|
|
|
|
ObjectCreateInfo = ObjectHeader->ObjectCreateInfo;
|
2008-06-18 12:28:29 +00:00
|
|
|
ObjectNameInfo = ObpReferenceNameInfo(ObjectHeader);
|
2007-01-08 08:03:47 +00:00
|
|
|
ObjectType = ObjectHeader->Type;
|
2007-01-15 20:24:40 +00:00
|
|
|
ObjectName = NULL;
|
2006-06-08 02:56:59 +00:00
|
|
|
|
2006-06-08 06:17:46 +00:00
|
|
|
/* Check if this is an named object */
|
2007-01-08 08:03:47 +00:00
|
|
|
if ((ObjectNameInfo) && (ObjectNameInfo->Name.Buffer))
|
|
|
|
{
|
|
|
|
/* Get the object name */
|
|
|
|
ObjectName = &ObjectNameInfo->Name;
|
|
|
|
}
|
|
|
|
|
2007-01-09 08:38:07 +00:00
|
|
|
/* Sanity check */
|
2007-01-08 08:03:47 +00:00
|
|
|
ASSERT((Handle) ||
|
|
|
|
((ObjectPointerBias == 0) &&
|
|
|
|
(ObjectName == NULL) &&
|
|
|
|
(ObjectType->TypeInfo.SecurityRequired) &&
|
|
|
|
(NewObject == NULL)));
|
2006-06-08 06:17:46 +00:00
|
|
|
|
|
|
|
/* Check if the object is unnamed and also doesn't have security */
|
2007-01-08 08:03:47 +00:00
|
|
|
PreviousMode = KeGetPreviousMode();
|
|
|
|
if (!(ObjectType->TypeInfo.SecurityRequired) && !(ObjectName))
|
2006-06-08 06:17:46 +00:00
|
|
|
{
|
2007-01-09 08:38:07 +00:00
|
|
|
/* Assume failure */
|
|
|
|
*Handle = NULL;
|
2007-01-15 20:24:40 +00:00
|
|
|
ObjectHeader->ObjectCreateInfo = NULL;
|
2006-06-10 00:41:16 +00:00
|
|
|
|
2007-01-09 08:38:07 +00:00
|
|
|
/* Create the handle */
|
|
|
|
Status = ObpCreateUnnamedHandle(Object,
|
|
|
|
DesiredAccess,
|
|
|
|
ObjectPointerBias + 1,
|
|
|
|
ObjectCreateInfo->Attributes,
|
|
|
|
PreviousMode,
|
|
|
|
NewObject,
|
|
|
|
Handle);
|
2006-06-10 00:41:16 +00:00
|
|
|
|
|
|
|
/* Free the create information */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpFreeObjectCreateInformation(ObjectCreateInfo);
|
2007-01-08 08:03:47 +00:00
|
|
|
|
2007-01-15 20:24:40 +00:00
|
|
|
/* Release the object name information */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpDereferenceNameInfo(ObjectNameInfo);
|
2006-06-10 00:41:16 +00:00
|
|
|
|
|
|
|
/* Remove the extra keep-alive reference */
|
2007-01-09 08:38:07 +00:00
|
|
|
ObDereferenceObject(Object);
|
2006-06-10 00:41:16 +00:00
|
|
|
|
|
|
|
/* Return */
|
2006-06-27 01:52:16 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
|
|
|
"%s - returning Object with PC S: %lx %lx\n",
|
|
|
|
__FUNCTION__,
|
2007-01-08 08:03:47 +00:00
|
|
|
ObjectHeader->PointerCount,
|
2006-06-27 01:52:16 +00:00
|
|
|
Status);
|
2006-06-10 00:41:16 +00:00
|
|
|
return Status;
|
2006-06-08 06:17:46 +00:00
|
|
|
}
|
|
|
|
|
2006-06-08 02:56:59 +00:00
|
|
|
/* Check if we didn't get an access state */
|
2007-01-08 08:03:47 +00:00
|
|
|
if (!AccessState)
|
2006-06-08 02:56:59 +00:00
|
|
|
{
|
|
|
|
/* Use our built-in access state */
|
2007-01-08 08:03:47 +00:00
|
|
|
AccessState = &LocalAccessState;
|
|
|
|
Status = SeCreateAccessState(&LocalAccessState,
|
2006-06-08 02:56:59 +00:00
|
|
|
&AuxData,
|
|
|
|
DesiredAccess,
|
|
|
|
&ObjectType->TypeInfo.GenericMapping);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Fail */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpDereferenceNameInfo(ObjectNameInfo);
|
2006-06-08 02:56:59 +00:00
|
|
|
ObDereferenceObject(Object);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-08 06:17:46 +00:00
|
|
|
/* Save the security descriptor */
|
2007-01-08 08:03:47 +00:00
|
|
|
AccessState->SecurityDescriptor = ObjectCreateInfo->SecurityDescriptor;
|
|
|
|
|
|
|
|
/* Validate the access mask */
|
2008-09-24 17:32:14 +00:00
|
|
|
Status = ObpValidateAccessMask(AccessState);
|
2007-01-08 08:03:47 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Fail */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpDereferenceNameInfo(ObjectNameInfo);
|
2007-01-08 08:03:47 +00:00
|
|
|
ObDereferenceObject(Object);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup a lookup context */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpInitializeLookupContext(&Context);
|
2007-01-08 08:03:47 +00:00
|
|
|
InsertObject = Object;
|
|
|
|
OpenReason = ObCreateHandle;
|
2006-06-08 02:56:59 +00:00
|
|
|
|
|
|
|
/* Check if the object is named */
|
2007-01-08 08:03:47 +00:00
|
|
|
if (ObjectName)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-08 02:56:59 +00:00
|
|
|
/* Look it up */
|
2007-01-08 08:03:47 +00:00
|
|
|
Status = ObpLookupObjectName(ObjectCreateInfo->RootDirectory,
|
|
|
|
ObjectName,
|
|
|
|
ObjectCreateInfo->Attributes,
|
|
|
|
ObjectType,
|
|
|
|
(ObjectHeader->Flags & OB_FLAG_KERNEL_MODE) ?
|
|
|
|
KernelMode : UserMode,
|
|
|
|
ObjectCreateInfo->ParseContext,
|
|
|
|
ObjectCreateInfo->SecurityQos,
|
|
|
|
Object,
|
|
|
|
AccessState,
|
|
|
|
&Context,
|
|
|
|
&InsertObject);
|
|
|
|
|
2006-06-08 06:17:46 +00:00
|
|
|
/* Check if we found an object that doesn't match the one requested */
|
2007-01-08 08:03:47 +00:00
|
|
|
if ((NT_SUCCESS(Status)) && (InsertObject) && (Object != InsertObject))
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-08 06:17:46 +00:00
|
|
|
/* This means we're opening an object, not creating a new one */
|
|
|
|
OpenReason = ObOpenHandle;
|
|
|
|
|
|
|
|
/* Make sure the caller said it's OK to do this */
|
|
|
|
if (ObjectCreateInfo->Attributes & OBJ_OPENIF)
|
|
|
|
{
|
|
|
|
/* He did, but did he want this type? */
|
2007-01-08 08:03:47 +00:00
|
|
|
if (ObjectType != OBJECT_TO_OBJECT_HEADER(InsertObject)->Type)
|
2006-06-08 06:17:46 +00:00
|
|
|
{
|
|
|
|
/* Wrong type, so fail */
|
|
|
|
Status = STATUS_OBJECT_TYPE_MISMATCH;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Right type, so warn */
|
|
|
|
Status = STATUS_OBJECT_NAME_EXISTS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-02-14 17:51:48 +00:00
|
|
|
/* Check if this was a symbolic link */
|
|
|
|
if (OBJECT_TO_OBJECT_HEADER(InsertObject)->Type ==
|
|
|
|
ObSymbolicLinkType)
|
|
|
|
{
|
|
|
|
/* Dereference it */
|
|
|
|
ObDereferenceObject(InsertObject);
|
|
|
|
}
|
|
|
|
|
2006-06-08 06:17:46 +00:00
|
|
|
/* Caller wanted to create a new object, fail */
|
|
|
|
Status = STATUS_OBJECT_NAME_COLLISION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if anything until now failed */
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2007-01-10 01:00:46 +00:00
|
|
|
/* Cleanup after lookup */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpReleaseLookupContext(&Context);
|
2007-01-10 01:00:46 +00:00
|
|
|
|
2007-01-09 07:28:40 +00:00
|
|
|
/* Remove query reference that we added */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpDereferenceNameInfo(ObjectNameInfo);
|
2007-01-09 07:28:40 +00:00
|
|
|
|
|
|
|
/* Dereference the object and delete the access state */
|
2006-06-08 06:17:46 +00:00
|
|
|
ObDereferenceObject(Object);
|
2007-01-08 08:03:47 +00:00
|
|
|
if (AccessState == &LocalAccessState)
|
2006-06-08 06:17:46 +00:00
|
|
|
{
|
|
|
|
/* We used a local one; delete it */
|
2007-01-08 08:03:47 +00:00
|
|
|
SeDeleteAccessState(AccessState);
|
2006-06-08 06:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return failure code */
|
|
|
|
return Status;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
2007-01-08 08:03:47 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Check if this is a symbolic link */
|
|
|
|
if (ObjectType == ObSymbolicLinkType)
|
|
|
|
{
|
|
|
|
/* Create the internal name */
|
2007-01-08 19:52:49 +00:00
|
|
|
ObpCreateSymbolicLinkName(Object);
|
2007-01-08 08:03:47 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-08 06:17:46 +00:00
|
|
|
/* Now check if this object is being created */
|
2007-01-08 08:03:47 +00:00
|
|
|
if (InsertObject == Object)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-08 06:17:46 +00:00
|
|
|
/* Check if it's named or forces security */
|
2007-01-08 08:03:47 +00:00
|
|
|
if ((ObjectName) || (ObjectType->TypeInfo.SecurityRequired))
|
2006-06-08 05:41:39 +00:00
|
|
|
{
|
2006-06-08 06:17:46 +00:00
|
|
|
/* Make sure it's inserted into an object directory */
|
|
|
|
if ((ObjectNameInfo) && (ObjectNameInfo->Directory))
|
|
|
|
{
|
|
|
|
/* Get the current descriptor */
|
|
|
|
ObGetObjectSecurity(ObjectNameInfo->Directory,
|
2007-01-08 08:03:47 +00:00
|
|
|
&ParentDescriptor,
|
2006-06-08 06:17:46 +00:00
|
|
|
&SdAllocated);
|
|
|
|
}
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-08 06:17:46 +00:00
|
|
|
/* Now assign it */
|
2007-01-08 08:03:47 +00:00
|
|
|
Status = ObAssignSecurity(AccessState,
|
|
|
|
ParentDescriptor,
|
2006-06-08 06:17:46 +00:00
|
|
|
Object,
|
|
|
|
ObjectType);
|
2006-06-08 05:41:39 +00:00
|
|
|
|
2006-06-08 06:17:46 +00:00
|
|
|
/* Check if we captured one */
|
2007-01-08 08:03:47 +00:00
|
|
|
if (ParentDescriptor)
|
2006-06-08 06:17:46 +00:00
|
|
|
{
|
|
|
|
/* We did, release it */
|
2007-01-08 08:03:47 +00:00
|
|
|
ObReleaseObjectSecurity(ParentDescriptor, SdAllocated);
|
2006-06-08 06:17:46 +00:00
|
|
|
}
|
|
|
|
else if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Other we didn't, but we were able to use the current SD */
|
|
|
|
SeReleaseSecurityDescriptor(ObjectCreateInfo->SecurityDescriptor,
|
|
|
|
ObjectCreateInfo->ProbeMode,
|
|
|
|
TRUE);
|
|
|
|
|
|
|
|
/* Clear the current one */
|
2007-01-08 08:03:47 +00:00
|
|
|
AccessState->SecurityDescriptor =
|
|
|
|
ObjectCreateInfo->SecurityDescriptor = NULL;
|
2006-06-08 06:17:46 +00:00
|
|
|
}
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-08 06:17:46 +00:00
|
|
|
/* Check if anything until now failed */
|
|
|
|
if (!NT_SUCCESS(Status))
|
2006-06-08 05:41:39 +00:00
|
|
|
{
|
2007-01-15 20:24:40 +00:00
|
|
|
/* Check if the directory was added */
|
|
|
|
if (Context.DirectoryLocked)
|
|
|
|
{
|
|
|
|
/* Weird case where we need to do a manual delete */
|
|
|
|
DPRINT1("Unhandled path\n");
|
2008-08-24 15:48:05 +00:00
|
|
|
ASSERT(FALSE);
|
2007-01-15 20:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleanup the lookup */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpReleaseLookupContext(&Context);
|
2007-01-10 01:00:46 +00:00
|
|
|
|
2007-01-09 07:28:40 +00:00
|
|
|
/* Remove query reference that we added */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpDereferenceNameInfo(ObjectNameInfo);
|
2007-01-09 07:28:40 +00:00
|
|
|
|
|
|
|
/* Dereference the object and delete the access state */
|
2006-06-08 06:17:46 +00:00
|
|
|
ObDereferenceObject(Object);
|
2007-01-08 08:03:47 +00:00
|
|
|
if (AccessState == &LocalAccessState)
|
2006-06-08 06:17:46 +00:00
|
|
|
{
|
|
|
|
/* We used a local one; delete it */
|
2007-01-08 08:03:47 +00:00
|
|
|
SeDeleteAccessState(AccessState);
|
2006-06-08 06:17:46 +00:00
|
|
|
}
|
2006-06-08 05:41:39 +00:00
|
|
|
|
2006-06-08 06:17:46 +00:00
|
|
|
/* Return failure code */
|
2008-08-24 15:48:05 +00:00
|
|
|
ASSERT(FALSE);
|
2006-06-08 06:17:46 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2006-06-08 05:41:39 +00:00
|
|
|
}
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2006-06-27 01:52:16 +00:00
|
|
|
/* Save the actual status until here */
|
|
|
|
RealStatus = Status;
|
|
|
|
|
2007-01-09 08:38:07 +00:00
|
|
|
/* Check if caller wants us to create a handle */
|
2007-01-08 08:03:47 +00:00
|
|
|
ObjectHeader->ObjectCreateInfo = NULL;
|
2006-06-08 05:41:39 +00:00
|
|
|
if (Handle)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-08 02:56:59 +00:00
|
|
|
/* Create the handle */
|
|
|
|
Status = ObpCreateHandle(OpenReason,
|
2007-01-08 08:03:47 +00:00
|
|
|
InsertObject,
|
2006-06-07 05:37:04 +00:00
|
|
|
NULL,
|
2007-01-08 08:03:47 +00:00
|
|
|
AccessState,
|
|
|
|
ObjectPointerBias + 1,
|
2006-06-07 05:37:04 +00:00
|
|
|
ObjectCreateInfo->Attributes,
|
2007-01-08 08:03:47 +00:00
|
|
|
&Context,
|
|
|
|
PreviousMode,
|
|
|
|
NewObject,
|
2006-06-07 05:37:04 +00:00
|
|
|
Handle);
|
2007-01-09 08:38:07 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* If the object had a name, backout everything */
|
|
|
|
if (ObjectName) ObpDeleteNameCheck(Object);
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2007-01-09 08:38:07 +00:00
|
|
|
/* Return the status of the failure */
|
|
|
|
*Handle = NULL;
|
|
|
|
RealStatus = Status;
|
|
|
|
}
|
2006-06-08 02:56:59 +00:00
|
|
|
|
2007-01-09 08:38:07 +00:00
|
|
|
/* Remove a query reference */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpDereferenceNameInfo(ObjectNameInfo);
|
2007-01-09 08:38:07 +00:00
|
|
|
|
|
|
|
/* Remove the extra keep-alive reference */
|
|
|
|
ObDereferenceObject(Object);
|
2006-06-27 01:52:16 +00:00
|
|
|
}
|
2007-01-09 08:38:07 +00:00
|
|
|
else
|
|
|
|
{
|
2008-04-01 20:07:36 +00:00
|
|
|
/* Otherwise, lock the object */
|
|
|
|
ObpAcquireObjectLock(ObjectHeader);
|
2006-06-08 02:56:59 +00:00
|
|
|
|
2007-01-09 08:38:07 +00:00
|
|
|
/* And charge quota for the process to make it appear as used */
|
|
|
|
RealStatus = ObpChargeQuotaForObject(ObjectHeader,
|
|
|
|
ObjectType,
|
|
|
|
&IsNewObject);
|
2007-01-08 08:03:47 +00:00
|
|
|
|
2007-01-09 08:38:07 +00:00
|
|
|
/* Release the lock */
|
2008-04-01 20:07:36 +00:00
|
|
|
ObpReleaseObjectLock(ObjectHeader);
|
2007-01-09 08:38:07 +00:00
|
|
|
|
|
|
|
/* Check if we failed and dereference the object if so */
|
|
|
|
if (!NT_SUCCESS(RealStatus)) ObDereferenceObject(Object);
|
|
|
|
}
|
2007-01-08 08:03:47 +00:00
|
|
|
|
|
|
|
/* We can delete the Create Info now */
|
2008-06-18 12:28:29 +00:00
|
|
|
ObpFreeObjectCreateInformation(ObjectCreateInfo);
|
2007-01-08 08:03:47 +00:00
|
|
|
|
|
|
|
/* Check if we created our own access state and delete it if so */
|
|
|
|
if (AccessState == &LocalAccessState) SeDeleteAccessState(AccessState);
|
2006-06-08 02:56:59 +00:00
|
|
|
|
2006-06-27 01:52:16 +00:00
|
|
|
/* Return status code */
|
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
2008-09-12 14:10:27 +00:00
|
|
|
"%s - returning Object with PC RS/S: %lx %lx %lx\n",
|
2006-06-27 01:52:16 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
OBJECT_TO_OBJECT_HEADER(Object)->PointerCount,
|
|
|
|
RealStatus, Status);
|
|
|
|
return RealStatus;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-08 18:24:37 +00:00
|
|
|
/*++
|
|
|
|
* @name ObCloseHandle
|
|
|
|
* @implemented NT5.1
|
|
|
|
*
|
|
|
|
* The ObCloseHandle routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param Handle
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @param AccessMode
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
2006-06-08 18:09:01 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
ObCloseHandle(IN HANDLE Handle,
|
|
|
|
IN KPROCESSOR_MODE AccessMode)
|
|
|
|
{
|
2006-10-19 02:54:48 +00:00
|
|
|
/* Call the internal API */
|
2006-06-08 18:09:01 +00:00
|
|
|
return ObpCloseHandle(Handle, AccessMode);
|
|
|
|
}
|
|
|
|
|
2006-06-08 18:24:37 +00:00
|
|
|
/*++
|
|
|
|
* @name NtClose
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The NtClose routine <FILLMEIN>
|
|
|
|
*
|
|
|
|
* @param Handle
|
|
|
|
* <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @return <FILLMEIN>.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
2006-06-08 18:09:01 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtClose(IN HANDLE Handle)
|
|
|
|
{
|
2007-01-08 21:02:22 +00:00
|
|
|
/* Call the internal API */
|
2006-06-08 18:09:01 +00:00
|
|
|
return ObpCloseHandle(Handle, ExGetPreviousMode());
|
|
|
|
}
|
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtDuplicateObject(IN HANDLE SourceProcessHandle,
|
|
|
|
IN HANDLE SourceHandle,
|
|
|
|
IN HANDLE TargetProcessHandle OPTIONAL,
|
|
|
|
OUT PHANDLE TargetHandle OPTIONAL,
|
|
|
|
IN ACCESS_MASK DesiredAccess,
|
|
|
|
IN ULONG HandleAttributes,
|
|
|
|
IN ULONG Options)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2007-04-02 15:08:54 +00:00
|
|
|
PEPROCESS SourceProcess, TargetProcess, Target;
|
2006-05-24 22:07:11 +00:00
|
|
|
HANDLE hTarget;
|
2006-06-11 06:52:56 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
2009-08-26 17:31:02 +00:00
|
|
|
NTSTATUS Status;
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
|
|
|
"%s - Duplicating handle: %lx for %lx into %lx.\n",
|
|
|
|
__FUNCTION__,
|
|
|
|
SourceHandle,
|
|
|
|
SourceProcessHandle,
|
|
|
|
TargetProcessHandle);
|
2006-05-24 22:07:11 +00:00
|
|
|
|
2007-01-15 20:24:40 +00:00
|
|
|
/* Check if we have a target handle */
|
|
|
|
if ((TargetHandle) && (PreviousMode != KernelMode))
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Enter SEH */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2007-02-14 17:51:48 +00:00
|
|
|
/* Probe the handle and assume failure */
|
2006-05-24 22:07:11 +00:00
|
|
|
ProbeForWriteHandle(TargetHandle);
|
2007-02-14 17:51:48 +00:00
|
|
|
*TargetHandle = NULL;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2009-08-26 17:31:02 +00:00
|
|
|
/* Return the exception code */
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Now reference the input handle */
|
2006-05-24 22:07:11 +00:00
|
|
|
Status = ObReferenceObjectByHandle(SourceProcessHandle,
|
2006-06-11 06:52:56 +00:00
|
|
|
PROCESS_DUP_HANDLE,
|
|
|
|
PsProcessType,
|
|
|
|
PreviousMode,
|
|
|
|
(PVOID*)&SourceProcess,
|
|
|
|
NULL);
|
|
|
|
if (!NT_SUCCESS(Status)) return(Status);
|
|
|
|
|
|
|
|
/* Check if got a target handle */
|
|
|
|
if (TargetProcessHandle)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Now reference the output handle */
|
|
|
|
Status = ObReferenceObjectByHandle(TargetProcessHandle,
|
|
|
|
PROCESS_DUP_HANDLE,
|
|
|
|
PsProcessType,
|
|
|
|
PreviousMode,
|
|
|
|
(PVOID*)&TargetProcess,
|
|
|
|
NULL);
|
|
|
|
if (NT_SUCCESS(Status))
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Use this target process */
|
|
|
|
Target = TargetProcess;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No target process */
|
|
|
|
Target = NULL;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* No target process */
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
Target = NULL;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Call the internal routine */
|
|
|
|
Status = ObDuplicateObject(SourceProcess,
|
|
|
|
SourceHandle,
|
|
|
|
Target,
|
|
|
|
&hTarget,
|
|
|
|
DesiredAccess,
|
|
|
|
HandleAttributes,
|
|
|
|
Options,
|
|
|
|
PreviousMode);
|
|
|
|
|
|
|
|
/* Check if the caller wanted the return handle */
|
|
|
|
if (TargetHandle)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Protect the write to user mode */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Write the new handle */
|
2006-05-24 22:07:11 +00:00
|
|
|
*TargetHandle = hTarget;
|
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2006-05-24 22:07:11 +00:00
|
|
|
{
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Otherwise, get the exception code */
|
2008-11-24 13:40:26 +00:00
|
|
|
Status = _SEH2_GetExceptionCode();
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2006-05-24 22:07:11 +00:00
|
|
|
}
|
|
|
|
|
2006-06-11 06:52:56 +00:00
|
|
|
/* Dereference the processes */
|
2006-06-12 05:58:08 +00:00
|
|
|
OBTRACE(OB_HANDLE_DEBUG,
|
|
|
|
"%s - Duplicated handle: %lx into %lx S %lx\n",
|
2006-06-11 06:52:56 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
hTarget,
|
|
|
|
TargetProcessHandle,
|
|
|
|
Status);
|
2007-01-15 20:24:40 +00:00
|
|
|
if (Target) ObDereferenceObject(Target);
|
2006-06-11 06:52:56 +00:00
|
|
|
ObDereferenceObject(SourceProcess);
|
2006-05-24 22:07:11 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2006-07-14 15:26:19 +00:00
|
|
|
|
|
|
|
#undef ObIsKernelHandle
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
ObIsKernelHandle(IN HANDLE Handle)
|
|
|
|
{
|
|
|
|
/* We know we're kernel mode, so just check for the kernel handle flag */
|
2008-10-18 15:04:26 +00:00
|
|
|
return (BOOLEAN)(((ULONG_PTR)Handle & KERNEL_HANDLE_FLAG) != 0);
|
2006-07-14 15:26:19 +00:00
|
|
|
}
|
|
|
|
|
2006-05-24 22:07:11 +00:00
|
|
|
/* EOF */
|