Akhil Sharma
Mar 2, 2023

What is reentrancy attack in Solidity language?

Reentrancy attack allows an attacker to repeatedly execute a specific function within a smart contract before the previous function execution has been completed.

What is reentrancy attack in Solidity language?

A reentrancy attack is a type of vulnerability that occurs in a smart contract-based system, particularly in Ethereum blockchain. It allows an attacker to repeatedly execute a specific function within a smart contract before the previous function execution has been completed, potentially leading to unauthorized access to the contract's state and funds. The word reentrancy stems from the sphere of general computing. Reentrancy occurs each time program execution is interrupted and gets restarted, with no errors visible even though both operations run completed.

Here's how a reentrancy attack works:

  1. An attacker deploys a smart contract that contains a vulnerable function that calls an external contract's function.
  2. The attacker sends a transaction to the vulnerable function and passes the address of the external contract as a parameter.
  3. When the vulnerable function executes, it calls the external contract's function and pauses the execution of the vulnerable function.
  4. The external contract's function then calls back the vulnerable function before the first execution completes.
  5. The vulnerable function then executes again, allowing the attacker to withdraw funds or manipulate the contract's state.
  6. The process continues in a loop until the attacker has exhausted the contract's funds or the contract is stopped by the network.

Here is a list some of the exploits of this kind that has been in the spotlight for the last few years:

  • Uniswap/Lendf.Me lost $25 million
  • The BurgerSwap lost $7.2 million
  • The SURGEBNB lost $4 million
  • CREAM FINANCE lost $18.8 million
  • Siren protocol lost $3.5 million
  • Fei Protocol lost $80 million

Still, the most notorious exploit using the reentrancy vulnerability happened on June 17, 2016, when a bad actor siphoned 3.6 million ETH from TheDao’s smart contract (about 5% of all ETH at that time). Quite sophisticated, the attack involved creating a child DAO and calling several functions. The hacker managed to transfer funds to his personal wallet only 41 days later.

That being said, not every reentrant behavior can be called a bug. It can turn into a critical security issue if two conditions are met:

  • One contract calls another one while the former hasn’t updated its state yet. This can be accidentally enabled by adding a function that makes an external call to another untrusted contract before the state changes get executed.
  • Once the malicious actor gains control over the untrusted contract, they can recursively call the original function over and over again, draining crypto wallets or sneaking into the code to make unsafe changes.

To prevent reentrancy attacks, developers can implement various security measures such as:

  1. Use the "checks-effects-interactions" pattern where interactions with external contracts are performed only after all checks have been performed.
  2. Implement a withdrawal pattern where the amount to be withdrawn is first stored in a local variable before being transferred to an external account.
  3. Limit the amount of gas that can be used by the function to prevent infinite loops.
  4. Use the "require" statement to verify if the contract is in the expected state before any interaction with external contracts.

Reentrancy typically occurs across multiple functions or contracts, so preventing reentrancy within a single function is not enough. You have to finish all state changes before calling an external function. Overall, it's important for developers to thoroughly audit their smart contracts and implement proper security measures to prevent reentrancy attacks and other vulnerabilities.