code-423n4 / 2024-07-reserve-validation

0 stars 0 forks source link

Overflow in Fixed.sol::mul() Function #172

Closed c4-bot-4 closed 1 month ago

c4-bot-4 commented 1 month ago

Lines of code

https://github.com/code-423n4/2024-07-reserve/blob/3f133997e186465f4904553b0f8e86ecb7bbacbf/contracts/libraries/Fixed.sol#L252

Vulnerability details

Summary

The mul() function is designed to perform multiplication on uint256 values. However, it lacks appropriate overflow checks, which makes it susceptible to arithmetic overflow. This occurs when the product of two large numbers exceeds the maximum value that can be stored in a uint256 variable (2^256 - 1). When this happens, the function may produce incorrect results by wrapping around the value or causing the transaction to revert unexpectedly

Impact

The overflow vulnerability in the mul() function poses a severe risk to the contract's integrity and security. Depending on the function's role within the contract, this could result in:

Loss of Funds: Incorrect calculations due to overflow can lead to incorrect transfers, allocations, or balances. Transaction Reverts: If overflow occurs, it may cause the transaction to revert, potentially leading to a denial of service for users. Systemic Failure: In critical financial or operational contracts, overflow can lead to systemic failures, making the contract unusable Given that the mul() function is likely used throughout the contract, the risk of overflow has widespread implications and could compromise the entire system.

Reproduction Steps:

1.To confirm the overflow vulnerability, you can perform the following steps:

2.Deploy the Contract: Deploy the smart contract containing the mul() function to a test environment.

3.Test with Large Values: Invoke the mul() function with large input values close to the maximum uint256 limit (e.g., 2^128 * 2^128)

4.Observe the Result: If the function returns an incorrect product (due to wrapping around), the overflow is confirmed. If the transaction reverts without any clear reason, this also indicates an overflow issue

uint256 a = 2128; uint256 b = 2128; uint256 result = mul(a, b); // Expected: Overflow occurs, leading to incorrect result or reversion.

Tools Used

Manual Review

Recommended Mitigation

To mitigate the risk of overflow in the mul() function, it is recommended to implement the following solutions:

Use SafeMath Library: Integrate the SafeMath library from OpenZeppelin, which provides safe arithmetic operations, including multiplication with overflow checks.

using SafeMath for uint256; uint256 result = a.mul(b);

->Upgrade to Solidity 0.8.0 or Higher: Consider upgrading the contract to Solidity version 0.8.0 or higher, where arithmetic overflow checks are built into the language by default. This would eliminate the need for external libraries

Assessed type

Under/Overflow