DEX & Trading
LP-9024
DraftSecurity Audit Requirements for DeFi
Comprehensive security audit standards, requirements, and checklists for production DeFi protocols
LP-9024: Security Audit Requirements for DeFi
Abstract
This LP defines security audit requirements, vulnerability classifications, audit processes, and compliance standards for Lux DeFi protocols. Ensures all protocols handling user funds meet rigorous security standards.
Motivation
Security audits are essential for:
- Preventing financial losses from exploits
- Building user trust
- Regulatory compliance
- Insurance eligibility
- Protocol longevity
Specification
1. Audit Requirements
| Protocol Type | Required Audits | Minimum Auditors | Re-audit Frequency |
|---|---|---|---|
| Core DEX | 3 | Tier-1 | Every major upgrade |
| Lending | 3 | Tier-1 | Every major upgrade |
| Bridge | 4 | Tier-1 + Formal verification | Every upgrade |
| Oracle | 2 | Tier-1 | Annual + every upgrade |
| Vault/Strategy | 2 | Tier-1 or Tier-2 | Quarterly |
| Token | 1 | Tier-1 or Tier-2 | Initial + major changes |
2. Auditor Tiers
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
interface IAuditRegistry {
enum AuditorTier {
TIER_1, // Top-tier: Trail of Bits, OpenZeppelin, Consensys Diligence
TIER_2, // Established: Halborn, Peckshield, Certik, Code4rena
TIER_3, // Emerging: Community auditors, smaller firms
INTERNAL // Internal security team
}
struct Auditor {
bytes32 id;
string name;
AuditorTier tier;
uint256 auditsCompleted;
uint256 criticalFindings;
bool isActive;
}
struct AuditReport {
bytes32 id;
address protocol;
bytes32 auditorId;
uint256 startDate;
uint256 completionDate;
bytes32 commitHash;
bytes32 reportHash;
AuditResult result;
Finding[] findings;
}
struct Finding {
bytes32 id;
Severity severity;
string title;
string description;
string recommendation;
FindingStatus status;
bytes32 fixCommit;
}
enum Severity {
INFORMATIONAL,
LOW,
MEDIUM,
HIGH,
CRITICAL
}
enum FindingStatus {
OPEN,
ACKNOWLEDGED,
FIXED,
WONT_FIX,
DISPUTED
}
enum AuditResult {
PENDING,
PASSED,
PASSED_WITH_FINDINGS,
FAILED,
REQUIRES_REAUDIT
}
function registerAudit(AuditReport calldata report) external;
function getProtocolAudits(address protocol) external view returns (AuditReport[] memory);
function verifyAuditStatus(address protocol) external view returns (bool isAudited, uint256 lastAuditDate);
}
3. Vulnerability Classification
// Vulnerability categories and severity mapping
library VulnerabilityClassification {
// Critical (C) - Immediate fund loss possible
bytes32 constant REENTRANCY = keccak256("REENTRANCY");
bytes32 constant ORACLE_MANIPULATION = keccak256("ORACLE_MANIPULATION");
bytes32 constant FLASH_LOAN_ATTACK = keccak256("FLASH_LOAN_ATTACK");
bytes32 constant ACCESS_CONTROL_BYPASS = keccak256("ACCESS_CONTROL_BYPASS");
bytes32 constant ARBITRARY_EXTERNAL_CALL = keccak256("ARBITRARY_EXTERNAL_CALL");
bytes32 constant SIGNATURE_REPLAY = keccak256("SIGNATURE_REPLAY");
// High (H) - Significant fund risk
bytes32 constant PRICE_MANIPULATION = keccak256("PRICE_MANIPULATION");
bytes32 constant PRIVILEGE_ESCALATION = keccak256("PRIVILEGE_ESCALATION");
bytes32 constant DENIAL_OF_SERVICE = keccak256("DENIAL_OF_SERVICE");
bytes32 constant FRONT_RUNNING = keccak256("FRONT_RUNNING");
bytes32 constant INTEGER_OVERFLOW = keccak256("INTEGER_OVERFLOW");
// Medium (M) - Limited fund risk
bytes32 constant LOGIC_ERROR = keccak256("LOGIC_ERROR");
bytes32 constant MISSING_VALIDATION = keccak256("MISSING_VALIDATION");
bytes32 constant IMPROPER_STATE = keccak256("IMPROPER_STATE");
bytes32 constant GAS_GRIEFING = keccak256("GAS_GRIEFING");
// Low (L) - Minimal risk
bytes32 constant CODE_QUALITY = keccak256("CODE_QUALITY");
bytes32 constant GAS_OPTIMIZATION = keccak256("GAS_OPTIMIZATION");
bytes32 constant DOCUMENTATION = keccak256("DOCUMENTATION");
// Response requirements by severity
function getResponseTime(Severity severity) internal pure returns (uint256) {
if (severity == Severity.CRITICAL) return 4 hours;
if (severity == Severity.HIGH) return 24 hours;
if (severity == Severity.MEDIUM) return 7 days;
if (severity == Severity.LOW) return 30 days;
return 90 days; // Informational
}
}
4. Audit Checklist
# DeFi Security Audit Checklist
## 1. Access Control
- [ ] Admin functions properly protected
- [ ] Role-based access implemented correctly
- [ ] Owner cannot rug users
- [ ] Timelock on sensitive operations
- [ ] Multi-sig for critical functions
## 2. Reentrancy
- [ ] All external calls follow checks-effects-interactions
- [ ] ReentrancyGuard on vulnerable functions
- [ ] Cross-function reentrancy considered
- [ ] Cross-contract reentrancy considered
- [ ] Read-only reentrancy considered
## 3. Oracle Security
- [ ] Oracle manipulation resistance
- [ ] Staleness checks implemented
- [ ] Multiple oracle sources
- [ ] TWAP implementation correct
- [ ] Flash loan resistant pricing
## 4. Economic Security
- [ ] Flash loan attack vectors analyzed
- [ ] Sandwich attack protection
- [ ] Price impact limits
- [ ] Slippage protection
- [ ] MEV considerations
## 5. Math & Precision
- [ ] No integer overflow/underflow
- [ ] Correct decimal handling
- [ ] Rounding direction consistent
- [ ] No precision loss in calculations
- [ ] Fee calculations correct
## 6. Token Handling
- [ ] ERC20 return values checked
- [ ] Fee-on-transfer tokens handled
- [ ] Rebasing tokens considered
- [ ] Token approval race conditions
- [ ] Infinite approval risks
## 7. External Integrations
- [ ] External call return values checked
- [ ] Untrusted external calls minimized
- [ ] Callback safety verified
- [ ] Cross-chain message validation
- [ ] Third-party dependency risks
## 8. State Management
- [ ] Storage collision prevention
- [ ] Upgrade safety (UUPS/Transparent)
- [ ] Initialization protection
- [ ] State consistency across functions
- [ ] Emergency state handling
## 9. Gas & DOS
- [ ] Unbounded loops avoided
- [ ] Block gas limit considered
- [ ] DOS vectors identified
- [ ] Gas griefing prevented
- [ ] Efficient storage patterns
## 10. Cryptography
- [ ] Signature validation correct
- [ ] Replay protection implemented
- [ ] Randomness sources secure
- [ ] Hash functions appropriate
- [ ] Key management secure
5. Audit Process
interface AuditProcess {
phases: {
// Phase 1: Preparation (1-2 weeks)
preparation: {
steps: [
'Scope definition and documentation',
'Code freeze notification',
'Test suite review',
'Architecture documentation',
'Known issues disclosure',
];
deliverables: ['Scope document', 'Architecture diagram', 'Test results'];
};
// Phase 2: Initial Review (2-4 weeks)
initialReview: {
steps: [
'Automated analysis (Slither, Mythril, etc.)',
'Manual code review',
'Business logic analysis',
'Economic model review',
'Integration point analysis',
];
deliverables: ['Initial findings report'];
};
// Phase 3: Deep Dive (1-2 weeks)
deepDive: {
steps: [
'Exploit development for findings',
'Edge case analysis',
'Formal verification (if applicable)',
'Invariant testing',
'Attack simulation',
];
deliverables: ['Detailed findings with PoCs'];
};
// Phase 4: Remediation (1-2 weeks)
remediation: {
steps: [
'Finding review with team',
'Fix implementation',
'Fix verification',
'Regression testing',
'Re-review of changes',
];
deliverables: ['Fix verification report'];
};
// Phase 5: Final Report (1 week)
finalReport: {
steps: [
'Report compilation',
'Executive summary',
'Detailed findings documentation',
'Recommendation prioritization',
'Final sign-off',
];
deliverables: ['Final audit report'];
};
};
}
6. Continuous Security
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
interface IContinuousSecurity {
// Bug bounty integration
struct BugBounty {
uint256 criticalReward; // Up to $1M
uint256 highReward; // Up to $100K
uint256 mediumReward; // Up to $10K
uint256 lowReward; // Up to $1K
bool isActive;
address[] scope;
}
// Security monitoring
struct SecurityMonitor {
address[] watchedContracts;
bytes4[] monitoredFunctions;
uint256[] thresholds;
bool alertsEnabled;
}
function submitVulnerability(
address contract_,
Severity severity,
string calldata description,
bytes calldata proof
) external returns (bytes32 reportId);
function claimBounty(bytes32 reportId) external;
function getSecurityScore(address protocol) external view returns (uint256 score);
}
contract SecurityScoreCalculator {
struct ScoreFactors {
uint256 auditRecency; // Weight: 25%
uint256 auditCoverage; // Weight: 20%
uint256 bugBountyActive; // Weight: 15%
uint256 incidentHistory; // Weight: 20%
uint256 codeQuality; // Weight: 10%
uint256 testCoverage; // Weight: 10%
}
function calculateScore(address protocol) external view returns (uint256 score) {
ScoreFactors memory factors = getFactors(protocol);
// Calculate weighted score (0-100)
score = (
factors.auditRecency * 25 +
factors.auditCoverage * 20 +
factors.bugBountyActive * 15 +
factors.incidentHistory * 20 +
factors.codeQuality * 10 +
factors.testCoverage * 10
) / 100;
}
function getFactors(address protocol) internal view returns (ScoreFactors memory) {
// Implementation to gather security factors
return ScoreFactors(0, 0, 0, 0, 0, 0);
}
}
7. Formal Verification Requirements
// Formal verification specifications for critical protocols
interface IFormalVerification {
// Properties to verify
struct InvariantSpec {
string property;
string specification;
VerificationStatus status;
}
enum VerificationStatus {
PENDING,
VERIFIED,
FAILED,
PARTIAL
}
// Example invariants for DEX
// property: "No token loss"
// spec: "∀ swap: balance_before + amount_in = balance_after + amount_out + fee"
// property: "Constant product maintained"
// spec: "∀ tx: K_after >= K_before"
// property: "User funds recoverable"
// spec: "∀ user: canWithdraw(user.balance)"
}
// Tools to use:
// - Certora Prover
// - Echidna
// - Manticore
// - SMTChecker
8. Post-Audit Requirements
| Requirement | Timeline | Responsibility |
|---|---|---|
| Fix all Critical findings | Before deployment | Dev team |
| Fix all High findings | Before deployment | Dev team |
| Fix Medium findings | 30 days post-deploy | Dev team |
| Review Low findings | 90 days | Dev team |
| Publish audit report | At deployment | Security team |
| Update bug bounty scope | At deployment | Security team |
| Enable monitoring | At deployment | Ops team |
Bug Bounty Program
| Severity | Reward Range | Response Time |
|---|---|---|
| Critical | $100K - $1M | 4 hours |
| High | $25K - $100K | 24 hours |
| Medium | $5K - $25K | 72 hours |
| Low | $1K - $5K | 7 days |
| Informational | $100 - $1K | 30 days |
Rationale
The audit requirements are calibrated based on historical DeFi exploit analysis:
- Multiple auditors required because single-auditor reviews miss 40-60% of critical vulnerabilities based on industry data
- Tier-1 requirements for critical protocols reflect that top auditors have more comprehensive methodologies and catch more issues
- Bridge protocols require formal verification because bridge exploits account for >50% of DeFi losses
- Continuous security scoring enables dynamic risk assessment and insurance pricing
- Bug bounty integration extends security coverage beyond point-in-time audits
The severity-based response times align with industry standards and real-world incident response capabilities.
Backwards Compatibility
This LP builds on existing security infrastructure:
- Audit registries: Compatible with existing on-chain audit registries (e.g., Defisafety, DeFiLlama)
- Bug bounty platforms: Works with Immunefi, HackerOne, and Code4rena
- Monitoring tools: Integrates with Forta, OpenZeppelin Defender, and Tenderly
Protocols with existing audits can:
- Register historical audits in the on-chain registry
- Map previous findings to standardized severity levels
- Retroactively generate security scores
- Transition bug bounty programs to compliant structure
Security Considerations
- Auditor independence - No conflicts of interest
- Full code access - Complete codebase review
- Time adequacy - Sufficient time for thorough review
- Finding transparency - All findings disclosed
- Continuous improvement - Learn from each audit
Copyright
Copyright and related rights waived via CC0.