[NTOS:CM] Use the appropriate flags on functions that will call CmCheckRegistry & add missing CmCheckRegistry calls

In addition to that, in some functions like CmFlushKey, CmSaveKey and CmSaveMergedKeys we must validate the underlying hives as a matter of precaution that everything is alright and we don't fuck all the shit up.
This commit is contained in:
George Bișoc 2022-10-26 19:59:21 +02:00
parent f33da480af
commit d2b8b9ec96
No known key found for this signature in database
GPG key ID: 688C4FBE25D7DEF6
3 changed files with 73 additions and 10 deletions

View file

@ -1939,6 +1939,9 @@ CmFlushKey(IN PCM_KEY_CONTROL_BLOCK Kcb,
IN BOOLEAN ExclusiveLock)
{
PCMHIVE CmHive;
#if DBG
CM_CHECK_REGISTRY_STATUS CheckStatus;
#endif
NTSTATUS Status = STATUS_SUCCESS;
PHHIVE Hive;
@ -1957,6 +1960,12 @@ CmFlushKey(IN PCM_KEY_CONTROL_BLOCK Kcb,
}
else
{
#if DBG
/* Make sure the registry hive we're going to flush is OK */
CheckStatus = CmCheckRegistry(CmHive, CM_CHECK_REGISTRY_DONT_PURGE_VOLATILES | CM_CHECK_REGISTRY_VALIDATE_HIVE);
ASSERT(CM_CHECK_REGISTRY_SUCCESS(CheckStatus));
#endif
/* Don't touch the hive */
CmpLockHiveFlusherExclusive(CmHive);
@ -2054,7 +2063,7 @@ CmLoadKey(IN POBJECT_ATTRIBUTES TargetKey,
&ClientSecurityContext,
&Allocate,
&CmHive,
0);
CM_CHECK_REGISTRY_PURGE_VOLATILES);
/* Get rid of the security context */
SeDeleteClientSecurity(&ClientSecurityContext);
@ -2645,6 +2654,10 @@ CmSaveKey(IN PCM_KEY_CONTROL_BLOCK Kcb,
IN HANDLE FileHandle,
IN ULONG Flags)
{
#if DBG
CM_CHECK_REGISTRY_STATUS CheckStatus;
PCMHIVE HiveToValidate = NULL;
#endif
NTSTATUS Status = STATUS_SUCCESS;
PCMHIVE KeyHive = NULL;
PAGED_CODE();
@ -2655,6 +2668,11 @@ CmSaveKey(IN PCM_KEY_CONTROL_BLOCK Kcb,
CmpLockRegistry();
CmpAcquireKcbLockShared(Kcb);
#if DBG
/* Get the hive for validation */
HiveToValidate = (PCMHIVE)Kcb->KeyHive;
#endif
if (Kcb->Delete)
{
/* The source key has been deleted, do nothing */
@ -2669,6 +2687,12 @@ CmSaveKey(IN PCM_KEY_CONTROL_BLOCK Kcb,
goto Cleanup;
}
#if DBG
/* Make sure this control block has a sane hive */
CheckStatus = CmCheckRegistry(HiveToValidate, CM_CHECK_REGISTRY_DONT_PURGE_VOLATILES | CM_CHECK_REGISTRY_VALIDATE_HIVE);
ASSERT(CM_CHECK_REGISTRY_SUCCESS(CheckStatus));
#endif
/* Create a new hive that will hold the key */
Status = CmpInitializeHive(&KeyHive,
HINIT_CREATE,
@ -2679,7 +2703,7 @@ CmSaveKey(IN PCM_KEY_CONTROL_BLOCK Kcb,
NULL,
NULL,
NULL,
0);
CM_CHECK_REGISTRY_DONT_PURGE_VOLATILES);
if (!NT_SUCCESS(Status)) goto Cleanup;
/* Copy the key recursively into the new hive */
@ -2701,6 +2725,15 @@ Cleanup:
/* Free the hive */
if (KeyHive) CmpDestroyHive(KeyHive);
#if DBG
if (NT_SUCCESS(Status))
{
/* Before we say goodbye, make sure the hive is still OK */
CheckStatus = CmCheckRegistry(HiveToValidate, CM_CHECK_REGISTRY_DONT_PURGE_VOLATILES | CM_CHECK_REGISTRY_VALIDATE_HIVE);
ASSERT(CM_CHECK_REGISTRY_SUCCESS(CheckStatus));
}
#endif
/* Release the locks */
CmpReleaseKcbLock(Kcb);
CmpUnlockRegistry();
@ -2714,6 +2747,11 @@ CmSaveMergedKeys(IN PCM_KEY_CONTROL_BLOCK HighKcb,
IN PCM_KEY_CONTROL_BLOCK LowKcb,
IN HANDLE FileHandle)
{
#if DBG
CM_CHECK_REGISTRY_STATUS CheckStatus;
PCMHIVE LowHiveToValidate = NULL;
PCMHIVE HighHiveToValidate = NULL;
#endif
PCMHIVE KeyHive = NULL;
NTSTATUS Status = STATUS_SUCCESS;
@ -2726,6 +2764,12 @@ CmSaveMergedKeys(IN PCM_KEY_CONTROL_BLOCK HighKcb,
CmpAcquireKcbLockShared(HighKcb);
CmpAcquireKcbLockShared(LowKcb);
#if DBG
/* Get the high and low hives for validation */
HighHiveToValidate = (PCMHIVE)HighKcb->KeyHive;
LowHiveToValidate = (PCMHIVE)LowKcb->KeyHive;
#endif
if (LowKcb->Delete || HighKcb->Delete)
{
/* The source key has been deleted, do nothing */
@ -2733,6 +2777,14 @@ CmSaveMergedKeys(IN PCM_KEY_CONTROL_BLOCK HighKcb,
goto done;
}
#if DBG
/* Make sure that both the high and low precedence hives are OK */
CheckStatus = CmCheckRegistry(HighHiveToValidate, CM_CHECK_REGISTRY_DONT_PURGE_VOLATILES | CM_CHECK_REGISTRY_VALIDATE_HIVE);
ASSERT(CM_CHECK_REGISTRY_SUCCESS(CheckStatus));
CheckStatus = CmCheckRegistry(LowHiveToValidate, CM_CHECK_REGISTRY_DONT_PURGE_VOLATILES | CM_CHECK_REGISTRY_VALIDATE_HIVE);
ASSERT(CM_CHECK_REGISTRY_SUCCESS(CheckStatus));
#endif
/* Create a new hive that will hold the key */
Status = CmpInitializeHive(&KeyHive,
HINIT_CREATE,
@ -2743,7 +2795,7 @@ CmSaveMergedKeys(IN PCM_KEY_CONTROL_BLOCK HighKcb,
NULL,
NULL,
NULL,
0);
CM_CHECK_REGISTRY_DONT_PURGE_VOLATILES);
if (!NT_SUCCESS(Status))
goto done;
@ -2776,6 +2828,17 @@ done:
if (KeyHive)
CmpDestroyHive(KeyHive);
#if DBG
if (NT_SUCCESS(Status))
{
/* Check those hives again before we say goodbye */
CheckStatus = CmCheckRegistry(HighHiveToValidate, CM_CHECK_REGISTRY_DONT_PURGE_VOLATILES | CM_CHECK_REGISTRY_VALIDATE_HIVE);
ASSERT(CM_CHECK_REGISTRY_SUCCESS(CheckStatus));
CheckStatus = CmCheckRegistry(LowHiveToValidate, CM_CHECK_REGISTRY_DONT_PURGE_VOLATILES | CM_CHECK_REGISTRY_VALIDATE_HIVE);
ASSERT(CM_CHECK_REGISTRY_SUCCESS(CheckStatus));
}
#endif
/* Release the locks */
CmpReleaseKcbLock(LowKcb);
CmpReleaseKcbLock(HighKcb);

View file

@ -201,8 +201,8 @@ CmpInitializeHive(OUT PCMHIVE *CmHive,
(OperationType == HINIT_MAPFILE))
{
/* Verify integrity */
ULONG CheckStatus = CmCheckRegistry(Hive, CheckFlags);
if (CheckStatus != 0)
CM_CHECK_REGISTRY_STATUS CheckStatus = CmCheckRegistry(Hive, CheckFlags);
if (!CM_CHECK_REGISTRY_SUCCESS(CheckStatus))
{
/* Cleanup allocations and fail */
ExDeleteResourceLite(Hive->FlusherLock);

View file

@ -912,7 +912,7 @@ CmpInitializeSystemHive(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
NULL,
NULL,
&HiveName,
HiveBase ? 2 : 0);
HiveBase ? CM_CHECK_REGISTRY_PURGE_VOLATILES : CM_CHECK_REGISTRY_DONT_PURGE_VOLATILES);
if (!NT_SUCCESS(Status))
{
return FALSE;
@ -936,7 +936,7 @@ CmpInitializeSystemHive(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
{
/* Disable self-healing internally and check if boot type wanted it */
CmpSelfHeal = FALSE;
if (CmpBootType & 4)
if (CmpBootType & HBOOT_TYPE_SELF_HEAL)
{
/* We're disabled, so bugcheck */
KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO,
@ -1245,7 +1245,7 @@ CmpLoadHiveThread(IN PVOID StartContext)
CmpMachineHiveList[i].HHiveFlags,
&CmHive,
&CmpMachineHiveList[i].Allocate,
0);
CM_CHECK_REGISTRY_PURGE_VOLATILES);
if (!(NT_SUCCESS(Status)) ||
(!(CmpShareSystemHives) && !(CmHive->FileHandles[HFILE_TYPE_LOG])))
{
@ -1565,7 +1565,7 @@ CmInitSystem1(VOID)
NULL,
NULL,
NULL,
0);
CM_CHECK_REGISTRY_DONT_PURGE_VOLATILES);
if (!NT_SUCCESS(Status))
{
/* Bugcheck */
@ -1656,7 +1656,7 @@ CmInitSystem1(VOID)
NULL,
NULL,
NULL,
0);
CM_CHECK_REGISTRY_DONT_PURGE_VOLATILES);
if (!NT_SUCCESS(Status))
{
/* Bugcheck */