Spread calculation does not account for swap fees
The spread calculation in the swap function includes swap fees, making the computed spread larger than the actual slippage. This could allow a swap to pass the slippage tolerance check when it should fail.
Abdessamed Follow with 0x1982us, 0xRajkumar and 1 other
Originally disclosed on Code4rena
Submitted by Abdessamed, also found by 0x1982us, 0xRajkumar, and DOWSERS
/contracts/pool-manager/src/helpers.rs#L182-L185
Vulnerability Details
The spread represents the difference between the expected trade amount and the actual trade amount received during a swap. It is calculated to verify that the slippage remains within the user-defined tolerance during the swap operation. The spread formula varies depending on the pool type:
pub fn compute_swap(
n_coins: Uint256,
offer_pool: Uint128,
ask_pool: Uint128,
offer_amount: Uint128,
pool_fees: PoolFee,
swap_type: &PoolType,
offer_precision: u8,
ask_precision: u8,
) -> Result<SwapComputation, ContractError> {
// --SNIP
match swap_type {
PoolType::ConstantProduct => {
let return_amount: Uint256 = Decimal256::from_ratio(ask_pool.mul(offer_amount), offer_pool + offer_amount).to_uint_floor();
let exchange_rate = Decimal256::checked_from_ratio(ask_pool, offer_pool).map_err(|_| ContractError::PoolHasNoAssets)?;
let spread_amount: Uint256 = (Decimal256::from_ratio(offer_amount, Uint256::one()).checked_mul(exchange_rate)?.to_uint_floor())
@>>> .checked_sub(return_amount)?;
// --SNIP
let fees_computation: FeesComputation = compute_fees(pool_fees, return_amount)?;
Ok(get_swap_computation(
return_amount,
spread_amount,
fees_computation,
)?)
}
PoolType::StableSwap { amp } => {
// --SNIP
let return_amount = ask_pool.to_uint256_with_precision(u32::from(ask_precision))?.checked_sub(Uint256::from_uint128(new_pool))?;
// the spread is the loss from 1:1 conversion
// thus is it the offer_amount - return_amount
let spread_amount = offer_amount.to_uint256_with_precision(u32::from(ask_precision))?
@>>> .saturating_sub(return_amount);
let fees_computation = compute_fees(pool_fees, return_amount)?;
Ok(get_swap_computation(
return_amount,
spread_amount,
fees_computation,
)?)
}
}
}
The issue, as highlighted above, is the fact that the spread is calculated based on the return_amount without excluding the fees. As a result, the calculated spread_amount is higher than intended in which the slippage tolerance check in assert_max_spread may pass, when it should not.
Impact
The calculated spread_amount is higher than the actual slippage because it includes the swap fees.
Recommended mitigation steps
Consider subtracting the fees from return_amount before calculating the spread_amount.
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
What the attacker needed
None
The fix
The fees should be subtracted from the return amount before calculating the spread amount.
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.
When you swap tokens, the system checks that the price you get is not too far from what you expected. This check uses a number called 'spread'. The bug is that the spread is calculated after fees are taken out, so it looks bigger than it really is. That means a trade that should be rejected for being too far off might be allowed.
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.