Security: Fix 3 critical AMM vulnerabilities (LP inflation attack, swap logic bugs) - #1
Open
agentmila wants to merge 1 commit into
Open
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.rsDescription: 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:
9999/10001 * 1 = 0(integer division rounds down)Impact: Complete loss of funds for any depositor after the attacker.
Fix: Lock
MINIMUM_LIQUIDITY = 1000LP tokens permanently on first deposit (Uniswap V2 pattern).Vulnerability 2: Swap Formula Uses Inverted Reserves (CRITICAL)
Location:
amm/programs/amm/src/instructions/swap.rsDescription: The constant product formula has
from_reserveandto_reserveswapped, 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.rsDescription: 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.