Post

Learning from CmUpx.sys

Analyzing Vulnerable Driver "CmUpx.sys"

Learning from CmUpx.sys

Disclaimer: This blog is merely to demonstrate my learning process of this vulnerability. This is not an original discovery of the vulnerability nor an original exploit of the vulnerability. All references (other blog posts, PoCs) are listed at the end of this blog.

Introduction

This is a continuation of my learning blog series, where I try to analyze known vulnerable Windows drivers that can be found on https://www.loldrivers.io/. Similar to last time, I’m choosing a vulnerable driver that does not have any write ups at the time of this writing (that I’m aware of). However, unlike my last blog post, this time there is not even a hint in the Github Issue page. It seems like the original public repository contained a list of vulnerable drivers but is now changed to private or removed. This is a good opportunity for me to practice on analyzing a vulnerable driver with pretty much no guidance and creating a PoC for it.

This blog will cover:

  • My methodology to reverse engineer and analyze the vulnerable driver.
  • My own conclusion based on the analysis.

This blog does not cover:

  • Setting up the reverse engineering and debugging environment.
  • A PoC that abuses the vulnerable driver (due to limitations discussed in this blog).

The vulnerable driver sample can be found in the link below. https://www.loldrivers.io/drivers/64601d77-61ea-43b7-8178-9d45dfba6022/

Looking at Vulnerable IOCTLs

First we will need to identify the device name and the IOCTL Handler. The screenshot below shows that both of these can be identified directly at the entry point.

Screenshot

Following the IOCTL handler function (at CmUpx.sys+0x5000), we can see the code blocks that are checking the IOCTL code. Screenshot

Among these available IOCTL codes, the following 2 codes are particularly of interest:

  • 0x220058
  • 0x22005c

Reason being that the code blocks of these 2 IOCTL codes call multiple functions related to memory mapping such as MmMapIoSpace, IoAllocateMdl, MmBuildMdlForNonPagedPool and MmMapLockedPagesSpecifyCache. So at first glance, these seem to be the IOCTL codes that leads to the vulnerable code block.

Screenshot

Reaching the IOCTL codes

Before we dive deep into these code blocks, we will first create a simply program to reach the IOCTL code blocks and use WinDbg to aid on dynamic reverse engineering. This makes the reverse engineering process a bit easier and we can slowly build the proof of concept exploit along the way.

We can start with the following code. Since we don’t know what each of the IOCTL code at the moment, we’ll just simply assume that they are for reading and writing physical memory.

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
#include <windows.h>
#include <stdio.h>

// Placeholders. We don't know what each IOCTL code does yet.
#define IOCTL_READ_MEMORY 0x220058
#define IOCTL_WRITE_MEMORY 0x22005C

// structure of the input buffer
// we will figure this out on the way
typedef struct {
	DWORD placeholder;
} TESTPAYLOAD;

int wmain (int argc, wchar_t* argv[]){

	HANDLE hDevice;
	
	char inputBuffer[8] = { 0 };
	char outputBuffer[8] = { 0 };
	DWORD bytesReturned = 0;

	// Stage 1: Getting driver handle
	wchar_t* deviceName = L"\\\\.\\CmUpx";

	hDevice = CreateFileW(
		deviceName,
		GENERIC_READ | GENERIC_WRITE,
		0,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL
	);

	if (hDevice == INVALID_HANDLE_VALUE){
		printf("[-] Unable to get driver handle.\n");
		return -1;
	}

	// Stage 2: Accessing IOCTL 0x220058
	
	// Filling input buffer with 0xA
	for (int i = 0; i < sizeof(inputBuffer); i++){
		inputBuffer[i] = 0xAA;
	}
	
	DeviceIoControl(
		hDevice,
		IOCTL_READ_MEMORY,
		&inputBuffer,
		sizeof(inputBuffer),
		outputBuffer,
		sizeof(outputBuffer),
		&bytesReturned,
		NULL
	);
	
cleanup:
	CloseHandle(hDevice);
	return 0;
}

In WinDbg, we’ll set a breakpoint at CmUpx+0x507b to confirm that we can reach the IOCTL 0x220058 code block.

Screenshot

Identifying Conditions and Payload Structure

Now we will step through the code. After checking the IOCTL code, the driver checks if the input buffer size is larger than 0x17. Screenshot

In WinDbg we can see the comparison. Screenshot

