start	end	text
640	5660	Hello everyone, welcome to VMware Device Visualization Backhunting.
6020	11880	We are excited to share our talk with you and also feel a little disappointed for we cannot
11880	18380	arrive DEF CON in person. And we do hope our talk will serve as a guide for anyone who's
18380	28020	looking to start VMware security research. Firstly, allow me to introduce us to you.
28020	34140	My name is Jiaxin Huang and Hao Zheng and me are both security researchers of Tenggong team
34140	41260	at Xianxin Group. And we are both interested in reverse engineering and visualization security.
41720	47640	Yueliu is our team leader. And our team consists of lots of people interested in
48340	54900	viral security domain. And we have a website to post our blog on it. So if you are interested in
54900	62900	it, make sure to follow us. And if you have any questions, feel free to DM or email us.
64580	69420	Here is the short story. Let's describe our visualization backhunting journey.
69420	75060	We actually started to research visualization security from the end of 2022.
75800	84180	And in that time, we basically know nothing about visualization. So why are we interested in it?
84900	91020	Because visualization is involved in lots of hacking contests. And it's a very hard,
91020	98320	very challenging target. And for myself, I want to reverse engineer it and learn how it works.
98520	105200	So what have we achieved in last year? We successfully escaped from the paralyzed desktop
105200	113600	on GeekCon 2023. And we reported lots of workstation EXXI bugs to VMware.
115180	119530	And we learned a lot, not just about visualization,
120040	123290	but about the whole architecture of the entire computer.
125380	133380	So what can we bring to you today? Firstly, we will introduce the VMware visualization architecture
133380	140580	to you. Most people hunting bugs directly from debugging the visual device is code.
140840	146820	But we choose to take a bigger view. We studied it from reverse engineering the whole VMware
146820	152520	visualization architecture. And this helped us to figure out which code and how they can be
152520	161840	influenced by the guest OS. And hopefully, it will bring some new idea to you. And later,
161840	168080	we will use the bugs that we found in USB visualization and SCSI visualization to explain
168080	175100	the possible source of 3D in visual device. And anyway, a visual device is still the most
175900	179900	cost-effective choice in visualization bug hunting.
181700	187280	Okay, let's start the first part. VMware Hypervisor Reverse Engineering.
189400	196200	If you are new to VMware visualization, I do have some tips for you. The most important
196200	204900	binary on VMware Workstation or EXXI is VMX. It is responsible for starting the visual machine
204900	213260	and contains most of visual device code. And no matter it is EXXI or Workstation,
213260	220780	VMX is identical and basically just use different system API. And in this part,
220780	227940	we will use Workstation for example. And also debugging VMware Workstation on the Windows can
227940	238620	be much easier. OpenVM tools shares a lot of code with VMX, like IO function, event loop,
238620	246340	and this can really speed up your reverse engineering procedure. And also, as a beginner,
246340	254240	you may need to collect CVs, papers, device documents, and even check the QEMU code.
254240	258680	Just utilize every information you can find to reverse engineering.
260340	268320	So, first question, where should we start? If you already learned something about Hypervisor,
268320	273190	there's always a loop that handles switching in or switching out of the visual machine.
273680	281660	So we will locate the loop code and start reverse engineering the Hypervisor from there.
282100	290890	Also, we know that VMX is a user-mode process, so it should access the kernel for switching.
290890	299650	So they should have a kernel module. And on Windows, it's VMX86. And if we are talking
299650	310250	about the EXXI, it just directly communicates with the VM kernel. And tools such as API Monitor
310250	317510	can help us analyze the communication process between the VMX and the host OS kernel.
319710	327630	And so, of course, the OS lock screen can help us to figure out the meaning of the
327630	337810	device IO control. So it won't be hard to locate the main loop code. You can see that VMX
337810	345530	uses the IO control ROM VM switch into the visual machine and blocks it until the
345530	352270	visual machine needs to force you to help. VMX will get the return code of the IO control ROM VM
352270	358710	and call the corresponding user RPC handler to handle the visual machine request.
360230	369690	So what is the user RPC? And it's a mechanism that VMware designed for VMM to interact with
369690	379830	VMX on host. And very similar to hypercode, but on user space process VMX. And user RPC
379830	387210	contains lots of code related to device emulation. And basically, it is the start of device emulation.
388710	397090	And it will only take one argument, which is user RPC block pointer. And it's a shared area
397090	405890	memory region. Shared area is another mechanism for shared data with VMX and VMM. You may not be
405890	413490	able to see some data be changed in VMX because they may be modified in VMM. So if we want to
413490	419950	know what is passed in user RPC block, we should figure out how the shared area works.
421570	428330	The implementation of shared area is related to creation and loading of VMM.
428830	436430	So we need to study it from the initialization process and pay attention to every memory
436430	444710	location cost. And also remember to check the interaction between VMware VMX and VMX86.
446570	454430	We will notice that there is a EOF object called a VMM block embedded into VMX binary.
454850	462210	Also, there are lots of VMM extensions stored in VMM block section. And these are also EOF
462210	470850	objects. VMX will link them with VMM block in memory to construct the actual VMM binary
471690	474270	according to the user's configuration.
476910	487610	Okay, let's take a look at the VMM block. We can see there are predefined export symbols such as
487610	494450	user RPC block and their corresponding mapping address in the shared private CPU VMX section.
494750	501230	And some virtual devices require the shared memory too. So VMX will define this export
501230	508830	or symbol in it too. And after these symbols have been defined, VMX will calculate the total
508830	516090	size of shared area memories and allocate the corresponding memory space. Now we have seen how
516090	521170	shared memory were allocated, but how do VMM access them?
522790	529290	You will notice that VMM block has two sections. One of them contains VMM's
529290	535790	GDT information and the other contains the VMM's virtual address mapping information.
538050	545950	And combining this information, we will see VMX is responsible for allocating memory.
546090	552930	And building the page table structures based on VMM block's information. And the VMX86
552930	560310	will further populate the page table information and allow the VMM EO file, which is constructed
560310	569830	by VMX. And VMX, VMM block, VMX86 work together to build the VMM's environment.
570330	574030	And mapping the host allocated address to VMM's virtual address.
574030	579670	So if you need to know how to manipulate data in shared area region,
579670	588790	you will need to check the VMM's code. And VMX choose to run which user RPC code handler is
588790	596050	depending on the return code of the IO control VM run. So it's time for us to check how VMware's
596050	607110	switch works. And the most important structure is cross page. It's a one page size memory region
607110	616370	allocated for each vCPU by VMX86. And it's responsible for storing the context between
616370	626570	VMM and the host. Just like the VM says, VMCS in Intel VT. We can check the monitor
626570	635930	loader section to see its virtual address on the VMM's environment. When VMX call the IO control
635930	645130	run VM, it will cause the VMX86 cause the host to switch to VMM code. And saving the
645640	651690	CPU state to cross page and restore the VMM's CPU state.
651850	657330	And the operation to switch out is the opposite of this process.
658830	667150	Now we can check the VMM's code to see user RPC is implemented through the platform user code.
667210	673190	And save the user RPC opcode and the platform code invocation number 100 to the cross page.
675130	685350	And the platform code 100 will cause the VMX86 to return the user RPC code to VMX.
685350	692430	So when IO control run VM returns, VMX can call the corresponding user RPC code handler.
693010	698340	Also, whenever VMM prepares to call user RPC,
698750	704630	you will see VMM modify the user RPC block to transfer data to VMX process.
707230	715910	So now we finally understand the whole user RPC implementation. And based on this knowledge,
715910	721030	we are able to further reverse the engineering the device visualization.
721730	728790	For port IO emulation, VMM will reserve the x86 IO instructions
728790	734430	and may attempt to use the user RPC to call VMX to process.
736450	743290	And not all of the port IO callback will be registered in VMX process.
743330	747910	There are some devices implement their IO callback in VMM.
749230	761170	For MAMIO, in most cases, VMX associates the memory region with an ID and implement the callback in VMM.
763550	772650	And most MAMIO functions in VMM automatically will call user RPC to handle the device emulation.
772810	777350	But they may access the shared area to transfer data to VMX.
777350	785750	So you need to check the code in VMM to understand what happened when you access the MAMIO region in guest machine.
788130	793580	Also, physical memory access is another important part we need to analyze.
794310	798700	We will not discuss too much about the memory mapping structure.
799270	804560	But you should know that on Windows, the main guest memory is mapped as a file.
805130	810230	And on EXXI, it is mapped by VMKernel.
810890	817610	When VMX gets the physical memory region object, it will first check the object's type.
817610	822530	And in most cases, it will directly access the point field.
824410	832370	And the last interesting thing is we also found that on EXXI's VMM,
832370	836970	some visual devices MAMIO will not always call user RPC.
837430	842610	They will call user RPC only when hosted emulation is enabled.
842950	847630	Instead of using user RPC to interact with VMX,
847630	854670	they use VMK code directly called VMKernel to handle device emulation.
855410	860650	So although VMX are identical on EXXI and Workstation,
861730	865550	visual device emulation code won't always be the same by default.
869530	876350	And basically, this graph describes the result of our VMware hypervisor analysis.
881700	887940	Anyway, it is important for you to check the VMM code when you reverse engineering the device
887940	896980	visualization. If you are not sure where device's data comes from, you can go check the VMM's code.
897120	901280	If you are not sure how to trigger the device's code,
901280	908500	you can go check the VMM's code. And if you want to find some new device's code,
908500	912260	go check the VMM's code.
915590	923360	Okay, let's enter in the second part, VMware device visualization bug hunting.
923360	934260	Okay, let's start from the USB part. Basically, we can divide the USB emulation into three parts.
934260	943420	USB host controller emulation, like EHCI, EHCI, XHCI.
944360	952360	The second part, VUSB emulation. These are objects described in the USB specification.
952360	959880	For example, URB object, USB pipe object, and USB port object.
960640	968160	And the third part, VUSB backend device emulation. And these are actual USB devices,
968160	980100	such as Bluetooth, HID, and RNG, and so on. So, as you can see,
980100	988000	when guests want to access the USB device, it will need to put data in memory and access the
988000	996360	host controller MMIO space, which will trigger the VMM handler's MMIO access, causing the
996360	1004100	corresponding user RPC co-handler in VMX. Then it will convert the data into URB object,
1004100	1011920	and eventually processed by the backend USB device. If it tries to access a USB device
1011920	1017160	plugged in the host machine, it will also need another service to help.
1017600	1022620	And we actually find bugs in all these three parts.
1024400	1032560	The fourth bug is in the host controller part, and it's an uninitialized memory.
1033520	1041360	Before we start it, let me just explain some concepts. In control transfer, a USB device
1041360	1049020	accepts a type of request called a standard device request, which begins in the format of
1049020	1056900	setup packet. So it will be 8 bytes at least, and it has a w-length field,
1056900	1066340	the length of subsequent data. The USB device may use this type of data, but the host
1066340	1073860	controller does not transfer data based on this unit. For example, in UHCI, data is transferred
1073860	1084880	in units of transfer descriptors. So when VMX handles the UHCI emulation, it retrieves the
1084880	1092260	first TD on QuickHead and extracts the w-length field from the setup packet, and uses this size
1092260	1100680	to determine the size of data buffer for URBObject. After allocation is finished, it will copy the
1100680	1109960	data into URBObject, and they do lots of checks to prevent buffer overflow. Let's go back to the
1109960	1117960	allocation process. It's actually when we're depending on the target USB device you are
1117960	1125240	transferring to. A different type of backend USB device will result in URBObject with different
1125240	1133740	fields. For example, HID device will allocate URBObject with all the additional fields besides
1133740	1141360	the data buffer of the URBObject. Also, HID device will use malloc for data location.
1145020	1151220	So there are some problems here. Allocating w-length size URBObject doesn't mean you will
1151220	1157120	get the w-length size data from guest-supplied TDs. And malloc allocation lets the memory
1157120	1163720	un-initialized. Also, backend USB device will return data through the same URBObject buffer.
1163740	1168280	So it will eventually let the HIP data leak to the guest OS.
1170720	1177220	And the second bug is user-after-free appeared in the USB emulation code.
1177760	1186500	Again, let's first explain some concepts. In XHCI, there is a structure called device context
1186500	1194400	which stores the USB device status. It is constructed by device-slot-context and
1194400	1202700	endpoint-context. Device-slot-context holds the USB device information. Endpoint-context
1202700	1209800	holds the information for a single endpoint. Also, each endpoint has one or more transfer rings.
1209800	1217200	The data transfer requires blocks on the transfer rings. And XHCI transfer data is based
1217200	1224560	on these units. In order to explain this bug, we need to look back at an older bug.
1224780	1231220	And the old bug's patch is very interesting. It only alters the sequence between the calls
1231220	1236660	to release transfer ring and the code that assigns values to endpoint-context.
1237540	1242360	Let's first figure out how transfer ring release works.
1244660	1253040	Firstly, it will get the transfer ring object from the endpoint-context. Then, it will retrieve
1253040	1260780	the USB device object based on the USB port number saved in the device-slot-context.
1261470	1270030	Then, it will obtain the USB pipe object from the USB device object based on the endpoint ID.
1270680	1277340	And after that, it will release all the URB objects on that USB pipe object.
1277500	1281460	And finally, it will release the transfer ring object.
1283420	1291860	Also, it will release the possible URB object on the back-end USB device in case there may be
1292080	1301440	a dangling pointer on the back-end USB device. So, it seems no problems here. However, it's
1301440	1309420	actually not the URB object that could become the dangling pointer. It is because the transfer
1309420	1317140	object pointer is not only held by the endpoint-context. A URB object also holds a point
1317140	1324220	that points to a field of transfer ring object. And this field is responsible for tracking the
1324220	1330240	corresponding TRP when XHCI returns the USB device response to the guest.
1331940	1338980	And before the patch, XHCI command-like config endpoint could modify the endpoint-context
1338980	1346700	contents before releasing the transfer ring. So, it may lead the type stored in
1346700	1355520	endpoint-context mismatch with the USB pipe object type. And this will cause the function
1355520	1364900	written without a USB pipe object and left the URB object on that USB pipe object not be freed.
1365600	1374140	And the previous pointer now becomes a dangling pointer and causing user after-free. So,
1374140	1382840	now our question is, after this patch, is it possible to make the USB pipe object mismatch
1382840	1390980	again? We know vmx will need to check the port number stored in slot-connect to find the
1390980	1399200	USB device object. So, if we can modify the port number, it may lead vmx find a wrong USB
1399200	1405540	device object. So, in that case, it will also find a wrong USB pipe object.
1407300	1413560	So, we do have address-device command can make this possible. You can see the address-device
1413560	1419060	command only modifies the slot-context and the control-endpoint-context.
1420000	1427300	So, firstly, we finish the device configuration process and we create the transfer ring on
1427300	1435520	non-control-endpoint. And then we transfer URB data on these transfer rings. In the end,
1435540	1441660	we use the address-device command on that device to modify the device port number. Now,
1441660	1446680	if we try to release the transfer ring on that endpoint, it will fail to find the right
1446680	1454300	USB pipe object and causing UEF again. So, this is a bug that happens between host controller
1454300	1462500	and the USB emulation. And the cause is it fails to manage the object lifetime in USB emulation.
1464660	1469570	And the third bug is in the USB backend device emulation.
1470280	1475610	And it is out-of-boundary will be triggered within the system API.
1478740	1485800	Let's start with some concepts. The guest OS communicates with the smart card through the
1485800	1493500	virtual smart card reader. And the guest OS uses CCID protocol to interact with the virtual
1493500	1502840	smart card reader. The application data units, APD, serve as the payload for interaction between
1502840	1510560	the smart card reader and the smart card. And you can see their relationship are very like the
1510560	1516620	host controller and the USB device. And you may already seen that both of these data
1518040	1527440	structure has the length field. And it is always need to be careful handling this length field.
1528280	1535100	VMware do checks whether the message length field of CCID protocol matches the length field of
1535100	1541920	command APD. But it fails to verify whether these two fields conform to the size of URP buffer.
1543520	1550420	It directly uses this field as arguments to call the Windows sCAD transmits API.
1550820	1555740	Clearly, sCAD transmits cannot check whether the length matches the data buffer.
1556540	1563280	So it will out-of-bound access to the heap data and write it to the smart card on your host.
1563280	1569500	Also, you may be able to read it back. So it's kind of interesting.
1572200	1579320	Okay, in conclusion, it's very challenging to defend such complex system.
1579380	1586260	VUSB emulation can be attacked. USB device emulation can be attacked. And host
1586260	1594700	consoler emulation can be attacked too. Also, we still have cases cannot present.
1594700	1604640	But you can defer the binary to found. And maybe more attack scenarios in the future.
1604760	1615540	Maybe plugging evil USB device and leverage the VMX to execute the code. Or leverage local USB
1615540	1626900	service to privilege escalation. And anyway, that's the USB part. And next part will be
1627460	1636000	presented by my colleague. And we will show you that how SCSI works different on ESXi
1636000	1647350	and the workstation by default. Hello, everyone. I'm Zhenghao. I'm the
1647350	1655070	speaker on this topic. Now it's my turn to expose VMkernel, a whole new attack surface.
1655530	1663410	But I will first talk about some differences between ESXi and the workstation. Because our
1663410	1671570	new attack surface is hidden in these small differences. ESXi is a type 1 hypervisor
1671570	1680950	that runs directly on the host's physical hardware. Well, workstation is a type 2 hypervisor
1681310	1690190	that is installed on top of an existing host operating system. Workstation sports more
1690190	1699490	configurations and devices than ESXi. Which means that in certain situations, workstation can be
1699490	1710430	audited at more points than ESXi. In some specific device, the data flow direction of ESXi and
1710430	1721150	workstation is different. Next, I will take SCSI device emulation as an example to demonstrate
1721150	1730290	the vulnerabilities on different attack surface caused by different data flow orientations.
1730870	1739850	This is an overview of the SCSI data flow. You can see that there are two paths for writing
1739850	1748330	data to the disk. One through VMkernel and one through VMX. These are two completely
1748330	1755970	different code paths, belonging to ESXi and workstation, respectively.
1757390	1767150	The specific code path of SCSI in workstation is shown in the figure. If the user calls the
1767150	1779850	MPI function SCSIioRequest command, it will enter the LSI logic MPO process SCSIioMessage
1779850	1788850	function, where it will confirm whether the environment is hosted emulation. Finally,
1788850	1799410	the LSI logic hosted process SCSIioMessage function calls the relevant handler in VMX
1799410	1811410	through user RPC. The specific code path of SCSI in ESXi is shown in the figure.
1811410	1821410	ESXi divides the situation into whether the environment itself is in hosted emulation.
1821490	1829910	If it's in hosted emulation, it's the same as the workstation path. Otherwise,
1829910	1843510	it goes to LSI logic VMk process SCSIioMessage. Go to VMkernel through VMk call arguments.
1845710	1856250	Because of this small code gap, ESXi has a new attack surface that workstation does not have.
1856250	1864290	Next, I will introduce the vulnerabilities I found on these two different parts.
1864830	1872430	The first vulnerability we will talk about is located in VMX,
1872430	1878990	and the official description is out-of-bounds read-write vulnerability.
1879790	1890270	As a tool for storing data, stability is the most critical and basic requirement for disk.
1890270	1898540	VMware implements corresponding disk verification capabilities in device emulation.
1898810	1907310	One of the disk verification capabilities is to detect data corruption through chipset.
1907310	1912030	This is where the vulnerability occurs.
1912550	1924570	Before we start, let's first introduce a SCSI command. In the SCSI command's reference menu,
1924570	1934250	there is a command called write16 that allows the operation of 8-byte logical block address
1934250	1942550	and 4-byte transfer length. This shows that we can use this command to access
1942550	1953050	any address in the 64-bit space, provided there is no disk capacity limit check.
1953910	1962330	VMware did not implement the disk capacity limit check in its disk verification program
1962330	1969930	implementation. As can be seen in the figure, after applying for a heap of
1971150	1976590	corresponding size according to the disk capacity,
1976590	1984050	only the operation is checked to see if it is a read or write operation.
1985010	1996610	Obviously, this is a heap overflow. The write16 and other read and write commands in the SCSI
1996610	2006190	command's reference menu allow us to access any 8-byte address. This allows us to easily perform
2006190	2015150	arbitrary read and write operations. The vulnerability we will discuss next
2015150	2023530	is located in VMkernel, and the official description is out-of-bounds read.
2024150	2034150	Still start with the SCSI command's reference menu. In the SCSI command's reference menu,
2034150	2043410	there is a command called unmap, which can unmap one or more logical basic addresses.
2044430	2054350	The structure of unmap itself is relatively complex, and is divided into three parts.
2054350	2062030	unmap command, unmap parameter list, and unmap block descriptor.
2062030	2071570	Let's first look at the structure definition of unmap command in the SCSI command's reference
2071570	2081490	menu. The focus of the figure is the parameter length, which indicates the total length of the
2081490	2090890	unmapped parameter data. The unmapped parameter list is divided into two parts,
2090890	2099830	unmapped parameter list header and unmapped block descriptor data. The unmapped parameter list
2099830	2108710	header consists of three fields, unmapped data length, unmapped block descriptor data length,
2108710	2118470	and reserved. The unmapped block descriptor consists of two fields, unmapped logical block
2118470	2127750	address and number of logical blocks. These two fields indicate the starting address and
2127750	2137110	length of the block that the user wants to unmap. This is an open view of the three unmapped
2137110	2146330	structures, so that everyone can easily understand the structure of the unmapped command.
2147270	2155970	The three structures are very closely related, and we will focus on parameter list length
2156290	2164610	and unmapped block descriptor data length. Both fields indicate the length. Unmapped block
2164610	2174050	descriptor data length indicates the length of all subsequent unmapped block descriptors.
2174050	2182330	Parameter list length includes not only the length of all unmapped block descriptors,
2182330	2191450	but also the length of the unmapped parameter list header. Let's see how VMware implements
2191450	2198810	the unmapped command according to the SCSI command's reference menu.
2198970	2208410	Before actually running the unmapped command, the vscsi-check-unmapped-cmd function will be
2208410	2218250	entered first. In this function, a heap of the corresponding size is requested according to the
2218250	2225850	parameter list length, and all the unmapped data is copied to the heap.
2225890	2233650	After the unmapped data is copied, the content of the data will be checked.
2234030	2242010	But it forgets to check the relationship between parameter list length and unmapped block
2242010	2250250	descriptor data length. And the number of unmapped block descriptors is determined based
2250250	2258610	on the unmapped block descriptor data length. The user can set a special parameter list
2258940	2267250	to carry a large number of unmapped block descriptors, but only check the first unmapped
2267250	2277990	block descriptor. Check completed. Enter the actual use stage. According to the code in the
2277990	2287010	figure, parameter list length is used as the boundary length, which allows the previously
2287010	2296870	unchecked unmapped block descriptor to be put into use. The unchecked logical block size
2296870	2307030	is used as the size variable in the code shown in the figure. An obvious out-of-bounds write.
2307030	2316450	In fact, it's this out-of-bounds write that actually causes the crash, not the so-called
2316450	2325690	out-of-bounds read. Regarding the new attack surface of VMkernel, I have summarized the
2325690	2335650	following three major points. Modify the existing sandbox protection mechanism. Elevate the current
2335650	2344950	process privileges. Virtual machine escape. Okay, thank you for your listening. If you have any
2344950	2348910	problem, you can contact us with Twitter.
