Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update Fail_Driver_Pool_Leak Driver's Class in INX and Enhance README Documentation #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions tools/dv/kmdf/fail_driver_pool_leak/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
# Fail_Driver_Pool_Leak Sample
# Fail_Driver_Pool_Leak Sample Driver

This sample KMDF Fail Driver demonstrates the capabilities and features of **Driver Verifier** and the **Device Fundamentals Tests**.
This sample KMDF Fail Driver is designed to demonstrate the capabilities and features of **Driver Verifier** and the **Device Fundamentals Tests** for a driver written in Rust.

It allocates a pool of memory to a global buffer when a supported device is added by the PnP Manager and intentionally does not free it before the driver is unloaded. This memory leak fault is a system vulnerability that could lead to security and performance issues and bad user experience.
The driver allocates a pool of memory to a global buffer in its `evt_driver_device_add` function when a supported device is added by the PnP Manager. The driver intentionally does not free it anywhere, even in the `evt_driver_unload` callback, which get called before the driver is unloaded. This memory leak fault is a system vulnerability that could lead to security and performance issues and ultimately a bad user experience.

By enabling Driver Verifier on this driver, this pool leak violation can be caught before the driver is unloaded and with an active KDNET session, the bug can be analyzed further.
By enabling Driver Verifier on a driver, such violations can be caught and mitigated early in the development cycle. With an active KDNET session, the bugcheck can be analyzed further using WinDbg. The steps listed below demonstrate how to build, deploy, enable Driver Verifier, run Device Fundamentals Tests and debug the driver.

NOTE: The driver uses WDM's ExAllocatePool2 API directly to allocate memory for its buffer. Ideally, such allocations should be freed by using ExFreePool API. A cleaner way to manage memory in a WDF Driver is to use [wdfmemory](https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdfmemory/)


## Steps to reproduce the issue

1. Clone the repository and navigate to the project root.

2. Build the driver project using the following command in a WDK environment (or EWDK prompt) -
2. Install [Clang](https://clang.llvm.org/get_started.html)
* Easy install option:
```
winget install LLVM.LLVM
```

3. Build the driver project using the following command in an [EWDK environment](https://learn.microsoft.com/en-us/legal/windows/hardware/enterprise-wdk-license-2022) -
```
cargo make
```
3. Prepare a target system (a Hyper-V VM can be used) for testing
4. Prepare a target system (a Hyper-V VM can be used) for testing

Follow the below steps to setup the test system -
1. Disable Secure boot and start the system
Expand All @@ -37,11 +42,11 @@ NOTE: The driver uses WDM's ExAllocatePool2 API directly to allocate memory for
shutdown -r -t 0
```

4. Copy the driver package, available under ".\target\debug\fail_driver_pool_leak_package" to the target system.
5. Copy the driver package, available under ".\target\debug\fail_driver_pool_leak_package" to the target system.

5. Copy "devgen.exe" from host to the target system. Alternatively you may install WDK on the target system and add the directory that contains "devgen.exe" to PATH variable.
6. Copy "devgen.exe" from host to the target system. Alternatively you may install WDK on the target system and add the directory that contains "devgen.exe" to PATH variable.

6. Install the driver package and create the device in the target system using the below commands -
7. Install the driver package and create the device in the target system using the below commands -
```
cd "fail_driver_pool_leak_package"
devgen.exe /add /bus ROOT /hardwareid "fail_driver_pool_leak"
Expand All @@ -50,7 +55,7 @@ NOTE: The driver uses WDM's ExAllocatePool2 API directly to allocate memory for

pnputil.exe /add-driver .\fail_driver_pool_leak.inf /install
```
7. Enable Driver Verifier for 'fail_driver_pool_leak.sys' driver package
8. Enable Driver Verifier for 'fail_driver_pool_leak.sys' driver package
1. Open run command prompt (Start + R) or cmd as administator and run "verifier"
2. In the verifier manager,
- Create Standard Settings
Expand All @@ -59,23 +64,23 @@ NOTE: The driver uses WDM's ExAllocatePool2 API directly to allocate memory for
- Finish
- Restart the system

8. Follow the steps in https://learn.microsoft.com/en-us/windows-hardware/drivers/develop/how-to-test-a-driver-at-runtime-from-a-command-prompt to run tests against the device managed by this driver
9. Follow the steps in https://learn.microsoft.com/en-us/windows-hardware/drivers/develop/how-to-test-a-driver-at-runtime-from-a-command-prompt to run tests against the device managed by this driver

9. Install TAEF and WDTF on the test computer and run the following test -
10. Install TAEF and WDTF on the test computer and run the following test -
```
cd "C:\Program Files (x86)\Windows Kits\10\Testing\Tests\Additional Tests\x64\DevFund"
TE.exe .\Devfund_PnPDTest_WLK_Certification.dll /P:"DQ=DeviceID='ROOT\DEVGEN\{PASTE-DEVICE-ID-HERE}'" --rebootResumeOption:Manual
```

10. The test will lead to a Bugcheck and a BlueScreen on the target system with the following error -
11. The test will lead to a Bugcheck and a BlueScreen on the target system with the following error -
```
DRIVER_VERIFIER_DETECTED_VIOLATION (c4)
```
The logs will be available in WinDbg
run ```!analyze -v``` for detailed bugcheck report
run ```!verifier 3 fail_driver_pool_leak.sys``` for info on the allocations that were leaked that caused the bugcheck.
```
Run ```!analyze -v``` for detailed bugcheck report

Run ```!verifier 3 fail_driver_pool_leak.sys``` for info on the allocations that were leaked that caused the bugcheck.

11. (Alternatively), the bugcheck can be observed when all the devices managed by this driver are removed, i.e, when the driver is unloaded from the system.
12. (Alternatively), the bugcheck can be observed when all the devices managed by this driver are removed, i.e, when the driver is unloaded from the system.
You may use pnputil/devcon to enumerate and remove the devices -
```
# To enumerate the devices
Expand Down
4 changes: 2 additions & 2 deletions tools/dv/kmdf/fail_driver_pool_leak/fail_driver_pool_leak.inx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

[Version]
Signature = "$WINDOWS NT$"
Class = SoftwareComponent
ClassGuid = {5c4c3332-344d-483c-8739-259e934c9cc8}
Class = Sample
ClassGuid = {78A1C341-4539-11d3-B88D-00C04FAD5171}
Provider = %ProviderString%
PnpLockDown = 1

Expand Down
Loading