zl程序教程

您现在的位置是:首页 >  硬件

当前栏目

标准MDL方法修改Page、NonPage内存的属性

2023-09-27 14:26:38 时间
  1. typedef struct _REPROTECT_CONTEXT  
  2. {  
  3.     PMDL   Mdl;  
  4.     PUCHAR LockedVa;  
  5. } REPROTECT_CONTEXT, * PREPROTECT_CONTEXT;  
  6.    
  7. NTSTATUS  
  8. MmLockVaForWrite(  
  9.     __in PVOID Va,  
  10.     __in ULONG Length,  
  11.     __out PREPROTECT_CONTEXT ReprotectContext  
  12.     )  
  13. {  
  14.     NTSTATUS Status;  
  15.   
  16.     Status = STATUS_SUCCESS;  
  17.   
  18.     ReprotectContext->Mdl      = 0;  
  19.     ReprotectContext->LockedVa = 0;  
  20.   
  21.     ReprotectContext->Mdl = IoAllocateMdl(  
  22.         Va,  
  23.         Length,  
  24.         FALSE,  
  25.         FALSE,  
  26.         0  
  27.         );  
  28.   
  29.     if (!ReprotectContext->Mdl)  
  30.     {  
  31.         return STATUS_INSUFFICIENT_RESOURCES;  
  32.     }  
  33.   
  34.     //  
  35.     // Retrieve a locked VA mapping.  
  36.     //  
  37.   
  38.     __try  
  39.     {  
  40.         MmProbeAndLockPages(  
  41.             ReprotectContext->Mdl,  
  42.             KernelMode,  
  43.             IoModifyAccess  
  44.             );  
  45.     }  
  46.     __except (EXCEPTION_EXECUTE_HANDLER)  
  47.     {  
  48.        return GetExceptionCode();  
  49.     }  
  50.   
  51.     ReprotectContext->LockedVa = (PUCHAR)MmMapLockedPagesSpecifyCache(  
  52.         ReprotectContext->Mdl,  
  53.         KernelMode,  
  54.         MmCached,  
  55.         0,  
  56.         FALSE,  
  57.         NormalPagePriority  
  58.         );  
  59.   
  60.     if (!ReprotectContext->LockedVa)  
  61.     {  
  62.          
  63.   
  64.         IoFreeMdl(  
  65.             ReprotectContext->Mdl  
  66.             );  
  67.   
  68.         ReprotectContext->Mdl = 0;  
  69.   
  70.         return STATUS_ACCESS_VIOLATION;  
  71.     }  
  72.   
  73.     //  
  74.     // Reprotect.  
  75.     //  
  76.   
  77.     Status = MmProtectMdlSystemAddress(  
  78.         ReprotectContext->Mdl,  
  79.         PAGE_EXECUTE_READWRITE  
  80.         );  
  81.   
  82.     if (!NT_SUCCESS(Status))  
  83.     {  
  84.   
  85.   
  86.         MmUnmapLockedPages(  
  87.             ReprotectContext->LockedVa,  
  88.             ReprotectContext->Mdl  
  89.             );  
  90.         MmUnlockPages(  
  91.             ReprotectContext->Mdl  
  92.             );  
  93.         IoFreeMdl(  
  94.             ReprotectContext->Mdl  
  95.             );  
  96.   
  97.         ReprotectContext->LockedVa = 0;  
  98.         ReprotectContext->Mdl      = 0;  
  99.     }  
  100.   
  101.     return Status;  
  102. }  
  103.   
  104. NTSTATUS  
  105. MmUnlockVaForWrite(  
  106.     __in PREPROTECT_CONTEXT ReprotectContext  
  107.     )  
  108. {  
  109.     if (ReprotectContext->LockedVa)  
  110.     {  
  111.         MmUnmapLockedPages(  
  112.             ReprotectContext->LockedVa,  
  113.             ReprotectContext->Mdl  
  114.             );  
  115.         MmUnlockPages(  
  116.             ReprotectContext->Mdl  
  117.             );  
  118.         IoFreeMdl(  
  119.             ReprotectContext->Mdl  
  120.             );  
  121.   
  122.         ReprotectContext->LockedVa = 0;  
  123.         ReprotectContext->Mdl      = 0;  
  124.     }  
  125.   
  126.     return STATUS_SUCCESS;  
  127. }