Lending protocol security

From PlamenTSV/plamen · View interactive version

Lending protocol security is an AI agent skill from the GitHub repository PlamenTSV/plamen. Protocol Type Trigger lending (detected when recon finds liquidate|borrow|repay|collateral|lend|loan|LTV|healthFactor|interestRate|debtToken) - Inject Into Breadth agents, depth...

How to use this skill

Skill file

Injectable Skill: Lending Protocol Security

Protocol Type Trigger: lending (detected when recon finds: liquidate|borrow|repay|collateral|lend|loan|LTV|healthFactor|interestRate|debtToken)

Inject Into: Breadth agents, depth-token-flow, depth-edge-case, depth-state-trace

Language: Language-agnostic methodology

Finding prefix: [LEND-N]

Orchestrator Decomposition Guide

When decomposing this skill into depth agent investigation questions, map sections to domains:

When This Skill Activates

Recon classifies protocol as lending type based on indicators: liquidate, borrow, repay, collateral, lend, loan, LTV, healthFactor, interestRate, debtToken, reserve, utilizationRate, debtShare.

This skill adds lending-specific checks that the general ECONOMIC_DESIGN_AUDIT does not cover.


1. Health Factor Boundary Analysis

1a. Health Factor Computation

Identify the health factor (HF) formula and trace each input:

Substitute boundary values into the formula:

| State | HF Value | Expected Behavior | Actual? |

|-------|----------|-------------------|---------|

| HF = 1.0 exactly | | Liquidation threshold - which side? | |

| HF = 1.0 + 1 wei | | Should NOT be liquidatable | |

| HF = 1.0 - 1 wei | | Should be liquidatable | |

| HF after max interest accrual | | Worst case between check intervals | |

| HF with dust collateral | | Liquidation gas > reward? | |

1b. Check-to-Execution Consistency

1c. Dust Position Economics

Tag: [BOUNDARY:HF={value} → liquidatable={YES/NO} → operator={>=/>}]


1d. Guard Selector → Post-Transaction Enforcement Completeness

For protocols that use transaction guards or post-transaction hooks with conditional

enforcement checks (e.g., post-transaction guard/hook triggered by selector or return value):

  1. Enumerate ALL function selectors the guard/hook handles
  2. For each selector: does the operation affect HF? (changes collateral amount,

debt amount, collateral factor, efficiency/isolation mode, reserve configuration)

  1. For each HF-affecting selector: does the guard/hook trigger the

post-transaction HF enforcement check?

  1. Flag any HF-affecting selector where post-tx enforcement is skipped

| Selector | Function | Affects HF? | Triggers Post-Tx HF Check? | Gap? |

|----------|----------|-------------|---------------------------|------|

Tag: [TRACE:guard_hook_selector={fn} → HF_affecting={YES/NO} → post_tx_check={YES/NO}]


2. Interest Accrual Correctness

2a. Accrual Timing

Identify the interest accrual mechanism and trace when it updates:

2b. Index Update Ordering

For index-based interest (where debt = principal * currentIndex / borrowIndex):

2c. Precision Loss

2d. Pause-Interest Interaction

Tag: [TRACE:accrueInterest() → index_update_order={before/after} → user_balance_change → {correct/misordered}]


3. Liquidation Mechanism Safety

3a. Liquidation Profitability

For the liquidation incentive structure:

3b. Partial vs Full Liquidation

3c. Collateral Selection

3d. Front-Running Protection

Tag: [TRACE:liquidate(amount={X}) → bonus={Y} → seized_collateral={Z} → remaining_HF={value}]


3b. Liquidation DoS Vectors

Callback Reverts

Token Blocklist Interaction

Gas Bounds

Reentrancy Guard Conflicts

Tag: [TRACE:liquidate() → callback_to_borrower={YES/NO} → revert_possible={YES/NO} → fallback={exists/missing}]


3c. Bad Debt Socialization

Bad Debt Detection

Socialization Mechanism

Trace the bad debt absorption path in order:

  1. Is there an insurance fund or reserve? What fills it? (liquidation fees, protocol revenue)
  2. If insurance is exhausted: is bad debt spread across all lenders? (share price reduction, or explicit socialization call)
  3. Is there a protocol backstop? (treasury injection, governance action)
  4. What is the ordering? (insurance → socialization → backstop)

Bad Debt Amplification

Tag: [TRACE:position_underwater → insurance_fund={sufficient/exhausted} → socialization={mechanism} → lender_loss={amount}]


4. Collateral Factor Manipulation

4a. Retroactive Factor Changes

4b. Boundary Positions

Tag: [VARIATION:collateralFactor 80%→79% → positions_at_80%_LTV → instantly_liquidatable={YES/NO}]


5. Asymmetric Pause Analysis

5a. Pause Granularity

Enumerate all pausable functions and their pause groupings:

| Function | Pause Group | Paused Independently? |

|----------|-------------|----------------------|

| deposit | | |

| withdraw | | |

| borrow | | |

| repay | | |

| liquidate | | |

5b. Dangerous Asymmetries

Check for these specific asymmetric pause states:

5c. Post-Unpause Grace Period

Tag: [TRACE:pause(repay) → interest_accrues={YES/NO} → liquidation_active={YES/NO} → borrower_recourse={YES/NO}]


6. Oracle Dependency for Pricing

Cross-reference with ORACLE_ANALYSIS skill for general oracle checks. This section covers lending-specific oracle concerns only.

6a. Price-Token Matching

6b. Stale Oracle Impact on Liquidation

6c. Self-Liquidation via Oracle Manipulation

Tag: [TRACE:oracle_price={source} → token_match={exact/wrapper/underlying} → stale_revert_blocks_liquidation={YES/NO}]


Common False Positives

Step Execution Checklist (MANDATORY)

| Section | Required | Completed? | Notes |

|---------|----------|------------|-------|

| 1. Health Factor Boundary Analysis | YES | | HF computation, boundaries, dust economics |

| 1d. Guard Selector Enforcement | IF transaction guard pattern detected | | Post-tx HF check completeness per selector |

| 2. Interest Accrual Correctness | YES | | Timing, index ordering, precision, pause interaction |

| 3. Liquidation Mechanism Safety | YES | | Profitability, partial/full, collateral selection, front-running |

| 3b. Liquidation DoS Vectors | YES | | Callbacks, blocklists, gas bounds, reentrancy conflicts |

| 3c. Bad Debt Socialization | YES | | Detection, socialization mechanism, amplification |

| 4. Collateral Factor Manipulation | IF admin-configurable factors | | Retroactive changes, boundary positions |

| 5. Asymmetric Pause Analysis | IF pause mechanism detected | | Pause granularity, dangerous asymmetries, grace period |

| 6. Oracle Dependency for Pricing | IF oracle integration detected | | Price-token matching, stale oracle, self-liquidation |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.