Skip to content
Business logic Audit finding Intermediate

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.

hash Follow

Originally disclosed on Code4rena

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.

https://github.com/code-423n4/2023-12-autonolas/blob/2a095eb1f8359be349d23af67089795fb0be4ed1/lockbox-solana/solidity/liquidity_lockbox.sol#L277-L293

    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.

https://github.com/code-423n4/2023-12-autonolas/blob/2a095eb1f8359be349d23af67089795fb0be4ed1/lockbox-solana/solidity/interfaces/whirlpool.sol#L198

Whirlpool source code:

updatefeesand_rewards -> calculatefeeandrewardgrowths -> _calculatemodifyliquidity

https://github.com/orca-so/whirlpools/blob/3206c9cdfbf27c73c30cbcf5b6df2929cbf87618/programs/whirlpool/src/manager/liquidity_manager.rs#L97-L99

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.

kupermind commented:

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.


The original report as published on Code4rena, by hash. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

  1. Deposit liquidity into the lockbox so a position exists.
  2. Attempt to withdraw the entire liquidity of the first position.
  3. The withdraw function reduces the position's liquidity to zero and then calls updateFeesAndRewards.
  4. 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.

More by hash

View all

27 more disclosed reports, best first.

More from Olas

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.