[00:00.000 --> 00:03.180] And with no further ado, please continue. [00:07.880 --> 00:12.120] Hello, everyone, and welcome to my talk, [00:12.120 --> 00:16.520] Clash, Burn, and Exploit Manipulated Filters to Pwn Kernel CTF. [00:17.140 --> 00:22.960] I'm HexRabbit from Taiwan, currently working as a security researcher at DefCon. [00:22.960 --> 00:27.760] I specialize in binary exploit, especially in Linux kernel exploitation. [00:27.760 --> 00:31.300] I found some vulnerabilities in Linux kernel components like [00:32.140 --> 00:35.020] ksmbd, nstables, and others. [00:36.180 --> 00:41.160] Today, I will start from introducing kernel CTF and nstables. [00:41.160 --> 00:45.820] After that, we will dive into the nstable internals and talk about three vulnerabilities [00:45.820 --> 00:52.220] we discovered in nstables. And in between these three vulnerabilities, [00:52.220 --> 00:57.140] I will also share some story behind the scene during my kernel CTF journey. [00:57.740 --> 01:02.420] So, let's first talk about kernel CTF. [01:02.420 --> 01:08.760] Kernel CTF is part of Google VRP, and the way it works is quite similar to a CTF challenge. [01:08.980 --> 01:12.740] For about every two weeks, there will be a new kernel version released, [01:12.740 --> 01:15.820] and attendees will be given an unprivileged show. [01:15.820 --> 01:22.740] And you will need to exploit the Linux kernel to get flagged and submit it to Google to earn [01:22.800 --> 01:26.240] a bounty. There are three instances that can be pawned. [01:26.240 --> 01:31.700] LTS and CLS both use newer and quite similar kernel config, while the mitigation one [01:31.700 --> 01:38.120] uses an older kernel config with custom security patch to accept more one-day exploits. [01:38.560 --> 01:43.440] But the most important thing is, Google offers pretty big bounty reward. [01:44.660 --> 01:50.760] With some simple math calculation, we can quickly find for a zero-day LTS exploit, [01:50.760 --> 01:56.700] the bounty can easily surpass $50,000. And with the same vulnerability, you can earn both [01:56.700 --> 02:05.760] $21,000 maximum in both mitigation and CLS instance, which is quite appealing for the [02:05.760 --> 02:10.940] bounty hunter like me. So, why am I targeting nftables? [02:10.940 --> 02:16.400] First, nftable config is enabled in kernel CTF. And as you can see on the right, [02:16.400 --> 02:20.320] there are multiple vulnerabilities discovered just after kernel CTF is announced. [02:20.320 --> 02:25.620] So, for kernel CTF newbie like me, I think it's the right place to start. [02:26.520 --> 02:31.260] Okay, so first we take a brief intro to nftables. [02:33.060 --> 02:39.060] What exactly is nftables? nftable is a package filtering framework inside Linux kernel. [02:39.060 --> 02:45.400] It's used to replace the legacy IP, ARP, EB tables, merge them into a single entry point. [02:45.400 --> 02:52.620] It uses a simple VM design to implement its functionality and support new syntax for CY [02:52.620 --> 02:58.080] interface. As you can see on the right, instead of doing things one by one, new syntax can now [02:58.080 --> 03:04.240] allow config multiple things all at once. And these capabilities come from its design, [03:04.240 --> 03:11.700] which we will revisit later. nftable is constructed by a tree-like structure. [03:11.700 --> 03:17.380] It contains five core modules, table, chain, rule, expression, and set. [03:18.160 --> 03:23.910] Table is the top-level container for the below structures. It's used for the measurement purpose. [03:24.840 --> 03:30.720] Each table will only belong to a specific neural family, and any object contained by the table [03:31.150 --> 03:38.600] will follow the same family. Chain in nftables will hook at predefined hooking point provided [03:38.600 --> 03:44.940] by net filter infrastructure to do the packet filtering. When any packet flows through the [03:44.940 --> 03:50.040] hooking point, the chain will execute the expressions in the row it managed to accept [03:50.040 --> 03:56.760] or to block or to modify the packets. So what is the rule and what is the expression? [03:58.280 --> 04:04.960] As I mentioned earlier, nftable implement its functionality by a simple virtual machine. [04:04.960 --> 04:09.980] So you can think a rule as a small function that can contain multiple expressions, [04:09.980 --> 04:16.900] and the expression is the instruction for nftables. There are different types of expressions [04:16.900 --> 04:24.000] that can be used to manipulate VN registers, and the others can be used to load or to read or write [04:24.000 --> 04:32.160] data within packets. And thus, set is used to store a group of some type, like IP address, [04:32.160 --> 04:39.760] port, or any binary data. It can also be used as a map to map some type A to another type B, [04:39.760 --> 04:45.180] and these data stored in the set can be queried by the expressions during packet filtering. [04:47.220 --> 04:52.940] Okay, so after having some basic understanding of nftables, I will now introduce the first [04:52.940 --> 04:59.940] vulnerability I discovered. At the time, I'm not so familiar with nftables. However, [04:59.940 --> 05:04.860] I spotted something weird in the code base. These three functions are all used to delete [05:04.860 --> 05:12.460] objects in nftables. The nftable deleteRuleDeactivate will be called during rule deletion, [05:12.460 --> 05:19.900] nftFlush will be called during full config flush, and nftSetElementCatchAllDeactivate [05:19.900 --> 05:27.160] will be called during set element deletion. However, the pattern here is a bit different. [05:27.160 --> 05:37.160] The first two use nftIsActiveNext to check if the object is active. But the last one, [05:38.540 --> 05:44.200] nftSetElementCatchAllDeactivate use nftIsActive to check. So what's the difference? [05:45.160 --> 05:51.960] Actually, these two functions are used for object lifetime management in nftables. [05:51.960 --> 05:59.920] In order to understand how it works, we will need to first talk about the batch request mechanism. [06:02.300 --> 06:09.060] For a query request, for example, the getRule operation above, Netlink API will internally use [06:09.060 --> 06:17.160] netlinkReceiveXKB to get the raw information loglessly. However, if it's some operation that [06:17.160 --> 06:24.640] will modify the control plan, like new table or delete chain, nftables will use nfnetlinkReceiveBatch [06:24.640 --> 06:30.780] instead of the logless one to serve the request. The difference between them is that batch one can [06:30.780 --> 06:36.820] process a request which contains multiple operations, and they will be processed sequentially. [06:37.680 --> 06:44.260] And also because of this design, the COR interface can copy multiple things all at once. [06:44.260 --> 06:52.800] So it sounds great, but the thing becomes more complicated when any operation fails in the middle [06:52.800 --> 07:01.460] of a batch request. And what nftables do is to split batch request handling into multiple phases. [07:03.140 --> 07:09.940] There are three phases during the handling of the batch request. Prepare phase, commit phase, [07:09.940 --> 07:14.920] and abort phase. During the prepare phase, if every operation finished successfully [07:15.460 --> 07:20.400] in the batch request, it will go to the commit phase and commit the changes. [07:21.300 --> 07:25.880] However, if something went wrong, after every operation is processed, [07:25.880 --> 07:32.140] it will go to abort phase and revert all the changes. Here's the lifetime management's consign. [07:33.840 --> 07:39.920] Any object in nftables has two state variables, current generation and next generation. [07:39.940 --> 07:47.200] Only the object activated in current generation can affect the runtime behavior. And you can [07:47.200 --> 07:53.600] think the next generation as a temporary state for the prepare phase. Every operation modify [07:53.600 --> 07:59.040] the control plan, like new or delete operations, will only make changes on the next generation [07:59.040 --> 08:04.840] state in the prepare phase. So without affecting the current generation state. [08:04.840 --> 08:11.800] Then, during the commit phase, it will commit the modification. So the current [08:11.800 --> 08:17.540] generation will finally be set as the next generation, and the next generation state [08:17.540 --> 08:24.700] will be reset to a clean state. But in the case if something went wrong during the abort phase, [08:24.700 --> 08:31.180] it only needs to clean the next generation state. Let's go through some examples to know how it [08:31.180 --> 08:38.340] works. After a batch request is received, it goes into the prepare phase. [08:38.860 --> 08:45.960] The first operation is delete set C. Okay, so the set C's next generation state will be [08:45.960 --> 08:54.200] set to deactivated. Next, it continues to process a set D operation. [08:55.120 --> 09:00.260] So the set D will be added into hash table with next generation in activated state. [09:02.400 --> 09:09.200] If nothing went wrong, we will go to the commit phase, and at this phase, current generation will [09:09.200 --> 09:17.180] be set as the next generation. So the set D will be available in both runtime and query operations, [09:17.180 --> 09:25.820] and also the set C will be free. If something went wrong, we will enter the abort phase, [09:25.820 --> 09:33.100] and at this phase, all operations done in the prepare phase will be reverted in a reverse order. [09:33.380 --> 09:39.500] So the add set D will be undue, and also the delete set C will be undue. [09:42.660 --> 09:48.520] So okay, back to the check. So what's the problem here? As we can see, during the [09:48.520 --> 09:54.980] NFT set element catch all deactivate, it used NFT is active to check if the element is still [09:54.980 --> 10:01.720] alive. However, NFT is active checks current generation, but the function actually runs in [10:01.720 --> 10:07.520] the prepare state, which will only modify the next generation state of an object. [10:08.020 --> 10:15.480] So the check here will always pass, and by request multiple delete operations, [10:15.480 --> 10:21.660] we can free the element multiple times in the commit phase. So the batch request to [10:21.660 --> 10:26.780] trigger double free will look at this, since the lifetime check will always pass if the element [10:26.780 --> 10:34.240] exists during the prepare phase, two delete set element of operation will both execute successfully. [10:34.320 --> 10:41.080] And so when you get to the commit phase, the same element will be free twice, and we get a [10:41.080 --> 10:48.070] set catch all element double free. Great, so I found my first bug for current CTF. [10:48.070 --> 10:58.950] But the next day, I found the maintainer also find the same bug and send a patch to kernel mailing list. [10:58.990 --> 11:07.570] I mean, why? So, okay, this means my zero day is officially turns into a one day, and according to [11:07.570 --> 11:14.330] kernel CTF rules, my bounty will decrease by 20,000 USD. Ouch. [11:15.610 --> 11:21.350] Also, one day vulnerability means that everyone will know how to trigger the vulnerability, [11:21.350 --> 11:27.390] and they may also come up with some exploit. So I definitely need to exploit it before next [11:27.390 --> 11:37.000] kernel CTF slot opens. So after we get the double free primitive, we reuse the exploit method for [11:37.000 --> 11:45.980] CVE-2023-4004 to overlap the MFT table user data and MFT object to leak address and control IP. [11:46.100 --> 11:51.360] Due to a time limit of this talk, I won't expand the detail of it, but kudos to Lonely and Calm [11:51.360 --> 11:56.440] who discovered this technique and shared it on kernel CTF repository. [11:57.560 --> 12:03.600] So now we develop the exploits and wait for the slot to reopen. [12:04.560 --> 12:10.640] Fast forward to two hours before the competition starts. I nervously check if there's any kernel [12:10.640 --> 12:19.160] updates related to NF tables, and guess what? The patch previously posted on the mailing list has [12:19.160 --> 12:27.980] been merged into the kernel, and it added one line on it. It says reported by Lonely and Calm, [12:27.980 --> 12:32.880] which this means is the patch may actually come from a zero day kernel CTF submission, [12:32.880 --> 12:39.900] and since the reporter is Lonely and Calm whose exploit I just used, this is indeed the case. [12:40.180 --> 12:46.540] So just before I have the chance to exploit an official instance, I found it's a collision. [12:48.040 --> 12:55.000] So yes, I still found the instance and report the flag, but too bad for me, the first bug [12:55.000 --> 13:01.780] do collide with an old kernel CTF submission. That's how I look after the competition. [13:04.860 --> 13:12.700] Okay, so I take some break, and after three weeks later, I found the second bug. [13:14.160 --> 13:18.280] During the research, I came across an interesting commit. [13:18.380 --> 13:25.580] NF table validate family when identifying table by handle. The commit is added after the mitigation [13:25.580 --> 13:30.800] instance kernel version, so it can be used upon the mitigation instance as a one day. [13:31.300 --> 13:39.020] It added a check on NFC table lookup by handle function, which is used for searching a register [13:39.020 --> 13:45.460] table by the provided handle. Without the commit, during the table lookup, we can provide a family [13:45.460 --> 13:52.740] different from the one which table belongs to, but what can we do with this inconsistency? [13:54.160 --> 13:59.360] Let's trace where the family parameter comes from and where it goes. [13:59.360 --> 14:06.140] NF table lookup by handle is only code from NF table delete table, and this function is used to [14:06.140 --> 14:15.060] delete user-specified table from NF tables. The family variable is directly get from user-provided [14:15.060 --> 14:22.500] input and is saved into context family and passed to NFT flush table function. [14:23.640 --> 14:30.800] In NFT flush table, context argument is passed into every function handling object deletion. [14:30.800 --> 14:37.360] For me, trace which function use the context family is quite annoying, so instead of just [14:37.360 --> 14:46.680] tracing into each function, I take the shortcut to trace where it is used. So I just grab the [14:46.680 --> 14:54.920] whole code base to find where the context family is used. Grabbing context family gives us a lot [14:54.920 --> 15:02.480] of result, most of which comes from the NF table expressions, like count limit, net, T proxy, and [15:02.480 --> 15:10.460] et cetera. However, most of them cannot be used for the exploit, but I finally found one that can [15:10.460 --> 15:19.760] be exploited. It is in the NFT log expression. The trigger path I found starts from the NFT log [15:19.760 --> 15:25.580] destroy. This function takes context family as an argument and code to NF log output. [15:26.660 --> 15:33.210] Now it's becoming the PF argument and used as an index to de-reference the loggers array. [15:34.340 --> 15:40.440] The value range of our input context family and also the PF here is zero to 255. [15:40.460 --> 15:48.000] But if we check the size of loggers array, we can find it only has 13 elements in the first [15:48.000 --> 15:56.460] dimension. So we get the global array out of bound access here. But what can we do with it? [15:58.160 --> 16:04.500] After taking the logger out, it de-referenced the me field and feed it into the module put. [16:05.440 --> 16:11.740] The module put will again de-reference and decrease the refcon value in the module object. [16:12.220 --> 16:17.840] With this vulnerability, logger can be any pointer store adjacent to a loggers array. [16:17.840 --> 16:23.080] So we may cause some type confusion here and decrease some variable by one. [16:23.320 --> 16:29.900] However, we are dealing with an awkward situation here. In order to utilize this bug, [16:29.900 --> 16:34.480] we have to find a pointer store on global and can be de-referenced twice. [16:35.160 --> 16:41.780] The first is to take our logger me and the second is to take our module refcon. [16:43.640 --> 16:53.140] To sum up what we have by now, first, the maximum value of PF value we can control is 255 and we [16:53.140 --> 17:01.100] can trigger an out of bound reference count decrement by using a PF value larger than 12, [17:01.100 --> 17:06.800] which will execute like the code here. To exploit this, the first thing we need to do [17:06.800 --> 17:14.400] is to find a global pointer placed near the loggers array. So what do we have on the global? [17:14.920 --> 17:21.540] By inspect the marrying GDB, we quickly find there's an NFCT expect hash placed near [17:21.540 --> 17:28.640] the loggers array. And it's a hash table. So in this case, it seems to be a good target [17:28.640 --> 17:36.300] since the pointer point to an array of pointers. Therefore, it can be de-referenced twice. [17:38.320 --> 17:47.540] So let's check how the code normally works. First, it gets the me field at offset 1x and then [17:48.120 --> 17:57.780] get the refcon at offset 340x. Now let's check the out of bound access case. First, we set PF to 19 [17:57.780 --> 18:04.760] to get a hash table pointer, NFCT expect hash. The first de-reference we got is the index tree [18:04.760 --> 18:13.960] entry of the hash table. The entry points to the HNO field of a contract expect object. So after [18:13.960 --> 18:21.660] one more de-reference, it will access offset 340x down below the expect object. But since the [18:21.660 --> 18:29.400] expect object only has a D at size, it will access out of bounds. Fortunately, all NF [18:29.400 --> 18:33.600] contract expect object allocated in a dedicated Cayman cache. [18:34.420 --> 18:40.040] So the memory space below will also contain NF contract expect objects. [18:40.920 --> 18:46.860] So it will access to an RCU field and decrease the next pointer in the structure. [18:46.860 --> 18:50.680] But what does this next pointer use for? [18:52.140 --> 18:59.180] As we know, after RCU objects free, Linux kernel will not instantly free the object. Instead, [18:59.700 --> 19:05.180] it will hold the object for a grace period to wait for all other RCU readers to finish. [19:05.360 --> 19:09.900] So the next pointer in the RCU hash structure is used for temporary storing them [19:09.900 --> 19:14.080] and waiting for the RCU thread to process, to free them. [19:15.640 --> 19:22.400] So after the contract expect object are free, they will be like this, trained by the next pointer. [19:24.580 --> 19:27.980] And since we can decrease the next pointer in RCU head, [19:27.980 --> 19:31.720] we can make it point to another field inside the contract expect object. [19:33.140 --> 19:37.320] And to exploit it, we choose to land it on a user controllable field, [19:37.320 --> 19:43.340] which contains a union of IPv4 and IPv6 address. So when RCU callback triggered, [19:44.080 --> 19:49.040] it will read the fake function pointer we set, and we can control the IP. [19:50.660 --> 19:55.520] However, to actually exploit the vulnerability, we came across some roadblocks. [19:55.880 --> 20:01.140] First, we need the index tree of the hash table point to some expect object, [20:01.140 --> 20:09.760] so we can use it to do the out-of-bound write. But unfortunately, the hash value is unpredictable. [20:11.100 --> 20:16.740] Next, the out-of-bound written expect object needs to be in the grace period, [20:16.740 --> 20:23.500] so we can decrease its next pointer. But the next period is a small time window, [20:23.500 --> 20:27.220] and how to control when the kernel actually frees the object. [20:28.560 --> 20:34.600] Well, so long story short, we eventually found a way to exploit it by brute force and race. [20:35.900 --> 20:42.040] So to exploit this, first we use a tight loop to create a lot of expect objects. [20:42.800 --> 20:50.600] And at some point, the expect count exceeded the value max expected and start to evict the [20:50.600 --> 20:58.260] oldest expect object. So the oldest expect object will be placed into the RCU free list. [20:58.960 --> 21:05.800] And so on, and so on. And at some point, RCU thread gets scheduled by the kernel [21:05.800 --> 21:13.980] and starting to process objects. At this point, we add another thread to trigger [21:13.980 --> 21:20.560] out-of-bound decrement in a batch request and keep this system running for a bit. [21:22.520 --> 21:26.360] So we hope for a race scenario like this to emerge. [21:27.520 --> 21:34.160] First, the expect object allocated into index three of the hash table, and then the expect [21:34.160 --> 21:41.380] object we want to out-of-bound write is freed into the RCU free list. Then it continues to [21:41.380 --> 21:48.300] free another expect object. After that, we trigger the vulnerability 28 times in a batch [21:48.300 --> 21:55.400] request to make the next pointer point to a user-controllable value. And finally, the RCU [21:55.400 --> 22:00.360] start to process these free object and runs our set callback function. [22:01.860 --> 22:09.180] Great. Let's try it on local build. And we successful successfully control the IP to [22:09.180 --> 22:20.460] form, form, form, form. So what about remote? Let's try. And we got an error message while [22:20.460 --> 22:25.840] trying to add the lock expression. Since I have no idea what happened on the remote [22:25.840 --> 22:30.680] environment, I decided to download the official busy image to debug it locally. [22:32.360 --> 22:39.500] So I open the GDB and set some breakpoint on the functions, but when I set the break on [22:39.500 --> 22:45.620] nft-lock-destroy function, GDB complained that it was not defined. What's wrong here? [22:48.100 --> 22:54.840] I later found that it's because the kernel config I used is different from the one used in [22:54.840 --> 23:01.040] the official environment. So the log expression I used to exploit the vulnerability not even [23:01.040 --> 23:08.540] compiled into the official image, but I'm using the official build script to view the local [23:08.540 --> 23:16.460] test image. Why on earth there can be any difference? I mean, why? [23:19.540 --> 23:25.900] Eventually, I found it is caused by the official build script. The mitigation instance is built [23:25.900 --> 23:33.800] by COS kernel config plus the Google custom security security patch. And the official [23:33.800 --> 23:44.520] mitigation instance use COS config 6.1.55. The nftable expression are enabled in COS config [23:44.520 --> 23:53.240] after the version 6.1.64. However, the build script will always fetch the latest COS config, [23:53.240 --> 23:58.220] so the mitigation kernel I built has the nftable expression enabled, [23:58.220 --> 24:01.820] but in the official image, they're actually disabled. [24:03.780 --> 24:09.820] So, well, I think next time I should definitely check out the config first. [24:14.100 --> 24:22.840] Okay, so after my second failure, I revisit some old patch and I found a new bug. [24:25.170 --> 24:31.490] The patch I was looking at is a patch which introduced the first bug. The title says [24:31.490 --> 24:42.470] nftable, a set backend to use GC transaction API. So what does the GC mean? Here it means [24:42.470 --> 24:48.390] the set garbage collection. There are some set implementation support user to set a timeout [24:48.390 --> 24:56.570] on any set element. And there are two types of GC to clean the expired element. [24:56.570 --> 25:03.150] The first one, the synchronous one will be called in the commit phase, [25:03.150 --> 25:09.850] and the asynchronous one will be triggered periodically in the background. But here we [25:09.850 --> 25:17.970] only focus on the asynchronous one. So back to the patch. In the commit message, it says [25:17.970 --> 25:25.510] use GC transaction API to replace the old and buggy GC API and the busy mark approach. [25:26.190 --> 25:34.150] So what is the difference between the new and the old one? Let's take a look of the old GC design. [25:35.110 --> 25:42.150] In the old design, the synchronous GC will run loglessly and can be triggered at any time. [25:42.410 --> 25:48.310] So in order to protect GC threads from raising with the landing API, both of them will check [25:49.190 --> 25:54.190] a busy mark set on the element before delete it to prevent double free. [25:56.330 --> 26:03.430] But sometimes the developer might forget to check against it. In this case, [26:03.430 --> 26:09.110] there's a new function called NFT map deactivate forgot to check a busy mark. [26:09.270 --> 26:16.470] And this leads to a double free vulnerability. So in order to avoid this kind of issues, [26:16.470 --> 26:21.050] NFT developer decided to replace it with a new design. [26:22.250 --> 26:29.070] In the new GC design, it introduced the GC sequence. The GC sequence is a per net variable [26:29.070 --> 26:37.090] and it will be increased before and after any modification to the control plan. So it will [26:37.090 --> 26:43.750] increase at the beginning and at the end of both the commit and the abort phase. [26:44.390 --> 26:49.970] During a GC procedure, GC thread will record and check it to prevent raise. [26:50.270 --> 26:54.830] Even though the GC thread still run loglessly, during the check, [26:54.830 --> 27:03.630] it will now quad the commit mutex lock. The new GC design look like this. [27:05.010 --> 27:10.950] During batch request handling, GC sequence will increase both before and after the commit and [27:10.950 --> 27:18.930] abort phase. And in the GC thread, it will first run locklessly to store the GC sequence and [27:18.930 --> 27:25.530] collect all the expired elements. After that, it will quad the lock and check if the current GC [27:25.530 --> 27:32.270] sequence match the safe one. If they are the same, then the elements will be released. [27:33.350 --> 27:38.470] For this normal case, since GC thread does not raise with the batch request, [27:38.470 --> 27:44.490] the GC sequence will be the same. And the element can be freed by the GC thread. [27:47.960 --> 27:54.480] Next, we check if it still works in the raise case. If during the prepare phase, GC thread [27:54.480 --> 28:01.980] triggered and stored the global GC sequence into transfer objects, it will continue to run [28:01.980 --> 28:10.680] loglessly to find all the expired elements and wait for the mutex lock. [28:12.420 --> 28:16.540] At the same time, the main thread stepped into the commit phase, [28:16.540 --> 28:21.340] and some of the elements in the set can be released in this phase. [28:22.320 --> 28:27.400] The GC thread will need to wait until the main thread releases the mutex lock, [28:27.400 --> 28:35.460] so it can finally check the safe GC sequence and delete the element. But in this case, [28:35.460 --> 28:41.160] since GC sequence is recorded in the prepare phase, and we check it after the batch request [28:41.160 --> 28:47.000] is done, the GC sequence will be different. So, the element deletion will be skipped to [28:47.000 --> 28:54.540] avoid double free. Next, let's check the case if GC sequence is recorded in the commit phase. [28:54.540 --> 29:06.300] In this case, since GC sequence increased after the commit phase, the GC thread can only check [29:06.300 --> 29:20.800] GC sequence after the commit mutex unlocked. This raise will also be detected. So, to sum up, [29:20.800 --> 29:27.640] the new GC design requires both the batch request and the GC sequence check to hold the lock. [29:27.640 --> 29:32.120] This ensures that even there's a raise while collecting the expired element, [29:32.520 --> 29:37.580] the check will always happen after the GC sequence has increased. [29:40.670 --> 29:49.410] But does the batch request always lock mean there's no raise? Actually, if you look closely, [29:50.090 --> 30:01.720] we will find, well, it's not always locked. To find where it release the lock, let's first talk [30:01.720 --> 30:07.360] about the module autoload. During the prepare phase, if there's some expression type in the [30:07.360 --> 30:15.580] request is not found during the type resolution, it will instantly abort and try to load the [30:15.580 --> 30:24.080] type module during the end of the abort phase. After that, it will retry the whole batch request [30:24.080 --> 30:33.480] again. However, maybe there's some efficiency consideration or to just only prevent that lock [30:34.080 --> 30:41.780] before the function tries to load the request module, the mutex lock will be released. [30:42.480 --> 30:44.700] But could it cause any problem? [30:47.080 --> 30:53.020] Back to the diagram. Now there's a little time window in the abort phase that it will not hold [30:53.020 --> 31:01.280] the mutex lock. So there's a chance during the abort phase, the GC check kicks in, recalls the [31:01.280 --> 31:08.640] expired element and do a GC sequence check. In this case, however, the check will be bypassed. [31:08.640 --> 31:13.260] And the GC thread can delete arbitrary expired elements. [31:16.770 --> 31:21.250] Okay, so now the question is, how do we actually exploit the race? [31:23.110 --> 31:30.270] Our exploit plan is first, we add a set element with a short timeout. And then we trigger module [31:30.270 --> 31:37.610] autoload to force it to revert the commit and free the element we just added. And at the same time, [31:37.610 --> 31:48.130] we trigger GC to collect that element. Next, module autoload will release the mutex and we [31:48.130 --> 31:55.450] hope that the GC thread will grab it and bypass the check. So as a result, the element we just [31:55.450 --> 32:03.670] added will be double free. But in order to trigger double free, there are two time windows we need [32:03.670 --> 32:11.290] to enlarge. The first time window is from the abort phase start to the set element released. [32:11.510 --> 32:17.820] So the GC thread can recall the expired element before they get a link by the revert procedure. [32:19.290 --> 32:26.370] The second time window is from the load module to its end. So the GC thread can grab the lock [32:26.370 --> 32:35.770] and bypass the check. For the first one, since the abort phase will revert the operations in [32:35.770 --> 32:42.450] reverse order, we add a bunch of requests after the add set element request to delay the link [32:42.450 --> 32:49.850] operation. For the second one, we just request multiple unknown type to make module autoload [32:49.850 --> 33:05.790] runs longer. And here's the demo. Okay. So first we boot our machine and check the UID is a normal [33:05.790 --> 33:12.830] user and then we run the exploit. So here we will first measure the execution time to set up [33:12.830 --> 33:19.390] timing parameter for the race. And after that, we set up the environment to enlarge the race time [33:19.390 --> 33:31.390] windows. Then we trigger double free to leak address and control IP. And we got the root shell. [33:37.220 --> 33:48.740] Thank you, guys. Great. So now I write the exploit and submit it to kernel CTF. And this is my [33:48.740 --> 33:57.140] kernel CTF submission. I exploited the LTS instance on kernel version 6.1.81. But as you can see, it [33:57.140 --> 34:04.640] is marked with a dupe in that line. It is because for every LTS slot, only the first zero day or one [34:04.640 --> 34:13.080] day exploit will be accepted. And there's someone who also submitted a LTS exploit which beat me [34:13.080 --> 34:26.100] by nine seconds. That's too bad. So we have to wait for the slot to reopen. So, yes, just wait [34:26.100 --> 34:37.260] for about two weeks and the slot will reopen. Right? Wait until it starts to reopen. Yep. But [34:37.260 --> 34:47.720] do you know the F table will be disabled for April 1st? What? Actually, there's an announcement [34:47.720 --> 34:53.760] on kernel CTF. It says they are going to disable NF tables along with the switch to kernel version [34:53.760 --> 35:04.120] 6.6. It starts from March 15th but actually they delay the switch so it's from April 1st. Okay. [35:04.120 --> 35:14.580] So what's the date today? It's actually March 22nd. And the kernel CTF slot opens about every [35:14.580 --> 35:21.240] two weeks on average which means I won't be able to exploit before they disable the NF tables. [35:21.880 --> 35:29.360] So on my third kernel CTF attempt, I still could not get a bounty which is pretty sad. [35:30.800 --> 35:39.700] To sum up my journey, first I have hope and then I found bug one, a lifetime issue. And it [35:39.700 --> 35:48.200] collides with another submission. Then I found the bug two, a hard to use one day. But I used [35:48.200 --> 35:57.760] the wrong config so it's unusable. Then I found the bug three, a locking issue. But I lose the [35:57.760 --> 36:06.600] submission race and NF table will be disabled so it's killed. However, there's a final plot twist. [36:10.370 --> 36:16.850] A few days after, I found there's some changes in the kernel CTF spreadsheet. [36:17.590 --> 36:25.190] The person who previously got ahead of me is disqualified due to a bug collision. [36:26.070 --> 36:32.390] And so this time, I finally take over the slot and report it to Google. [36:34.330 --> 36:41.530] And this month, I got my kernel CTF bounty from Google which is my first Google VIP bounty. I'm [36:41.530 --> 36:55.930] so proud of myself. Thanks. So, well, I think this concludes my kernel CTF journey. [36:56.430 --> 37:06.830] And thank you, guys. Is there any questions? [37:15.320 --> 37:18.560] Yeah, if there's any questions, you can come up here to this microphone. [37:37.670 --> 37:38.610] Thank you.