sherlock-audit / 2023-04-ajna-judging

4 stars 3 forks source link

Oxhunter526 - Title: Precision Loss Due to Floating-Point Arithmetic in Loan Calculation #64

Closed sherlock-admin closed 1 year ago

sherlock-admin commented 1 year ago

Oxhunter526

high

Title: Precision Loss Due to Floating-Point Arithmetic in Loan Calculation

Summary

The Solidity code snippet provided contains a calculation that involves floating-point arithmetic, which can lead to precision loss. This issue may impact the accuracy of the loan calculation and potentially introduce unexpected results.

Vulnerability Detail

The vulnerability arises from the use of fixed-point decimal values and floating-point arithmetic in the loan calculation. Specifically, the expression (1e18 + poolRate) curMomp t0ThresholdPrice / lup / 1e18 can lead to precision loss due to the following factors:

  1. Addition: Adding poolRate to 1e18 may introduce precision loss if poolRate has a fractional component.
  2. Multiplication: Multiplying curMomp and t0ThresholdPrice can result in additional decimal places and potential precision loss if either value has a fractional component.
  3. Division: Dividing the result of the multiplication by lup may introduce further precision loss if lup has a fractional component.
  4. Scaling: The final division by 1e18 is intended to adjust the decimal places. However, if there were already precision loss during previous operations, dividing by 1e18 may not fully recover the original precision.

    Impact

    The precision loss can lead to inaccurate loan calculations and potentially affect the overall functionality of the system. It may result in incorrect loan thresholds, inaccurate interest rate calculations, or incorrect borrower balances. These inaccuracies can lead to financial losses or unexpected behavior within the loan management system.

    Code Snippet

    Link

    Proof of Concept (PoC):

    A PoC can be provided by simulating scenarios where the loan calculation involves fractional components or numbers with high precision. By analyzing the outputs of the calculation, it can demonstrate the potential precision loss and its impact on the loan management process.

Tool used

Manual Review

Recommendation

  1. Avoid floating-point arithmetic: Instead of using floating-point arithmetic, consider using fixed-point arithmetic or integer-based calculations to maintain precision.
  2. Choose appropriate scaling factors: Ensure that the scaling factors used in the calculation are sufficient to represent the desired precision without causing overflow or loss of significant digits.