skka3134

skka3134

email
telegram

Smart Contract Security: 3. Reentrancy Attack

What is a reentrancy attack?

When a contract is executing a certain function, an attacker can repeatedly call that function in various ways. Due to the dependency on the contract state, repeated calls can disrupt the expected logic. For example, a contract may decrease a user's balance before a transfer and increase the recipient's balance after the transfer. By repeatedly calling the contract when the balance is decreasing, an attacker can reduce the balance multiple times.

Methods to prevent reentrancy attacks:

Set a state variable to prevent reentrancy before executing a function.
Avoid external contract calls. Secure calling methods can be defined through interfaces.
Use a mutex (Mutex) to prevent conflicts in simultaneous function calls.
Avoid directly calling transferFrom after token approval. The approval amount should be checked between the two steps.

Code example:

// Use mutex to prevent reentrancy
Mutex mutex; 

function withdraw(uint amount) external {
  require(!mutex.locked());
  mutex.lock();
  
  msg.sender.call.value(amount)();

  mutex.release();
}

// Use state variable to prevent reentrancy
uint256 protecting;

function withdraw() external {
  require(protecting == 0);
  protecting = 1;

  msg.sender.transfer(balance[msg.sender]);

  protecting = 0;
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.