DeFi or decentralized finance is a growing sector in the blockchain and cryptocurrency space that defines an ecosystem of decentralized applications providing financial services with no governing authority.
Lendf.me is a DeFi app utilizing smart contracts in order to provide instant, decentralized lending. The platform suffered an attack causing the loss of $25m in cryptocurrency on the day of April 19, 2020.
Thus, joining the list of other DeFi protocols exploited recently:
Synthetix hack — 37M sETH stolen
bZx hack — $900k stolen
Lendf.me’s current vulnerability is a unique instance of the reentrancy bug. Reentrancy is a well-known issue in the field of computing, referring to the ability of a subroutine to be interrupted in the middle of its execution and (then safely) be called again.
Other reentrancy bugs have been exploited in the past, causing massive damage, including:
The DAO hack — $150m stolen
Spank Chain hack — $30k stolen
The Lendf.me attack took place on the Ethereum main-net smart contract named MoneyMarket, which implements the core logic of the Lendf.me app.
In order to best understand the underlying cause of the vulnerability, we should consider the contents of the various functions in the MoneyMarket contract.
First, we will consider the MoneyMarket.supply() function (line 1508).
The main purpose of the MoneyMarket.supply() function is to handle token deposits. The function takes two arguments, the asset (the asset that the user wishes to deposit), and the amount (the number of tokens he wishes to deposit).
The main logic flow of the MoneyMarket.supply() function is as follows:
First, we read the balance variable that represents the user’s deposited asset balance in MoneyMarket storage (line 1514), then, MoneyMarket.checkTransferIn() function is invoked (line 1526). This function (externally) calls the asset contract in order to figure if the user has the number of tokens he wishes to deposit and that he approved the MoneyMarket contract to withdraw this amount on his behalf.
Later MoneyMarket.doTransferIn() function is invoked (line 1583) which (externally) calls the asset contract’s transferFrom() function (line 405) that in turn transfers the amount from the user to the MoneyMarket contract. After the return from the external call, the MoneyMarket.supply() function is updating the user’s deposited balance (lines 1599–1600).
Let’s go over the MoneyMarket.withdraw() function’s logic briefly. In a simplified manner, this function gets the requested amount of tokens to withdraw, checks that the user holds at least this amount of tokens then transfers these tokens to the user by (externally) calling the token contract transfer() function.
The issue here is that MoneyMarket.supply() function is actually updating the user’s asset balance after the external call to asset.transferFrom() (lines 1599–1600), but based on a value that was read before the external call (line 1514), which means that the update potentially ignores any updates that were made within the external call. In many terms, we can consider this anomaly to be a “Lost Update”.
In order to understand this, we will have a look at the imBTC contract (or any other ERC-777 compliant contract)
The attacker took advantage of the fact that some of the assets implement ERC-777 standard, which means that the imBTC._callTokensToSend() function and thus, attackerContract.tokensToSend() function are invoked (lines 866, 1056 respectively) before the actual transfer of value between the two parties. This way, the attacker’s contract gets a chance to call MoneyMarket.withdraw() function before the invocation of MoneyMarket.supply() is finished!
The only prerequisite for attempting the exploit is for an attacker to deploy an attacker contract that holds some amount of any asset that is ERC-777 compliant, let’s assume for example that the attacker holds 10 tokens of imBTC.
When writing the smart contract code,
Valid Network’s blockchain security platform provides complete life cycle security for enterprise blockchains from initial development to active deployment and management. Based in Be'er Sheva, Israel, the company’s solutions enable enterprises to innovate with blockchain faster, providing complete visibility and control over their distributed applications and smart contract governance, compliance, and security posture through advanced platform capabilities.
Secure the block with Valid Network.
Learn more: https://valid.network
Valid Data’s real-time and predictive insights are used by Cryptocurrency traders and exchanges, as well as investors and hedge funds, to make better investment and trading decisions, to protect the value of their digital assets, and to capitalize on market opportunities that only Valid Network’s technology can uncover.
Get crypto analysis, insights and updates right to your inbox! Sign up here so you don't miss a single newsletter.