Skip to content
This repository has been archived by the owner on Jun 30, 2024. It is now read-only.

hash - Incorrect deviation check #174

Closed
sherlock-admin2 opened this issue Dec 26, 2023 · 0 comments
Closed

hash - Incorrect deviation check #174

sherlock-admin2 opened this issue Dec 26, 2023 · 0 comments
Labels
Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label Medium A valid Medium severity issue Reward A payout will be made for this issue

Comments

@sherlock-admin2
Copy link

sherlock-admin2 commented Dec 26, 2023

hash

medium

Incorrect deviation check

Summary

Deviation is calculated incorrectly allowing more than permitted manipulations of pool spot prices

Vulnerability Detail

When computing the deviations of prices from TWAP in Bunni and UniV3 Deviation.isDeviatingWithBpsCheck() always uses the highest of the two values as the denominator

    function isDeviating(
        uint256 value0_,
        uint256 value1_,
        uint256 deviationBps_,
        uint256 deviationMax_
    ) internal pure returns (bool) {
        return
            (value0_ < value1_)
                ? _isDeviating(value1_, value0_, deviationBps_, deviationMax_)
                : _isDeviating(value0_, value1_, deviationBps_, deviationMax_);
    }

    function _isDeviating(
        uint256 value0_,
        uint256 value1_,
        uint256 deviationBps_,
        uint256 deviationMax_
    ) internal pure returns (bool) {
        return ((value0_ - value1_) * deviationMax_) / value0_ > deviationBps_;
    }
    function _validateReserves(
        BunniKey memory key_,
        BunniLens lens_,
        uint16 twapMaxDeviationBps_,
        uint32 twapObservationWindow_
    ) internal view {

        .....

        if (
            // `isDeviatingWithBpsCheck()` will revert if `deviationBps` is invalid.
            Deviation.isDeviatingWithBpsCheck(
                reservesTokenRatio,
                twapTokenRatio,
                twapMaxDeviationBps_,
                TWAP_MAX_DEVIATION_BASE
            )
        ) {
            revert BunniPrice_PriceMismatch(address(key_.pool), twapTokenRatio, reservesTokenRatio);
        }
    function getTokenPrice(
        address lookupToken_,
        uint8 outputDecimals_,
        bytes calldata params_
    ) external view returns (uint256) {
        
        ......

        if (
            // `isDeviatingWithBpsCheck()` will revert if `deviationBps` is invalid.
            Deviation.isDeviatingWithBpsCheck(
                baseInQuotePrice,
                baseInQuoteTWAP,
                params.maxDeviationBps,
                DEVIATION_BASE
            )
        ) {
            revert UniswapV3_PriceMismatch(address(params.pool), baseInQuoteTWAP, baseInQuotePrice);
        }

This allows for underreporting of the deviation causing the pool to be manipulated outside of acceptable limits

Example

TWAP = 100
Spot Price = 200
maxDeviationBps = 51%

Actual deviation = (200 - 100) / 100 == 100%
Calculated deviation = (200 - 100) / 200 == 50%

Impact

Pools can be manipulated outside of expected limits

Code Snippet

deviation lib
https://github.com/sherlock-audit/2023-11-olympus/blob/main/bophades/src/libraries/Deviation.sol#L69

bunni deviation check
https://github.com/sherlock-audit/2023-11-olympus/blob/main/bophades/src/modules/PRICE/submodules/feeds/BunniPrice.sol#L255-L265

uniV3 deviation check
https://github.com/sherlock-audit/2023-11-olympus/blob/main/bophades/src/modules/PRICE/submodules/feeds/UniswapV3Price.sol#L227-L235

Tool used

Manual Review

Recommendation

Keep TWAP in the denominator

Duplicate of #193

@sherlock-admin sherlock-admin changed the title Massive Grey Barracuda - Incorrect StablePool BPT price calculation Massive Grey Barracuda - Incorrect deviation check Dec 28, 2023
@github-actions github-actions bot added High A valid High severity issue Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label labels Dec 30, 2023
@sherlock-admin2 sherlock-admin2 changed the title Massive Grey Barracuda - Incorrect deviation check hash - Incorrect deviation check Jan 8, 2024
@sherlock-admin2 sherlock-admin2 added the Reward A payout will be made for this issue label Jan 8, 2024
@Czar102 Czar102 added Medium A valid Medium severity issue and removed High A valid High severity issue labels Jan 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label Medium A valid Medium severity issue Reward A payout will be made for this issue
Projects
None yet
Development

No branches or pull requests

2 participants