DSS

DSS 2026 / Day 2 / Pearl Hall

How clean is your code? Dirty Bits in Solidity

Doris Benda

16:03 · Recording

Watch on YouTube ↗Open recording viewerOriginal programme entry
Download plain textDownload timestamped JSON

Source: YouTube automatic caption track · Exported 2026-11-16. Timestamps link to the source video. Cue end times were not supplied.

Transcript

0:00Okay. Hello everyone. My name is Doris

0:02Bender. I am an auditor at Chain

0:04Security. And in case you don't know

0:06Chain Security, so I think we're quite

0:07famous for DeFi protocols. So we have

0:09audited most of the top-tier protocols

0:12or like work with them.

0:13And we have 30 people and also have a

0:15lot of other areas we now covering. So

0:17it's not just Solidity, but my talk will

0:20be mainly about Solidity and Assembly.

0:22And in case maybe last Friday you have

0:24heard that Klampet and was delayed and I

0:27think it's also related to us so in in

0:29case you in interested in that. So

0:31Klampet and was delayed on testnet.

0:33And there will be a talk this afternoon.

0:35So if you're interested our colleague

0:36will give that.

0:38But to my talk I will talk about how

0:40clean is your code.

0:42It is about dirty bits and Solidity.

0:46And I first have a quiz and so I hope

0:48you can read it.

0:50I want to know from you what the value

0:52of C is. So the top few lines is

0:55Solidity and only the last line is

0:56Assembly.

0:57And

0:59X is like one and Y we why we create we

1:02shift one by

1:04256 bits. So I

1:07write write down what value that would

1:08be.

1:09And because Y is a U and 8 we just look

1:12at the

1:13last byte. So the Solidity will look

1:15into the last byte because it's a U and

1:168.

1:17So it will truncate the value to zero.

1:19So Y is zero at the end and I want to

1:22know what is set when you then read it

1:23in Assembly. So in Assembly we read we

1:25add X and Y together. So it's literally

1:28like one plus zero and I want to know

1:30what's the result of that.

1:31And I have three options for you and

1:33think about it for 10 seconds and then

1:35we will see the results.

1:39So I don't ask in in the audience but if

1:41anyone wants to give an guess you're

1:43welcome.

1:44Otherwise

1:46show it.

1:48Because

1:49you can actually be both A or B.

1:51C is wrong.

1:53It was a tricky question. So why can it

1:55be both?

1:57So it depends actually how you compile

1:59the code. So the doesn't change. It's

2:01really that just if you use forge built,

2:03it's the legacy pipeline and you get the

2:05this value.

2:07And if you use a compiler flag, you get

2:09the other value. And you would maybe

2:10wonder why. So in the second case, we

2:12use this intermediate representation is

2:14a different pipeline.

2:16And it's it's literally just that this

2:18part here

2:20the Y can be two different values

2:21depending on which pipeline. And then if

2:24you add them, you get a different value

2:26for Z. And we look into it why this

2:28value is different for the different

2:29pipelines.

2:31And for that to understand, we look

2:33first into what is 30 bits.

2:35So you probably know that the EVM

2:37operates on stacks on word sizes on the

2:39stack. So I have one word size here.

2:42And our Y, it was literally just the

2:45last byte because it's a uint8. We just

2:47read and solid to the last byte. And

2:49it's zero because there's just zero

2:51zero.

2:52But it has a dirty bit. So you see like

2:54the value

2:56above it, it was like one.

2:58And the one documentation you find about

3:01inline assembly in the official

3:03documentation, it says you cannot make

3:05any assumption about bits not part of

3:07the encoding of the type, especially do

3:09not assume them to be zero.

3:12So I've written down for some other

3:14types what it actually means clean.

3:17I think you don't need to remember just

3:19for the uint8 because later I will use

3:21it. And maybe it's interesting if you

3:23have a positive number.

3:26Clean means there's like zero zero zero

3:27and then you actually number.

3:29If it's a negative number, then it's a

3:31signed extend version. So you have F F F

3:33F F in a new actual number. And we will

3:35use this later. So maybe remember that's

3:37that's what it means to be clean in in

3:39an integer like in

3:42signed integer.

3:46Okay.

3:47So now we can maybe think about why does

3:49actually the compiler behave that way?

3:51So like why do we actually leave dirt

3:52there? Why we don't clean up?

3:54And

3:56for solid in assembly, it's actually

