Part 2

Zaros
PerpetualsDEXFoundrySolidity
70,000 USDC
View results
Submission Details
Severity: medium
Invalid

Potential Integer Overflow/Underflow

Summary

https://github.com/Cyfrin/2025-01-zaros-part-2/blob/39e33b2f6b3890573bb1affc41a7e520277ceb2c/src/market-making/leaves/Distribution.sol#L28

The contract performs arithmetic operations on unsigned and signed integers without explicitly handling overflow or underflow conditions. In the absence of proper checks or usage of libraries like SafeMath (or SafeCast for the specific data types used), this can result in unintended behavior such as incorrect calculations, contract failure, or manipulation of values, which can be exploited maliciously.

Vulnerability Details

distributeValue(Data storage self, SD59x18 value):

This function divides and adds values based on the total number of shares and the distributed value. If totalShares or value is extremely large, it could overflow during the division or addition operations.

setActorShares(Data storage self, bytes32 actorId, UD60x18 newActorShares):

This function modifies the total number of shares and updates actor shares. The addition and subtraction of shares are potential candidates for overflow or underflow.

_updateLastValuePerShare(Data storage self, Actor storage actor, UD60x18 newActorShares):

Similar to setActorShares, any arithmetic operation here could potentially overflow, particularly when large shares are set.

Root Cause

The contract performs arithmetic without explicit overflow and underflow checks, particularly when modifying totalShares or shares values. The arithmetic operations rely on casting, which may cause overflows or underflows when values exceed their respective bounds.

Impact

Data Corruption: If an overflow or underflow occurs, it could result in incorrect share calculations, leading to data corruption.

Loss of Funds: Actors may receive more or less than their fair share of the distributed value, causing financial discrepancies.

Contract Failure: If an overflow occurs during operations, it could cause the contract to fail or behave unexpectedly.

Security Exploits: Malicious users could exploit these vulnerabilities by crafting large values to manipulate or disrupt the intended distribution mechanism.

Tools Used

Mythril

Recommendations

Use SafeMath: To prevent overflow and underflow, use SafeMath or SafeCast libraries that automatically revert on arithmetic overflows and underflows.

For unsigned integers (like uint128, uint256), the OpenZeppelin SafeMath library should be used.

For signed integers (like int256), the SafeCast library from OpenZeppelin can be used for safe casting.

Explicit Overflow Checks: Ensure that every arithmetic operation (addition, subtraction, multiplication, division) includes overflow/underflow checks.

Updates

Lead Judging Commences

inallhonesty Lead Judge 4 months ago
Submission Judgement Published
Invalidated
Reason: Too generic

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.