DeFiHardhatOracleProxyUpdates
100,000 USDC
View results
Submission Details
Severity: medium
Invalid

Division Before Multiplication Vulnerability in Sun.sol

Summary

The contract Sun.sol has instances of the "Divide before multiply" vulnerability, where division is performed before multiplication. This pattern can lead to precision loss due to Solidity's integer division truncation. The affected functions include Sun.rewardToFertilizer and Sun.setSoilAbovePeg.

Vulnerability Details

Instance 1: Sun.rewardToFertilizer (contracts/beanstalk/sun/SeasonFacet/Sun.sol#109-149)
Code Snippet:

// Sun.sol#117
newBpf = maxNewFertilized.div(s.activeFertilizer);
// Sun.sol#147
newFertilized = newFertilized.add(newBpf.mul(s.activeFertilizer));

Description: In the Sun.rewardToFertilizer function, division is performed before multiplication, leading to potential precision loss.

Instance 2: Sun.rewardToFertilizer (contracts/beanstalk/sun/SeasonFacet/Sun.sol#109-149)
Code Snippet:

// Sun.sol#117
newBpf = maxNewFertilized.div(s.activeFertilizer);
// Sun.sol#128
newFertilized = newFertilized.add(newBpf.mul(s.activeFertilizer));

Description:
Another instance in the same function where division is performed before multiplication, introducing a risk of precision loss.

Instance 3: Sun.setSoilAbovePeg (contracts/beanstalk/sun/SeasonFacet/Sun.sol#216-224)
Code Snippet:

// Sun.sol#217
newSoil = newHarvestable.mul(100).div(100 + s.w.t);
// Sun.sol#221
newSoil = newSoil.mul(SOIL_COEFFICIENT_LOW).div(C.PRECISION);

Description:
In the Sun.setSoilAbovePeg function, division is performed before multiplication, potentially leading to precision loss.

Instance 4: Sun.setSoilAbovePeg (contracts/beanstalk/sun/SeasonFacet/Sun.sol#216-224)
Code Snippet:

// Sun.sol#219
newSoil = newSoil.mul(SOIL_COEFFICIENT_HIGH).div(C.PRECISION);
// Sun.sol#221
newSoil = newSoil.mul(SOIL_COEFFICIENT_LOW).div(C.PRECISION);

Another instance in the same function where division is performed before multiplication, introducing a risk of precision loss.

Impact

Performing division before multiplication can lead to precision loss, potentially affecting the accuracy of calculations and introducing unexpected behavior in the contract. It may result in incorrect distribution of rewards or misallocation of resources.

Tools Used

Manual review and slither.

Recommendations

It's recommended to reorder the arithmetic operations to perform multiplication before division to prevent precision loss. Review and update the relevant calculations in the affected functions accordingly.

Updates

Lead Judging Commences

giovannidisiena Lead Judge about 1 year ago
Submission Judgement Published
Invalidated
Reason: Too generic

Support

FAQs

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