3:57that both of them in the end will be

3:59compiled to EVM opcodes.

4:01And they operate on these full word

4:03sizes.

4:04And some of the operation we do on them,

4:07they don't doesn't matter if they're

4:09dirty or not. So the compiler is

4:10somewhat efficient and say like, okay,

4:12if I don't need to clean up, why should

4:13I do it? Because it takes gas.

4:15I have an example here.

4:17Here we just look into Solidity.

4:19So we have Y equals zero, so the normal

4:22comparison in in Solidity.

4:24And if we do this on a dirty Y and a

4:27clean Y, so we use again these two

4:29examples that we had of the value that

4:30could be 000 or like our dirty value

4:32that we had. And you can see if we do

4:34this in Solidity,

4:36Solidity just looks at the last byte.

4:38It's both zero, so both of them are

4:39true, so it doesn't matter. You get a

4:42byte result even if it was dirty or not

4:43dirty.

4:45If you do this in assembly, so now we

4:46use equal.

4:48And in that way, like if you do this in

4:51assembly, it actually looks at a whole

4:53raw

4:55word size. So in that case, actually it

4:57looks through the whole thing and then

4:58it's not equal and you get different

5:00results.

5:01So in assembly, it matters. In in

5:03Solidity, it doesn't matter because the

5:04compiler does these kind of cleanups for

5:06you.

5:10And to remember that a little bit

5:12better, I have some kind of comparison.

5:14So in the legacy pipeline, yep,

5:17you can think about it a little bit is

5:19it's a lazy roommate. It doesn't clean

5:21up the dishes. It just puts it into the

5:22sink and then waits until the guest

5:24comes and then it starts to clean up.

5:26And how do you see it?

5:28So for example, this is the Solidity

5:30code here, and this is the compilation

5:32to the EVM opcodes.

5:35And here we do the the left shifts and

5:37it doesn't clean up. We just leave it on

5:38the stack like dirty.

5:40And only then here it cleans up.

5:43And if you look into the Yul IR

5:45pipeline, it's different. We clean up

5:46immediately. So you see here we do the

5:48cleanup step immediately and then if you

5:51later do comparison, it's already kind

5:52of clean.

5:53And that's why you get different values.

5:56And for some comparison, I have found

5:58some interesting cases

6:01yeah, where we get actually very

6:02different values depending on if you use

6:03the legacy pipeline or the YAR IR and

6:06you read out the word size you get see

6:08here

6:09the values are actually very different

6:10and they could be potentially bugs if

6:12you not aware of these different

6:13behaviors.

6:17Um maybe also something we can think

6:20about is like do we actually need to

6:21talk about it anymore? Is legacy

6:23pipeline even used nowadays? And it's

6:25quite much still used

6:27because if you just use the main forge

6:30build the command you still use the

6:32legacy pipeline

6:33only if you enabled it explicitly like

6:36you need to say YAR then it you see the

6:38newer pipeline.

6:39The same thing is for hard head you

6:42by default still use like the old

6:43pipeline.

6:44The other thing is like I did just a

6:45search on some main net contracts on one

6:48day and just compared it and there were

6:49still 61% were like legacy pipeline the

6:52rest were like the newer pipelines so

6:54still kind of 50/50

6:56and if you look in the Solidity

6:57Developer Survey from 2025 then 35 of

7:01the

7:02um respondents even don't know what the

7:04new pipeline is so they definitely

7:05probably not going to use it if they not

7:07even know what it is.

7:10Uh so that was like one reason or one

7:13area where different um

7:15dirty bits can appear and behave

7:16differently. I found another area where

7:19dirty bits also make a difference and

7:21it's uh the the decoders. We have the

7:23API decoder version one and version two

7:26and for a very kind of simplified table

7:28I put here like you can think about it

7:30like if Solidity is um below 0.8 the

7:33version we used the

7:35decompiler at a decoder version one

7:37and if you in the Solidity 0.8 or like

7:40above or equal we use um the the second

7:43decompiler. It's a bit um very broad

7:46this assumption because there's a lot of

7:47exceptions but we don't look into it for

7:50now.

7:51And for the different decoders and how

7:52they behave in a simplified way way you

7:55can also say the version one decoder

7:57decoder it masks dirty bits and version

7:59two it reverts on the dirty arguments.

8:02It's a bit simplification like the real

8:04statements this in the documentation so

