LPsLux Proposals
DEX & Trading
LP-9024

Security Audit Requirements for DeFi

Draft

Comprehensive security audit standards, requirements, and checklists for production DeFi protocols

Category
Core
Created
2025-01-15

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 TypeRequired AuditsMinimum AuditorsRe-audit Frequency
Core DEX3Tier-1Every major upgrade
Lending3Tier-1Every major upgrade
Bridge4Tier-1 + Formal verificationEvery upgrade
Oracle2Tier-1Annual + every upgrade
Vault/Strategy2Tier-1 or Tier-2Quarterly
Token1Tier-1 or Tier-2Initial + 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

RequirementTimelineResponsibility
Fix all Critical findingsBefore deploymentDev team
Fix all High findingsBefore deploymentDev team
Fix Medium findings30 days post-deployDev team
Review Low findings90 daysDev team
Publish audit reportAt deploymentSecurity team
Update bug bounty scopeAt deploymentSecurity team
Enable monitoringAt deploymentOps team

Bug Bounty Program

SeverityReward RangeResponse Time
Critical$100K - $1M4 hours
High$25K - $100K24 hours
Medium$5K - $25K72 hours
Low$1K - $5K7 days
Informational$100 - $1K30 days

Rationale

The audit requirements are calibrated based on historical DeFi exploit analysis:

  1. Multiple auditors required because single-auditor reviews miss 40-60% of critical vulnerabilities based on industry data
  2. Tier-1 requirements for critical protocols reflect that top auditors have more comprehensive methodologies and catch more issues
  3. Bridge protocols require formal verification because bridge exploits account for >50% of DeFi losses
  4. Continuous security scoring enables dynamic risk assessment and insurance pricing
  5. 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:

  1. Register historical audits in the on-chain registry
  2. Map previous findings to standardized severity levels
  3. Retroactively generate security scores
  4. Transition bug bounty programs to compliant structure

Security Considerations

  1. Auditor independence - No conflicts of interest
  2. Full code access - Complete codebase review
  3. Time adequacy - Sufficient time for thorough review
  4. Finding transparency - All findings disclosed
  5. Continuous improvement - Learn from each audit

Copyright and related rights waived via CC0.