2007-11-02 23:12:22 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Kernel
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
2015-10-04 11:54:25 +00:00
|
|
|
* FILE: ntoskrnl/config/ntapi.c
|
2007-11-02 23:12:22 +00:00
|
|
|
* PURPOSE: Configuration Manager - Internal Registry APIs
|
|
|
|
* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
|
2010-09-26 11:37:40 +00:00
|
|
|
* Eric Kohl
|
2007-11-02 23:12:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
|
|
|
#include "ntoskrnl.h"
|
|
|
|
#define NDEBUG
|
|
|
|
#include "debug.h"
|
|
|
|
|
2007-12-02 18:40:33 +00:00
|
|
|
BOOLEAN CmBootAcceptFirstTime = TRUE;
|
|
|
|
BOOLEAN CmFirstTime = TRUE;
|
2014-11-04 20:47:18 +00:00
|
|
|
extern ULONG InitSafeBootMode;
|
2007-12-02 18:40:33 +00:00
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
|
|
|
|
/* PRIVATE FUNCTIONS *********************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adapted from ntoskrnl/include/internal/ob_x.h:ObpReleaseObjectCreateInformation()
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
ReleaseCapturedObjectAttributes(
|
|
|
|
_In_ POBJECT_ATTRIBUTES CapturedObjectAttributes,
|
|
|
|
_In_ KPROCESSOR_MODE AccessMode)
|
|
|
|
{
|
|
|
|
/* Check if we have a security descriptor */
|
|
|
|
if (CapturedObjectAttributes->SecurityDescriptor)
|
|
|
|
{
|
|
|
|
/* Release it */
|
|
|
|
SeReleaseSecurityDescriptor(CapturedObjectAttributes->SecurityDescriptor,
|
|
|
|
AccessMode,
|
|
|
|
TRUE);
|
|
|
|
CapturedObjectAttributes->SecurityDescriptor = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have an object name */
|
|
|
|
if (CapturedObjectAttributes->ObjectName)
|
|
|
|
{
|
|
|
|
/* Release it */
|
|
|
|
ReleaseCapturedUnicodeString(CapturedObjectAttributes->ObjectName, AccessMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adapted from ntoskrnl/ob/oblife.c:ObpCaptureObjectCreateInformation()
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
ProbeAndCaptureObjectAttributes(
|
|
|
|
_Out_ POBJECT_ATTRIBUTES CapturedObjectAttributes,
|
|
|
|
_Out_ PUNICODE_STRING ObjectName,
|
|
|
|
_In_ KPROCESSOR_MODE AccessMode,
|
|
|
|
_In_ POBJECT_ATTRIBUTES ObjectAttributes,
|
|
|
|
_In_ BOOLEAN CaptureSecurity)
|
|
|
|
{
|
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
|
|
|
PSECURITY_DESCRIPTOR SecurityDescriptor;
|
|
|
|
// PSECURITY_QUALITY_OF_SERVICE SecurityQos;
|
|
|
|
PUNICODE_STRING LocalObjectName = NULL;
|
|
|
|
|
|
|
|
/* Zero out the Capture Data */
|
|
|
|
RtlZeroMemory(CapturedObjectAttributes, sizeof(*CapturedObjectAttributes));
|
|
|
|
|
|
|
|
/* SEH everything here for protection */
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
/* Check if we got attributes */
|
|
|
|
if (ObjectAttributes)
|
|
|
|
{
|
|
|
|
/* Check if we're in user mode */
|
|
|
|
if (AccessMode != KernelMode)
|
|
|
|
{
|
|
|
|
/* Probe the attributes */
|
|
|
|
ProbeForRead(ObjectAttributes,
|
|
|
|
sizeof(OBJECT_ATTRIBUTES),
|
|
|
|
sizeof(ULONG));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate the Size and Attributes */
|
|
|
|
if ((ObjectAttributes->Length != sizeof(OBJECT_ATTRIBUTES)) ||
|
|
|
|
(ObjectAttributes->Attributes & ~OBJ_VALID_KERNEL_ATTRIBUTES)) // Understood as all the possible valid attributes
|
|
|
|
{
|
|
|
|
/* Invalid combination, fail */
|
|
|
|
_SEH2_YIELD(return STATUS_INVALID_PARAMETER);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set some Create Info and do not allow user-mode kernel handles */
|
|
|
|
CapturedObjectAttributes->Length = sizeof(OBJECT_ATTRIBUTES);
|
|
|
|
CapturedObjectAttributes->RootDirectory = ObjectAttributes->RootDirectory;
|
|
|
|
CapturedObjectAttributes->Attributes = ObpValidateAttributes(ObjectAttributes->Attributes, AccessMode);
|
|
|
|
LocalObjectName = ObjectAttributes->ObjectName;
|
|
|
|
SecurityDescriptor = ObjectAttributes->SecurityDescriptor;
|
|
|
|
// SecurityQos = ObjectAttributes->SecurityQualityOfService;
|
|
|
|
|
|
|
|
/* Check if we have a security descriptor */
|
|
|
|
if (CaptureSecurity && SecurityDescriptor)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Capture it.
|
|
|
|
* Note: This has an implicit memory barrier due
|
|
|
|
* to the function call, so cleanup is safe here.
|
|
|
|
*/
|
|
|
|
Status = SeCaptureSecurityDescriptor(SecurityDescriptor,
|
|
|
|
AccessMode,
|
|
|
|
NonPagedPool,
|
|
|
|
TRUE,
|
|
|
|
&CapturedObjectAttributes->
|
|
|
|
SecurityDescriptor);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Capture failed, quit */
|
|
|
|
CapturedObjectAttributes->SecurityDescriptor = NULL;
|
|
|
|
_SEH2_YIELD(return Status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CapturedObjectAttributes->SecurityDescriptor = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// We don't use the QoS!
|
|
|
|
|
|
|
|
/* Check if we have QoS */
|
|
|
|
if (SecurityQos)
|
|
|
|
{
|
|
|
|
/* Check if we came from user mode */
|
|
|
|
if (AccessMode != KernelMode)
|
|
|
|
{
|
|
|
|
/* Validate the QoS */
|
|
|
|
ProbeForRead(SecurityQos,
|
|
|
|
sizeof(SECURITY_QUALITY_OF_SERVICE),
|
|
|
|
sizeof(ULONG));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save Info */
|
|
|
|
CapturedObjectAttributes->SecurityQualityOfService = *SecurityQos;
|
|
|
|
CapturedObjectAttributes->SecurityQos =
|
|
|
|
&CapturedObjectAttributes->SecurityQualityOfService;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
CapturedObjectAttributes->SecurityQualityOfService = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We don't have a name */
|
|
|
|
LocalObjectName = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(ExSystemExceptionFilter())
|
|
|
|
{
|
|
|
|
/* Cleanup and return the exception code */
|
|
|
|
ReleaseCapturedObjectAttributes(CapturedObjectAttributes, AccessMode);
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
|
|
|
|
/* Now check if the Object Attributes had an Object Name */
|
|
|
|
if (LocalObjectName)
|
|
|
|
{
|
|
|
|
Status = ProbeAndCaptureUnicodeString(ObjectName, AccessMode, LocalObjectName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Clear the string */
|
|
|
|
RtlInitEmptyUnicodeString(ObjectName, NULL, 0);
|
|
|
|
|
|
|
|
/* It cannot have specified a Root Directory */
|
|
|
|
if (CapturedObjectAttributes->RootDirectory)
|
|
|
|
{
|
|
|
|
Status = STATUS_OBJECT_NAME_INVALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the caputured object attributes name pointer to the one the user gave to us */
|
|
|
|
CapturedObjectAttributes->ObjectName = ObjectName;
|
|
|
|
|
|
|
|
/* Cleanup if we failed */
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
ReleaseCapturedObjectAttributes(CapturedObjectAttributes, AccessMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return status to caller */
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
NTSTATUS
|
|
|
|
CmpConvertHandleToKernelHandle(
|
|
|
|
_In_ HANDLE SourceHandle,
|
|
|
|
_In_opt_ POBJECT_TYPE ObjectType,
|
|
|
|
_In_ ACCESS_MASK DesiredAccess,
|
|
|
|
_In_ KPROCESSOR_MODE AccessMode,
|
|
|
|
_Out_ PHANDLE KernelHandle)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
PVOID Object;
|
|
|
|
|
|
|
|
*KernelHandle = NULL;
|
|
|
|
|
|
|
|
/* NULL handle is valid */
|
|
|
|
if (SourceHandle == NULL)
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
|
|
|
/* Get the object pointer */
|
|
|
|
Status = ObReferenceObjectByHandle(SourceHandle,
|
|
|
|
DesiredAccess,
|
|
|
|
ObjectType,
|
|
|
|
AccessMode,
|
|
|
|
&Object,
|
|
|
|
NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return Status;
|
|
|
|
|
|
|
|
/* Create a kernel handle from the pointer */
|
|
|
|
Status = ObOpenObjectByPointer(Object,
|
|
|
|
OBJ_KERNEL_HANDLE,
|
|
|
|
NULL,
|
|
|
|
DesiredAccess,
|
|
|
|
ObjectType,
|
|
|
|
KernelMode,
|
|
|
|
KernelHandle);
|
|
|
|
|
|
|
|
/* Dereference the object */
|
|
|
|
ObDereferenceObject(Object);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-02 23:12:22 +00:00
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
2007-12-09 19:36:04 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtCreateKey(OUT PHANDLE KeyHandle,
|
|
|
|
IN ACCESS_MASK DesiredAccess,
|
|
|
|
IN POBJECT_ATTRIBUTES ObjectAttributes,
|
|
|
|
IN ULONG TitleIndex,
|
2009-07-10 10:02:37 +00:00
|
|
|
IN PUNICODE_STRING Class OPTIONAL,
|
2007-12-09 19:36:04 +00:00
|
|
|
IN ULONG CreateOptions,
|
2009-07-10 10:02:37 +00:00
|
|
|
OUT PULONG Disposition OPTIONAL)
|
2007-12-09 19:36:04 +00:00
|
|
|
{
|
2009-08-24 19:58:15 +00:00
|
|
|
NTSTATUS Status;
|
2007-12-09 19:36:04 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
|
|
|
CM_PARSE_CONTEXT ParseContext = {0};
|
2007-12-15 18:14:41 +00:00
|
|
|
HANDLE Handle;
|
2007-12-09 19:36:04 +00:00
|
|
|
PAGED_CODE();
|
2014-07-08 18:42:20 +00:00
|
|
|
|
|
|
|
DPRINT("NtCreateKey(Path: %wZ, Root %x, Access: %x, CreateOptions %x)\n",
|
|
|
|
ObjectAttributes->ObjectName, ObjectAttributes->RootDirectory,
|
|
|
|
DesiredAccess, CreateOptions);
|
2007-12-15 20:51:40 +00:00
|
|
|
|
2016-10-15 20:16:47 +00:00
|
|
|
/* Ignore the WOW64 flag, it's not valid in the kernel */
|
|
|
|
DesiredAccess &= ~KEY_WOW64_RES;
|
|
|
|
|
2008-06-23 12:45:27 +00:00
|
|
|
/* Check for user-mode caller */
|
2008-09-07 09:25:31 +00:00
|
|
|
if (PreviousMode != KernelMode)
|
2008-06-06 21:40:37 +00:00
|
|
|
{
|
2008-06-23 12:45:27 +00:00
|
|
|
/* Prepare to probe parameters */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2008-06-06 21:40:37 +00:00
|
|
|
{
|
|
|
|
/* Check if we have a class */
|
|
|
|
if (Class)
|
|
|
|
{
|
|
|
|
/* Probe it */
|
2009-01-04 01:50:37 +00:00
|
|
|
ParseContext.Class = ProbeForReadUnicodeString(Class);
|
2008-06-06 21:40:37 +00:00
|
|
|
ProbeForRead(ParseContext.Class.Buffer,
|
|
|
|
ParseContext.Class.Length,
|
|
|
|
sizeof(WCHAR));
|
|
|
|
}
|
2008-06-23 12:45:27 +00:00
|
|
|
|
2008-06-06 21:40:37 +00:00
|
|
|
/* Probe the key handle */
|
|
|
|
ProbeForWriteHandle(KeyHandle);
|
|
|
|
*KeyHandle = NULL;
|
2008-06-23 12:45:27 +00:00
|
|
|
|
2008-06-06 21:40:37 +00:00
|
|
|
/* Probe object attributes */
|
2008-09-07 09:25:31 +00:00
|
|
|
ProbeForRead(ObjectAttributes,
|
|
|
|
sizeof(OBJECT_ATTRIBUTES),
|
|
|
|
sizeof(ULONG));
|
2009-07-10 10:02:37 +00:00
|
|
|
|
2014-07-08 18:42:20 +00:00
|
|
|
if (Disposition)
|
|
|
|
ProbeForWriteUlong(Disposition);
|
2008-06-06 21:40:37 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2008-06-06 21:40:37 +00:00
|
|
|
{
|
2009-08-24 19:58:15 +00:00
|
|
|
/* Return the exception code */
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2008-06-06 21:40:37 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2008-06-23 12:45:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Save the class directly */
|
|
|
|
if (Class) ParseContext.Class = *Class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup the parse context */
|
|
|
|
ParseContext.CreateOperation = TRUE;
|
|
|
|
ParseContext.CreateOptions = CreateOptions;
|
2008-06-22 16:24:15 +00:00
|
|
|
|
2008-06-23 12:45:27 +00:00
|
|
|
/* Do the create */
|
|
|
|
Status = ObOpenObjectByName(ObjectAttributes,
|
|
|
|
CmpKeyObjectType,
|
|
|
|
PreviousMode,
|
|
|
|
NULL,
|
|
|
|
DesiredAccess,
|
|
|
|
&ParseContext,
|
|
|
|
&Handle);
|
|
|
|
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2008-06-23 12:45:27 +00:00
|
|
|
{
|
2008-06-22 16:24:15 +00:00
|
|
|
/* Return data to user */
|
2008-09-07 09:25:31 +00:00
|
|
|
if (NT_SUCCESS(Status)) *KeyHandle = Handle;
|
2008-06-22 16:24:15 +00:00
|
|
|
if (Disposition) *Disposition = ParseContext.Disposition;
|
2008-06-06 21:40:37 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2008-06-06 21:40:37 +00:00
|
|
|
{
|
|
|
|
/* Get the status */
|
2008-11-24 13:40:26 +00:00
|
|
|
Status = _SEH2_GetExceptionCode();
|
2008-06-06 21:40:37 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2008-06-22 16:24:15 +00:00
|
|
|
|
2014-07-08 18:42:20 +00:00
|
|
|
DPRINT("Returning handle %x, Status %x.\n", Handle, Status);
|
|
|
|
|
2008-06-22 16:24:15 +00:00
|
|
|
/* Return status */
|
2007-12-09 19:36:04 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2007-12-08 21:00:45 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtOpenKey(OUT PHANDLE KeyHandle,
|
|
|
|
IN ACCESS_MASK DesiredAccess,
|
|
|
|
IN POBJECT_ATTRIBUTES ObjectAttributes)
|
|
|
|
{
|
|
|
|
CM_PARSE_CONTEXT ParseContext = {0};
|
2007-12-15 18:14:41 +00:00
|
|
|
HANDLE Handle;
|
2009-08-24 19:58:15 +00:00
|
|
|
NTSTATUS Status;
|
2008-06-06 21:40:37 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
2007-12-08 21:00:45 +00:00
|
|
|
PAGED_CODE();
|
2014-07-08 18:42:20 +00:00
|
|
|
DPRINT("NtOpenKey(Path: %wZ, Root %x, Access: %x)\n",
|
|
|
|
ObjectAttributes->ObjectName, ObjectAttributes->RootDirectory, DesiredAccess);
|
2007-12-15 20:51:40 +00:00
|
|
|
|
2016-10-15 20:16:47 +00:00
|
|
|
/* Ignore the WOW64 flag, it's not valid in the kernel */
|
|
|
|
DesiredAccess &= ~KEY_WOW64_RES;
|
|
|
|
|
2008-06-23 12:45:27 +00:00
|
|
|
/* Check for user-mode caller */
|
2008-09-07 09:25:31 +00:00
|
|
|
if (PreviousMode != KernelMode)
|
2008-06-06 21:40:37 +00:00
|
|
|
{
|
2008-06-23 12:45:27 +00:00
|
|
|
/* Prepare to probe parameters */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2008-06-22 16:24:15 +00:00
|
|
|
{
|
2008-06-06 21:40:37 +00:00
|
|
|
/* Probe the key handle */
|
|
|
|
ProbeForWriteHandle(KeyHandle);
|
|
|
|
*KeyHandle = NULL;
|
2008-06-22 16:24:15 +00:00
|
|
|
|
2008-06-06 21:40:37 +00:00
|
|
|
/* Probe object attributes */
|
2008-09-07 09:25:31 +00:00
|
|
|
ProbeForRead(ObjectAttributes,
|
|
|
|
sizeof(OBJECT_ATTRIBUTES),
|
|
|
|
sizeof(ULONG));
|
2008-06-06 21:40:37 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2008-06-23 12:45:27 +00:00
|
|
|
{
|
2009-08-24 19:58:15 +00:00
|
|
|
/* Return the exception code */
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2008-06-23 12:45:27 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2008-06-06 21:40:37 +00:00
|
|
|
}
|
2008-06-23 12:45:27 +00:00
|
|
|
|
|
|
|
/* Just let the object manager handle this */
|
|
|
|
Status = ObOpenObjectByName(ObjectAttributes,
|
|
|
|
CmpKeyObjectType,
|
2018-10-20 17:17:55 +00:00
|
|
|
PreviousMode,
|
2008-06-23 12:45:27 +00:00
|
|
|
NULL,
|
|
|
|
DesiredAccess,
|
|
|
|
&ParseContext,
|
|
|
|
&Handle);
|
|
|
|
|
2008-09-07 09:25:31 +00:00
|
|
|
/* Only do this if we succeeded */
|
2008-06-23 12:45:27 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
2008-06-06 21:40:37 +00:00
|
|
|
{
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2008-06-23 12:45:27 +00:00
|
|
|
{
|
2008-09-07 09:25:31 +00:00
|
|
|
/* Return the handle to caller */
|
2008-06-23 12:45:27 +00:00
|
|
|
*KeyHandle = Handle;
|
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2008-06-23 12:45:27 +00:00
|
|
|
{
|
|
|
|
/* Get the status */
|
2008-11-24 13:40:26 +00:00
|
|
|
Status = _SEH2_GetExceptionCode();
|
2008-06-23 12:45:27 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2008-06-06 21:40:37 +00:00
|
|
|
}
|
2008-06-22 16:24:15 +00:00
|
|
|
|
2014-07-08 18:42:20 +00:00
|
|
|
DPRINT("Returning handle %x, Status %x.\n", Handle, Status);
|
|
|
|
|
2007-12-15 18:14:41 +00:00
|
|
|
/* Return status */
|
|
|
|
return Status;
|
2007-12-08 21:00:45 +00:00
|
|
|
}
|
|
|
|
|
2007-12-15 18:14:41 +00:00
|
|
|
|
2007-11-02 23:12:22 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtDeleteKey(IN HANDLE KeyHandle)
|
|
|
|
{
|
2007-12-06 21:11:57 +00:00
|
|
|
PCM_KEY_BODY KeyObject;
|
2007-11-02 23:12:22 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
REG_DELETE_KEY_INFORMATION DeleteKeyInfo;
|
|
|
|
REG_POST_OPERATION_INFORMATION PostOperationInfo;
|
|
|
|
PAGED_CODE();
|
|
|
|
DPRINT("NtDeleteKey(KH 0x%p)\n", KeyHandle);
|
|
|
|
|
|
|
|
/* Verify that the handle is valid and is a registry key */
|
|
|
|
Status = ObReferenceObjectByHandle(KeyHandle,
|
|
|
|
DELETE,
|
|
|
|
CmpKeyObjectType,
|
|
|
|
ExGetPreviousMode(),
|
2007-12-15 20:51:40 +00:00
|
|
|
(PVOID*)&KeyObject,
|
2007-11-02 23:12:22 +00:00
|
|
|
NULL);
|
2007-12-15 20:51:40 +00:00
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
2007-11-02 23:12:22 +00:00
|
|
|
|
|
|
|
/* Setup the callback */
|
|
|
|
PostOperationInfo.Object = (PVOID)KeyObject;
|
|
|
|
DeleteKeyInfo.Object = (PVOID)KeyObject;
|
|
|
|
Status = CmiCallRegisteredCallbacks(RegNtPreDeleteKey, &DeleteKeyInfo);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
2007-12-15 20:51:40 +00:00
|
|
|
/* Check if we are read-only */
|
|
|
|
if ((KeyObject->KeyControlBlock->ExtFlags & CM_KCB_READ_ONLY_KEY) ||
|
|
|
|
(KeyObject->KeyControlBlock->ParentKcb->ExtFlags & CM_KCB_READ_ONLY_KEY))
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
Status = STATUS_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
else
|
2007-11-02 23:12:22 +00:00
|
|
|
{
|
2007-12-15 20:51:40 +00:00
|
|
|
/* Call the internal API */
|
2008-11-24 13:40:26 +00:00
|
|
|
Status = CmDeleteKey(KeyObject);
|
2007-11-02 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do post callback */
|
|
|
|
PostOperationInfo.Status = Status;
|
|
|
|
CmiCallRegisteredCallbacks(RegNtPostDeleteKey, &PostOperationInfo);
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Dereference and return status */
|
2007-11-02 23:12:22 +00:00
|
|
|
ObDereferenceObject(KeyObject);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtEnumerateKey(IN HANDLE KeyHandle,
|
|
|
|
IN ULONG Index,
|
|
|
|
IN KEY_INFORMATION_CLASS KeyInformationClass,
|
|
|
|
OUT PVOID KeyInformation,
|
|
|
|
IN ULONG Length,
|
|
|
|
OUT PULONG ResultLength)
|
|
|
|
{
|
2009-07-10 10:02:37 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
2007-11-02 23:12:22 +00:00
|
|
|
NTSTATUS Status;
|
2007-12-06 21:11:57 +00:00
|
|
|
PCM_KEY_BODY KeyObject;
|
2007-11-02 23:12:22 +00:00
|
|
|
REG_ENUMERATE_KEY_INFORMATION EnumerateKeyInfo;
|
|
|
|
REG_POST_OPERATION_INFORMATION PostOperationInfo;
|
|
|
|
PAGED_CODE();
|
2013-08-31 16:02:13 +00:00
|
|
|
DPRINT("NtEnumerateKey() KH 0x%p, Index 0x%x, KIC %d, Length %lu\n",
|
2007-12-15 20:51:40 +00:00
|
|
|
KeyHandle, Index, KeyInformationClass, Length);
|
|
|
|
|
|
|
|
/* Reject classes we don't know about */
|
|
|
|
if ((KeyInformationClass != KeyBasicInformation) &&
|
|
|
|
(KeyInformationClass != KeyNodeInformation) &&
|
|
|
|
(KeyInformationClass != KeyFullInformation))
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2007-11-02 23:12:22 +00:00
|
|
|
|
|
|
|
/* Verify that the handle is valid and is a registry key */
|
|
|
|
Status = ObReferenceObjectByHandle(KeyHandle,
|
|
|
|
KEY_ENUMERATE_SUB_KEYS,
|
|
|
|
CmpKeyObjectType,
|
2018-10-20 17:17:55 +00:00
|
|
|
PreviousMode,
|
2007-12-15 20:51:40 +00:00
|
|
|
(PVOID*)&KeyObject,
|
2007-11-02 23:12:22 +00:00
|
|
|
NULL);
|
2007-12-15 20:51:40 +00:00
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
2007-11-02 23:12:22 +00:00
|
|
|
|
2009-07-10 10:02:37 +00:00
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
ProbeForWriteUlong(ResultLength);
|
|
|
|
ProbeForWrite(KeyInformation,
|
|
|
|
Length,
|
|
|
|
sizeof(ULONG));
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
/* Dereference and return status */
|
|
|
|
ObDereferenceObject(KeyObject);
|
2009-08-24 19:58:15 +00:00
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2009-07-10 10:02:37 +00:00
|
|
|
}
|
2009-08-24 19:58:15 +00:00
|
|
|
_SEH2_END;
|
2009-07-10 10:02:37 +00:00
|
|
|
}
|
|
|
|
|
2007-11-02 23:12:22 +00:00
|
|
|
/* Setup the callback */
|
|
|
|
PostOperationInfo.Object = (PVOID)KeyObject;
|
|
|
|
EnumerateKeyInfo.Object = (PVOID)KeyObject;
|
|
|
|
EnumerateKeyInfo.Index = Index;
|
|
|
|
EnumerateKeyInfo.KeyInformationClass = KeyInformationClass;
|
|
|
|
EnumerateKeyInfo.Length = Length;
|
|
|
|
EnumerateKeyInfo.ResultLength = ResultLength;
|
|
|
|
|
|
|
|
/* Do the callback */
|
|
|
|
Status = CmiCallRegisteredCallbacks(RegNtPreEnumerateKey, &EnumerateKeyInfo);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Call the internal API */
|
|
|
|
Status = CmEnumerateKey(KeyObject->KeyControlBlock,
|
|
|
|
Index,
|
|
|
|
KeyInformationClass,
|
|
|
|
KeyInformation,
|
|
|
|
Length,
|
|
|
|
ResultLength);
|
|
|
|
|
|
|
|
/* Do the post callback */
|
|
|
|
PostOperationInfo.Status = Status;
|
|
|
|
CmiCallRegisteredCallbacks(RegNtPostEnumerateKey, &PostOperationInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dereference and return status */
|
|
|
|
ObDereferenceObject(KeyObject);
|
2014-07-08 18:42:20 +00:00
|
|
|
DPRINT("Returning status %x.\n", Status);
|
2007-11-02 23:12:22 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtEnumerateValueKey(IN HANDLE KeyHandle,
|
|
|
|
IN ULONG Index,
|
|
|
|
IN KEY_VALUE_INFORMATION_CLASS KeyValueInformationClass,
|
|
|
|
OUT PVOID KeyValueInformation,
|
|
|
|
IN ULONG Length,
|
|
|
|
OUT PULONG ResultLength)
|
|
|
|
{
|
2009-07-10 10:02:37 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
2007-11-02 23:12:22 +00:00
|
|
|
NTSTATUS Status;
|
2007-12-06 21:11:57 +00:00
|
|
|
PCM_KEY_BODY KeyObject;
|
2007-11-02 23:12:22 +00:00
|
|
|
REG_ENUMERATE_VALUE_KEY_INFORMATION EnumerateValueKeyInfo;
|
|
|
|
REG_POST_OPERATION_INFORMATION PostOperationInfo;
|
2018-10-21 16:12:08 +00:00
|
|
|
|
2007-11-02 23:12:22 +00:00
|
|
|
PAGED_CODE();
|
2018-10-21 16:12:08 +00:00
|
|
|
|
2013-08-31 16:02:13 +00:00
|
|
|
DPRINT("NtEnumerateValueKey() KH 0x%p, Index 0x%x, KVIC %d, Length %lu\n",
|
2007-12-15 20:51:40 +00:00
|
|
|
KeyHandle, Index, KeyValueInformationClass, Length);
|
|
|
|
|
|
|
|
/* Reject classes we don't know about */
|
2018-10-21 16:12:08 +00:00
|
|
|
if ((KeyValueInformationClass != KeyValueBasicInformation) &&
|
|
|
|
(KeyValueInformationClass != KeyValueFullInformation) &&
|
|
|
|
(KeyValueInformationClass != KeyValuePartialInformation) &&
|
|
|
|
(KeyValueInformationClass != KeyValueFullInformationAlign64) &&
|
|
|
|
(KeyValueInformationClass != KeyValuePartialInformationAlign64))
|
2007-12-15 20:51:40 +00:00
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2007-11-02 23:12:22 +00:00
|
|
|
|
|
|
|
/* Verify that the handle is valid and is a registry key */
|
|
|
|
Status = ObReferenceObjectByHandle(KeyHandle,
|
|
|
|
KEY_QUERY_VALUE,
|
|
|
|
CmpKeyObjectType,
|
2018-10-20 17:17:55 +00:00
|
|
|
PreviousMode,
|
2007-12-15 20:51:40 +00:00
|
|
|
(PVOID*)&KeyObject,
|
2007-11-02 23:12:22 +00:00
|
|
|
NULL);
|
2007-12-15 20:51:40 +00:00
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
2007-11-02 23:12:22 +00:00
|
|
|
|
2009-07-10 10:02:37 +00:00
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
ProbeForWriteUlong(ResultLength);
|
|
|
|
ProbeForWrite(KeyValueInformation,
|
|
|
|
Length,
|
|
|
|
sizeof(ULONG));
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
/* Dereference and return status */
|
|
|
|
ObDereferenceObject(KeyObject);
|
2009-08-24 19:58:15 +00:00
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2009-07-10 10:02:37 +00:00
|
|
|
}
|
2009-08-24 19:58:15 +00:00
|
|
|
_SEH2_END;
|
2009-07-10 10:02:37 +00:00
|
|
|
}
|
|
|
|
|
2007-11-02 23:12:22 +00:00
|
|
|
/* Setup the callback */
|
|
|
|
PostOperationInfo.Object = (PVOID)KeyObject;
|
|
|
|
EnumerateValueKeyInfo.Object = (PVOID)KeyObject;
|
|
|
|
EnumerateValueKeyInfo.Index = Index;
|
|
|
|
EnumerateValueKeyInfo.KeyValueInformationClass = KeyValueInformationClass;
|
|
|
|
EnumerateValueKeyInfo.KeyValueInformation = KeyValueInformation;
|
|
|
|
EnumerateValueKeyInfo.Length = Length;
|
|
|
|
EnumerateValueKeyInfo.ResultLength = ResultLength;
|
|
|
|
|
|
|
|
/* Do the callback */
|
|
|
|
Status = CmiCallRegisteredCallbacks(RegNtPreEnumerateValueKey,
|
|
|
|
&EnumerateValueKeyInfo);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Call the internal API */
|
|
|
|
Status = CmEnumerateValueKey(KeyObject->KeyControlBlock,
|
|
|
|
Index,
|
|
|
|
KeyValueInformationClass,
|
|
|
|
KeyValueInformation,
|
|
|
|
Length,
|
|
|
|
ResultLength);
|
|
|
|
|
|
|
|
/* Do the post callback */
|
|
|
|
PostOperationInfo.Status = Status;
|
|
|
|
CmiCallRegisteredCallbacks(RegNtPostEnumerateValueKey, &PostOperationInfo);
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Dereference and return status */
|
2007-11-02 23:12:22 +00:00
|
|
|
ObDereferenceObject(KeyObject);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtQueryKey(IN HANDLE KeyHandle,
|
|
|
|
IN KEY_INFORMATION_CLASS KeyInformationClass,
|
|
|
|
OUT PVOID KeyInformation,
|
|
|
|
IN ULONG Length,
|
|
|
|
OUT PULONG ResultLength)
|
|
|
|
{
|
2009-07-10 10:02:37 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
2007-11-02 23:12:22 +00:00
|
|
|
NTSTATUS Status;
|
2007-12-06 21:11:57 +00:00
|
|
|
PCM_KEY_BODY KeyObject;
|
2007-11-02 23:12:22 +00:00
|
|
|
REG_QUERY_KEY_INFORMATION QueryKeyInfo;
|
|
|
|
REG_POST_OPERATION_INFORMATION PostOperationInfo;
|
2007-12-15 20:51:40 +00:00
|
|
|
OBJECT_HANDLE_INFORMATION HandleInfo;
|
2007-11-02 23:12:22 +00:00
|
|
|
PAGED_CODE();
|
2013-08-31 16:02:13 +00:00
|
|
|
DPRINT("NtQueryKey() KH 0x%p, KIC %d, Length %lu\n",
|
2007-12-15 20:51:40 +00:00
|
|
|
KeyHandle, KeyInformationClass, Length);
|
|
|
|
|
|
|
|
/* Reject invalid classes */
|
|
|
|
if ((KeyInformationClass != KeyBasicInformation) &&
|
|
|
|
(KeyInformationClass != KeyNodeInformation) &&
|
|
|
|
(KeyInformationClass != KeyFullInformation) &&
|
|
|
|
(KeyInformationClass != KeyNameInformation) &&
|
|
|
|
(KeyInformationClass != KeyCachedInformation) &&
|
|
|
|
(KeyInformationClass != KeyFlagsInformation))
|
2007-11-02 23:12:22 +00:00
|
|
|
{
|
2007-12-15 20:51:40 +00:00
|
|
|
/* Fail */
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
2007-11-02 23:12:22 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-12-15 20:51:40 +00:00
|
|
|
/* Check if just the name is required */
|
|
|
|
if (KeyInformationClass == KeyNameInformation)
|
|
|
|
{
|
|
|
|
/* Ignore access level */
|
|
|
|
Status = ObReferenceObjectByHandle(KeyHandle,
|
|
|
|
0,
|
|
|
|
CmpKeyObjectType,
|
2018-10-20 17:17:55 +00:00
|
|
|
PreviousMode,
|
2007-12-15 20:51:40 +00:00
|
|
|
(PVOID*)&KeyObject,
|
|
|
|
&HandleInfo);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* At least a single bit of access is required */
|
|
|
|
if (!HandleInfo.GrantedAccess)
|
|
|
|
{
|
|
|
|
/* No such luck */
|
|
|
|
ObDereferenceObject(KeyObject);
|
|
|
|
Status = STATUS_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Get a reference */
|
|
|
|
Status = ObReferenceObjectByHandle(KeyHandle,
|
|
|
|
KEY_QUERY_VALUE,
|
|
|
|
CmpKeyObjectType,
|
2018-10-20 17:17:55 +00:00
|
|
|
PreviousMode,
|
2007-12-15 20:51:40 +00:00
|
|
|
(PVOID*)&KeyObject,
|
|
|
|
NULL);
|
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-12-15 20:51:40 +00:00
|
|
|
/* Quit on failure */
|
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
2007-11-02 23:12:22 +00:00
|
|
|
|
2009-07-10 10:02:37 +00:00
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
ProbeForWriteUlong(ResultLength);
|
|
|
|
ProbeForWrite(KeyInformation,
|
|
|
|
Length,
|
|
|
|
sizeof(ULONG));
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
/* Dereference and return status */
|
|
|
|
ObDereferenceObject(KeyObject);
|
2009-08-24 19:58:15 +00:00
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2009-07-10 10:02:37 +00:00
|
|
|
}
|
2009-08-24 19:58:15 +00:00
|
|
|
_SEH2_END;
|
2009-07-10 10:02:37 +00:00
|
|
|
}
|
|
|
|
|
2007-11-02 23:12:22 +00:00
|
|
|
/* Setup the callback */
|
|
|
|
PostOperationInfo.Object = (PVOID)KeyObject;
|
|
|
|
QueryKeyInfo.Object = (PVOID)KeyObject;
|
|
|
|
QueryKeyInfo.KeyInformationClass = KeyInformationClass;
|
|
|
|
QueryKeyInfo.KeyInformation = KeyInformation;
|
|
|
|
QueryKeyInfo.Length = Length;
|
|
|
|
QueryKeyInfo.ResultLength = ResultLength;
|
|
|
|
|
|
|
|
/* Do the callback */
|
|
|
|
Status = CmiCallRegisteredCallbacks(RegNtPreQueryKey, &QueryKeyInfo);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Call the internal API */
|
|
|
|
Status = CmQueryKey(KeyObject->KeyControlBlock,
|
|
|
|
KeyInformationClass,
|
|
|
|
KeyInformation,
|
|
|
|
Length,
|
|
|
|
ResultLength);
|
|
|
|
|
|
|
|
/* Do the post callback */
|
|
|
|
PostOperationInfo.Status = Status;
|
|
|
|
CmiCallRegisteredCallbacks(RegNtPostQueryKey, &PostOperationInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dereference and return status */
|
|
|
|
ObDereferenceObject(KeyObject);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtQueryValueKey(IN HANDLE KeyHandle,
|
|
|
|
IN PUNICODE_STRING ValueName,
|
|
|
|
IN KEY_VALUE_INFORMATION_CLASS KeyValueInformationClass,
|
|
|
|
OUT PVOID KeyValueInformation,
|
|
|
|
IN ULONG Length,
|
|
|
|
OUT PULONG ResultLength)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
2018-10-21 13:42:13 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
2007-12-06 21:11:57 +00:00
|
|
|
PCM_KEY_BODY KeyObject;
|
2007-11-02 23:12:22 +00:00
|
|
|
REG_QUERY_VALUE_KEY_INFORMATION QueryValueKeyInfo;
|
|
|
|
REG_POST_OPERATION_INFORMATION PostOperationInfo;
|
2018-10-21 13:42:13 +00:00
|
|
|
UNICODE_STRING ValueNameCopy;
|
2018-10-20 17:30:59 +00:00
|
|
|
|
2007-11-02 23:12:22 +00:00
|
|
|
PAGED_CODE();
|
2018-10-20 17:30:59 +00:00
|
|
|
|
2013-08-31 16:02:13 +00:00
|
|
|
DPRINT("NtQueryValueKey() KH 0x%p, VN '%wZ', KVIC %d, Length %lu\n",
|
2007-11-02 23:12:22 +00:00
|
|
|
KeyHandle, ValueName, KeyValueInformationClass, Length);
|
|
|
|
|
2018-10-20 17:30:59 +00:00
|
|
|
/* Reject classes we don't know about */
|
|
|
|
if ((KeyValueInformationClass != KeyValueBasicInformation) &&
|
|
|
|
(KeyValueInformationClass != KeyValueFullInformation) &&
|
|
|
|
(KeyValueInformationClass != KeyValuePartialInformation) &&
|
|
|
|
(KeyValueInformationClass != KeyValueFullInformationAlign64) &&
|
|
|
|
(KeyValueInformationClass != KeyValuePartialInformationAlign64))
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2007-11-02 23:12:22 +00:00
|
|
|
/* Verify that the handle is valid and is a registry key */
|
|
|
|
Status = ObReferenceObjectByHandle(KeyHandle,
|
|
|
|
KEY_QUERY_VALUE,
|
|
|
|
CmpKeyObjectType,
|
2018-10-20 17:17:55 +00:00
|
|
|
PreviousMode,
|
2007-12-15 20:51:40 +00:00
|
|
|
(PVOID*)&KeyObject,
|
2007-11-02 23:12:22 +00:00
|
|
|
NULL);
|
2018-10-21 13:42:13 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return Status;
|
2007-12-15 20:51:40 +00:00
|
|
|
|
2009-07-10 10:02:37 +00:00
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
ProbeForWriteUlong(ResultLength);
|
|
|
|
ProbeForWrite(KeyValueInformation,
|
|
|
|
Length,
|
|
|
|
sizeof(ULONG));
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
/* Dereference and return status */
|
|
|
|
ObDereferenceObject(KeyObject);
|
2009-08-24 19:58:15 +00:00
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2009-07-10 10:02:37 +00:00
|
|
|
}
|
2009-08-24 19:58:15 +00:00
|
|
|
_SEH2_END;
|
2009-07-10 10:02:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Capture the string */
|
|
|
|
Status = ProbeAndCaptureUnicodeString(&ValueNameCopy, PreviousMode, ValueName);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
goto Quit;
|
|
|
|
|
2007-12-15 20:51:40 +00:00
|
|
|
/* Make sure the name is aligned properly */
|
|
|
|
if ((ValueNameCopy.Length & (sizeof(WCHAR) - 1)))
|
2007-11-02 23:12:22 +00:00
|
|
|
{
|
2007-12-15 20:51:40 +00:00
|
|
|
/* It isn't, so we'll fail */
|
2018-10-21 13:42:13 +00:00
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
goto Quit;
|
2007-12-15 20:51:40 +00:00
|
|
|
}
|
2018-10-21 13:42:13 +00:00
|
|
|
|
|
|
|
/* Ignore any null characters at the end */
|
|
|
|
while ((ValueNameCopy.Length) &&
|
|
|
|
!(ValueNameCopy.Buffer[ValueNameCopy.Length / sizeof(WCHAR) - 1]))
|
2007-12-15 20:51:40 +00:00
|
|
|
{
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Skip it */
|
|
|
|
ValueNameCopy.Length -= sizeof(WCHAR);
|
2007-11-02 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup the callback */
|
|
|
|
PostOperationInfo.Object = (PVOID)KeyObject;
|
|
|
|
QueryValueKeyInfo.Object = (PVOID)KeyObject;
|
2007-12-15 20:51:40 +00:00
|
|
|
QueryValueKeyInfo.ValueName = &ValueNameCopy;
|
2007-11-02 23:12:22 +00:00
|
|
|
QueryValueKeyInfo.KeyValueInformationClass = KeyValueInformationClass;
|
|
|
|
QueryValueKeyInfo.Length = Length;
|
|
|
|
QueryValueKeyInfo.ResultLength = ResultLength;
|
|
|
|
|
|
|
|
/* Do the callback */
|
|
|
|
Status = CmiCallRegisteredCallbacks(RegNtPreQueryValueKey, &QueryValueKeyInfo);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Call the internal API */
|
|
|
|
Status = CmQueryValueKey(KeyObject->KeyControlBlock,
|
2007-12-15 20:51:40 +00:00
|
|
|
ValueNameCopy,
|
2007-11-02 23:12:22 +00:00
|
|
|
KeyValueInformationClass,
|
|
|
|
KeyValueInformation,
|
|
|
|
Length,
|
|
|
|
ResultLength);
|
|
|
|
|
|
|
|
/* Do the post callback */
|
|
|
|
PostOperationInfo.Status = Status;
|
|
|
|
CmiCallRegisteredCallbacks(RegNtPostQueryValueKey, &PostOperationInfo);
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
Quit:
|
|
|
|
if (ValueNameCopy.Buffer)
|
|
|
|
ReleaseCapturedUnicodeString(&ValueNameCopy, PreviousMode);
|
|
|
|
|
2007-11-02 23:12:22 +00:00
|
|
|
/* Dereference and return status */
|
|
|
|
ObDereferenceObject(KeyObject);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtSetValueKey(IN HANDLE KeyHandle,
|
|
|
|
IN PUNICODE_STRING ValueName,
|
|
|
|
IN ULONG TitleIndex,
|
|
|
|
IN ULONG Type,
|
|
|
|
IN PVOID Data,
|
|
|
|
IN ULONG DataSize)
|
|
|
|
{
|
2014-07-08 18:42:20 +00:00
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
2018-10-21 13:42:13 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode;
|
|
|
|
PCM_KEY_BODY KeyObject;
|
2007-11-02 23:12:22 +00:00
|
|
|
REG_SET_VALUE_KEY_INFORMATION SetValueKeyInfo;
|
|
|
|
REG_POST_OPERATION_INFORMATION PostOperationInfo;
|
2014-07-08 18:42:20 +00:00
|
|
|
UNICODE_STRING ValueNameCopy;
|
|
|
|
|
2007-11-02 23:12:22 +00:00
|
|
|
PAGED_CODE();
|
2014-07-08 18:42:20 +00:00
|
|
|
|
|
|
|
PreviousMode = ExGetPreviousMode();
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Verify that the handle is valid and is a registry key */
|
|
|
|
Status = ObReferenceObjectByHandle(KeyHandle,
|
|
|
|
KEY_SET_VALUE,
|
|
|
|
CmpKeyObjectType,
|
|
|
|
PreviousMode,
|
|
|
|
(PVOID*)&KeyObject,
|
|
|
|
NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return Status;
|
|
|
|
|
2014-07-10 09:43:30 +00:00
|
|
|
if (!DataSize)
|
|
|
|
Data = NULL;
|
2014-07-08 18:42:20 +00:00
|
|
|
|
|
|
|
/* Probe and copy the data */
|
2016-05-02 13:19:44 +00:00
|
|
|
if ((PreviousMode != KernelMode) && (DataSize != 0))
|
2014-07-08 18:42:20 +00:00
|
|
|
{
|
2020-02-23 20:35:57 +00:00
|
|
|
PVOID DataCopy = NULL;
|
|
|
|
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
ProbeForRead(Data, DataSize, 1);
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
Status = _SEH2_GetExceptionCode();
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Dereference and return status */
|
|
|
|
ObDereferenceObject(KeyObject);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataCopy = ExAllocatePoolWithTag(PagedPool, DataSize, TAG_CM);
|
2014-07-08 18:42:20 +00:00
|
|
|
if (!DataCopy)
|
2018-10-21 13:42:13 +00:00
|
|
|
{
|
|
|
|
/* Dereference and return status */
|
|
|
|
ObDereferenceObject(KeyObject);
|
2016-05-02 13:19:44 +00:00
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
2018-10-21 13:42:13 +00:00
|
|
|
}
|
2020-02-23 20:35:57 +00:00
|
|
|
|
2014-07-08 18:42:20 +00:00
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
RtlCopyMemory(DataCopy, Data, DataSize);
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
Status = _SEH2_GetExceptionCode();
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Dereference and return status */
|
2014-07-08 18:42:20 +00:00
|
|
|
ExFreePoolWithTag(DataCopy, TAG_CM);
|
2018-10-21 13:42:13 +00:00
|
|
|
ObDereferenceObject(KeyObject);
|
2014-07-08 18:42:20 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2020-02-23 20:35:57 +00:00
|
|
|
|
2014-07-08 18:42:20 +00:00
|
|
|
Data = DataCopy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Capture the string */
|
|
|
|
Status = ProbeAndCaptureUnicodeString(&ValueNameCopy, PreviousMode, ValueName);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2018-10-21 13:42:13 +00:00
|
|
|
goto Quit;
|
2014-07-08 18:42:20 +00:00
|
|
|
|
2013-08-31 16:02:13 +00:00
|
|
|
DPRINT("NtSetValueKey() KH 0x%p, VN '%wZ', TI %x, T %lu, DS %lu\n",
|
2014-07-08 18:42:20 +00:00
|
|
|
KeyHandle, &ValueNameCopy, TitleIndex, Type, DataSize);
|
2007-11-02 23:12:22 +00:00
|
|
|
|
2007-12-15 20:51:40 +00:00
|
|
|
/* Make sure the name is aligned, not too long, and the data under 4GB */
|
|
|
|
if ( (ValueNameCopy.Length > 32767) ||
|
|
|
|
((ValueNameCopy.Length & (sizeof(WCHAR) - 1))) ||
|
|
|
|
(DataSize > 0x80000000))
|
|
|
|
{
|
|
|
|
/* Fail */
|
2014-07-08 18:42:20 +00:00
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
2018-10-21 13:42:13 +00:00
|
|
|
goto Quit;
|
2007-12-15 20:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore any null characters at the end */
|
|
|
|
while ((ValueNameCopy.Length) &&
|
|
|
|
!(ValueNameCopy.Buffer[ValueNameCopy.Length / sizeof(WCHAR) - 1]))
|
2007-11-02 23:12:22 +00:00
|
|
|
{
|
2007-12-15 20:51:40 +00:00
|
|
|
/* Skip it */
|
|
|
|
ValueNameCopy.Length -= sizeof(WCHAR);
|
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-12-15 20:51:40 +00:00
|
|
|
/* Don't touch read-only keys */
|
|
|
|
if (KeyObject->KeyControlBlock->ExtFlags & CM_KCB_READ_ONLY_KEY)
|
|
|
|
{
|
|
|
|
/* Fail */
|
2014-07-08 18:42:20 +00:00
|
|
|
Status = STATUS_ACCESS_DENIED;
|
2018-10-21 13:42:13 +00:00
|
|
|
goto Quit;
|
2007-11-02 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup callback */
|
|
|
|
PostOperationInfo.Object = (PVOID)KeyObject;
|
|
|
|
SetValueKeyInfo.Object = (PVOID)KeyObject;
|
2014-07-08 18:42:20 +00:00
|
|
|
SetValueKeyInfo.ValueName = &ValueNameCopy;
|
2007-11-02 23:12:22 +00:00
|
|
|
SetValueKeyInfo.TitleIndex = TitleIndex;
|
|
|
|
SetValueKeyInfo.Type = Type;
|
|
|
|
SetValueKeyInfo.Data = Data;
|
|
|
|
SetValueKeyInfo.DataSize = DataSize;
|
|
|
|
|
|
|
|
/* Do the callback */
|
|
|
|
Status = CmiCallRegisteredCallbacks(RegNtPreSetValueKey, &SetValueKeyInfo);
|
|
|
|
if (NT_SUCCESS(Status))
|
2008-11-24 13:40:26 +00:00
|
|
|
{
|
2007-11-02 23:12:22 +00:00
|
|
|
/* Call the internal API */
|
|
|
|
Status = CmSetValueKey(KeyObject->KeyControlBlock,
|
2007-12-15 20:51:40 +00:00
|
|
|
&ValueNameCopy,
|
2007-11-02 23:12:22 +00:00
|
|
|
Type,
|
|
|
|
Data,
|
|
|
|
DataSize);
|
|
|
|
|
2018-10-20 17:30:59 +00:00
|
|
|
/* Do the post-callback */
|
|
|
|
PostOperationInfo.Status = Status;
|
|
|
|
CmiCallRegisteredCallbacks(RegNtPostSetValueKey, &PostOperationInfo);
|
|
|
|
}
|
2007-11-02 23:12:22 +00:00
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
Quit:
|
|
|
|
if (ValueNameCopy.Buffer)
|
|
|
|
ReleaseCapturedUnicodeString(&ValueNameCopy, PreviousMode);
|
|
|
|
|
2014-07-08 18:42:20 +00:00
|
|
|
if ((PreviousMode != KernelMode) && Data)
|
|
|
|
ExFreePoolWithTag(Data, TAG_CM);
|
2018-10-21 13:42:13 +00:00
|
|
|
|
|
|
|
/* Dereference and return status */
|
|
|
|
ObDereferenceObject(KeyObject);
|
2007-11-02 23:12:22 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtDeleteValueKey(IN HANDLE KeyHandle,
|
|
|
|
IN PUNICODE_STRING ValueName)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
2018-10-21 13:42:13 +00:00
|
|
|
PCM_KEY_BODY KeyObject;
|
2007-11-02 23:12:22 +00:00
|
|
|
REG_DELETE_VALUE_KEY_INFORMATION DeleteValueKeyInfo;
|
|
|
|
REG_POST_OPERATION_INFORMATION PostOperationInfo;
|
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
2018-10-21 13:42:13 +00:00
|
|
|
UNICODE_STRING ValueNameCopy;
|
|
|
|
|
2007-11-02 23:12:22 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Verify that the handle is valid and is a registry key */
|
|
|
|
Status = ObReferenceObjectByHandle(KeyHandle,
|
|
|
|
KEY_SET_VALUE,
|
|
|
|
CmpKeyObjectType,
|
|
|
|
PreviousMode,
|
2018-10-20 17:17:55 +00:00
|
|
|
(PVOID*)&KeyObject,
|
2007-11-02 23:12:22 +00:00
|
|
|
NULL);
|
2018-10-21 13:42:13 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return Status;
|
2007-12-15 20:51:40 +00:00
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Capture the string */
|
|
|
|
Status = ProbeAndCaptureUnicodeString(&ValueNameCopy, PreviousMode, ValueName);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
goto Quit;
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-12-15 20:51:40 +00:00
|
|
|
/* Make sure the name is aligned properly */
|
|
|
|
if ((ValueNameCopy.Length & (sizeof(WCHAR) - 1)))
|
|
|
|
{
|
|
|
|
/* It isn't, so we'll fail */
|
2018-10-21 13:42:13 +00:00
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
goto Quit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't touch read-only keys */
|
|
|
|
if (KeyObject->KeyControlBlock->ExtFlags & CM_KCB_READ_ONLY_KEY)
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
Status = STATUS_ACCESS_DENIED;
|
|
|
|
goto Quit;
|
2007-11-02 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the callback */
|
|
|
|
DeleteValueKeyInfo.Object = (PVOID)KeyObject;
|
|
|
|
DeleteValueKeyInfo.ValueName = ValueName;
|
|
|
|
Status = CmiCallRegisteredCallbacks(RegNtPreDeleteValueKey,
|
|
|
|
&DeleteValueKeyInfo);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Call the internal API */
|
2007-12-15 20:51:40 +00:00
|
|
|
Status = CmDeleteValueKey(KeyObject->KeyControlBlock, ValueNameCopy);
|
2007-11-02 23:12:22 +00:00
|
|
|
|
|
|
|
/* Do the post callback */
|
|
|
|
PostOperationInfo.Object = (PVOID)KeyObject;
|
|
|
|
PostOperationInfo.Status = Status;
|
|
|
|
CmiCallRegisteredCallbacks(RegNtPostDeleteValueKey,
|
|
|
|
&PostOperationInfo);
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
Quit:
|
|
|
|
if (ValueNameCopy.Buffer)
|
|
|
|
ReleaseCapturedUnicodeString(&ValueNameCopy, PreviousMode);
|
|
|
|
|
|
|
|
/* Dereference and return status */
|
2007-11-02 23:12:22 +00:00
|
|
|
ObDereferenceObject(KeyObject);
|
2007-11-05 22:54:47 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtFlushKey(IN HANDLE KeyHandle)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
2007-12-06 21:11:57 +00:00
|
|
|
PCM_KEY_BODY KeyObject;
|
2007-11-05 22:54:47 +00:00
|
|
|
PAGED_CODE();
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-11-05 22:54:47 +00:00
|
|
|
/* Get the key object */
|
|
|
|
Status = ObReferenceObjectByHandle(KeyHandle,
|
|
|
|
0,
|
|
|
|
CmpKeyObjectType,
|
|
|
|
ExGetPreviousMode(),
|
|
|
|
(PVOID*)&KeyObject,
|
|
|
|
NULL);
|
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-11-05 22:54:47 +00:00
|
|
|
/* Lock the registry */
|
2007-12-15 20:51:40 +00:00
|
|
|
CmpLockRegistry();
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-12-15 20:51:40 +00:00
|
|
|
/* Lock the KCB */
|
|
|
|
CmpAcquireKcbLockShared(KeyObject->KeyControlBlock);
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-11-05 22:54:47 +00:00
|
|
|
/* Make sure KCB isn't deleted */
|
|
|
|
if (KeyObject->KeyControlBlock->Delete)
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
Status = STATUS_KEY_DELETED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Call the internal API */
|
|
|
|
Status = CmFlushKey(KeyObject->KeyControlBlock, FALSE);
|
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-12-15 20:51:40 +00:00
|
|
|
/* Release the locks */
|
|
|
|
CmpReleaseKcbLock(KeyObject->KeyControlBlock);
|
|
|
|
CmpUnlockRegistry();
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-11-05 22:54:47 +00:00
|
|
|
/* Dereference the object and return status */
|
|
|
|
ObDereferenceObject(KeyObject);
|
2007-11-02 23:12:22 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtLoadKey(IN POBJECT_ATTRIBUTES KeyObjectAttributes,
|
|
|
|
IN POBJECT_ATTRIBUTES FileObjectAttributes)
|
|
|
|
{
|
2007-12-08 21:00:45 +00:00
|
|
|
/* Call the newer API */
|
2008-08-25 15:10:27 +00:00
|
|
|
return NtLoadKeyEx(KeyObjectAttributes, FileObjectAttributes, 0, NULL);
|
2007-11-02 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
2007-11-22 18:38:32 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtLoadKey2(IN POBJECT_ATTRIBUTES KeyObjectAttributes,
|
|
|
|
IN POBJECT_ATTRIBUTES FileObjectAttributes,
|
|
|
|
IN ULONG Flags)
|
|
|
|
{
|
2007-12-08 21:00:45 +00:00
|
|
|
/* Call the newer API */
|
2007-11-22 18:38:32 +00:00
|
|
|
return NtLoadKeyEx(KeyObjectAttributes, FileObjectAttributes, Flags, NULL);
|
|
|
|
}
|
|
|
|
|
2007-11-02 23:12:22 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtLoadKeyEx(IN POBJECT_ATTRIBUTES TargetKey,
|
|
|
|
IN POBJECT_ATTRIBUTES SourceFile,
|
|
|
|
IN ULONG Flags,
|
2007-11-22 18:38:32 +00:00
|
|
|
IN HANDLE TrustClassKey)
|
2007-11-02 23:12:22 +00:00
|
|
|
{
|
2007-11-22 18:38:32 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
2018-10-21 13:42:13 +00:00
|
|
|
OBJECT_ATTRIBUTES CapturedTargetKey;
|
|
|
|
OBJECT_ATTRIBUTES CapturedSourceFile;
|
|
|
|
UNICODE_STRING TargetKeyName, SourceFileName;
|
|
|
|
HANDLE KmTargetKeyRootDir = NULL, KmSourceFileRootDir = NULL;
|
2007-12-06 21:11:57 +00:00
|
|
|
PCM_KEY_BODY KeyBody = NULL;
|
2018-10-21 13:42:13 +00:00
|
|
|
|
2007-11-22 18:38:32 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Validate flags */
|
2018-10-21 13:42:13 +00:00
|
|
|
if (Flags & ~REG_NO_LAZY_FLUSH)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-11-22 18:38:32 +00:00
|
|
|
/* Validate privilege */
|
|
|
|
if (!SeSinglePrivilegeCheck(SeRestorePrivilege, PreviousMode))
|
|
|
|
{
|
|
|
|
DPRINT1("Restore Privilege missing!\n");
|
2007-12-12 11:42:20 +00:00
|
|
|
return STATUS_PRIVILEGE_NOT_HELD;
|
2007-11-22 18:38:32 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-11-22 18:38:32 +00:00
|
|
|
/* Block APCs */
|
|
|
|
KeEnterCriticalRegion();
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Check for user-mode caller */
|
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
|
|
|
/* Prepare to probe parameters */
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
/* Probe target key */
|
|
|
|
ProbeForRead(TargetKey,
|
|
|
|
sizeof(OBJECT_ATTRIBUTES),
|
|
|
|
sizeof(ULONG));
|
|
|
|
|
|
|
|
/* Probe source file */
|
|
|
|
ProbeForRead(SourceFile,
|
|
|
|
sizeof(OBJECT_ATTRIBUTES),
|
|
|
|
sizeof(ULONG));
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
/* Return the exception code */
|
|
|
|
Status = _SEH2_GetExceptionCode();
|
|
|
|
_SEH2_YIELD(goto Quit);
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Probe and capture the target key attributes, including the security */
|
|
|
|
Status = ProbeAndCaptureObjectAttributes(&CapturedTargetKey,
|
|
|
|
&TargetKeyName,
|
|
|
|
PreviousMode,
|
|
|
|
TargetKey,
|
|
|
|
TRUE);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
goto Quit;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Probe and capture the source file attributes, but not the security.
|
|
|
|
* A proper security context is built by CmLoadKey().
|
|
|
|
*/
|
|
|
|
Status = ProbeAndCaptureObjectAttributes(&CapturedSourceFile,
|
|
|
|
&SourceFileName,
|
|
|
|
PreviousMode,
|
|
|
|
SourceFile,
|
|
|
|
FALSE);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
ReleaseCapturedObjectAttributes(&CapturedTargetKey, PreviousMode);
|
|
|
|
goto Quit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure the target key root directory handle is a kernel handle */
|
|
|
|
Status = CmpConvertHandleToKernelHandle(CapturedTargetKey.RootDirectory,
|
|
|
|
CmpKeyObjectType,
|
|
|
|
KEY_READ,
|
|
|
|
PreviousMode,
|
|
|
|
&KmTargetKeyRootDir);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
goto Cleanup;
|
|
|
|
CapturedTargetKey.RootDirectory = KmTargetKeyRootDir;
|
|
|
|
CapturedTargetKey.Attributes |= OBJ_KERNEL_HANDLE;
|
|
|
|
|
|
|
|
/* Make sure the source file root directory handle is a kernel handle */
|
|
|
|
Status = CmpConvertHandleToKernelHandle(CapturedSourceFile.RootDirectory,
|
|
|
|
IoFileObjectType,
|
|
|
|
FILE_TRAVERSE,
|
|
|
|
PreviousMode,
|
|
|
|
&KmSourceFileRootDir);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
goto Cleanup;
|
|
|
|
CapturedSourceFile.RootDirectory = KmSourceFileRootDir;
|
|
|
|
CapturedSourceFile.Attributes |= OBJ_KERNEL_HANDLE;
|
|
|
|
|
2007-11-22 18:38:32 +00:00
|
|
|
/* Check if we have a trust class */
|
|
|
|
if (TrustClassKey)
|
|
|
|
{
|
|
|
|
/* Reference it */
|
|
|
|
Status = ObReferenceObjectByHandle(TrustClassKey,
|
|
|
|
0,
|
|
|
|
CmpKeyObjectType,
|
|
|
|
PreviousMode,
|
2018-10-20 17:17:55 +00:00
|
|
|
(PVOID*)&KeyBody,
|
2007-11-22 18:38:32 +00:00
|
|
|
NULL);
|
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-11-22 18:38:32 +00:00
|
|
|
/* Call the internal API */
|
2018-10-21 13:42:13 +00:00
|
|
|
Status = CmLoadKey(&CapturedTargetKey,
|
|
|
|
&CapturedSourceFile,
|
|
|
|
Flags,
|
|
|
|
KeyBody);
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-11-22 18:38:32 +00:00
|
|
|
/* Dereference the trust key, if any */
|
|
|
|
if (KeyBody) ObDereferenceObject(KeyBody);
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
Cleanup:
|
|
|
|
/* Close the local kernel handles */
|
|
|
|
if (KmSourceFileRootDir)
|
|
|
|
ObCloseHandle(KmSourceFileRootDir, KernelMode);
|
|
|
|
if (KmTargetKeyRootDir)
|
|
|
|
ObCloseHandle(KmTargetKeyRootDir, KernelMode);
|
|
|
|
|
|
|
|
/* Release the captured object attributes */
|
|
|
|
ReleaseCapturedObjectAttributes(&CapturedSourceFile, PreviousMode);
|
|
|
|
ReleaseCapturedObjectAttributes(&CapturedTargetKey, PreviousMode);
|
|
|
|
|
|
|
|
Quit:
|
2007-11-22 18:38:32 +00:00
|
|
|
/* Bring back APCs */
|
|
|
|
KeLeaveCriticalRegion();
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-11-22 18:38:32 +00:00
|
|
|
/* Return status */
|
|
|
|
return Status;
|
2007-11-02 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 21:00:45 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtNotifyChangeKey(IN HANDLE KeyHandle,
|
|
|
|
IN HANDLE Event,
|
|
|
|
IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
|
|
|
|
IN PVOID ApcContext OPTIONAL,
|
|
|
|
OUT PIO_STATUS_BLOCK IoStatusBlock,
|
|
|
|
IN ULONG CompletionFilter,
|
|
|
|
IN BOOLEAN WatchTree,
|
|
|
|
OUT PVOID Buffer,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN BOOLEAN Asynchronous)
|
|
|
|
{
|
|
|
|
/* Call the newer API */
|
|
|
|
return NtNotifyChangeMultipleKeys(KeyHandle,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
Event,
|
|
|
|
ApcRoutine,
|
|
|
|
ApcContext,
|
|
|
|
IoStatusBlock,
|
|
|
|
CompletionFilter,
|
|
|
|
WatchTree,
|
|
|
|
Buffer,
|
|
|
|
Length,
|
|
|
|
Asynchronous);
|
|
|
|
}
|
|
|
|
|
2007-12-02 18:40:33 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtInitializeRegistry(IN USHORT Flag)
|
|
|
|
{
|
|
|
|
BOOLEAN SetupBoot;
|
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Always do this as kernel mode */
|
2017-06-02 15:47:52 +00:00
|
|
|
if (KeGetPreviousMode() == UserMode)
|
|
|
|
return ZwInitializeRegistry(Flag);
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2010-01-28 15:51:18 +00:00
|
|
|
/* Enough of the system has booted by now */
|
2010-02-01 03:51:45 +00:00
|
|
|
Ki386PerfEnd();
|
2014-11-04 20:47:18 +00:00
|
|
|
|
2007-12-02 18:40:33 +00:00
|
|
|
/* Validate flag */
|
|
|
|
if (Flag > CM_BOOT_FLAG_MAX) return STATUS_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
/* Check if boot was accepted */
|
|
|
|
if ((Flag >= CM_BOOT_FLAG_ACCEPTED) && (Flag <= CM_BOOT_FLAG_MAX))
|
|
|
|
{
|
|
|
|
/* Only allow once */
|
|
|
|
if (!CmBootAcceptFirstTime) return STATUS_ACCESS_DENIED;
|
|
|
|
CmBootAcceptFirstTime = FALSE;
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-12-02 18:40:33 +00:00
|
|
|
/* Get the control set accepted */
|
|
|
|
Flag -= CM_BOOT_FLAG_ACCEPTED;
|
|
|
|
if (Flag)
|
|
|
|
{
|
2018-12-16 11:48:56 +00:00
|
|
|
/* Save the last known good boot */
|
|
|
|
Status = CmpSaveBootControlSet(Flag);
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-12-02 18:40:33 +00:00
|
|
|
/* Notify HAL */
|
|
|
|
HalEndOfBoot();
|
|
|
|
|
|
|
|
/* Enable lazy flush */
|
|
|
|
CmpHoldLazyFlush = FALSE;
|
|
|
|
CmpLazyFlush();
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, invalid boot */
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2007-12-02 18:40:33 +00:00
|
|
|
/* Check if this was a setup boot */
|
|
|
|
SetupBoot = (Flag == CM_BOOT_FLAG_SETUP ? TRUE : FALSE);
|
|
|
|
|
|
|
|
/* Make sure we're only called once */
|
|
|
|
if (!CmFirstTime) return STATUS_ACCESS_DENIED;
|
|
|
|
CmFirstTime = FALSE;
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2017-06-02 15:47:52 +00:00
|
|
|
/* Lock the registry exclusively */
|
|
|
|
CmpLockRegistryExclusive();
|
2007-12-02 18:40:33 +00:00
|
|
|
|
|
|
|
/* Initialize the hives and lazy flusher */
|
|
|
|
CmpCmdInit(SetupBoot);
|
|
|
|
|
2011-05-08 21:37:00 +00:00
|
|
|
/* Save version data */
|
|
|
|
CmpSetVersionData();
|
2007-12-02 18:40:33 +00:00
|
|
|
|
|
|
|
/* Release the registry lock */
|
2017-06-02 15:47:52 +00:00
|
|
|
CmpUnlockRegistry();
|
2007-12-02 18:40:33 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-12-08 21:00:45 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtCompactKeys(IN ULONG Count,
|
|
|
|
IN PHANDLE KeyArray)
|
|
|
|
{
|
|
|
|
UNIMPLEMENTED;
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtCompressKey(IN HANDLE Key)
|
|
|
|
{
|
|
|
|
UNIMPLEMENTED;
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2014-11-04 20:47:18 +00:00
|
|
|
// FIXME: different for different windows versions!
|
|
|
|
#define PRODUCT_ACTIVATION_VERSION 7749
|
|
|
|
|
2007-11-02 23:12:22 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtLockProductActivationKeys(IN PULONG pPrivateVer,
|
|
|
|
IN PULONG pSafeMode)
|
|
|
|
{
|
2014-11-04 20:47:18 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode;
|
|
|
|
|
|
|
|
PreviousMode = ExGetPreviousMode();
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
/* Check if the caller asked for the version */
|
|
|
|
if (pPrivateVer != NULL)
|
|
|
|
{
|
|
|
|
/* For user mode, probe it */
|
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
2019-12-29 09:33:38 +00:00
|
|
|
ProbeForWriteUlong(pPrivateVer);
|
2014-11-04 20:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the expected version */
|
|
|
|
*pPrivateVer = PRODUCT_ACTIVATION_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the caller asked for safe mode mode state */
|
|
|
|
if (pSafeMode != NULL)
|
|
|
|
{
|
|
|
|
/* For user mode, probe it */
|
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
2019-12-29 09:33:38 +00:00
|
|
|
ProbeForWriteUlong(pSafeMode);
|
2014-11-04 20:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the safe boot mode state */
|
|
|
|
*pSafeMode = InitSafeBootMode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
2016-09-07 18:52:09 +00:00
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2014-11-04 20:47:18 +00:00
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
2007-11-02 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtLockRegistryKey(IN HANDLE KeyHandle)
|
|
|
|
{
|
|
|
|
UNIMPLEMENTED;
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtNotifyChangeMultipleKeys(IN HANDLE MasterKeyHandle,
|
|
|
|
IN ULONG Count,
|
|
|
|
IN POBJECT_ATTRIBUTES SlaveObjects,
|
|
|
|
IN HANDLE Event,
|
|
|
|
IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
|
|
|
|
IN PVOID ApcContext OPTIONAL,
|
|
|
|
OUT PIO_STATUS_BLOCK IoStatusBlock,
|
|
|
|
IN ULONG CompletionFilter,
|
|
|
|
IN BOOLEAN WatchTree,
|
|
|
|
OUT PVOID Buffer,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN BOOLEAN Asynchronous)
|
|
|
|
{
|
2018-01-08 20:18:30 +00:00
|
|
|
UNIMPLEMENTED_ONCE;
|
2007-11-02 23:12:22 +00:00
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtQueryMultipleValueKey(IN HANDLE KeyHandle,
|
|
|
|
IN OUT PKEY_VALUE_ENTRY ValueList,
|
|
|
|
IN ULONG NumberOfValues,
|
|
|
|
OUT PVOID Buffer,
|
|
|
|
IN OUT PULONG Length,
|
|
|
|
OUT PULONG ReturnLength)
|
|
|
|
{
|
|
|
|
UNIMPLEMENTED;
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtQueryOpenSubKeys(IN POBJECT_ATTRIBUTES TargetKey,
|
2008-08-03 19:24:09 +00:00
|
|
|
OUT PULONG HandleCount)
|
2007-11-02 23:12:22 +00:00
|
|
|
{
|
2010-09-26 11:37:40 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode;
|
|
|
|
PCM_KEY_BODY KeyBody = NULL;
|
|
|
|
HANDLE KeyHandle;
|
|
|
|
NTSTATUS Status;
|
2019-12-29 09:00:59 +00:00
|
|
|
ULONG SubKeys;
|
2010-09-26 11:37:40 +00:00
|
|
|
|
|
|
|
DPRINT("NtQueryOpenSubKeys()\n");
|
|
|
|
|
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Get the processor mode */
|
|
|
|
PreviousMode = KeGetPreviousMode();
|
|
|
|
|
2018-10-20 17:17:55 +00:00
|
|
|
/* Check for user-mode caller */
|
2010-09-26 11:37:40 +00:00
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
|
|
|
/* Prepare to probe parameters */
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
/* Probe target key */
|
|
|
|
ProbeForRead(TargetKey,
|
|
|
|
sizeof(OBJECT_ATTRIBUTES),
|
|
|
|
sizeof(ULONG));
|
|
|
|
|
|
|
|
/* Probe handle count */
|
|
|
|
ProbeForWriteUlong(HandleCount);
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
/* Return the exception code */
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open a handle to the key */
|
|
|
|
Status = ObOpenObjectByName(TargetKey,
|
|
|
|
CmpKeyObjectType,
|
|
|
|
PreviousMode,
|
|
|
|
NULL,
|
|
|
|
KEY_READ,
|
|
|
|
NULL,
|
|
|
|
&KeyHandle);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Reference the key object */
|
|
|
|
Status = ObReferenceObjectByHandle(KeyHandle,
|
|
|
|
KEY_READ,
|
|
|
|
CmpKeyObjectType,
|
|
|
|
PreviousMode,
|
2018-10-20 17:17:55 +00:00
|
|
|
(PVOID*)&KeyBody,
|
2010-09-26 11:37:40 +00:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Close the handle */
|
|
|
|
NtClose(KeyHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fail, if the key object could not be referenced */
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return Status;
|
|
|
|
|
|
|
|
/* Lock the registry exclusively */
|
|
|
|
CmpLockRegistryExclusive();
|
|
|
|
|
|
|
|
/* Fail, if we did not open a hive root key */
|
|
|
|
if (KeyBody->KeyControlBlock->KeyCell !=
|
|
|
|
KeyBody->KeyControlBlock->KeyHive->BaseBlock->RootCell)
|
|
|
|
{
|
|
|
|
DPRINT("Error: Key is not a hive root key!\n");
|
|
|
|
CmpUnlockRegistry();
|
|
|
|
ObDereferenceObject(KeyBody);
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call the internal API */
|
2019-12-29 09:00:59 +00:00
|
|
|
SubKeys = CmpEnumerateOpenSubKeys(KeyBody->KeyControlBlock,
|
|
|
|
FALSE, FALSE);
|
2010-09-26 11:37:40 +00:00
|
|
|
|
|
|
|
/* Unlock the registry */
|
|
|
|
CmpUnlockRegistry();
|
|
|
|
|
|
|
|
/* Dereference the key object */
|
|
|
|
ObDereferenceObject(KeyBody);
|
|
|
|
|
2019-12-29 09:00:59 +00:00
|
|
|
/* Write back the result */
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
*HandleCount = SubKeys;
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
Status = _SEH2_GetExceptionCode();
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
|
2010-09-26 11:37:40 +00:00
|
|
|
DPRINT("Done.\n");
|
|
|
|
|
|
|
|
return Status;
|
2007-11-02 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtQueryOpenSubKeysEx(IN POBJECT_ATTRIBUTES TargetKey,
|
|
|
|
IN ULONG BufferLength,
|
|
|
|
IN PVOID Buffer,
|
|
|
|
IN PULONG RequiredSize)
|
|
|
|
{
|
|
|
|
UNIMPLEMENTED;
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2008-01-05 17:29:44 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtRenameKey(IN HANDLE KeyHandle,
|
|
|
|
IN PUNICODE_STRING ReplacementName)
|
|
|
|
{
|
|
|
|
UNIMPLEMENTED;
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2007-11-02 23:12:22 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtReplaceKey(IN POBJECT_ATTRIBUTES ObjectAttributes,
|
|
|
|
IN HANDLE Key,
|
|
|
|
IN POBJECT_ATTRIBUTES ReplacedObjectAttributes)
|
|
|
|
{
|
|
|
|
UNIMPLEMENTED;
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtRestoreKey(IN HANDLE KeyHandle,
|
|
|
|
IN HANDLE FileHandle,
|
|
|
|
IN ULONG RestoreFlags)
|
|
|
|
{
|
|
|
|
UNIMPLEMENTED;
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtSaveKey(IN HANDLE KeyHandle,
|
|
|
|
IN HANDLE FileHandle)
|
|
|
|
{
|
2014-05-29 19:44:36 +00:00
|
|
|
/* Call the extended API */
|
|
|
|
return NtSaveKeyEx(KeyHandle, FileHandle, REG_STANDARD_FORMAT);
|
2007-11-02 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtSaveKeyEx(IN HANDLE KeyHandle,
|
|
|
|
IN HANDLE FileHandle,
|
|
|
|
IN ULONG Flags)
|
|
|
|
{
|
2014-05-29 19:44:36 +00:00
|
|
|
NTSTATUS Status;
|
2018-10-21 13:42:13 +00:00
|
|
|
HANDLE KmFileHandle = NULL;
|
2014-05-29 19:44:36 +00:00
|
|
|
PCM_KEY_BODY KeyObject;
|
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
|
|
|
|
|
|
|
PAGED_CODE();
|
|
|
|
|
2015-02-08 11:49:58 +00:00
|
|
|
DPRINT("NtSaveKeyEx(0x%p, 0x%p, %lu)\n", KeyHandle, FileHandle, Flags);
|
2014-05-29 19:44:36 +00:00
|
|
|
|
|
|
|
/* Verify the flags */
|
|
|
|
if ((Flags != REG_STANDARD_FORMAT)
|
|
|
|
&& (Flags != REG_LATEST_FORMAT)
|
|
|
|
&& (Flags != REG_NO_COMPRESSION))
|
|
|
|
{
|
|
|
|
/* Only one of these values can be specified */
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2018-10-20 17:17:55 +00:00
|
|
|
/* Validate privilege */
|
2014-05-29 19:44:36 +00:00
|
|
|
if (!SeSinglePrivilegeCheck(SeBackupPrivilege, PreviousMode))
|
|
|
|
{
|
|
|
|
return STATUS_PRIVILEGE_NOT_HELD;
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Make sure the target file handle is a kernel handle */
|
|
|
|
Status = CmpConvertHandleToKernelHandle(FileHandle,
|
|
|
|
IoFileObjectType,
|
|
|
|
FILE_WRITE_DATA,
|
|
|
|
PreviousMode,
|
|
|
|
&KmFileHandle);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
goto Quit;
|
|
|
|
|
2014-05-29 19:44:36 +00:00
|
|
|
/* Verify that the handle is valid and is a registry key */
|
|
|
|
Status = ObReferenceObjectByHandle(KeyHandle,
|
|
|
|
KEY_READ,
|
|
|
|
CmpKeyObjectType,
|
|
|
|
PreviousMode,
|
|
|
|
(PVOID*)&KeyObject,
|
|
|
|
NULL);
|
2018-10-21 13:42:13 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
goto Quit;
|
2014-05-29 19:44:36 +00:00
|
|
|
|
|
|
|
/* Call the internal API */
|
2018-10-21 13:42:13 +00:00
|
|
|
Status = CmSaveKey(KeyObject->KeyControlBlock, KmFileHandle, Flags);
|
2014-05-29 19:44:36 +00:00
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Dereference the registry key */
|
2014-05-29 19:44:36 +00:00
|
|
|
ObDereferenceObject(KeyObject);
|
2018-10-21 13:42:13 +00:00
|
|
|
|
|
|
|
Quit:
|
|
|
|
/* Close the local kernel handle */
|
|
|
|
if (KmFileHandle)
|
|
|
|
ObCloseHandle(KmFileHandle, KernelMode);
|
|
|
|
|
2014-05-29 19:44:36 +00:00
|
|
|
return Status;
|
2007-11-02 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtSaveMergedKeys(IN HANDLE HighPrecedenceKeyHandle,
|
|
|
|
IN HANDLE LowPrecedenceKeyHandle,
|
|
|
|
IN HANDLE FileHandle)
|
|
|
|
{
|
2018-10-21 13:42:13 +00:00
|
|
|
NTSTATUS Status;
|
2015-02-08 11:49:58 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode;
|
2018-10-21 13:42:13 +00:00
|
|
|
HANDLE KmFileHandle = NULL;
|
2015-02-08 11:49:58 +00:00
|
|
|
PCM_KEY_BODY HighPrecedenceKeyObject = NULL;
|
|
|
|
PCM_KEY_BODY LowPrecedenceKeyObject = NULL;
|
|
|
|
|
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
DPRINT("NtSaveMergedKeys(0x%p, 0x%p, 0x%p)\n",
|
|
|
|
HighPrecedenceKeyHandle, LowPrecedenceKeyHandle, FileHandle);
|
|
|
|
|
|
|
|
PreviousMode = ExGetPreviousMode();
|
|
|
|
|
2018-10-20 17:17:55 +00:00
|
|
|
/* Validate privilege */
|
2015-02-08 11:49:58 +00:00
|
|
|
if (!SeSinglePrivilegeCheck(SeBackupPrivilege, PreviousMode))
|
|
|
|
{
|
|
|
|
return STATUS_PRIVILEGE_NOT_HELD;
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Make sure the target file handle is a kernel handle */
|
|
|
|
Status = CmpConvertHandleToKernelHandle(FileHandle,
|
|
|
|
IoFileObjectType,
|
|
|
|
FILE_WRITE_DATA,
|
|
|
|
PreviousMode,
|
|
|
|
&KmFileHandle);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
goto Quit;
|
|
|
|
|
2015-02-08 11:49:58 +00:00
|
|
|
/* Verify that the handles are valid and are registry keys */
|
|
|
|
Status = ObReferenceObjectByHandle(HighPrecedenceKeyHandle,
|
|
|
|
KEY_READ,
|
|
|
|
CmpKeyObjectType,
|
|
|
|
PreviousMode,
|
|
|
|
(PVOID*)&HighPrecedenceKeyObject,
|
|
|
|
NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2018-10-21 13:42:13 +00:00
|
|
|
goto Quit;
|
2015-02-08 11:49:58 +00:00
|
|
|
|
|
|
|
Status = ObReferenceObjectByHandle(LowPrecedenceKeyHandle,
|
|
|
|
KEY_READ,
|
|
|
|
CmpKeyObjectType,
|
|
|
|
PreviousMode,
|
|
|
|
(PVOID*)&LowPrecedenceKeyObject,
|
|
|
|
NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2018-10-21 13:42:13 +00:00
|
|
|
goto Quit;
|
2015-02-08 11:49:58 +00:00
|
|
|
|
|
|
|
/* Call the internal API */
|
|
|
|
Status = CmSaveMergedKeys(HighPrecedenceKeyObject->KeyControlBlock,
|
|
|
|
LowPrecedenceKeyObject->KeyControlBlock,
|
2018-10-21 13:42:13 +00:00
|
|
|
KmFileHandle);
|
2015-02-08 11:49:58 +00:00
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
Quit:
|
|
|
|
/* Dereference the opened key objects */
|
2015-02-08 11:49:58 +00:00
|
|
|
if (LowPrecedenceKeyObject)
|
|
|
|
ObDereferenceObject(LowPrecedenceKeyObject);
|
|
|
|
if (HighPrecedenceKeyObject)
|
|
|
|
ObDereferenceObject(HighPrecedenceKeyObject);
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Close the local kernel handle */
|
|
|
|
if (KmFileHandle)
|
|
|
|
ObCloseHandle(KmFileHandle, KernelMode);
|
|
|
|
|
2015-02-08 11:49:58 +00:00
|
|
|
return Status;
|
2007-11-02 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtSetInformationKey(IN HANDLE KeyHandle,
|
|
|
|
IN KEY_SET_INFORMATION_CLASS KeyInformationClass,
|
|
|
|
IN PVOID KeyInformation,
|
|
|
|
IN ULONG KeyInformationLength)
|
|
|
|
{
|
|
|
|
UNIMPLEMENTED;
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtUnloadKey(IN POBJECT_ATTRIBUTES KeyObjectAttributes)
|
|
|
|
{
|
2009-08-10 13:44:54 +00:00
|
|
|
return NtUnloadKey2(KeyObjectAttributes, 0);
|
2007-11-02 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtUnloadKey2(IN POBJECT_ATTRIBUTES TargetKey,
|
|
|
|
IN ULONG Flags)
|
|
|
|
{
|
2009-08-24 19:58:15 +00:00
|
|
|
NTSTATUS Status;
|
2018-10-21 13:42:13 +00:00
|
|
|
OBJECT_ATTRIBUTES CapturedTargetKey;
|
2009-08-10 13:44:54 +00:00
|
|
|
UNICODE_STRING ObjectName;
|
2018-10-21 13:42:13 +00:00
|
|
|
HANDLE KmTargetKeyRootDir = NULL;
|
2009-08-10 13:44:54 +00:00
|
|
|
CM_PARSE_CONTEXT ParseContext = {0};
|
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
|
|
|
PCM_KEY_BODY KeyBody = NULL;
|
|
|
|
ULONG ParentConv = 0, ChildConv = 0;
|
|
|
|
HANDLE Handle;
|
2015-06-27 19:26:12 +00:00
|
|
|
|
2009-08-10 13:44:54 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Validate privilege */
|
|
|
|
if (!SeSinglePrivilegeCheck(SeRestorePrivilege, PreviousMode))
|
|
|
|
{
|
|
|
|
DPRINT1("Restore Privilege missing!\n");
|
|
|
|
return STATUS_PRIVILEGE_NOT_HELD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for user-mode caller */
|
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
|
|
|
/* Prepare to probe parameters */
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
/* Probe object attributes */
|
|
|
|
ProbeForRead(TargetKey,
|
|
|
|
sizeof(OBJECT_ATTRIBUTES),
|
|
|
|
sizeof(ULONG));
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
CapturedTargetKey = *TargetKey;
|
2009-08-10 13:44:54 +00:00
|
|
|
|
|
|
|
/* Probe the string */
|
2018-10-21 13:42:13 +00:00
|
|
|
ObjectName = ProbeForReadUnicodeString(CapturedTargetKey.ObjectName);
|
2009-08-10 13:44:54 +00:00
|
|
|
ProbeForRead(ObjectName.Buffer,
|
|
|
|
ObjectName.Length,
|
|
|
|
sizeof(WCHAR));
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
CapturedTargetKey.ObjectName = &ObjectName;
|
2009-08-10 13:44:54 +00:00
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
2009-08-24 19:58:15 +00:00
|
|
|
/* Return the exception code */
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2009-08-10 13:44:54 +00:00
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Save the target attributes directly */
|
2018-10-21 13:42:13 +00:00
|
|
|
CapturedTargetKey = *TargetKey;
|
2009-08-10 13:44:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Make sure the target key root directory handle is a kernel handle */
|
|
|
|
Status = CmpConvertHandleToKernelHandle(CapturedTargetKey.RootDirectory,
|
|
|
|
CmpKeyObjectType,
|
|
|
|
KEY_WRITE,
|
|
|
|
PreviousMode,
|
|
|
|
&KmTargetKeyRootDir);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return Status;
|
|
|
|
CapturedTargetKey.RootDirectory = KmTargetKeyRootDir;
|
|
|
|
CapturedTargetKey.Attributes |= OBJ_KERNEL_HANDLE;
|
|
|
|
|
2009-08-10 13:44:54 +00:00
|
|
|
/* Setup the parse context */
|
|
|
|
ParseContext.CreateOperation = TRUE;
|
|
|
|
ParseContext.CreateOptions = REG_OPTION_BACKUP_RESTORE;
|
|
|
|
|
|
|
|
/* Do the create */
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Open a local handle to the key */
|
|
|
|
Status = ObOpenObjectByName(&CapturedTargetKey,
|
2009-08-10 13:44:54 +00:00
|
|
|
CmpKeyObjectType,
|
|
|
|
KernelMode,
|
|
|
|
NULL,
|
|
|
|
KEY_WRITE,
|
|
|
|
&ParseContext,
|
|
|
|
&Handle);
|
2018-10-21 13:42:13 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Reference the key object */
|
|
|
|
Status = ObReferenceObjectByHandle(Handle,
|
|
|
|
KEY_WRITE,
|
|
|
|
CmpKeyObjectType,
|
|
|
|
KernelMode,
|
|
|
|
(PVOID*)&KeyBody,
|
|
|
|
NULL);
|
2009-08-10 13:44:54 +00:00
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Close the handle */
|
|
|
|
ObCloseHandle(Handle, KernelMode);
|
|
|
|
}
|
2009-08-10 13:44:54 +00:00
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Close the local kernel handle */
|
|
|
|
if (KmTargetKeyRootDir)
|
|
|
|
ObCloseHandle(KmTargetKeyRootDir, KernelMode);
|
2009-08-10 13:44:54 +00:00
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Return if a failure was encountered */
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return Status;
|
2009-08-10 13:44:54 +00:00
|
|
|
|
|
|
|
/* Acquire the lock depending on flags */
|
|
|
|
if (Flags == REG_FORCE_UNLOAD)
|
|
|
|
{
|
|
|
|
/* Lock registry exclusively */
|
|
|
|
CmpLockRegistryExclusive();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Lock registry */
|
|
|
|
CmpLockRegistry();
|
|
|
|
|
|
|
|
/* Acquire the hive loading lock */
|
|
|
|
ExAcquirePushLockExclusive(&CmpLoadHiveLock);
|
|
|
|
|
|
|
|
/* Lock parent and child */
|
|
|
|
if (KeyBody->KeyControlBlock->ParentKcb)
|
|
|
|
ParentConv = KeyBody->KeyControlBlock->ParentKcb->ConvKey;
|
|
|
|
else
|
|
|
|
ParentConv = KeyBody->KeyControlBlock->ConvKey;
|
|
|
|
|
|
|
|
ChildConv = KeyBody->KeyControlBlock->ConvKey;
|
|
|
|
|
|
|
|
CmpAcquireTwoKcbLocksExclusiveByKey(ChildConv, ParentConv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if it's being deleted already */
|
|
|
|
if (KeyBody->KeyControlBlock->Delete)
|
|
|
|
{
|
|
|
|
/* Return appropriate status */
|
|
|
|
Status = STATUS_KEY_DELETED;
|
2018-10-21 13:42:13 +00:00
|
|
|
goto Quit;
|
2009-08-10 13:44:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-20 17:17:55 +00:00
|
|
|
/* Check if it's a read-only key */
|
2009-08-10 13:44:54 +00:00
|
|
|
if (KeyBody->KeyControlBlock->ExtFlags & CM_KCB_READ_ONLY_KEY)
|
|
|
|
{
|
|
|
|
/* Return appropriate status */
|
|
|
|
Status = STATUS_ACCESS_DENIED;
|
2018-10-21 13:42:13 +00:00
|
|
|
goto Quit;
|
2009-08-10 13:44:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* Call the internal API. Note that CmUnloadKey() unlocks the registry only on success. */
|
|
|
|
Status = CmUnloadKey(KeyBody->KeyControlBlock, Flags);
|
2009-08-10 13:44:54 +00:00
|
|
|
|
|
|
|
/* Check if we failed, but really need to succeed */
|
|
|
|
if ((Status == STATUS_CANNOT_DELETE) && (Flags == REG_FORCE_UNLOAD))
|
|
|
|
{
|
|
|
|
/* TODO: We should perform another attempt here */
|
2018-10-21 13:42:13 +00:00
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
DPRINT1("NtUnloadKey2(%wZ): We want to force-unload the hive but couldn't unload it: Retrying is UNIMPLEMENTED!\n", TargetKey->ObjectName);
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
2009-08-10 13:44:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
/* If CmUnloadKey() failed we need to unlock registry ourselves */
|
2009-08-10 13:44:54 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
if (Flags != REG_FORCE_UNLOAD)
|
|
|
|
{
|
2017-06-16 21:02:27 +00:00
|
|
|
/* Release the KCB locks */
|
2009-08-10 13:44:54 +00:00
|
|
|
CmpReleaseTwoKcbLockByKey(ChildConv, ParentConv);
|
2015-06-27 19:26:12 +00:00
|
|
|
|
|
|
|
/* Release the hive loading lock */
|
|
|
|
ExReleasePushLockExclusive(&CmpLoadHiveLock);
|
2009-08-10 13:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock the registry */
|
|
|
|
CmpUnlockRegistry();
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:42:13 +00:00
|
|
|
Quit:
|
2009-08-10 13:44:54 +00:00
|
|
|
/* Dereference the key */
|
|
|
|
ObDereferenceObject(KeyBody);
|
|
|
|
|
|
|
|
/* Return status */
|
|
|
|
return Status;
|
2007-11-02 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtUnloadKeyEx(IN POBJECT_ATTRIBUTES TargetKey,
|
|
|
|
IN HANDLE Event)
|
|
|
|
{
|
|
|
|
UNIMPLEMENTED;
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|