[NTOS:MM] Reformat MmCallDllInitialize and MiCallDllUnloadAndUnloadDll.

This commit is contained in:
Hermès Bélusca-Maïto 2023-08-19 19:39:56 +02:00
parent aa719b9989
commit e8b048a282
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
2 changed files with 35 additions and 30 deletions

View file

@ -1653,9 +1653,8 @@ MmCheckSystemImage(
NTSTATUS NTSTATUS
NTAPI NTAPI
MmCallDllInitialize( MmCallDllInitialize(
IN PLDR_DATA_TABLE_ENTRY LdrEntry, _In_ PLDR_DATA_TABLE_ENTRY LdrEntry,
IN PLIST_ENTRY ListHead _In_ PLIST_ENTRY ModuleListHead);
);
VOID VOID
NTAPI NTAPI

View file

@ -292,24 +292,26 @@ MiLocateExportName(IN PVOID DllBase,
NTSTATUS NTSTATUS
NTAPI NTAPI
MmCallDllInitialize(IN PLDR_DATA_TABLE_ENTRY LdrEntry, MmCallDllInitialize(
IN PLIST_ENTRY ListHead) _In_ PLDR_DATA_TABLE_ENTRY LdrEntry,
_In_ PLIST_ENTRY ModuleListHead)
{ {
UNICODE_STRING ServicesKeyName = RTL_CONSTANT_STRING( UNICODE_STRING ServicesKeyName = RTL_CONSTANT_STRING(
L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\"); L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\");
PMM_DLL_INITIALIZE DllInit; PMM_DLL_INITIALIZE DllInit;
UNICODE_STRING RegPath, ImportName; UNICODE_STRING RegPath, ImportName;
PCWCH Extension;
NTSTATUS Status; NTSTATUS Status;
PAGED_CODE();
/* Try to see if the image exports a DllInitialize routine */ /* Try to see if the image exports a DllInitialize routine */
DllInit = (PMM_DLL_INITIALIZE)MiLocateExportName(LdrEntry->DllBase, DllInit = (PMM_DLL_INITIALIZE)MiLocateExportName(LdrEntry->DllBase,
"DllInitialize"); "DllInitialize");
if (!DllInit) return STATUS_SUCCESS; if (!DllInit)
return STATUS_SUCCESS;
/* /* Make a temporary copy of BaseDllName because we will alter its length */
* Do a temporary copy of BaseDllName called ImportName
* because we'll alter the length of the string.
*/
ImportName.Length = LdrEntry->BaseDllName.Length; ImportName.Length = LdrEntry->BaseDllName.Length;
ImportName.MaximumLength = LdrEntry->BaseDllName.MaximumLength; ImportName.MaximumLength = LdrEntry->BaseDllName.MaximumLength;
ImportName.Buffer = LdrEntry->BaseDllName.Buffer; ImportName.Buffer = LdrEntry->BaseDllName.Buffer;
@ -322,7 +324,8 @@ MmCallDllInitialize(IN PLDR_DATA_TABLE_ENTRY LdrEntry,
TAG_LDR_WSTR); TAG_LDR_WSTR);
/* Check if this allocation was unsuccessful */ /* Check if this allocation was unsuccessful */
if (!RegPath.Buffer) return STATUS_INSUFFICIENT_RESOURCES; if (!RegPath.Buffer)
return STATUS_INSUFFICIENT_RESOURCES;
/* Build and append the service name itself */ /* Build and append the service name itself */
RegPath.Length = ServicesKeyName.Length; RegPath.Length = ServicesKeyName.Length;
@ -330,49 +333,52 @@ MmCallDllInitialize(IN PLDR_DATA_TABLE_ENTRY LdrEntry,
ServicesKeyName.Buffer, ServicesKeyName.Buffer,
ServicesKeyName.Length); ServicesKeyName.Length);
/* Check if there is a dot in the filename */ /* If the filename has an extension, remove it */
if (wcschr(ImportName.Buffer, L'.')) Extension = wcschr(ImportName.Buffer, L'.');
{ if (Extension)
/* Remove the extension */ ImportName.Length = (USHORT)(Extension - ImportName.Buffer) * sizeof(WCHAR);
ImportName.Length = (USHORT)(wcschr(ImportName.Buffer, L'.') -
ImportName.Buffer) * sizeof(WCHAR);
}
/* Append service name (the basename without extension) */ /* Append the service name (base name without extension) */
RtlAppendUnicodeStringToString(&RegPath, &ImportName); RtlAppendUnicodeStringToString(&RegPath, &ImportName);
/* Now call the DllInit func */ /* Now call DllInitialize */
DPRINT("Calling DllInit(%wZ)\n", &RegPath); DPRINT("Calling DllInit(%wZ)\n", &RegPath);
Status = DllInit(&RegPath); Status = DllInit(&RegPath);
/* Clean up */ /* Clean up */
ExFreePoolWithTag(RegPath.Buffer, TAG_LDR_WSTR); ExFreePoolWithTag(RegPath.Buffer, TAG_LDR_WSTR);
/* Return status value which DllInitialize returned */ // TODO: This is for Driver Verifier support.
UNREFERENCED_PARAMETER(ModuleListHead);
/* Return the DllInitialize status value */
return Status; return Status;
} }
BOOLEAN BOOLEAN
NTAPI MiCallDllUnloadAndUnloadDll(
MiCallDllUnloadAndUnloadDll(IN PLDR_DATA_TABLE_ENTRY LdrEntry) _In_ PLDR_DATA_TABLE_ENTRY LdrEntry)
{ {
NTSTATUS Status; NTSTATUS Status;
PMM_DLL_UNLOAD Func; PMM_DLL_UNLOAD DllUnload;
PAGED_CODE(); PAGED_CODE();
/* Get the unload routine */ /* Retrieve the DllUnload routine */
Func = (PMM_DLL_UNLOAD)MiLocateExportName(LdrEntry->DllBase, "DllUnload"); DllUnload = (PMM_DLL_UNLOAD)MiLocateExportName(LdrEntry->DllBase, "DllUnload");
if (!Func) return FALSE; if (!DllUnload)
return FALSE;
/* Call it and check for success */ /* Call it and check for success */
Status = Func(); Status = DllUnload();
if (!NT_SUCCESS(Status)) return FALSE; if (!NT_SUCCESS(Status))
return FALSE;
/* Lie about the load count so we can unload the image */ /* Lie about the load count so we can unload the image */
ASSERT(LdrEntry->LoadCount == 0); ASSERT(LdrEntry->LoadCount == 0);
LdrEntry->LoadCount = 1; LdrEntry->LoadCount = 1;
/* Unload it and return true */ /* Unload it */
MmUnloadSystemImage(LdrEntry); MmUnloadSystemImage(LdrEntry);
return TRUE; return TRUE;
} }