从用户模式访问内核内存(Windows)

时间:2015-04-29 08:42:37

标签: windows memory kernel driver

我正在编写一个需要分配非分页内存池的驱动程序,出于性能考虑,这个内存必须可以直接从用户模式程序访问。

在驱动程序条目中,我已经为这两种方法分配了一些内存:

pMdl = IoAllocateMdl(NULL,
                     4096,
                     FALSE,
                     FALSE,
                     NULL);
if(!pMdl) {
    DbgPrintEx(DPFLTR_IHVVIDEO_ID, DPFLTR_INFO_LEVEL, "Error on IoAllocateMdl. Returning from driver early.\n");
    return STATUS_INSUFFICIENT_RESOURCES;
}
MmBuildMdlForNonPagedPool(pMdl);
userMemory = (void *)MmMapLockedPagesSpecifyCache(pMdl, UserMode, MmWriteCombined, NULL, FALSE, LowPagePriority);

userMemory = ExAllocatePoolWithTag(
                NonPagedPool,
                4096,
                POOL_TAG);

现在我不想每次需要从这个内存写入/读取时发出一个DeviceIoControl,而是我想做这样的事情:

char* sharedMem;
.....
transactionResult = DeviceIoControl ( hDevice,
                        (DWORD) IOCTL_MMAP,
                        NULL,
                        0,
                        sharedMem,
                        sizeof(int),
                        &bRetur,
                        NULL
                        );
.....
sharedMem[0]='c';

使用DeviceIoControl获取内核内存中的地址,然后直接使用它,就像Linux下的mmap一样。

在Windows中有什么办法吗?

我做到了这一点:

hMapFile = OpenFileMapping(
                FILE_MAP_ALL_ACCESS,     // Read/write access
                TRUE,
                "Global\\SharedMemory"); // Name of mapping object

lastError = GetLastError();
if (hMapFile == NULL)
{
    printf("Could not create file mapping object (%d).\n" ,GetLastError());
    return 1;
}
pBuf = (char*)MapViewOfFile(hMapFile,            // Handle to map object
                            FILE_MAP_ALL_ACCESS, // Read/write permission
                            0,
                            0,
                            4096);

if (pBuf == NULL)
{
    printf("Could not map view of file (%d).\n", GetLastError());
    CloseHandle(hMapFile);
    return 1;
}
pBuf[0] = 'c';
pBuf[1] = '\n';
CloseHandle(hMapFile);

我在内核中创建了这样的视图:

RtlInitUnicodeString(&name, L"\\BaseNamedObjects\\SharedMemory");
InitializeObjectAttributes(&oa, &name, 0, 0, NULL);
ZwCreateSection(&hsection, SECTION_ALL_ACCESS, &oa, &Li, PAGE_READWRITE, SEC_COMMIT, NULL);

ZwMapViewOfSection(hsection, NtCurrentProcess(),
                   &userMem, 0, MEM_WIDTH, NULL,
                   &j, ViewShare, 0, PAGE_READWRITE);

但是在内核中我读取内存时它是空的:它怎么可能?

1 个答案:

答案 0 :(得分:3)

我终于明白了这是如何运作的。

首先,我创建了如下结构。

 <form id="loginForm" method='post'>

这将用于将虚拟地址从内核空间返回到用户空间。

在我使用过的DriverEntry中

typedef struct _MEMORY_ENTRY
{
    PVOID pBuffer;
} MEMORY_ENTRY, *PMEMORY_ENTRY;

设置NonPaged内存。

然后我创建了一个在DIRECT_OUT模式下工作的IOCTL,它执行以下代码片段:

userMem = ExAllocatePoolWithTag(NonPagedPool,
                                MEM_WIDTH,
                                POOL_TAG );

在用户模式程序中我只需要这个

...
PMDL         mdl = NULL;
PVOID        buffer = NULL;
MEMORY_ENTRY returnedValue;
 void*       UserVirtualAddress = NULL;
...
buffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority); // Gets safely the pointer for the output in the IRP
mdl = IoAllocateMdl(userMem, MEM_WIDTH, FALSE, FALSE, NULL); // Allocate the memory descriptor list
MmBuildMdlForNonPagedPool(mdl); // This is needed when we're managing NonPaged memory
UserVirtualAddress = MmMapLockedPagesSpecifyCache(
                         mdl,
                         UserMode,
                         MmNonCached,
                         NULL,
                         FALSE,
                         NormalPagePriority); // Return the virtual address in the context of
                                              // the user space program who called the IOCTL

returnedValue.pBuffer = UserVirtualAddress;
RtlCopyMemory(buffer,
            &returnedValue,
            sizeof(PVOID)); // I copy the virtual address in the structure that will
                            // be returned to the user mode program by the IRP

在(MEMORY_ENTRY *)sharedMem-&gt; pBuffer中,我们将找到由内核和用户程序直接访问的内核空间创建和共享的内存区域。

我还没写过,但我们需要记住将整个MmGetSystemAddressForMdlSafe(...)-----&gt; RtlCopyMemory(...)包装在Try ... Except块中,因为我们可以遇到各种各样的这里的问题最终可能会导致BugCheck更安全而不是抱歉。 无论如何,如果您在已检查的环境中编译此类代码,Microsoft AutocodeReview将指出这一点。

如果有人需要更多澄清,或者我写错了,请告诉我,我很乐意修改这篇文章。

相关问题