Skip to content
Business logic Audit finding Intermediate

PerpDepository#getPositionValue uses incorrect value for TWAP interval allowing more than intended funds to be extracted

The PerpDepository contract's getPositionValue function used a 15-second TWAP interval instead of the intended 15-minute TWAP when fetching the mark price. This caused the position value to frequently differ from the true mark price, allowing larger rebalances than should be possible.

berndartmueller Follow with DecorativePineapple, cccz and 4 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-01-uxd-judging/issues/249

Found by

berndartmueller, DecorativePineapple, cccz, HonorLt, 0x52, ctf_sec, 0Kage

Summary

PerpDepository#getPositionValue queries the exchange for the mark price to calculate the unrealized PNL. Mark price is defined as the 15 minute TWAP of the market. The issue is that it uses the 15 second TWAP instead of the 15 minute TWAP

Vulnerability Detail

As stated in the docs and as implemented in the ClearHouseConfig contract, the mark price is a 15 minute / 900 second TWAP.

function getPositionValue() public view returns (uint256) {
    uint256 markPrice = getMarkPriceTwap(15);
    int256 positionSize = IAccountBalance(clearingHouse.getAccountBalance())
        .getTakerPositionSize(address(this), market);
    return markPrice.mulWadUp(_abs(positionSize));
}

function getMarkPriceTwap(uint32 twapInterval)
    public
    view
    returns (uint256)
{
    IExchange exchange = IExchange(clearingHouse.getExchange());
    uint256 markPrice = exchange
        .getSqrtMarkTwapX96(market, twapInterval)
        .formatSqrtPriceX96ToPriceX96()
        .formatX96ToX10_18();
    return markPrice;
}

As seen in the code above getPositionValue uses 15 as the TWAP interval. This means it is pulling a 15 second TWAP rather than a 15 minute TWAP as intended.

Impact

The mark price and by extension the position value will frequently be different from true mark price of the market allowing for larger rebalances than should be possible.

Code Snippet

https://github.com/sherlock-audit/2023-01-uxd/blob/main/contracts/integrations/perp/PerpDepository.sol#L708-L713

Tool used

Manual Review

Recommendation

I recommend pulling pulling the TWAP fresh each time from ClearingHouseConfig, because the TWAP can be changed at anytime. If it is desired to make it a constant then it should at least be changed from 15 to 900.

Discussion

hrishibhat

Fix: https://github.com/UXDProtocol/uxd-evm/pull/21

IAm0x52

Fix looks good. TWAP corrected from 15 to 900

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

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 passed a TWAP interval of 15 (seconds) instead of 900 (seconds) to the mark price function.

Impact

The mark price and position value could frequently differ from the true mark price, allowing larger rebalances than should be possible.

What the attacker needed

None

The fix

The TWAP interval was corrected from 15 to 900.

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 calculated the value of a trading position using a price average over 15 seconds instead of the correct 15 minutes. This made the value inaccurate, so someone could withdraw more funds than allowed.

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 berndartmueller

View all

97 more disclosed reports, best first.

More from UXD Protocol

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.