Skip to content

Commit 6c515f5

Browse files
authored
[NTOS:MM] Release system image PTEs on unload (reactos#9078)
MmUnloadSystemImage() had an explicit leak placeholder for driver images. Implement the MmUnloadSystemImage() that performs matching teardown for the image mappings, releasing the reserved system PTE range. Also fix similar leak in MiLoadImageSection() by releasing image mappings after load failures. CORE-8294 Note that the driver images loaded at boot, which were not reloaded by MiReloadBootLoadedDrivers(), remain at their loader-assigned addresses and do not own system PTEs.
1 parent ec4af44 commit 6c515f5

1 file changed

Lines changed: 58 additions & 4 deletions

File tree

‎ntoskrnl/mm/ARM3/sysldr.c‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,37 @@ MiLoadImageSection(_Inout_ PSECTION *SectionPtr,
214214
return Status;
215215
}
216216

217+
/**
218+
* @brief
219+
* Undoes the mapping performed by MiLoadImageSection(): frees the pages
220+
* backing the image, and returns the system PTEs it had reserved.
221+
*
222+
* @param[in] ImageBase
223+
* The base address of the image located in the system PTE space,
224+
* as returned by MiLoadImageSection().
225+
* Do not pass the original base addresses of the boot-start drivers here.
226+
*
227+
* @param[in] ImageSize
228+
* The size of the image, in bytes.
229+
**/
230+
static
231+
VOID
232+
MiUnmapSystemImage(
233+
_In_ PVOID ImageBase,
234+
_In_ ULONG ImageSize)
235+
{
236+
PMMPTE BasePte = MiAddressToPte(ImageBase);
237+
PFN_COUNT NumberOfPages = BYTES_TO_PAGES(ImageSize);
238+
239+
/* TODO: Support large-page and session image mappings */
240+
NT_ASSERT(!MI_IS_PHYSICAL_ADDRESS(ImageBase));
241+
NT_ASSERT(!MI_IS_SESSION_ADDRESS(ImageBase));
242+
243+
/* Free the pages, then give the reserved PTEs back */
244+
MiDeleteSystemPageableVm(BasePte, NumberOfPages, 0, NULL);
245+
MiReleaseSystemPtes(BasePte, NumberOfPages, SystemPteSpace);
246+
}
247+
217248
#ifndef RVA
218249
#define RVA(m, b) ((PVOID)((ULONG_PTR)(b) + (ULONG_PTR)(m)))
219250
#endif
@@ -985,9 +1016,20 @@ MmUnloadSystemImage(IN PVOID ImageHandle)
9851016
}
9861017
}
9871018

988-
/* FIXME: Free the driver */
989-
DPRINT1("Leaking driver: %wZ\n", &LdrEntry->BaseDllName);
990-
//MmFreeSection(LdrEntry->DllBase);
1019+
/*
1020+
* Delete the image mapping and return its system PTEs. Only images that
1021+
* MiLoadImageSection or MiReloadBootLoadedDrivers put into system PTE
1022+
* space can be released this way; the boot drivers that the latter had
1023+
* to skip still sit wherever the bootloader placed them.
1024+
*/
1025+
if (LdrEntry->Flags & LDRP_SYSTEM_MAPPED)
1026+
{
1027+
MiUnmapSystemImage(LdrEntry->DllBase, LdrEntry->SizeOfImage);
1028+
}
1029+
else
1030+
{
1031+
DPRINT1("Leaking non system-mapped image: %wZ\n", &LdrEntry->BaseDllName);
1032+
}
9911033

9921034
/* Check if we're linked in */
9931035
if (LdrEntry->InLoadOrderLinks.Flink)
@@ -2961,7 +3003,7 @@ MmLoadSystemImage(IN PUNICODE_STRING FileName,
29613003
PIMAGE_NT_HEADERS NtHeader;
29623004
UNICODE_STRING BaseName, BaseDirectory, PrefixName;
29633005
PLDR_DATA_TABLE_ENTRY LdrEntry = NULL;
2964-
ULONG EntrySize, DriverSize;
3006+
ULONG EntrySize, DriverSize = 0;
29653007
PLOAD_IMPORTS LoadedImports = MM_SYSLDR_NO_IMPORTS;
29663008
PCHAR MissingApiName, Buffer;
29673009
PWCHAR MissingDriverName, PrefixedBuffer = NULL;
@@ -3503,6 +3545,18 @@ MmLoadSystemImage(IN PUNICODE_STRING FileName,
35033545
*ImageBaseAddress = LdrEntry->DllBase;
35043546

35053547
Quickie:
3548+
/*
3549+
* If we failed after the image had been mapped, tear the mapping down.
3550+
* ModuleLoadBase is only set once MiLoadImageSection() has reserved the
3551+
* system PTEs for it, and the load lock is still held at that point.
3552+
*/
3553+
if (!NT_SUCCESS(Status) && (ModuleLoadBase != NULL))
3554+
{
3555+
ASSERT(LockOwned);
3556+
MiUnmapSystemImage(ModuleLoadBase, DriverSize);
3557+
ModuleLoadBase = NULL;
3558+
}
3559+
35063560
/* Check if we have the lock acquired */
35073561
if (LockOwned)
35083562
{

0 commit comments

Comments
 (0)