start	end	text
0	3180	And with no further ado, please continue.
7880	12120	Hello, everyone, and welcome to my talk,
12120	16520	Clash, Burn, and Exploit Manipulated Filters to Pwn Kernel CTF.
17140	22960	I'm HexRabbit from Taiwan, currently working as a security researcher at DefCon.
22960	27760	I specialize in binary exploit, especially in Linux kernel exploitation.
27760	31300	I found some vulnerabilities in Linux kernel components like
32140	35020	ksmbd, nstables, and others.
36180	41160	Today, I will start from introducing kernel CTF and nstables.
41160	45820	After that, we will dive into the nstable internals and talk about three vulnerabilities
45820	52220	we discovered in nstables. And in between these three vulnerabilities,
52220	57140	I will also share some story behind the scene during my kernel CTF journey.
57740	62420	So, let's first talk about kernel CTF.
62420	68760	Kernel CTF is part of Google VRP, and the way it works is quite similar to a CTF challenge.
68980	72740	For about every two weeks, there will be a new kernel version released,
72740	75820	and attendees will be given an unprivileged show.
75820	82740	And you will need to exploit the Linux kernel to get flagged and submit it to Google to earn
82800	86240	a bounty. There are three instances that can be pawned.
86240	91700	LTS and CLS both use newer and quite similar kernel config, while the mitigation one
91700	98120	uses an older kernel config with custom security patch to accept more one-day exploits.
98560	103440	But the most important thing is, Google offers pretty big bounty reward.
104660	110760	With some simple math calculation, we can quickly find for a zero-day LTS exploit,
110760	116700	the bounty can easily surpass $50,000. And with the same vulnerability, you can earn both
116700	125760	$21,000 maximum in both mitigation and CLS instance, which is quite appealing for the
125760	130940	bounty hunter like me. So, why am I targeting nftables?
130940	136400	First, nftable config is enabled in kernel CTF. And as you can see on the right,
136400	140320	there are multiple vulnerabilities discovered just after kernel CTF is announced.
140320	145620	So, for kernel CTF newbie like me, I think it's the right place to start.
146520	151260	Okay, so first we take a brief intro to nftables.
153060	159060	What exactly is nftables? nftable is a package filtering framework inside Linux kernel.
159060	165400	It's used to replace the legacy IP, ARP, EB tables, merge them into a single entry point.
165400	172620	It uses a simple VM design to implement its functionality and support new syntax for CY
172620	178080	interface. As you can see on the right, instead of doing things one by one, new syntax can now
178080	184240	allow config multiple things all at once. And these capabilities come from its design,
184240	191700	which we will revisit later. nftable is constructed by a tree-like structure.
191700	197380	It contains five core modules, table, chain, rule, expression, and set.
198160	203910	Table is the top-level container for the below structures. It's used for the measurement purpose.
204840	210720	Each table will only belong to a specific neural family, and any object contained by the table
211150	218600	will follow the same family. Chain in nftables will hook at predefined hooking point provided
218600	224940	by net filter infrastructure to do the packet filtering. When any packet flows through the
224940	230040	hooking point, the chain will execute the expressions in the row it managed to accept
230040	236760	or to block or to modify the packets. So what is the rule and what is the expression?
238280	244960	As I mentioned earlier, nftable implement its functionality by a simple virtual machine.
244960	249980	So you can think a rule as a small function that can contain multiple expressions,
249980	256900	and the expression is the instruction for nftables. There are different types of expressions
256900	264000	that can be used to manipulate VN registers, and the others can be used to load or to read or write
264000	272160	data within packets. And thus, set is used to store a group of some type, like IP address,
272160	279760	port, or any binary data. It can also be used as a map to map some type A to another type B,
279760	285180	and these data stored in the set can be queried by the expressions during packet filtering.
287220	292940	Okay, so after having some basic understanding of nftables, I will now introduce the first
292940	299940	vulnerability I discovered. At the time, I'm not so familiar with nftables. However,
299940	304860	I spotted something weird in the code base. These three functions are all used to delete
304860	312460	objects in nftables. The nftable deleteRuleDeactivate will be called during rule deletion,
312460	319900	nftFlush will be called during full config flush, and nftSetElementCatchAllDeactivate
319900	327160	will be called during set element deletion. However, the pattern here is a bit different.
327160	337160	The first two use nftIsActiveNext to check if the object is active. But the last one,
338540	344200	nftSetElementCatchAllDeactivate use nftIsActive to check. So what's the difference?
345160	351960	Actually, these two functions are used for object lifetime management in nftables.
351960	359920	In order to understand how it works, we will need to first talk about the batch request mechanism.
362300	369060	For a query request, for example, the getRule operation above, Netlink API will internally use
369060	377160	netlinkReceiveXKB to get the raw information loglessly. However, if it's some operation that
377160	384640	will modify the control plan, like new table or delete chain, nftables will use nfnetlinkReceiveBatch
384640	390780	instead of the logless one to serve the request. The difference between them is that batch one can
390780	396820	process a request which contains multiple operations, and they will be processed sequentially.
397680	404260	And also because of this design, the COR interface can copy multiple things all at once.
404260	412800	So it sounds great, but the thing becomes more complicated when any operation fails in the middle
412800	421460	of a batch request. And what nftables do is to split batch request handling into multiple phases.
423140	429940	There are three phases during the handling of the batch request. Prepare phase, commit phase,
429940	434920	and abort phase. During the prepare phase, if every operation finished successfully
435460	440400	in the batch request, it will go to the commit phase and commit the changes.
441300	445880	However, if something went wrong, after every operation is processed,
445880	452140	it will go to abort phase and revert all the changes. Here's the lifetime management's consign.
453840	459920	Any object in nftables has two state variables, current generation and next generation.
459940	467200	Only the object activated in current generation can affect the runtime behavior. And you can
467200	473600	think the next generation as a temporary state for the prepare phase. Every operation modify
473600	479040	the control plan, like new or delete operations, will only make changes on the next generation
479040	484840	state in the prepare phase. So without affecting the current generation state.
484840	491800	Then, during the commit phase, it will commit the modification. So the current
491800	497540	generation will finally be set as the next generation, and the next generation state
497540	504700	will be reset to a clean state. But in the case if something went wrong during the abort phase,
504700	511180	it only needs to clean the next generation state. Let's go through some examples to know how it
511180	518340	works. After a batch request is received, it goes into the prepare phase.
518860	525960	The first operation is delete set C. Okay, so the set C's next generation state will be
525960	534200	set to deactivated. Next, it continues to process a set D operation.
535120	540260	So the set D will be added into hash table with next generation in activated state.
542400	549200	If nothing went wrong, we will go to the commit phase, and at this phase, current generation will
549200	557180	be set as the next generation. So the set D will be available in both runtime and query operations,
557180	565820	and also the set C will be free. If something went wrong, we will enter the abort phase,
565820	573100	and at this phase, all operations done in the prepare phase will be reverted in a reverse order.
573380	579500	So the add set D will be undue, and also the delete set C will be undue.
582660	588520	So okay, back to the check. So what's the problem here? As we can see, during the
588520	594980	NFT set element catch all deactivate, it used NFT is active to check if the element is still
594980	601720	alive. However, NFT is active checks current generation, but the function actually runs in
601720	607520	the prepare state, which will only modify the next generation state of an object.
608020	615480	So the check here will always pass, and by request multiple delete operations,
615480	621660	we can free the element multiple times in the commit phase. So the batch request to
621660	626780	trigger double free will look at this, since the lifetime check will always pass if the element
626780	634240	exists during the prepare phase, two delete set element of operation will both execute successfully.
634320	641080	And so when you get to the commit phase, the same element will be free twice, and we get a
641080	648070	set catch all element double free. Great, so I found my first bug for current CTF.
648070	658950	But the next day, I found the maintainer also find the same bug and send a patch to kernel mailing list.
658990	667570	I mean, why? So, okay, this means my zero day is officially turns into a one day, and according to
667570	674330	kernel CTF rules, my bounty will decrease by 20,000 USD. Ouch.
675610	681350	Also, one day vulnerability means that everyone will know how to trigger the vulnerability,
681350	687390	and they may also come up with some exploit. So I definitely need to exploit it before next
687390	697000	kernel CTF slot opens. So after we get the double free primitive, we reuse the exploit method for
697000	705980	CVE-2023-4004 to overlap the MFT table user data and MFT object to leak address and control IP.
706100	711360	Due to a time limit of this talk, I won't expand the detail of it, but kudos to Lonely and Calm
711360	716440	who discovered this technique and shared it on kernel CTF repository.
717560	723600	So now we develop the exploits and wait for the slot to reopen.
724560	730640	Fast forward to two hours before the competition starts. I nervously check if there's any kernel
730640	739160	updates related to NF tables, and guess what? The patch previously posted on the mailing list has
739160	747980	been merged into the kernel, and it added one line on it. It says reported by Lonely and Calm,
747980	752880	which this means is the patch may actually come from a zero day kernel CTF submission,
752880	759900	and since the reporter is Lonely and Calm whose exploit I just used, this is indeed the case.
760180	766540	So just before I have the chance to exploit an official instance, I found it's a collision.
768040	775000	So yes, I still found the instance and report the flag, but too bad for me, the first bug
775000	781780	do collide with an old kernel CTF submission. That's how I look after the competition.
784860	792700	Okay, so I take some break, and after three weeks later, I found the second bug.
794160	798280	During the research, I came across an interesting commit.
798380	805580	NF table validate family when identifying table by handle. The commit is added after the mitigation
805580	810800	instance kernel version, so it can be used upon the mitigation instance as a one day.
811300	819020	It added a check on NFC table lookup by handle function, which is used for searching a register
819020	825460	table by the provided handle. Without the commit, during the table lookup, we can provide a family
825460	832740	different from the one which table belongs to, but what can we do with this inconsistency?
834160	839360	Let's trace where the family parameter comes from and where it goes.
839360	846140	NF table lookup by handle is only code from NF table delete table, and this function is used to
846140	855060	delete user-specified table from NF tables. The family variable is directly get from user-provided
855060	862500	input and is saved into context family and passed to NFT flush table function.
863640	870800	In NFT flush table, context argument is passed into every function handling object deletion.
870800	877360	For me, trace which function use the context family is quite annoying, so instead of just
877360	886680	tracing into each function, I take the shortcut to trace where it is used. So I just grab the
886680	894920	whole code base to find where the context family is used. Grabbing context family gives us a lot
894920	902480	of result, most of which comes from the NF table expressions, like count limit, net, T proxy, and
902480	910460	et cetera. However, most of them cannot be used for the exploit, but I finally found one that can
910460	919760	be exploited. It is in the NFT log expression. The trigger path I found starts from the NFT log
919760	925580	destroy. This function takes context family as an argument and code to NF log output.
926660	933210	Now it's becoming the PF argument and used as an index to de-reference the loggers array.
934340	940440	The value range of our input context family and also the PF here is zero to 255.
940460	948000	But if we check the size of loggers array, we can find it only has 13 elements in the first
948000	956460	dimension. So we get the global array out of bound access here. But what can we do with it?
958160	964500	After taking the logger out, it de-referenced the me field and feed it into the module put.
965440	971740	The module put will again de-reference and decrease the refcon value in the module object.
972220	977840	With this vulnerability, logger can be any pointer store adjacent to a loggers array.
977840	983080	So we may cause some type confusion here and decrease some variable by one.
983320	989900	However, we are dealing with an awkward situation here. In order to utilize this bug,
989900	994480	we have to find a pointer store on global and can be de-referenced twice.
995160	1001780	The first is to take our logger me and the second is to take our module refcon.
1003640	1013140	To sum up what we have by now, first, the maximum value of PF value we can control is 255 and we
1013140	1021100	can trigger an out of bound reference count decrement by using a PF value larger than 12,
1021100	1026800	which will execute like the code here. To exploit this, the first thing we need to do
1026800	1034400	is to find a global pointer placed near the loggers array. So what do we have on the global?
1034920	1041540	By inspect the marrying GDB, we quickly find there's an NFCT expect hash placed near
1041540	1048640	the loggers array. And it's a hash table. So in this case, it seems to be a good target
1048640	1056300	since the pointer point to an array of pointers. Therefore, it can be de-referenced twice.
1058320	1067540	So let's check how the code normally works. First, it gets the me field at offset 1x and then
1068120	1077780	get the refcon at offset 340x. Now let's check the out of bound access case. First, we set PF to 19
1077780	1084760	to get a hash table pointer, NFCT expect hash. The first de-reference we got is the index tree
1084760	1093960	entry of the hash table. The entry points to the HNO field of a contract expect object. So after
1093960	1101660	one more de-reference, it will access offset 340x down below the expect object. But since the
1101660	1109400	expect object only has a D at size, it will access out of bounds. Fortunately, all NF
1109400	1113600	contract expect object allocated in a dedicated Cayman cache.
1114420	1120040	So the memory space below will also contain NF contract expect objects.
1120920	1126860	So it will access to an RCU field and decrease the next pointer in the structure.
1126860	1130680	But what does this next pointer use for?
1132140	1139180	As we know, after RCU objects free, Linux kernel will not instantly free the object. Instead,
1139700	1145180	it will hold the object for a grace period to wait for all other RCU readers to finish.
1145360	1149900	So the next pointer in the RCU hash structure is used for temporary storing them
1149900	1154080	and waiting for the RCU thread to process, to free them.
1155640	1162400	So after the contract expect object are free, they will be like this, trained by the next pointer.
1164580	1167980	And since we can decrease the next pointer in RCU head,
1167980	1171720	we can make it point to another field inside the contract expect object.
1173140	1177320	And to exploit it, we choose to land it on a user controllable field,
1177320	1183340	which contains a union of IPv4 and IPv6 address. So when RCU callback triggered,
1184080	1189040	it will read the fake function pointer we set, and we can control the IP.
1190660	1195520	However, to actually exploit the vulnerability, we came across some roadblocks.
1195880	1201140	First, we need the index tree of the hash table point to some expect object,
1201140	1209760	so we can use it to do the out-of-bound write. But unfortunately, the hash value is unpredictable.
1211100	1216740	Next, the out-of-bound written expect object needs to be in the grace period,
1216740	1223500	so we can decrease its next pointer. But the next period is a small time window,
1223500	1227220	and how to control when the kernel actually frees the object.
1228560	1234600	Well, so long story short, we eventually found a way to exploit it by brute force and race.
1235900	1242040	So to exploit this, first we use a tight loop to create a lot of expect objects.
1242800	1250600	And at some point, the expect count exceeded the value max expected and start to evict the
1250600	1258260	oldest expect object. So the oldest expect object will be placed into the RCU free list.
1258960	1265800	And so on, and so on. And at some point, RCU thread gets scheduled by the kernel
1265800	1273980	and starting to process objects. At this point, we add another thread to trigger
1273980	1280560	out-of-bound decrement in a batch request and keep this system running for a bit.
1282520	1286360	So we hope for a race scenario like this to emerge.
1287520	1294160	First, the expect object allocated into index three of the hash table, and then the expect
1294160	1301380	object we want to out-of-bound write is freed into the RCU free list. Then it continues to
1301380	1308300	free another expect object. After that, we trigger the vulnerability 28 times in a batch
1308300	1315400	request to make the next pointer point to a user-controllable value. And finally, the RCU
1315400	1320360	start to process these free object and runs our set callback function.
1321860	1329180	Great. Let's try it on local build. And we successful successfully control the IP to
1329180	1340460	form, form, form, form. So what about remote? Let's try. And we got an error message while
1340460	1345840	trying to add the lock expression. Since I have no idea what happened on the remote
1345840	1350680	environment, I decided to download the official busy image to debug it locally.
1352360	1359500	So I open the GDB and set some breakpoint on the functions, but when I set the break on
1359500	1365620	nft-lock-destroy function, GDB complained that it was not defined. What's wrong here?
1368100	1374840	I later found that it's because the kernel config I used is different from the one used in
1374840	1381040	the official environment. So the log expression I used to exploit the vulnerability not even
1381040	1388540	compiled into the official image, but I'm using the official build script to view the local
1388540	1396460	test image. Why on earth there can be any difference? I mean, why?
1399540	1405900	Eventually, I found it is caused by the official build script. The mitigation instance is built
1405900	1413800	by COS kernel config plus the Google custom security security patch. And the official
1413800	1424520	mitigation instance use COS config 6.1.55. The nftable expression are enabled in COS config
1424520	1433240	after the version 6.1.64. However, the build script will always fetch the latest COS config,
1433240	1438220	so the mitigation kernel I built has the nftable expression enabled,
1438220	1441820	but in the official image, they're actually disabled.
1443780	1449820	So, well, I think next time I should definitely check out the config first.
1454100	1462840	Okay, so after my second failure, I revisit some old patch and I found a new bug.
1465170	1471490	The patch I was looking at is a patch which introduced the first bug. The title says
1471490	1482470	nftable, a set backend to use GC transaction API. So what does the GC mean? Here it means
1482470	1488390	the set garbage collection. There are some set implementation support user to set a timeout
1488390	1496570	on any set element. And there are two types of GC to clean the expired element.
1496570	1503150	The first one, the synchronous one will be called in the commit phase,
1503150	1509850	and the asynchronous one will be triggered periodically in the background. But here we
1509850	1517970	only focus on the asynchronous one. So back to the patch. In the commit message, it says
1517970	1525510	use GC transaction API to replace the old and buggy GC API and the busy mark approach.
1526190	1534150	So what is the difference between the new and the old one? Let's take a look of the old GC design.
1535110	1542150	In the old design, the synchronous GC will run loglessly and can be triggered at any time.
1542410	1548310	So in order to protect GC threads from raising with the landing API, both of them will check
1549190	1554190	a busy mark set on the element before delete it to prevent double free.
1556330	1563430	But sometimes the developer might forget to check against it. In this case,
1563430	1569110	there's a new function called NFT map deactivate forgot to check a busy mark.
1569270	1576470	And this leads to a double free vulnerability. So in order to avoid this kind of issues,
1576470	1581050	NFT developer decided to replace it with a new design.
1582250	1589070	In the new GC design, it introduced the GC sequence. The GC sequence is a per net variable
1589070	1597090	and it will be increased before and after any modification to the control plan. So it will
1597090	1603750	increase at the beginning and at the end of both the commit and the abort phase.
1604390	1609970	During a GC procedure, GC thread will record and check it to prevent raise.
1610270	1614830	Even though the GC thread still run loglessly, during the check,
1614830	1623630	it will now quad the commit mutex lock. The new GC design look like this.
1625010	1630950	During batch request handling, GC sequence will increase both before and after the commit and
1630950	1638930	abort phase. And in the GC thread, it will first run locklessly to store the GC sequence and
1638930	1645530	collect all the expired elements. After that, it will quad the lock and check if the current GC
1645530	1652270	sequence match the safe one. If they are the same, then the elements will be released.
1653350	1658470	For this normal case, since GC thread does not raise with the batch request,
1658470	1664490	the GC sequence will be the same. And the element can be freed by the GC thread.
1667960	1674480	Next, we check if it still works in the raise case. If during the prepare phase, GC thread
1674480	1681980	triggered and stored the global GC sequence into transfer objects, it will continue to run
1681980	1690680	loglessly to find all the expired elements and wait for the mutex lock.
1692420	1696540	At the same time, the main thread stepped into the commit phase,
1696540	1701340	and some of the elements in the set can be released in this phase.
1702320	1707400	The GC thread will need to wait until the main thread releases the mutex lock,
1707400	1715460	so it can finally check the safe GC sequence and delete the element. But in this case,
1715460	1721160	since GC sequence is recorded in the prepare phase, and we check it after the batch request
1721160	1727000	is done, the GC sequence will be different. So, the element deletion will be skipped to
1727000	1734540	avoid double free. Next, let's check the case if GC sequence is recorded in the commit phase.
1734540	1746300	In this case, since GC sequence increased after the commit phase, the GC thread can only check
1746300	1760800	GC sequence after the commit mutex unlocked. This raise will also be detected. So, to sum up,
1760800	1767640	the new GC design requires both the batch request and the GC sequence check to hold the lock.
1767640	1772120	This ensures that even there's a raise while collecting the expired element,
1772520	1777580	the check will always happen after the GC sequence has increased.
1780670	1789410	But does the batch request always lock mean there's no raise? Actually, if you look closely,
1790090	1801720	we will find, well, it's not always locked. To find where it release the lock, let's first talk
1801720	1807360	about the module autoload. During the prepare phase, if there's some expression type in the
1807360	1815580	request is not found during the type resolution, it will instantly abort and try to load the
1815580	1824080	type module during the end of the abort phase. After that, it will retry the whole batch request
1824080	1833480	again. However, maybe there's some efficiency consideration or to just only prevent that lock
1834080	1841780	before the function tries to load the request module, the mutex lock will be released.
1842480	1844700	But could it cause any problem?
1847080	1853020	Back to the diagram. Now there's a little time window in the abort phase that it will not hold
1853020	1861280	the mutex lock. So there's a chance during the abort phase, the GC check kicks in, recalls the
1861280	1868640	expired element and do a GC sequence check. In this case, however, the check will be bypassed.
1868640	1873260	And the GC thread can delete arbitrary expired elements.
1876770	1881250	Okay, so now the question is, how do we actually exploit the race?
1883110	1890270	Our exploit plan is first, we add a set element with a short timeout. And then we trigger module
1890270	1897610	autoload to force it to revert the commit and free the element we just added. And at the same time,
1897610	1908130	we trigger GC to collect that element. Next, module autoload will release the mutex and we
1908130	1915450	hope that the GC thread will grab it and bypass the check. So as a result, the element we just
1915450	1923670	added will be double free. But in order to trigger double free, there are two time windows we need
1923670	1931290	to enlarge. The first time window is from the abort phase start to the set element released.
1931510	1937820	So the GC thread can recall the expired element before they get a link by the revert procedure.
1939290	1946370	The second time window is from the load module to its end. So the GC thread can grab the lock
1946370	1955770	and bypass the check. For the first one, since the abort phase will revert the operations in
1955770	1962450	reverse order, we add a bunch of requests after the add set element request to delay the link
1962450	1969850	operation. For the second one, we just request multiple unknown type to make module autoload
1969850	1985790	runs longer. And here's the demo. Okay. So first we boot our machine and check the UID is a normal
1985790	1992830	user and then we run the exploit. So here we will first measure the execution time to set up
1992830	1999390	timing parameter for the race. And after that, we set up the environment to enlarge the race time
1999390	2011390	windows. Then we trigger double free to leak address and control IP. And we got the root shell.
2017220	2028740	Thank you, guys. Great. So now I write the exploit and submit it to kernel CTF. And this is my
2028740	2037140	kernel CTF submission. I exploited the LTS instance on kernel version 6.1.81. But as you can see, it
2037140	2044640	is marked with a dupe in that line. It is because for every LTS slot, only the first zero day or one
2044640	2053080	day exploit will be accepted. And there's someone who also submitted a LTS exploit which beat me
2053080	2066100	by nine seconds. That's too bad. So we have to wait for the slot to reopen. So, yes, just wait
2066100	2077260	for about two weeks and the slot will reopen. Right? Wait until it starts to reopen. Yep. But
2077260	2087720	do you know the F table will be disabled for April 1st? What? Actually, there's an announcement
2087720	2093760	on kernel CTF. It says they are going to disable NF tables along with the switch to kernel version
2093760	2104120	6.6. It starts from March 15th but actually they delay the switch so it's from April 1st. Okay.
2104120	2114580	So what's the date today? It's actually March 22nd. And the kernel CTF slot opens about every
2114580	2121240	two weeks on average which means I won't be able to exploit before they disable the NF tables.
2121880	2129360	So on my third kernel CTF attempt, I still could not get a bounty which is pretty sad.
2130800	2139700	To sum up my journey, first I have hope and then I found bug one, a lifetime issue. And it
2139700	2148200	collides with another submission. Then I found the bug two, a hard to use one day. But I used
2148200	2157760	the wrong config so it's unusable. Then I found the bug three, a locking issue. But I lose the
2157760	2166600	submission race and NF table will be disabled so it's killed. However, there's a final plot twist.
2170370	2176850	A few days after, I found there's some changes in the kernel CTF spreadsheet.
2177590	2185190	The person who previously got ahead of me is disqualified due to a bug collision.
2186070	2192390	And so this time, I finally take over the slot and report it to Google.
2194330	2201530	And this month, I got my kernel CTF bounty from Google which is my first Google VIP bounty. I'm
2201530	2215930	so proud of myself. Thanks. So, well, I think this concludes my kernel CTF journey.
2216430	2226830	And thank you, guys. Is there any questions?
2235320	2238560	Yeah, if there's any questions, you can come up here to this microphone.
2257670	2258610	Thank you.
