Speakers. One of them is returning. One of them is brand new. We have a little tradition for brand new speakers. So before we get started, we're going to do a shot. Hello. Hello. So good afternoon everyone. Today, today we will present a topic about, uh, kernel security. One bite and all your dreams will come true. Analyzing and attacking Apple kernel drivers. So, uh, this is Baixiaolong. Um, my name is Zhengming. You can call me Spark. Uh, we are PhDs and work for Alibaba. So, uh, I'm going to introduce myself. I'm going to introduce myself. So here is the agenda. First, we briefly introduce what is kernel driver. Then we talk about new vulnerabilities in Apple drivers. Uh, after that we will talk about obstacles in analyzing Apple drivers. And then introduce a new tool called RIC for analyzing and fuzzing kernel drivers. So let's start. First thing you should know is every driver is in uh, in kernel is in XNU. Is a kernel extension. They share a same space with the kernel. On Mac OS, uh, the drivers locate in the uh, system library extensions. On iOS drivers are integrated with kernel in the kernel cache. Uh, kernel drivers provide you the client, which are kernel objects for drivers to provide you the credentials. So that, uh, the project can use the service to programs in user space. Also user client is the interface between user space applications and devices. In order to provide services, user clients need to implement several callback methods like external method, client memory for type, register notification port, client close and so on. The most important method, callback method is the external method which provides the methods to user space program. The selector is used to select the method in user client. Arguments are passed to the selected method. Dispatch is a struct representing the method to be called. The target is the target user client for the method to be called on. Reference is the result which is sent back to the user space. Uh despite the strict uh sandbox restriction, some user clients are still be accessible to sandbox apps on IOS. For example, uh IOHID lib user client and uh IOService root user client. Uh this is the default. So now we will share some uh vulnerabilities we found in Apple drivers. Uh the first thing you should know is the driver is uh the driver are good targets for exploring the kernel. Because they share the same space with the kernel and they have the same kernel privileges. So uh some drivers are programmed by third party vendors, not the kernel developers. So uh the code quality is not guaranteed. So a lot of drivers are frequently exploited. Uh int attacks against this kernel including the jailbreak. Uh a lot of vulnerabilities are used in jailbreak. For example IOS 11 used IOService root. Uh pangold nine use the uh first in their in their first version they used uh iOS11. Then uh iOS14 IOS15. Then uh IOS7. If you have a IO-HID family. And the second version they used the IO buffer frame. Uh, IO mobile buffer, uh, frame buffer. Uh, the first, uh, this is the first vulnerability. Uh, it is, uh, info information link in IO firewall family. It is caused by, uh, uh, initialize the kernel, uh, uh, stack, uh, variable, which is out channel handle, uh, in the ISO channel alloc case. As you can see in the IO sage channel create, the out, uh, channel handle is passed into the, uh, add object function. In this function you can see the out handle should be set with the index of new added object. But when the new, uh, capacity reaches FFFE, new object will not be added and out handle will not be set. Recall the allocation case in external method. Out handle is, uh, uh, uh, initialized, uh, variable and returned to user space. So that is, uh, a information link. Uh, you can use this information to help you to get the kernel slide and then defeat KASLR. Uh, the second bug is a user after free vulnerability in the IO accelerator family two. It has a method called s transaction and in this method the user the client will call transaction end in IO uh uh accelerer uh display pipe two. Note that all user clients share a same pipe two instance. Pipe two can uh contains a link list for transactions and pipe two will uh traverse the link list to find a transaction. And then call the set transaction arguments for the found transaction. However in the transaction arguments set the trans- transaction arguments function uh user client pipe user client two is a member of transaction two. And the function will call the return method uh of the user client. But the pipe user client can be released by calling IO service close for the uh from user space. Uh uh the transaction is closed. Uh uh the transaction is closed. Uh the transaction is released which will call the user after free. So here is the steps for uh exploit this bug. Firstly we create two pipe user client. Then we add a transaction for the first user client. And then we release user client. After that we end the transaction from the second user client. It will trigger the user after free. Uh but this bug is fixed because uh now the user the pipe user clients don't share a same pipe. Uh so how to exploit a user after free bug in the kernel? Here is the basic flaw. Firstly we construct the rope change and then trigger the release. After that we use a fig object with fig V table to occupy the the employee slot. Last but not least we trigger the user after free by invoking the message of the user after free. Uh uh of old object. The HIP3 technique we used in this vulnerability is OS Uncentralized XML. You can invoke it by setting properties of a device. Note that the spread data can be read and the head of spread data is controllable. So this is the rope chain for the privilege escalation. Firstly, we use a stack pivot to control the stack and then get the pointer to the struct proc of current process. Then we get the U credit from proc and set CRUID, CRRUID, and CRSVUID to zero. After that, we will use thread exception to exist the kernel mode and return to user mode. Then we can execute the system bash to get a shell with root privilege. The stack pivot we used in this rope chain is the following gadgets used in the stack pivot. At the beginning, we use IX to control RCX. Then we use RCX to control ISP, which is the stack pointer. So here is the layout of gadgets in a rope chain. Note that RAX is controlled by us. By using these vulnerabilities, we got a privilege escalation on Mac OS 10.13, 10.13.2, and 10.13.3. So here is a demo on 10.13.3. As you can see, we got a root shell in this Mac OS. So Xiaolong will talk more about drivers analyzing and fuzzing. Okay. Thanks, Spaz. And next, we are going to show how we found those vulnerabilities. And Apple drivers are mostly closed-sourced. In order to find vulnerabilities, we need to do some reward engineering. But it is not that easy. Let's first have a look at the decompiled code of driver boundaries in IDEA Pro. In the decompiled code of a Mac OS driver, you can see that the function names are kept. But there are many problems, such as wrong parameter inference, lost variable types, and unknown virtual function calls. As for iOS kernel drivers, the situation is even worse. We have no idea of what this decompiled code represents. It's a mess. In fact, in order to find some vulnerabilities, or do some reward engineering, we want the decompiled code to be just like the source code, just like the figures suggest. So we propose a new tool to analyze drivers in Mac OS and iOS. This tool is called Rilk, which is a statistical analysis tool for making Apple drivers decompile the code, more like source code. It is implemented as an IDEA Pro Python script. The name Rilk stands for a character in the comic series, Death Note, who loves eating apple. So Rilk has several features as below. It can identify name, size, and retables for classes. It can recover function names, resolve variable types, extend cross-reference, extend UI support, and construct all call graphs. So firstly, Rilk identifies name and size for classes. We identified this information from some specific initial adjacent function. As the figure shows, a sample initial adjacent function looks like this. You can see that this code indicates some information of the class, for example, its name and its size. With this identified class name and size, we can create structures in IDEA Pro to represent these classes. Besides, Rilk can also identify retables for classes. You should know that in C++, if a class has any virtual function, it always has a structure called retable to organize its virtual functions, just like the figure suggests. We identify classes' retables with a hint, and that is, ahead of a class retable, there is always a pointer pointing to the class, metaclass object. This is a special object. And I recall that in the specific, in a special initial addition function, this function also, also, shows where the metaclass object is. So with about hint, we can use the metaclass object address to find the class retable. And after that, we can create structures in IDEA Pro to represent these retables, and send the first member of class structures to a pointer to their retables. The next feature of Rilk is recovering function names. As we can see, iOS drivers are lost of symbols. Functions in iOS drivers do not have meaningful names at all. They are all meaningless names prefixed with star. We recover function names by leveraging several facts. The first fact is that most classes in driver are incurred from classes in the kernel. And these kernel classes have symbols. The second fact is that, in C++, when a child class inherits a parent class and overrides a virtual function, the parent's function and the child's function should have the same name and the same offset as shown in this figure. So, with the above facts, we can recover the names of virtual functions that override functions in kernel classes. Actually, this is not a complete solution, but it can cover plenty of critical functions. Another critical feature of ReelCase is that it can resolve types of local, global, and member variables in the compiled code. Its method is to first identify local variables' types and then perform type propagation along the same line. There are mainly two kinds of local variables which are called parameter variable and stack variable. For parameter variable, we identify their types by leveraging a fact that is, in a binary, C++ function names are encoded with the function's class name and the parameter types. So, by encoding these names, we can know the function's parameter types just as the function's figure shows. For stack variables, we identify their creation and initialization by finding some typical functions as listed here. So, after variable types are identified, we can propagate its type along function's control flow graph. During type propagation, we figure out what variables are assigned with the variable whose type is defined and then send the types for them according to the identified type, just like this figure shows. Next, we extend cross-reference for member variables and virtual functions. We examine every sentence in the decompiled code and of all functions and check whether any member variable or virtual function is used. And then, we add a cross-reference in the edit pro from the usage of, from this usage, from this usage to member variable or virtual function's location in their class or with table structures. And then now, you can see that the decompiled code now looks more like the source code, right? Right? Further, in order to, in order for better reward engineering experience, we extend U.S. operations in idea pro. For example, as the demo will show, RIC allows jumping to a virtual function's implementation by just a double-click on a virtual function's call. You can see the, you can see that the, one, two, three, one and a double-click, we can derail-jump kilometers functions implementation. And we can also jump to the choice in, Kimplementation deleted . Okay. Finally, after all writeable types are identified, which function calls are resolved, and now we can generate call graphs for all drivers. And this call graph is very important for further inter-procedure control flow and data flow analysis. And now everything is ready. We can do manual analysis or other kinds of static analysis as we want. Below we will show a use case of ROOC, which is called ROOC Fuzz. The idea of ROOC Fuzz is to guide driver fuzzing with static analysis results. We implement it by integrating ROOC with the state-of-the-art driver fuzzer, Passive Fuzz Framework OS X. ROOC Fuzz takes two steps. In the first step, the performed data flow analysis to infer drivers required user input performance. The second step uses the inferred input format to guide input generation in the fuzzing process and improve fuzzing efficiency. So in its first step, we performed static data flow analysis in the decompiled code to identify whether user input is checked in conditions and what parts are checked and what is the condition. Just like this figure shows that we can identify that the offset 24 and the size one should not be equal to six or one in order to avoid this field pass. And then we can know driver's requirements on user inputs for different execution paths. And then use such knowledge to get fuzzing. But Passive Fuzz Framework OS X only works on Mac OS 10.11 now. For later versions, there are several errors that need to be resolved. The first error is that it uses wrong buffer sets to read the kernel header. Passive Fuzz Framework OS X, suppose that, you know, that the size of the kernel header is less than one page. Actually, after Mac OS 10.12, the kernel header is larger than one page. In order to solve this problem, we changed the buffer set to two pages. The second problem is that Passive Fuzz Framework OS X uses a wrong way to infer the kernel text base. In its original implementation, it finds the address of interrupt handler, which was in the kernel text section, and it uses this address to find the kernel text space. But for now, in the current Mac OS, the interrupt handler is no longer in the kernel text. As a result, we cannot find kernel text space with that name. the handler address. This change is caused by Apple's mitigation on the famous mail-down vulnerability. In order to defend against mail-down, Apple decides to move the interrupt handler and some other code related to the user space from the kernel text to another special location, just like the figure on the right suggests. So now the interrupt handler is not in the kernel text space. As a solution, we look for the kernel text space with another method, that is, we find the address of some other kernel functions, for example, the address of lock-mutex-lock, and traverse back to find the kernel text space. And this is the most, more stable than the previous method. So, in conclusions, in this talk, we have showed several vulnerabilities that can be exploited for privilege expression on macOS. And we have introduced the technique of exploiting user-free vulnerabilities. In the kernel. And we also explained a new static analysis tool called Ryuk. And also shows a use case called Ryuk Fuzz. Most importantly, part of Ryuk has been open-sourced. You can fork Ryuk or star Ryuk on my GitHub. And also, very welcome to follow us on Twitter and Weibo. My Twitter account name is bxl1989. And Spark's Twitter account name is sparkzhen. And that's all. Thank you for your attention.