Calculating new rewards is susceptible to precision loss due to division before multiplication
The rewards calculation divides the staker's interest by the total interest before multiplying by the total burned, which can round down to zero when the staker's interest is small relative to the total. As a result, a staker may receive no rewards.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-01-ajna-judging/issues/121
Found by
berndartmueller
Summary
Rewards may be lost (0) due to division before multiplication precision issues.
Vulnerability Detail
The RewardsManager._calculateNewRewards function calculates the new rewards for a staker by first dividing interestEarned_ by totalInterestEarnedInPeriod and then multiplying by totalBurnedInPeriod. If interestEarned_ is small enough and totalInterestEarnedInPeriod is large enough, the division may result in a value of 0, resulting in the staker receiving 0 rewards.
Impact
Stakers may not receive rewards due to precision loss.
Code Snippet
contracts/src/RewardsManager.sol#L426-L428
408: function _calculateNewRewards(
409: address ajnaPool_,
410: uint256 interestEarned_,
411: uint256 nextEpoch_,
412: uint256 epoch_,
413: uint256 rewardsClaimedInEpoch_
414: ) internal view returns (uint256 newRewards_) {
415: (
416: ,
417: // total interest accumulated by the pool over the claim period
418: uint256 totalBurnedInPeriod,
419: // total tokens burned over the claim period
420: uint256 totalInterestEarnedInPeriod
421: ) = _getPoolAccumulators(ajnaPool_, nextEpoch_, epoch_);
422:
423: // calculate rewards earned
424: newRewards_ = Maths.wmul(
425: REWARD_FACTOR,
426: Maths.wmul(
427: Maths.wdiv(interestEarned_, totalInterestEarnedInPeriod), totalBurnedInPeriod
428: )
429: );
Tool used
Manual Review
Recommendation
Consider calculating the new rewards by first multiplying interestEarned_ by totalBurnedInPeriod and then dividing by totalInterestEarnedInPeriod to avoid precision loss.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the calculation performs division before multiplication, causing integer truncation to zero when the numerator is small.
Impact
Stakers may not receive rewards due to precision loss.
What the attacker needed
Staker has a small interest amount; total interest in period is large
The fix
The rewards should be calculated by first multiplying `interestEarned_` by `totalBurnedInPeriod` and then dividing by `totalInterestEarnedInPeriod`.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When a program does maths with whole numbers, dividing too early can throw away small amounts. Here the code divides before multiplying, so a small share can become zero and the person gets no reward. Doing the multiplication first keeps the small amount alive until the final division.