code-423n4 / 2023-05-venus-findings

2 stars 1 forks source link

Incorrectly calculation of the total tokens to be seized because of the difference on the scale of magnitude for the prices of the underlying assets #551

Closed code423n4 closed 1 year ago

code423n4 commented 1 year ago

Lines of code

https://github.com/code-423n4/2023-05-venus/blob/main/contracts/Comptroller.sol#L1084-L1112

Vulnerability details

Impact

The total number of tokens to be seized could be wrongly calculated if the underlying assets of vTokenBorrowed & vTokenCollateral have a different decimals.

Proof of Concept

The price returned by the ChainlinkOracle contract of the Venus Protocol, the price is returned in the scale of 10 ^ (36 - underlying asset decimals))

Example

    WBNB (18 decimals in BSC).  USD price: $300.    Value returned by the Oracles:  300^1e18
    TRX (6 decimals in BSC).    USD price: $0.068.  Value returned by the Oracles:  0.068^1e30

Tools Used

Recommended Mitigation Steps

The recommendation to prevent this issue is to normalize the amount of the two underlying assets before operating between the two, that means, when calculating the numerator and denominator, make sure to divide by 1e18 so the two values are now scaled by the same magnitude.

Assessed type

Math

0xean commented 1 year ago

similar to #468 but in a different part of the codebase.

c4-sponsor commented 1 year ago

chechu marked the issue as sponsor disputed

chechu commented 1 year ago

The maths seems correct. It should be taken into account that exchangeRate has a different number of decimals depending on the underlying decimals. Example:

—- the pen & paper numbers:

—- now applying the formulas coded in the contract, numerator: 300 1.1 1e18 denominator: 0.068 1e28 ratio: _div(300 1.1 1e18 , 0.068 1e28) = (300 1.1 1e8) / 0.068 seizedTokens: mul_ScalarTruncate((300 1.1 1e8) / 0.068 , 105 1e18) = 509,558 1e8 vTRX

given the exchange rate (1e16), and the formula underlying = mul_ScalarTruncate(exchangeRate, vTokens), 509,558 1e8 vTRX are 509,558 1e6 TRX (atoms). The same amount calculated with pen & paper

c4-judge commented 1 year ago

0xean marked the issue as unsatisfactory: Invalid