Talk: EVM and Solidity: Same but Different

Secureum Talks · 15:11

Gonzalo, co-founder of ConsenSys Diligence, explains why the EVM and Solidity are easy to confuse and how that confusion creates vulnerabilities. A real bug from the Zodiac scope guard shows how short calldata is padded with zeros and lands on the fallback function. The talk then covers storage layout, safeTransfer differences between ERC20 and ERC721, and the habit of hunting for mismatches between specification and implementation. Q&A touches on Vyper, audit workflow and work with the Solidity team.

Topics

From Secureum Talks; community: Secureum.

Transcript

[Music] i'm gonna start this with a request rajiv next time let's make this in the beginning of the week because this has taken the best of me so tired so tired um hey uh for those of you that do not know me um as rajiv said i'm the co-founder of consensus diligence um and i've been doing this for the past five years and by this i mean um

providing services of offensive security into weaponry space more specifically on ethereum and um getting more and more involved with solidity in evm both at the compiler level and at an eip level um and thinking about how i could help all the people that are now starting to get into the space and and all the education efforts that securium has going on i thought the best way to do that would be to give concrete advice so we're going to start a little bit abstract but we're going to zone in into what is hopefully very precise and cool advice if you're

just starting to think about security whether you're a developer or whether you want to be an auditor so let's start with the fact that evm the evm itself is very different from solidity um and let's illustrate this with an example i don't know if you guys can see it from the audience and i also don't know if you guys are prepared to do security this early but does anyone want to take a job of what is wrong here by the way this this code base is from gnosis safe zodiac um new scope card so this was a bug that i found late last year that actually got me thinking about

the exact thing that we're gonna uh talk about here today right uh which is the differences and mismatches of things that look similar the ways your brain tricks you into thinking that some things have fuzzy boundaries so i'll go ahead this is this is not a very obvious one what is happening here is that um the zodiac team while writing this code made an assumption about the first four bytes of call data right what you're seeing here is the scope card is a is a function that actually um as the name indicates

guards the scope of what can be accessed by certain uh transactions going out of the multisig and so it permissions based on function ids right based on function signatures function signatures and functionalities are basically just like um the the cat check hash of the canonical types of a function you you take the ketchup hash reduce it to the first four bytes and that gives you the identifier to get to a certain function within a smart contract right when they were writing this code they assumed that either your call data has more than four bytes and then you go into a list and see whether that

those four bytes are permitted or within the scope or you have less than four bytes and therefore your call date is going to be zero and so it's permissioned by the fallback function and this is oh did we lose the slides no oh i have to keep clicking my sorry guys no we're on sorry um in this is the a very good representation exactly of how these fuzzy lines

might might get you into trouble right the fact is that jump tables and the abi are not an evm construct um it's fine we'll keep going um the fact is that jump tables and the abi are not an evm construct right and so what happened here is that they forgot that if you send less than four four bytes of call data the evm still extends that space with zeros if you try to access longer what this means is that if i have a function id that ends with zero byte then it'll actually fall

on the fallback function case and therefore i could access other contracts functions that were not within scope just because the fallback function was a permitted function for that contract uh and again we're going to zone in this started off very abstract right uh but what this is in its essence is a mismatch um other examples of mismatches between um the the evm and solidity are the other examples of like fuzzy lines or for example the storage model and types right newcomers new developers might think that the storage model is something that is native something that ethereum itself

has but it's not solidity actually created an an addressable space out of out of uh mappings and ketchup hashes and all of that but but none of these things are really native you know solidity has its own way of handling it but this is down to the compiler and this is the the trying to to get this into precise advice vulnerabilities pretty much exist as mismatches if you are trying to think about how to secure stuff or how how to find problems within code bases you have to look for mismatches these types of mismatches

where where does the evm and where the solidity begin right and these mismatches present themselves in business logic as well not only in code related bugs or or the execution environment boundaries um and i made a nice graphic for you guys to enjoy uh vulnerabilities are exactly the thing that sticks in between differences of the specification and the implementation if we're talking about business logic right that's what an auditor does really um so you have a certain expectation of how your code should behave