8:06read it it's not 100% accurate, but for

8:08this target it will work.

8:12And okay, I give you a real example

8:14where these two two different decoders

8:15actually mattered.

8:17And this is an example from Soulmate. So

8:19we look into this function here.

8:21We want to transfer a token. So it's a

8:23you have a token, you have a two

8:25receiving um account, and you have some

8:28amounts.

8:29And maybe you know that Soulmate um it

8:31uses a lot of assembly code.

8:33And this is the code example from before

8:35June 2023, so now it's fixed. So don't

8:38worry, it's not like that anymore.

8:40Um so it has the two address. This one,

8:43it can be dirty potentially if the input

8:46um the user inputs a dirty um address.

8:49And then at that point we want to call

8:51the actually token and transfer the

8:52amount. And now this funny thing

8:54happens. Okay, if we call this token,

8:56this token can either be a one that was

8:58compiled with decoder version two, or it

9:00can be one that was compiled with

9:02version

9:04one with the decoder.

9:06And the behavior of these two types of

9:07tokens actually is very different

9:09because if you put in dirty arguments

9:11and go through such a token, you it

9:14works. Like you you get your tokens

9:16transferred.

9:17But we found some very interesting kind

9:19of tokens here.

9:21Uh like this works and this one rejects.

9:23And it's very very unexpected that why

9:25these tokens reject because they use the

9:26other decoder. And at that point if they

9:29are dirty their addresses, then um the

9:32transaction rewards because it doesn't

9:34allow the new decoder doesn't allow

9:35dirty bits.

9:37And um yeah, these are kind of contracts

9:39which are with the newer decoder. So

9:43and I think um the library was fixed at

9:45some point when they discovered that

9:47this different behavior exists.

9:51And yeah, also very maybe unaware

9:54situation where differences can happen

9:56is if you compare functions and

9:58fallbacks.

9:59So we have here a little bit of assembly

10:01code. We we think still about like

10:02wanting to transfer a token. So we want

10:04to read the two address from the call

10:05data.

10:06And this assembly block we can put it

10:08into two different functions. So either

10:09we put it into a withdraw function, is a

10:11kind of a normal function,

10:13or we put it into a fallback function.

10:15And then we compare, okay, what happens

10:16if we use

10:17decoders? And maybe you would think

10:19everything behaves the same,

10:21but we have one exception.

10:23If you have the decoder V2, you revert

10:26if you input dirty bits, like if you

10:28call this function with a two address

10:30that's dirty,

10:31but all the other cases it still works

10:33fine. Like you you dirty address go

10:35through and your function succeeds.

10:37And especially this case is maybe

10:40unexpected because why we don't revert

10:43here? Because actually in the fallback

10:45function we don't have any input

10:46parameter defined, so it doesn't the

10:48decoder doesn't check. So actually now

10:50dirty bits go through through the

10:51fallback function and suddenly appear in

10:53your fallback function.

10:58Okay, so the second part of the talk is

11:00about Now I want to explain what are

11:02sources and sinks of dirty bits.

11:04So to actually have a dirty bit bug, you

11:07need to have an area where you create

11:08dirty bits or dirty bits appear,

11:10and then you need to have an area that

11:12minds about the dirty bits. So you need

11:13to have these two areas. If you only

11:14have one side, then it's actually not a

11:16dirty bit. Because a lot of reports we

11:18see that people submit, they have either

11:20sources or sinks, but it's not maybe

11:22just a low low issue we've defined it,

11:24but if they find both, it's usually a

11:26high critical issue.

11:30What could be like a kind of source of

11:32dirty bits? We have a few examples, and

11:34what could be a sink also a few

11:36examples. Actually it's just a

11:38small list because the real list I have

11:39put it on the blog post because it's

11:41much more areas we found where dirty

11:42bits appear and can be graded and where

11:45sinks

11:46are created or

11:47maybe some interesting things like the

11:48compiler can be of course

11:51source of dirty bits either as a sink or

11:53a source if there's some compiler bugs.

11:55Also like the call data or the return

11:57data can be dirty can be a source of

11:58dirty bits which people maybe don't

12:00expect.

12:01And also legacy pipeline especially some

12:03of the operation create dirty bits.

12:06On the sinks like maybe hashing is very

12:08common like of course it should be if

12:10there is some dirty bits in it that you

12:12get a different result.

12:17Um yeah here for comparison for people

