Enhancing Smart Contract Security Across the Supply Chain

DeFi Security Summit 2023 · 15 Jul 2023 · 21:12

Mikhail Lazarev draws on taking Gearbox Protocol from a hackathon project to 125 million dollars in value to show how a small team can keep a complex lending protocol safe. He covers the Gearbox risk framework that keeps the community informed, a robot that regularly liquidates every account on a forked node, front-end and dependency threats, and a change process built on Snapshot voting and a two day timelock. He also describes emergency controls and a tool that explains parameter changes in plain language.

Speakers

Topics

From DeFi Security Summit 2023, hosted by DeFi Security Summit.

Transcript

thank you thank you guys really nice to see all of you please grab your coffee and come in we will talk a little bit interesting think about how to manage protocol and uh just a few topics what I really want to tell with you today so my primary things is based only on real experience because I will launched gearbox protocol from the hackathon to 125 millions of tvl so it was really a lot of mistakes a lot of things we have learned from practical experience and I will talk you to you how to launch

manage and updates safely the five protocol with 125 Millions on board now it's quite less we have around 10K lines of code we use different tokens as collateral you can just imagine we have 70 at the moment and this quantity is growing we have at least seven major defy protocols connected so users use funds which was provided to the pools with external protocols and our protocol is Dow driven from the day Zero it was a quite nice idea for to give people leverage we use credit accounts credit

accounts are isolated smart contracts so when even before launch the protocol they ask people to pay some ease to the places credit accounts these smart contracts and give them tokens and we had a really extremely high partition High quantity of participants it was 5 000 participants at the launch and their active contributors to the dial and now we are delivering the third version of the protocol so we have different protocols we have a lot of system interconnections and everything is changed on the file and could be hacked so what I'm willing to tell you today

how to sleep well when everything could be is on the Fire and just to explain what gearbox is gearbox is a decentralized brokerage account and the idea here is that if you want to get leverage you open your credit account you provide your part of the funds and you can take a margin loan from the pool and then all these funds on account could be managed by you so you can build any strategy what you want you can swap tokens on uniswap you can provide them into Lido you can put them into convex position so it's a full compatibility and at the same time only things that is forbidden you can't

withdraw money into your pocket so all funds on this credit account which is isolate a smart contract I used as collateral and here just a short architecture part so as you can see there are two sides one side uh is called passive lenders these people who want to get high yields they just give Capital to the protocol and another part we call this needs as people who are Risk Takers they provide initial funds to the credit account and it's significant each credit account is isolated smart contract then this credit account immediately takes a

margin loan from the pool and then he just could send different transactions and these transactions the same like you send to normal protocols so you can swap on uni swap on Sushi swap you provide this money into cure for convex and so on and what we did at the end of this batch of transaction we make a collateral check to understand if this credit account healthy or not and of course we have some risk management but it's not what I'm willing to talk today because this uh gearbox like a lensing protocol has a lot of risk and today we built a pretty complex system so on this slide you can see what under the hood we have protocol

itself which could be a target for potential attacks of hackers and people who want to steal money we have our own smart contract system which is called router and we had to develop it we could not use one inch for example to find all ways between normal tokens and LP tokens for example we can find the best route from usdc into small complex convex position and build what we called multiple so it will be a list of transaction how to swap and deposit all tokens to get this position and it also could have bugs which as a result could

block some liquidations we have Liquidators and of course each time when these Liquidators are under attack it could be problem we have front-end taxes Farms so a lot of moving parts and to control this from the small team we have maybe some conversation and we try to build the process to do that so we call this system is a gearbox risk framework and the goal of this system is to provide a full transparency for our community so each dial contributor could understand what's going on we are fully transparent we talk freely about risk and potential problems we want to be safe and we want to deploy with

confidence without any side effects and sometimes it's quite difficult to test that Liquidators could get stuck somewhere so there are three steps how we do that the first one we write the list of all potential risk and start building monitoring system for example as you can see for the protocol risk we monitor all changes in the pools we check that there is no unhandled liquidation or if they happened we get immediately some notification to the telegram and I will later check the website we have a pretty interesting idea we called it optimistic liquidations when we work with our created account which holds a few

different assets on this example you can see on the right there is a Lido token and CVX for X3 CRV to swap it back to West we should do a lot of steps so here you can find we should swap a convex position into Frac series and we should unwrap it into three series then we should unwrap 3 Series to die and are applied to die and then convert them into less so it's a quite complex things and if one pool in the middle could have a lack of liquidity if you have back and rotor or Liquidator it could be a bad depth as a result so to really reduce the risk each three hours we have a special robot which run a fork node the

impersonate our configurator to change params and then we start to liquidate all accounts one by one and we have a logs that if this account should be liquidated our system could use it properly and there is no problem like lack of liquidity or any other stuff related to our software another good point which was already into consideration related to front end as you know it was a hug to the curve when someone was bribed and changed DNS for some provider and people use like a malicious front end another big problem here that

the industry is growing so fast so it's quite difficult to check all libraries you use for front-end application and if someone make a malicious part of the code and you build your new decentralized application the some disaster like unexpected approvals who happens so what we did to eliminate such a risk we run a special robot each few hours it run a test framework and try as a user to run application and do different stuff like depositing money or building position from our website and we used our proxy provider which has like an expected results how which

transaction should be built and if we can find that our software builds the usual user routine but transaction which was generated by our application is different or malicious we should immediately send an alert and of course we can talk to people that something is going wrong at least on Twitter Discord we can block our application and so on but on this stage we check that nobody could add any address for approval and so on so it owes another part how to protect the whole supply chain from the protocol to end user and of course maybe one of the nightmares for all of us is a