and then you implement it slightly different and there comes an attacker and finds space in between these two boundaries to come and wreck it all steal all your money um a good example of this is that safe transfer does not work the same way for erc20 and erc721 right if you think about it one of them has has a callback one of them might include reentrancy issues re-entry by itself is not bad but one of them might cause reentrancy issues the other definitely does not but from the outside for someone that is again a newcomer someone that is like not yet this hasn't yet found where these boundaries lie this might create space

for um for attackers again your job as a knob as an auditor is literally identifying the boundaries first and that's also part of like gaining knowledge you should read docs you should you should you know you should inform yourself and then finding the vulnerabilities in the fringes and and i'll end this with what is hopefully actionable advice i think that if you start auditing or reviewing your code as a developer you know doesn't this doesn't need to be specific advice for auditors but if you're reviewing any piece of code the moment you hear your mind

say something different a different concept and in the in the event of safe transfer if you're talking to your colleagues and and they're like oh but we're going to use this with erc721 your mind should light up and go back to the code and think oh this might throw everything into let me just reassess that is the fringe um and oh and this is another thing uh again for new auditors for all the people coming out of securium um feel free to go this is this is a link uh i distributed the the presentation uh on twitter um but happy to to distribute the links uh afterwards somehow go read about why

security is really really hard it's very very rewarding as well but uh go read about why it's hard first and that is all guys thank you for listening any questions for any questions for gonzalo i guess this is very non-contentious topic it's fine

[Music] all right [Music] where they just repeat the question do you know examples from other languages where wiper like other languages smart contact languages like viper and i know what else is there but these things happen oh yeah absolutely it'll happen in all languages and that problem specifically has to do with the abi right so it's a standard that is like it's literally how contracts interact with each other so

viper bites to that as well um i do not know specifically like about the jump table implementation in viper contracts um so i i would wouldn't be capable of telling you whether this specific problem would surface in a viper contract i guess it would um but at the very end of the day this will happen in any execution environment with any language any compiler you know it doesn't need to specifically be about web3 we just made the examples about web3 because you know dog footing but but this will happen anywhere right the mindset that you need to know what is exactly happening below

in in order to reason about the higher level in order to reason about source code is appliable to pretty much anything mindset [Music] right we have second question i think the first one is the quickest remind yourself so what's like the set of steps that you usually go through when you start auditing a contract or a set of smart contracts yeah the instructions try trying to keep it like again generalizable not uh down to what we do with diligence um i would say that uh like if when you

start doing these things you uh go from the general to the specific right i'll first look at the entire code build all the visualizations stuff that helps me mentally map exactly what the code is doing and then start reading the specific things start going like modularly from function to function like functionally so like the the visualizations are also good to to to reason about the the way a program is working so basically like general to specific and start following down some rabbit holes about things that look like hot spots again like i think

tools play a big role in aiding with the with the with the visualization and understanding of systems and then comes the human brain and the manual part and we just like go again down the rabbit hole um [Music] i'll repeat the question so uh the question is whether we work uh closely with the ethereum foundation since and more specifically the solidity team uh if because we see so many contracts today so yes um and this says i think i was

i was first involved with solidity and and then diligence happened so that is definitely a thing and and we keep talking to the solidity team because like the the the sheer amount of examples of code bases that we see are actually relevant information for them to shape the language going forward uh at diligence specifically uh one of our axe daily members called steve marks even created a a mock language so we we actually built a compiler with all the security features that we would like to see imbued into solidity and they ended up picking up one or two so yeah very very active well as as much as time permits you know like at the end of the day we still do audits

but but time permitting yes we we like to be as involved as possible in in shaping solidity's future and we're going to do a shout out this year's underhanded solidity context contest had very little submissions guys so next year if you get into it you should definitely submit a an entry to the contest okay any more questions for gonzalo all right thank you gonzalo for uh for the wonderful talk thank you guys

[Music]

Related talks