Intro to Security First Development – Gonçalo Sá
ETHDenver 2020 · 14 Feb 2020 · 13:11
At ETHDenver 2020, ConsenSys Diligence co-founder Gonçalo Sá introduces security-first development for Ethereum, arguing that defensive design should shape smart contracts from the first line of code rather than being bolted on through late-stage audits. He covers common vulnerability patterns and practices for reducing risk before deployment. The talk draws on his experience auditing production Ethereum contracts.
Speakers
Topics
From ETHDenver 2020, hosted by ETH Denver.
Transcript
I'm gonna talk about like an intro to Experian I realized that like probably everybody already knows about a theorem so we're gonna play a few like game meant to educate you on solidity security and I'm gonna distribute these beanies to everybody like that gets like right answer so yeah this is me Gonzalo and this is a presentation built by our bar colleague Marillion so thank you to him awesome game and let's play how familiar are we with solidity in the
room do you guys write code have your written code before yes do you guys want to be me let's go so name that vulnerability round one [Music] wrong with this what no yeah that's that's kind of that's kind of it like the transfer the the problematic line is in transfer but what can go wrong here it fails silently okay well what exactly
fits so let's if this means anything to you this is something that was problematic for king of the ether back then yeah exactly so the answer is like the the OS macGruber right so like what happens is that you provide and the highest bidder is an address that always reverts right if it's a smart contract that will have a fallback function that it always reverts you if successfully do s the game nobody else will be able to claim any beats higher than that so okay cool yeah one way to to mitigate this would
be to use a we drop at pool patent instead of a push one so people can take the money out themselves instead of what you pushing money to someone else round two then you don't get to answer again someone else come on and bounded and unbounded Boyd yeah yeah exactly so the the problem is that like you can do s with the gasps Lehman right
like if the array gets too long then the while loop gets too expensive and the contract will be broken forever until the Contra the the block gas limit is is increased by the miners for example right but all you know it's not doable one thing that you could do to mitigate this would be to use batches in a possible function like this right like you have an argument that you can provide to just like do it incrementally so that you don't hit the guests on it wrong three do it do it
do it do it do it what is it okay but it rhymes with Nancy sir yeah exactly so the you're exactly right that there's a there's a call that forwards all the gas in the user balances are being updated afterwards right so yeah just like this is exactly an example that is very similar to the vowel yeah you're just like transferred
execute like the the control flow to and to another smart contracting you can never know what's happening so you should use just the the check effects interactions patterns where you actually do all your stuff and use your external calls at the end so round four oh by the way sometimes they're like there's more than one right answer so I'll take care I'll take anything you guys can ship yeah exactly so there's a lot of problems
here like this is this is bad yeah there's definitely an overflow there's an underflow haha yeah and the first one is another flow so the the first line please don't do that like like nobody does this this is only for illustration purposes like that line is terrible like that check is not is literally not doing anything that's a an unsigned integer I think bigger equal than zero like that's that's just bad that's just like no op and then the others below do have the problem that if you could under flow the vet that your balance right and just
like own a huge amount of tokens because of that under flow yeah so the the mitigation for this would be to use proper requires check for check for the for for balance overflows before you actually update them or use safe method right which is the which is the around 5:00 again several problems aiming for what this is a little bit more convoluted but feel free to shoot anything that you
think is okay yeah there's overflow problems with that multiplication not what we're looking for here that one is done more and more ideas this hint is terrible I'm sorry guys this is like this is this is not obvious so it has to do with the way transactions or they're right in the gas auction and the problem here is that let's pretend that this is a exactly
like a token auction or something right like if if the owner sees a transaction a by transaction coming in he could front-run the buyer and actually increase the price right before he buys it right and he gets more money than he sure they were like the buyer gets no dope no tokens whatsoever so yeah that's it what are some safer approaches on in that specific example a possible solution would be permission key functions right so type of user can
update the price or the buyer could specify the price or you could use a time lock right so it when the owner changes the price it actually only gets active activated in like two days or something right so that that way users are sure that like the prices do not get that the rug does not get pulled from under them right yeah next from oh really good really good nice nice one and there's also another problem but yeah that was the main one yeah but yeah
that's that's you you got it exactly right but but there's also another thing right which is like randomness is hard like this is not random at all like people could gain this randomness there's absolutely no no nothing random about this and yeah and like you said blocks only know about the 256 latest blocks on chain right so two problems this is a hot topic now I guess but a solution would be to use the random there's vdf sand vr af-s to to have like as proper sources of randomness be at in
general don't use like a low entropy to to create a randomness I guess you round seven I guess same same horrendous goat what yeah
it's clearly bad right because we've looked at the code previously what could go wrong with this if I see what exactly that's it you cannot get all of them right yeah that's exactly it so there's no way to guarantee that the balance of the contract is is static right so you can forcibly send ether to a smart contract and today I guess you get another beanie those of you who are
hacking you can check out our best practices those best practices it'll help you write better code safer code you can fight it on github if you can't find it you just come to me I'll point you to it please use reusable frameworks use open sapling that that makes your code safer too maybe that pub if you feel like hipster enough dual into your code please it it's it's effortless just lose your soul hint or something I don't know I don't care just do it lint it use me thanks you should be using the text me with X
dot IO if you need we can give you a trial account if you pass by the booth downstairs you can talk to us and give you a few analysis for free to run I live here I'm sure and that's it yes now I'm really over thank you