withdraw_liquidity lacks slippage protection
The withdraw_liquidity function in a Cosmos-based DEX lacks a way for users to specify a minimum acceptable amount of tokens when withdrawing liquidity. This means users could receive fewer tokens than expected due to market changes between transaction submission and execution.
Abdessamed Follow with 0x1982us, 0xAlix2 and 9 others
Originally disclosed on Code4rena
Submitted by Abdessamed, also found by 0x1982us, 0xAlix2, 0xRajkumar, Bauchibred, carrotsmuggler, DadeKuma, Egis_Security, honey-k12, jasonxiale, Sparrow, and Usagi
/contracts/pool-manager/src/liquidity/commands.rs#L412-L503
Vulnerability Details
The withdraw_liquidity function allows users to withdraw assets from a pool in exchange for burning their LP tokens. However, the function does not provide a mechanism for users to specify the minimum amount of tokens they are willing to accept upon withdrawal. This omission exposes users to the risk of receiving fewer tokens than expected due to market conditions especially for ConstantProduct pools between the transaction initiation and execution.
pub fn withdraw_liquidity(
deps: DepsMut,
env: Env,
info: MessageInfo,
pool_identifier: String,
) -> Result<Response, ContractError> {
// --SNIP
let refund_assets: Vec<Coin> = pool
.assets
.iter()
.map(|pool_asset| {
Ok(Coin {
denom: pool_asset.denom.clone(),
amount: Uint128::try_from(
Decimal256::from_ratio(pool_asset.amount, Uint256::one())
.checked_mul(share_ratio)?
.to_uint_floor(),
)?,
})
})
.collect::<Result<Vec<Coin>, ContractError>>()?
.into_iter()
// filter out assets with zero amount
.filter(|coin| coin.amount > Uint128::zero())
.collect();
let mut messages: Vec<CosmosMsg> = vec![];
// Transfer the refund assets to the sender
messages.push(CosmosMsg::Bank(BankMsg::Send {
to_address: info.sender.to_string(),
amount: refund_assets.clone(),
}));
// --SNIP
}
As seen above, the function directly calculates the refund amounts and transfers them to the user without any check for slippage or allowing the user to specify a minimum acceptable amount.
Impact
Users withdrawing their liquidity can receive less amount than they expected.
Mitigations
Consider allowing users to provide minimum amount of tokens to receive.
jvr0x (MANTRA) disputed and commented:
Users, especially in xyk pools will face impermanent loss when providing liquidity into the pool. That’s why there are swap fees going to LPers, to compensate in a way for that potential loss. While having slippage protection in the withdrawal function can help, it would prevent users going out of the pool if the minimum received tokens don’t match their expectation.
Will consider it as potential improvement though. This is low, not a medium issue.
Impermanent loss is implicit, but it’s reasonable to expect a protection during high volatility - the industry standard of XYK pools (uniswap v2) does allow users to provide
amountAMinandamountBMin; they are free to set them to 0 if they want the withdrawal to always succeed.
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
The withdraw_liquidity function calculates refund amounts based solely on the current pool ratio and transfers them without any user-specified minimum or slippage check.
Impact
Users withdrawing liquidity could receive fewer tokens than they expected due to market movements, especially in ConstantProduct pools.
What the attacker needed
User withdraws liquidity from a pool
The fix
The function should be modified to allow users to provide a minimum amount of tokens to receive, as suggested in the report.
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 withdraw money from a liquidity pool, the amount you get can change between when you click 'withdraw' and when the transaction actually happens. This report says the function doesn't let you say 'I want at least this much', so you might get less than you expected. It's like ordering a item online but the price changes before it ships, and you have no way to cancel if it gets too expensive.
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.