Smart Contract Security: Protecting Your Blockchain Applications
Security in smart contracts is paramount - once deployed, they cannot be modified. This guide covers essential security practices and common vulnerabilities in smart contract development.
Common Vulnerabilities
1. Reentrancy Attacks
// Vulnerable contract
contract Vulnerable {
mapping(address => uint) public balances;
function withdraw() public {
uint balance = balances[msg.sender];
(bool success, ) = msg.sender.call{value: balance}("");
require(success);
balances[msg.sender] = 0; // Too late!
}
}
// Secure contract
contract Secure {
mapping(address => uint) public balances;
function withdraw() public {
uint balance = balances[msg.sender];
balances[msg.sender] = 0; // Update before external call
(bool success, ) = msg.sender.call{value: balance}("");
require(success);
}
}
2. Integer Overflow/Underflow
Use SafeMath or Solidity 0.8+ built-in checks:
// Solidity 0.8+ automatically includes overflow checks
uint256 a = type(uint256).max;
uint256 b = a + 1; // This will revert
3. Access Control Vulnerabilities
contract AccessControlled {
address private owner;
mapping(address => bool) private admins;
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
modifier onlyAdmin() {
require(admins[msg.sender], "Not admin");
_;
}
}
Security Best Practices
-
Checks-Effects-Interactions Pattern
- Perform all checks first
- Make state changes
- Interact with external contracts last
-
Use OpenZeppelin Contracts
- Tested, audited implementations
- Regular security updates
- Community-reviewed code
-
Proper Testing
- Unit tests for all functions
- Integration tests
- Fuzzing tests
- Test on testnet before mainnet
-
Access Control
- Implement proper authorization
- Use multi-sig where appropriate
- Time locks for sensitive operations
Auditing Tools
-
Static Analysis
- Slither
- Mythril
- Solhint
-
Dynamic Analysis
- Echidna
- Manticore
Incident Response Plan
-
Monitoring
- Set up monitoring for unusual activity
- Implement circuit breakers
-
Emergency Response
- Have a pause mechanism
- Plan for upgrades if needed
- Document recovery procedures