This threat model is designed for EVM smart contracts (Solidity) and evaluates significant attack vectors and mitigations. It assumes a production-grade DeFi or token contract using OpenZeppelin patterns and upgradeable proxies.
- Description: An external call to an attacker-controlled contract re-enters the vulnerable function before state updates are finalized.
- Example:
withdrawfunction updates balance after ETH transfer. - Impact: Funds drained, invariant violation, leveraged attacks.
- Mitigations:
- Checks-Effects-Interactions pattern.
nonReentrantmodifier from OpenZeppelinReentrancyGuard.- Use
transfer/sendfor small fixed stipend or pull patterns.
- Description: Adversary reorders transactions in the mempool to gain profit by exploiting price-sensitive operations.
- Common targets:
swap,liquidate,mint,redeem,oracle updates. - Mitigations:
- Use time-weighted average price (TWAP) or oracle-signed data.
- Use commit-reveal or auction mechanisms.
- Optimize gas to avoid miners dropping transactions.
- Protect from sandwich attacks via slippage controls.
- Description: Arithmetic overflow/underflow when using unchecked math on uints/ints.
- Solidity >=0.8 has built-in overflow checks by default, but unchecked blocks and inline assembly can bypass.
- Mitigations:
- Prefer
SafeMathsemantics if using older compiler versions. - Avoid unchecked loops and manual casts.
- Add explicit bounds checks for array indices and multiplications.
- Prefer
- Description: Contract logic can be blocked by one or more malicious actors (e.g., gas exhaustion, locked states, blocklist).
- Variants:
- DoS with block gas limit (e.g., unbounded iteration over user array).
- Starvation attacks (e.g., set a high fee that prevents calls).
- Mitigations:
- Avoid unbounded loops; use pagination or checkpoints.
- Design for single-user failure to not break global operation.
- Use
requireguards and break up operations into multiple transactions.
- Use
AccessControlorAccessControlEnumerable. - Define roles as
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");. - Setup roles with
grantRoleandrevokeRole. - Ensure at least one
DEFAULT_ADMIN_ROLEholder for recovery, but use MultiSig for high-value contracts. - Example:
hasRole(ADMIN_ROLE, msg.sender)guard.onlyRolemodifier usage.
- Never use
tx.originfor auth. - Enforce least privilege, separate
PAUSER_ROLE,UPGRADER_ROLE,MINTER_ROLE. - Protect role renouncement paths and emergency admin key compromise.
- Provide slippage / max price impact parameters in functions that execute swaps or pricing-sensitive operations.
- Validate on-chain prices with oracle oracles such as Chainlink.
require(amountOut >= minAmountOut, "Slippage exceeded").
- Description: avoid sending ETH/tokens to arbitrary user-controlled addresses in same function; instead record entitlements and let users withdraw.
- Benefits: prevents reentrancy, failed transfer due to gas/stipend constraints, and reduces atomic risk.
- Example:
pendingWithdrawals[user] += amount;followed by user callingwithdraw().
- Use
Pausablefrom OpenZeppelin. - Implement
pause()andunpause()guarded byPAUSER_ROLE. - Add
whenNotPausedandwhenPausedto critical operational methods. - Include a
isPaused()public view.
- Add emergency
rescueTokens/rescueETHfunction with strict access control and timelock. - Log events:
EmergencyPause,EmergencyUnpause,RescueTokens.
- Code review and static analysis by two or more peers.
- Keep contract size manageable, avoid enormous logic in single contract.
- Use immutable state and constants when possible.
- Minimize trust assumptions, design robust invariants.
- Include comprehensive unit tests and fuzz testing.
- Establish bug bounty and responsible disclosure policy.
- Slither: static analysis and invariant checking; run
slither . --solc-remaps .... - MythX / Certora / VeriSol: deeper semantic analysis.
- Echidna: property-based fuzzing to catch assertion violations.
- Foundry
forge test --fuzzplusforge coverage. - Formal verification of core invariants (balance conservation, role invariants) through tools such as Scribble with
yulformalization. - Third-party audit briefing: supply detailed architecture, state machine, and threat model.