Skip to content
This repository has been archived by the owner on Dec 17, 2023. It is now read-only.

ShadowForce - division before multiplication may result in truncation of result #299

Closed
sherlock-admin opened this issue Jun 14, 2023 · 7 comments
Labels
Disagree With Severity The sponsor disputed the severity of this issue Low/Info A valid Low/Informational severity issue Non-Reward This issue will not receive a payout Sponsor Confirmed The sponsor acknowledged this issue is valid Will Fix The sponsor confirmed this issue will be fixed

Comments

@sherlock-admin
Copy link
Contributor

ShadowForce

high

division before multiplication may result in truncation of result

Summary

division before multiplication may result in truncation of result

Vulnerability Detail

 function _calculateMinRepayUnits(uint256 _collateralRebalanceUnits, uint256 _slippageTolerance, ActionInfo memory _actionInfo) internal pure returns (uint256) {
        // @audit
        // division before manipulation?
        return _collateralRebalanceUnits
            .preciseMul(_actionInfo.collateralPrice)
            .preciseDiv(_actionInfo.borrowPrice)
            .preciseMul(PreciseUnitMath.preciseUnit().sub(_slippageTolerance));
    }

in the snippet above we can see there is division before multiplication. As we all know doing this can result in truncation.
funds may be lost (0) due to division before multiplication precision issues

for example:
if _actionInfo.borrowPrice is less than 1, it will be truncated to 0. any further multiplication with this number will also result in 0.
we observe multiplication after this point in this specific place.

 .preciseMul(_actionInfo.collateralPrice)
            .preciseDiv(_actionInfo.borrowPrice)
            .preciseMul(PreciseUnitMath.preciseUnit().sub(_slippageTolerance));

all multiplacation must first be done before we decide to divide.

Impact

funds may be lost (0) due to division before multiplication precision issues

Code Snippet

https://github.com/IndexCoop/index-coop-smart-contracts/blob/317dfb677e9738fc990cf69d198358065e8cb595/contracts/adapters/AaveLeverageStrategyExtension.sol#L1147-L1152

Tool used

Manual Review

Recommendation

multiply first then divide to avoid precision loss

@ckoopmann
Copy link

While technically correct the precision loss would be limited to 3/4 decimals, and will not have a material impact when applied to the "minAmountOut" parameter.

We might adjust the order anyway, but will review first internally.

Not sure about "medium" severity, since this does not seem to have a material impact on the user experience / protocol safety.

@ckoopmann ckoopmann added Sponsor Confirmed The sponsor acknowledged this issue is valid Disagree With Severity The sponsor disputed the severity of this issue labels Jun 20, 2023
@pblivin0x
Copy link

The proposed fix to flip the order of operations here looks reasonable to me 👍

@0xffff11
Copy link
Collaborator

I believe sherlock does mark this issue as a med in most cases because there is a small impact in the calculation of funds

@0xffff11
Copy link
Collaborator

There are several instances on the code where this happens, all of them market as duplicates. I believe that a small truncation where precission would be needed due to calculating any type of repayment is a medium

@ckoopmann ckoopmann added the Will Fix The sponsor confirmed this issue will be fixed label Jun 25, 2023
@hrishibhat
Copy link

While technically correct the precision loss would be limited to 3/4 decimals, and will not have a material impact when applied to the "minAmountOut" parameter.

Considering this issue as a low

@sherlock-admin sherlock-admin added Low/Info A valid Low/Informational severity issue Non-Reward This issue will not receive a payout and removed Medium A valid Medium severity issue Has Duplicates A valid issue with 1+ other issues describing the same vulnerability labels Jun 27, 2023
@ckoopmann
Copy link

@IAm0x52
Copy link
Collaborator

IAm0x52 commented Aug 1, 2023

Fix looks good. Instances of div before mul have been swapped

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Disagree With Severity The sponsor disputed the severity of this issue Low/Info A valid Low/Informational severity issue Non-Reward This issue will not receive a payout Sponsor Confirmed The sponsor acknowledged this issue is valid Will Fix The sponsor confirmed this issue will be fixed
Projects
None yet
Development

No branches or pull requests

6 participants