Self Destruct Vulnerability in Smart Contracts
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
Unrestricted Access: If the
selfdestructfunction is not properly protected (e.g., usingonlyOwner), anyone can call it to:Destroy the contract.
Send all its Ether to a specified address, potentially stealing funds.
Forced Ether Transfers: Attackers can use
selfdestructin 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).balancefor 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:
Restrict Access: Always ensure only authorized users can call
selfdestruct:function destroy(address payable recipient) public onlyOwner { selfdestruct(recipient); }Handle Forced Ether Transfers: Avoid relying solely on
address(this).balancefor logic. Use secure methods to track balances or payments.