| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#if defined(_MSC_VER) |
| 27 |
#include <ntddk.h> |
| 28 |
#ifndef STDCALL |
| 29 |
#define STDCALL |
| 30 |
#endif |
| 31 |
#elif defined(__MINGW32__) |
| 32 |
#ifndef STDCALL |
| 33 |
#define STDCALL __attribute__((stdcall)) |
| 34 |
#endif |
| 35 |
#include <ddk/ntddk.h> |
| 36 |
#define NO_SEH |
| 37 |
#else |
| 38 |
#error Unsupported compiler. This driver requires MSVC+DDK or MinGW to build. |
| 39 |
#endif |
| 40 |
|
| 41 |
#include "dhahelper.h" |
| 42 |
|
| 43 |
#define OutputDebugString DbgPrint |
| 44 |
|
| 45 |
#define IOPM_SIZE 0x2000 |
| 46 |
typedef char IOPM[IOPM_SIZE]; |
| 47 |
static IOPM *pIOPM = NULL; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
typedef struct { |
| 52 |
PMDL Mdl; |
| 53 |
PVOID SystemVirtualAddress; |
| 54 |
PVOID UserVirtualAddress; |
| 55 |
ULONG PhysMemSizeInBytes; |
| 56 |
}alloc_priv; |
| 57 |
static alloc_priv* alloclist; |
| 58 |
static unsigned int alloccount=0; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static STDCALL NTSTATUS dhahelperdispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp); |
| 67 |
static STDCALL void dhahelperunload(IN PDRIVER_OBJECT DriverObject); |
| 68 |
static STDCALL NTSTATUS UnmapPhysicalMemory(PVOID UserVirtualAddress); |
| 69 |
static STDCALL NTSTATUS MapPhysicalMemoryToLinearSpace(PVOID pPhysAddress,ULONG PhysMemSizeInBytes,PVOID *PhysMemLin); |
| 70 |
|
| 71 |
void STDCALL Ke386SetIoAccessMap(int, IOPM *); |
| 72 |
void STDCALL Ke386QueryIoAccessMap(int, IOPM *); |
| 73 |
void STDCALL Ke386IoSetAccessProcess(PEPROCESS, int); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
STDCALL NTSTATUS DriverEntry (IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath){ |
| 80 |
UNICODE_STRING DeviceNameUnicodeString; |
| 81 |
UNICODE_STRING DeviceLinkUnicodeString; |
| 82 |
NTSTATUS ntStatus; |
| 83 |
PDEVICE_OBJECT DeviceObject = NULL; |
| 84 |
|
| 85 |
OutputDebugString ("dhahelper: entering DriverEntry"); |
| 86 |
|
| 87 |
RtlInitUnicodeString (&DeviceNameUnicodeString, L"\\Device\\DHAHELPER"); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
ntStatus = IoCreateDevice(DriverObject,0,&DeviceNameUnicodeString,FILE_DEVICE_DHAHELPER,0,TRUE,&DeviceObject); |
| 93 |
|
| 94 |
if (NT_SUCCESS(ntStatus)){ |
| 95 |
|
| 96 |
DriverObject->MajorFunction[IRP_MJ_CREATE] = |
| 97 |
DriverObject->MajorFunction[IRP_MJ_CLOSE] = |
| 98 |
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = dhahelperdispatch; |
| 99 |
DriverObject->DriverUnload = dhahelperunload; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\\DosDevices\\DHAHELPER"); |
| 105 |
|
| 106 |
ntStatus = IoCreateSymbolicLink(&DeviceLinkUnicodeString,&DeviceNameUnicodeString); |
| 107 |
|
| 108 |
if (!NT_SUCCESS(ntStatus)){ |
| 109 |
|
| 110 |
|
| 111 |
OutputDebugString ("dhahelper: IoCreateSymbolicLink failed"); |
| 112 |
IoDeleteDevice (DeviceObject); |
| 113 |
} |
| 114 |
} |
| 115 |
else{ |
| 116 |
OutputDebugString ("dhahelper: IoCreateDevice failed"); |
| 117 |
} |
| 118 |
OutputDebugString ("dhahelper: leaving DriverEntry"); |
| 119 |
return ntStatus; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
static STDCALL NTSTATUS dhahelperdispatch(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp){ |
| 126 |
PIO_STACK_LOCATION IrpStack; |
| 127 |
ULONG dwInputBufferLength; |
| 128 |
ULONG dwOutputBufferLength; |
| 129 |
ULONG dwIoControlCode; |
| 130 |
PVOID pvIOBuffer; |
| 131 |
NTSTATUS ntStatus; |
| 132 |
dhahelper_t dhahelper_priv; |
| 133 |
|
| 134 |
OutputDebugString ("dhahelper: entering dhahelperdispatch"); |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
Irp->IoStatus.Status = STATUS_SUCCESS; |
| 139 |
Irp->IoStatus.Information = 0; |
| 140 |
|
| 141 |
IrpStack = IoGetCurrentIrpStackLocation(Irp); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
pvIOBuffer = Irp->AssociatedIrp.SystemBuffer; |
| 146 |
dwInputBufferLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength; |
| 147 |
dwOutputBufferLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength; |
| 148 |
|
| 149 |
switch (IrpStack->MajorFunction){ |
| 150 |
case IRP_MJ_CREATE: |
| 151 |
OutputDebugString("dhahelper: IRP_MJ_CREATE"); |
| 152 |
break; |
| 153 |
case IRP_MJ_CLOSE: |
| 154 |
OutputDebugString("dhahelper: IRP_MJ_CLOSE"); |
| 155 |
break; |
| 156 |
case IRP_MJ_DEVICE_CONTROL: |
| 157 |
OutputDebugString("dhahelper: IRP_MJ_DEVICE_CONTROL"); |
| 158 |
dwIoControlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode; |
| 159 |
switch (dwIoControlCode){ |
| 160 |
case IOCTL_DHAHELPER_ENABLEDIRECTIO: |
| 161 |
OutputDebugString("dhahelper: IOCTL_DHAHELPER_ENABLEDIRECTIO"); |
| 162 |
pIOPM = MmAllocateNonCachedMemory(sizeof(IOPM)); |
| 163 |
if (pIOPM){ |
| 164 |
RtlZeroMemory(pIOPM, sizeof(IOPM)); |
| 165 |
Ke386IoSetAccessProcess(PsGetCurrentProcess(), 1); |
| 166 |
Ke386SetIoAccessMap(1, pIOPM); |
| 167 |
} |
| 168 |
else Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES; |
| 169 |
break; |
| 170 |
case IOCTL_DHAHELPER_DISABLEDIRECTIO: |
| 171 |
OutputDebugString("dhahelper: IOCTL_DHAHELPER_DISABLEDIRECTIO"); |
| 172 |
if (pIOPM){ |
| 173 |
Ke386IoSetAccessProcess(PsGetCurrentProcess(), 0); |
| 174 |
Ke386SetIoAccessMap(1, pIOPM); |
| 175 |
MmFreeNonCachedMemory(pIOPM, sizeof(IOPM)); |
| 176 |
pIOPM = NULL; |
| 177 |
} |
| 178 |
break; |
| 179 |
case IOCTL_DHAHELPER_MAPPHYSTOLIN: |
| 180 |
OutputDebugString("dhahelper: IOCTL_DHAHELPER_MAPPHYSTOLIN"); |
| 181 |
if (dwInputBufferLength){ |
| 182 |
memcpy (&dhahelper_priv, pvIOBuffer, dwInputBufferLength); |
| 183 |
ntStatus = MapPhysicalMemoryToLinearSpace(dhahelper_priv.base,dhahelper_priv.size,&dhahelper_priv.ptr); |
| 184 |
if (NT_SUCCESS(ntStatus)){ |
| 185 |
memcpy (pvIOBuffer, &dhahelper_priv, dwInputBufferLength); |
| 186 |
Irp->IoStatus.Information = dwInputBufferLength; |
| 187 |
} |
| 188 |
Irp->IoStatus.Status = ntStatus; |
| 189 |
} |
| 190 |
else Irp->IoStatus.Status = STATUS_INVALID_PARAMETER; |
| 191 |
break; |
| 192 |
case IOCTL_DHAHELPER_UNMAPPHYSADDR: |
| 193 |
OutputDebugString("dhahelper: IOCTL_DHAHELPER_UNMAPPHYSADDR"); |
| 194 |
if (dwInputBufferLength){ |
| 195 |
memcpy (&dhahelper_priv, pvIOBuffer, dwInputBufferLength); |
| 196 |
ntStatus = UnmapPhysicalMemory(dhahelper_priv.ptr); |
| 197 |
Irp->IoStatus.Status = ntStatus; |
| 198 |
} |
| 199 |
else |
| 200 |
Irp->IoStatus.Status = STATUS_INVALID_PARAMETER; |
| 201 |
break; |
| 202 |
default: |
| 203 |
OutputDebugString("dhahelper: unknown IRP_MJ_DEVICE_CONTROL"); |
| 204 |
Irp->IoStatus.Status = STATUS_INVALID_PARAMETER; |
| 205 |
break; |
| 206 |
} |
| 207 |
break; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
ntStatus = Irp->IoStatus.Status; |
| 214 |
|
| 215 |
IoCompleteRequest (Irp, IO_NO_INCREMENT); |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
OutputDebugString("dhahelper: leaving dhahelperdispatch"); |
| 220 |
|
| 221 |
return ntStatus; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
static STDCALL void dhahelperunload(IN PDRIVER_OBJECT DriverObject){ |
| 227 |
UNICODE_STRING DeviceLinkUnicodeString; |
| 228 |
NTSTATUS ntStatus=STATUS_SUCCESS; |
| 229 |
OutputDebugString ("dhahelper: entering dhahelperunload"); |
| 230 |
OutputDebugString ("dhahelper: unmapping remaining memory"); |
| 231 |
|
| 232 |
while(alloccount && (ntStatus==STATUS_SUCCESS))ntStatus = UnmapPhysicalMemory(alloclist[alloccount-1].UserVirtualAddress); |
| 233 |
RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\\DosDevices\\DHAHELPER"); |
| 234 |
ntStatus = IoDeleteSymbolicLink (&DeviceLinkUnicodeString); |
| 235 |
|
| 236 |
if (NT_SUCCESS(ntStatus)){ |
| 237 |
IoDeleteDevice (DriverObject->DeviceObject); |
| 238 |
} |
| 239 |
else { |
| 240 |
OutputDebugString ("dhahelper: IoDeleteSymbolicLink failed"); |
| 241 |
} |
| 242 |
OutputDebugString ("dhahelper: leaving dhahelperunload"); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
static STDCALL NTSTATUS MapPhysicalMemoryToLinearSpace(PVOID pPhysAddress,ULONG PhysMemSizeInBytes,PVOID *PhysMemLin){ |
| 260 |
alloc_priv* alloclisttmp; |
| 261 |
PMDL Mdl=NULL; |
| 262 |
PVOID SystemVirtualAddress=NULL; |
| 263 |
PVOID UserVirtualAddress=NULL; |
| 264 |
PHYSICAL_ADDRESS pStartPhysAddress; |
| 265 |
OutputDebugString ("dhahelper: entering MapPhysicalMemoryToLinearSpace"); |
| 266 |
|
| 267 |
#ifdef _WIN64 |
| 268 |
pStartPhysAddress.QuadPart = (ULONGLONG)pPhysAddress; |
| 269 |
#else |
| 270 |
pStartPhysAddress.QuadPart = (ULONGLONG)(ULONG)pPhysAddress; |
| 271 |
#endif |
| 272 |
#ifndef NO_SEH |
| 273 |
__try { |
| 274 |
#endif |
| 275 |
SystemVirtualAddress=MmMapIoSpace(pStartPhysAddress,PhysMemSizeInBytes, MmNonCached); |
| 276 |
if(!SystemVirtualAddress){ |
| 277 |
OutputDebugString("dhahelper: MmMapIoSpace failed"); |
| 278 |
return STATUS_INVALID_PARAMETER; |
| 279 |
} |
| 280 |
OutputDebugString("dhahelper: SystemVirtualAddress 0x%x",SystemVirtualAddress); |
| 281 |
Mdl=IoAllocateMdl(SystemVirtualAddress, PhysMemSizeInBytes, FALSE, FALSE,NULL); |
| 282 |
if(!Mdl){ |
| 283 |
OutputDebugString("dhahelper: IoAllocateMdl failed"); |
| 284 |
return STATUS_INSUFFICIENT_RESOURCES; |
| 285 |
} |
| 286 |
OutputDebugString("dhahelper: Mdl 0x%x",Mdl); |
| 287 |
MmBuildMdlForNonPagedPool(Mdl); |
| 288 |
#ifdef _WIN64 |
| 289 |
UserVirtualAddress = (PVOID)(((ULONGLONG)PAGE_ALIGN(MmMapLockedPages(Mdl,UserMode))) + MmGetMdlByteOffset(Mdl)); |
| 290 |
#else |
| 291 |
UserVirtualAddress = (PVOID)(((ULONG)PAGE_ALIGN(MmMapLockedPages(Mdl,UserMode))) + MmGetMdlByteOffset(Mdl)); |
| 292 |
#endif |
| 293 |
if(!UserVirtualAddress){ |
| 294 |
OutputDebugString("dhahelper: MmMapLockedPages failed"); |
| 295 |
return STATUS_INSUFFICIENT_RESOURCES; |
| 296 |
} |
| 297 |
OutputDebugString("dhahelper: UserVirtualAddress 0x%x",UserVirtualAddress); |
| 298 |
#ifndef NO_SEH |
| 299 |
}__except(EXCEPTION_EXECUTE_HANDLER){ |
| 300 |
NTSTATUS ntStatus; |
| 301 |
ntStatus = GetExceptionCode(); |
| 302 |
OutputDebugString("dhahelper: MapPhysicalMemoryToLinearSpace failed due to exception 0x%0x\n", ntStatus); |
| 303 |
return ntStatus; |
| 304 |
} |
| 305 |
#endif |
| 306 |
|
| 307 |
|
| 308 |
OutputDebugString("dhahelper: adding data to internal allocation list"); |
| 309 |
alloclisttmp=MmAllocateNonCachedMemory((alloccount+1)*sizeof(alloc_priv)); |
| 310 |
|
| 311 |
|
| 312 |
if(!alloclisttmp){ |
| 313 |
OutputDebugString("dhahelper: not enough memory to create temporary allocation list"); |
| 314 |
MmUnmapLockedPages(UserVirtualAddress, Mdl); |
| 315 |
IoFreeMdl(Mdl); |
| 316 |
return STATUS_INSUFFICIENT_RESOURCES; |
| 317 |
} |
| 318 |
if(alloccount){ |
| 319 |
memcpy(alloclisttmp,alloclist,alloccount * sizeof(alloc_priv)); |
| 320 |
MmFreeNonCachedMemory(alloclist,alloccount*sizeof(alloc_priv)); |
| 321 |
} |
| 322 |
alloclist=alloclisttmp; |
| 323 |
alloclist[alloccount].Mdl=Mdl; |
| 324 |
alloclist[alloccount].SystemVirtualAddress=SystemVirtualAddress; |
| 325 |
alloclist[alloccount].UserVirtualAddress=UserVirtualAddress; |
| 326 |
alloclist[alloccount].PhysMemSizeInBytes=PhysMemSizeInBytes; |
| 327 |
++alloccount; |
| 328 |
|
| 329 |
*PhysMemLin=UserVirtualAddress; |
| 330 |
|
| 331 |
OutputDebugString("dhahelper: leaving MapPhysicalMemoryToLinearSpace"); |
| 332 |
return STATUS_SUCCESS; |
| 333 |
} |
| 334 |
|
| 335 |
static STDCALL NTSTATUS UnmapPhysicalMemory(PVOID UserVirtualAddress){ |
| 336 |
unsigned int i; |
| 337 |
unsigned int x=0; |
| 338 |
unsigned int alloccounttmp=alloccount; |
| 339 |
OutputDebugString("dhahelper: entering UnmapPhysicalMemory to unmapp 0x%x",UserVirtualAddress); |
| 340 |
if(!alloccount){ |
| 341 |
OutputDebugString("dhahelper: UnmapPhysicalMemory: nothing todo -> leaving..."); |
| 342 |
return STATUS_SUCCESS; |
| 343 |
} |
| 344 |
|
| 345 |
for(i=0;i<alloccount;i++){ |
| 346 |
if(alloclist[i].UserVirtualAddress!=UserVirtualAddress){ |
| 347 |
if(x!=i){ |
| 348 |
alloclist[x].Mdl=alloclist[i].Mdl; |
| 349 |
alloclist[x].SystemVirtualAddress=alloclist[i].SystemVirtualAddress; |
| 350 |
alloclist[x].UserVirtualAddress=alloclist[i].UserVirtualAddress; |
| 351 |
alloclist[x].PhysMemSizeInBytes=alloclist[i].PhysMemSizeInBytes; |
| 352 |
|
| 353 |
} |
| 354 |
x++; |
| 355 |
} |
| 356 |
else if(alloclist[i].UserVirtualAddress==UserVirtualAddress){ |
| 357 |
if(x==i){ |
| 358 |
#ifndef NO_SEH |
| 359 |
__try { |
| 360 |
#endif |
| 361 |
MmUnmapLockedPages(alloclist[x].UserVirtualAddress, alloclist[x].Mdl); |
| 362 |
IoFreeMdl(alloclist[x].Mdl); |
| 363 |
MmUnmapIoSpace(alloclist[x].SystemVirtualAddress,alloclist[x].PhysMemSizeInBytes); |
| 364 |
#ifndef NO_SEH |
| 365 |
}__except(EXCEPTION_EXECUTE_HANDLER){ |
| 366 |
NTSTATUS ntStatus; |
| 367 |
ntStatus = GetExceptionCode(); |
| 368 |
OutputDebugString("dhahelper: UnmapPhysicalMemory failed due to exception 0x%0x (Mdl 0x%x)\n", ntStatus,alloclist[x].Mdl); |
| 369 |
return ntStatus; |
| 370 |
} |
| 371 |
#endif |
| 372 |
} |
| 373 |
alloccounttmp--; |
| 374 |
} |
| 375 |
|
| 376 |
} |
| 377 |
|
| 378 |
if(alloccounttmp){ |
| 379 |
alloc_priv* alloclisttmp; |
| 380 |
alloclisttmp=MmAllocateNonCachedMemory(alloccounttmp*sizeof(alloc_priv)); |
| 381 |
if(!alloclisttmp){ |
| 382 |
OutputDebugString("dhahelper: not enough memory to create temporary allocation list"); |
| 383 |
return STATUS_INSUFFICIENT_RESOURCES; |
| 384 |
} |
| 385 |
memcpy(alloclisttmp,alloclist,alloccounttmp * sizeof(alloc_priv)); |
| 386 |
MmFreeNonCachedMemory(alloclist,alloccount*sizeof(alloc_priv)); |
| 387 |
alloclist=alloclisttmp; |
| 388 |
} |
| 389 |
alloccount=alloccounttmp; |
| 390 |
|
| 391 |
OutputDebugString("dhahelper: leaving UnmapPhysicalMemory"); |
| 392 |
return STATUS_SUCCESS; |
| 393 |
} |