How can I created shared memory that can be accessed at Dispatch-Level in my driver and accessed from user-mode? I tried to do the following but I am fairly sure this created a pageable section of memory.
sectionSize.QuadPart = sizeof(INPUT_DATA);
RtlInitUnicodeString(§ionUniName, L"\\BaseNamedObjects\\SharedSection");
InitializeObjectAttributes(&objAttributes, §ionUniName, OBJ_KERNEL_HANDLE, NULL, NULL);
if (!NT_SUCCESS(status = ZwCreateSection(&g_hSection, PAGE_READWRITE, &objAttributes, §ionSize, PAGE_READWRITE, SEC_COMMIT, NULL))) {
DebugMessage("Failed mouse ZwCreateSection \n");
}
else if (!NT_SUCCESS(status = ZwMapViewOfSection(g_hSection, NtCurrentProcess(), &g_pSharedBuffer, 0, sizeof(INPUT_DATA), NULL, &viewSize, ViewUnmap, 0, PAGE_READWRITE))) {
DebugMessage("Failed mouse ZwMapViewOfSection \n");
}
I also tried to create the memory in my C# application and then send a pointer to that address along with other info like the memory’s size and such but I am fairly sure this is also pageable and maybe not an ideal way of doing it.
HANDLE usermodeHandle = verifyData->SharedMemoryHandle;
// Convert the user-mode handle to a kernel object.
PVOID sharedSection;
status = ObReferenceObjectByHandle(
usermodeHandle,
SECTION_MAP_WRITE | SECTION_MAP_READ,
NULL, // Use NULL for type to bypass strict type checking.
UserMode,
(PVOID*)&sharedSection,
NULL
);
if (!NT_SUCCESS(status)) {
// Handle error.
DebugMessage("Failed ObReferenceObjectByHandle \n");
break;
}
// Map the section into the system address space.
SIZE_T viewSize = sizeof(INPUT_DATA);
status = MmMapViewInSystemSpace(sharedSection, &g_SharedBuffer, &viewSize);
if (!NT_SUCCESS(status)) {
ObDereferenceObject(sharedSection);
break;
}
Anyways I just am trying to figure out the best way to go about this which I think might be some variation of that first solution but I am not totally sure what the standard way of accomplishing would be. I rather not create a ‘KDPC’ and go through all of the work of dropping the IRQL especially since I need these operations done quickly. Also I plan to call ‘RtlCopyMemory’ at Dispatch-Level to copy data to this shared memory which hopefully can then be accessed from my client application after using ‘KeSetEvent’ after it to denote a change in memory. My driver wont need to read any memory from this shared memory it just needs to write to it.