[NTOS:CM] Disable hard errors when setting up a new size for a hive file / annotate CmpFileSetSize parameters with SAL

During a I/O failure of whatever kind the upper-level driver, namely a FSD, can raise a hard error and a deadlock can occur. We wouldn't want that to happen for particular files like hives or logs so in such cases we must disable hard errors before toying with hives until we're done.

In addition to that, annotate the CmpFileSetSize function's parameters with SAL.
This commit is contained in:
George Bișoc 2022-10-26 19:00:08 +02:00
parent 0d776beac9
commit bfcb28787d
No known key found for this signature in database
GPG key ID: 688C4FBE25D7DEF6
2 changed files with 33 additions and 10 deletions

View file

@ -129,21 +129,32 @@ CmpFileWrite(IN PHHIVE RegistryHive,
BOOLEAN
NTAPI
CmpFileSetSize(IN PHHIVE RegistryHive,
IN ULONG FileType,
IN ULONG FileSize,
IN ULONG OldFileSize)
CmpFileSetSize(
_In_ PHHIVE RegistryHive,
_In_ ULONG FileType,
_In_ ULONG FileSize,
_In_ ULONG OldFileSize)
{
PCMHIVE CmHive = (PCMHIVE)RegistryHive;
HANDLE HiveHandle = CmHive->FileHandles[FileType];
FILE_END_OF_FILE_INFORMATION EndOfFileInfo;
FILE_ALLOCATION_INFORMATION FileAllocationInfo;
IO_STATUS_BLOCK IoStatusBlock;
BOOLEAN HardErrors;
NTSTATUS Status;
/* Just return success if no file is associated with this hive */
if (HiveHandle == NULL)
{
DPRINT1("No hive handle associated with the given hive\n");
return TRUE;
}
/*
* Disable hard errors so that we don't deadlock
* when touching with the hive files.
*/
HardErrors = IoSetThreadHardErrorMode(FALSE);
EndOfFileInfo.EndOfFile.QuadPart = FileSize;
Status = ZwSetInformationFile(HiveHandle,
@ -151,7 +162,12 @@ CmpFileSetSize(IN PHHIVE RegistryHive,
&EndOfFileInfo,
sizeof(FILE_END_OF_FILE_INFORMATION),
FileEndOfFileInformation);
if (!NT_SUCCESS(Status)) return FALSE;
if (!NT_SUCCESS(Status))
{
DPRINT1("ZwSetInformationFile failed to set new size of end of file (Status 0x%lx)\n", Status);
IoSetThreadHardErrorMode(HardErrors);
return FALSE;
}
FileAllocationInfo.AllocationSize.QuadPart = FileSize;
Status = ZwSetInformationFile(HiveHandle,
@ -159,8 +175,15 @@ CmpFileSetSize(IN PHHIVE RegistryHive,
&FileAllocationInfo,
sizeof(FILE_ALLOCATION_INFORMATION),
FileAllocationInformation);
if (!NT_SUCCESS(Status)) return FALSE;
if (!NT_SUCCESS(Status))
{
DPRINT1("ZwSetInformationFile failed to set new of allocation file (Status 0x%lx)\n", Status);
IoSetThreadHardErrorMode(HardErrors);
return FALSE;
}
/* Reset the hard errors back */
IoSetThreadHardErrorMode(HardErrors);
return TRUE;
}

View file

@ -1236,10 +1236,10 @@ CmpFileWrite(
BOOLEAN
NTAPI
CmpFileSetSize(
IN PHHIVE RegistryHive,
IN ULONG FileType,
IN ULONG FileSize,
IN ULONG OldFileSize
_In_ PHHIVE RegistryHive,
_In_ ULONG FileType,
_In_ ULONG FileSize,
_In_ ULONG OldFileSize
);
BOOLEAN