market risk because as you know many hacks depends when people could manipulate some assets many hacks were happened when it was like a reader entrancy for some curve pools and so on we have some interesting ideas implemented on this smart contracts but to monitor more we have a lot of different and robots which run this simulation for example we work with risk Dao as you can see it checks the liquidity of the pools it's a quite crucial if there is no liquidity and we have a big exposure of the protocol of some particular asset it means in the case of big price drop Liquidators would

start to swap it back and back we will use whole liquidity and acquire bad depth so to prevent such things the continuously monitor all this stuff got notification to make a better decision and of course governance risk it's another huge topic now we have a Dao and which works for some multis for snapshot and then multi-six signs this transaction and of course as you know there were some cases when using social engineering people try to push some incorrect or malicious transactions it could be some incorrect way which could be also to the hack

so we built a lot of bots and connected them to the one website which is available for everyone so if you can compare with centralized exchanges which just provide a paper we have such uh things on our accounts it works totally different we are fully transparent as a protocol for people who are willing to use and we try to write this way in more simple way and we send this data to telegram notifications and to website then the next part is the simulation how to do next step so when you get hacked

we I have a personal experience when bug bounce you found some critical bug and at this point you can't think you really try to solve something because you feel you are not okay and in this case we discuss all possible actions what we do so team could be under pressure response time is a king so be ready for that and here we're connecting the dots so we have a continuous moritong we have an analyzing all these possible cases together as a team and write all specification what to do and the last part is how it worked how to response for all this stuff and how to

continuously delivering not new version not to break another part and here is you can see how we deliver all changes to our protocol as you can see the first part is Snapshot voting so people try to list new tokens people want to add new protocols people want to change something and each changes could break in changes it could break something and really could have a bad consequences so we use time lock of course it's a good practice for two days so if you are not willing to work continue to continue to work with these parameters you can withdraw your money you can close your credit account

or if you are a dow contributor you can tell us hey guys there is a mistake it's a problem could be here and so on and another part what we implemented for some emergency cases for example lack of liquidity some protocol got hacked chaining price Oracle providing correct data what really was when lunar price dropped down and we really acted immediately otherwise the protocol could be hard because deviation between chaining price Oracle and real price was in times so we have controller we have a lower staff over time to act and we also

have lower we should not ask a community we also have a special roles who can manage and solve the problem and about the protocol updates when we really create some new transaction we make this system which everyone could go to gearbox rigs framework website and I'll show the demo and check what's going on from my point of view when we built a decentralized system it's a very very crucial you can find a bad example of this multi-chain when one person control everything he would cover by police and the protocol drops in gearbox through the centralization is a key

and another interesting part before showing sedema is cicd it's a practice which is commonly used on GitHub when you deliver your code in gearbox when the queue transaction for multi-seek is provided we immediately launch a special code on GitHub the impersonate multi-seek we apply all this transaction run all tests optimistic liquidation I already mentioned to understand could this change break something existing or not and of course we have a system which compare some code from GitHub whichever Audi that we have I think seven or eight

audits at the moment to quote which were deployed to the either scan and now let's check how it works so it's a public website and here you can see what I already mentioned uh there are a few things what we are checking uh so for example for optimistic liquidation you can see the list of all accounts you can see the double we have a two different Liquidators to be able to liquidate at any case if one gets stuck another should work because they have a totally different code base you have a list of accounts you can find all these ways you can click on any account

and get immediately the whole data history about things so if we can find some problems for liquidation we can go deeper and understand everything another interesting part what I already told you related to protocol updates and here you can see this time lock we have two types of transaction one is Q which sets the things and we can immediately understand what's going on and another is execute and uh I think maybe this is interesting yes so here you can see we have at least things which based on usually it's a

snapshot vaulting we have a simulation what I already mentioned on GitHub we run a special cicd script to check that everything works as expected and with these changes we do not break anything and you can see this comparison too so basically these two get this the code from azer scan then it searched for all our GitHub try to find the same commit and GitHub repo and then all commits are related to some security audits so you can simply find the discrete configurator was audited by Chain security because this commit hash was

related to this thing so you as external part if you want to understand about the safety of this action you can find a related related Audi sometimes the system that takes two of them or more and another part what I already want to show you and it's yeah and as as of course there is a lot of different data here uh yeah here as you can see it also shows us all differences previous values so if you are want to learn more you can click on

actions and find the full explanation and human language so basically it's what I really want to present thanks so much if you have any questions feel free to ask yeah guys we have one minute yeah thank you and we have one or two minutes for your questions so please raise your hand any questions yeah over there hi Michael very inspiration talk um I do have one question given that you said like every

protocol device protocol you will have a rigorous audits or smart contract coding so how would you plan to scale gearbox as the prime brokerage oh thanks to the best strategy to do that uh it's a really great question thanks for that so we try on the first part we really take Blue Chips protocol to be enabled like curve and so on which is really uh proven by Battlefield now we have the third version we have a quarter so we able to limit exposure of any protocol so we can start with a

small limit a few Millions check that everything is going wrong and if the protocol works well if it's battle tested we can continuously increase this limit for exposure of any particular token to create more trust and of course it's not by our team it's more related to our community down members and risk team okay um thank you for the great talk and I'll just checked the site that you share that risk that gearbox the foundation then I just found that there are there is a minus 50 deviation on the Oracle prices for yeah yeah yeah it's cool yeah

yeah it's what thanks for checking it's what we uh listed today it's not in use and the problem is in spot price calculation so we double checked but for some complex token as you can see this is OHM fluxbp so itself to compute it correctly you should do a lot of steps so on the real Market I think it's more related to incorrect Sport price provided by one inch what we use at the moment not to the real time but these tokens are not in use and the biggest way why we build it and thanks for question that Community people like you could check what's going on ask questions and we do all steps

continuously to make everything safe thanks so much