Slippage tolerance vulnerability in StableSwap
The slippage tolerance check for StableSwap deposits compared a simple ratio of total pool balances to pool token supply against the deposit ratio, ignoring the StableSwap invariant and amplification factor. This could let deposits through whose real price impact exceeds the user's stated tolerance, causing unexpected losses.
Submitted by DOWSERS, also found by Lambda
The assert_slippage_tolerance function does not properly account for the non-linear pricing curve of StableSwap pools, which are influenced by the amplification factor (A). Specifically, the function calculates slippage tolerance based solely on a simple ratio of total deposits and pool tokens without integrating the StableSwap invariant (D). This oversight can lead to the acceptance of transactions with higher actual slippage than allowed, exposing the protocol to user losses or economic inefficiencies.
Link to code
/contracts/pool-manager/src/helpers.rs#L423
match pool_type {
PoolType::StableSwap { .. } => {
let pools_total: Uint256 = pools
.into_iter()
.fold(Uint256::zero(), |acc, x| acc.checked_add(x).unwrap());
let deposits_total: Uint256 = deposits
.into_iter()
.fold(Uint256::zero(), |acc, x| acc.checked_add(x).unwrap());
let pool_ratio = Decimal256::from_ratio(pools_total, pool_token_supply);
let deposit_ratio = Decimal256::from_ratio(deposits_total, amount);
if pool_ratio * one_minus_slippage_tolerance > deposit_ratio {
return Err(ContractError::MaxSlippageAssertion);
}
}
}
Impact
Failure to account for the StableSwap invariant (D) and amplification factor (A) allows for:
- Incorrect slippage validation: Transactions with actual slippage exceeding the user-defined tolerance may be accepted.
- User losses: Users may incur unexpected losses due to high slippage.
Example: Incorrect slippage validation:
- Initial pool: [1000, 1000].
- Amplification factor (A): 100.
- User deposit: [100, 50].
- Slippage tolerance: 1% (0.01).
pool_token_supply: 2000.- Amount (pool token received): 150.
Steps:
-
Calculate total values:
pools_total = 1000 + 1000 = 2000.deposits_total = 100 + 50 = 150.
-
Calculate ratios:
pool_ratio = pools_total / pool_token_supply = 2000 / 2000 = 1.0.deposit_ratio = deposits_total / amount = 150 / 150 = 1.0.
-
Slippage check:
pool_ratio * (1 - slippage_tolerance) = 1.0 * 0.99 = 0.99.deposit_ratio = 1.0.- Result:
0.99 > 1.0is false, so the transaction is accepted.
Real slippage using StableSwap invariant
- Before deposit:
𝐷initial= calculation based on [1000,1000] and𝐴= 100.
Let’s say 𝐷initial = 2000 (perfect equilibrium)
- After deposit:
[1000 + 100, 1000 + 50] = [1100, 1050].
𝐷final = StableSwap invariant recalculated
Let’s say 𝐷final = 2105.
Relative change:
- Actual slippage =
(𝐷final - 𝐷initial) / 𝐷initial = (2105 - 2000) / 2000 = 0.0525(5.25%).
Comparison with Tolerance:
- Specified tolerance : 1% (0.01).
- Actual slippage = 5.25%
>1%, so the transaction should have been rejected.
Risk
- Likelihood: Medium, the vulnerability depends on specific deposit patterns, such as highly imbalanced deposits.
- Impact: Medium, users face financial losses.
Recommended mitigation steps
Implement a StableSwap-specific slippage calculation that incorporates the invariant (D) and amplification factor (A):
- Calculate (D) before and after the deposit.
- Derive the price impact based on (D) and compare it to the user-defined slippage tolerance.
jvr0x (MANTRA) confirmed
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the slippage check used a linear ratio of summed balances to token supply instead of computing the StableSwap invariant before and after the deposit.
Impact
Users could incur unexpected losses because deposits with real slippage above their tolerance may be accepted; the report rates likelihood and impact as medium.
What the attacker needed
Ability to submit a liquidity deposit; imbalanced deposit amounts; StableSwap pool with amplification factor
The fix
The slippage check should compute the StableSwap invariant before and after the deposit and compare the resulting price impact against the user's tolerance.
How it was found
- Take a pool with equal balances and a high amplification factor.
- Submit an imbalanced deposit with a tight slippage tolerance.
- Observe the ratio-based check accepts the deposit.
- Recompute the invariant before and after to see the real slippage exceeds the tolerance.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A liquidity pool holds two kinds of tokens, and when you add more of one than the other you shift the balance and effectively get a slightly worse deal, called slippage. Users set a maximum slippage they will accept, and the code is meant to reject anything worse. Here the check used a rough ratio that ignores how StableSwap pools actually price trades, so a deposit could pass the check even though the real slippage was far above the limit.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.