Skip to content

Security: Fix 3 critical AMM vulnerabilities (LP inflation attack, swap logic bugs) - #1

Open
agentmila wants to merge 1 commit into
jogeshwar01:mainfrom
agentmila:fix/amm-critical-vulnerabilities
Open

Security: Fix 3 critical AMM vulnerabilities (LP inflation attack, swap logic bugs)#1
agentmila wants to merge 1 commit into
jogeshwar01:mainfrom
agentmila:fix/amm-critical-vulnerabilities

Conversation

@agentmila

Copy link
Copy Markdown

Security Audit: Critical AMM Vulnerabilities

Summary

An audit of the AMM program revealed 3 critical vulnerabilities that can lead to loss of user funds and broken swap functionality.


Vulnerability 1: First-Depositor LP Inflation Attack (CRITICAL - Fund Loss)

Location: amm/programs/amm/src/instructions/deposit.rs

Description: The first liquidity provider receives LP tokens equal to sqrt(quantity_a * quantity_b) with no minimum liquidity lock. This enables a classic inflation attack:

Attack Scenario:

  1. Attacker deposits 1 token_a + 1 token_b → receives 1 LP token
  2. Attacker donates (transfers directly) 10,000 token_a + 10,000 token_b to the reserves
  3. Each LP token is now worth ~10,001 of each token
  4. Victim deposits 9,999 token_a + 9,999 token_b
  5. LP calculation: 9999/10001 * 1 = 0 (integer division rounds down)
  6. Victim gets 0 LP tokens but their funds are in the pool
  7. Attacker redeems 1 LP token → gets ~20,000 of each token (stealing victim funds)

Impact: Complete loss of funds for any depositor after the attacker.

Fix: Lock MINIMUM_LIQUIDITY = 1000 LP tokens permanently on first deposit (Uniswap V2 pattern).


Vulnerability 2: Swap Formula Uses Inverted Reserves (CRITICAL)

Location: amm/programs/amm/src/instructions/swap.rs

Description: The constant product formula has from_reserve and to_reserve swapped, producing incorrect output amounts.

Fix: Use output_reserve * amountIn / (input_reserve + amountIn).


Vulnerability 3: Swap Token Routing Sends to Wrong Reserves (CRITICAL - DoS)

Location: amm/programs/amm/src/instructions/swap.rs

Description: Swap transfers input tokens to the output reserve (mint mismatch), causing all swaps to always revert.

Fix: Route input tokens to input_reserve, output tokens from output_reserve.

…c bugs

Three critical vulnerabilities found and fixed in the AMM program:

1. CRITICAL: First-depositor LP inflation attack (fund loss)
   - No minimum liquidity lock on first deposit
   - Attacker can steal all subsequent depositors' funds
   - Fix: Lock MINIMUM_LIQUIDITY (1000) on first deposit (Uniswap V2 pattern)

2. CRITICAL: Swap formula uses inverted reserves
   - from_reserve and to_reserve swapped in calculation
   - Results in incorrect swap amounts (users get wrong output)
   - Fix: Use output_reserve in numerator, input_reserve in denominator

3. CRITICAL: Swap token routing sends to wrong reserves
   - User's input tokens sent to output reserve (mint mismatch = always reverts)
   - Output tokens sent from input reserve (mint mismatch = always reverts)
   - Fix: Route input tokens to input_reserve, output from output_reserve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant