Skip to content
Last updated: March 11, 2025

SCWE-038: Insecure Use of Selfdestruct

Stable Version v0.0.1

This content is in the version-(v0.0.1) and still under active development, so it is subject to change any time (e.g. structure, IDs, content, URLs, etc.).

Send Feedback

Relationships

  • CWE-732: Incorrect Permission Assignment for Critical Resource
    CWE-732 Link

Description

Insecure use of selfdestruct refers to vulnerabilities that arise when the selfdestruct function is used without proper safeguards. This can lead to: - Unauthorized destruction of the contract. - Loss of funds or data. - Exploitation of vulnerabilities in contract logic.

Remediation

  • Restrict access: Ensure only authorized addresses can call selfdestruct.
  • Implement circuit breakers: Add mechanisms to halt operations in case of suspicious activity.
  • Test thoroughly: Conduct extensive testing to ensure selfdestruct is used securely.

Examples

  • Insecure Selfdestruct Usage

    pragma solidity ^0.8.0;
    
    contract InsecureSelfdestruct {
        function destroy() public {
            selfdestruct(payable(msg.sender)); // No access control
        }
    }
    

  • Secure Selfdestruct Usage

    pragma solidity ^0.8.0;
    
    contract SecureSelfdestruct {
        address public admin;
    
        constructor(address _admin) {
            admin = _admin;
        }
    
        modifier onlyAdmin() {
            require(msg.sender == admin, "Unauthorized");
            _;
        }
    
        function destroy() public onlyAdmin {
            selfdestruct(payable(admin)); // Restricted to admin
        }
    }