DSS 2026 — How clean is your code? Dirty Bits in Solidity Doris Benda Source video: https://www.youtube.com/watch?v=MA2kGnDin2k Automatic captions — not human-reviewed. Complete spoken-audio coverage is unverified. Source language tag: en (English). Source export SHA256: 350474ade9c49e32e18cf66c912fdd4c9b3d510993b8e77ecf727d68e5108036 [0:00] Okay. Hello everyone. My name is Doris [0:02] Bender. I am an auditor at Chain [0:04] Security. And in case you don't know [0:06] Chain Security, so I think we're quite [0:07] famous for DeFi protocols. So we have [0:09] audited most of the top-tier protocols [0:12] or like work with them. [0:13] And we have 30 people and also have a [0:15] lot of other areas we now covering. So [0:17] it's not just Solidity, but my talk will [0:20] be mainly about Solidity and Assembly. [0:22] And in case maybe last Friday you have [0:24] heard that Klampet and was delayed and I [0:27] think it's also related to us so in in [0:29] case you in interested in that. So [0:31] Klampet and was delayed on testnet. [0:33] And there will be a talk this afternoon. [0:35] So if you're interested our colleague [0:36] will give that. [0:38] But to my talk I will talk about how [0:40] clean is your code. [0:42] It is about dirty bits and Solidity. [0:46] And I first have a quiz and so I hope [0:48] you can read it. [0:50] I want to know from you what the value [0:52] of C is. So the top few lines is [0:55] Solidity and only the last line is [0:56] Assembly. [0:57] And [0:59] X is like one and Y we why we create we [1:02] shift one by [1:04] 256 bits. So I [1:07] write write down what value that would [1:08] be. [1:09] And because Y is a U and 8 we just look [1:12] at the [1:13] last byte. So the Solidity will look [1:15] into the last byte because it's a U and [1:16] 8. [1:17] So it will truncate the value to zero. [1:19] So Y is zero at the end and I want to [1:22] know what is set when you then read it [1:23] in Assembly. So in Assembly we read we [1:25] add X and Y together. So it's literally [1:28] like one plus zero and I want to know [1:30] what's the result of that. [1:31] And I have three options for you and [1:33] think about it for 10 seconds and then [1:35] we will see the results. [1:39] So I don't ask in in the audience but if [1:41] anyone wants to give an guess you're [1:43] welcome. [1:44] Otherwise [1:46] show it. [1:48] Because [1:49] you can actually be both A or B. [1:51] C is wrong. [1:53] It was a tricky question. So why can it [1:55] be both? [1:57] So it depends actually how you compile [1:59] the code. So the doesn't change. It's [2:01] really that just if you use forge built, [2:03] it's the legacy pipeline and you get the [2:05] this value. [2:07] And if you use a compiler flag, you get [2:09] the other value. And you would maybe [2:10] wonder why. So in the second case, we [2:12] use this intermediate representation is [2:14] a different pipeline. [2:16] And it's it's literally just that this [2:18] part here [2:20] the Y can be two different values [2:21] depending on which pipeline. And then if [2:24] you add them, you get a different value [2:26] for Z. And we look into it why this [2:28] value is different for the different [2:29] pipelines. [2:31] And for that to understand, we look [2:33] first into what is 30 bits. [2:35] So you probably know that the EVM [2:37] operates on stacks on word sizes on the [2:39] stack. So I have one word size here. [2:42] And our Y, it was literally just the [2:45] last byte because it's a uint8. We just [2:47] read and solid to the last byte. And [2:49] it's zero because there's just zero [2:51] zero. [2:52] But it has a dirty bit. So you see like [2:54] the value [2:56] above it, it was like one. [2:58] And the one documentation you find about [3:01] inline assembly in the official [3:03] documentation, it says you cannot make [3:05] any assumption about bits not part of [3:07] the encoding of the type, especially do [3:09] not assume them to be zero. [3:12] So I've written down for some other [3:14] types what it actually means clean. [3:17] I think you don't need to remember just [3:19] for the uint8 because later I will use [3:21] it. And maybe it's interesting if you [3:23] have a positive number. [3:26] Clean means there's like zero zero zero [3:27] and then you actually number. [3:29] If it's a negative number, then it's a [3:31] signed extend version. So you have F F F [3:33] F F in a new actual number. And we will [3:35] use this later. So maybe remember that's [3:37] that's what it means to be clean in in [3:39] an integer like in [3:42] signed integer. [3:46] Okay. [3:47] So now we can maybe think about why does [3:49] actually the compiler behave that way? [3:51] So like why do we actually leave dirt [3:52] there? Why we don't clean up? [3:54] And [3:56] for solid in assembly, it's actually [3:57] that both of them in the end will be [3:59] compiled to EVM opcodes. [4:01] And they operate on these full word [4:03] sizes. [4:04] And some of the operation we do on them, [4:07] they don't doesn't matter if they're [4:09] dirty or not. So the compiler is [4:10] somewhat efficient and say like, okay, [4:12] if I don't need to clean up, why should [4:13] I do it? Because it takes gas. [4:15] I have an example here. [4:17] Here we just look into Solidity. [4:19] So we have Y equals zero, so the normal [4:22] comparison in in Solidity. [4:24] And if we do this on a dirty Y and a [4:27] clean Y, so we use again these two [4:29] examples that we had of the value that [4:30] could be 000 or like our dirty value [4:32] that we had. And you can see if we do [4:34] this in Solidity, [4:36] Solidity just looks at the last byte. [4:38] It's both zero, so both of them are [4:39] true, so it doesn't matter. You get a [4:42] byte result even if it was dirty or not [4:43] dirty. [4:45] If you do this in assembly, so now we [4:46] use equal. [4:48] And in that way, like if you do this in [4:51] assembly, it actually looks at a whole [4:53] raw [4:55] word size. So in that case, actually it [4:57] looks through the whole thing and then [4:58] it's not equal and you get different [5:00] results. [5:01] So in assembly, it matters. In in [5:03] Solidity, it doesn't matter because the [5:04] compiler does these kind of cleanups for [5:06] you. [5:10] And to remember that a little bit [5:12] better, I have some kind of comparison. [5:14] So in the legacy pipeline, yep, [5:17] you can think about it a little bit is [5:19] it's a lazy roommate. It doesn't clean [5:21] up the dishes. It just puts it into the [5:22] sink and then waits until the guest [5:24] comes and then it starts to clean up. [5:26] And how do you see it? [5:28] So for example, this is the Solidity [5:30] code here, and this is the compilation [5:32] to the EVM opcodes. [5:35] And here we do the the left shifts and [5:37] it doesn't clean up. We just leave it on [5:38] the stack like dirty. [5:40] And only then here it cleans up. [5:43] And if you look into the Yul IR [5:45] pipeline, it's different. We clean up [5:46] immediately. So you see here we do the [5:48] cleanup step immediately and then if you [5:51] later do comparison, it's already kind [5:52] of clean. [5:53] And that's why you get different values. [5:56] And for some comparison, I have found [5:58] some interesting cases [6:01] yeah, where we get actually very [6:02] different values depending on if you use [6:03] the legacy pipeline or the YAR IR and [6:06] you read out the word size you get see [6:08] here [6:09] the values are actually very different [6:10] and they could be potentially bugs if [6:12] you not aware of these different [6:13] behaviors. [6:17] Um maybe also something we can think [6:20] about is like do we actually need to [6:21] talk about it anymore? Is legacy [6:23] pipeline even used nowadays? And it's [6:25] quite much still used [6:27] because if you just use the main forge [6:30] build the command you still use the [6:32] legacy pipeline [6:33] only if you enabled it explicitly like [6:36] you need to say YAR then it you see the [6:38] newer pipeline. [6:39] The same thing is for hard head you [6:42] by default still use like the old [6:43] pipeline. [6:44] The other thing is like I did just a [6:45] search on some main net contracts on one [6:48] day and just compared it and there were [6:49] still 61% were like legacy pipeline the [6:52] rest were like the newer pipelines so [6:54] still kind of 50/50 [6:56] and if you look in the Solidity [6:57] Developer Survey from 2025 then 35 of [7:01] the [7:02] um respondents even don't know what the [7:04] new pipeline is so they definitely [7:05] probably not going to use it if they not [7:07] even know what it is. [7:10] Uh so that was like one reason or one [7:13] area where different um [7:15] dirty bits can appear and behave [7:16] differently. I found another area where [7:19] dirty bits also make a difference and [7:21] it's uh the the decoders. We have the [7:23] API decoder version one and version two [7:26] and for a very kind of simplified table [7:28] I put here like you can think about it [7:30] like if Solidity is um below 0.8 the [7:33] version we used the [7:35] decompiler at a decoder version one [7:37] and if you in the Solidity 0.8 or like [7:40] above or equal we use um the the second [7:43] decompiler. It's a bit um very broad [7:46] this assumption because there's a lot of [7:47] exceptions but we don't look into it for [7:50] now. [7:51] And for the different decoders and how [7:52] they behave in a simplified way way you [7:55] can also say the version one decoder [7:57] decoder it masks dirty bits and version [7:59] two it reverts on the dirty arguments. [8:02] It's a bit simplification like the real [8:04] statements this in the documentation so [8:06] read it it's not 100% accurate, but for [8:08] this target it will work. [8:12] And okay, I give you a real example [8:14] where these two two different decoders [8:15] actually mattered. [8:17] And this is an example from Soulmate. So [8:19] we look into this function here. [8:21] We want to transfer a token. So it's a [8:23] you have a token, you have a two [8:25] receiving um account, and you have some [8:28] amounts. [8:29] And maybe you know that Soulmate um it [8:31] uses a lot of assembly code. [8:33] And this is the code example from before [8:35] June 2023, so now it's fixed. So don't [8:38] worry, it's not like that anymore. [8:40] Um so it has the two address. This one, [8:43] it can be dirty potentially if the input [8:46] um the user inputs a dirty um address. [8:49] And then at that point we want to call [8:51] the actually token and transfer the [8:52] amount. And now this funny thing [8:54] happens. Okay, if we call this token, [8:56] this token can either be a one that was [8:58] compiled with decoder version two, or it [9:00] can be one that was compiled with [9:02] version [9:04] one with the decoder. [9:06] And the behavior of these two types of [9:07] tokens actually is very different [9:09] because if you put in dirty arguments [9:11] and go through such a token, you it [9:14] works. Like you you get your tokens [9:16] transferred. [9:17] But we found some very interesting kind [9:19] of tokens here. [9:21] Uh like this works and this one rejects. [9:23] And it's very very unexpected that why [9:25] these tokens reject because they use the [9:26] other decoder. And at that point if they [9:29] are dirty their addresses, then um the [9:32] transaction rewards because it doesn't [9:34] allow the new decoder doesn't allow [9:35] dirty bits. [9:37] And um yeah, these are kind of contracts [9:39] which are with the newer decoder. So [9:43] and I think um the library was fixed at [9:45] some point when they discovered that [9:47] this different behavior exists. [9:51] And yeah, also very maybe unaware [9:54] situation where differences can happen [9:56] is if you compare functions and [9:58] fallbacks. [9:59] So we have here a little bit of assembly [10:01] code. We we think still about like [10:02] wanting to transfer a token. So we want [10:04] to read the two address from the call [10:05] data. [10:06] And this assembly block we can put it [10:08] into two different functions. So either [10:09] we put it into a withdraw function, is a [10:11] kind of a normal function, [10:13] or we put it into a fallback function. [10:15] And then we compare, okay, what happens [10:16] if we use [10:17] decoders? And maybe you would think [10:19] everything behaves the same, [10:21] but we have one exception. [10:23] If you have the decoder V2, you revert [10:26] if you input dirty bits, like if you [10:28] call this function with a two address [10:30] that's dirty, [10:31] but all the other cases it still works [10:33] fine. Like you you dirty address go [10:35] through and your function succeeds. [10:37] And especially this case is maybe [10:40] unexpected because why we don't revert [10:43] here? Because actually in the fallback [10:45] function we don't have any input [10:46] parameter defined, so it doesn't the [10:48] decoder doesn't check. So actually now [10:50] dirty bits go through through the [10:51] fallback function and suddenly appear in [10:53] your fallback function. [10:58] Okay, so the second part of the talk is [11:00] about Now I want to explain what are [11:02] sources and sinks of dirty bits. [11:04] So to actually have a dirty bit bug, you [11:07] need to have an area where you create [11:08] dirty bits or dirty bits appear, [11:10] and then you need to have an area that [11:12] minds about the dirty bits. So you need [11:13] to have these two areas. If you only [11:14] have one side, then it's actually not a [11:16] dirty bit. Because a lot of reports we [11:18] see that people submit, they have either [11:20] sources or sinks, but it's not maybe [11:22] just a low low issue we've defined it, [11:24] but if they find both, it's usually a [11:26] high critical issue. [11:30] What could be like a kind of source of [11:32] dirty bits? We have a few examples, and [11:34] what could be a sink also a few [11:36] examples. Actually it's just a [11:38] small list because the real list I have [11:39] put it on the blog post because it's [11:41] much more areas we found where dirty [11:42] bits appear and can be graded and where [11:45] sinks [11:46] are created or [11:47] maybe some interesting things like the [11:48] compiler can be of course [11:51] source of dirty bits either as a sink or [11:53] a source if there's some compiler bugs. [11:55] Also like the call data or the return [11:57] data can be dirty can be a source of [11:58] dirty bits which people maybe don't [12:00] expect. [12:01] And also legacy pipeline especially some [12:03] of the operation create dirty bits. [12:06] On the sinks like maybe hashing is very [12:08] common like of course it should be if [12:10] there is some dirty bits in it that you [12:12] get a different result. [12:17] Um yeah here for comparison for people [12:19] who want to hunt for these kind of dirty [12:21] bits where to look for it so I am [12:24] decided to make here sources [12:26] and sink [12:27] sinks so if the source is in solidity [12:30] and sink is in assembly then the main [12:32] risk where we found bugs were in the [12:34] legacy pipeline because of that reason [12:36] that the legacy pipeline doesn't clean [12:37] up [12:38] and immediately but a little bit later. [12:41] Of course if you have assembly and [12:43] assembly meaning the source is an [12:44] assembly and the sink is an assembly you [12:45] have [12:46] in both pipelines quite high risk for [12:48] dirty bits [12:49] because nobody cleans up for you in that [12:51] way. [12:52] And if I mean assembly assembly it could [12:53] be also that in between there's a [12:54] solidity block it means just if you [12:56] haven't touched the value in between by [12:58] solidity then it's still assembly [13:00] assembly case. [13:02] And then for the assembly solidity case [13:04] in the last row we only found one [13:06] exception that is here in the YIR [13:08] pipeline and I will show it in the next [13:09] slide. [13:10] And it was also very interesting because [13:12] now actually the YIR pipeline is maybe [13:14] problematic and maybe you were not aware [13:16] of that. [13:19] So there's like one oddity I found and [13:21] it's actually an open issue for two [13:23] years already on the compiler but nobody [13:25] seems to fix it. [13:27] And what what happens here so we have an [13:30] array it's in storage it has like four [13:32] values in it [13:33] and then again we have our U and eight [13:36] which we then in assembly we add some [13:38] dirty bits to it so that this is our [13:39] source of dirty bits. [13:40] And then we have two lines where we read [13:42] the D. [13:44] And you would maybe expect that D is the [13:46] same but the require in both cases even [13:48] if you use legacy or YIR both of the [13:50] require lines pass. [13:52] But then suddenly in the next line a [13:54] problem happens because in the YIR [13:56] pipeline suddenly you panic and you see [13:58] a different value like D is suddenly [13:59] 257. [14:01] And [14:02] why we panic is just that we we kind of [14:04] read the array outside its length that's [14:06] why we the panic. [14:08] And for some reason the compiler doesn't [14:09] clean up in that case so it's a compiler [14:11] bug. [14:12] It still exists. [14:15] Oh wait. [14:17] Yeah. And another areas where we found [14:19] bug dirty bits was that people who use a [14:21] lot of assembly, they they were now [14:23] didn't do clean up their their values. [14:25] But of course, sometimes it happens that [14:27] they do it in a wrong way. So I call it [14:28] like washing programs. So if you do the [14:30] wrong way you're washing, things can go [14:32] wrong. And one thing that we found here [14:33] was like [14:35] we had kind of a Uniswap V three kind of [14:37] pool. [14:38] We have the ticks. Ticks are kind of [14:39] prices in this pool. [14:41] And you want to get this value clean out [14:43] out of out of assembly. So the first [14:45] line here, we kind of shifted to the [14:47] right so the ticks are on the right side [14:48] now. [14:49] And then we have here assembly to clean [14:51] it. So we can also do it either do it [14:53] like that. If people do it like that, [14:55] they get a wrong value. If they do it [14:56] the right way, it's sign extend and they [14:58] get the right value. So often people mix [15:00] up how to clean things up and then bugs [15:02] happen. [15:04] And to remember, I found the five most [15:06] important types and gave you some care [15:08] labels how to clean them. [15:10] Uh we don't need to go through them in [15:11] detail, but like please remember to [15:13] clean your things correctly. [15:15] Uh like some tips I get for give you for [15:18] the your next washing cycle. I mean, you [15:20] should always wash at both ends of your [15:21] assembly. Like if you get dirty or [15:23] values into your assembly, make sure [15:24] it's clean if you use it. If you get it [15:26] out of assembly, make sure it's clean. [15:28] And if you have some things in in in the [15:30] assembly, make sure the values are clean [15:32] if you operate on them. [15:34] And then you should always of course [15:35] wash what you ship, meaning if you use [15:36] legacy pipeline test with legacy [15:38] pipeline, if you say [15:39] IR pipeline, test with that. [15:42] And also use the tokens that you [15:43] actually use on mainnet, not um [15:45] uh other tokens. [15:47] And you should fast with the solidity [15:49] that you can use. And then also for [15:51] auditors, you should try to find the [15:52] sink and the source. [15:54] Okay, I think I'm done. And yeah, if you [15:57] have any [15:58] information or can you can talk to me [16:00] afterwards.