Skip to content
Audit finding Intermediate

_updateBucketExchangeRateAndCalculateRewards reward calculation accuracy loss

The reward calculation in _updateBucketExchangeRateAndCalculateRewards divides before multiplying, causing precision loss. An attacker could exploit this to receive slightly fewer rewards, with the loss proportional to the interest earned.

kutugu Follow

Originally disclosed on Code4rena

Submitted by kutugu

Divide first and then multiply, which has precision loss. The loss depends on the denominator, which is at most denominator - 1. Although Maths.wdiv rounded adopted in _updateBucketExchangeRateAndCalculateRewards, reduce the loss, but theoretically interestFactor loss is about interestEarned_ / 2. This means that the more interest are earned, the more users lose.

Proof of Concept

Modify for testing

diff --git a/ajna-core/src/RewardsManager.sol b/ajna-core/src/RewardsManager.sol
index 314b476..421940f 100644
--- a/ajna-core/src/RewardsManager.sol
+++ b/ajna-core/src/RewardsManager.sol
@@ -801,8 +801,12 @@ contract RewardsManager is IRewardsManager, ReentrancyGuard {
                 rewards_ += Maths.wmul(UPDATE_CLAIM_REWARD, Maths.wmul(burnFactor, interestFactor));
             }
         }
+
+        emit CalculateReward(rewards_);
     }

+    event CalculateReward(uint256);
+
     /** @notice Utility method to transfer `Ajna` rewards to the sender
      *  @dev   This method is used to transfer rewards to the `msg.sender` after a successful claim or update.
      *  @dev   It is used to ensure that rewards claimers will be able to claim some portion of the remaining tokens if a claim would exceed the remaining contract balance.

Modify reward calculation

diff --git a/ajna-core/src/RewardsManager.sol b/ajna-core/src/RewardsManager.sol
index 421940f..4cdfefa 100644
--- a/ajna-core/src/RewardsManager.sol
+++ b/ajna-core/src/RewardsManager.sol
@@ -792,13 +792,15 @@ contract RewardsManager is IRewardsManager, ReentrancyGuard {
                 (, , , uint256 bucketDeposit, ) = IPool(pool_).bucketInfo(bucketIndex_);

                 uint256 burnFactor     = Maths.wmul(totalBurned_, bucketDeposit);
-                uint256 interestFactor = interestEarned_ == 0 ? 0 : Maths.wdiv(
-                    Maths.WAD - Maths.wdiv(prevBucketExchangeRate, curBucketExchangeRate),
-                    interestEarned_
-                );

                 // calculate rewards earned for updating bucket exchange rate
-                rewards_ += Maths.wmul(UPDATE_CLAIM_REWARD, Maths.wmul(burnFactor, interestFactor));
+                rewards_ += interestEarned_ == 0 ? 0 : Maths.wmul(UPDATE_CLAIM_REWARD, Maths.wdiv(
+                    Maths.wmul(
+                        Maths.WAD - Maths.wdiv(prevBucketExchangeRate, curBucketExchangeRate),
+                        burnFactor
+                    ),
+                    interestEarned_
+                ));
             }
         }
forge test --match-test testClaimRewardsFuzzy -vvvv

For a Fuzzing input:
indexes: 3
mintAmount: 73528480588506366763626

Divide first and multiply
emit CalculateReward(: 334143554965844407584)

Multiply first and divide
emit CalculateReward(: 334143554965846586903)

Tools Used

Foundry

Recommended Mitigation Steps

As modified above, multiply first and then divide.

MikeHathaway (Ajna) confirmed


The original report as published on Code4rena, by kutugu. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 code performs division before multiplication in the reward formula, leading to truncation.

Impact

The precision loss could cause users to receive slightly fewer rewards, with the loss increasing with the amount of interest earned.

What the attacker needed

None

The fix

The code should be modified to multiply before dividing to minimise precision loss.

How it was found

  1. Modify the contract to emit the calculated reward for testing.
  2. Run the fuzzy test with specific inputs.
  3. Compare the reward when dividing first versus multiplying first.

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 decimals, the order of operations matters. Dividing first and then multiplying can lose tiny amounts due to rounding. Here, that means users might get slightly less reward than they should.

More by kutugu

View all

8 more disclosed reports, best first.

More from Ajna Protocol

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.