1 00:00:00,000 --> 00:00:03,180 And with no further ado, please continue. 2 00:00:07,880 --> 00:00:16,520 Hello, everyone, and welcome to my talk, Clash, Burn, and Exploit Manipulated Filters to Pwn Kernel CTF. 3 00:00:17,140 --> 00:00:22,960 I'm HexRabbit from Taiwan, currently working as a security researcher at DefCon. 4 00:00:22,960 --> 00:00:27,760 I specialize in binary exploit, especially in Linux kernel exploitation. 5 00:00:27,760 --> 00:00:35,020 I found some vulnerabilities in Linux kernel components like ksmbd, nstables, and others. 6 00:00:36,180 --> 00:00:41,160 Today, I will start from introducing kernel CTF and nstables. 7 00:00:41,160 --> 00:00:48,680 After that, we will dive into the nstable internals and talk about three vulnerabilities we discovered in nstables. 8 00:00:49,140 --> 00:00:57,140 And in between these three vulnerabilities, I will also share some story behind the scene during my kernel CTF journey. 9 00:00:57,740 --> 00:01:02,420 So, let's first talk about kernel CTF. 10 00:01:02,420 --> 00:01:08,760 Kernel CTF is part of Google VRP, and the way it works is quite similar to a CTF challenge. 11 00:01:08,980 --> 00:01:15,820 For about every two weeks, there will be a new kernel version released, and attendees will be given an unprivileged show. 12 00:01:15,820 --> 00:01:23,600 And you will need to exploit the Linux kernel to get flagged and submit it to Google to earn a bounty. 13 00:01:23,600 --> 00:01:26,240 There are three instances that can be pawned. 14 00:01:26,240 --> 00:01:38,120 LTS and CLS both use newer and quite similar kernel config, while the mitigation one uses an older kernel config with custom security patch to accept more one-day exploits. 15 00:01:38,560 --> 00:01:43,440 But the most important thing is, Google offers pretty big bounty reward. 16 00:01:44,660 --> 00:01:53,460 With some simple math calculation, we can quickly find for a zero-day LTS exploit, the bounty can easily surpass $50,000. 17 00:01:53,780 --> 00:02:07,400 And with the same vulnerability, you can earn both $21,000 maximum in both mitigation and CLS instance, which is quite appealing for the bounty hunter like me. 18 00:02:08,460 --> 00:02:10,940 So, why am I targeting nftables? 19 00:02:10,940 --> 00:02:14,760 First, nftable config is enabled in kernel CTF. 20 00:02:14,760 --> 00:02:20,320 And as you can see on the right, there are multiple vulnerabilities discovered just after kernel CTF is announced. 21 00:02:20,320 --> 00:02:25,620 So, for kernel CTF newbie like me, I think it's the right place to start. 22 00:02:26,520 --> 00:02:31,260 Okay, so first we take a brief intro to nftables. 23 00:02:33,060 --> 00:02:35,520 What exactly is nftables? 24 00:02:35,520 --> 00:02:39,060 nftable is a package filtering framework inside Linux kernel. 25 00:02:39,060 --> 00:02:45,400 It's used to replace the legacy IP, ARP, EB tables, merge them into a single entry point. 26 00:02:45,400 --> 00:02:53,620 It uses a simple VM design to implement its functionality and support new syntax for CY interface. 27 00:02:53,620 --> 00:03:01,340 As you can see on the right, instead of doing things one by one, new syntax can now allow config multiple things all at once. 28 00:03:01,440 --> 00:03:06,480 And these capabilities come from its design, which we will revisit later. 29 00:03:08,740 --> 00:03:11,700 nftable is constructed by a tree-like structure. 30 00:03:11,700 --> 00:03:17,380 It contains five core modules, table, chain, rule, expression, and set. 31 00:03:18,160 --> 00:03:22,100 Table is the top-level container for the below structures. 32 00:03:22,100 --> 00:03:23,910 It's used for the measurement purpose. 33 00:03:24,840 --> 00:03:33,120 Each table will only belong to a specific neural family, and any object contained by the table will follow the same family. 34 00:03:34,340 --> 00:03:42,560 Chain in nftables will hook at predefined hooking point provided by net filter infrastructure to do the packet filtering. 35 00:03:42,800 --> 00:03:53,200 When any packet flows through the hooking point, the chain will execute the expressions in the row it managed to accept or to block or to modify the packets. 36 00:03:53,240 --> 00:03:56,760 So what is the rule and what is the expression? 37 00:03:58,280 --> 00:04:04,960 As I mentioned earlier, nftable implement its functionality by a simple virtual machine. 38 00:04:04,960 --> 00:04:14,100 So you can think a rule as a small function that can contain multiple expressions, and the expression is the instruction for nftables. 39 00:04:14,460 --> 00:04:25,640 There are different types of expressions that can be used to manipulate VN registers, and the others can be used to load or to read or write data within packets. 40 00:04:27,260 --> 00:04:34,860 And thus, set is used to store a group of some type, like IP address, port, or any binary data. 41 00:04:34,960 --> 00:04:45,180 It can also be used as a map to map some type A to another type B, and these data stored in the set can be queried by the expressions during packet filtering. 42 00:04:47,220 --> 00:04:54,920 Okay, so after having some basic understanding of nftables, I will now introduce the first vulnerability I discovered. 43 00:04:55,560 --> 00:04:59,220 At the time, I'm not so familiar with nftables. 44 00:04:59,220 --> 00:05:02,580 However, I spotted something weird in the code base. 45 00:05:02,860 --> 00:05:07,080 These three functions are all used to delete objects in nftables. 46 00:05:07,300 --> 00:05:23,100 The nftable deleteRuleDeactivate will be called during rule deletion, nftFlush will be called during full config flush, and nftSetElementCatchAllDeactivate will be called during set element deletion. 47 00:05:23,940 --> 00:05:27,160 However, the pattern here is a bit different. 48 00:05:27,160 --> 00:05:35,420 The first two use nftIsActiveNext to check if the object is active. 49 00:05:35,420 --> 00:05:41,960 But the last one, nftSetElementCatchAllDeactivate use nftIsActive to check. 50 00:05:42,000 --> 00:05:44,200 So what's the difference? 51 00:05:45,160 --> 00:05:51,960 Actually, these two functions are used for object lifetime management in nftables. 52 00:05:51,960 --> 00:05:59,920 In order to understand how it works, we will need to first talk about the batch request mechanism. 53 00:06:02,300 --> 00:06:13,840 For a query request, for example, the getRule operation above, Netlink API will internally use netlinkReceiveXKB to get the raw information loglessly. 54 00:06:14,340 --> 00:06:27,640 However, if it's some operation that will modify the control plan, like new table or delete chain, nftables will use nfnetlinkReceiveBatch instead of the logless one to serve the request. 55 00:06:27,640 --> 00:06:36,820 The difference between them is that batch one can process a request which contains multiple operations, and they will be processed sequentially. 56 00:06:37,680 --> 00:06:44,260 And also because of this design, the COR interface can copy multiple things all at once. 57 00:06:44,260 --> 00:06:54,920 So it sounds great, but the thing becomes more complicated when any operation fails in the middle of a batch request. 58 00:06:55,420 --> 00:07:01,460 And what nftables do is to split batch request handling into multiple phases. 59 00:07:03,140 --> 00:07:07,800 There are three phases during the handling of the batch request. 60 00:07:07,800 --> 00:07:11,500 Prepare phase, commit phase, and abort phase. 61 00:07:11,500 --> 00:07:20,400 During the prepare phase, if every operation finished successfully in the batch request, it will go to the commit phase and commit the changes. 62 00:07:21,300 --> 00:07:29,200 However, if something went wrong, after every operation is processed, it will go to abort phase and revert all the changes. 63 00:07:29,780 --> 00:07:32,140 Here's the lifetime management's consign. 64 00:07:33,840 --> 00:07:39,920 Any object in nftables has two state variables, current generation and next generation. 65 00:07:39,940 --> 00:07:46,600 Only the object activated in current generation can affect the runtime behavior. 66 00:07:46,860 --> 00:07:51,440 And you can think the next generation as a temporary state for the prepare phase. 67 00:07:52,040 --> 00:08:01,440 Every operation modify the control plan, like new or delete operations, will only make changes on the next generation state in the prepare phase. 68 00:08:01,440 --> 00:08:04,840 So without affecting the current generation state. 69 00:08:04,840 --> 00:08:10,740 Then, during the commit phase, it will commit the modification. 70 00:08:11,040 --> 00:08:19,880 So the current generation will finally be set as the next generation, and the next generation state will be reset to a clean state. 71 00:08:20,480 --> 00:08:28,320 But in the case if something went wrong during the abort phase, it only needs to clean the next generation state. 72 00:08:28,780 --> 00:08:32,200 Let's go through some examples to know how it works. 73 00:08:33,380 --> 00:08:38,340 After a batch request is received, it goes into the prepare phase. 74 00:08:38,860 --> 00:08:42,120 The first operation is delete set C. 75 00:08:42,220 --> 00:08:47,880 Okay, so the set C's next generation state will be set to deactivated. 76 00:08:49,080 --> 00:08:54,200 Next, it continues to process a set D operation. 77 00:08:55,120 --> 00:09:00,260 So the set D will be added into hash table with next generation in activated state. 78 00:09:02,400 --> 00:09:11,860 If nothing went wrong, we will go to the commit phase, and at this phase, current generation will be set as the next generation. 79 00:09:12,340 --> 00:09:19,620 So the set D will be available in both runtime and query operations, and also the set C will be free. 80 00:09:21,880 --> 00:09:33,100 If something went wrong, we will enter the abort phase, and at this phase, all operations done in the prepare phase will be reverted in a reverse order. 81 00:09:33,380 --> 00:09:39,500 So the add set D will be undue, and also the delete set C will be undue. 82 00:09:42,660 --> 00:09:44,520 So okay, back to the check. 83 00:09:44,780 --> 00:09:46,740 So what's the problem here? 84 00:09:46,980 --> 00:09:55,900 As we can see, during the NFT set element catch all deactivate, it used NFT is active to check if the element is still alive. 85 00:09:56,020 --> 00:10:07,520 However, NFT is active checks current generation, but the function actually runs in the prepare state, which will only modify the next generation state of an object. 86 00:10:08,020 --> 00:10:19,460 So the check here will always pass, and by request multiple delete operations, we can free the element multiple times in the commit phase. 87 00:10:20,160 --> 00:10:34,240 So the batch request to trigger double free will look at this, since the lifetime check will always pass if the element exists during the prepare phase, two delete set element of operation will both execute successfully. 88 00:10:34,320 --> 00:10:43,080 And so when you get to the commit phase, the same element will be free twice, and we get a set catch all element double free. 89 00:10:44,160 --> 00:10:48,070 Great, so I found my first bug for current CTF. 90 00:10:48,070 --> 00:10:58,950 But the next day, I found the maintainer also find the same bug and send a patch to kernel mailing list. 91 00:10:58,990 --> 00:11:00,550 I mean, why? 92 00:11:00,890 --> 00:11:12,970 So, okay, this means my zero day is officially turns into a one day, and according to kernel CTF rules, my bounty will decrease by 20,000 USD. 93 00:11:13,330 --> 00:11:14,330 Ouch. 94 00:11:15,610 --> 00:11:24,410 Also, one day vulnerability means that everyone will know how to trigger the vulnerability, and they may also come up with some exploit. 95 00:11:24,410 --> 00:11:29,370 So I definitely need to exploit it before next kernel CTF slot opens. 96 00:11:31,940 --> 00:11:45,980 So after we get the double free primitive, we reuse the exploit method for CVE-2023-4004 to overlap the MFT table user data and MFT object to leak address and control IP. 97 00:11:46,100 --> 00:11:56,440 Due to a time limit of this talk, I won't expand the detail of it, but kudos to Lonely and Calm who discovered this technique and shared it on kernel CTF repository. 98 00:11:57,560 --> 00:12:03,600 So now we develop the exploits and wait for the slot to reopen. 99 00:12:04,560 --> 00:12:08,620 Fast forward to two hours before the competition starts. 100 00:12:08,620 --> 00:12:16,080 I nervously check if there's any kernel updates related to NF tables, and guess what? 101 00:12:16,260 --> 00:12:24,200 The patch previously posted on the mailing list has been merged into the kernel, and it added one line on it. 102 00:12:24,200 --> 00:12:39,900 It says reported by Lonely and Calm, which this means is the patch may actually come from a zero day kernel CTF submission, and since the reporter is Lonely and Calm whose exploit I just used, this is indeed the case. 103 00:12:40,180 --> 00:12:46,540 So just before I have the chance to exploit an official instance, I found it's a collision. 104 00:12:48,040 --> 00:12:58,760 So yes, I still found the instance and report the flag, but too bad for me, the first bug do collide with an old kernel CTF submission. 105 00:12:59,600 --> 00:13:01,780 That's how I look after the competition. 106 00:13:04,860 --> 00:13:12,700 Okay, so I take some break, and after three weeks later, I found the second bug. 107 00:13:14,160 --> 00:13:18,280 During the research, I came across an interesting commit. 108 00:13:18,380 --> 00:13:22,880 NF table validate family when identifying table by handle. 109 00:13:23,500 --> 00:13:30,800 The commit is added after the mitigation instance kernel version, so it can be used upon the mitigation instance as a one day. 110 00:13:31,300 --> 00:13:41,440 It added a check on NFC table lookup by handle function, which is used for searching a register table by the provided handle. 111 00:13:41,940 --> 00:13:52,740 Without the commit, during the table lookup, we can provide a family different from the one which table belongs to, but what can we do with this inconsistency? 112 00:13:54,160 --> 00:13:59,360 Let's trace where the family parameter comes from and where it goes. 113 00:13:59,360 --> 00:14:10,020 NF table lookup by handle is only code from NF table delete table, and this function is used to delete user-specified table from NF tables. 114 00:14:10,140 --> 00:14:22,500 The family variable is directly get from user-provided input and is saved into context family and passed to NFT flush table function. 115 00:14:23,640 --> 00:14:30,800 In NFT flush table, context argument is passed into every function handling object deletion. 116 00:14:30,800 --> 00:14:44,520 For me, trace which function use the context family is quite annoying, so instead of just tracing into each function, I take the shortcut to trace where it is used. 117 00:14:45,060 --> 00:14:50,420 So I just grab the whole code base to find where the context family is used. 118 00:14:52,460 --> 00:15:03,480 Grabbing context family gives us a lot of result, most of which comes from the NF table expressions, like count limit, net, T proxy, and et cetera. 119 00:15:03,900 --> 00:15:11,660 However, most of them cannot be used for the exploit, but I finally found one that can be exploited. 120 00:15:12,040 --> 00:15:14,900 It is in the NFT log expression. 121 00:15:16,700 --> 00:15:20,640 The trigger path I found starts from the NFT log destroy. 122 00:15:20,640 --> 00:15:25,580 This function takes context family as an argument and code to NF log output. 123 00:15:26,660 --> 00:15:33,210 Now it's becoming the PF argument and used as an index to de-reference the loggers array. 124 00:15:34,340 --> 00:15:40,440 The value range of our input context family and also the PF here is zero to 255. 125 00:15:40,460 --> 00:15:49,060 But if we check the size of loggers array, we can find it only has 13 elements in the first dimension. 126 00:15:49,440 --> 00:15:54,120 So we get the global array out of bound access here. 127 00:15:54,260 --> 00:15:56,460 But what can we do with it? 128 00:15:58,160 --> 00:16:04,500 After taking the logger out, it de-referenced the me field and feed it into the module put. 129 00:16:05,440 --> 00:16:11,740 The module put will again de-reference and decrease the refcon value in the module object. 130 00:16:12,220 --> 00:16:17,840 With this vulnerability, logger can be any pointer store adjacent to a loggers array. 131 00:16:17,840 --> 00:16:23,080 So we may cause some type confusion here and decrease some variable by one. 132 00:16:23,320 --> 00:16:27,420 However, we are dealing with an awkward situation here. 133 00:16:27,420 --> 00:16:34,480 In order to utilize this bug, we have to find a pointer store on global and can be de-referenced twice. 134 00:16:35,160 --> 00:16:41,780 The first is to take our logger me and the second is to take our module refcon. 135 00:16:43,640 --> 00:17:03,720 To sum up what we have by now, first, the maximum value of PF value we can control is 255 and we can trigger an out of bound reference count decrement by using a PF value larger than 12, which will execute like the code here. 136 00:17:04,140 --> 00:17:11,240 To exploit this, the first thing we need to do is to find a global pointer placed near the loggers array. 137 00:17:12,340 --> 00:17:14,400 So what do we have on the global? 138 00:17:14,920 --> 00:17:23,540 By inspect the marrying GDB, we quickly find there's an NFCT expect hash placed near the loggers array. 139 00:17:24,120 --> 00:17:25,920 And it's a hash table. 140 00:17:25,920 --> 00:17:33,560 So in this case, it seems to be a good target since the pointer point to an array of pointers. 141 00:17:33,560 --> 00:17:36,300 Therefore, it can be de-referenced twice. 142 00:17:38,320 --> 00:17:42,060 So let's check how the code normally works. 143 00:17:42,540 --> 00:17:51,860 First, it gets the me field at offset 1x and then get the refcon at offset 340x. 144 00:17:52,880 --> 00:17:56,280 Now let's check the out of bound access case. 145 00:17:56,280 --> 00:18:01,580 First, we set PF to 19 to get a hash table pointer, NFCT expect hash. 146 00:18:01,960 --> 00:18:07,180 The first de-reference we got is the index tree entry of the hash table. 147 00:18:07,660 --> 00:18:13,500 The entry points to the HNO field of a contract expect object. 148 00:18:13,580 --> 00:18:19,900 So after one more de-reference, it will access offset 340x down below the expect object. 149 00:18:20,240 --> 00:18:26,280 But since the expect object only has a D at size, it will access out of bounds. 150 00:18:27,100 --> 00:18:33,600 Fortunately, all NF contract expect object allocated in a dedicated Cayman cache. 151 00:18:34,420 --> 00:18:40,040 So the memory space below will also contain NF contract expect objects. 152 00:18:40,920 --> 00:18:46,860 So it will access to an RCU field and decrease the next pointer in the structure. 153 00:18:46,860 --> 00:18:50,680 But what does this next pointer use for? 154 00:18:52,140 --> 00:18:58,620 As we know, after RCU objects free, Linux kernel will not instantly free the object. 155 00:18:58,620 --> 00:19:05,180 Instead, it will hold the object for a grace period to wait for all other RCU readers to finish. 156 00:19:05,360 --> 00:19:14,080 So the next pointer in the RCU hash structure is used for temporary storing them and waiting for the RCU thread to process, to free them. 157 00:19:15,640 --> 00:19:22,400 So after the contract expect object are free, they will be like this, trained by the next pointer. 158 00:19:24,580 --> 00:19:31,720 And since we can decrease the next pointer in RCU head, we can make it point to another field inside the contract expect object. 159 00:19:33,140 --> 00:19:41,300 And to exploit it, we choose to land it on a user controllable field, which contains a union of IPv4 and IPv6 address. 160 00:19:41,440 --> 00:19:49,040 So when RCU callback triggered, it will read the fake function pointer we set, and we can control the IP. 161 00:19:50,660 --> 00:19:55,520 However, to actually exploit the vulnerability, we came across some roadblocks. 162 00:19:55,880 --> 00:20:04,240 First, we need the index tree of the hash table point to some expect object, so we can use it to do the out-of-bound write. 163 00:20:04,780 --> 00:20:09,760 But unfortunately, the hash value is unpredictable. 164 00:20:11,100 --> 00:20:19,480 Next, the out-of-bound written expect object needs to be in the grace period, so we can decrease its next pointer. 165 00:20:20,400 --> 00:20:27,220 But the next period is a small time window, and how to control when the kernel actually frees the object. 166 00:20:28,560 --> 00:20:34,600 Well, so long story short, we eventually found a way to exploit it by brute force and race. 167 00:20:35,900 --> 00:20:42,040 So to exploit this, first we use a tight loop to create a lot of expect objects. 168 00:20:42,800 --> 00:20:52,440 And at some point, the expect count exceeded the value max expected and start to evict the oldest expect object. 169 00:20:53,620 --> 00:20:58,260 So the oldest expect object will be placed into the RCU free list. 170 00:20:58,960 --> 00:21:01,980 And so on, and so on. 171 00:21:02,000 --> 00:21:08,180 And at some point, RCU thread gets scheduled by the kernel and starting to process objects. 172 00:21:11,160 --> 00:21:20,560 At this point, we add another thread to trigger out-of-bound decrement in a batch request and keep this system running for a bit. 173 00:21:22,520 --> 00:21:26,360 So we hope for a race scenario like this to emerge. 174 00:21:27,520 --> 00:21:38,840 First, the expect object allocated into index three of the hash table, and then the expect object we want to out-of-bound write is freed into the RCU free list. 175 00:21:39,220 --> 00:21:43,360 Then it continues to free another expect object. 176 00:21:44,420 --> 00:21:53,620 After that, we trigger the vulnerability 28 times in a batch request to make the next pointer point to a user-controllable value. 177 00:21:53,960 --> 00:22:00,360 And finally, the RCU start to process these free object and runs our set callback function. 178 00:22:01,860 --> 00:22:02,960 Great. 179 00:22:02,960 --> 00:22:04,940 Let's try it on local build. 180 00:22:05,020 --> 00:22:10,620 And we successful successfully control the IP to form, form, form, form. 181 00:22:12,220 --> 00:22:14,560 So what about remote? 182 00:22:16,440 --> 00:22:17,760 Let's try. 183 00:22:17,780 --> 00:22:22,680 And we got an error message while trying to add the lock expression. 184 00:22:23,080 --> 00:22:30,680 Since I have no idea what happened on the remote environment, I decided to download the official busy image to debug it locally. 185 00:22:32,360 --> 00:22:44,620 So I open the GDB and set some breakpoint on the functions, but when I set the break on nft-lock-destroy function, GDB complained that it was not defined. 186 00:22:44,620 --> 00:22:45,620 What's wrong here? 187 00:22:48,100 --> 00:22:56,460 I later found that it's because the kernel config I used is different from the one used in the official environment. 188 00:22:56,460 --> 00:23:09,520 So the log expression I used to exploit the vulnerability not even compiled into the official image, but I'm using the official build script to view the local test image. 189 00:23:09,520 --> 00:23:12,100 Why on earth there can be any difference? 190 00:23:14,380 --> 00:23:16,460 I mean, why? 191 00:23:19,540 --> 00:23:24,200 Eventually, I found it is caused by the official build script. 192 00:23:24,200 --> 00:23:32,080 The mitigation instance is built by COS kernel config plus the Google custom security security patch. 193 00:23:32,180 --> 00:23:38,600 And the official mitigation instance use COS config 6.1.55. 194 00:23:39,480 --> 00:23:48,220 The nftable expression are enabled in COS config after the version 6.1.64. 195 00:23:48,220 --> 00:24:01,820 However, the build script will always fetch the latest COS config, so the mitigation kernel I built has the nftable expression enabled, but in the official image, they're actually disabled. 196 00:24:03,780 --> 00:24:09,820 So, well, I think next time I should definitely check out the config first. 197 00:24:14,100 --> 00:24:22,840 Okay, so after my second failure, I revisit some old patch and I found a new bug. 198 00:24:25,170 --> 00:24:30,610 The patch I was looking at is a patch which introduced the first bug. 199 00:24:30,670 --> 00:24:37,930 The title says nftable, a set backend to use GC transaction API. 200 00:24:38,210 --> 00:24:40,570 So what does the GC mean? 201 00:24:40,990 --> 00:24:44,950 Here it means the set garbage collection. 202 00:24:45,110 --> 00:24:51,910 There are some set implementation support user to set a timeout on any set element. 203 00:24:51,910 --> 00:24:56,570 And there are two types of GC to clean the expired element. 204 00:24:56,570 --> 00:25:07,910 The first one, the synchronous one will be called in the commit phase, and the asynchronous one will be triggered periodically in the background. 205 00:25:08,370 --> 00:25:13,070 But here we only focus on the asynchronous one. 206 00:25:13,390 --> 00:25:15,770 So back to the patch. 207 00:25:16,370 --> 00:25:25,510 In the commit message, it says use GC transaction API to replace the old and buggy GC API and the busy mark approach. 208 00:25:26,190 --> 00:25:30,130 So what is the difference between the new and the old one? 209 00:25:30,390 --> 00:25:34,150 Let's take a look of the old GC design. 210 00:25:35,110 --> 00:25:42,150 In the old design, the synchronous GC will run loglessly and can be triggered at any time. 211 00:25:42,410 --> 00:25:54,190 So in order to protect GC threads from raising with the landing API, both of them will check a busy mark set on the element before delete it to prevent double free. 212 00:25:56,330 --> 00:26:01,670 But sometimes the developer might forget to check against it. 213 00:26:01,790 --> 00:26:09,110 In this case, there's a new function called NFT map deactivate forgot to check a busy mark. 214 00:26:09,270 --> 00:26:12,170 And this leads to a double free vulnerability. 215 00:26:13,410 --> 00:26:21,050 So in order to avoid this kind of issues, NFT developer decided to replace it with a new design. 216 00:26:22,250 --> 00:26:26,690 In the new GC design, it introduced the GC sequence. 217 00:26:26,830 --> 00:26:36,190 The GC sequence is a per net variable and it will be increased before and after any modification to the control plan. 218 00:26:36,210 --> 00:26:43,750 So it will increase at the beginning and at the end of both the commit and the abort phase. 219 00:26:44,390 --> 00:26:49,970 During a GC procedure, GC thread will record and check it to prevent raise. 220 00:26:50,270 --> 00:26:58,150 Even though the GC thread still run loglessly, during the check, it will now quad the commit mutex lock. 221 00:27:01,030 --> 00:27:03,630 The new GC design look like this. 222 00:27:05,010 --> 00:27:12,210 During batch request handling, GC sequence will increase both before and after the commit and abort phase. 223 00:27:12,330 --> 00:27:21,250 And in the GC thread, it will first run locklessly to store the GC sequence and collect all the expired elements. 224 00:27:21,530 --> 00:27:27,770 After that, it will quad the lock and check if the current GC sequence match the safe one. 225 00:27:28,170 --> 00:27:32,270 If they are the same, then the elements will be released. 226 00:27:33,350 --> 00:27:40,530 For this normal case, since GC thread does not raise with the batch request, the GC sequence will be the same. 227 00:27:41,230 --> 00:27:44,490 And the element can be freed by the GC thread. 228 00:27:47,960 --> 00:27:51,860 Next, we check if it still works in the raise case. 229 00:27:51,980 --> 00:28:10,680 If during the prepare phase, GC thread triggered and stored the global GC sequence into transfer objects, it will continue to run loglessly to find all the expired elements and wait for the mutex lock. 230 00:28:12,420 --> 00:28:21,340 At the same time, the main thread stepped into the commit phase, and some of the elements in the set can be released in this phase. 231 00:28:22,320 --> 00:28:33,220 The GC thread will need to wait until the main thread releases the mutex lock, so it can finally check the safe GC sequence and delete the element. 232 00:28:33,720 --> 00:28:44,360 But in this case, since GC sequence is recorded in the prepare phase, and we check it after the batch request is done, the GC sequence will be different. 233 00:28:44,540 --> 00:28:48,320 So, the element deletion will be skipped to avoid double free. 234 00:28:49,200 --> 00:28:54,540 Next, let's check the case if GC sequence is recorded in the commit phase. 235 00:28:54,540 --> 00:29:12,780 In this case, since GC sequence increased after the commit phase, the GC thread can only check GC sequence after the commit mutex unlocked. 236 00:29:13,900 --> 00:29:17,040 This raise will also be detected. 237 00:29:18,220 --> 00:29:27,640 So, to sum up, the new GC design requires both the batch request and the GC sequence check to hold the lock. 238 00:29:27,640 --> 00:29:37,580 This ensures that even there's a raise while collecting the expired element, the check will always happen after the GC sequence has increased. 239 00:29:40,670 --> 00:29:46,430 But does the batch request always lock mean there's no raise? 240 00:29:46,430 --> 00:29:55,390 Actually, if you look closely, we will find, well, it's not always locked. 241 00:29:57,940 --> 00:30:03,780 To find where it release the lock, let's first talk about the module autoload. 242 00:30:03,960 --> 00:30:20,300 During the prepare phase, if there's some expression type in the request is not found during the type resolution, it will instantly abort and try to load the type module during the end of the abort phase. 243 00:30:20,820 --> 00:30:25,160 After that, it will retry the whole batch request again. 244 00:30:27,920 --> 00:30:41,780 However, maybe there's some efficiency consideration or to just only prevent that lock before the function tries to load the request module, the mutex lock will be released. 245 00:30:42,480 --> 00:30:44,700 But could it cause any problem? 246 00:30:47,080 --> 00:30:48,760 Back to the diagram. 247 00:30:48,820 --> 00:30:54,480 Now there's a little time window in the abort phase that it will not hold the mutex lock. 248 00:30:54,560 --> 00:31:04,360 So there's a chance during the abort phase, the GC check kicks in, recalls the expired element and do a GC sequence check. 249 00:31:04,720 --> 00:31:08,640 In this case, however, the check will be bypassed. 250 00:31:08,640 --> 00:31:13,260 And the GC thread can delete arbitrary expired elements. 251 00:31:16,770 --> 00:31:21,250 Okay, so now the question is, how do we actually exploit the race? 252 00:31:23,110 --> 00:31:28,790 Our exploit plan is first, we add a set element with a short timeout. 253 00:31:28,790 --> 00:31:35,370 And then we trigger module autoload to force it to revert the commit and free the element we just added. 254 00:31:35,750 --> 00:31:41,610 And at the same time, we trigger GC to collect that element. 255 00:31:43,950 --> 00:31:52,130 Next, module autoload will release the mutex and we hope that the GC thread will grab it and bypass the check. 256 00:31:52,890 --> 00:31:57,750 So as a result, the element we just added will be double free. 257 00:31:59,210 --> 00:32:05,070 But in order to trigger double free, there are two time windows we need to enlarge. 258 00:32:06,050 --> 00:32:11,290 The first time window is from the abort phase start to the set element released. 259 00:32:11,510 --> 00:32:17,820 So the GC thread can recall the expired element before they get a link by the revert procedure. 260 00:32:19,290 --> 00:32:24,130 The second time window is from the load module to its end. 261 00:32:24,130 --> 00:32:28,190 So the GC thread can grab the lock and bypass the check. 262 00:32:31,470 --> 00:32:43,690 For the first one, since the abort phase will revert the operations in reverse order, we add a bunch of requests after the add set element request to delay the link operation. 263 00:32:44,870 --> 00:32:51,150 For the second one, we just request multiple unknown type to make module autoload runs longer. 264 00:32:54,510 --> 00:32:56,270 And here's the demo. 265 00:32:58,190 --> 00:32:58,610 Okay. 266 00:32:58,610 --> 00:33:08,410 So first we boot our machine and check the UID is a normal user and then we run the exploit. 267 00:33:08,990 --> 00:33:14,970 So here we will first measure the execution time to set up timing parameter for the race. 268 00:33:15,570 --> 00:33:20,330 And after that, we set up the environment to enlarge the race time windows. 269 00:33:22,450 --> 00:33:28,030 Then we trigger double free to leak address and control IP. 270 00:33:28,970 --> 00:33:31,390 And we got the root shell. 271 00:33:37,220 --> 00:33:38,900 Thank you, guys. 272 00:33:40,860 --> 00:33:42,060 Great. 273 00:33:42,220 --> 00:33:46,900 So now I write the exploit and submit it to kernel CTF. 274 00:33:47,660 --> 00:33:50,360 And this is my kernel CTF submission. 275 00:33:50,840 --> 00:33:55,700 I exploited the LTS instance on kernel version 6.1.81. 276 00:33:55,720 --> 00:33:59,540 But as you can see, it is marked with a dupe in that line. 277 00:34:00,440 --> 00:34:06,760 It is because for every LTS slot, only the first zero day or one day exploit will be accepted. 278 00:34:06,960 --> 00:34:14,940 And there's someone who also submitted a LTS exploit which beat me by nine seconds. 279 00:34:16,080 --> 00:34:18,060 That's too bad. 280 00:34:18,720 --> 00:34:22,160 So we have to wait for the slot to reopen. 281 00:34:23,520 --> 00:34:29,540 So, yes, just wait for about two weeks and the slot will reopen. 282 00:34:30,080 --> 00:34:31,180 Right? 283 00:34:32,920 --> 00:34:35,280 Wait until it starts to reopen. 284 00:34:35,780 --> 00:34:36,560 Yep. 285 00:34:36,980 --> 00:34:41,160 But do you know the F table will be disabled for April 1st? 286 00:34:41,660 --> 00:34:42,600 What? 287 00:34:46,080 --> 00:34:48,780 Actually, there's an announcement on kernel CTF. 288 00:34:48,920 --> 00:34:55,240 It says they are going to disable NF tables along with the switch to kernel version 6.6. 289 00:34:55,240 --> 00:35:03,080 It starts from March 15th but actually they delay the switch so it's from April 1st. 290 00:35:03,160 --> 00:35:04,120 Okay. 291 00:35:04,120 --> 00:35:06,500 So what's the date today? 292 00:35:06,900 --> 00:35:09,960 It's actually March 22nd. 293 00:35:10,580 --> 00:35:21,240 And the kernel CTF slot opens about every two weeks on average which means I won't be able to exploit before they disable the NF tables. 294 00:35:21,880 --> 00:35:29,360 So on my third kernel CTF attempt, I still could not get a bounty which is pretty sad. 295 00:35:30,800 --> 00:35:38,920 To sum up my journey, first I have hope and then I found bug one, a lifetime issue. 296 00:35:39,380 --> 00:35:41,840 And it collides with another submission. 297 00:35:42,560 --> 00:35:46,360 Then I found the bug two, a hard to use one day. 298 00:35:46,780 --> 00:35:51,020 But I used the wrong config so it's unusable. 299 00:35:51,440 --> 00:35:55,600 Then I found the bug three, a locking issue. 300 00:35:56,300 --> 00:36:02,280 But I lose the submission race and NF table will be disabled so it's killed. 301 00:36:02,980 --> 00:36:06,600 However, there's a final plot twist. 302 00:36:10,370 --> 00:36:16,850 A few days after, I found there's some changes in the kernel CTF spreadsheet. 303 00:36:17,590 --> 00:36:25,190 The person who previously got ahead of me is disqualified due to a bug collision. 304 00:36:26,070 --> 00:36:32,390 And so this time, I finally take over the slot and report it to Google. 305 00:36:34,330 --> 00:36:41,050 And this month, I got my kernel CTF bounty from Google which is my first Google VIP bounty. 306 00:36:41,050 --> 00:36:42,870 I'm so proud of myself. 307 00:36:50,630 --> 00:36:51,590 Thanks. 308 00:36:51,810 --> 00:36:55,930 So, well, I think this concludes my kernel CTF journey. 309 00:36:56,430 --> 00:36:57,810 And thank you, guys. 310 00:37:05,110 --> 00:37:06,830 Is there any questions? 311 00:37:15,320 --> 00:37:18,560 Yeah, if there's any questions, you can come up here to this microphone. 312 00:37:37,670 --> 00:37:38,610 Thank you.