Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

position.update fees operations must be in unchecked #69

Closed
howlbot-integration bot opened this issue Sep 16, 2024 · 4 comments
Closed

position.update fees operations must be in unchecked #69

howlbot-integration bot opened this issue Sep 16, 2024 · 4 comments
Labels
3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working duplicate-143 🤖_54_group AI based duplicate group recommendation satisfactory satisfies C4 submission criteria; eligible for awards sufficient quality report This report is of sufficient quality upgraded by judge Original issue severity upgraded from QA/Gas by judge

Comments

@howlbot-integration
Copy link

Lines of code

https://github.com/code-423n4/2024-08-superposition/blob/4528c9d2dbe1550d2660dac903a8246076044905/pkg/seawater/src/position.rs#L43-L66

Vulnerability details

Impact

overflow/underflow when updating the position fees is prevented, which may give wrong values.

Proof of Concept

When the position is updated, owed_fees are updated, but all operations (add and subtract) use functions that prevent overflow/underflow (eg checked_sub, wrapping_add).

position.rs#L43-L66

pub fn update(
    &mut self,
    id: U256,
    delta: i128,
    fee_growth_inside_0: U256,
    fee_growth_inside_1: U256,
) -> Result<(), Error> {
    let mut info = self.positions.setter(id);

    let owed_fees_0 = full_math::mul_div(
        fee_growth_inside_0
            .checked_sub(info.fee_growth_inside_0.get())
            .ok_or(Error::FeeGrowthSubPos)?,
        U256::from(info.liquidity.get()),
        full_math::Q128,
    )?;

    let owed_fees_1 = full_math::mul_div(
        fee_growth_inside_1
            .checked_sub(info.fee_growth_inside_1.get())
            .ok_or(Error::FeeGrowthSubPos)?,
        U256::from(info.liquidity.get()),
        full_math::Q128,
    )?;

https://docs.rs/alloy-primitives/0.7.6/alloy_primitives/aliases/type.U256.html#method.checked_sub

/// Computes `self - rhs`, returning [`None`] if overflow occurred.
#[inline(always)]
#[must_use]
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
    match self.overflowing_sub(rhs) {
        (value, false) => Some(value),
        _ => None,
    }
}

But this is not the case, how this is done in the Uniswap’s Position library, there all these operations are in an unchecked block, since overflow and underflow should not prevent these operations and are expected.

https://github.com/Uniswap/v3-core/blob/6562c52e8f75f0c10f9deaf44861847585fc8129/contracts/libraries/Position.sol#L66-L91

function update(
    Info storage self,
    int128 liquidityDelta,
    uint256 feeGrowthInside0X128,
    uint256 feeGrowthInside1X128
) internal {
    Info memory _self = self;

    uint128 liquidityNext;
    if (liquidityDelta == 0) {
        if (_self.liquidity <= 0) revert NP(); // disallow pokes for 0 liquidity positions
        liquidityNext = _self.liquidity;
    } else {
        liquidityNext = liquidityDelta < 0
            ? _self.liquidity - uint128(-liquidityDelta)
            : _self.liquidity + uint128(liquidityDelta);
    }

    // calculate accumulated fees. overflow in the subtraction of fee growth is expected
    uint128 tokensOwed0;
    uint128 tokensOwed1;
    unchecked {
        tokensOwed0 = uint128(
            FullMath.mulDiv(
                feeGrowthInside0X128 - _self.feeGrowthInside0LastX128,
                _self.liquidity,
                FixedPoint128.Q128
            )
        );
        tokensOwed1 = uint128(
            FullMath.mulDiv(
                feeGrowthInside1X128 - _self.feeGrowthInside1LastX128,
                _self.liquidity,
                FixedPoint128.Q128
            )
        );

        // update the position
        if (liquidityDelta != 0) self.liquidity = liquidityNext;
        self.feeGrowthInside0LastX128 = feeGrowthInside0X128;
        self.feeGrowthInside1LastX128 = feeGrowthInside1X128;
        if (tokensOwed0 > 0 || tokensOwed1 > 0) {
            // overflow is acceptable, user must withdraw before they hit type(uint128).max fees
            self.tokensOwed0 += tokensOwed0;
            self.tokensOwed1 += tokensOwed1;
        }
    }
}

NOTE: Furthermore, one of the Main Invariants in the README mentioned that - Superposition should follow the UniswapV3 math faithfully, which is not the case here, violating the invariant.
https://github.com/code-423n4/2024-08-superposition?tab=readme-ov-file#main-invariants

Tools Used

Manual Review

Recommended Mitigation Steps

Move all these operations in unsafe/unchecked block.

Assessed type

Math

@howlbot-integration howlbot-integration bot added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value 🤖_54_group AI based duplicate group recommendation bug Something isn't working duplicate-46 sufficient quality report This report is of sufficient quality labels Sep 16, 2024
howlbot-integration bot added a commit that referenced this issue Sep 16, 2024
@c4-judge
Copy link
Contributor

alex-ppg marked the issue as not a duplicate

@c4-judge
Copy link
Contributor

alex-ppg marked the issue as duplicate of #143

@c4-judge c4-judge added duplicate-143 3 (High Risk) Assets can be stolen/lost/compromised directly upgraded by judge Original issue severity upgraded from QA/Gas by judge and removed 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value labels Sep 23, 2024
@c4-judge
Copy link
Contributor

alex-ppg changed the severity to 3 (High Risk)

@c4-judge
Copy link
Contributor

alex-ppg marked the issue as satisfactory

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working duplicate-143 🤖_54_group AI based duplicate group recommendation satisfactory satisfies C4 submission criteria; eligible for awards sufficient quality report This report is of sufficient quality upgraded by judge Original issue severity upgraded from QA/Gas by judge
Projects
None yet
Development

No branches or pull requests

1 participant