Good afternoon, everybody. Welcome to the last day of DEF CON 26. Yeah. And I'm assuming most of you are still waking up. But this will be a really good presentation. Yu Wang is going to be talking about attacking the macOS kernel graphics driver. So please give him a hand and welcome him here. Hello, everyone. Welcome to my presentation, especially during lunchtime. My name is Wang Yu from DD Research America. What I'd like to share with you today is related to macOS graphics driver vulnerability and macOS kernel security. I started to pay attention to macOS kernel security last year. Including kernel driver development, bug hunting, and vulnerability exploitation. Before that, I preferred to study the Windows kernel security and Android Linux kernel root. After entering the macOS kernel world last year, I learned a lot of well-known kernel vulnerabilities. And I picked up four of them as backups. So let's take a look at the background for today. The first case is from EMVUE, from Google Project Zero. And the CV number is the CVE-2015-3712. It's a NVIDIA GeForce driver arbitrary kernel memory write vulnerability. This vulnerability can eventually lead to code execution due to lack of input parameter validation. And it's a very common case. And at here, you can find a POC. The second vulnerability comes from Cisco Talos research team, which is a non-point-deference bug. It seems that the non-point-deference type of vulnerability can be exploited two years ago. Which also means that I'm late. Which also means that I'm late. And you can see that there's a full exploit code on the exploit database. Which is a good starting point for exploitation research. And the next case is from my friend, Chen Liang. The attack surface he chooses is a user mode graphics daemon. And he runs a pontoon game. With this vulnerability two years ago. The user mode daemon associated with the graphics library has certain permissions. And they are usually accessible in the sandbox process. And the logic of those daemon is usually very complicated. This condition determines that the daemon are nature attack surface. And you can get more detailed information from the writer. And you can get more detailed information from the writer. The last is also from KingLab. The CV number is 20161850. It's an IO accelerator family out of bounds kernel memory write vulnerability. The graphics rendering engine is one of the hardest hit areas on any operating system platform. The graphics rendering engine is one of the hardest hit areas on any operating system platform. The graphics rendering engine is one of the hardest hit areas on any operating system platform. The graphics rendering engine is one of the hardest hit areas on any operating system platform. The graphics rendering engine is one of the hardest hit areas on any operating system platform. The graphics rendering engine is one of the hardest hit areas on any operating system platform. The graphics rendering engine is one of the hardest hit areas on any operating system platform. The graphics rendering engine is one of the hardest hit areas on any operating system platform. The graphics rendering engine is one of the hardest hit areas on any operating system platform. The graphics rendering engine is one of the hardest hit areas on any operating system platform. The graphics rendering engine is one of the hardest hit areas on any operating system platform. The graphics rendering engine is one of the hardest hit areas on any operating system platform. The graphics rendering engine is one of the hardest hit areas on any operating system platform. This case has repeatedly reminded us that the graphics security cannot be ignored. So therefore, I decided to start my kernel, macOS kernel research from here. And part two, part two is from NDPOC to the other days. When I decided to investigate the Apple graphics driver vulnerability last year, I started my research from the POC samples from EMBU such as 2017-2443 and 2017-2489. The reason for choosing this example is that they are very easy to learn. The logic of the POC code is clear. And the amount of the code is less than 100. We can take the first one, the 2443 as an example. 2443 is a kernel arbitrary code execution vulnerability. As you can see, the target of the POC is Intel FB client control. It's number one in green color. And the input selector here is number three in blue color. The input selector is 291 in hex format. And then the only thing we need to do is fill the input buffer with random garbage data. It's number three in red color. So just this. So I think for the security research community, each of us will think about how to start a new round of kernel code auditing from here. After I had this idea, one of my friends told me that starting from here might be a waste of time. Because the binary has already been carefully examined by Google Project Zero. This reminds me of my Windows kernel from Skitter Engine. I did a Windows Engine vulnerability presentation at Black Hat USA four years ago. At that time, Jura from Google Project Zero found a large number of kernel font Skitter vulnerabilities. But I can still find a new kernel double fetch zero days in the code that has been audited, such as CVE-2015-1819. So this time, I still want to give it a try. But then I began to feel that my friend was probably right. My fuzzing tool didn't have any valid output on the first day. And even the breakpoints I set in macOS kernel didn't trigger. Fortunately for me, I didn't give up at that time. Otherwise, I don't think we will meet today. When I analyzed the root cause, I found at least three problems hidden in the work of my fuzzing system. Without first serving these problems, the whole system will become very efficient. There are three obstacles. The first one is target selection. The second one is field driver protection. And the third one is unremarkable selectors. Let me discuss it in detail. First one, target selection. There are many different targets on the macOS system. From kernel extension to internal classes. And we mentioned before that Intel FB client control is just one of the very small branches of the graphics kernel. I list some of the target drivers here. There are a lot of them, from AMD to Nvidia to Intel. And also IO family drivers. Intel is like a mini pod driver. And the family driver is a more general driver, kernel driver. And I think as a professional security researcher, we should not miss any possibility. And the next one is more important. The second obstacle is the field driver. Before touching the target graphics driver, there are usually field drivers that protect them. Which can cause our fuzzing tool to fail into an ineffective loop. In this figure, we can see that before touching the graphics driver, Apple Intel frame buffer also filter driver Apple graphics device control. It is defining against the malicious input for the graphics driver. Here is the filter driver in the right line. And the green line is the graphics driver itself. More specifically, in this example, selector 707 represents FB set EDID. And correspondingly, in the filter driver here, if the input buffer length is not equal to 408 bytes, all fuzzing attempts for the interface are meaningless. This means that if we only rely on the static analysis of the target driver, and then build the fuzzing process, it's obviously not enough. We need to consider that the entire architecture of the graphics driver set. Otherwise, we are just wasting our time. The third obstacle is unremarkable selectors. After actually getting to the target driver, I found that there are still some hidden control codes or selectors. And I found... There are also... The control codes affect the efficiency of the fuzzing system indeed. These control codes are important because they are the key to open the door to another world. Meaning that there are a lot of unknown codes behind their doors, their stalls. After extracting all the validated control codes, with ID script last year, I found at least one such key. It's 80000 and some random data. As you can see, after entering the corresponding handler, there's another word hidden there. And after taking... After taking... So after taking their three small steps, things quickly become clear. And I found a lot of problems in just one day. Let me show... Share some examples with you. The first... The first one is an unpatched lock panic caused by a division by zero error. But be aware that the root cause behind this error is out of bounds read access to the input buffer. It's a... It's a division by zero. Register R12 is zero. And a case two. Case two is an unpatched lock of panic caused by non-point dereference. And the root cause of the problem is that the driver incorrectly initialized and dereferenced the variable. At here, RSI register is zero. And offset is 3F70. The next case is a kernel stack... Stack-based buffer overflow vulnerability. As you can see here, this stack overflow vulnerability is mitigated by stack cookie. So in order to exploit this case, we need to have a... We need to have the kernel arbitrary memory reading capability. Fortunately, I quickly found one. CVE-2017-13383. Combined with the two vulnerabilities, we can bypass the stack cookie protection and gain kernel arbitrary code execution capability. But compared to... To kernel arbitrary kernel memory reading, I think kernel arbitrary memory writing capability is often what we really want. And here is an example. CVE-ID 7155. And through it, I find more cases. Such as CVE-2017-7163. Let's take the... Let's take this one as an example to discuss the kernel arbitrary memory write vulnerability. The root cause of this vulnerability is that frame buffer driver lacks input validation and sanitization. And as you can see here, this instruction... Oh, it's a little... It's small. This instruction, move EDX to RAX plus RSI. This instruction is going to write value in EDX to offset of... To the offset of RAX plus RSI. And RAX at here is a base address of a kernel object. It's... And we can lock it... Through the arbitrary kernel memory read vulnerability. And we can control the ESI register at here. It's all a... We can control this register. This means that we can control the target memory address of arbitrary write primitive. And then, we can fully control the RD... EDX register at here. EDX is all controlled by... By attacker. This means that we can control the value to be written. The above conditions are perfect to achieve arbitrary memory... Arbitrary memory write arbitrary values. So, in my opinion, the quality of this vulnerability is pretty good. Yeah, but I still choose to report it to Apple last year. Yeah. Yeah. Because I'm not using my laptop, I'm unable to... Unable to make the demonstration. Yeah, I didn't take the video for it. But, trust me, this type of demo is boring. Just run the exploit and pop-up root shell. And... And the quality of this... Because the quality of this vulnerability is pretty good. So, it's very easy to write exploit code for this. I think you can give it a try. Yeah. The first part is... Yeah. Yes, here. From the NDPOC to the RD. Next part, I think, is more interesting. A key-mode framework and other projects I did last year. Last year, one of my tasks was to build a kernel monitoring system for our DLP project, Data Leakage Prevention Project. And when I went to study the existing kernel monitoring interface, I found that the building monitoring mechanisms were not very friendly for third-party development. Specifically, there are two building monitoring mechanisms available. Kernel authorization subsystem and mandatory access control policy subsystem. But the bad news is, is that they are not suitable for the current security-related kernel development tasks. The kernel authorization subsystem was introduced in the macOS 10.4 Tagger kernel in 2005. And the problem now is that this callback interface has a lot of benefits like the necessary maintenance and have not been upgraded for about 13 years. Yeah. For the scope file operation, number two, for the file operation listener, there are only seven file operation-related callbacks available, I think, which is not... which is obviously not enough. There are only file open, close, close, close, read write, rename, rename exchange, and the create process. But I think it's obviously not enough. And for the operation... file operation listeners, they are unable to block any file operations. It's a just notification. Just notification is unacceptable. See, we detect a ransom well and we can only watch it and we cannot block it. So if my endpoint security solution is this, I think my boss might fire me. And for some specific callbacks, the input parameter often lack critical context information. For example, for critical process callback handler, the input parameter is missing command line information. This is very important to us. And in the file operation callback handler, we cannot distinguish between new file creation and the open existing. It's also important to us. And for, we know the listeners, not every file system operation triggers an authorization request. This means that our monitor can be bypassed. It's also not acceptable. Yeah, compared with kernel authorization subsystem, monitor access control framework has a series of more granular callback interface, which are introduced into kernel from macOS 10.5. Yeah, 10.5. However, Apple quickly banned the third parties from using this interface and climbed that. This interface were not part of the KPI, a KPI. The KPI is short for kernel programming interface. This means that a monitor access control framework is totally private. Yeah, we cannot use that interface. But if you really want to use that interface in kernel, since we are all kernel extension, we have permission to lock it to the target function in kernel and then invoke it. Yeah, if you really want to use the interface in kernel, I think you will meet the following compatibility issues. I reviewed almost all the kernel open source code about the Mac policy. And I found this one, the following cases. The case one, case one shows that the interface were deleted or replaced directly by kernel. This is the case one, case one shows that the interface was deleted or replaced directly by kernel. This is unacceptable because the feature disappeared, disappeared directly. I cannot accept this. And case two, case two shows that the particles and the input parameters were changed directly. It's also unacceptable because my driver will panic. You cannot, not add a parameter directly into the prototype, right? And case three shows that the interface was inserted into the middle of the dispatch table. Yeah, my driver will panic too. And here's the case four. The interface has been rewritten, but forgot to upgrade the policy version number. So, my point is that, as a third-party developer, we have to use this mechanism very, very carefully. And in order to bring out some changes, I'd like to introduce you to Kimo, open source and open source pre and post callback-based framework for macOS kernel monitoring. Since there's no patch guard or similar kernel self-detection subsystem, I built the pre and post operation callback interface based on my kernel-inline-hook engine. By using this framework, I can add new features to any function I care about. The idea of the pre and post operation callback architecture is actually borrowed from Windows kernel. Generally speaking, in the pre-callback handler, I can field the input priming, and in the post-callback handler, I can reset the function's written value if needed. I have two examples here. The first one is a kernel extension monitor, or kernel extension firewall. I know there are similar functions in management access control policy mechanism, but I still want to use my own method to achieve it again. As can be seen, the kernel search is the same as before. As of now, the kernel extension service is just the same. I use the pre-callback handler. In the pre-callback handler, The first line is a pre-callback handler. In the pre-callback handler, I can filled the input parameters. It's small. Such as I can get the UID, I can get PID, parent ID, and the kernel extension name is com.maninet.monitor. And the kernel extension name is com.maninet.monitor. and also version number, module base, and module size. Yeah, in this case, I use a five-minute tour. It's named monitor.app as an example. The reason is just because it contains a driver and will load the driver into kernel. This is tour. Yeah, and in the post callback handler, in the pre-callback handler here, I'm using a disassembler to search the end point of the target driver, and I can patch the driver end point. So as can be seen here, the driver failed to load. And in the post callback handler, I can reset the function return value if needed. Another more interesting example in the mandatory access control policy is mandatory access control policy monitor. So are you wondering about which module in the system use the mandatory access control policy, and which policy set do they use? I think here is the answer, as can be seen, the first one. The first one I dumped from kernel is the AMFI. AMFI is short for Apple Mobile File Integrity, and it's all handlers of this module, including base address, module offset, and policy name. Yeah, as can be seen, AMFI, and this one is the sandbox. Mac sandbox registered a large number of Mac policies. So I think it's a good idea to use it. The tricky part of this feature is how to get the mutex lock. I mean, we need to hold the lock before accessing kernel data structures. But the policy lock, policy mutex lock is not exported. Fortunately, I finally found a way to lock that mutex lock. And you can review my code for more information. And in addition to enumerating and monitoring mandatory access control policy, KMon can also block or handrick arbitrary handlers, if needed. In this case, I still use the FireManagementToolMonitor.app as an example. The reason is, this tool will try try to register five amazing callbacks. The first one is for process monitoring. The second one is for dynamic library monitoring. The third one is for keyboard monitoring and it's file operation. Last one is kernel extension. And the keymon can block those requests. Here's an example. Yeah, demo. I demonstrated this at Black Hat Arsenal three days ago and I didn't make the video for it. Yeah, but don't worry. Talk is cheap. Show me the code, right? Definitely. I released all my source code. Yeah, please check out my source code for more detailed information. Yeah, in addition to the two application above, I also implement a kernel fuzzing helper by using keymon framework. I call it Keeper. Keeper can randomly inject errors, assist in recording the input parameters and call stacks of the graphics driver. For me, this feature are very helpful. As can be seen here, I hook the Intel FB client control do attributes and this is my fuzzing tool. A selector is random and the input data is also random. Yeah, and this figure shows that the input data is random data. And I can do bit flipping here. Actually, I also implement another project for monitoring IPC communication. Okay, okay. Yeah, the last part is discussion of zero-days and macOS kernel protection. Yeah, first let me show you a graphics driver related zero-day. I have a simple demo. This system has all the system patches installed. And... And after running my POC, the system crashed. It's just a reboot. And here is the call stack information. It's a heap of overwrite vulnerability. And the panic call stack shows that it's a victim thread. This victim thread is accessing the corrupted heap. And this is another crash thread. In fact, they are all victims of the vulnerability because the thread crops the heap is not theirs. And the root cause of this vulnerability is the following piece of code. Let me give you some background on this code. The input in the yellow color, the input is the length of my input tent data. And the length of the input data should be a multiple of 10. All numbers are in Higgs format. It should be a multiple of 10. And the general logic of this code is that it processed one round every 10 bytes. If the final length is less than 10 at here, if the final length is less than 10, the code breaks the loop. So can you find the bug in this code? I find the bug manually. The problem comes from this subtraction operation. If the input buffer length is greater than 10, this code is fine. But if the buffer size I passed in is less than 10, you can see here, less than 10, this subtraction will cause an integer flaw. And then the memory copy. Here, at here, will destroy the adjacent heap. And I submitted this vulnerability to Apple security this week. And I also met them this morning. I'm sure the vulnerability will be fixed soon. And to protect against the threat from the graphics drivers, I did some extra work based on the K-MOON framework. By using the kernel in my hook engine, I hooked up some key functions of graphics driver, such as do attributes interface. As can be seen in those handlers, I can filter the opening and the setting operations of untrusted domain. The untrusted domain is my, graphics further process. So which means I can reject some malicious requests. By the way, I'm very much hope that Apple can add the similar functionality to kernel through the mandatory access control policy and our D-trace interface. Yeah. Yeah. So this is my presentation. This is almost my presentation. We discuss the bug hunting, zero-day vulnerabilities, and the K-MOON open source project. And I also mentioned the third-party kernel protection and mitigation. I released all the source code. Please check it for more detailed information. Yeah, that's it. Thank you, guys. Thank you.