Hello everyone, welcome to VMware Device Visualization Backhunting. We are excited to share our talk with you and also feel a little disappointed for we cannot arrive DEF CON in person. And we do hope our talk will serve as a guide for anyone who's looking to start VMware security research. Firstly, allow me to introduce us to you. My name is Jiaxin Huang and Hao Zheng and me are both security researchers of Tenggong team at Xianxin Group. And we are both interested in reverse engineering and visualization security. Yueliu is our team leader. And our team consists of lots of people interested in viral security domain. And we have a website to post our blog on it. So if you are interested in it, make sure to follow us. And if you have any questions, feel free to DM or email us. Here is the short story. Let's describe our visualization backhunting journey. We actually started to research visualization security from the end of 2022. And in that time, we basically know nothing about visualization. So why are we interested in it? Because visualization is involved in lots of hacking contests. And it's a very hard, very challenging target. And for myself, I want to reverse engineer it and learn how it works. So what have we achieved in last year? We successfully escaped from the paralyzed desktop on GeekCon 2023. And we reported lots of workstation EXXI bugs to VMware. And we learned a lot, not just about visualization, but about the whole architecture of the entire computer. So what can we bring to you today? Firstly, we will introduce the VMware visualization architecture to you. Most people hunting bugs directly from debugging the visual device is code. But we choose to take a bigger view. We studied it from reverse engineering the whole VMware visualization architecture. And this helped us to figure out which code and how they can be influenced by the guest OS. And hopefully, it will bring some new idea to you. And later, we will use the bugs that we found in USB visualization and SCSI visualization to explain the possible source of 3D in visual device. And anyway, a visual device is still the most cost-effective choice in visualization bug hunting. Okay, let's start the first part. VMware Hypervisor Reverse Engineering. If you are new to VMware visualization, I do have some tips for you. The most important binary on VMware Workstation or EXXI is VMX. It is responsible for starting the visual machine and contains most of visual device code. And no matter it is EXXI or Workstation, VMX is identical and basically just use different system API. And in this part, we will use Workstation for example. And also debugging VMware Workstation on the Windows can be much easier. OpenVM tools shares a lot of code with VMX, like IO function, event loop, and this can really speed up your reverse engineering procedure. And also, as a beginner, you may need to collect CVs, papers, device documents, and even check the QEMU code. Just utilize every information you can find to reverse engineering. So, first question, where should we start? If you already learned something about Hypervisor, there's always a loop that handles switching in or switching out of the visual machine. So we will locate the loop code and start reverse engineering the Hypervisor from there. Also, we know that VMX is a user-mode process, so it should access the kernel for switching. So they should have a kernel module. And on Windows, it's VMX86. And if we are talking about the EXXI, it just directly communicates with the VM kernel. And tools such as API Monitor can help us analyze the communication process between the VMX and the host OS kernel. And so, of course, the OS lock screen can help us to figure out the meaning of the device IO control. So it won't be hard to locate the main loop code. You can see that VMX uses the IO control ROM VM switch into the visual machine and blocks it until the visual machine needs to force you to help. VMX will get the return code of the IO control ROM VM and call the corresponding user RPC handler to handle the visual machine request. So what is the user RPC? And it's a mechanism that VMware designed for VMM to interact with VMX on host. And very similar to hypercode, but on user space process VMX. And user RPC contains lots of code related to device emulation. And basically, it is the start of device emulation. And it will only take one argument, which is user RPC block pointer. And it's a shared area memory region. Shared area is another mechanism for shared data with VMX and VMM. You may not be able to see some data be changed in VMX because they may be modified in VMM. So if we want to know what is passed in user RPC block, we should figure out how the shared area works. The implementation of shared area is related to creation and loading of VMM. So we need to study it from the initialization process and pay attention to every memory location cost. And also remember to check the interaction between VMware VMX and VMX86. We will notice that there is a EOF object called a VMM block embedded into VMX binary. Also, there are lots of VMM extensions stored in VMM block section. And these are also EOF objects. VMX will link them with VMM block in memory to construct the actual VMM binary according to the user's configuration. Okay, let's take a look at the VMM block. We can see there are predefined export symbols such as user RPC block and their corresponding mapping address in the shared private CPU VMX section. And some virtual devices require the shared memory too. So VMX will define this export or symbol in it too. And after these symbols have been defined, VMX will calculate the total size of shared area memories and allocate the corresponding memory space. Now we have seen how shared memory were allocated, but how do VMM access them? You will notice that VMM block has two sections. One of them contains VMM's GDT information and the other contains the VMM's virtual address mapping information. And combining this information, we will see VMX is responsible for allocating memory. And building the page table structures based on VMM block's information. And the VMX86 will further populate the page table information and allow the VMM EO file, which is constructed by VMX. And VMX, VMM block, VMX86 work together to build the VMM's environment. And mapping the host allocated address to VMM's virtual address. So if you need to know how to manipulate data in shared area region, you will need to check the VMM's code. And VMX choose to run which user RPC code handler is depending on the return code of the IO control VM run. So it's time for us to check how VMware's switch works. And the most important structure is cross page. It's a one page size memory region allocated for each vCPU by VMX86. And it's responsible for storing the context between VMM and the host. Just like the VM says, VMCS in Intel VT. We can check the monitor loader section to see its virtual address on the VMM's environment. When VMX call the IO control run VM, it will cause the VMX86 cause the host to switch to VMM code. And saving the CPU state to cross page and restore the VMM's CPU state. And the operation to switch out is the opposite of this process. Now we can check the VMM's code to see user RPC is implemented through the platform user code. And save the user RPC opcode and the platform code invocation number 100 to the cross page. And the platform code 100 will cause the VMX86 to return the user RPC code to VMX. So when IO control run VM returns, VMX can call the corresponding user RPC code handler. Also, whenever VMM prepares to call user RPC, you will see VMM modify the user RPC block to transfer data to VMX process. So now we finally understand the whole user RPC implementation. And based on this knowledge, we are able to further reverse the engineering the device visualization. For port IO emulation, VMM will reserve the x86 IO instructions and may attempt to use the user RPC to call VMX to process. And not all of the port IO callback will be registered in VMX process. There are some devices implement their IO callback in VMM. For MAMIO, in most cases, VMX associates the memory region with an ID and implement the callback in VMM. And most MAMIO functions in VMM automatically will call user RPC to handle the device emulation. But they may access the shared area to transfer data to VMX. So you need to check the code in VMM to understand what happened when you access the MAMIO region in guest machine. Also, physical memory access is another important part we need to analyze. We will not discuss too much about the memory mapping structure. But you should know that on Windows, the main guest memory is mapped as a file. And on EXXI, it is mapped by VMKernel. When VMX gets the physical memory region object, it will first check the object's type. And in most cases, it will directly access the point field. And the last interesting thing is we also found that on EXXI's VMM, some visual devices MAMIO will not always call user RPC. They will call user RPC only when hosted emulation is enabled. Instead of using user RPC to interact with VMX, they use VMK code directly called VMKernel to handle device emulation. So although VMX are identical on EXXI and Workstation, visual device emulation code won't always be the same by default. And basically, this graph describes the result of our VMware hypervisor analysis. Anyway, it is important for you to check the VMM code when you reverse engineering the device visualization. If you are not sure where device's data comes from, you can go check the VMM's code. If you are not sure how to trigger the device's code, you can go check the VMM's code. And if you want to find some new device's code, go check the VMM's code. Okay, let's enter in the second part, VMware device visualization bug hunting. Okay, let's start from the USB part. Basically, we can divide the USB emulation into three parts. USB host controller emulation, like EHCI, EHCI, XHCI. The second part, VUSB emulation. These are objects described in the USB specification. For example, URB object, USB pipe object, and USB port object. And the third part, VUSB backend device emulation. And these are actual USB devices, such as Bluetooth, HID, and RNG, and so on. So, as you can see, when guests want to access the USB device, it will need to put data in memory and access the host controller MMIO space, which will trigger the VMM handler's MMIO access, causing the corresponding user RPC co-handler in VMX. Then it will convert the data into URB object, and eventually processed by the backend USB device. If it tries to access a USB device plugged in the host machine, it will also need another service to help. And we actually find bugs in all these three parts. The fourth bug is in the host controller part, and it's an uninitialized memory. Before we start it, let me just explain some concepts. In control transfer, a USB device accepts a type of request called a standard device request, which begins in the format of setup packet. So it will be 8 bytes at least, and it has a w-length field, the length of subsequent data. The USB device may use this type of data, but the host controller does not transfer data based on this unit. For example, in UHCI, data is transferred in units of transfer descriptors. So when VMX handles the UHCI emulation, it retrieves the first TD on QuickHead and extracts the w-length field from the setup packet, and uses this size to determine the size of data buffer for URBObject. After allocation is finished, it will copy the data into URBObject, and they do lots of checks to prevent buffer overflow. Let's go back to the allocation process. It's actually when we're depending on the target USB device you are transferring to. A different type of backend USB device will result in URBObject with different fields. For example, HID device will allocate URBObject with all the additional fields besides the data buffer of the URBObject. Also, HID device will use malloc for data location. So there are some problems here. Allocating w-length size URBObject doesn't mean you will get the w-length size data from guest-supplied TDs. And malloc allocation lets the memory un-initialized. Also, backend USB device will return data through the same URBObject buffer. So it will eventually let the HIP data leak to the guest OS. And the second bug is user-after-free appeared in the USB emulation code. Again, let's first explain some concepts. In XHCI, there is a structure called device context which stores the USB device status. It is constructed by device-slot-context and endpoint-context. Device-slot-context holds the USB device information. Endpoint-context holds the information for a single endpoint. Also, each endpoint has one or more transfer rings. The data transfer requires blocks on the transfer rings. And XHCI transfer data is based on these units. In order to explain this bug, we need to look back at an older bug. And the old bug's patch is very interesting. It only alters the sequence between the calls to release transfer ring and the code that assigns values to endpoint-context. Let's first figure out how transfer ring release works. Firstly, it will get the transfer ring object from the endpoint-context. Then, it will retrieve the USB device object based on the USB port number saved in the device-slot-context. Then, it will obtain the USB pipe object from the USB device object based on the endpoint ID. And after that, it will release all the URB objects on that USB pipe object. And finally, it will release the transfer ring object. Also, it will release the possible URB object on the back-end USB device in case there may be a dangling pointer on the back-end USB device. So, it seems no problems here. However, it's actually not the URB object that could become the dangling pointer. It is because the transfer object pointer is not only held by the endpoint-context. A URB object also holds a point that points to a field of transfer ring object. And this field is responsible for tracking the corresponding TRP when XHCI returns the USB device response to the guest. And before the patch, XHCI command-like config endpoint could modify the endpoint-context contents before releasing the transfer ring. So, it may lead the type stored in endpoint-context mismatch with the USB pipe object type. And this will cause the function written without a USB pipe object and left the URB object on that USB pipe object not be freed. And the previous pointer now becomes a dangling pointer and causing user after-free. So, now our question is, after this patch, is it possible to make the USB pipe object mismatch again? We know vmx will need to check the port number stored in slot-connect to find the USB device object. So, if we can modify the port number, it may lead vmx find a wrong USB device object. So, in that case, it will also find a wrong USB pipe object. So, we do have address-device command can make this possible. You can see the address-device command only modifies the slot-context and the control-endpoint-context. So, firstly, we finish the device configuration process and we create the transfer ring on non-control-endpoint. And then we transfer URB data on these transfer rings. In the end, we use the address-device command on that device to modify the device port number. Now, if we try to release the transfer ring on that endpoint, it will fail to find the right USB pipe object and causing UEF again. So, this is a bug that happens between host controller and the USB emulation. And the cause is it fails to manage the object lifetime in USB emulation. And the third bug is in the USB backend device emulation. And it is out-of-boundary will be triggered within the system API. Let's start with some concepts. The guest OS communicates with the smart card through the virtual smart card reader. And the guest OS uses CCID protocol to interact with the virtual smart card reader. The application data units, APD, serve as the payload for interaction between the smart card reader and the smart card. And you can see their relationship are very like the host controller and the USB device. And you may already seen that both of these data structure has the length field. And it is always need to be careful handling this length field. VMware do checks whether the message length field of CCID protocol matches the length field of command APD. But it fails to verify whether these two fields conform to the size of URP buffer. It directly uses this field as arguments to call the Windows sCAD transmits API. Clearly, sCAD transmits cannot check whether the length matches the data buffer. So it will out-of-bound access to the heap data and write it to the smart card on your host. Also, you may be able to read it back. So it's kind of interesting. Okay, in conclusion, it's very challenging to defend such complex system. VUSB emulation can be attacked. USB device emulation can be attacked. And host consoler emulation can be attacked too. Also, we still have cases cannot present. But you can defer the binary to found. And maybe more attack scenarios in the future. Maybe plugging evil USB device and leverage the VMX to execute the code. Or leverage local USB service to privilege escalation. And anyway, that's the USB part. And next part will be presented by my colleague. And we will show you that how SCSI works different on ESXi and the workstation by default. Hello, everyone. I'm Zhenghao. I'm the speaker on this topic. Now it's my turn to expose VMkernel, a whole new attack surface. But I will first talk about some differences between ESXi and the workstation. Because our new attack surface is hidden in these small differences. ESXi is a type 1 hypervisor that runs directly on the host's physical hardware. Well, workstation is a type 2 hypervisor that is installed on top of an existing host operating system. Workstation sports more configurations and devices than ESXi. Which means that in certain situations, workstation can be audited at more points than ESXi. In some specific device, the data flow direction of ESXi and workstation is different. Next, I will take SCSI device emulation as an example to demonstrate the vulnerabilities on different attack surface caused by different data flow orientations. This is an overview of the SCSI data flow. You can see that there are two paths for writing data to the disk. One through VMkernel and one through VMX. These are two completely different code paths, belonging to ESXi and workstation, respectively. The specific code path of SCSI in workstation is shown in the figure. If the user calls the MPI function SCSIioRequest command, it will enter the LSI logic MPO process SCSIioMessage function, where it will confirm whether the environment is hosted emulation. Finally, the LSI logic hosted process SCSIioMessage function calls the relevant handler in VMX through user RPC. The specific code path of SCSI in ESXi is shown in the figure. ESXi divides the situation into whether the environment itself is in hosted emulation. If it's in hosted emulation, it's the same as the workstation path. Otherwise, it goes to LSI logic VMk process SCSIioMessage. Go to VMkernel through VMk call arguments. Because of this small code gap, ESXi has a new attack surface that workstation does not have. Next, I will introduce the vulnerabilities I found on these two different parts. The first vulnerability we will talk about is located in VMX, and the official description is out-of-bounds read-write vulnerability. As a tool for storing data, stability is the most critical and basic requirement for disk. VMware implements corresponding disk verification capabilities in device emulation. One of the disk verification capabilities is to detect data corruption through chipset. This is where the vulnerability occurs. Before we start, let's first introduce a SCSI command. In the SCSI command's reference menu, there is a command called write16 that allows the operation of 8-byte logical block address and 4-byte transfer length. This shows that we can use this command to access any address in the 64-bit space, provided there is no disk capacity limit check. VMware did not implement the disk capacity limit check in its disk verification program implementation. As can be seen in the figure, after applying for a heap of corresponding size according to the disk capacity, only the operation is checked to see if it is a read or write operation. Obviously, this is a heap overflow. The write16 and other read and write commands in the SCSI command's reference menu allow us to access any 8-byte address. This allows us to easily perform arbitrary read and write operations. The vulnerability we will discuss next is located in VMkernel, and the official description is out-of-bounds read. Still start with the SCSI command's reference menu. In the SCSI command's reference menu, there is a command called unmap, which can unmap one or more logical basic addresses. The structure of unmap itself is relatively complex, and is divided into three parts. unmap command, unmap parameter list, and unmap block descriptor. Let's first look at the structure definition of unmap command in the SCSI command's reference menu. The focus of the figure is the parameter length, which indicates the total length of the unmapped parameter data. The unmapped parameter list is divided into two parts, unmapped parameter list header and unmapped block descriptor data. The unmapped parameter list header consists of three fields, unmapped data length, unmapped block descriptor data length, and reserved. The unmapped block descriptor consists of two fields, unmapped logical block address and number of logical blocks. These two fields indicate the starting address and length of the block that the user wants to unmap. This is an open view of the three unmapped structures, so that everyone can easily understand the structure of the unmapped command. The three structures are very closely related, and we will focus on parameter list length and unmapped block descriptor data length. Both fields indicate the length. Unmapped block descriptor data length indicates the length of all subsequent unmapped block descriptors. Parameter list length includes not only the length of all unmapped block descriptors, but also the length of the unmapped parameter list header. Let's see how VMware implements the unmapped command according to the SCSI command's reference menu. Before actually running the unmapped command, the vscsi-check-unmapped-cmd function will be entered first. In this function, a heap of the corresponding size is requested according to the parameter list length, and all the unmapped data is copied to the heap. After the unmapped data is copied, the content of the data will be checked. But it forgets to check the relationship between parameter list length and unmapped block descriptor data length. And the number of unmapped block descriptors is determined based on the unmapped block descriptor data length. The user can set a special parameter list to carry a large number of unmapped block descriptors, but only check the first unmapped block descriptor. Check completed. Enter the actual use stage. According to the code in the figure, parameter list length is used as the boundary length, which allows the previously unchecked unmapped block descriptor to be put into use. The unchecked logical block size is used as the size variable in the code shown in the figure. An obvious out-of-bounds write. In fact, it's this out-of-bounds write that actually causes the crash, not the so-called out-of-bounds read. Regarding the new attack surface of VMkernel, I have summarized the following three major points. Modify the existing sandbox protection mechanism. Elevate the current process privileges. Virtual machine escape. Okay, thank you for your listening. If you have any problem, you can contact us with Twitter.