{"id":25478,"date":"2019-12-25T12:47:47","date_gmt":"2019-12-25T10:47:47","guid":{"rendered":"https:\/\/www.kaspersky.co.za\/blog\/gemini-front-running\/25478\/"},"modified":"2019-12-25T12:47:47","modified_gmt":"2019-12-25T10:47:47","slug":"gemini-front-running","status":"publish","type":"post","link":"https:\/\/www.kaspersky.co.za\/blog\/gemini-front-running\/25478\/","title":{"rendered":"GUSD smart contract allows for theft of antispam payments"},"content":{"rendered":"<p>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Winklevoss_twins\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">Winklevoss twins<\/a> are best known as the alleged founding fathers of Facebook \u2014 and even received <a href=\"https:\/\/www.law.com\/almID\/1202428141988\/\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">$65 million<\/a> in compensation from Mark Zuckerberg in 2008. In 2013 they invested heavily in Bitcoin, buying about 1% of all existing coins at $120 apiece.<\/p>\n<p>Soon after, the brothers opened the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Gemini_(company)\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">Gemini<\/a> cryptocurrency exchange, and in 2018 they <a href=\"https:\/\/medium.com\/gemini\/gemini-launches-the-gemini-dollar-62787f963fb4\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">launched<\/a> the stablecoin <a href=\"https:\/\/gemini.com\/dollar\/\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">Gemini dollar<\/a> (GUSD). A stablecoin is a fixed-rate cryptocurrency \u2014 1 GUSD token always costs 1 US dollar. Stablecoins are handy for \u201cdigitizing\u201d real dollars. They make moving blockchain dollars between exchanges quick and easy. The guarantor of the reverse conversion to dollars is the company that issued and sold them to you.<\/p>\n<p>Under the <a href=\"https:\/\/www.kaspersky.com\/enterprise-security\/ico-sto-security\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">Kaspersky Smart Contract Source Code Review<\/a> service, we analyzed a <a href=\"https:\/\/etherscan.io\/token\/0x056Fd409E1d7A124BD7017459dFEa2F387b6d5Cd\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">smart contract<\/a> that provides GUSD functionality, and we detected a flaw.<\/p>\n<div style=\"background-color: #e5f0ec;padding: 10px 25px;margin-bottom: 10px\">\n<h2>Disclaimer<\/h2>\n<p>Note that the given smart contract had already been <a href=\"https:\/\/gemini.com\/wp-content\/themes\/gemini\/assets\/img\/dollar\/gemini-dollar-trailofbits-audit.pdf\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">reviewed<\/a>, although we do not know if any code flaws were described in the report.<\/p>\n<p>In line with our Responsible Disclosure Policy, we contacted Gemini\u2019s security team to report the problem. They informed us the issue was considered during the design phase but presented no risk to GUSD.\n<\/p><\/div>\n<p>For a simple explanation of how smart contracts work, see our <a href=\"https:\/\/www.kaspersky.com\/blog\/ethereum-ico\/19846\/\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">post on smart contracts, Ethereum, and ICOs<\/a>.<\/p>\n<h2>Gemini dollar smart contracts<\/h2>\n<p>Generally speaking, when someone wants to create new tokens based on the Ethereum blockchain, they write a smart contract (a miniprogram) that specifies the following:<\/p>\n<ol>\n<li>Data (\u201cthis many tokens are at such-and-such address),<\/li>\n<li>Methods (\u201cplease transfer my tokens to such-and-such address,\u201d plus a few others).<\/li>\n<\/ol>\n<p>The creators of the Gemini dollar system <a href=\"https:\/\/gemini.com\/wp-content\/themes\/gemini\/assets\/img\/dollar\/gemini-dollar-whitepaper.pdf\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">implemented<\/a> the following enhancements as well:<\/p>\n<ol>\n<li>They separated the contract into three components: Proxy (the permanent interface with which token holders can interact and perform operations), store (the mapping of token holders to their balances), and Impl (the underlying logic);<\/li>\n<li>The component describing the logic can be updated and supplemented with new features such as the ability to freeze funds. Meanwhile, the data and interface remained unchanged; the update is transparent to everyone;<\/li>\n<li>For updating and control, a separate, \u201cCustodian\u201d smart contract is used that is managed by several people (custodians) for additional protection. If one custodian proposes an action, the others must confirm before the action can take place.<\/li>\n<\/ol>\n<p>The enhancements are sound and increase overall security and flexibility.<\/p>\n<h2>Antispam payments<\/h2>\n<p>If someone other than the primary custodian enters a proposal in a custodian contract, they must pay a 1 ETH stake (about $200 at the current exchange rate). As noted in the <a href=\"https:\/\/github.com\/gemini\/dollar\/blob\/master\/Custodian.sol#L151\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">comments<\/a> to the contract itself, this antispam measure aims to dissuade participants from creating too many requests.<\/p>\n<p>The antispam payments ultimately <a href=\"https:\/\/github.com\/gemini\/dollar\/blob\/master\/Custodian.sol#L264\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">go<\/a> to one person: the one who announces the approval of a particular proposal\/request. This implementation may not look terrifically fair, but the comments clearly indicate that its creators conceived it that way.<\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } else {<\/strong><br>\n<strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (address(this).balance &gt; 0) {<\/strong><br>\n<strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ reward sender with anti-spam payments<\/strong><br>\n<strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ ignore send success (assign to \u02b9success\u02b9 but this will be overwritten)<\/strong><br>\n<strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 success = msg.sender.send(address(this).balance);<\/strong><\/p>\n<p>For our part, we recommend using the <a href=\"https:\/\/solidity.readthedocs.io\/en\/v0.5.10\/common-patterns.html\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">Solidity Withdrawal Pattern<\/a> approach.<\/p>\n<h2>Front-running attackers can steal all antispam payments<\/h2>\n<p>The person who determines the approval of the request thus also receives all ETH antispam payments. To do so, he or she calls the smart contract function <strong>completeUnlock<\/strong> and passes the signatures of two custodians in the parameters.<\/p>\n<p>The problem is that Ethereum, like any other blockchain, executes requests on a delay. A client transaction (transferring money or calling a function) waits in line for some time (usually 15 seconds or longer). During this time, absolutely anyone can view the planned transfers of other Ethereum users, including amounts, recipients, and parameters. And the peeper can use this information to create their own transaction and push it to the front by paying a higher commission to the miner.<\/p>\n<p>Any advantage gained through peeping is considered front-running, a form of attack (<a href=\"https:\/\/consensys.github.io\/smart-contract-best-practices\/known_attacks\/#front-running-aka-transaction-ordering-dependence\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">Known Attacks: Front-Running<\/a>).<\/p>\n<div style=\"background-color: #e5f0ec;padding: 10px 25px;margin-bottom: 10px\">\nFrom <a href=\"https:\/\/www.investopedia.com\/terms\/f\/frontrunning.asp\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">investopedia.com<\/a>:\n<p>Front-running is when a broker or other entity enters into a trade because they have foreknowledge of a big nonpublicized transaction that will influence the price of the asset, resulting in a likely financial gain for the broker. It also occurs when a broker or analyst buys or sells shares for their account ahead of their firm\u2019s buy or sell recommendation to clients.\n<\/p><\/div>\n<p>In our case, a complete outsider can set up a robot to monitor the <a href=\"https:\/\/etherscan.io\/address\/0x9a7b5f6e453d0cda978163cb4a9a88367250a52d\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">custodian contract<\/a>. If it sees that someone called the <strong>completeUnlock<\/strong> function (that is, a custodian is interacting with Gemini dollar), it immediately copies all of the parameters and calls the function to extract the Ether that has accumulated there.<\/p>\n<p>To counter such an attack, we again recommend using the popular <a href=\"https:\/\/solidity.readthedocs.io\/en\/v0.5.10\/common-patterns.html\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">Solidity Withdrawal Pattern<\/a> approach.<\/p>\n<p>On top of that, we recommend blocking <a href=\"https:\/\/github.com\/gemini\/dollar\/blob\/master\/Custodian.sol#L233\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">unknowns<\/a> from calling a function intended for custodians.<\/p>\n<h2>Practical implementation of an attack<\/h2>\n<p>Although dangerous in theory, the detected vulnerability is fairly benign in practice. Here\u2019s why:<\/p>\n<ol>\n<li>Antispam payments are of little concern to custodians of such a major venture as the Gemini dollar. GUSD capitalization (the total volume of issued tokens) at one point topped $100 million. Even now it exceeds $5 million.<\/li>\n<li>Antispam payments have not yet appeared in <a href=\"https:\/\/etherscan.io\/address\/0x9a7b5f6e453d0cda978163cb4a9a88367250a52d\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">this contract<\/a> and may never do so, because the primary custodian is in no way obliged to deposit them (all others are).<\/li>\n<li>Knowing about the vulnerability, users can simply avoid the vulnerable function, or update the contract.<\/li>\n<li>During the review, we found no vulnerabilities that threaten GUSD tokens.<\/li>\n<\/ol>\n<p>Gemini comments: \u201cWe chose this design because Gemini does not intend to stake ether under normal conditions, and, as a result, we made a risk-based decision not to materially expand the complexity of our codebase solely for the immaterial benefit of a more robust recovery mechanism for a theoretical, and nominal, anti-spam stake. Prioritizing secure, simple code remains the best solution for the Gemini dollar and its users. In the future we may revisit this decision if the risk changes and a more costly and complex contract becomes appropriate.\u201d<\/p>\n<p>We decided to publish this post in coordination with Gemini, given that antispam stakes are at risk only through a combination of specific and unlikely circumstances, and GUSD is not at risk.<\/p>\n<p>Again, we remind everyone of the need for a holistic security approach to ICOs and other activities related to <a href=\"https:\/\/www.kaspersky.com\/enterprise-security\/ico-sto-security\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">cryptocurrencies and blockchains<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Exploring front-running to capture the Gemini dollar\u2019s antispam stake.<\/p>\n","protected":false},"author":669,"featured_media":25479,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1999,3020,3021],"tags":[2646,3251,2697,3252,2800],"class_list":{"0":"post-25478","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-business","8":"category-enterprise","9":"category-smb","10":"tag-cryptocurrencies","11":"tag-ether","12":"tag-ethereum","13":"tag-stablecoins","14":"tag-tokens"},"hreflang":[{"hreflang":"en-za","url":"https:\/\/www.kaspersky.co.za\/blog\/gemini-front-running\/25478\/"},{"hreflang":"en-in","url":"https:\/\/www.kaspersky.co.in\/blog\/gemini-front-running\/18359\/"},{"hreflang":"en-ae","url":"https:\/\/me-en.kaspersky.com\/blog\/gemini-front-running\/15236\/"},{"hreflang":"en-us","url":"https:\/\/usa.kaspersky.com\/blog\/gemini-front-running\/20065\/"},{"hreflang":"en-gb","url":"https:\/\/www.kaspersky.co.uk\/blog\/gemini-front-running\/18423\/"},{"hreflang":"es-mx","url":"https:\/\/latam.kaspersky.com\/blog\/gemini-front-running\/16832\/"},{"hreflang":"es","url":"https:\/\/www.kaspersky.es\/blog\/gemini-front-running\/20830\/"},{"hreflang":"it","url":"https:\/\/www.kaspersky.it\/blog\/gemini-front-running\/19597\/"},{"hreflang":"ru","url":"https:\/\/www.kaspersky.ru\/blog\/gemini-front-running\/26060\/"},{"hreflang":"x-default","url":"https:\/\/www.kaspersky.com\/blog\/gemini-front-running\/31924\/"},{"hreflang":"pt-br","url":"https:\/\/www.kaspersky.com.br\/blog\/gemini-front-running\/13850\/"},{"hreflang":"de","url":"https:\/\/www.kaspersky.de\/blog\/gemini-front-running\/21847\/"},{"hreflang":"nl","url":"https:\/\/www.kaspersky.nl\/blog\/gemini-front-running\/24769\/"},{"hreflang":"en-au","url":"https:\/\/www.kaspersky.com.au\/blog\/gemini-front-running\/25647\/"}],"acf":[],"banners":"","maintag":{"url":"https:\/\/www.kaspersky.co.za\/blog\/tag\/cryptocurrencies\/","name":"cryptocurrencies"},"_links":{"self":[{"href":"https:\/\/www.kaspersky.co.za\/blog\/wp-json\/wp\/v2\/posts\/25478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kaspersky.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kaspersky.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kaspersky.co.za\/blog\/wp-json\/wp\/v2\/users\/669"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kaspersky.co.za\/blog\/wp-json\/wp\/v2\/comments?post=25478"}],"version-history":[{"count":0,"href":"https:\/\/www.kaspersky.co.za\/blog\/wp-json\/wp\/v2\/posts\/25478\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kaspersky.co.za\/blog\/wp-json\/wp\/v2\/media\/25479"}],"wp:attachment":[{"href":"https:\/\/www.kaspersky.co.za\/blog\/wp-json\/wp\/v2\/media?parent=25478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaspersky.co.za\/blog\/wp-json\/wp\/v2\/categories?post=25478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaspersky.co.za\/blog\/wp-json\/wp\/v2\/tags?post=25478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}