Wrong invocation of Whirpools’s updateFeesAndRewards will cause it to always revert
The withdraw function in the lockbox contract calls updateFeesAndRewards on the Orca Whirlpool after the position's liquidity has been fully removed, but that call always reverts when the position's liquidity is zero. As a result, the final withdrawal that would empty a position fails, making deposits unwithdrawable.
Submitted by hash
Deposits will be unwithdrawable from the lockbox.
Proof of Concept
If the entire liquidity of a position has been removed, the withdraw function calls the updateFeesAndRewards function on the Orca pool before attempting to close the position.
function withdraw(uint64 amount) external {
address positionAddress = positionAccounts[firstAvailablePositionAccountIndex];
......
uint64 positionLiquidity = mapPositionAccountLiquidity[positionAddress];
......
uint64 remainder = positionLiquidity - amount;
......
if (remainder == 0) {
// Update fees for the position
AccountMeta[4] metasUpdateFees = [
AccountMeta({pubkey: pool, is_writable: true, is_signer: false}),
AccountMeta({pubkey: positionAddress, is_writable: true, is_signer: false}),
AccountMeta({pubkey: tx.accounts.tickArrayLower.key, is_writable: false, is_signer: false}),
AccountMeta({pubkey: tx.accounts.tickArrayUpper.key, is_writable: false, is_signer: false})
];
whirlpool.updateFeesAndRewards{accounts: metasUpdateFees, seeds: [[pdaProgramSeed, pdaBump]]}();
This is faulty as the updateFeesAndRewards function will always revert if the position’s liquidity is 0.
Whirlpool source code:
updatefeesand_rewards -> calculatefeeandrewardgrowths -> _calculatemodifyliquidity
fn _calculate_modify_liquidity(
whirlpool: &Whirlpool,
position: &Position,
tick_lower: &Tick,
tick_upper: &Tick,
tick_lower_index: i32,
tick_upper_index: i32,
liquidity_delta: i128,
timestamp: u64,
) -> Result<ModifyLiquidityUpdate> {
// Disallow only updating position fee and reward growth when position has zero liquidity
if liquidity_delta == 0 && position.liquidity == 0 {
return Err(ErrorCode::LiquidityZero.into());
}
Since the withdrawal positions are chosen sequentially, only a maximum of (first position’s liquidity - 1) amount of liquidity can be withdrawn.
POC Test
https://gist.github.com/10xhash/a687ef66de8210444a41360b86ed4bca
Recommended Mitigation Steps
Avoid the update_fees_and_rewards call completely since fees and rewards would be updated in the decreaseLiquidity call.
We have changed the order of operations in our rust program indeed, and it works there. Verified the order of instructions provided here and it fails if the remainder is zero.
Note: See full discussion here.
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 contract calls updateFeesAndRewards after reducing the position's liquidity to zero, but the Whirlpool program rejects that call when liquidity is zero.
Impact
Deposits cannot be fully withdrawn; at most the first position's liquidity minus one can be withdrawn, leaving the remainder stuck.
What the attacker needed
A position with liquidity in the lockbox; withdrawing the full liquidity of a position
The fix
The updateFeesAndRewards call should be removed because fees and rewards are already updated in the decreaseLiquidity call.
How it was found
- Deposit liquidity into the lockbox so a position exists.
- Attempt to withdraw the entire liquidity of the first position.
- The withdraw function reduces the position's liquidity to zero and then calls updateFeesAndRewards.
- The Whirlpool program reverts because the position's liquidity is zero, so the withdrawal fails.
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 smart contract lets people deposit funds into a pool and later withdraw them. When someone tries to withdraw the last bit, the contract asks the pool to update some internal accounting, but the pool refuses because the position is now empty. That refusal blocks the withdrawal, so the last portion of funds gets stuck.
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.