KMDF中未分页内存的类型选择

本文介绍了Windows内核驱动在分配未分页内存时,应当使用NonPagedPoolNx而非NonPagedPool以提高安全性。从Windows 8开始,NonPagedPool等同于NonPagedPoolExecute,允许代码执行,存在安全隐患。建议驱动开发者使用NonPagedPoolNx,防止恶意代码执行。
摘要由CSDN通过智能技术生成

内核驱动程序中,内存空间的动态分配不能使用C语言的malloc等函数,取而代之的是ExAllocatePoolWithTag()和ExFreePool(),使用方法举例如下:

#defineTEST_POOL_TAG            (ULONG)'test'

PUCHAR tempBuf = NULL;

tempBuf = ExAllocatePoolWithTag ( NonPagedPool,tempBufSize, TEST_POOL_TAG );

ExFreePool ( (PVOID)tempBuf ); //free memory

但此种写法在进行HLK测试时,会出现蓝屏的现象,具体的错误信息如下:

DRIVER_VERIFIER_DETECTED_VIOLATION(c4)

A device driverattempting to corrupt the system has been caught.  This is

because the driverwas specified in the registry as being suspect (by the

administrator) andthe kernel has enabled substantial checking of this driver.

If the driverattempts to corrupt the system, bugchecks 0xC4, 0xC1 and 0xA will</

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
KMDF 驱动中,可以通过内存映射的方式来读写硬盘的原始数据。具体来说,可以使用 `MmMapLockedPagesSpecifyCache` 函数将物理内存页面映射到驱动程序的虚拟地址空间中,然后使用指针访问这些内存页面,实现对硬盘数据的读写。 以下是一个示例代码: ```C NTSTATUS ReadWriteRawDiskData(PDEVICE_OBJECT DeviceObject, ULONGLONG Offset, PVOID Buffer, ULONG Length, BOOLEAN IsWrite) { NTSTATUS Status; PPHYSICAL_MEMORY_RANGE PhysicalRanges = NULL; ULONG PhysicalRangeCount = 0; PHYSICAL_ADDRESS PhysicalAddress; PVOID MappedAddress = NULL; ULONG_PTR MdlHack; ULONG_PTR PageOffset; ULONG_PTR PageCount; ULONG_PTR i; // Calculate the physical memory ranges that correspond to the buffer Status = IoGetDmaAdapter(DeviceObject, NULL, &PhysicalRanges, &PhysicalRangeCount); if (!NT_SUCCESS(Status)) { return Status; } PhysicalAddress.QuadPart = Offset; MappedAddress = MmMapLockedPagesSpecifyCache(PhysicalRanges, PhysicalRangeCount, MmCached, NULL, FALSE, HighPagePriority | MdlMappingNoExecute); if (MappedAddress == NULL) { IoFreeMapRegisters(DeviceObject->DeviceExtension, PhysicalRanges, PhysicalRangeCount); return STATUS_INSUFFICIENT_RESOURCES; } // Calculate the page offset and the number of pages MdlHack = (ULONG_PTR)(MappedAddress); PageOffset = MdlHack & (PAGE_SIZE - 1); PageCount = ADDRESS_AND_SIZE_TO_SPAN_PAGES(MdlHack - PageOffset, Length + PageOffset); // Adjust the physical address to point to the correct page PhysicalAddress.QuadPart -= PageOffset; if (IsWrite) { // Write the data to the mapped memory RtlCopyMemory((PVOID)((ULONG_PTR)MappedAddress + PageOffset), Buffer, Length); } else { // Read the data from the mapped memory RtlCopyMemory(Buffer, (PVOID)((ULONG_PTR)MappedAddress + PageOffset), Length); } // Flush the data to the disk KeFlushIoBuffers(MappedAddress, Length); // Unmap the memory MmUnmapLockedPages(MappedAddress, NULL); IoFreeMapRegisters(DeviceObject->DeviceExtension, PhysicalRanges, PhysicalRangeCount); return STATUS_SUCCESS; } ``` 在调用 `ReadWriteRawDiskData` 函数时,需要指定要读写的设备对象、读写的偏移量、读写的数据缓冲区以及要读写的数据长度等参数。例如: ```C ReadWriteRawDiskData(DeviceObject, 0x1000, pData, 0x100, FALSE); ``` 这将从设备对象 `DeviceObject` 的偏移量 `0x1000` 处开始读取长度为 `0x100` 字节的数据到缓冲区 `pData` 中。 需要注意的是,通过内存映射读写硬盘数据需要谨慎操作,因为这可能会对硬盘的数据造成破坏。在进行读写操作之前,需要确保已经对硬盘进行了备份,并且已经获得了足够的权限来进行操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值