{"id":16170,"date":"2020-04-26T15:36:04","date_gmt":"2020-04-26T15:36:04","guid":{"rendered":"https:\/\/davidgerard.co.uk\/blockchain\/?p=16170"},"modified":"2020-10-03T20:59:56","modified_gmt":"2020-10-03T20:59:56","slug":"the-dforce-and-hegic-defi-exploits-and-why-smart-contracts-are-bad","status":"publish","type":"post","link":"https:\/\/davidgerard.co.uk\/blockchain\/2020\/04\/26\/the-dforce-and-hegic-defi-exploits-and-why-smart-contracts-are-bad\/","title":{"rendered":"The dForce and Hegic DeFi exploits, and why Smart Contracts are bad"},"content":{"rendered":"<p>A &#8220;smart contract&#8221; is a computer program running right there on the blockchain itself, so you can do complicated things with your cryptocurrencies and tokens, automatically. Ethereum was the first popular smart contract platform.<\/p>\n<p>Smart contract programs are extremely hard to alter \u2014 in Nick Szabo&#8217;s original conception of the idea, they were <em>impossible<\/em> to alter.<\/p>\n<p>The idea is that you can have faith that the program is immune to interference from mere humans \u2014 that you&#8217;ve automated the human element out.<\/p>\n<p>The problem there is that another term for &#8220;immutable program&#8221; is &#8220;sitting duck for attackers.&#8221;<\/p>\n<p>The root cause of smart contract issues in practice is the clash of two factors:<\/p>\n<ol>\n<li>Smart contracts are hard or impossible to alter, by design. They require the most painstaking code review and analysis \u2014 so that you don&#8217;t lose money to an exploit.<\/li>\n<li>You make more money by being quick to market.<\/li>\n<\/ol>\n<p><a href=\"https:\/\/davidgerard.co.uk\/blockchain\/2020\/01\/05\/decentralised-finance-defi-in-practice-when-you-have-a-spare-eth-to-play-with\/\">Decentralised Finance,<\/a> or DeFi, uses chains of smart contract programs to automate complex financial transactions \u2014 so you can chain the attacks too. Unsurprisingly, it&#8217;s a continuing dumpster fire, reliably delivering comedy gold.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/davidgerard.co.uk\/blockchain\/2020\/04\/26\/the-dforce-and-hegic-defi-exploits-and-why-smart-contracts-are-bad\/dforce-aum\/\" rel=\"attachment wp-att-16171\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-16171\" src=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2020\/04\/dforce-aum.png\" alt=\"\" width=\"601\" height=\"317\" srcset=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2020\/04\/dforce-aum.png 1202w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2020\/04\/dforce-aum-300x158.png 300w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2020\/04\/dforce-aum-1024x540.png 1024w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2020\/04\/dforce-aum-768x405.png 768w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h3>dForce pulls a DAO<\/h3>\n<p>DeFi provider dForce suffered an unfortunate exploit of its lendf.me protocol on 18 April \u2014 in which an attacker took off with $25 million of assets under management, leaving just $18,900. [<a href=\"https:\/\/www.coindesk.com\/attacker-drains-decentralized-protocol-dforce-of-25m-in-weekend-attack\"><i>CoinDesk<\/i><\/a>]<\/p>\n<p>The assets were mostly ether and bitcoins \u2014 not actual ether and bitcoins, but tokens representing them. The exploit involved using imBTC tokens as collateral.<\/p>\n<p>imBTC is an ERC-777 token. ERC-777 is an updated version of the ERC-20 standard, which most ICO token contracts were built on \u2014 but the imBTC smart contract had a re-entrancy bug, where you could withdraw repeatedly before the balance updated. (This is the sort of bug <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/the-dao\/\">The DAO fell to in 2016.<\/a>)<\/p>\n<p>After sufficient iterations, the attacker used their imBTC balance as collateral to borrow multiple other assets. The attacker then surrendered their collateral, and kept the borrowed assets. [<a href=\"https:\/\/twitter.com\/FrankResearcher\/status\/1251950684334952453\"><i>Twitter<\/i><\/a>]<\/p>\n<p>The imBTC pool on Uniswap had been attacked and drained the same way, the previous day. [<a href=\"https:\/\/twitter.com\/tokenlon\/status\/1251423721476116480\"><i>Twitter<\/i><\/a>]<\/p>\n<p>Peckshield wrote up both attacks. &#8220;ERC777 itself is a community-established token standard with its advanced features for various scenarios. However, these advanced features might not be compatible with certain DeFi scenarios. Worse, such incompatibility could further lead to undesirable consequences (e.g., reentrancy).&#8221; [<a href=\"https:\/\/medium.com\/@peckshield\/uniswap-lendf-me-hacks-root-cause-and-loss-analysis-50f3263dcc09\"><i>Medium<\/i><\/a>]<\/p>\n<p>The attacker returned some assets to dForce \u2014 tokens they couldn&#8217;t possibly cash in, because the tokens were centrally issued, and were already blacklisted by the issuer. These included Huobi BTC and Huobi USD, two ERC-20 tokens representing bitcoins or US dollars, that can only be redeemed at the Huobi crypto exchange. [<a href=\"https:\/\/twitter.com\/lawmaster\/status\/1251953291891802112\"><i>Twitter<\/i><\/a>]<\/p>\n<p>DeFi startup Compound alleged a few months ago that dForce had copied its code \u2014 the word &#8220;compound&#8221; appears throughout dForce&#8217;s code. Compound&#8217;s code is not under an open source copyright license \u2014 it&#8217;s all-rights-reserved, with the source being publicly available. [<a href=\"https:\/\/www.theblockcrypto.com\/daily\/54389\/vc-backed-compound-is-alleging-that-a-fast-growing-defi-startup-stole-its-code-the-dispute-raises-questions-about-what-open-source-actually-means\"><i>The Block<\/i><\/a><i>, paywalled; <\/i><a href=\"https:\/\/github.com\/dforce-network\"><i>GitHub<\/i><\/a><i>; <\/i><a href=\"https:\/\/github.com\/compound-finance\/compound-protocol\/blob\/master\/LICENSE\"><i>GitHub<\/i><\/a>]<\/p>\n<h3>Hegic Options \u2014 security theatre as a service<\/h3>\n<p>Hegic is an on-chain options trading protocol on Ethereum \u2014 intended for use in decentralised finance (DeFi). Hegic proudly proclaims it was audited by Trail of Bits, before its launch on 24 April. [<a href=\"https:\/\/hegic.gitbook.io\/start\/\"><i>Hegic<\/i><\/a>]<\/p>\n<p>The next day, Hegic alerted users to a bug in the code: &#8220;!! ALERT A typo has been found in the code. Because of that, liquidity in expired options contracts can\u2019t be unlocked for new options. \u203c\ufe0f Please EXERCISE ALL OF YOUR ACTIVE OPTIONS CONTRACTS NOW.&#8221; [<a href=\"https:\/\/twitter.com\/HegicOptions\/status\/1253937104666742787\"><i>Twitter<\/i><\/a>]<\/p>\n<p>The bug was a typographical error in a function name \u2014\u00a0 Hegic used <tt>options.length<\/tt> instead of <tt>optionIDs.length<\/tt>, while they had <tt>options<\/tt> defined in outer scope, so the Solidity compiler tried to use that. [<i><a href=\"https:\/\/twitter.com\/gakonst\/status\/1253987789219205121\">Twitter<\/a>; <a href=\"https:\/\/github.com\/hegic\/hegic-contracts-v1\/blob\/632395e2a800cc8b38e12b06f64b7f781ddbc21b\/HegicOptions.sol#L74\">GitHub<\/a><\/i>]<\/p>\n<p>Hegic originally reported the bug incorrectly: &#8220;It\u2019s an incorrect function name (optionIDs instead of optionsIDs).&#8221; But they did describe the consensequences correctly: &#8220;This function unlocks liquidity in expired contracts. If it doesn\u2019t work, funds are just forever locked.&#8221; In any case, Hegic will be refunding any funds that were locked by the bug. [<a href=\"https:\/\/twitter.com\/HegicOptions\/status\/1253954145113038849\"><i>Twitter<\/i><\/a>]<\/p>\n<p>Trail of Bits was not so happy that Hegic claimed they had &#8220;audited&#8221; the smart contract. CEO Dan Guido asked them in a tweet: &#8220;Please stop holding up a 3-day code review as an &#8216;audit&#8217; that proves the code is safe.&#8221;<\/p>\n<p>Guido later deleted that tweet, but he did post a thread explaining precisely what Trail of Bits had \u2014 and hadn&#8217;t \u2014 done: [<a href=\"https:\/\/twitter.com\/dguido\/status\/1254260710470815744\"><i>Twitter<\/i><\/a>]<\/p>\n<blockquote><p>In 3 days earlier this month, we identified 10 critical flaws in @HegicOptions that could harm users. We noted a lack of tests, a lack of documentation, and that the time afforded to review their code was insufficient.<\/p>\n<p>Bottom line: we told them to hold off deploying. This was the right advice, and we generally expect people listen to us when they&#8217;re paying for our help.<\/p>\n<p>Instead, Hegic patched the few bugs we found, made no further changes, misrepresented our 3-day code review as an &#8220;audit&#8221;, then immediately deployed.<\/p><\/blockquote>\n<p>Trail of Bits has fired Hegic as a client. [<a href=\"https:\/\/twitter.com\/dguido\/status\/1254260725431894020\"><i>Twitter<\/i><\/a>]<\/p>\n<p>The audit summary itself is a collection of polite ways to phrase &#8220;this program is laughably ill-constructed and has so many problems we didn&#8217;t have time to find them all.&#8221; MyCrypto summarised Trail of Bits&#8217; audit summary in a Twitter thread. [<a href=\"https:\/\/github.com\/trailofbits\/publications\/blob\/master\/reviews\/hegic-summary.pdf\"><i>Trail of Bits<\/i><\/a><i>, PDF; <\/i><a href=\"https:\/\/twitter.com\/MyCrypto\/status\/1254058121342803968\"><i>Twitter<\/i><\/a>]<\/p>\n<h3>This will keep happening<\/h3>\n<p>I published &#8220;Smart contracts, stupid humans&#8221;, <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/table-of-contents\/\">chapter 10<\/a> of <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/book\/\"><i>Attack of the 50 Foot Blockchain,<\/i><\/a> in 2017 \u2014 which I foolishly thought would knock this deeply and fundamentally dumb and bad idea on the head. If you don&#8217;t have the book, <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/2017\/12\/03\/smart-contracts-article-in-heise-technology-review-oktober-2017-original-interview-in-english\/\">I did an interview<\/a> a few months later, about why smart contracts are fundamentally dumb and bad, that covers some of the same ground.<\/p>\n<p>The problem with DeFi is not the technology, though that&#8217;s bad too \u2014 it&#8217;s that people are greedy and foolish.<\/p>\n<hr \/>\n<p><b>Update:<\/b> corrected the Hegic bug description \u2014 Hegic misdescribed their own bug, just as if they didn&#8217;t know the code very well. (Cheers to <a href=\"https:\/\/lobste.rs\/s\/tw0trx\/dforce_hegic_defi_exploits_why_smart\">myfreeweb on lobste.rs.<\/a>)<\/p>\n<br><br><div align=\"center\"><p><a href=\"https:\/\/www.patreon.com\/bePatron?u=8420236\"><img src=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2021\/10\/become_a_patron_button.svg\" alt=\"Become a Patron!\" title=\"Become a Patron!\" width=217 height=51><\/a><br><p style=\"align:center;\" class=\"patreon-badge\"><i>Your subscriptions keep this site going. <a href=\"https:\/\/www.patreon.com\/bePatron?u=8420236\">Sign up today!<\/a><\/i><\/p><\/div>","protected":false},"excerpt":{"rendered":"<p>The problem with DeFi is not the technology, though that&#8217;s bad too \u2014 it&#8217;s that people are greedy and foolish.<\/p>\n","protected":false},"author":1,"featured_media":17567,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[1668,1752,1552,1751,1750,83,1423,1669],"class_list":["post-16170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorised","tag-compound","tag-dan-guido","tag-defi","tag-dforce","tag-hegic","tag-smart-contract","tag-trail-of-bits","tag-uniswap"],"jetpack_featured_media_url":"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2020\/04\/dforce-aum-header.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/16170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/comments?post=16170"}],"version-history":[{"count":22,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/16170\/revisions"}],"predecessor-version":[{"id":17017,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/16170\/revisions\/17017"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/media\/17567"}],"wp:attachment":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/media?parent=16170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/categories?post=16170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/tags?post=16170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}