Spark 1 o'clock is from Xiaolong from Alibaba and Spark let's give them a big DEF CON help welcome, come on. Louder, I don't hear you. Louder. Yeah. Alright then. Thank you, thank you. Uh hello uh okay. Uh hello everyone uh I'm Xiaolong Bai from Alibaba. Uh this is Ming-Spark Zheng and um uh he's my colleague. And uh today we are going to talk about uh IOS security. Uh the title of our talk is uh HackPack, Hacking Ponto Authentication in IOS Userspace. Uh before starting our talk I'd like to have a self self-introduction. Um uh I'm from Alibaba and uh before joining Alibaba I have got my uh PhD degree in uh Tsinghua University and uh I have published several papers on uh Black Hat USA, Black Hat Europe, and uh The Big Four. Uh my Twitter account name is bxl1989 uh so welcome to uh follow me on Twitter. Uh my colleague Spark Zheng is uh is a PhD from uh COHK and uh he's now a senior security expert in Alibaba. Uh his Twitter account name is Spark Zheng. Uh this talk will be separated into several parts. Uh firstly I will have a brief e- a brief introduction uh of what uh the ponto authentication is. Uh and then I will show how ponto authentication is protecting IOS. Uh and then uh comes to the uh main part of this talk. That is we found a design flaw in the userspace ponto authentication. And uh with this uh design flaw I will show you how to do uh exploitation in the uh userspace of iOS. Uh I will show you how to do uh exploitation in the uh iOS in this PSE era. Uh and and at last I will show you uh a tool we designed to uh to use basic exploitations uh which is called PSE gadget. So what is ponto authentication? Uh ponto authentication is uh an extended feature of ARM. Uh it is available in uh a trial uh which is the call of iPhone XR, XS, and the XS max. Uh it protects pointers with crypto graphics signatures. Uh how does it work? Uh it works by introduction of two uh instruction sets which are called uh PSE instruction. Uh PSE instructions and uh IoT instructions. Um PSE instructions calculates uh PSE instructions calculates uh the PSE instruction uh is using calculate a kit hash of the pointer uh which is named the PSA and uh inserts the PSA into the pointer. And the AOT instructions ex- extract the PSA from the standard pointer and authenticate whether the PSA is consistent with the original pointer. Uh you can see from the figure that when uh PSA instruction is uh executed on uh uh pointer value the uh PSA value will be uh inserted into the pointer uh like the red one. Uh in PSA and AOT instructions there are two main factors uh they are called the keys and the contacts. Uh uh the PSA instructions work uh work as follows. Uh it it it will use a key register and a contacts register to sign uh to calculate the uh the uh the uh the uh the uh the uh the uh the uh the key register and then demonstrate uh uh PSA value from the register XD. And uh then it will uh insert the PSA value into the register XD uh which then forms a uh sign pointer. And uh for AOT instructions uh these instructions uh extract the PSA value from uh the XD register. And uh then it also use the key register under the contacts register to verify whether the PSA PSA value will beест przysz. is consistent with the original uh pointer value. Uh if the verification succeeds uh it will use the uh verified pointer value to do jump or load. Uh if the verification uh fails uh it will insert a error code into the uh into the into the into the pointer uh so the pointer cannot be used for further use. Uh there are five key registers in uh in in in in the CPU. They are called A I key, B I key, A D key, B D key, and a G key. Uh here the I keys means uh is used for instruction pointers uh sign and a verification. And the D keys are used for data pointers. Uh these key registers are only readable. And the readable on exception levels larger than uh ER1. Um uh different from the key registers, contact registers are only uh general registers. Uh they are used to prevent intro process pointer uh substitution attacks. Uh that is uh when we send the same pointer value with the same key but a different context value. We will result in different PC values. Uh as a result uh for example uh pointer uh signed with context A uh with a field verification with context B uh even in the same process. Uh but you should you should remember that there are some instructions just using uh null pointer uh null null null context uh I mean the the context is all zero. Um these are the uh the uh the uh the uh the uh the uh the list of uh uh uh supported uh PSA and uh AOT instructions in the ARM. Um so we know uh what the uh we we have a brief re-understanding of the pointer authentication. So how pointer authentication is protecting iOS. Uh let's answer this question. Uh pointer authentication uh uh reduced uh the threat of memory corruption by preventing code reuse attacks. Uh what is memory corruption vulnerabilities? Uh it is one of the most frequently exploited vulnerabilities in iOS user space services and the kernel. Uh typical memory corruption vulnerabilities include uh userfree and the buffer overflow. And when uh we have uh memory corruption vulnerabilities we always use the code reuse attacks to uh fix the code, uh to do a exploit these vulnerabilities. Uh code reuse uh attacks uh always utilize the gadgets that already present in the program or in uh shared libraries uh to do malicious things. Uh typical code reuse attacks include a return oriented program which is also called ROP and a jumper oriented program which is called uh JOP. Uh the difference between uh ROP and JOP is that ROP in in ROP gadgets always end with uh write instructions. And uh for JOP gadgets uh are often uh ended with uh BRR and BR they are jump instructions. Uh a sample buffer overflow is like like this. Uh uh from this code uh we can see that in the overflow function uh there is a uh mem copy. Uh there is uh a mem copy. Uh there is uh a mem copy. Uh there is a uh function called. Um but the mem copy function uh does not uh before the mem copy function called uh this function does not check uh whether the uh buffer size is uh lower than is lower than zero zero x uh four thousand. Uh when we encounter this vulnerability how do we exploit them? Uh uh we we exploit it uh it like follows uh we can write more buffer uh more data into the buffer array and uh uh the uh the more we uh the data we write we overwrite uh the area in victim array. Uh that is we may uh override the uh uh function ponder. Uh in the victim array uh which is called a dummy. And uh then uh by uh by giving dummy uh uh arbitrary uh address we can force the uh victim process to jump to uh other uh locations. And from those locations we can use ROP chain, ROP gadgets or JOP gadgets to do malicious things. And uh here the pointer authentication uh is going to prevent uh ROP and JOP uh but how? Uh it prevents ROP and JOP by replacing dangerous instructions in the uh in the gadgets we we see. Uh for ROP uh uh uh ROP for for ROP uh pointer authentication replaces the write addresses with uh write instructions with uh write AB instruction. Uh write AB instruction ensures that the return addresses uh was correctly signed. So the signed return uh uh addresses will be protected by the key and the context. And for JOP uh BRR and BR is now replaced by uh BRRAA and BRAA. Uh these instructions ensure that the function pointers uh we are going to use to jump was correctly signed. Uh so the now the signed pointer function pointers is uh protected by the key and the context. Uh so now the instruction uh w- by replacing uh this uh write and the BRR BRR uh instructions uh we can prevent ROP and JOP. Uh even if the PSA uh even if we in um uh we can prevent ROP and JOP. Uh even if the PSA uh even if we uh we can prevent ROP and JOP uh use but we we can prevent ROP and JOP. So while this the pointer authentication uh since both the key and the contacts are unknown to the attacker it can successfully uh prevent ROP and GOP attacks. Sorry. Uh so for now uh when we talk about iOS attacks on the GOPrix uh we we we know that the iOS uh attacks on the GOPrix has come into the uh PSL. Uh in previous GO GOPrix ROP and GOP was the first step to exploit uh user space demons and the kernel. But now uh ROP and GOP attacks are prevented by uh pointer authentications. Uh instruction pointers are now sent everywhere. Uh so uh what can we do now? Uh this talk will only focus on uh pointer authentication in the in the user space. Uh in the uh this means uh when we talk about a user space attack scenario we mean that the attacker is a local application on the same device with the uh with the target system service demons. Uh and for uh security analysis on kernel pointer authentication please refer to uh Brandon's blog. Uh so we we know that the pointer authentication should be very strong but uh um for now we have found some weak points in the pointer authentication in user space. Uh the foundation of uh successful pointer authentication protection uh should be that uh the attacker cannot fake send pointers uh that can be correctly verified by the target process. Uh uh the uh this is based on tool uh tool uh basic knowledge is that uh we should uh we know that the sign key is unique for different uh processes and the context is also very private in different process. So uh without knowing the sign key and the context the attacker cannot create uh uh correctly signed pointers. But if the sign key and the context uh are both private then uh the uh identical in two different processes. Uh inter-process uh pointer substitution attack uh would be possible and uh pointer authentication protection will fail. So here comes the question, is iOS correctly protecting sign key and the context? Uh what if it- it fails to successfully protect us? So let's do some experiments. Uh with- our experiments is set up as follows. Uh we developed two programs called program A and program B. Uh they are developed by different develop- developer accounts. Uh and uh we sent a pointer in pro- pro- progress A and verified in progress B. With all the file key uh uh the uh uh uh uh uh four keys. With all the keys and the- and the two contacts uh the two contacts are zero contacts or uh zero x one two three four. So uh what is this experiment for? We want to track whether the pointer just signed in program A can be correctly verified in program B. If it can be correctly verified uh it can be verified in progress B. If it can be correctly verified in progress B, it should uh uh indicate that the program A and program B are using the same key. So if- if this is true, uh there is a problem. Uh let's see. So the experiment result- results show that uh uh the pointer is signed by AI key and uh AD key uh uh in program A. can be correctly verified in program B. So that's very uh surprising. Uh it seems that iOS is using the same keys in different user space processes. But it is more important to know whether the result is is also true uh for user space system demons. Uh we did the experiments on our own developed programs but uh we should know whether it is true for uh system demons. Uh we can answer this question by checking uh whether a sign pointer in a service can be correct uh can be correctly verified in our own program. Uh so how to retrieve a sign pointer from a user space demon or system service. Uh here we use uh denial of service vulnerability uh our system service to retrieve a sign pointer. Uh the the the basics here is that uh when a process uh encounters uh uh denial of service vulnerability it uh it will crash. And when it crashes uh it's register values are included in the crash log uh which may contain some sign pointers. We retrieve those sign pointers from the crash log. Uh uh the uh the uh the uh we found a null pointer dereference uh vulnerability in the service uh in the service logd. And uh which is in iOS uh 12.1. This null pointer dereference vulnerability will cause the logd service to crash. Uh and uh we know logd s- service is un-unboxed and uh run- running as a root. Uh we found a null pointer dereference vulnerability in the service uh in the service logd so there is this vulnerability. Uh logd provides a uh IPC service which is called com dot apple dot logd uh the s- IPC service is implemented in the uh uh in the source code fair-host uh underscore uh server dot c. And uh in the uh source code uh we can find that uh uh fair host server handle MAC event is the IPC handler. And uh from this handler uh it calls Firehose Server Demux. And the Demux dispatch the IPC request to different methods including Firehose Server register, Firehose Server push sync and Firehose Server push and wait. And uh during this process it creates uh a global variable called the current client info. Um but it passes this uh global variable to different methods uh without checking whether the uh global variable is null. So in in in those dispatched methods it just trusted uh uh uh the current uh client info variable. Uh uh the uh the uh the uh the uh the uh the uh the uh the uh the without checking whether it is null. Uh when it use it it will cause a null point dereference. Uh and it crashes the log D demon. Uh by triggering this vulnerabilities we can get a crash log like followed. In a crash log we can we can see that the existing register seems like a signed pointer. And uh uh by reverse engineering log D we can find that the uh uh instruction has just uh been executed before the crash. Uh the the instructions like this B R A X uh 16 uh X 17. This instruction uh verifies X 16 with AI key as the key and the X 16 as the context. Uh from the crash log we know that we know the value of X 16 and the X 17. And uh we get these uh register values and uh use these these register values to uh verify them in our own process. We try to verify X 16 by AUTIA instruction in our own process. Uh uh in this way we are using the uh our own AI key as a as the key and the uh uh the previous X 17 value as the context to verify X 16. And the re- result shows that the verification succeeds. So uh this re- this experiment is has the same result as the previous experiment. That means iOS is really using the same AI key in our own process as the under the system services. Uh this is a flaw. Uh but it it should not be a problem because it should not be exploitable uh as I have said that the context uh register is introduced to to uh prevent a pointer substitution attacks. And the attacker should not know the context in another process for example the X 17. Uh but did you forget that uh I mentioned uh there are some instructions that just use the zero context or no context. So uh this is a very simple context. Uh these instructions link to the B R R A A Z and the B R A A Z. Uh when you use the null context or zero context uh the context is not do uh is is is not doing anything to help you to protect the signed pointers. So what does that mean to an attacker? Uh that means that a local attacker can create a signed pointers in his own process. And the way this is done is when the, uh when the pointers are used in another system service process uh they will pass the verification of the B R R A A Z and the B R R A A Z in the system service. So uh the J O P is alive now. And that means we can use uh we can we we we can still use the uh gadgets that are on the B R R A A, B R A B R A B R R A A A and the B R R A A Z. Uh so more of the easy or B-R-A easy to uh to find the GOP gadgets and chain uh GOP gadgets to do malicious things. Uh but how about ROP? Uh we know that uh ROP uh gadgets are now ended with uh should now be ended with our uh write AB. Uh but in this uh in these instructions uh return addresses are now signed uh by uh by the B key and uh and uh with with uh but the B key is unique in different processes. Uh and uh we also know that uh our uh write AB uh the context of write AB is SP and also uh private in different processes. So ROP uh has no chance. Uh so we know that there is uh uh key manag- key management problem in iOS user space uh which is uh this uh we know A key is a per device key. Uh it is renewed at uh each device reboot and all processes are using the same A key. And the B key is a per process key. It is renewed when a process restarts and the different processes are using the different uh B keys. Uh what is the root cause of this key management problem? Uh the root cause is is a trade-off between the performance and the security. Uh let's consider the situation that the function pointers, there are function pointers in shared libraries. Uh we we know that the shared la- memory regions of shared libraries are copy on write. Uh so if the pointers are signed by different keys. Uh we know that the memory regions of the shared libraries are uh in different processes. It will cost uh more time and uh more memory space to create a new new process. Uh in order to copy the shared libraries. Um like this figure shows uh if we sign the pointers with the same key in different processes. Uh when we fork or create a new process the shared li- the share- shared libraries uh do not need to be signed by the same keys. Uh need to uh be uh copied uh at the fork. But if we sign pointers with different key in different processes uh we have to uh create a new copy of the shared libraries when we fork a process. So uh the actual copy will cost um more time and uh more memory space. That's um uh a lot of performance loss. So Apple uh has no trust but to use the same key between different processes. This is a design issue uh which is inevitable. Um with this issue uh um let me uh let's have a look at how to do uh exploitations in the uh user space right now. Uh let's have a look. Uh here I will use a real uh iOS user space vulnerability to show how to do uh exploitations in iOS user space uh demons. Uh this is uh a vulnerability in the uh demon called the media library D. Uh media library D provides a NSSPC service which allows sandbox applications to execute uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh SQL commands on a SQLite database. Uh the XPC uh function is called execute uh query uh in the left figure. Uh in the right figure uh you you can write uh okay you you you you can write the code like like like write to uh execute some uh SQL commands on a uh on a on a SQL database. Uh SQLite has a very interesting feature uh which is that the users can create a customized token uh token tokenizer implementation and specify the tokenizer to be used uh when indexing uh text. So how to create a customized tokenizer implementation? Uh we can provide a uh address in the SQL command and the SQLite will retrieve a function pointer from the address. And the function pointer uh is exactly the tokenizer implementation. Um uh you can see from the uh figure that uh we uh create a uh tokenizer called a my tokenizer uh which uh which is in the address four one four one four one four one. And uh we use the my tokenizer to uh create a table. And uh when this secure uh is uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh the commands are executed. Um the SQLite will uh try to uh find a function pointer which is indicated in the four one four one four one uh address. Uh and uh use that function pointer to uh call function. So so when those uh securing commands are executed, media library D will try to uh invoke a uh customized tokenizer uh from a function pointer uh like follows. Uh this is the uh the the disassembled uh instructions. It will try to load a uh function pointer from uh x twenty four uh plus eight. And uh the x uh the the function pointer should be a signed function pointer and installed in x eight. And uh media library D will call the signed function pointer uh by B-R-R-A-A-Z uh which is installed in x eight. So uh here the attacker can control the x uh twenty four. As a result the attacker can control x eight and further hijack hijack the PC in media library D. So uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh how to further exploit this vulnerability. Uh we we know that uh now uh x eight is under our control. Uh then we can use keepspray uh techniques to uh to to punt the x eight to uh to to punt to the x eight to uh uh uh uh uh arbitrary address. Uh let's give a look at Uh how to uh sorry. Here we know that x8 is a sign pointer and uh we created the sign pointer in our all process and uh put the sign pointer in the heap3 payload and then when we hijack the PC through the x8, the x8 will will go through the uh sign pointers we uh set in the heap3 payload and then from the sign pointers we can do JOP. Uh we we we can use many JOP gadgets to do malicious things. Uh as an as an example I will show you uh how to print a message in the media library this uh service. Uh we use a JOP gadget to call NSLog. Uh uh the first thing we need to do is to find a JOP gadget uh uh to call the NSLog. Uh the first thing we need to do is to find a JOP gadget uh to call the NSLog. Uh we found a JO a uh gadget in the SQLite uh DAO uh DEVALEAP. Uh this gadget uh sites uh uh sets x x0 and uh also uh retrieval function pointer uh from the x24. Uh so we can now control x24, x8, x0 and uh we can call uh then we can call x uh SSLog. Uh and then we can call the uh the x24. And log from the x8 and we set the x0 uh to be a string structure. Uh so now we can print a log uh in the media library D. Uh that is uh a more specific uh detailed uh uh uh graph of uh what what I have talked. Uh we uh first uh control the uh x8 uh to point to uh uh uh the purple one. And uh the purple one will lead to uh the uh right gadget. And uh the gadget will uh further retrieve the function pointer uh in the green one. And uh uh also x x0 uh from the orange one. And then the green one will lead us to the s log. So we can successfully ma- make uh uh uh GOP exploit in the media library D. And uh from the uh log we can see that the media lib- media library D is is now controlled by us and uh print uh uh a message like this hello you are exploiting. Uh next uh I will show that uh in order to do uh GOP uh attacks in the PS error. Uh we have developed a uh new tool which is called the PS gadget. Uh why we uh uh want to build this tool. Uh we build this tool because that manually looking for GOP gadgets is very tedious. Uh so so this tool uh so this tool will be used to will be used to to automatically find the uh GOP gadgets in uh binary. Uh so this tool will be used to uh will be used to uh uh in the binary is protected by pointer authentication. And uh we develop a PS gadget as uh idea pro plug in. Uh the basic work flow of PS gadget is uh is like follows. Uh we first find the BRRAA, BRAA, BRRAAZ and the BRAAZ instructions in the uh in the binaries. And uh then we get uh and record a new instruction in the backwards order. And we check whether the instruction will affect the control. Uh if it is true uh we go to the final. Uh uh if if it's not true we go to the uh second and uh we we we get a new instruction in the backwards order. Uh so in this way we can get uh a few code sequence uh which uh you know which is under the ways those uh BRRAA or instructions. And uh this instruction uh this this code sequence can be used uh in our GOP gadget. And also PS uh PS uh gadget will further examine uh each instruction in the gadget uh in in the code sequence we have found. Uh it will check uh whether the branch target uh where the branch target is from. And uh what each registers uh can be influenced uh in this this code sequence. Uh it will also generate a a short description of what the registers have been uh you you you can control uh in this code sequence. Uh so uh we can there is a special kind of uh GOP gadget which is called dispatcher gadget. Uh dispatcher gadget uh re repeatedly uh invoke other gadgets or call functions in a while loop. Uh the feature of dispatch gadget is that the loop body will load a uh function pointer from the address which is indicated by uh uh base register plus uh uh the uh uh uh uh uh uh uh uh offset. Uh uh the this offset is updated uh by a look counter. Uh with the high powerful PS gadget we automatically uh find uh such a uh dispatcher gadget uh in libc++. Uh you uh you can see from the this this figure that uh you can see from the this this figure that uh in this gadget uh the gadget will uh retrieve the uh function pointer from uh x19. And uh the fun uh the the the function pointer will be further used to call other gadgets. And uh we we can loop us through the through the gadget. Hmm. So uh the uh the uh the uh the uh function pointer is uh a function pointer b. That's how we can uh x19. So now with the uh dispatcher gadget uh we can uh do arbitrary code execution in user space system demons. Uh so next I will conclude out um this talk. Uh in this talk uh uh we can have a basic understandings of what uh pointer authentication is and uh how pointer authentication works and uh how pointer authentication works and how pointer authentication works and uh how pointer authentication how it is protecting iOS user space daemons. Uh more importantly I have shown that there is a design flaw in iOS user space pointer authentication. That is iOS using the same key in different user space processes. And uh I show that uh attacker can leverage this flaw to launch JOP uh attacks in user space daemons. And uh I use uh a real exploit to then demonstrate the JOP attacks in pointer authentication protected uh processes. And finally I have shown a tool to automatically find the JOP gadgets in uh pointer authentication protected banners. And here are some uh references. Um Brandon has uh Brandon has already uh uh do some exper- uh research on the uh pointer authentication issues in the kernel. And uh Ian Bearer has also uh do some experiments uh of pointer authentication in the user space. Uh okay. And uh um Protis and uh XRub has developed several tools to help you understanding what the pointer authentication is and how how you can inspect uh pointer authentication instructions in banners. So that's all thank you. Uh if you are interested in my talk uh we can have a further discussion and uh please follow me on Twitter. Thank you very much.