{"id":6618,"date":"2018-04-26T21:11:14","date_gmt":"2018-04-26T21:11:14","guid":{"rendered":"https:\/\/davidgerard.co.uk\/blockchain\/?p=6618"},"modified":"2020-03-06T23:00:00","modified_gmt":"2020-03-06T23:00:00","slug":"smart-contracts-stupid-humans-new-major-erc-20-token-bugs-batchoverflow-and-proxyoverflow","status":"publish","type":"post","link":"https:\/\/davidgerard.co.uk\/blockchain\/2018\/04\/26\/smart-contracts-stupid-humans-new-major-erc-20-token-bugs-batchoverflow-and-proxyoverflow\/","title":{"rendered":"Smart contracts, stupid humans: new major Ethereum ERC-20 token bugs BatchOverflow and ProxyOverflow"},"content":{"rendered":"<p>Smart contracts are <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/2017\/12\/03\/smart-contracts-article-in-heise-technology-review-oktober-2017-original-interview-in-english\/\">fundamentally bad software engineering,<\/a> part 666 of a never-ending series\u00a0\u2014 <a href=\"https:\/\/peckshield.com\/\">PeckShield<\/a> have been running an automatic scanner on the public Ethereum blockchain:<\/p>\n<blockquote><p>Built on our earlier efforts in analyzing EOS tokens, we have developed an automated system to scan and analyze Ethereum-based (ERC-20) token transfers. Specifically, our system will automatically send out alerts if any suspicious transactions (e.g., involving unreasonably large tokens) occur.<\/p><\/blockquote>\n<p>They&#8217;ve found a couple of beauties, which they&#8217;ve branded &#8220;BatchOverflow&#8221; and &#8220;ProxyOverflow.&#8221; These affect multiple ERC-20 tokens \u2014 which are the basis for almost all <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/icos-magic-beans-and-bubble-machines\/\">ICOs.<\/a><\/p>\n<p>The root cause is that smart contract coders just copy each other&#8217;s code a <i>lot<\/i>, because who needs formal methods when you can cut&#8217;n&#8217;paste&#8217;n&#8217;bodge.<\/p>\n<h3>BatchOverflow<\/h3>\n<p>On Sunday 22 April, PeckShield <a href=\"https:\/\/peckshield.com\/2018\/04\/22\/batchOverflow\/\">detected<\/a> two transfers of\u00a02<sup>255<\/sup> \u2014 or, in hexadecimal, 0x8000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000\u00a0\u2014 BeautyChain (BEC) tokens.<\/p>\n<p>If you add these two numbers, you get 2<sup>256<\/sup>, a 257-bit number \u2014 or, since we&#8217;re working in Solidity, which has 256-bit integers, you get an overflow, and the counter cycles back around to 0.<\/p>\n<p>This occurs in a function called <tt>batchTransfer()<\/tt> \u2014 a version of which is used in quite a lot of ERC-20 token contracts, because smart contract programmers copy code from each other lots and lots:<\/p>\n<p><a href=\"https:\/\/davidgerard.co.uk\/blockchain\/2018\/04\/26\/smart-contracts-stupid-humans-new-major-erc-20-token-bugs-batchoverflow-and-proxyoverflow\/batch_overflow_fig2\/\" rel=\"attachment wp-att-6640\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-6640\" src=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2018\/04\/batch_overflow_fig2.jpg\" alt=\"\" width=\"602\" height=\"166\" srcset=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2018\/04\/batch_overflow_fig2.jpg 1098w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2018\/04\/batch_overflow_fig2-300x83.jpg 300w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2018\/04\/batch_overflow_fig2-768x212.jpg 768w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2018\/04\/batch_overflow_fig2-1024x283.jpg 1024w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/a><\/p>\n<p>In line 257, amount is <tt>cnt<\/tt> times <tt>_value<\/tt> \u2014 and if <tt>_value<\/tt> is a huge number, this can easily overflow. This then passes the sanity checks in lines 258 and 259.<\/p>\n<p>Finally, in lines 262 to 265, the balances of the two receivers will have the very large <tt>_value<\/tt> added to them.<\/p>\n<p><tt>batchTransfer()<\/tt> is not part of the ERC-20 standard, but it&#8217;s widely used \u2014 so, despite the CVE (<a href=\"https:\/\/cve.mitre.org\/cgi-bin\/cvename.cgi?name=CVE-2018-10299\">CVE-2018-10299<\/a>) specifying only BeautyChain, PeckShield found over a dozen tokens vulnerable to this exploit.<\/p>\n<h3>ProxyOverflow<\/h3>\n<p>The CVE report for ProxyFlow (<a href=\"https:\/\/cve.mitre.org\/cgi-bin\/cvename.cgi?name=CVE-2018-10376\">CVE-2018-10376<\/a>) specifies only one ERC-20 token, SmartMesh (SMT) \u2014 but it affects multiple tokens.<\/p>\n<p>PeckShield <a href=\"https:\/\/peckshield.com\/2018\/04\/25\/proxyOverflow\/\">first detected the ProxyOverflow exploit<\/a> in MESH, when someone transferred 6.5\u00d710<sup>76<\/sup> tokens\u00a0\u2014 or 0x8fff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff in hexadecimal \u2014 with a transfer fee of 0x7000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001.<\/p>\n<p>If you add these values, you get 2<sup>256<\/sup> \u2014 which overflows and the counter cycles back to 0 again!<em><br \/>\n<\/em><\/p>\n<p>Here&#8217;s the code this was exploiting:<\/p>\n<p><a href=\"https:\/\/davidgerard.co.uk\/blockchain\/2018\/04\/26\/smart-contracts-stupid-humans-new-major-erc-20-token-bugs-batchoverflow-and-proxyoverflow\/todays-ethereum-integer-overflow-bug\/\" rel=\"attachment wp-att-6621\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-6621\" src=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2018\/04\/todays-ethereum-integer-overflow-bug.jpeg\" alt=\"\" width=\"600\" height=\"264\" srcset=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2018\/04\/todays-ethereum-integer-overflow-bug.jpeg 800w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2018\/04\/todays-ethereum-integer-overflow-bug-300x132.jpeg 300w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2018\/04\/todays-ethereum-integer-overflow-bug-768x338.jpeg 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><tt>_fee<\/tt> and <tt>_value<\/tt> are both set by the sender. If you put in values that add to more than 256 bits, the result wraps around. In this case, it adds to 0. This bypasses the sanity check in line 206. The huge number of tokens in <tt>_value<\/tt> are transferred to the attacker in line 214, and a huge <tt>_fee<\/tt> goes to msg.sender in line 217.<\/p>\n<p>This pattern is found in <em>lots<\/em> of ERC-20 tokens! PeckShield lists MESH, UGToken, SMT, SMART, MTC, FirstCoin, GG Token, CNY Token and CNYTokenPlus.<\/p>\n<h3>The consequences<\/h3>\n<p>OKex first suspended trading just on BeautyChain, but has now <a href=\"https:\/\/support.okex.com\/hc\/en-us\/articles\/360003019292\">suspended all ERC-20 tokens,<\/a> as has <a href=\"http:\/\/space.bitleek.com\/topic\/2132\/huobi-pro-suspends-deposits-and-withdrawals-of-all-coins\">Huobi Pro.<\/a> A pile of other exchanges are still allowing trading \u2014 and there&#8217;s still decentralised exchanges such as <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/2017\/09\/28\/etherdelta-hack-malicious-javascript-in-a-contract-name-stealing-the-users-key\/\">EtherDelta,<\/a> which can trade <em>any<\/em> ERC-20 token.<\/p>\n<p>SmartMesh will be <a href=\"https:\/\/www.coindesk.com\/crypto-exchanges-pause-services-over-contract-bugs\/\">destroying the counterfeit tokens<\/a> \u2014 remember that the underlying Ethereum blockchain may be (<a href=\"https:\/\/www.etherchain.org\/charts\/topMiners\">somewhat<\/a>) decentralised, but ERC-20 tokens themselves can be under absolute central control \u2014 and BeautyChain are looking into what they can do here. There&#8217;s no word as yet from the other tokens.<\/p>\n<p>Fixing these bugs will require redeploying the contracts \u2014 assuming they have been coded to be upgradable. The usual method is per the <a href=\"https:\/\/github.com\/ethereum\/wiki\/wiki\/White-Paper#decentralized-autonomous-organizations\">Ethereum white paper:<\/a><\/p>\n<blockquote><p>Although code is theoretically immutable, one can easily get around this and have de-facto mutability by having chunks of the code in separate contracts, and having the address of which contracts to call stored in the modifiable storage.<\/p><\/blockquote>\n<p>Even then, it can be a massive pain in the backside \u2014 and isn&#8217;t a panacea, as <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/2017\/11\/08\/the-ethereum-parity-wallet-disaster-play-by-play\/\">Parity discovered.<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/davidgerard.co.uk\/blockchain\/2017\/11\/08\/the-ethereum-parity-wallet-disaster-play-by-play\/artists-impression-of-ethereum-tue-7-nov-2017\/\" rel=\"attachment wp-att-2065\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2065\" src=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2017\/11\/artists-impression-of-ethereum-tue-7-nov-2017.jpeg\" alt=\"\" width=\"601\" height=\"338\" srcset=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2017\/11\/artists-impression-of-ethereum-tue-7-nov-2017.jpeg 800w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2017\/11\/artists-impression-of-ethereum-tue-7-nov-2017-300x169.jpeg 300w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2017\/11\/artists-impression-of-ethereum-tue-7-nov-2017-768x432.jpeg 768w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h3>Smart contracts, stupid humans<\/h3>\n<p>As I spent all of <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/table-of-contents\/\">chapter 10<\/a> of <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/book\/\">the book<\/a> attempting to bludgeon home \u2014 smart contracts are almost impossible for humans to program safely.<\/p>\n<p>The value proposition of \u201cimmutability\u201d is that nobody can mess with your contract once it\u2019s been deployed. But this is another way of saying &#8220;bugs can&#8217;t be fixed, ever.&#8221;<\/p>\n<p>The first really famous smart contract, <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/the-dao\/\">The DAO<\/a> in 2016, crashed and burned when it turned out to have a security hole that couldn\u2019t be fixed in time and it got hacked.<\/p>\n<p>(Smart contract security hits a worst case scenario \u2014 everyone can see your financial code and poke it for security holes, but you can&#8217;t quickly deploy fixes.)<\/p>\n<p>The eventual fix for The DAO hack demonstrates the other problem with smart contracts: the \u201cimmutable\u201d system containing the smart contract was suddenly considered changeable the moment the big boys risked losing money.<\/p>\n<p>Solidity, the standard Ethereum smart contract language, is a JavaScript derivative, so as to bring smart contracts to middling programmers. Ethereum successfully leveraged <a href=\"https:\/\/en.wikipedia.org\/wiki\/Worse_is_better\">Worse is Better<\/a> \u2014 an imperfect solution that&#8217;s easily reproducible will spread much more virally than the perfect, painstaking solution \u2014 to become the first smart contract platform to be widely used, with almost all its smart contracts written in Solidity.<\/p>\n<p>But humans are <i class=\"calibre3\">really <\/i>bad at coding without error. Programs that can&#8217;t be fixed once they&#8217;re deployed need <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/ethereum-smart-contracts-in-practice\/\">formal methods, functional programming and preferably a non-Turing-complete language.<\/a> You need to program like <a href=\"http:\/\/pixelscommander.com\/wp-content\/uploads\/2014\/12\/P10.pdf\">NASA programming spacecraft.<\/a><\/p>\n<p>Programs that <em>cannot<\/em> be allowed to have bugs &#8230; can&#8217;t be bodged by an average JavaScript programmer used to working in an iterative <a href=\"https:\/\/en.wikipedia.org\/wiki\/Agile_software_development\">Agile<\/a> manner. And particularly not a programmer who&#8217;s copying and pasting code like they&#8217;re still doing web site front ends and hitting StackOverflow for cribs.<\/p>\n<p>And you can even deploy fully-audited code that you\u2019ve mathematically proven is correct\u00a0\u2014 and then a bug in a lower layer means you have a security hole anyway. And this has <a href=\"https:\/\/blog.ethereum.org\/2016\/11\/01\/security-alert-solidity-variables-can-overwritten-storage\/\">already happened.<\/a><\/p>\n<p>Remember that not even Gavin Wood \u2014 the Ph.D computer scientist who wrote the <a href=\"https:\/\/github.com\/ethereum\/yellowpaper\">Ethereum protocol specification<\/a> \u2014 could write a smart contract safely enough not to lose hundreds of millions of dollars of his startup&#8217;s ICO funds in the <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/2017\/11\/08\/the-ethereum-parity-wallet-disaster-play-by-play\/\">Parity wallet disaster last November.<\/a> What makes you sufficiently sure that you can?<\/p>\n<p>&nbsp;<\/p>\n<blockquote class=\"twitter-tweet\" data-width=\"550\" data-dnt=\"true\">\n<p lang=\"en\" dir=\"ltr\">Some of us called out the Solidity overflow issues a long time ago.<\/p>\n<p>This is me back in July 17, 2017. <a href=\"https:\/\/t.co\/3799yJTR4b\">pic.twitter.com\/3799yJTR4b<\/a><\/p>\n<p>&mdash; Emin G\u00fcn Sirer (@el33th4xor) <a href=\"https:\/\/twitter.com\/el33th4xor\/status\/989281557662879745?ref_src=twsrc%5Etfw\">April 25, 2018<\/a><\/p><\/blockquote>\n<p><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<p>&nbsp;<\/p>\n<blockquote class=\"twitter-tweet\" data-width=\"550\" data-dnt=\"true\">\n<p lang=\"en\" dir=\"ltr\">while all the bitcoin people are laughing at the horrible erc20 token exploits, never forget the day that someone created 92 billion bitcoins.<a href=\"https:\/\/t.co\/1cE0W6cuoO\">https:\/\/t.co\/1cE0W6cuoO<\/a> <a href=\"https:\/\/t.co\/Ez2Gszbcz3\">https:\/\/t.co\/Ez2Gszbcz3<\/a><\/p>\n<p>&mdash; Buttcoin (@ButtCoin) <a href=\"https:\/\/twitter.com\/ButtCoin\/status\/989340067012358146?ref_src=twsrc%5Etfw\">April 26, 2018<\/a><\/p><\/blockquote>\n<p><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/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>Smart contracts are fundamentally bad software engineering, part 666 in a never-ending series.<\/p>\n","protected":false},"author":1,"featured_media":0,"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":[608,6,607,606,83],"class_list":["post-6618","post","type-post","status-publish","format-standard","hentry","category-uncategorised","tag-beautychain","tag-links","tag-mesh","tag-peckshield","tag-smart-contract"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/6618","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=6618"}],"version-history":[{"count":52,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/6618\/revisions"}],"predecessor-version":[{"id":15887,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/6618\/revisions\/15887"}],"wp:attachment":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/media?parent=6618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/categories?post=6618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/tags?post=6618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}