
Author: Jagjit Singh, CoinTelegraph; Compiled by: Wuzhu, Bitchain Vision
1. Potential vulnerabilities in smart contracts
Although smart contracts are groundbreaking, they are not immune to flaws that malicious parties may exploit.
Inadequate input verification is a common weakness, allowing an attacker to influence the execution of a contract by providing unexpected input.In addition, improper application of business logic can create unexpected behavior or logical gaps in the contract, resulting in vulnerabilities.Additionally, unsafe external calls (such as calls involving interfaces with external data sources or other contracts) can create vulnerabilities if handled improperly.
A reentry attack is a weakness that occurs when a contract calls another contract from outside before completing its own state change.This allows the called contract to re-enter the call contract and may perform some of its operations again.This can lead to unexpected behavior and allow an attacker to change the status of the contract, which can drain funds or have other negative effects.
Given the possibility of such attacks, developers should also exercise caution when using external contracts or data sources to ensure that external calls are properly processed to avoid unexpected behavior and vulnerabilities.They can help protect smart contracts from ever-changing threats by keeping a close eye on security programs such as smart contract testing.
2. What is a reentry attack in a smart contract?
In a smart contract, a reentry attack occurs when a contract calls another contract or function from outside before completing its own state change.
This allows the called contract to re-enter the calling contract and may perform part of its operations again, which may lead to unforeseen and frequent malicious behavior.For example, Contract A calls Contract B to send funds and then modify its own status.
The code of contract B may contain a callback function that allows it to re-enter Contract A and may re-execute the transfer function before contract A completes its state change.This will enable the attacker to obtain funds from the contract multiple times before completing the initial transaction.
The 2016 Decentralized Autonomous Organization (DAO) hacking on the Ethereum blockchain is another well-known example.The attacker took advantage of the reentry flaw in the smart contract code to recursively remove funds from the DAO, which eventually led to the stolen millions of dollars in Ethereum (ETH).
In addition, multiple decentralized finance (DeFi) protocols including Uniswap, Lendf.Me, BurgerSwap, SURGEBNB, Cream Finance and Siren Protocol suffered significant financial losses due to reentrability vulnerabilities.These violations caused losses of $3.5 million to $25 million, highlighting the ongoing threat posed by reentrant vulnerabilities in the DeFi sector.
3. How to reentry attacks work
Reentry attacks utilize sequential execution of smart contract functions and external calls to form a loop where an attacker can execute specific functions multiple times before completion, which can lead to malicious behavior and unauthorized withdrawal of funds.
The attacker’s contract effectively “scammers” the victim’s contract callback to the attacker’s contract before the victim completes the status modification.This action may lead to repeated withdrawals or other negligence.
The image above demonstrates a reentry attack on smart contracts.The attacker’s contract calls the victim’s “withdraw()” function, which sends Ethereum before updating the balance.The attacker’s fallback function is then fired, and again recursively calls withdraw() to exhaust funds from the victim contract.This attack exploits the situation where the victim failed to update the balance before sending the funds.
Let’s use a simplified example to break down how reentry attacks work:
Smart contract with “withdrawal” function
Suppose there is a digital wallet smart contract that allows users to withdraw funds.In addition to tracking user balances, the contract also has a withdrawal function to facilitate withdrawal of funds.The withdrawal feature usually allows users to withdraw their tokens or Ethereum from smart contracts to their personal wallet.
User interaction and function execution
The user requests withdrawal from the wallet on his own.They use the withdrawal function to enter the required withdrawal amount.
The withdrawal function will verify when called whether the user has enough funds to withdraw.If the requirements are met, it transfers the required funds to the address selected by the user.
External call
This is where weaknesses appear.The contract makes an external call to another contract or account before the withdrawal is reflected in the user’s balance.
Recursive call
If the code of the external contract contains a function that can call the original contract again (such as another withdrawal function), a recursive loop is created.This allows the withdraw method to be called again before it is finished.
Re-entry and utilization
The attacker then uses a malicious contract to exploit this loop.The attacker contract quickly calls the wallet’s withdrawal function again before the balance is updated during the wallet contract calls the external contract.
Fallback function
In some cases, the fallback feature of a smart contract (a unique feature that is initiated when a contract receives a call without any data or Ethereum) may be used by the attacker.Reentry attacks can be performed by repeatedly calling the fallback function while the funds are still being processed.
Manipulation and recurring withdrawals
The attacker’s contract can reuse the withdrawal feature in the same transaction, because the wallet contract delays the update of the balance until an external call is received.This therefore makes it easier for funds to be withdrawn without authorization, allowing attackers to steal funds beyond their legal rights.It then caused huge economic losses to the users of the wallet contract.
4. The consequences of reentering attacks
Reentry attacks can have a serious impact on smart contract users because they have the potential to cause significant financial losses.
One of the most direct consequences of a reentry attack is the unauthorized withdrawal or manipulation of cash saved in a susceptible smart contract.The attacker exploited the vulnerability to repeatedly withdraw funds from the contract, exhausting the contract balance and potentially causing significant financial losses to users who invest or store assets in the affected contract.
In addition, reentry attacks may weaken users’ confidence in the security and integrity of smart contracts and blockchain technologies.Reentry vulnerabilities can have catastrophic effects, as evidenced by high-profile events such as the DAO hacking incident on the Ethereum blockchain in 2016, which caused huge economic losses and damaged the reputation of the community.
In addition to short-term financial consequences, reentry attacks can have long-term effects such as regulatory and legal concerns, declining investor trust and damage to the reputation of blockchain platforms and projects.The perception of vulnerability may lead users to be cautious when interacting with smart contracts or investing in decentralized applications (DApps), thus hindering the adoption and expansion of blockchain technology.
5. How to mitigate reentry attacks
Implementing best practices in smart contract creation and auditing is necessary to mitigate the threat of reentry.
This includes using a well-known code base with a secure record, which is one way to achieve this.These libraries have been extensively tested and peer-reviewed, which reduces the chances of introducing vulnerabilities.
Developers should also use security checks such as “check-effect-interaction” designs to minimize the chance of reentry attacks by ensuring state modifications occur atomically.If available, you can use a reentrant and secure smart contract development framework to add additional defense against such vulnerabilities.
It is unlikely for developers to add security protection manually, as these frameworks often include built-in methods and protections specifically designed to avoid reentry attacks.However, as blockchain security is still evolving, developers must continue to look for new threats and weaknesses.