Missing slippage protection in liquidity_lockbox::withdraw
The withdraw function in liquidity_lockbox calls decreaseLiquidity with minimum token amounts hard-coded to zero, so the slippage check never triggers. An attacker could exploit this to cause the transaction to accept any amount, including zero, leading to a loss of funds.
erebus Follow with windowhan001, oakcobalt and 2 others
Originally disclosed on Code4rena
Submitted by erebus, also found by windowhan001, oakcobalt, adeolu, and thank_you
Vanilla example of missing slippage protection.
Proof of Concept
As any AMM, Orca implements slippage protections by letting the caller specify the minimum amount of input and output tokens to be used in a given operation, so that if the transaction outputs a value less than the expected ones, it will revert preventing users from incurring in unacceptable losses.
In our situation, we are interested in the decreaseLiquidity handler:
decrease_liquidity, lines 18 and 19
pub fn handler(
ctx: Context<ModifyLiquidity>,
liquidity_amount: u128,
token_min_a: u64,
token_min_b: u64,
) -> Result<()> {
...
let (delta_a, delta_b) = calculate_liquidity_token_deltas(
ctx.accounts.whirlpool.tick_current_index,
ctx.accounts.whirlpool.sqrt_price,
&ctx.accounts.position,
liquidity_delta,
)?;
if delta_a < token_min_a {
return Err(ErrorCode::TokenMinSubceeded.into());
} else if delta_b < token_min_b {
return Err(ErrorCode::TokenMinSubceeded.into());
}
...
}
The code snippet above calculates the delta variations of the two tokens amount in the given pool and reverts the whole transaction if the calculated amounts are less than the specified ones. However, if we go to:
liquidity_lockbox, function withdraw
...
whirlpool.decreaseLiquidity{accounts: metasDecreaseLiquidity, seeds: [[pdaProgramSeed, pdaBump]]}(amount, 0, 0);
...
We see that token_min_a and token_min_b are hard-coded to $0$, meaning that the liquidity decrease operation will accept ANY amount in exchange, even $0$, leading to a loss of funds as MEV exists in Solana too (see here). The mathematical reason is that:
$x >= 0, \forall x \in [0, 2⁶⁴-1]$
So the errors:
if delta_a < token_min_a {
return Err(ErrorCode::TokenMinSubceeded.into());
} else if delta_b < token_min_b {
return Err(ErrorCode::TokenMinSubceeded.into());
}
Won’t be triggered as:
if delta_a < token_min_a { // @audit delta_a < 0 => false for all delta_a, so no error
return Err(ErrorCode::TokenMinSubceeded.into());
} else if delta_b < token_min_b { // @audit delta_b < 0 => false for al delta_b, so no error
return Err(ErrorCode::TokenMinSubceeded.into());
}
// @audit if token_min_a = token_min_b = 0, then delta_a and delta_b can be ANY value,
// @audit even 0, which means total loss of funds is possible
Meaning there is no slippage protection at all if called with token_min_a = token_min_b = 0 (as it’s the case with liquidity_lockbock::withdraw).
Recommended Mitigation Steps
Let users provide the minimum amount of tokens they are willing to use as function arguments and pass them straight to decreaseLiquidity like:
- function withdraw(uint64 amount) external {
+ function withdraw(uint64 amount, uint64 minA, uint64 minB) external {
...
- whirlpool.decreaseLiquidity{accounts: metasDecreaseLiquidity, seeds: [[pdaProgramSeed, pdaBump]]}(amount, 0, 0);
+ whirlpool.decreaseLiquidity{accounts: metasDecreaseLiquidity, seeds: [[pdaProgramSeed, pdaBump]]}(amount, minA, minB);
...
}
mariapiamo (Olas) confirmed, but disagreed with severity
LSDan (Judge) decreased severity to Medium
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 function passes zero for the minimum token amounts to decreaseLiquidity, disabling the slippage protection that would otherwise revert if the received amounts are too low.
Impact
An attacker could cause a loss of funds because the transaction will accept any amount, even zero, and MEV exists on Solana.
What the attacker needed
None
The fix
The withdraw function should accept minimum token amounts as parameters and pass them to decreaseLiquidity instead of hard-coded zeros.
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 remove money from a trading pool, you expect a minimum amount back. Here, the code always asks for zero minimum, so you could get nothing back and lose your funds.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.