windows/virt2phys: fix paging issue
cover windows/virt2phys: fix paging issue
Physical addresses exposed by virt2phys driver could become pageable. This presents stability and security issues that prevent Microsoft from signing virt2phys, because a signed driver would be trusted by all end-user machines.
Ensure that memory for which physical addresses are exposed by virt2phys is non-pageable at least for the lifetime of the process. As virt2phys code grows, make its development and debugging easier.
There are other known issues that come from using PA and accessing DMA from userspace. They are not related to virt2phys par se. It is planned to address them later by enabling the use of IOMMU for DPDK on Windows.
Dmitry Kozlyuk (3):
windows/virt2phys: use local time for signing windows/virt2phys: do not expose pageable physical addresses windows/virt2phys: add tracing
windows/virt2phys/virt2phys.c | 89 ++++-- windows/virt2phys/virt2phys.vcxproj | 8 +- windows/virt2phys/virt2phys.vcxproj.filters | 11 +- windows/virt2phys/virt2phys_logic.c | 312 ++++++++++++++++++++ windows/virt2phys/virt2phys_logic.h | 32 ++ windows/virt2phys/virt2phys_trace.h | 49 +++ 6 files changed, 472 insertions(+), 29 deletions(-) create mode 100644 windows/virt2phys/virt2phys_logic.c create mode 100644 windows/virt2phys/virt2phys_logic.h create mode 100644 windows/virt2phys/virt2phys_trace.h
Signature · 1 line
2.29.3
PATCH 1/3 windows/virt2phys: use local time for signing
Inf2Cat utility for signing drivers considers time in UTC by default and aborts with "date in the future" error when developing in positive-offset timezones. Set "Use Local Time" flag to fix it.
--- windows/virt2phys/virt2phys.vcxproj | 1 + 1 file changed, 1 insertion(+)
windows/virt2phys/virt2phys.vcxproj +1 -0
@@ -133,6 +133,7 @@ </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor> + <Inf2CatUseLocalTime>true</Inf2CatUseLocalTime> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor>
Signature · 1 line
2.29.3
PATCH 2/3 windows/virt2phys: do not expose pageable physical addresses
In reply to Dmitry Kozlyukvirt2phys relied on the user to ensure that memory for which physical address (PA) is obtained is non-pageable and remains such for the lifetime of the process. While DPDK does lock pages in memory, virt2phys can be accessed by any process with sufficient privileges. A malicious process could get PA and make memory pageable. When it is reused for another process, attacker can get access to private data or crash the system if another process is kernel.
Solution is to lock all memory for which PA is requested. Locked blocks are tracked to unlock them when the process exits. If driver is unloaded while some memory is still locked, it leaves pages locked, effectively leaking RAM, trading stability for security.
Factor out a module that locks memory for which physical addresses are obtained and keeps a list of locked memory blocks for each process. It exposes a thread-safe interface for use in driver callbacks. The driver reacts to a process exit by unlocking its tracked blocks.
Also clean up the driver code. Remove debugging output to replace it with structured tracing in the next patch.
--- windows/virt2phys/virt2phys.c | 75 +++-- windows/virt2phys/virt2phys.vcxproj | 2 + windows/virt2phys/virt2phys.vcxproj.filters | 8 +- windows/virt2phys/virt2phys_logic.c | 304 ++++++++++++++++++++ windows/virt2phys/virt2phys_logic.h | 32 +++ 5 files changed, 394 insertions(+), 27 deletions(-) create mode 100644 windows/virt2phys/virt2phys_logic.c create mode 100644 windows/virt2phys/virt2phys_logic.h
windows/virt2phys/virt2phys.c +49 -26
@@ -1,21 +1,25 @@/* SPDX-License-Identifier: BSD-3-Clause -* Copyright(c) 2020 Dmitry Kozlyuk +* Copyright 2020-2021 Dmitry Kozlyuk */ #include <ntddk.h> #include <wdf.h> -#include <wdmsec.h> #include <initguid.h> #include "virt2phys.h" +#include "virt2phys_logic.h" DRIVER_INITIALIZE DriverEntry; +EVT_WDF_DRIVER_UNLOAD virt2phys_driver_unload;EVT_WDF_DRIVER_DEVICE_ADD virt2phys_driver_EvtDeviceAdd;EVT_WDF_IO_IN_CALLER_CONTEXT virt2phys_device_EvtIoInCallerContext; +static VOID virt2phys_on_process_event ( +HANDLE parent_id,HANDLE process_id,BOOLEAN create); + +_Use_decl_annotations_ NTSTATUS -DriverEntry ( - IN PDRIVER_OBJECT driver_object, IN PUNICODE_STRING registry_path) +DriverEntry (PDRIVER_OBJECT driver_object,PUNICODE_STRING registry_path) {WDF_DRIVER_CONFIG config;WDF_OBJECT_ATTRIBUTES attributes;
@@ -23,22 +27,42 @@ DriverEntry(PAGED_CODE (); -WDF_DRIVER_CONFIG_INIT (&config, virt2phys_driver_EvtDeviceAdd);WDF_OBJECT_ATTRIBUTES_INIT (&attributes); +WDF_DRIVER_CONFIG_INIT (&config, virt2phys_driver_EvtDeviceAdd); + config.EvtDriverUnload = virt2phys_driver_unload; status =WdfDriverCreate ( driver_object, registry_path, &attributes, &config,WDF_NO_HANDLE );if (!NT_SUCCESS (status)) { -KdPrint (("WdfDriverCreate() failed, status=%08x\n" , status)); +return status; } + status =virt2phys_init (); +if (!NT_SUCCESS (status)) +return status; + + status =PsSetCreateProcessNotifyRoutine ( + virt2phys_on_process_event, FALSE); +if (!NT_SUCCESS (status)) +return status; +return status; } +_Use_decl_annotations_ +VOID +virt2phys_driver_unload (WDFDRIVER driver) +{ +UNREFERENCED_PARAMETER (driver); + +PsSetCreateProcessNotifyRoutine (virt2phys_on_process_event, TRUE); + +virt2phys_cleanup (); +} +_Use_decl_annotations_ NTSTATUS -virt2phys_driver_EvtDeviceAdd ( -WDFDRIVER driver,PWDFDEVICE_INIT init) +virt2phys_driver_EvtDeviceAdd (WDFDRIVER driver,PWDFDEVICE_INIT init) {WDF_OBJECT_ATTRIBUTES attributes;WDFDEVICE device;
@@ -46,8 +70,6 @@ virt2phys_driver_EvtDeviceAdd(UNREFERENCED_PARAMETER (driver); -PAGED_CODE (); -WdfDeviceInitSetIoType ( init, WdfDeviceIoNeither); WdfDeviceInitSetIoInCallerContextCallback(
@@ -57,15 +79,12 @@ virt2phys_driver_EvtDeviceAdd( status =WdfDeviceCreate (&init, &attributes, &device);if (!NT_SUCCESS (status)) { -KdPrint (("WdfDeviceCreate() failed, status=%08x\n" , status));return status; } status =WdfDeviceCreateDeviceInterface ( - device, &GUID_DEVINTERFACE_VIRT2PHYS ,NULL ); + device, &GUID_DEVINTERFACE_VIRT2PHYS ,NULL );if (!NT_SUCCESS (status)) { -KdPrint (("WdfDeviceCreateDeviceInterface() failed, " -"status=%08x\n" , status));return status; }
@@ -74,8 +93,7 @@ virt2phys_driver_EvtDeviceAdd(_Use_decl_annotations_ VOID -virt2phys_device_EvtIoInCallerContext ( - IN WDFDEVICE device, IN WDFREQUEST request) +virt2phys_device_EvtIoInCallerContext (WDFDEVICE device,WDFREQUEST request) {WDF_REQUEST_PARAMETERS params;ULONG code;
@@ -85,21 +103,18 @@ virt2phys_device_EvtIoInCallerContext(NTSTATUS status;UNREFERENCED_PARAMETER (device); -PAGED_CODE ();WDF_REQUEST_PARAMETERS_INIT (¶ms);WdfRequestGetParameters (request, ¶ms);if (params.Type != WdfRequestTypeDeviceControl) { -KdPrint (("bogus request type=%u\n" , params.Type));WdfRequestComplete (request,STATUS_NOT_SUPPORTED );return ; } code = params.Parameters.DeviceIoControl.IoControlCode;if (code !=IOCTL_VIRT2PHYS_TRANSLATE ) { -KdPrint (("bogus IO control code=%lu\n" , code));WdfRequestComplete (request,STATUS_NOT_SUPPORTED );return ; }
@@ -107,8 +122,6 @@ virt2phys_device_EvtIoInCallerContext( status =WdfRequestRetrieveInputBuffer ( request,sizeof (*virt), (PVOID *)&virt, &size);if (!NT_SUCCESS (status)) { -KdPrint (("WdfRequestRetrieveInputBuffer() failed, " -"status=%08x\n" , status));WdfRequestComplete (request, status);return ; }
@@ -116,14 +129,24 @@ virt2phys_device_EvtIoInCallerContext( status =WdfRequestRetrieveOutputBuffer ( request,sizeof (*phys), (PVOID *)&phys, &size);if (!NT_SUCCESS (status)) { -KdPrint (("WdfRequestRetrieveOutputBuffer() failed, " -"status=%08x\n" , status));WdfRequestComplete (request, status);return ; } - *phys =MmGetPhysicalAddress (*virt); + status =virt2phys_translate (*virt, phys); +if (NT_SUCCESS (status)) +WdfRequestSetInformation (request,sizeof (*phys)); +WdfRequestComplete (request, status); +} + +static VOID +virt2phys_on_process_event ( +HANDLE parent_id,HANDLE process_id,BOOLEAN create) +{ +UNREFERENCED_PARAMETER (parent_id); + +if (create) +return ; -WdfRequestCompleteWithInformation ( - request,STATUS_SUCCESS ,sizeof (*phys)); +virt2phys_process_cleanup (process_id); }
windows/virt2phys/virt2phys.vcxproj +2 -0
@@ -36,9 +36,11 @@ </ItemGroup> <ItemGroup> <ClCompile Include="virt2phys.c" /> + <ClCompile Include="virt2phys_logic.c" /> </ItemGroup> <ItemGroup> <ClInclude Include="virt2phys.h" /> + <ClInclude Include="virt2phys_logic.h" /> </ItemGroup> <ItemGroup> <Inf Include="virt2phys.inf" />
windows/virt2phys/virt2phys.vcxproj.filters +7 -1
@@ -27,10 +27,16 @@ <ClInclude Include="virt2phys.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="virt2phys_logic.h"> + <Filter>Header Files</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="virt2phys.c"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="virt2phys_logic.c"> + <Filter>Source Files</Filter> + </ClCompile> </ItemGroup> -</Project> +</Project>
windows/virt2phys/virt2phys_logic.c new file +304 -0
304 lines not on this page · raw message
windows/virt2phys/virt2phys_logic.h new file +32 -0
@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright 2021 Dmitry Kozlyuk +*/ + +#ifndef VIRT2PHYS_LOGIC_H +#define VIRT2PHYS_LOGIC_H + +/** +* Initialize internal data structures. +*/ +NTSTATUS virt2phys_init (void ); + +/** +* Free memory allocated for internal data structures. +* Do not unlock memory so that it's not paged even if driver is unloaded +* when an application still uses this memory. +*/ +void virt2phys_cleanup (void ); + +/** +* Unlock all tracked memory blocks of a process. +* Free memory allocated for tracking of the process. +*/ +void virt2phys_process_cleanup (HANDLE process_id); + +/** +* Lock current process memory region containing @p virt +* and get physical address corresponding to @p virt. +*/ +NTSTATUS virt2phys_translate (PVOID virt,PHYSICAL_ADDRESS *phys); + +#endif /* VIRT2PHYS_LOGIC_H */
Signature · 1 line
2.29.3
PATCH 3/3 windows/virt2phys: add tracing
In reply to Dmitry KozlyukWPP tracing [1] allows kernel drivers to print logs that can be viewed without attaching a debugger to the running system. Traces are colelcted only when enabled. Instrument virt2phys with traces:
* ERROR: failures that prevent the driver from working.
* WARNING: incorrect calls to the driver.
* INFO: starting or completing operations with memory.
[1]: https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/wpp-software-tracing
--- windows/virt2phys/virt2phys.c | 18 +++++++- windows/virt2phys/virt2phys.vcxproj | 5 ++- windows/virt2phys/virt2phys.vcxproj.filters | 3 ++ windows/virt2phys/virt2phys_logic.c | 8 ++++ windows/virt2phys/virt2phys_trace.h | 49 +++++++++++++++++++++ 5 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 windows/virt2phys/virt2phys_trace.h
windows/virt2phys/virt2phys.c +16 -2
@@ -8,6 +8,8 @@#include "virt2phys.h" #include "virt2phys_logic.h" +#include "virt2phys_trace.h" +#include "virt2phys.tmh" DRIVER_INITIALIZE DriverEntry;EVT_WDF_DRIVER_UNLOAD virt2phys_driver_unload;
@@ -46,6 +48,8 @@ DriverEntry(PDRIVER_OBJECT driver_object, PUNICODE_STRING registry_path)if (!NT_SUCCESS (status))return status; +WPP_INIT_TRACING (driver_object, registry_path); +return status; }
@@ -53,11 +57,11 @@ _Use_decl_annotations_VOID virt2phys_driver_unload (WDFDRIVER driver) { -UNREFERENCED_PARAMETER (driver); -PsSetCreateProcessNotifyRoutine (virt2phys_on_process_event, TRUE);virt2phys_cleanup (); + +WPP_CLEANUP (WdfDriverWdmGetDriverObject (driver)); } _Use_decl_annotations_
@@ -79,12 +83,15 @@ virt2phys_driver_EvtDeviceAdd(WDFDRIVER driver, PWDFDEVICE_INIT init) status =WdfDeviceCreate (&init, &attributes, &device);if (!NT_SUCCESS (status)) { +TraceError ("WdfDriverCreate() = %!STATUS!" , status);return status; } status =WdfDeviceCreateDeviceInterface ( device, &GUID_DEVINTERFACE_VIRT2PHYS ,NULL );if (!NT_SUCCESS (status)) { +TraceError ("WdfDeviceCreateDeviceInterface() = %!STATUS!" , + status);return status; }
@@ -109,12 +116,14 @@ virt2phys_device_EvtIoInCallerContext(WDFDEVICE device, WDFREQUEST request)WdfRequestGetParameters (request, ¶ms);if (params.Type != WdfRequestTypeDeviceControl) { +TraceWarning ("Bogus IO request type %lu" , params.Type);WdfRequestComplete (request,STATUS_NOT_SUPPORTED );return ; } code = params.Parameters.DeviceIoControl.IoControlCode;if (code !=IOCTL_VIRT2PHYS_TRANSLATE ) { +TraceWarning ("Bogus IO control code %lx" , code);WdfRequestComplete (request,STATUS_NOT_SUPPORTED );return ; }
@@ -122,6 +131,7 @@ virt2phys_device_EvtIoInCallerContext(WDFDEVICE device, WDFREQUEST request) status =WdfRequestRetrieveInputBuffer ( request,sizeof (*virt), (PVOID *)&virt, &size);if (!NT_SUCCESS (status)) { +TraceWarning ("Retrieving input buffer: %!STATUS!" , status);WdfRequestComplete (request, status);return ; }
@@ -129,6 +139,7 @@ virt2phys_device_EvtIoInCallerContext(WDFDEVICE device, WDFREQUEST request) status =WdfRequestRetrieveOutputBuffer ( request,sizeof (*phys), (PVOID *)&phys, &size);if (!NT_SUCCESS (status)) { +TraceWarning ("Retrieving output buffer: %!STATUS!" , status);WdfRequestComplete (request, status);return ; }
@@ -136,6 +147,9 @@ virt2phys_device_EvtIoInCallerContext(WDFDEVICE device, WDFREQUEST request) status =virt2phys_translate (*virt, phys);if (NT_SUCCESS (status))WdfRequestSetInformation (request,sizeof (*phys)); + +TraceInfo ("Translate %p to %llx: %!STATUS!" , + virt, phys->QuadPart, status);WdfRequestComplete (request, status); }
windows/virt2phys/virt2phys.vcxproj +3 -2
@@ -41,6 +41,7 @@ <ItemGroup> <ClInclude Include="virt2phys.h" /> <ClInclude Include="virt2phys_logic.h" /> + <ClInclude Include="virt2phys_trace.h" /> </ItemGroup> <ItemGroup> <Inf Include="virt2phys.inf" />
@@ -170,9 +171,9 @@ </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ClCompile> - <WppEnabled>false</WppEnabled> + <WppEnabled>true</WppEnabled> <WppRecorderEnabled>true</WppRecorderEnabled> - <WppScanConfigurationData Condition="'%(ClCompile.ScanConfigurationData)' == ''">trace.h</WppScanConfigurationData> + <WppScanConfigurationData Condition="'%(ClCompile.ScanConfigurationData)' == ''">virt2phys_trace.h</WppScanConfigurationData> <WppKernelMode>true</WppKernelMode> </ClCompile> <Link>
windows/virt2phys/virt2phys.vcxproj.filters +3 -0
@@ -30,6 +30,9 @@ <ClInclude Include="virt2phys_logic.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="virt2phys_trace.h"> + <Filter>Header Files</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="virt2phys.c">
windows/virt2phys/virt2phys_logic.c +8 -0
@@ -6,6 +6,8 @@#include <ntddk.h> #include "virt2phys_logic.h" +#include "virt2phys_trace.h" +#include "virt2phys_logic.tmh" struct virt2phys_process {HANDLE id;
@@ -37,6 +39,8 @@ virt2phys_block_create(PMDL mdl)static void virt2phys_block_free (struct virt2phys_block *block,BOOLEAN unmap) { +TraceInfo ("VA = %p, unmap = %!bool!" , block->mdl->StartVa, unmap); +if (unmap)MmUnlockPages (block->mdl);
@@ -63,6 +67,8 @@ virt2phys_process_free(struct virt2phys_process *process, BOOLEAN unmap)PSINGLE_LIST_ENTRY node;struct virt2phys_block *block; +TraceInfo ("ID = %p, unmap = %!bool!" , process->id, unmap); + node = process->blocks.Next;while (node !=NULL ) { block = CONTAINING_RECORD(node, struct virt2phys_block, next);
@@ -185,6 +191,8 @@ virt2phys_add_block(struct virt2phys_process *process, {struct virt2phys_process *existing; +TraceInfo ("ID = %p, VA = %p" , process->id, block->mdl->StartVa); + existing =virt2phys_process_find (process->id);if (existing ==NULL )InsertHeadList (&g_processes, &process->next);
windows/virt2phys/virt2phys_trace.h new file +49 -0
49 lines not on this page · raw message
Signature · 1 line
2.29.3
v2 cover windows/virt2phys: fix paging issue
In reply to Dmitry KozlyukPhysical addresses exposed by virt2phys driver could become pageable. This presents stability and security issues that prevent Microsoft from signing virt2phys, because a signed driver would be trusted by all end-user machines.
Ensure that memory for which physical addresses are exposed by virt2phys is non-pageable at least for the lifetime of the process. As virt2phys code grows, make its development and debugging easier.
There are other known issues that come from using PA and accessing DMA from userspace. They are not related to virt2phys par se. It is planned to address them later by enabling the use of IOMMU for DPDK on Windows.
v2:
* Following ofline review by DmitryM:
- Add comment explaining tracking approach for validation team.
- Replace deprecated allocation API calls.
- Check properties of locked memory (see docs in patch 3/4).
- Add configurable limits for tracked processes and memory.
* Add end-user documentation.
* Drop patch for Inf2Cat settings UseLocalTime=true:
the issue it resolves originated from development VM.
* Add PnpLockdown=1 patch.
Dmitry Kozlyuk (4):
windows/virt2phys: add PnpLockdown directive windows/virt2phys: do not expose pageable physical addresses windows/virt2phys: add limits against resource exhaustion windows/virt2phys: add tracing
windows/virt2phys/README.md | 38 ++ windows/virt2phys/virt2phys.c | 173 ++++++-- windows/virt2phys/virt2phys.inf | 1 + windows/virt2phys/virt2phys.vcxproj | 7 +- windows/virt2phys/virt2phys.vcxproj.filters | 9 + windows/virt2phys/virt2phys_logic.c | 415 ++++++++++++++++++++ windows/virt2phys/virt2phys_logic.h | 39 ++ windows/virt2phys/virt2phys_trace.h | 50 +++ 8 files changed, 701 insertions(+), 31 deletions(-) create mode 100644 windows/virt2phys/README.md create mode 100644 windows/virt2phys/virt2phys_logic.c create mode 100644 windows/virt2phys/virt2phys_logic.h create mode 100644 windows/virt2phys/virt2phys_trace.h
Signature · 1 line
2.29.3
PATCH v2 1/4 windows/virt2phys: add PnpLockdown directive
WDK for Windows 10, version 2004 emits a warning suggesting to add PnpLockdown directive to INI [1]. Add it since virt2phys has no potential use-cases that require modifications to driver files.
[1]: https://docs.microsoft.com/en-us/windows-hardware/drivers/install/inf-version-section
--- windows/virt2phys/virt2phys.inf | 1 + 1 file changed, 1 insertion(+)
windows/virt2phys/virt2phys.inf +1 -0
@@ -8,6 +8,7 @@ ClassGuid = {78A1C341-4539-11d3-B88D-00C04FAD5171} Provider = %ManufacturerName% CatalogFile = virt2phys.cat DriverVer = +PnpLockdown = 1 [DestinationDirs] DefaultDestDir = 12
Signature · 1 line
2.29.3
PATCH v2 2/4 windows/virt2phys: do not expose pageable physical addresses
In reply to Dmitry Kozlyukvirt2phys relied on the user to ensure that memory for which physical address (PA) is obtained is non-pageable and remains such for the lifetime of the process. While DPDK does lock pages in memory, virt2phys can be accessed by any process with sufficient privileges. A malicious process could get PA and make memory pageable. When it is reused for another process, attacker can get access to private data or crash the system if another process is kernel.
Solution is to lock all memory for which PA is requested. Locked blocks are tracked to unlock them when the process exits. If driver is unloaded while some memory is still locked, it leaves pages locked, effectively leaking RAM, trading stability for security.
Factor out a module that locks memory for which physical addresses are obtained and keeps a list of locked memory blocks for each process. It exposes a thread-safe interface for use in driver callbacks. The driver reacts to a process exit by unlocking its tracked blocks.
Also clean up the driver code. Remove debugging output to replace it with structured tracing in the next patch.
---
There's a couple of line length violations, but 80 characters is not a hard limit now and also those lines are ideomatic, splitting won't help readability.
windows/virt2phys/virt2phys.c | 92 +++-- windows/virt2phys/virt2phys.vcxproj | 2 + windows/virt2phys/virt2phys.vcxproj.filters | 6 + windows/virt2phys/virt2phys_logic.c | 368 ++++++++++++++++++++ windows/virt2phys/virt2phys_logic.h | 32 ++ 5 files changed, 470 insertions(+), 30 deletions(-) create mode 100644 windows/virt2phys/virt2phys_logic.c create mode 100644 windows/virt2phys/virt2phys_logic.h
windows/virt2phys/virt2phys.c +62 -30
@@ -1,21 +1,25 @@/* SPDX-License-Identifier: BSD-3-Clause -* Copyright(c) 2020 Dmitry Kozlyuk +* Copyright 2020-2021 Dmitry Kozlyuk */ #include <ntddk.h> #include <wdf.h> -#include <wdmsec.h> #include <initguid.h> #include "virt2phys.h" +#include "virt2phys_logic.h" DRIVER_INITIALIZE DriverEntry; +EVT_WDF_DRIVER_UNLOAD virt2phys_driver_unload;EVT_WDF_DRIVER_DEVICE_ADD virt2phys_driver_EvtDeviceAdd;EVT_WDF_IO_IN_CALLER_CONTEXT virt2phys_device_EvtIoInCallerContext; +static VOID virt2phys_on_process_event ( +HANDLE parent_id,HANDLE process_id,BOOLEAN create); + +_Use_decl_annotations_ NTSTATUS -DriverEntry ( - IN PDRIVER_OBJECT driver_object, IN PUNICODE_STRING registry_path) +DriverEntry (PDRIVER_OBJECT driver_object,PUNICODE_STRING registry_path) {WDF_DRIVER_CONFIG config;WDF_OBJECT_ATTRIBUTES attributes;
@@ -23,22 +27,51 @@ DriverEntry(PAGED_CODE (); -WDF_DRIVER_CONFIG_INIT (&config, virt2phys_driver_EvtDeviceAdd);WDF_OBJECT_ATTRIBUTES_INIT (&attributes); +WDF_DRIVER_CONFIG_INIT (&config, virt2phys_driver_EvtDeviceAdd); + config.EvtDriverUnload = virt2phys_driver_unload; status =WdfDriverCreate ( - driver_object, registry_path, - &attributes, &config,WDF_NO_HANDLE ); -if (!NT_SUCCESS (status)) { -KdPrint (("WdfDriverCreate() failed, status=%08x\n" , status)); - } + driver_object, registry_path, + &attributes, &config,WDF_NO_HANDLE ); +if (!NT_SUCCESS (status)) +return status; + + status =virt2phys_init (); +if (!NT_SUCCESS (status)) +return status; + +/* +* The goal is to ensure that no process obtains a physical address +* of pageable memory. To do this the driver locks every memory region +* for which physical address is requested. This memory must remain +* locked until process has no access to it anymore. A process can use +* the memory after it closes all handles to the interface device, +* so the driver cannot unlock memory at device cleanup callback. +* It has to track process termination instead, after which point +* a process cannot attempt any memory access. +*/ + status =PsSetCreateProcessNotifyRoutine ( + virt2phys_on_process_event, FALSE); +if (!NT_SUCCESS (status)) +return status;return status; } +_Use_decl_annotations_ +VOID +virt2phys_driver_unload (WDFDRIVER driver) +{ +UNREFERENCED_PARAMETER (driver); + +PsSetCreateProcessNotifyRoutine (virt2phys_on_process_event, TRUE); + +virt2phys_cleanup (); +} +_Use_decl_annotations_ NTSTATUS -virt2phys_driver_EvtDeviceAdd ( -WDFDRIVER driver,PWDFDEVICE_INIT init) +virt2phys_driver_EvtDeviceAdd (WDFDRIVER driver,PWDFDEVICE_INIT init) {WDF_OBJECT_ATTRIBUTES attributes;WDFDEVICE device;
@@ -46,8 +79,6 @@ virt2phys_driver_EvtDeviceAdd(UNREFERENCED_PARAMETER (driver); -PAGED_CODE (); -WdfDeviceInitSetIoType ( init, WdfDeviceIoNeither); WdfDeviceInitSetIoInCallerContextCallback(
@@ -57,15 +88,12 @@ virt2phys_driver_EvtDeviceAdd( status =WdfDeviceCreate (&init, &attributes, &device);if (!NT_SUCCESS (status)) { -KdPrint (("WdfDeviceCreate() failed, status=%08x\n" , status));return status; } status =WdfDeviceCreateDeviceInterface ( - device, &GUID_DEVINTERFACE_VIRT2PHYS ,NULL ); + device, &GUID_DEVINTERFACE_VIRT2PHYS ,NULL );if (!NT_SUCCESS (status)) { -KdPrint (("WdfDeviceCreateDeviceInterface() failed, " -"status=%08x\n" , status));return status; }
@@ -74,8 +102,7 @@ virt2phys_driver_EvtDeviceAdd(_Use_decl_annotations_ VOID -virt2phys_device_EvtIoInCallerContext ( - IN WDFDEVICE device, IN WDFREQUEST request) +virt2phys_device_EvtIoInCallerContext (WDFDEVICE device,WDFREQUEST request) {WDF_REQUEST_PARAMETERS params;ULONG code;
@@ -85,21 +112,18 @@ virt2phys_device_EvtIoInCallerContext(NTSTATUS status;UNREFERENCED_PARAMETER (device); -PAGED_CODE ();WDF_REQUEST_PARAMETERS_INIT (¶ms);WdfRequestGetParameters (request, ¶ms);if (params.Type != WdfRequestTypeDeviceControl) { -KdPrint (("bogus request type=%u\n" , params.Type));WdfRequestComplete (request,STATUS_NOT_SUPPORTED );return ; } code = params.Parameters.DeviceIoControl.IoControlCode;if (code !=IOCTL_VIRT2PHYS_TRANSLATE ) { -KdPrint (("bogus IO control code=%lu\n" , code));WdfRequestComplete (request,STATUS_NOT_SUPPORTED );return ; }
@@ -107,8 +131,6 @@ virt2phys_device_EvtIoInCallerContext( status =WdfRequestRetrieveInputBuffer ( request,sizeof (*virt), (PVOID *)&virt, &size);if (!NT_SUCCESS (status)) { -KdPrint (("WdfRequestRetrieveInputBuffer() failed, " -"status=%08x\n" , status));WdfRequestComplete (request, status);return ; }
@@ -116,14 +138,24 @@ virt2phys_device_EvtIoInCallerContext( status =WdfRequestRetrieveOutputBuffer ( request,sizeof (*phys), (PVOID *)&phys, &size);if (!NT_SUCCESS (status)) { -KdPrint (("WdfRequestRetrieveOutputBuffer() failed, " -"status=%08x\n" , status));WdfRequestComplete (request, status);return ; } - *phys =MmGetPhysicalAddress (*virt); + status =virt2phys_translate (*virt, phys); +if (NT_SUCCESS (status)) +WdfRequestSetInformation (request,sizeof (*phys)); +WdfRequestComplete (request, status); +} + +static VOID +virt2phys_on_process_event ( +HANDLE parent_id,HANDLE process_id,BOOLEAN create) +{ +UNREFERENCED_PARAMETER (parent_id); + +if (create) +return ; -WdfRequestCompleteWithInformation ( - request,STATUS_SUCCESS ,sizeof (*phys)); +virt2phys_process_cleanup (process_id); }
windows/virt2phys/virt2phys.vcxproj +2 -0
@@ -36,9 +36,11 @@ </ItemGroup> <ItemGroup> <ClCompile Include="virt2phys.c" /> + <ClCompile Include="virt2phys_logic.c" /> </ItemGroup> <ItemGroup> <ClInclude Include="virt2phys.h" /> + <ClInclude Include="virt2phys_logic.h" /> </ItemGroup> <ItemGroup> <Inf Include="virt2phys.inf" />
windows/virt2phys/virt2phys.vcxproj.filters +6 -0
@@ -27,10 +27,16 @@ <ClInclude Include="virt2phys.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="virt2phys_logic.h"> + <Filter>Header Files</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="virt2phys.c"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="virt2phys_logic.c"> + <Filter>Source Files</Filter> + </ClCompile> </ItemGroup> </Project>
windows/virt2phys/virt2phys_logic.c new file +368 -0
368 lines not on this page · raw message
windows/virt2phys/virt2phys_logic.h new file +32 -0
@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright 2021 Dmitry Kozlyuk +*/ + +#ifndef VIRT2PHYS_LOGIC_H +#define VIRT2PHYS_LOGIC_H + +/** +* Initialize internal data structures. +*/ +NTSTATUS virt2phys_init (void ); + +/** +* Free memory allocated for internal data structures. +* Do not unlock memory so that it's not paged even if driver is unloaded +* when an application still uses this memory. +*/ +void virt2phys_cleanup (void ); + +/** +* Unlock all tracked memory blocks of a process. +* Free memory allocated for tracking of the process. +*/ +void virt2phys_process_cleanup (HANDLE process_id); + +/** +* Lock current process memory region containing @p virt +* and get physical address corresponding to @p virt. +*/ +NTSTATUS virt2phys_translate (PVOID virt,PHYSICAL_ADDRESS *phys); + +#endif /* VIRT2PHYS_LOGIC_H */
Signature · 1 line
2.29.3
PATCH v2 3/4 windows/virt2phys: add limits against resource exhaustion
In reply to Dmitry KozlyukTracked processes are enumerated in process creation/termination callback, making it O(N = number of tracked processes). A malicious user could cause system-wide slowdown of process termination by making just one call to virt2phys from many processes. Limit the number of tracked processes by ProcessCountLimit items.
Any process with access to virt2phys could exhaust RAM for all the system by locking it. It can also exhaust RAM needed for other processes with access to virt2phys. Limit amount of memory locked by each tracked process with ProcessMemoryLimitMB.
All limits are read from registry at driver load. Each limit can be turned off by setting it to 0. Document limits and their non-zero defaults for administrators.
--- windows/virt2phys/README.md | 38 +++++++++++++++ windows/virt2phys/virt2phys.c | 73 ++++++++++++++++++++++++++++- windows/virt2phys/virt2phys_logic.c | 46 +++++++++++++----- windows/virt2phys/virt2phys_logic.h | 9 +++- 4 files changed, 152 insertions(+), 14 deletions(-) create mode 100644 windows/virt2phys/README.md
windows/virt2phys/README.md new file +38 -0
@@ -0,0 +1,38 @@ +Virtual to Physical Address Translator +====================================== + +Purpose and Operation +--------------------- + +``virt2phys`` driver allows user-mode processes to obtain physical address +of a given virtual address in their address space. +Virtual addresses must belong to regions from process private working set. +These regions must be physically contiguous. +The driver ensures that memory regions with translated addresses +are not swapped out as long as the process has access to this memory. + +It is not safe to administratively unload the driver +while there are processes that have used virt2phys to translate addresses. +Doing so will permanently leak RAM occupied by all memory regions +that contain translated addresses. +Terminate all such processes before unloading the driver. + +Configuration +------------- + +``virt2phys`` is configured at loading time via registry key +``HKLM\SYSTEM\ControlSet001\Services\virt2phys\Parameters``. + +* ``ProcessCountLimit`` (default 16) + + Maximum number of processes that can have access to memory regions + with translated addresses. When this limit is reached, no more processes + can translate addresses using ``virt2phys``. Large number of tracked + processes may slow down system operation. Set limit to 0 to disable it. + +* ``ProcessMemoryLimitMB`` (default 16384, i.e. 16 GB) + + Maximum amount of memory in all regions that contain translated addresses, + total per process. When this limit is reached, the process can not translate + addresses from new regions. Large values can cause RAM exhaustion. + Set limit to 0 to disable it.
windows/virt2phys/virt2phys.c +71 -2
103 lines not on this page · raw message
windows/virt2phys/virt2phys_logic.c +35 -11
103 lines not on this page · raw message
windows/virt2phys/virt2phys_logic.h +8 -1
@@ -5,10 +5,17 @@#ifndef VIRT2PHYS_LOGIC_H #define VIRT2PHYS_LOGIC_H +struct virt2phys_params { +/** Maximum number of tracked processes (0 = unlimited). */ +ULONG process_count_limit; +/** Maximum amount of memory locked by a process (0 = unlimited). */ +ULONG64 process_memory_limit; +}; +/** * Initialize internal data structures. */ -NTSTATUS virt2phys_init (void ); +NTSTATUS virt2phys_init (const struct virt2phys_params *params);/** * Free memory allocated for internal data structures.
Signature · 1 line
2.29.3
PATCH v2 4/4 windows/virt2phys: add tracing
In reply to Dmitry KozlyukWPP tracing [1] allows kernel drivers to print logs that can be viewed without attaching a debugger to the running system. Traces are colelcted only when enabled. Instrument virt2phys with traces:
* ERROR: failures that prevent the driver from working.
* WARNING: incorrect calls to the driver.
* INFO: starting or completing operations with memory.
[1]: https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/wpp-software-tracing
---
Comment at the bottom of virt2phys_trace.h is consumed by WPP code generator, which is the reason it has no leading asterisks.
windows/virt2phys/virt2phys.c | 18 +++++++- windows/virt2phys/virt2phys.vcxproj | 5 ++- windows/virt2phys/virt2phys.vcxproj.filters | 3 ++ windows/virt2phys/virt2phys_logic.c | 35 ++++++++++++--- windows/virt2phys/virt2phys_trace.h | 50 +++++++++++++++++++++ 5 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 windows/virt2phys/virt2phys_trace.h
windows/virt2phys/virt2phys.c +16 -2
@@ -8,6 +8,8 @@#include "virt2phys.h" #include "virt2phys_logic.h" +#include "virt2phys_trace.h" +#include "virt2phys.tmh" DRIVER_INITIALIZE DriverEntry;EVT_WDF_DRIVER_UNLOAD virt2phys_driver_unload;
@@ -66,6 +68,8 @@ DriverEntry(PDRIVER_OBJECT driver_object, PUNICODE_STRING registry_path)if (!NT_SUCCESS (status))return status; +WPP_INIT_TRACING (driver_object, registry_path); +return status; }
@@ -131,11 +135,11 @@ _Use_decl_annotations_VOID virt2phys_driver_unload (WDFDRIVER driver) { -UNREFERENCED_PARAMETER (driver); -PsSetCreateProcessNotifyRoutine (virt2phys_on_process_event, TRUE);virt2phys_cleanup (); + +WPP_CLEANUP (WdfDriverWdmGetDriverObject (driver)); } _Use_decl_annotations_
@@ -157,12 +161,15 @@ virt2phys_driver_EvtDeviceAdd(WDFDRIVER driver, PWDFDEVICE_INIT init) status =WdfDeviceCreate (&init, &attributes, &device);if (!NT_SUCCESS (status)) { +TraceError ("WdfDriverCreate() = %!STATUS!" , status);return status; } status =WdfDeviceCreateDeviceInterface ( device, &GUID_DEVINTERFACE_VIRT2PHYS ,NULL );if (!NT_SUCCESS (status)) { +TraceError ("WdfDeviceCreateDeviceInterface() = %!STATUS!" , + status);return status; }
@@ -187,12 +194,14 @@ virt2phys_device_EvtIoInCallerContext(WDFDEVICE device, WDFREQUEST request)WdfRequestGetParameters (request, ¶ms);if (params.Type != WdfRequestTypeDeviceControl) { +TraceWarning ("Bogus IO request type %lu" , params.Type);WdfRequestComplete (request,STATUS_NOT_SUPPORTED );return ; } code = params.Parameters.DeviceIoControl.IoControlCode;if (code !=IOCTL_VIRT2PHYS_TRANSLATE ) { +TraceWarning ("Bogus IO control code %lx" , code);WdfRequestComplete (request,STATUS_NOT_SUPPORTED );return ; }
@@ -200,6 +209,7 @@ virt2phys_device_EvtIoInCallerContext(WDFDEVICE device, WDFREQUEST request) status =WdfRequestRetrieveInputBuffer ( request,sizeof (*virt), (PVOID *)&virt, &size);if (!NT_SUCCESS (status)) { +TraceWarning ("Retrieving input buffer: %!STATUS!" , status);WdfRequestComplete (request, status);return ; }
@@ -207,6 +217,7 @@ virt2phys_device_EvtIoInCallerContext(WDFDEVICE device, WDFREQUEST request) status =WdfRequestRetrieveOutputBuffer ( request,sizeof (*phys), (PVOID *)&phys, &size);if (!NT_SUCCESS (status)) { +TraceWarning ("Retrieving output buffer: %!STATUS!" , status);WdfRequestComplete (request, status);return ; }
@@ -214,6 +225,9 @@ virt2phys_device_EvtIoInCallerContext(WDFDEVICE device, WDFREQUEST request) status =virt2phys_translate (*virt, phys);if (NT_SUCCESS (status))WdfRequestSetInformation (request,sizeof (*phys)); + +TraceInfo ("Translate %p to %llx: %!STATUS!" , + virt, phys->QuadPart, status);WdfRequestComplete (request, status); }
windows/virt2phys/virt2phys.vcxproj +3 -2
@@ -41,6 +41,7 @@ <ItemGroup> <ClInclude Include="virt2phys.h" /> <ClInclude Include="virt2phys_logic.h" /> + <ClInclude Include="virt2phys_trace.h" /> </ItemGroup> <ItemGroup> <Inf Include="virt2phys.inf" />
@@ -169,9 +170,9 @@ </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ClCompile> - <WppEnabled>false</WppEnabled> + <WppEnabled>true</WppEnabled> <WppRecorderEnabled>true</WppRecorderEnabled> - <WppScanConfigurationData Condition="'%(ClCompile.ScanConfigurationData)' == ''">trace.h</WppScanConfigurationData> + <WppScanConfigurationData Condition="'%(ClCompile.ScanConfigurationData)' == ''">virt2phys_trace.h</WppScanConfigurationData> <WppKernelMode>true</WppKernelMode> </ClCompile> <Link>
windows/virt2phys/virt2phys.vcxproj.filters +3 -0
@@ -30,6 +30,9 @@ <ClInclude Include="virt2phys_logic.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="virt2phys_trace.h"> + <Filter>Header Files</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="virt2phys.c">
windows/virt2phys/virt2phys_logic.c +29 -6
@@ -6,6 +6,8 @@#include <ntddk.h> #include "virt2phys_logic.h" +#include "virt2phys_trace.h" +#include "virt2phys_logic.tmh" struct virt2phys_process {HANDLE id;
@@ -38,6 +40,8 @@ virt2phys_block_create(PMDL mdl)static void virt2phys_block_free (struct virt2phys_block *block,BOOLEAN unmap) { +TraceInfo ("VA = %p, unmap = %!bool!" , block->mdl->StartVa, unmap); +if (unmap)MmUnlockPages (block->mdl);
@@ -76,6 +80,8 @@ virt2phys_process_free(struct virt2phys_process *process, BOOLEAN unmap)PSINGLE_LIST_ENTRY node;struct virt2phys_block *block; +TraceInfo ("ID = %p, unmap = %!bool!" , process->id, unmap); + node = process->blocks.Next;while (node !=NULL ) { block = CONTAINING_RECORD(node, struct virt2phys_block, next);
@@ -208,6 +214,8 @@ virt2phys_add_block(struct virt2phys_process *process,struct virt2phys_process *existing;size_t size; +TraceInfo ("ID = %p, VA = %p" , process->id, block->mdl->StartVa); + existing =virt2phys_process_find (process->id); *process_exists = existing !=NULL ;if (existing ==NULL ) {
@@ -217,8 +225,11 @@ virt2phys_add_block(struct virt2phys_process *process,* because decrement is done without holding the lock. */ if (virt2phys_exceeeds (g_process_count +1 , - g_params.process_count_limit)) + g_params.process_count_limit)) { +TraceWarning ("Process count limit reached (%lu)" , + g_params.process_count_limit);return STATUS_QUOTA_EXCEEDED ; + }InsertHeadList (&g_processes, &process->next);InterlockedIncrement (&g_process_count);
@@ -227,8 +238,11 @@ virt2phys_add_block(struct virt2phys_process *process, size =MmGetMdlByteCount (block->mdl);if (virt2phys_exceeeds (process->memory + size, - g_params.process_memory_limit)) + g_params.process_memory_limit)) { +TraceWarning ("Process %p memory limit reached (%llu bytes)" , + process->id, g_params.process_memory_limit);return STATUS_QUOTA_EXCEEDED ; + }PushEntryList (&process->blocks, &block->next); process->memory += size;
@@ -277,8 +291,10 @@ virt2phys_check_memory(PMDL mdl)size_t size;NTSTATUS status; -if (!virt2phys_is_contiguous (mdl)) +if (!virt2phys_is_contiguous (mdl)) { +TraceWarning ("Locked region is not physycally contiguous" );return STATUS_UNSUCCESSFUL ; + } virt =MmGetMdlVirtualAddress (mdl); size =MmGetMdlByteCount (mdl);
@@ -288,12 +304,19 @@ virt2phys_check_memory(PMDL mdl)if (!NT_SUCCESS (status))return status; -if (info.AllocationBase != virt || info.RegionSize != size) +if (info.AllocationBase != virt || info.RegionSize != size) { +TraceWarning ("Race for the region: supplied %p (%llu bytes), locked %p (%llu bytes)" , + virt, size, info.AllocationBase, info.RegionSize);return STATUS_UNSUCCESSFUL ; -if (info.State !=MEM_COMMIT ) + } +if (info.State !=MEM_COMMIT ) { +TraceWarning ("Attempt to lock uncommitted memory" );return STATUS_UNSUCCESSFUL ; -if (info.Type !=MEM_PRIVATE ) + } +if (info.Type !=MEM_PRIVATE ) { +TraceWarning ("Attempt to lock shared memory" );return STATUS_UNSUCCESSFUL ; + }return status; }
windows/virt2phys/virt2phys_trace.h new file +50 -0
50 lines not on this page · raw message
Signature · 1 line
2.29.3
Re: [dpdk-dev] [kmods PATCH v2 4/4] windows/virt2phys: add tracing
Hi Dmitry,
Quoted 7 lines · Dmitry Kozlyuk
WPP tracing [1] allows kernel drivers to print logs that can be viewed without attaching a debugger to the running system. Traces are colelcted only when enabled. Instrument virt2phys with traces:
* ERROR: failures that prevent the driver from working.
* WARNING: incorrect calls to the driver.
* INFO: starting or completing operations with memory.
Quoted 5 lines
[1]: https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/wpp-software-tracing
Signed-off-by Dmitry Kozlyuk <[email protected]>---
<snip!>
windows/virt2phys/virt2phys.vcxproj +0 -0
Quoted 12 lines
index b462493..c9f884a 100644
windows/virt2phys/virt2phys.vcxproj +1
-0@@ -41,6 +41,7 @@ <ItemGroup> <ClInclude Include="virt2phys.h" /> <ClInclude Include="virt2phys_logic.h" /> + <ClInclude Include="virt2phys_trace.h" /> </ItemGroup> <ItemGroup> <Inf Include="virt2phys.inf" />
windows/virt2phys/virt2phys.vcxproj line 175
Quoted 8 lines
windows/virt2phys/virt2phys.vcxproj +2
-2@@ -169,9 +170,9 @@ </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ClCompile> - <WppEnabled>false</WppEnabled> + <WppEnabled>true</WppEnabled> <WppRecorderEnabled>true</WppRecorderEnabled> - <WppScanConfigurationData Condition="'%(ClCompile.ScanConfigurationData)' == ''">trace.h</WppScanConfigurationData> + <WppScanConfigurationData Condition="'%(ClCompile.ScanConfigurationData)' == ''">virt2phys_trace.h</WppScanConfigurationData>
This change is also required for the 'Release|x64' configuration, otherwise 'Release' builds fail.
<snip!>
Also, it appears the newer version of the compiler (combined with the new version of WDK/SDK), requires that the driver signing process mandate a File Digest Algorithm (using the /fd option). It is a warning today, but they claim it could become a requirement in the future.
To fix this, we can include the following in the project file:
<DriverSign> <FileDigestAlgorithm>SHA256</FileDigestAlgorithm> </DriverSign>
Or set the above, using project 'Properties'->Driver Signing->File Digest Algorithm = 256.
(This will need to be fixed in the netuio driver project also)
thanks,
ranjit m.
Re: [dpdk-dev] [kmods PATCH v2 4/4] windows/virt2phys: add tracing
Typo below:
Quoted 45 lines · Ranjit
Hi Dmitry,
Quoted 12 lines · Dmitry Kozlyuk
WPP tracing [1] allows kernel drivers to print logs that can be viewed without attaching a debugger to the running system. Traces are colelcted only when enabled. Instrument virt2phys with traces:
* ERROR: failures that prevent the driver from working.
* WARNING: incorrect calls to the driver.
* INFO: starting or completing operations with memory.[1]: https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/wpp-software-tracing
Signed-off-by Dmitry Kozlyuk <[email protected]>---
<snip!>windows/virt2phys/virt2phys.vcxproj +0
-0Quoted 14 lines
index b462493..c9f884a 100644
windows/virt2phys/virt2phys.vcxproj +0
-0@@ -41,6 +41,7 @@<ItemGroup>
<ClInclude Include="virt2phys.h" />
<ClInclude Include="virt2phys_logic.h" />
+ <ClInclude Include="virt2phys_trace.h" />
</ItemGroup>
<ItemGroup>
<Inf Include="virt2phys.inf" />windows/virt2phys/virt2phys.vcxproj +0
-0@@ -169,9 +170,9 @@</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Quoted 8 lines
<ClCompile>
- <WppEnabled>false</WppEnabled>
+ <WppEnabled>true</WppEnabled>
<WppRecorderEnabled>true</WppRecorderEnabled>
- <WppScanConfigurationData
Condition="'%(ClCompile.ScanConfigurationData)' == ''">trace.h</WppScanConfigurationData> + <WppScanConfigurationData
Condition="'%(ClCompile.ScanConfigurationData)' == ''">virt2phys_trace.h</WppScanConfigurationData>This change is also required for the 'Release|x64' configuration, otherwise 'Release' builds fail.
<snip!>
Also, it appears the newer version of the compiler (combined with the new version of WDK/SDK), requires that the driver signing process mandate a File Digest Algorithm (using the /fd option). It is a warning today, but they claim it could become a requirement in the future.
Quoted 7 lines
To fix this, we can include the following in the project file:
<DriverSign> <FileDigestAlgorithm>SHA256</FileDigestAlgorithm> </DriverSign>
Or set the above, using project 'Properties'->Driver
Signing->FileDigest Algorithm = 256.
Should be:
"Or set the above, using project 'Properties'->Driver Signing->File Digest Algorithm = SHA256"
Quoted 7 lines
(This will need to be fixed in the netuio driver project also)
thanks,
ranjit m.
Re: [dpdk-dev] [kmods PATCH v2 4/4] windows/virt2phys: add tracing
In reply to Menon, Ranjit2021-09-30 15:07 (UTC-0700), Menon, Ranjit:
Quoted 11 lines
[...]
Review
Quoted 7 lines
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ClCompile>(quoted diff) +2
-2- <WppEnabled>false</WppEnabled> + <WppEnabled>true</WppEnabled> <WppRecorderEnabled>true</WppRecorderEnabled> - <WppScanConfigurationData Condition="'%(ClCompile.ScanConfigurationData)' == ''">trace.h</WppScanConfigurationData> + <WppScanConfigurationData Condition="'%(ClCompile.ScanConfigurationData)' == ''">virt2phys_trace.h</WppScanConfigurationData>This change is also required for the 'Release|x64' configuration, otherwise 'Release' builds fail.
Thanks Ranjit, will fix in v3.
Quoted 6 lines
<snip!>
Also, it appears the newer version of the compiler (combined with the new version of WDK/SDK), requires that the driver signing process mandate a File Digest Algorithm (using the /fd option). It is a warning today, but they claim it could become a requirement in the future.
Yes, it started with WDK 22000.1. On my system it's an error, not a warning. I think I'll create a separate series with trivial fixes like this, so that it can be merged quickly and fix the build with newer WDK.
26/05/2021 23:01, Dmitry Kozlyuk:
Quoted 11 lines
v2:
* Following ofline review by DmitryM:
- Add comment explaining tracking approach for validation team.
- Replace deprecated allocation API calls.
- Check properties of locked memory (see docs in patch 3/4).
- Add configurable limits for tracked processes and memory.
* Add end-user documentation.
* Drop patch for Inf2Cat settings UseLocalTime=true:
the issue it resolves originated from development VM.
* Add PnpLockdown=1 patch.
Quoted 5 lines
Dmitry Kozlyuk (4):
windows/virt2phys: add PnpLockdown directive windows/virt2phys: do not expose pageable physical addresses windows/virt2phys: add limits against resource exhaustion windows/virt2phys: add tracing
Waiting for reviews, especially from Microsoft experts.
23/06/2021 09:13, Thomas Monjalon:
Quoted 12 lines
26/05/2021 23:01, Dmitry Kozlyuk:
Quoted 11 lines
v2:
* Following ofline review by DmitryM:
- Add comment explaining tracking approach for validation team.
- Replace deprecated allocation API calls.
- Check properties of locked memory (see docs in patch 3/4).
- Add configurable limits for tracked processes and memory.
* Add end-user documentation.
* Drop patch for Inf2Cat settings UseLocalTime=true:
the issue it resolves originated from development VM.
* Add PnpLockdown=1 patch.
Quoted 7 lines
Quoted 5 lines
Dmitry Kozlyuk (4):
windows/virt2phys: add PnpLockdown directive windows/virt2phys: do not expose pageable physical addresses windows/virt2phys: add limits against resource exhaustion windows/virt2phys: add tracingWaiting for reviews, especially from Microsoft experts.
Why is it taking so long?
2021-09-30 22:24 (UTC+0200), Thomas Monjalon:
Quoted 19 lines
23/06/2021 09:13, Thomas Monjalon:
Quoted 18 lines
26/05/2021 23:01, Dmitry Kozlyuk:
Quoted 16 lines
v2:
* Following ofline review by DmitryM:
- Add comment explaining tracking approach for validation team.
- Replace deprecated allocation API calls.
- Check properties of locked memory (see docs in patch 3/4).
- Add configurable limits for tracked processes and memory.
* Add end-user documentation.
* Drop patch for Inf2Cat settings UseLocalTime=true:
the issue it resolves originated from development VM.
* Add PnpLockdown=1 patch.Dmitry Kozlyuk (4):
windows/virt2phys: add PnpLockdown directive windows/virt2phys: do not expose pageable physical addresses windows/virt2phys: add limits against resource exhaustion windows/virt2phys: add tracing
Quoted 3 lines
Quoted 1 line
Waiting for reviews, especially from Microsoft experts.
Why is it taking so long?
DmitryM, Microsoft expert who suggested this improvement, has been unavailable until recently. It is expected that he will review this series shortly now.
v3 cover windows/virt2phys: fix paging issue
In reply to Dmitry KozlyukPhysical addresses exposed by virt2phys driver could become pageable. This presents stability and security issues that prevent Microsoft from signing virt2phys, because a signed driver would be trusted by all end-user machines.
Ensure that memory for which physical addresses are exposed by virt2phys is non-pageable at least for the lifetime of the process. As virt2phys code grows, make its development and debugging easier.
There are other known issues that come from using PA and accessing DMA from userspace. They are not related to virt2phys par se. It is planned to address them later by enabling the use of IOMMU for DPDK on Windows.
Depends-on: series-19342 ("windows: independent fixes")
v3:
* Fix Release build (Ranjit).
* Drop PnpLockdown=1 patch as it is now in dependency series.
v2:
* Following ofline review by DmitryM:
- Add comment explaining tracking approach for validation team.
- Replace deprecated allocation API calls.
- Check properties of locked memory (see docs in patch 3/4).
- Add configurable limits for tracked processes and memory.
* Add end-user documentation.
* Drop patch for Inf2Cat settings UseLocalTime=true:
the issue it resolves originated from development VM.
* Add PnpLockdown=1 patch.
Dmitry Kozlyuk (3):
windows/virt2phys: do not expose pageable physical addresses windows/virt2phys: add limits against resource exhaustion windows/virt2phys: add tracing
windows/virt2phys/README.md | 38 ++ windows/virt2phys/virt2phys.c | 173 ++++++-- windows/virt2phys/virt2phys.vcxproj | 11 +- windows/virt2phys/virt2phys.vcxproj.filters | 11 +- windows/virt2phys/virt2phys_logic.c | 415 ++++++++++++++++++++ windows/virt2phys/virt2phys_logic.h | 39 ++ windows/virt2phys/virt2phys_trace.h | 50 +++ 7 files changed, 703 insertions(+), 34 deletions(-) create mode 100644 windows/virt2phys/README.md create mode 100644 windows/virt2phys/virt2phys_logic.c create mode 100644 windows/virt2phys/virt2phys_logic.h create mode 100644 windows/virt2phys/virt2phys_trace.h
Signature · 1 line
2.29.3
PATCH v3 1/3 windows/virt2phys: do not expose pageable physical addresses
virt2phys relied on the user to ensure that memory for which physical address (PA) is obtained is non-pageable and remains such for the lifetime of the process. While DPDK does lock pages in memory, virt2phys can be accessed by any process with sufficient privileges. A malicious process could get PA and make memory pageable. When it is reused for another process, attacker can get access to private data or crash the system if another process is kernel.
Solution is to lock all memory for which PA is requested. Locked blocks are tracked to unlock them when the process exits. If driver is unloaded while some memory is still locked, it leaves pages locked, effectively leaking RAM, trading stability for security.
Factor out a module that locks memory for which physical addresses are obtained and keeps a list of locked memory blocks for each process. It exposes a thread-safe interface for use in driver callbacks. The driver reacts to a process exit by unlocking its tracked blocks.
Also clean up the driver code. Remove debugging output to replace it with structured tracing in the next patch.
--- windows/virt2phys/virt2phys.c | 92 +++-- windows/virt2phys/virt2phys.vcxproj | 2 + windows/virt2phys/virt2phys.vcxproj.filters | 8 +- windows/virt2phys/virt2phys_logic.c | 368 ++++++++++++++++++++ windows/virt2phys/virt2phys_logic.h | 32 ++ 5 files changed, 471 insertions(+), 31 deletions(-) create mode 100644 windows/virt2phys/virt2phys_logic.c create mode 100644 windows/virt2phys/virt2phys_logic.h
windows/virt2phys/virt2phys.c +62 -30
@@ -1,21 +1,25 @@/* SPDX-License-Identifier: BSD-3-Clause -* Copyright(c) 2020 Dmitry Kozlyuk +* Copyright 2020-2021 Dmitry Kozlyuk */ #include <ntddk.h> #include <wdf.h> -#include <wdmsec.h> #include <initguid.h> #include "virt2phys.h" +#include "virt2phys_logic.h" DRIVER_INITIALIZE DriverEntry; +EVT_WDF_DRIVER_UNLOAD virt2phys_driver_unload;EVT_WDF_DRIVER_DEVICE_ADD virt2phys_driver_EvtDeviceAdd;EVT_WDF_IO_IN_CALLER_CONTEXT virt2phys_device_EvtIoInCallerContext; +static VOID virt2phys_on_process_event ( +HANDLE parent_id,HANDLE process_id,BOOLEAN create); + +_Use_decl_annotations_ NTSTATUS -DriverEntry ( - IN PDRIVER_OBJECT driver_object, IN PUNICODE_STRING registry_path) +DriverEntry (PDRIVER_OBJECT driver_object,PUNICODE_STRING registry_path) {WDF_DRIVER_CONFIG config;WDF_OBJECT_ATTRIBUTES attributes;
@@ -23,22 +27,51 @@ DriverEntry(PAGED_CODE (); -WDF_DRIVER_CONFIG_INIT (&config, virt2phys_driver_EvtDeviceAdd);WDF_OBJECT_ATTRIBUTES_INIT (&attributes); +WDF_DRIVER_CONFIG_INIT (&config, virt2phys_driver_EvtDeviceAdd); + config.EvtDriverUnload = virt2phys_driver_unload; status =WdfDriverCreate ( - driver_object, registry_path, - &attributes, &config,WDF_NO_HANDLE ); -if (!NT_SUCCESS (status)) { -KdPrint (("WdfDriverCreate() failed, status=%08x\n" , status)); - } + driver_object, registry_path, + &attributes, &config,WDF_NO_HANDLE ); +if (!NT_SUCCESS (status)) +return status; + + status =virt2phys_init (); +if (!NT_SUCCESS (status)) +return status; + +/* +* The goal is to ensure that no process obtains a physical address +* of pageable memory. To do this the driver locks every memory region +* for which physical address is requested. This memory must remain +* locked until process has no access to it anymore. A process can use +* the memory after it closes all handles to the interface device, +* so the driver cannot unlock memory at device cleanup callback. +* It has to track process termination instead, after which point +* a process cannot attempt any memory access. +*/ + status =PsSetCreateProcessNotifyRoutine ( + virt2phys_on_process_event, FALSE); +if (!NT_SUCCESS (status)) +return status;return status; } +_Use_decl_annotations_ +VOID +virt2phys_driver_unload (WDFDRIVER driver) +{ +UNREFERENCED_PARAMETER (driver); + +PsSetCreateProcessNotifyRoutine (virt2phys_on_process_event, TRUE); + +virt2phys_cleanup (); +} +_Use_decl_annotations_ NTSTATUS -virt2phys_driver_EvtDeviceAdd ( -WDFDRIVER driver,PWDFDEVICE_INIT init) +virt2phys_driver_EvtDeviceAdd (WDFDRIVER driver,PWDFDEVICE_INIT init) {WDF_OBJECT_ATTRIBUTES attributes;WDFDEVICE device;
@@ -46,8 +79,6 @@ virt2phys_driver_EvtDeviceAdd(UNREFERENCED_PARAMETER (driver); -PAGED_CODE (); -WdfDeviceInitSetIoType ( init, WdfDeviceIoNeither); WdfDeviceInitSetIoInCallerContextCallback(
@@ -57,15 +88,12 @@ virt2phys_driver_EvtDeviceAdd( status =WdfDeviceCreate (&init, &attributes, &device);if (!NT_SUCCESS (status)) { -KdPrint (("WdfDeviceCreate() failed, status=%08x\n" , status));return status; } status =WdfDeviceCreateDeviceInterface ( - device, &GUID_DEVINTERFACE_VIRT2PHYS ,NULL ); + device, &GUID_DEVINTERFACE_VIRT2PHYS ,NULL );if (!NT_SUCCESS (status)) { -KdPrint (("WdfDeviceCreateDeviceInterface() failed, " -"status=%08x\n" , status));return status; }
@@ -74,8 +102,7 @@ virt2phys_driver_EvtDeviceAdd(_Use_decl_annotations_ VOID -virt2phys_device_EvtIoInCallerContext ( - IN WDFDEVICE device, IN WDFREQUEST request) +virt2phys_device_EvtIoInCallerContext (WDFDEVICE device,WDFREQUEST request) {WDF_REQUEST_PARAMETERS params;ULONG code;
@@ -85,21 +112,18 @@ virt2phys_device_EvtIoInCallerContext(NTSTATUS status;UNREFERENCED_PARAMETER (device); -PAGED_CODE ();WDF_REQUEST_PARAMETERS_INIT (¶ms);WdfRequestGetParameters (request, ¶ms);if (params.Type != WdfRequestTypeDeviceControl) { -KdPrint (("bogus request type=%u\n" , params.Type));WdfRequestComplete (request,STATUS_NOT_SUPPORTED );return ; } code = params.Parameters.DeviceIoControl.IoControlCode;if (code !=IOCTL_VIRT2PHYS_TRANSLATE ) { -KdPrint (("bogus IO control code=%lu\n" , code));WdfRequestComplete (request,STATUS_NOT_SUPPORTED );return ; }
@@ -107,8 +131,6 @@ virt2phys_device_EvtIoInCallerContext( status =WdfRequestRetrieveInputBuffer ( request,sizeof (*virt), (PVOID *)&virt, &size);if (!NT_SUCCESS (status)) { -KdPrint (("WdfRequestRetrieveInputBuffer() failed, " -"status=%08x\n" , status));WdfRequestComplete (request, status);return ; }
@@ -116,14 +138,24 @@ virt2phys_device_EvtIoInCallerContext( status =WdfRequestRetrieveOutputBuffer ( request,sizeof (*phys), (PVOID *)&phys, &size);if (!NT_SUCCESS (status)) { -KdPrint (("WdfRequestRetrieveOutputBuffer() failed, " -"status=%08x\n" , status));WdfRequestComplete (request, status);return ; } - *phys =MmGetPhysicalAddress (*virt); + status =virt2phys_translate (*virt, phys); +if (NT_SUCCESS (status)) +WdfRequestSetInformation (request,sizeof (*phys)); +WdfRequestComplete (request, status); +} + +static VOID +virt2phys_on_process_event ( +HANDLE parent_id,HANDLE process_id,BOOLEAN create) +{ +UNREFERENCED_PARAMETER (parent_id); + +if (create) +return ; -WdfRequestCompleteWithInformation ( - request,STATUS_SUCCESS ,sizeof (*phys)); +virt2phys_process_cleanup (process_id); }
windows/virt2phys/virt2phys.vcxproj +2 -0
@@ -12,9 +12,11 @@ </ItemGroup> <ItemGroup> <ClCompile Include="virt2phys.c" /> + <ClCompile Include="virt2phys_logic.c" /> </ItemGroup> <ItemGroup> <ClInclude Include="virt2phys.h" /> + <ClInclude Include="virt2phys_logic.h" /> </ItemGroup> <ItemGroup> <Inf Include="virt2phys.inf" />
windows/virt2phys/virt2phys.vcxproj.filters +7 -1
@@ -27,10 +27,16 @@ <ClInclude Include="virt2phys.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="virt2phys_logic.h"> + <Filter>Header Files</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="virt2phys.c"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="virt2phys_logic.c"> + <Filter>Source Files</Filter> + </ClCompile> </ItemGroup> -</Project> +</Project>
windows/virt2phys/virt2phys_logic.c new file +368 -0
368 lines not on this page · raw message
windows/virt2phys/virt2phys_logic.h new file +32 -0
@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright 2021 Dmitry Kozlyuk +*/ + +#ifndef VIRT2PHYS_LOGIC_H +#define VIRT2PHYS_LOGIC_H + +/** +* Initialize internal data structures. +*/ +NTSTATUS virt2phys_init (void ); + +/** +* Free memory allocated for internal data structures. +* Do not unlock memory so that it's not paged even if driver is unloaded +* when an application still uses this memory. +*/ +void virt2phys_cleanup (void ); + +/** +* Unlock all tracked memory blocks of a process. +* Free memory allocated for tracking of the process. +*/ +void virt2phys_process_cleanup (HANDLE process_id); + +/** +* Lock current process memory region containing @p virt +* and get physical address corresponding to @p virt. +*/ +NTSTATUS virt2phys_translate (PVOID virt,PHYSICAL_ADDRESS *phys); + +#endif /* VIRT2PHYS_LOGIC_H */
Signature · 1 line
2.29.3
PATCH v3 2/3 windows/virt2phys: add limits against resource exhaustion
In reply to Dmitry KozlyukTracked processes are enumerated in process creation/termination callback, making it O(N = number of tracked processes). A malicious user could cause system-wide slowdown of process termination by making just one call to virt2phys from many processes. Limit the number of tracked processes by ProcessCountLimit items.
Any process with access to virt2phys could exhaust RAM for all the system by locking it. It can also exhaust RAM needed for other processes with access to virt2phys. Limit amount of memory locked by each tracked process with ProcessMemoryLimitMB.
All limits are read from registry at driver load. Each limit can be turned off by setting it to 0. Document limits and their non-zero defaults for administrators.
--- windows/virt2phys/README.md | 38 +++++++++++++++ windows/virt2phys/virt2phys.c | 73 ++++++++++++++++++++++++++++- windows/virt2phys/virt2phys_logic.c | 46 +++++++++++++----- windows/virt2phys/virt2phys_logic.h | 9 +++- 4 files changed, 152 insertions(+), 14 deletions(-) create mode 100644 windows/virt2phys/README.md
windows/virt2phys/README.md new file +38 -0
@@ -0,0 +1,38 @@ +Virtual to Physical Address Translator +====================================== + +Purpose and Operation +--------------------- + +``virt2phys`` driver allows user-mode processes to obtain physical address +of a given virtual address in their address space. +Virtual addresses must belong to regions from process private working set. +These regions must be physically contiguous. +The driver ensures that memory regions with translated addresses +are not swapped out as long as the process has access to this memory. + +It is not safe to administratively unload the driver +while there are processes that have used virt2phys to translate addresses. +Doing so will permanently leak RAM occupied by all memory regions +that contain translated addresses. +Terminate all such processes before unloading the driver. + +Configuration +------------- + +``virt2phys`` is configured at loading time via registry key +``HKLM\SYSTEM\ControlSet001\Services\virt2phys\Parameters``. + +* ``ProcessCountLimit`` (default 16) + + Maximum number of processes that can have access to memory regions + with translated addresses. When this limit is reached, no more processes + can translate addresses using ``virt2phys``. Large number of tracked + processes may slow down system operation. Set limit to 0 to disable it. + +* ``ProcessMemoryLimitMB`` (default 16384, i.e. 16 GB) + + Maximum amount of memory in all regions that contain translated addresses, + total per process. When this limit is reached, the process can not translate + addresses from new regions. Large values can cause RAM exhaustion. + Set limit to 0 to disable it.
windows/virt2phys/virt2phys.c +71 -2
103 lines not on this page · raw message
windows/virt2phys/virt2phys_logic.c +35 -11
103 lines not on this page · raw message
windows/virt2phys/virt2phys_logic.h +8 -1
@@ -5,10 +5,17 @@#ifndef VIRT2PHYS_LOGIC_H #define VIRT2PHYS_LOGIC_H +struct virt2phys_params { +/** Maximum number of tracked processes (0 = unlimited). */ +ULONG process_count_limit; +/** Maximum amount of memory locked by a process (0 = unlimited). */ +ULONG64 process_memory_limit; +}; +/** * Initialize internal data structures. */ -NTSTATUS virt2phys_init (void ); +NTSTATUS virt2phys_init (const struct virt2phys_params *params);/** * Free memory allocated for internal data structures.
Signature · 1 line
2.29.3
PATCH v3 3/3 windows/virt2phys: add tracing
In reply to Dmitry KozlyukWPP tracing [1] allows kernel drivers to print logs that can be viewed without attaching a debugger to the running system. Traces are colelcted only when enabled. Instrument virt2phys with traces:
* ERROR: failures that prevent the driver from working.
* WARNING: incorrect calls to the driver.
* INFO: starting or completing operations with memory.
[1]: https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/wpp-software-tracing
--- windows/virt2phys/virt2phys.c | 18 +++++++- windows/virt2phys/virt2phys.vcxproj | 9 ++-- windows/virt2phys/virt2phys.vcxproj.filters | 3 ++ windows/virt2phys/virt2phys_logic.c | 35 ++++++++++++--- windows/virt2phys/virt2phys_trace.h | 50 +++++++++++++++++++++ 5 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 windows/virt2phys/virt2phys_trace.h
windows/virt2phys/virt2phys.c +16 -2
@@ -8,6 +8,8 @@#include "virt2phys.h" #include "virt2phys_logic.h" +#include "virt2phys_trace.h" +#include "virt2phys.tmh" DRIVER_INITIALIZE DriverEntry;EVT_WDF_DRIVER_UNLOAD virt2phys_driver_unload;
@@ -66,6 +68,8 @@ DriverEntry(PDRIVER_OBJECT driver_object, PUNICODE_STRING registry_path)if (!NT_SUCCESS (status))return status; +WPP_INIT_TRACING (driver_object, registry_path); +return status; }
@@ -131,11 +135,11 @@ _Use_decl_annotations_VOID virt2phys_driver_unload (WDFDRIVER driver) { -UNREFERENCED_PARAMETER (driver); -PsSetCreateProcessNotifyRoutine (virt2phys_on_process_event, TRUE);virt2phys_cleanup (); + +WPP_CLEANUP (WdfDriverWdmGetDriverObject (driver)); } _Use_decl_annotations_
@@ -157,12 +161,15 @@ virt2phys_driver_EvtDeviceAdd(WDFDRIVER driver, PWDFDEVICE_INIT init) status =WdfDeviceCreate (&init, &attributes, &device);if (!NT_SUCCESS (status)) { +TraceError ("WdfDriverCreate() = %!STATUS!" , status);return status; } status =WdfDeviceCreateDeviceInterface ( device, &GUID_DEVINTERFACE_VIRT2PHYS ,NULL );if (!NT_SUCCESS (status)) { +TraceError ("WdfDeviceCreateDeviceInterface() = %!STATUS!" , + status);return status; }
@@ -187,12 +194,14 @@ virt2phys_device_EvtIoInCallerContext(WDFDEVICE device, WDFREQUEST request)WdfRequestGetParameters (request, ¶ms);if (params.Type != WdfRequestTypeDeviceControl) { +TraceWarning ("Bogus IO request type %lu" , params.Type);WdfRequestComplete (request,STATUS_NOT_SUPPORTED );return ; } code = params.Parameters.DeviceIoControl.IoControlCode;if (code !=IOCTL_VIRT2PHYS_TRANSLATE ) { +TraceWarning ("Bogus IO control code %lx" , code);WdfRequestComplete (request,STATUS_NOT_SUPPORTED );return ; }
@@ -200,6 +209,7 @@ virt2phys_device_EvtIoInCallerContext(WDFDEVICE device, WDFREQUEST request) status =WdfRequestRetrieveInputBuffer ( request,sizeof (*virt), (PVOID *)&virt, &size);if (!NT_SUCCESS (status)) { +TraceWarning ("Retrieving input buffer: %!STATUS!" , status);WdfRequestComplete (request, status);return ; }
@@ -207,6 +217,7 @@ virt2phys_device_EvtIoInCallerContext(WDFDEVICE device, WDFREQUEST request) status =WdfRequestRetrieveOutputBuffer ( request,sizeof (*phys), (PVOID *)&phys, &size);if (!NT_SUCCESS (status)) { +TraceWarning ("Retrieving output buffer: %!STATUS!" , status);WdfRequestComplete (request, status);return ; }
@@ -214,6 +225,9 @@ virt2phys_device_EvtIoInCallerContext(WDFDEVICE device, WDFREQUEST request) status =virt2phys_translate (*virt, phys);if (NT_SUCCESS (status))WdfRequestSetInformation (request,sizeof (*phys)); + +TraceInfo ("Translate %p to %llx: %!STATUS!" , + virt, phys->QuadPart, status);WdfRequestComplete (request, status); }
windows/virt2phys/virt2phys.vcxproj +5 -4
@@ -17,6 +17,7 @@ <ItemGroup> <ClInclude Include="virt2phys.h" /> <ClInclude Include="virt2phys_logic.h" /> + <ClInclude Include="virt2phys_trace.h" /> </ItemGroup> <ItemGroup> <Inf Include="virt2phys.inf" />
@@ -63,9 +64,9 @@ </PropertyGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ClCompile> - <WppEnabled>false</WppEnabled> + <WppEnabled>true</WppEnabled> <WppRecorderEnabled>true</WppRecorderEnabled> - <WppScanConfigurationData Condition="'%(ClCompile.ScanConfigurationData)' == ''">trace.h</WppScanConfigurationData> + <WppScanConfigurationData Condition="'%(ClCompile.ScanConfigurationData)' == ''">virt2phys_trace.h</WppScanConfigurationData> <WppKernelMode>true</WppKernelMode> </ClCompile> <Link>
@@ -80,9 +81,9 @@ </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ClCompile> - <WppEnabled>false</WppEnabled> + <WppEnabled>true</WppEnabled> <WppRecorderEnabled>true</WppRecorderEnabled> - <WppScanConfigurationData Condition="'%(ClCompile.ScanConfigurationData)' == ''">trace.h</WppScanConfigurationData> + <WppScanConfigurationData Condition="'%(ClCompile.ScanConfigurationData)' == ''">virt2phys_trace.h</WppScanConfigurationData> <WppKernelMode>true</WppKernelMode> </ClCompile> <DriverSign>
windows/virt2phys/virt2phys.vcxproj.filters +3 -0
@@ -30,6 +30,9 @@ <ClInclude Include="virt2phys_logic.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="virt2phys_trace.h"> + <Filter>Header Files</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="virt2phys.c">
windows/virt2phys/virt2phys_logic.c +29 -6
@@ -6,6 +6,8 @@#include <ntddk.h> #include "virt2phys_logic.h" +#include "virt2phys_trace.h" +#include "virt2phys_logic.tmh" struct virt2phys_process {HANDLE id;
@@ -38,6 +40,8 @@ virt2phys_block_create(PMDL mdl)static void virt2phys_block_free (struct virt2phys_block *block,BOOLEAN unmap) { +TraceInfo ("VA = %p, unmap = %!bool!" , block->mdl->StartVa, unmap); +if (unmap)MmUnlockPages (block->mdl);
@@ -76,6 +80,8 @@ virt2phys_process_free(struct virt2phys_process *process, BOOLEAN unmap)PSINGLE_LIST_ENTRY node;struct virt2phys_block *block; +TraceInfo ("ID = %p, unmap = %!bool!" , process->id, unmap); + node = process->blocks.Next;while (node !=NULL ) { block = CONTAINING_RECORD(node, struct virt2phys_block, next);
@@ -208,6 +214,8 @@ virt2phys_add_block(struct virt2phys_process *process,struct virt2phys_process *existing;size_t size; +TraceInfo ("ID = %p, VA = %p" , process->id, block->mdl->StartVa); + existing =virt2phys_process_find (process->id); *process_exists = existing !=NULL ;if (existing ==NULL ) {
@@ -217,8 +225,11 @@ virt2phys_add_block(struct virt2phys_process *process,* because decrement is done without holding the lock. */ if (virt2phys_exceeeds (g_process_count +1 , - g_params.process_count_limit)) + g_params.process_count_limit)) { +TraceWarning ("Process count limit reached (%lu)" , + g_params.process_count_limit);return STATUS_QUOTA_EXCEEDED ; + }InsertHeadList (&g_processes, &process->next);InterlockedIncrement (&g_process_count);
@@ -227,8 +238,11 @@ virt2phys_add_block(struct virt2phys_process *process, size =MmGetMdlByteCount (block->mdl);if (virt2phys_exceeeds (process->memory + size, - g_params.process_memory_limit)) + g_params.process_memory_limit)) { +TraceWarning ("Process %p memory limit reached (%llu bytes)" , + process->id, g_params.process_memory_limit);return STATUS_QUOTA_EXCEEDED ; + }PushEntryList (&process->blocks, &block->next); process->memory += size;
@@ -277,8 +291,10 @@ virt2phys_check_memory(PMDL mdl)size_t size;NTSTATUS status; -if (!virt2phys_is_contiguous (mdl)) +if (!virt2phys_is_contiguous (mdl)) { +TraceWarning ("Locked region is not physycally contiguous" );return STATUS_UNSUCCESSFUL ; + } virt =MmGetMdlVirtualAddress (mdl); size =MmGetMdlByteCount (mdl);
@@ -288,12 +304,19 @@ virt2phys_check_memory(PMDL mdl)if (!NT_SUCCESS (status))return status; -if (info.AllocationBase != virt || info.RegionSize != size) +if (info.AllocationBase != virt || info.RegionSize != size) { +TraceWarning ("Race for the region: supplied %p (%llu bytes), locked %p (%llu bytes)" , + virt, size, info.AllocationBase, info.RegionSize);return STATUS_UNSUCCESSFUL ; -if (info.State !=MEM_COMMIT ) + } +if (info.State !=MEM_COMMIT ) { +TraceWarning ("Attempt to lock uncommitted memory" );return STATUS_UNSUCCESSFUL ; -if (info.Type !=MEM_PRIVATE ) + } +if (info.Type !=MEM_PRIVATE ) { +TraceWarning ("Attempt to lock shared memory" );return STATUS_UNSUCCESSFUL ; + }return status; }
windows/virt2phys/virt2phys_trace.h new file +50 -0
50 lines not on this page · raw message
Signature · 1 line
2.29.3
12/10/2021 02:42, Dmitry Kozlyuk:
Quoted 15 lines
v3:
* Fix Release build (Ranjit).
* Drop PnpLockdown=1 patch as it is now in dependency series.
v2:
* Following ofline review by DmitryM:
- Add comment explaining tracking approach for validation team.
- Replace deprecated allocation API calls.
- Check properties of locked memory (see docs in patch 3/4).
- Add configurable limits for tracked processes and memory.
* Add end-user documentation.
* Drop patch for Inf2Cat settings UseLocalTime=true:
the issue it resolves originated from development VM.
* Add PnpLockdown=1 patch.
Quoted 4 lines
Dmitry Kozlyuk (3):
windows/virt2phys: do not expose pageable physical addresses windows/virt2phys: add limits against resource exhaustion windows/virt2phys: add tracing
I suppose we will never have a review from Microsoft, so Applied, thanks.
Quoted 25 lines · Dmitry Kozlyuk
Physical addresses exposed by virt2phys driver could become pageable. This presents stability and security issues that prevent Microsoft from signing virt2phys, because a signed driver would be trusted by all end-user machines.
Ensure that memory for which physical addresses are exposed by virt2phys is non-pageable at least for the lifetime of the process. As virt2phys code grows, make its development and debugging easier.
There are other known issues that come from using PA and accessing DMA from userspace. They are not related to virt2phys par se. It is planned to address them later by enabling the use of IOMMU for DPDK on Windows.
Dmitry Kozlyuk (3):
windows/virt2phys: use local time for signing windows/virt2phys: do not expose pageable physical addresses windows/virt2phys: add tracingwindows/virt2phys/virt2phys.c | 89 ++++-- windows/virt2phys/virt2phys.vcxproj | 8 +- windows/virt2phys/virt2phys.vcxproj.filters | 11 +- windows/virt2phys/virt2phys_logic.c | 312 ++++++++++++++++++++ windows/virt2phys/virt2phys_logic.h | 32 ++ windows/virt2phys/virt2phys_trace.h | 49 +++ 6 files changed, 472 insertions(+), 29 deletions(-)
Quoted 3 lines
create mode 100644
windows/virt2phys/virt2phys_logic.ccreate mode 100644windows/virt2phys/virt2phys_logic.hcreate mode 100644windows/virt2phys/virt2phys_trace.h