12:19who want to hunt for these kind of dirty

12:21bits where to look for it so I am

12:24decided to make here sources

12:26and sink

12:27sinks so if the source is in solidity

12:30and sink is in assembly then the main

12:32risk where we found bugs were in the

12:34legacy pipeline because of that reason

12:36that the legacy pipeline doesn't clean

12:37up

12:38and immediately but a little bit later.

12:41Of course if you have assembly and

12:43assembly meaning the source is an

12:44assembly and the sink is an assembly you

12:45have

12:46in both pipelines quite high risk for

12:48dirty bits

12:49because nobody cleans up for you in that

12:51way.

12:52And if I mean assembly assembly it could

12:53be also that in between there's a

12:54solidity block it means just if you

12:56haven't touched the value in between by

12:58solidity then it's still assembly

13:00assembly case.

13:02And then for the assembly solidity case

13:04in the last row we only found one

13:06exception that is here in the YIR

13:08pipeline and I will show it in the next

13:09slide.

13:10And it was also very interesting because

13:12now actually the YIR pipeline is maybe

13:14problematic and maybe you were not aware

13:16of that.

13:19So there's like one oddity I found and

13:21it's actually an open issue for two

13:23years already on the compiler but nobody

13:25seems to fix it.

13:27And what what happens here so we have an

13:30array it's in storage it has like four

13:32values in it

13:33and then again we have our U and eight

13:36which we then in assembly we add some

13:38dirty bits to it so that this is our

13:39source of dirty bits.

13:40And then we have two lines where we read

13:42the D.

13:44And you would maybe expect that D is the

13:46same but the require in both cases even

13:48if you use legacy or YIR both of the

13:50require lines pass.

13:52But then suddenly in the next line a

13:54problem happens because in the YIR

13:56pipeline suddenly you panic and you see

13:58a different value like D is suddenly

13:59257.

14:01And

14:02why we panic is just that we we kind of

14:04read the array outside its length that's

14:06why we the panic.

14:08And for some reason the compiler doesn't

14:09clean up in that case so it's a compiler

14:11bug.

14:12It still exists.

14:15Oh wait.

14:17Yeah. And another areas where we found

14:19bug dirty bits was that people who use a

14:21lot of assembly, they they were now

14:23didn't do clean up their their values.

14:25But of course, sometimes it happens that

14:27they do it in a wrong way. So I call it

14:28like washing programs. So if you do the

14:30wrong way you're washing, things can go

14:32wrong. And one thing that we found here

14:33was like

14:35we had kind of a Uniswap V three kind of

14:37pool.

14:38We have the ticks. Ticks are kind of

14:39prices in this pool.

14:41And you want to get this value clean out

14:43out of out of assembly. So the first

14:45line here, we kind of shifted to the

14:47right so the ticks are on the right side

14:48now.

14:49And then we have here assembly to clean

14:51it. So we can also do it either do it

14:53like that. If people do it like that,

14:55they get a wrong value. If they do it

14:56the right way, it's sign extend and they

14:58get the right value. So often people mix

15:00up how to clean things up and then bugs

15:02happen.

15:04And to remember, I found the five most

15:06important types and gave you some care

15:08labels how to clean them.

15:10Uh we don't need to go through them in

15:11detail, but like please remember to

15:13clean your things correctly.

15:15Uh like some tips I get for give you for

15:18the your next washing cycle. I mean, you

15:20should always wash at both ends of your

15:21assembly. Like if you get dirty or

15:23values into your assembly, make sure

15:24it's clean if you use it. If you get it

15:26out of assembly, make sure it's clean.

15:28And if you have some things in in in the

15:30assembly, make sure the values are clean

15:32if you operate on them.

15:34And then you should always of course

15:35wash what you ship, meaning if you use

15:36legacy pipeline test with legacy

15:38pipeline, if you say

15:39IR pipeline, test with that.

15:42And also use the tokens that you

15:43actually use on mainnet, not um

15:45uh other tokens.

15:47And you should fast with the solidity

15:49that you can use. And then also for

15:51auditors, you should try to find the

15:52sink and the source.

15:54Okay, I think I'm done. And yeah, if you

15:57have any

15:58information or can you can talk to me

16:00afterwards.

Source and accuracy details

Source export SHA256: 350474ade9c49e32e18cf66c912fdd4c9b3d510993b8e77ecf727d68e5108036