### Smart Contract Security Audit #### Advanced Smart Contract
Yuet Loo Wong
### Topics - Security Audit - Audit Report - Analysis Tools - References
### Security Audit An analysis of smart contracts in order to find security vulnerabilities, typically involves: 1. Analyze specifications 1. Run tests 1. Run automated analysis tools 1. Perform manual code audit 1. Create audit report
### Audit Report - Typically includes: - Disclaimer - Not guarantee bug free - What was audited, version - Findings - Severity: Critical, High, Low - Recommendations
### Issue Severity - Critical: can cost unlimited amount of users' funds - High: can cost limited amount of users' funds - Low: minor problems or better solutions
### Sample Audit Reports -
Dai Security Audit Report
-
OpenZeppelin Security Audit Report
### Security Tools - Static Analyzer in Remix - MythX in Remix or truffle -
Security Tools
### Manual Audit Automatic analysis tools is not reliable, give false positive and miss finding issues; manual auditing is still required
### Audit Checklists A list of common issues is a useful tool for security audit
### Useful Checklists and References -
Consensys Security Best Practices
-
Smart Contract Weakness Classification
-
Solidity Security: known attack vectors and fixes
-
Ethereum Safety Wiki
-
Smart contract security audit
-
Blockgeeks Security Audit Checklists
-
Ethernaut Contract Hacking Game
### Check for Warnings - Any compiler or analysis tool warnings? - Warnings could be - false positive - bug - == should be =
### Testing - Any test failures? - Any function missing tests?
### Code Currency - Use latest solidity compiler? - may have patches for security risks - solidity v0.6.0 changes - array.length-- is not allowed -
state variable shadowing
not allowed
### External Calls - Is there any state changes after external calls? - Is
Checks-Effects-Interactions
pattern used to avoid re-entrancy issue? - Are errors in external calls
handled properly
?
### Denial of Service (DoS) - Do external calls
favor push over pull
? - push: 2 accounts, A pushes to B - pull: 1 account, A pulls from contract - Is
withdrawal
pattern being used?
### DoS With Block Gas Limit - Cost of function execution exceeds block gas limit -
Here
is an example of modifying large array leads to denial of service attack
### Permission - Functions should have proper permission checking to ensure only the authorized can run the functions -
Unprotected selfdestruct
``` function kill() public { selfdestruct(msg.sender); } ```
### Failure Handling - Can the contract
pause operation
? - Can malicious actions be
delayed
? - How are fund at risk managed? - Can broken contract be
upgraded
? - Are users given grace period to upgrade?
### Front Running - Is there
transaction ordering dependence
? - Insertion attack: ERC20 Approve/TransferFrom - changing approval from 5 to 3 may result in 8 (5 + 3) tokens transfered - work around by approving 5, then 0, then 3
### Overflow - Is there integer wraparound issue? - When arithmetic reaches bound, it wraps - uint8(255) + uint8(1) == 0 - uint8(0) - uint8(1) == 255
### Division - Is there division by zero? - EVM will revert - Is there
rounding
issue?
### Delegate Calls - Is the context used properly? - Will the call cause
change of control
?
### Unexpected Contract Balance - Any strict assumption of contract balance? - Ether can be
forced sent to a contract
- Contract can be pre-funded with ether - Attack on this
example
can make claimReward() to always fail
### Bounty Program - Used to get community to audit code -
Consensys Bug Bounty List
-
Ethereum Bounty Program