• windows 驱动与内核调试 学习3


    前言

    windows 驱动与内核调试 学习2 这篇介绍基础的驱动读写,我们看如下一例子

    	char szBuffer[1];
    	char* p= szBuffer-500;
    	//读取驱动在一个不确定的位置
    	if (ReadFile(hFile, p, 200, &drBytes, NULL))
    	{
    		std::cout << "read ok  " << drBytes << " "<< szBuffer << std::endl;
    	}
    	else {
    		std::cout << "read failure " << std::endl;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    因为驱动运行在ring0 所以,可以随意写入存在一定安全隐患。另外还有线程安全问题如多线程对于同一IO地址进行读写。为了解决相关问题WindowsDEVICE_OBJECT对象提供一个flags字段可以增加io方式应对相关问题

    官方文档

    其中有两种方式分别为DO_DIRECT_IODO_BUFFERED_IO,两则使用场景不同。DO_BUFFERED_IO适合在小数据传输中使用便捷,DO_DIRECT_IO使用在大数据传输操作繁琐但效率高。
    可参阅如下文档

    windows驱动编程中的DO_DIRECT_IO和DO_BUFFERED_IO标志位

    Addressing a Data Buffer

    Methods for Accessing Data Buffers

    在这里插入图片描述

    上图你可以很明显看出两则差别

    DO_BUFFERED_IO

    这种模式会申请和用户空间等同大小的内存块在内核空间,如果是写入请求那么操作性还会申请内存块的同时会将原内存信息拷贝到新申请内存块中。
    操作系统做的事情类似如下代码所示(例子来自参考链接并作稍作修改):

    PVOID uva;             //  用户模式传入缓冲区地址.可以理解调用驱动时传入的
    ULONG length;          // 用户模式传入缓冲区地址长度
    //新建一块内存区域
    PVOID sva; = ExAllocatePoolWithQuota(NonPagedPoolCacheAligned, length);
    //如果写入模式,会进行内存区块拷贝
    if (writing)
      RtlCopyMemory(sva, uva, length);
    //拷贝地址到特定字段
    Irp->AssociatedIrp.SystemBuffer = sva; 
    //将长度拷贝IRP栈中
    PIO_STACK_LOCATION stack = IoGetNextIrpStackLocation(Irp);
    if (reading)
      stack->Parameters.Read.Length = length; 
    else
      stack->Parameters.Write.Length = length; 
    
    <code to send and await IRP>
    
    //如果时读请求,拷贝内存到用户模式传入的地址
    if (reading)
      RtlCopyMemory(uva, sva, length);
    //内核地址释放
    ExFreePool(sva);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    案例:

    
    //驱动被加载的时候会调用此函数
    NTSTATUS
    DriverEntry(
    	_In_ struct _DRIVER_OBJECT* DriverObject,
    	_In_ PUNICODE_STRING    RegistryPath
    )
    {
    	//如果你没有用到参数需要告诉系统。
    	UNREFERENCED_PARAMETER(RegistryPath);
    	//打印信息
    	DbgPrint("hello  drive loaded");
    	//触发一个断点
    	//DbgBreakPoint();
    	//驱动卸载回调注册
    	DriverObject->DriverUnload = myUnload;
    
    
    	DriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchCreate;
    	DriverObject->MajorFunction[IRP_MJ_CLOSE] = DispatchClose;
    	DriverObject->MajorFunction[IRP_MJ_READ] = DispatchRead;
    	DriverObject->MajorFunction[IRP_MJ_WRITE] = DispatchWrite;
    	DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchControl;
    
    
    	UNICODE_STRING ustrDevName;
    	RtlInitUnicodeString(&ustrDevName, L"\\Device\\MytestDriver");
    	PDEVICE_OBJECT  pDevObj = NULL;
    	
    	auto ret = IoCreateDevice(DriverObject, 0, &ustrDevName, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &pDevObj);
    
    
    	if (NT_SUCCESS(ret))
    	{
    		//指定IO模式
    		pDevObj->Flags |= DO_BUFFERED_IO;
    		DbgPrint("IoCreateDevice 成功 \r\n");
    	}
    	else {
    		DbgPrint("IoCreateDevice 失败 %d\r\n", ret);
    		return STATUS_FAIL_CHECK;
    	}
    
    
    	UNICODE_STRING symbolDevName;
    	RtlInitUnicodeString(&symbolDevName, L"\\DosDevices\\MytestDriver");
    	ret = IoCreateSymbolicLink(&symbolDevName, &ustrDevName);
    	if (NT_SUCCESS(ret))
    	{
    		DbgPrint("IoCreateSymbolicLink 成功 \r\n");
    	}
    	else {
    		DbgPrint("IoCreateSymbolicLink 失败%d\r\n", ret);
    
    		IoDeleteDevice(pDevObj);
    
    		return STATUS_FAIL_CHECK;
    	}
    
    	return STATUS_SUCCESS;
    }
    
    • 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
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    我们查看对应的写入函数

    NTSTATUS
    DispatchWrite(
    	_In_ struct _DEVICE_OBJECT* DeviceObject,
    	_Inout_ struct _IRP* Irp
    ) {
    	DbgPrint("DispatchWrite");
    	UNREFERENCED_PARAMETER(DeviceObject);
    
    
    	PIO_STACK_LOCATION	pIrp = IoGetCurrentIrpStackLocation(Irp);
    	ULONG nLength = pIrp->Parameters.Write.Length;
    
    	DbgPrint("DispatchWrite UserBuffer:%p bytes:%d content %s", Irp->UserBuffer, nLength, Irp->UserBuffer);
    	
    	int cLen = pIrp->Parameters.Write.Length;
    
    
    
    	DbgPrint("DispatchWrite SystemBuffer:%p bytes:%d content %s", Irp->AssociatedIrp.SystemBuffer, cLen, Irp->AssociatedIrp.SystemBuffer);
    
    	Irp->IoStatus.Status = STATUS_SUCCESS;
    	Irp->IoStatus.Information = 6;
    	IoCompleteRequest(Irp, IO_NO_INCREMENT);
    
    	return STATUS_SUCCESS;
    }
    
    • 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

    读取函数

    NTSTATUS
    DispatchRead(
    	_In_ struct _DEVICE_OBJECT* DeviceObject,
    	_Inout_ struct _IRP* Irp
    ) {
    	DbgPrint("DispatchRead");
    
    	UNREFERENCED_PARAMETER(DeviceObject);
    
    
    	PIO_STACK_LOCATION	pIrp = IoGetCurrentIrpStackLocation(Irp);
    	ULONG nLength = pIrp->Parameters.Read.Length;
    	//打印原始用户传入的缓存区
    	DbgPrint("DispatchRead UserBuffer:%p bytes:%d", Irp->UserBuffer, nLength);
    
    
    	int cLen = pIrp->Parameters.Read.Length;
    
    	DbgPrint("DispatchRead SystemBuffer:%p bytes:%d", Irp->AssociatedIrp.SystemBuffer, cLen);
    
    	
    
    
    
    	memcpy(Irp->AssociatedIrp.SystemBuffer,"helloread",10);
    
    	Irp->IoStatus.Status = STATUS_SUCCESS;
    	Irp->IoStatus.Information = 10;
    	IoCompleteRequest(Irp, IO_NO_INCREMENT);
    
    
    	return STATUS_SUCCESS;
    }
    
    • 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
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    我们打印驱动触发读写后的样子
    在这里插入图片描述

    DO_DIRECT_IO

    这个IO模式是使用用来传输大数据时使用。微软提供两种模式让我们使用:

    1. using-direct-i-o-with-pio

    2. using-direct-i-o-with-dma

    这里用DMA方式做讲解。
    我们首先需要明白DMA的概念,我们假设拷贝一个电影从u盘到内存,在之前远古时代需要CPU参与,在现代有DMA的硬件加持,cpu只需要传递需求给DMA即可。但是使用DMA需要固化虚拟地址和物理地址映射关系(比如虚拟地址0x111可以映射硬件内存地址是可以随时变化的)。
    在我们使用个IO模式的时候,操作会构建一个叫MDL的宏,去表述相关结构

    typedef struct _MDL {
      struct _MDL *Next;
      CSHORT Size;
      CSHORT MdlFlags;
      struct _EPROCESS *Process;
      PVOID MappedSystemVa;
      PVOID StartVa;
      ULONG ByteCount;
      ULONG ByteOffset;
      } MDL, *PMDL;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    这个结构存储的虚拟地址信息

    在这里插入图片描述
    并且在个数据结构内存后面紧贴一个物理地址映射数组(但是没有API让驱动开发者访问)。

    我们不会驱动中直接碰触这个数据结构,而是用微软提供相关的宏。如下图

    在这里插入图片描述
    操作系统其实做了如下类似代码:

    KPROCESSOR_MODE mode;   //  either KernelMode or UserMode
    //创建MDL对象
    PMDL mdl = IoAllocateMdl(uva, length, FALSE, TRUE, Irp);
    //锁定虚拟地址和物理的映射 方便执行DMA
    MmProbeAndLockPages(mdl, mode,
      reading ? IoWriteAccess : IoReadAccess); 
    
    <code to send and await IRP>
    //解决映射
    MmUnlockPages(mdl);
    //释放MDL
    ExFreePool(mdl);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    我们看看官方使用指南
    在这里插入图片描述

    案例:

    NTSTATUS
    DispatchRead(
    	_In_ struct _DEVICE_OBJECT* DeviceObject,
    	_Inout_ struct _IRP* Irp
    ) {
    	DbgPrint("DispatchRead");
    
    	UNREFERENCED_PARAMETER(DeviceObject);
    
    
    	PIO_STACK_LOCATION	pIrp = IoGetCurrentIrpStackLocation(Irp);
    	ULONG nLength = pIrp->Parameters.Read.Length;
    	//打印原始用户传入的缓存区
    	DbgPrint("DispatchRead UserBuffer:%p bytes:%d", Irp->UserBuffer, nLength);
    
    
    	
    	PVOID kenelP = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);
    
    	DbgPrint("DispatchRead MmGetSystemAddressForMdlSafe:%p ", kenelP);
    
    
    
    
    
    	memcpy(kenelP, "helloread", 10);
    
    	Irp->IoStatus.Status = STATUS_SUCCESS;
    	Irp->IoStatus.Information = 10;
    	IoCompleteRequest(Irp, IO_NO_INCREMENT);
    
    
    	return STATUS_SUCCESS;
    }
    NTSTATUS
    DispatchWrite(
    	_In_ struct _DEVICE_OBJECT* DeviceObject,
    	_Inout_ struct _IRP* Irp
    ) {
    	DbgPrint("DispatchWrite");
    	UNREFERENCED_PARAMETER(DeviceObject);
    
    
    	PIO_STACK_LOCATION	pIrp = IoGetCurrentIrpStackLocation(Irp);
    	ULONG nLength = pIrp->Parameters.Write.Length;
    
    	DbgPrint("DispatchWrite UserBuffer:%p bytes:%d content %s", Irp->UserBuffer, nLength, Irp->UserBuffer);
    
    
    	PVOID kenelP = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);
    
    
    	DbgPrint("DispatchWrite SystemBuffer:%p  content %s", kenelP, kenelP);
    
    	Irp->IoStatus.Status = STATUS_SUCCESS;
    	Irp->IoStatus.Information = 6;
    	IoCompleteRequest(Irp, IO_NO_INCREMENT);
    
    	return STATUS_SUCCESS;
    }
    
    
    //驱动被加载的时候会调用此函数
    NTSTATUS
    DriverEntry(
    	_In_ struct _DRIVER_OBJECT* DriverObject,
    	_In_ PUNICODE_STRING    RegistryPath
    )
    {
    	//如果你没有用到参数需要告诉系统。
    	UNREFERENCED_PARAMETER(RegistryPath);
    	//打印信息
    	DbgPrint("hello  drive loaded");
    	//触发一个断点
    	//DbgBreakPoint();
    	//驱动卸载回调注册
    	DriverObject->DriverUnload = myUnload;
    
    
    	DriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchCreate;
    	DriverObject->MajorFunction[IRP_MJ_CLOSE] = DispatchClose;
    	DriverObject->MajorFunction[IRP_MJ_READ] = DispatchRead;
    	DriverObject->MajorFunction[IRP_MJ_WRITE] = DispatchWrite;
    	DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchControl;
    
    
    	UNICODE_STRING ustrDevName;
    	RtlInitUnicodeString(&ustrDevName, L"\\Device\\MytestDriver");
    	PDEVICE_OBJECT  pDevObj = NULL;
    
    	auto ret = IoCreateDevice(DriverObject, 0, &ustrDevName, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &pDevObj);
    
    
    
    
    	if (NT_SUCCESS(ret))
    	{
    		//指定IO模式
    		pDevObj->Flags |= DO_DIRECT_IO;
    		DbgPrint("IoCreateDevice 成功 \r\n");
    	}
    	else {
    		DbgPrint("IoCreateDevice 失败 %d\r\n", ret);
    		return STATUS_FAIL_CHECK;
    	}
    
    
    	UNICODE_STRING symbolDevName;
    	RtlInitUnicodeString(&symbolDevName, L"\\DosDevices\\MytestDriver");
    	ret = IoCreateSymbolicLink(&symbolDevName, &ustrDevName);
    	if (NT_SUCCESS(ret))
    	{
    		DbgPrint("IoCreateSymbolicLink 成功 \r\n");
    	}
    	else {
    		DbgPrint("IoCreateSymbolicLink 失败%d\r\n", ret);
    
    		IoDeleteDevice(pDevObj);
    
    		return STATUS_FAIL_CHECK;
    	}
    
    	return STATUS_SUCCESS;
    }
    
    
    • 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
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125

    在这里插入图片描述

  • 相关阅读:
    Java基础---第十四篇
    Springboot jpa 查询排序Sort,分页Page使用报错
    CK04# ClickHouse日志存储调优总结
    MySQL的默认引擎为什么是InnoDB
    高速串行总线—Rapid IO
    92.(leaflet之家)leaflet态势标绘-进攻方向采集
    R语言使用马尔可夫链对营销中的渠道归因建模
    Pytorch学习:torch.randn()
    计算机网络概念基础——分组交换
    [附源码]java毕业设计物理中考复习在线考试系统
  • 原文地址:https://blog.csdn.net/qfanmingyiq/article/details/127704300