sherlock-audit / 2024-08-flayer-judging

2 stars 0 forks source link

rcadob - Unchecked arithmetic operations will lead to incorrect calculations for users #751

Open sherlock-admin4 opened 1 month ago

sherlock-admin4 commented 1 month ago

rcadob

High

Unchecked arithmetic operations will lead to incorrect calculations for users

Summary

In multiple files (TaxCalculator.sol, LinearRangeCurve.sol, Listings.sol, ProtectedListings.sol, UniswapImplementation.sol), unchecked arithmetic operations will lead to incorrect calculations for users as underflows and overflows may occur without proper validation.

Root Cause

Arithmetic operations are performed without ensuring values remain within data type bounds:

Internal pre-conditions

  1. Underflow Conditions:

    • TaxCalculator.sol Line 69:
      • _utilizationRate is less than UTILIZATION_KINK.
    • LinearRangeCurve.sol Line 60:
      • end is less than start.
    • Listings.sol Line 933:
      • block.timestamp exceeds _listing.created + _listing.duration.
  2. Overflow Conditions:

    • TaxCalculator.sol Line 90:
      • _previousCompoundedFactor, perSecondRate, or _timePeriod are large enough to cause overflow.
    • ProtectedListings.sol Line 273:
      • listingsOfType_ and collectionToken.denomination() are large.
    • UniswapImplementation.sol Line 607:
      • swapAmount and ammFee are large values close to their maximum limits.

External pre-conditions

N/A.

Attack Path

  1. Users invoke functions with vulnerable arithmetic operations:

    • For example, functions that calculate interest rates, utilization rates, or fees.
  2. Underflow or overflow occurs during calculations:

    • Arithmetic operations exceed the data type limits.
  3. Incorrect values are computed, or transactions revert:

    • Users receive wrong amounts or experience transaction failures.

Impact

The users suffer incorrect calculations, which can lead to financial losses or contract instability due to underflow and overflow errors in arithmetic operations.

PoC

Underflow in TaxCalculator.sol Line 69:

uint256 UTILIZATION_KINK = 0.8 ether;
uint256 _utilizationRate = 0.5 ether; // Less than UTILIZATION_KINK

// Underflow occurs here
uint256 interestRate_ = (((_utilizationRate - UTILIZATION_KINK) * (100 - 8)) / (1 ether - UTILIZATION_KINK) + 8) * 100;
// _utilizationRate - UTILIZATION_KINK underflows

Overflow in UniswapImplementation.sol Line 607:

uint128 swapAmount = type(uint128).max; // Maximum uint128 value
uint256 ammFee = 100000; // Large fee

// Potential overflow during multiplication
uint256 feeAmount = uint128(swapAmount) * ammFee / 100_000;
// Multiplication overflows, resulting in incorrect feeAmount

Overflow in UniswapImplementation.sol Line 607:

uint128 swapAmount = type(uint128).max; // Maximum uint128 value
uint256 ammFee = 100000; // Large fee

// Potential overflow during multiplication
uint256 feeAmount = uint128(swapAmount) * ammFee / 100_000;
// Multiplication overflows, resulting in incorrect feeAmount

Mitigation