https://git.reactos.org/?p=reactos.git;a=commitdiff;h=3625238ecb5eec1b65d6efd756013d3738bacb95

commit 3625238ecb5eec1b65d6efd756013d3738bacb95
Author:     Hermès Bélusca-Maïto <[email protected]>
AuthorDate: Sun Jul 14 00:04:19 2019 +0200
Commit:     Hermès Bélusca-Maïto <[email protected]>
CommitDate: Sun Jul 14 22:23:49 2019 +0200

    [NTOS:MM] Add a MmChangeKernelResourceSectionProtection() helper. (#1649)
    
    This allows setting the memory protection of the kernel's resource
    section as will. MmMakeKernelResourceSectionWritable() is re-implemented
    around this helper.
---
 ntoskrnl/include/internal/mm.h |  4 ++++
 ntoskrnl/mm/ARM3/sysldr.c      | 41 +++++++++++++++++++++++++++++------------
 2 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/ntoskrnl/include/internal/mm.h b/ntoskrnl/include/internal/mm.h
index da3a720d4dc..32fd7b1146d 100644
--- a/ntoskrnl/include/internal/mm.h
+++ b/ntoskrnl/include/internal/mm.h
@@ -1370,6 +1370,10 @@ MiInitializeLoadedModuleList(
     IN PLOADER_PARAMETER_BLOCK LoaderBlock
 );
 
+BOOLEAN
+NTAPI
+MmChangeKernelResourceSectionProtection(IN ULONG_PTR ProtectionMask);
+
 VOID
 NTAPI
 MmMakeKernelResourceSectionWritable(VOID);
diff --git a/ntoskrnl/mm/ARM3/sysldr.c b/ntoskrnl/mm/ARM3/sysldr.c
index 036c38505d6..ab6e1165cc1 100644
--- a/ntoskrnl/mm/ARM3/sysldr.c
+++ b/ntoskrnl/mm/ARM3/sysldr.c
@@ -2281,20 +2281,20 @@ MiInitializeLoadedModuleList(IN PLOADER_PARAMETER_BLOCK 
LoaderBlock)
     return TRUE;
 }
 
-VOID
+BOOLEAN
 NTAPI
-MmMakeKernelResourceSectionWritable(VOID)
+MmChangeKernelResourceSectionProtection(IN ULONG_PTR ProtectionMask)
 {
     PMMPTE PointerPte;
     MMPTE TempPte;
 
     /* Don't do anything if the resource section is already writable */
     if (MiKernelResourceStartPte == NULL || MiKernelResourceEndPte == NULL)
-        return;
+        return FALSE;
 
     /* If the resource section is physical, we cannot change its protection */
     if (MI_IS_PHYSICAL_ADDRESS(MiPteToAddress(MiKernelResourceStartPte)))
-        return;
+        return FALSE;
 
     /* Loop the PTEs */
     for (PointerPte = MiKernelResourceStartPte; PointerPte < 
MiKernelResourceEndPte; ++PointerPte)
@@ -2303,19 +2303,36 @@ MmMakeKernelResourceSectionWritable(VOID)
         TempPte = *PointerPte;
 
         /* Update the protection */
-        MI_MAKE_HARDWARE_PTE_KERNEL(&TempPte, PointerPte, MM_READWRITE, 
TempPte.u.Hard.PageFrameNumber);
+        MI_MAKE_HARDWARE_PTE_KERNEL(&TempPte, PointerPte, ProtectionMask, 
TempPte.u.Hard.PageFrameNumber);
         MI_UPDATE_VALID_PTE(PointerPte, TempPte);
     }
 
-    /*
-     * Invalidate the cached resource section PTEs
-     * so as to not change its protection again later.
-     */
-    MiKernelResourceStartPte = NULL;
-    MiKernelResourceEndPte = NULL;
-
     /* Only flush the current processor's TLB */
     KeFlushCurrentTb();
+    return TRUE;
+}
+
+VOID
+NTAPI
+MmMakeKernelResourceSectionWritable(VOID)
+{
+    /* Don't do anything if the resource section is already writable */
+    if (MiKernelResourceStartPte == NULL || MiKernelResourceEndPte == NULL)
+        return;
+
+    /* If the resource section is physical, we cannot change its protection */
+    if (MI_IS_PHYSICAL_ADDRESS(MiPteToAddress(MiKernelResourceStartPte)))
+        return;
+
+    if (MmChangeKernelResourceSectionProtection(MM_READWRITE))
+    {
+        /*
+         * Invalidate the cached resource section PTEs
+         * so as to not change its protection again later.
+         */
+        MiKernelResourceStartPte = NULL;
+        MiKernelResourceEndPte = NULL;
+    }
 }
 
 LOGICAL

Reply via email to