So we will need to update inpuBuffer to be at least 0x18 (24) bytes long.

After the input size check, we will see that it’s copying some values from the input buffer. Here rdi is a pointer to out input buffer. We can dump the pointer to confirm that it contains a number of 0xA bytes initialized in our PoC code.

Screenshot

The value at [rdi+10] is later used in the MmMapIoSpace call as the NumberOfBytes argument. So we can assume that the value at inputBuffer+0x10 will determine how many bytes the driver should read from the physical address.

Screenshot

We can also start mapping out the structure required for the input buffer:

1
2
3
4
5
6
// Input buffer structure
typedef struct {
	DWORD placeholder[4];		// + 0x00
	DWORD memSize;				// + 0x10
	DWORD placeholder2;			// + 0x14
} TESTPAYLOAD;

We can test by assigning the value 0xBBBBBBBB to the memSize of the structure. We will update the PoC code as shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// ...
TESTPAYLOAD inputBuffer = { 0 };

// ...
// Stage 2: Accessing IOCTL 0x220058
	
	// Filling input buffer with 0xA
	for (int i = 0; i < 4; i++){
		inputBuffer.placeholder[i] = 0xAAAAAAAA;
	}
	
	inputBuffer.memSize = 0xBBBBBBBB; // Test value
	inputBuffer.placeholder2 = 0xAAAAAAAA;
	
	
	DeviceIoControl(
	// ...

And in WinDbg we can see that the value of 0xBBBBBBBB at [rdi+0x10]. As mentioned above, this value will decide the memory size mapped using MmMapIoSpace. Screenshot

If we continued down the disassemble code, we can see that it’s also getting the physical address from the input buffer. Based on the disassembly code, the physical address will be at [rdi+0x8], which is positioned right before the memSize of the payload structure. Screenshot

So we will update the payload structure and the PoC code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// ...
// Input buffer structure
typedef struct {
	DWORD placeholder[2];		// + 0x00
	DWORD physicalAddr;			// + 0x08
	DWORD placeholder2;			// + 0x09
	DWORD memSize;				// + 0x10
	DWORD placeholder3;			// + 0x14
} TESTPAYLOAD;

// ...
// Stage 2: Accessing IOCTL 0x220058
	
	// Filling input buffer with 0xA
	for (int i = 0; i < 4; i++){
		inputBuffer.placeholder[i] = 0xAAAAAAAA;
	}
	
	inputBuffer.physicalAddr = 0xCCCCCCCC;
	inputBuffer.memSize = 0xBBBBBBBB;
	inputBuffer.placeholder2 = 0xAAAAAAAA;
	inputBuffer.placeholder3 = 0xAAAAAAAA;

// ...

We can double check by settings a breakpoint at cmupx+0x51af to confirm. Screenshot

If we continue stepping over the assembly code, we will reach the call to MmMapIoSpace. Here we can see that the 2 values we’ve setup above are used as the arguments for MmMapIoSpace. This means that we can ask the driver to map arbitrary physical memory. Screenshot

However, we can see a limitation for the physical address provided. Since the mov ecx, dword ptr [rdi+8] instruction only copies 4 bytes to ecx (aka lower 4 bytes of rcx), it means that the physical address can only be a 32-bit address. The upper 4 bytes of rcx is always set to 0.

Testing Physical Memory Mapping

If we try to step over the MmMapIoSpace function call, we will not be able to proceed since we provided a dummy physical address and MmMapIoSpace will return an error. So from this point, we will need to find a valid physical address on the OS running the vulnerable driver. We can use the RamMap tool from Microsoft’s Sysinternal Suite to get a list of physical address pages along with the corresponding state of these pages.

An example of the RamMap interface is shown below. Since we are still in the process of figuring out what the vulnerable driver functions actually do, it is recommended to choose a physical address range that is not being used (as shown below) to reduce the frequency of our PoC crashing the OS and requiring to restore a snapshot. Screenshot

Here we will update the payload values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// ...
// Filling input buffer with 0xA
	for (int i = 0; i < 2; i++){
		inputBuffer.placeholder[i] = 0xAAAAAAAA;
	}
	
	//inputBuffer.physicalAddr = 0xCCCCCCCC;
	//inputBuffer.memSize = 0xBBBBBBBB;
	inputBuffer.physicalAddr = 0xe0d6000; 		// test physical address (unused)
	inputBuffer.memSize = 0x100; 				// test size
	inputBuffer.placeholder2 = 0xAAAAAAAA;
	inputBuffer.placeholder3 = 0xAAAAAAAA;

// ...

We will set a breakpoint at the MmMapIoSpace call cmupx+0x51b8 and step over it to ensure that MmMapIoSpace returns a virtual address mapped to the target physical address. Screenshot

Following the MmMapIoSpace call, the driver calls multiple functions related to memory descriptor list (MDL). An MDL describes the physical page layout of a virtual memory buffer. This is used because an I/O buffer across multiple virtual memory page (contiguous) can be spread across multiple physical pages (could be discontiguous). I highly recommend reading this blog post by WaterBucket which explains MDL in more details.

We can actually get a fairly good idea of what the driver is trying to achieve based on these function calls:

  • IoAllocateMdl
    • Allocates a MDL for the virtual address being mapped (which is a virtual address mapped to the user-supplied physical address)
  • MmBuildMdlForNonPagedPool
    • Updates the MDL to describe the underlying physical pages
  • MmMapLockedPagesSpecifyCache
    • Maps the described physical pages to a virtual address

So basically, the final output after calling MmMapLockedPagesSpecifyCache is still a virtual address mapped to the physical address specified in the input buffer. This obviously makes you think why do we have to go through all of the MDL steps when we already have a virtual address from MmMapIoSpace. From what I can tell, this sequence usually happens when the driver tries to map a physical address to a user-mode buffer. The virtual address returned by MmMapIoSpace is in kernel space and cannot be access by user-mode applications. By using the MDL sequence above, MmMapLockedPagesSpecifyCache can map the physical page to a user-mode virtual address by setting AccessMode to UserMode (1).

However, this driver sample actually sets the AccessMode to KernelMode (0), which kind of yields the same result as using the virtual address returned by MmMapIoSpace directly. After some more Googling, it seems that the main reason could be to ensure context-safe kernel access of the buffer. (TODO: do more research on this).

But for analyzing this vulnerable driver, we just need to know that a virtual address mapped to our provided physical address is generated after calling MmMapLockedPagesSpecifyCache.

Custom Memory Copy?

Then if MmMapLockedPagesSpecifyCache returns a non-zero virtual address, the driver copies that address into another variable (named pBuffer2 here). It also copies a pointer to the input buffer at offset 0x14 (named pBuffer here).

Screenshot

Following the code flow to cmupx+0x511f, we see a function call (FUN_140001a00) using the 2 addresses above along with a third variable as argument. Backtracking the third argument, we can see that it’s just a copy of the buffer size we specified in our input buffer. Screenshot

Even though we can try to inspect and understand the function, we can probably already tell what this function is meant for. Just like my comment in the screenshot above, my initial theory is that this function probably does what a regular memcpy does (not a wrapper though since it does not actually call memcpy). This is nothing more than an educated guess, simply based on how the 3 arguments resemble that of a memcpy function call (dst,src,size).

So to verify this, we can set a breakpoint at cmupx+0x5209 (calling MmMapLockedPagesSpecifyCache) at see what happens. Firstly the MmMapLockedPagesSpecifyCache call should return a virtual address pointing to the physical address we specified. Screenshot

Then, we can see that a pointer to the input buffer is moved to rdx. Screenshot

We keep stepping over until we reach the function call at cmupx+0x5122. We can see that rcx is a pointer to the mapped physical memory, and rdx is a pointer to our input buffer. Screenshot

Now if we step over the function, we can inspect the physical memory again and see that it’s now filled with the same content as the input buffer (start from offset 0x14). Screenshot

And now we can conclude that this is indeed a custom memcpy function with the same argument structure as shown below.

1
2
// Pseudo function call arguments
void CustomMemcpy(VOID *dst, VOID *src, size_t size);

The updated payload structure will look like this. Note that since we have control over how many bytes would be copied in the custom memcpy function, the size of buffer can be arbitrary but must be at least 4 bytes long in order to reach the vulnerable code.

1
2
3
4
5
6
7
8
// Input buffer structure
typedef struct {
	DWORD placeholder[2];		// + 0x00
	DWORD physicalAddr;			// + 0x08
	DWORD placeholder2;			// + 0x09
	DWORD memSize;				// + 0x10
	DWORD buffer;				// + 0x14
} TESTPAYLOAD;

The other IOCTL code

Initially we assumed that the IOCTL code 0x220058 corresponds to the memory read operation, but it turns out to be a memory write operation instead. So now we will look at the 0x22005c, which perform a physical memory read.

The code for both operations are largely similar. But from the screenshot below, we can see that the memory read IOCTL checks for an additional third condition. Screenshot

From our analysis in the earlier sections, we know that *(pInputBuffer + 2) points to the memSize member of our TESTPAYLOAD structure, which defines the size of the custom memcpy operation. The IF statement here is checking if this value is smaller than uVar1. This variable can be backtracked to the start of the IOCTL handler function where it’s value is taken from the I/O Request Packet (IRP) it received. Screenshot

If we set a breakpoint at cmupx+0x5083, we can see what value uVar1 is being assigned. Screenshot

In our PoC code, the output buffer size is the only argument that we passed to DeviceIoControl that contains a value of 8. We can try to change the size of the output buffer to 9 and see if this value changes.

1
2
3
4
// ...
// Simply change the array size to 9
char outputBuffer[9] = { 0 };
// ...

Screenshot

And this confirms that it’s ensuring the memSize value is not larger than the output buffer size so that the custom memcpy operation won’t overflow the buffer.

The memory mapping operation is the same as the write IOCTL code pattern. The only difference here is that the user buffer is set as the destination of the memcpy operation and the mapped physical memory is the source. This fits the assumption that this IOCTL code will attempt to read contents from the user-provided physical address. Screenshot

As the input buffer is the destination address, the content stored in the input buffer will be overwritten by the custom memcpy function.

Caveat

While this looks like an abusable function for arbitrary kernel memory read, note that the value is stored in pVirtualAddr2 which is an address in the kernel space. Looking back at the IOCTL codes, both of these operations uses METHOD_BUFFERED to transfer data from user land to kernel land. This means that the user input buffer is copied into a buffer in the kernel memory. So the read operation above copies data to the kernel-version of the user buffer, but not the actual buffer that the user application (our PoC code) has created. So in the end, the data read from the specified physical address is not returned to the user and therefore not really an arbitrary read operation that can be abused.

Abusing the Vulnerable Driver (PoC)

As mentioned in earlier sections, there is a critical limitation to abuse the arbitrary kernel memory write where the target physical address cannot be larger than 0xFFFFFFFF. Based on my own experiment, most of the EPROCESS structures are mapped to a physical address that exceeds this limitation (physical addresses were obtained using this Superfetch code). Therefore, I was not able to create a working PoC to disable arbitrary process protection.

Conclusion

Here we will conclude what we have learned from this vulnerable driver. First off, the 2 vulnerable IOCTL codes are:

  • 0x220058 (Write to arbitrary physical memory)
  • 0x22005c (Read from arbitrary physical memory)

For the write operation, the conditions are listed below:

  • inpuBuffer to be at least 0x18 (24) bytes long.
  • The upper 4 bytes of the physical address must be 0
    • i.e. the effective physical memory range is from 0x0 to 0xFFFFFFFF on a 64-bit system.

And the input buffer structure is listed below, where buffer must be at least 4 bytes long (but can be longer).

1
2
3
4
5
6
7
8
// Input buffer structure
typedef struct {
	DWORD placeholder[2];		// + 0x00
	DWORD physicalAddr;			// + 0x08, Target physical address
	DWORD placeholder2;			// + 0x09
	DWORD memSize;				// + 0x10, Size of the write operation
	DWORD buffer;				// + 0x14, Content to write to physical address)
} TESTPAYLOAD;

For the read operation, the conditions are listed below:

  • inpuBuffer to be at least 0x18 (24) bytes long.
  • the memSize value specified in the input buffer structure cannot be larger than the output buffer size.

However, from our analysis, the kernel memory read operation does not return the value read back to the user application. This leaves only the kernel memory write operation (with IOCTL code 0x220058) to be abusable, provided that you are able to obtain a physical address that does not exceed the value of 0xFFFFFFFF. This proves that the abusability of this driver is very limited.

Reference

Windows Functions

MmMapIoSpace

IoAllocateMdl

MmBuildMdlFromNonPagedPool

Others

Understanding Memory Descriptor Lists (MDLs) for Windows Vulnerability Research & Exploit Development

This post is licensed under CC BY 4.0 by the author.