Skip to content
Business logic Audit finding Intermediate

Reallocation incorrectly sends the exceed quoteTokens to Market contract instead of reallocator

During reallocation, if the swap produces more quote tokens than required, the excess is sent to the Market contract instead of the reallocator, causing a loss of funds for the reallocator.

pkqs90 Follow

Originally disclosed on Code4rena

Submitted by pkqs90

Loss of funds (quoteToken) for reallocator if the swapped quoteTokens exceeds requirement during reallocation.

Bug Description

See the reallocation flowchart here.

The reallocator would call a Market contract, which triggers the reallocation logic in PredyPool. Then tokens would then be swapped accordingly using the SettlementCallbackLib.

For baseToken, there is a check that the diff in baseToken (before/after the swap) must be equal to what is required for reallocation. For quoteToken, if the swap generates more quoteToken than what is required (e.g. the exceedsQuote > 0 case in the following code), the quoteTokens should be transferred back to the initial reallocator, since it was the reallocator who was performing the swap.

However, the quoteTokens are transferred back to msg.sender, which is the Market contract, and would cause a loss of funds for the reallocator.

    function reallocate(GlobalDataLibrary.GlobalData storage globalData, uint256 pairId, bytes memory settlementData)
        external
        returns (bool isRangeChanged)
    {
        ...
        {
            int256 deltaPositionBase;
            int256 deltaPositionQuote;

            (relocationOccurred, isRangeChanged, deltaPositionBase, deltaPositionQuote) =
                Perp.reallocate(pairStatus, pairStatus.sqrtAssetStatus);

            if (deltaPositionBase != 0) {
                globalData.initializeLock(pairId);

                globalData.callSettlementCallback(settlementData, deltaPositionBase);

                (int256 settledQuoteAmount, int256 settledBaseAmount) = globalData.finalizeLock();

                int256 exceedsQuote = settledQuoteAmount + deltaPositionQuote;

                if (exceedsQuote < 0) {
                    revert IPredyPool.QuoteTokenNotSettled();
                }

                if (settledBaseAmount + deltaPositionBase != 0) {
                    revert IPredyPool.BaseTokenNotSettled();
                }

                if (exceedsQuote > 0) {
>                   ERC20(pairStatus.quotePool.token).safeTransfer(msg.sender, uint256(exceedsQuote));
                }
            }
            ...
        }
        ...
    }

Recommended Mitigation Steps

Send the tokens to the reallocator.

Assessed type

Token-Transfer

0xsomeone (judge) decreased severity to Low and commented:

The reallocation function is indeed invoked by the BaseMarket and such an execution path would result in funds being incorrectly sent to the Market instead of the reallocators directly. Given that reallocations can occur directly, I believe that this is better suited as a QA (Low) risk as it arises under very specific circumstances and can be circumvented. Some markets also appear to be using balance of measurements and thus are more than likely to automatically “consume” those funds instead of having them remain locked. I will invite the Sponsor to visit this submission nonetheless.

pkqs90 (warden) commented:

@0xsomeone - maybe I’m missing something important here, but to my current understanding, the reallocation function must be called to change the range for squart, and this does not happen directly during regular trade/swaps.

Also, the funds sent to the Market would be unretrievable since there is no function to do so. That’s why I placed it with a high severity issue.

syuhei176 (Predy) confirmed

0xsomeone (judge) increased severity to Medium and commented:

This report lacked Sponsor input due to being invalidated during the validation round. I explicitly requested the Sponsor to provide input to this and several other exhibits so as to provide a fine-tuned judgment, and my earlier judgment was based on validation.

@pkqs90 - I agree with what you shared; however, I believe a medium-risk rating is better appropriate as the fund loss involved in this exhibit versus #27 is significantly lower and arises under specific conditions.


The original report as published on Code4rena, by pkqs90. 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 code transfers excess quote tokens to msg.sender (the Market contract) instead of the original reallocator.

Impact

The reallocator loses the excess quote tokens, which become stuck in the Market contract.

What the attacker needed

Reallocation with excess quote tokens

The fix

Send the excess tokens to the reallocator instead of msg.sender.

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 a reallocation happens, someone (the reallocator) swaps tokens and may end up with extra quote tokens. The code sends these extra tokens to the wrong address (the Market contract) instead of back to the reallocator. This means the reallocator loses those tokens.

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 pkqs90

View all

25 more disclosed reports, best first.

More from Predy

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.