Vulnerable liquidity slippage calculation doesn’t ensure slippage protection due to unscaled assets sum
The stableswap liquidity slippage check adds raw token amounts without scaling for differing decimals, so deposits with very different real value produce the same total. An attacker could add liquidity at a worse ratio than their stated slippage tolerance without the contract rejecting it.
Submitted by oakcobalt
/contracts/pool-manager/src/helpers.rs#L426
/contracts/pool-manager/src/helpers.rs#L429
Finding description and impact
Current slippage calculation for providing liquidity to stableswap pool uses simple sum of unscaled asset balance. This is vulnerable because it doesn’t account for differences in asset decimals, which might cause slippage calculation to be invalid.
Proof of Concept
Vulnerable case: (1000000 usdc + 1e18 dai) vs (1e18 usdc + 1e6 dai)
The pool impact of depositing 1000000 usdc + 1e18 dai vs depositing 1e18 usdc + 1e6 dai are expected to diverge.
However, in the current slippage check, both cases will lead to the same deposits_total. This invalidates the pool_ratio/deposit_ratio calculation.
//contracts/pool-manager/src/helpers.rs
pub fn assert_slippage_tolerance(
slippage_tolerance: &Option<Decimal>,
deposits: &[Coin],
pools: &[Coin],
pool_type: PoolType,
amount: Uint128,
pool_token_supply: Uint128,
) -> Result<(), ContractError> {
if let Some(slippage_tolerance) = *slippage_tolerance {
...
let deposits: Vec<Uint256> = deposits.iter().map(|coin| coin.amount.into()).collect();
let pools: Vec<Uint256> = pools.iter().map(|coin| coin.amount.into()).collect();
// Ensure each prices are not dropped as much as slippage tolerance rate
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);
// the slippage tolerance for the stableswap can't use a simple ratio for calculating
// slippage when adding liquidity. Due to the math behind the stableswap, the amp factor
// needs to be in as well, much like when swaps are done
|> if pool_ratio * one_minus_slippage_tolerance > deposit_ratio {
return Err(ContractError::MaxSlippageAssertion);
}
/contracts/pool-manager/src/helpers.rs#L424-L429
Flows: contracts/pool-manager/src/liquidity/commands::provide_liquidity → helpers::assert_slippage_tolerance
As we can see, in the vulnerable case above, pools_total and deposits_total will be the same in both cases where the actual deposits assets amount when aligned to 18 decimals and the actual deposit assets value vastly differ. This invalidates the comparison check results.
Recommended mitigation steps
Scale the asset to the same decimal before performing sum.
jvr0x (MANTRA) confirmed via chat with C4 staff
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 sums raw token amounts without normalising each asset to a common decimal scale before comparing ratios.
Impact
The slippage tolerance check may pass when the real deposit ratio is far worse than the user allowed, so liquidity providers could lose value without the contract reverting.
What the attacker needed
A stableswap pool with tokens of differing decimals; user provides liquidity with a slippage tolerance
The fix
The assets should be scaled to the same number of decimals before summing them in the slippage check.
How it was found
- Call provide_liquidity on a stableswap pool with a slippage tolerance set.
- The flow reaches helpers::assert_slippage_tolerance.
- The function sums the raw pool and deposit amounts without scaling for decimals.
- The resulting ratios are compared, but the comparison is invalid because the totals do not reflect real value.
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.
Tokens can have different numbers of decimal places, so one unit of one token is not worth the same as one unit of another. The contract added the raw numbers together without converting them to a common scale, so two very different deposits looked identical to the safety check. That means a user's slippage protection could silently fail.
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.