找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 2474|回复: 0

转载一段代码,MDL修改page,nonpag内存属性的代码

[复制链接]

3

主题

13

回帖

0

精华

铜牌会员

积分
131
发表于 2012-2-3 12:56:38 | 显示全部楼层 |阅读模式
  1. typedef struct _REPROTECT_CONTEXT
  2. {
  3.     PMDL   Mdl;
  4.     PUCHAR LockedVa;
  5. } REPROTECT_CONTEXT, * PREPROTECT_CONTEXT;

  6. NTSTATUS
  7. MmLockVaForWrite(
  8.     __in PVOID Va,
  9.     __in ULONG Length,
  10.     __out PREPROTECT_CONTEXT ReprotectContext
  11.     )
  12. {
  13.     NTSTATUS Status;

  14.     Status = STATUS_SUCCESS;

  15.     ReprotectContext->Mdl      = 0;
  16.     ReprotectContext->LockedVa = 0;

  17.     ReprotectContext->Mdl = IoAllocateMdl(
  18.         Va,
  19.         Length,
  20.         FALSE,
  21.         FALSE,
  22.         0
  23.         );

  24.     if (!ReprotectContext->Mdl)
  25.     {
  26.         return STATUS_INSUFFICIENT_RESOURCES;
  27.     }

  28.     //
  29.     // Retrieve a locked VA mapping.
  30.     //

  31.     __try
  32.     {
  33.         MmProbeAndLockPages(
  34.             ReprotectContext->Mdl,
  35.             KernelMode,
  36.             IoModifyAccess
  37.             );
  38.     }
  39.     __except (EXCEPTION_EXECUTE_HANDLER)
  40.     {
  41.        return GetExceptionCode();
  42.     }

  43.     ReprotectContext->LockedVa = (PUCHAR)MmMapLockedPagesSpecifyCache(
  44.         ReprotectContext->Mdl,
  45.         KernelMode,
  46.         MmCached,
  47.         0,
  48.         FALSE,
  49.         NormalPagePriority
  50.         );

  51.     if (!ReprotectContext->LockedVa)
  52.     {
  53.       

  54.         IoFreeMdl(
  55.             ReprotectContext->Mdl
  56.             );

  57.         ReprotectContext->Mdl = 0;

  58.         return STATUS_ACCESS_VIOLATION;
  59.     }

  60.     //
  61.     // Reprotect.
  62.     //

  63.     Status = MmProtectMdlSystemAddress(
  64.         ReprotectContext->Mdl,
  65.         PAGE_EXECUTE_READWRITE
  66.         );

  67.     if (!NT_SUCCESS(Status))
  68.     {


  69.         MmUnmapLockedPages(
  70.             ReprotectContext->LockedVa,
  71.             ReprotectContext->Mdl
  72.             );
  73.         MmUnlockPages(
  74.             ReprotectContext->Mdl
  75.             );
  76.         IoFreeMdl(
  77.             ReprotectContext->Mdl
  78.             );

  79.         ReprotectContext->LockedVa = 0;
  80.         ReprotectContext->Mdl      = 0;
  81.     }

  82.     return Status;
  83. }

  84. NTSTATUS
  85. MmUnlockVaForWrite(
  86.     __in PREPROTECT_CONTEXT ReprotectContext
  87.     )
  88. {
  89.     if (ReprotectContext->LockedVa)
  90.     {
  91.         MmUnmapLockedPages(
  92.             ReprotectContext->LockedVa,
  93.             ReprotectContext->Mdl
  94.             );
  95.         MmUnlockPages(
  96.             ReprotectContext->Mdl
  97.             );
  98.         IoFreeMdl(
  99.             ReprotectContext->Mdl
  100.             );

  101.         ReprotectContext->LockedVa = 0;
  102.         ReprotectContext->Mdl      = 0;
  103.     }

  104.     return STATUS_SUCCESS;
  105. }

复制代码
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表