Self Destruct Vulnerability in Smart Contracts

·

1 min read

A selfdestruct vulnerability happens when a smart contract allows unauthorized users to call its selfdestruct function, which can lead to serious consequences:

How It Becomes a Vulnerability

  1. Unrestricted Access: If the selfdestruct function is not properly protected (e.g., using onlyOwner), anyone can call it to:

    • Destroy the contract.

    • Send all its Ether to a specified address, potentially stealing funds.

  2. Forced Ether Transfers: Attackers can use selfdestruct in another contract to forcefully send Ether to your contract. This can:

    • Manipulate the contract's balance (e.g., making it appear as if it holds more Ether than it actually earned).

    • Cause issues if your contract relies on address(this).balance for logic, leading to unexpected behavior.

Example of Vulnerability:

Unprotected Selfdestruct

contract Vulnerable {
    function destroy(address payable recipient) public {
        selfdestruct(recipient); // No access control!
    }
}
  • What Happens: Anyone can call destroy() to delete the contract and steal its funds.

How to Prevent It:

  1. Restrict Access: Always ensure only authorized users can call selfdestruct:

     function destroy(address payable recipient) public onlyOwner {
         selfdestruct(recipient);
     }
    
  2. Handle Forced Ether Transfers: Avoid relying solely on address(this).balance for logic. Use secure methods to track balances or payments.