Skip to content
Business logic Audit finding Advanced

Discrepancy between the lastRewadTime and the lastAllPoolUpdate can allow for incorrect reward distribution to pools if registerRewardDeposit deposits less assets

The reward accounting sets the global last-update timestamp to the current block time even when individual pools were only advanced to an earlier reward end time, so the two timestamps drift apart. When a small reward deposit is registered after the end time has passed, some pools can later accrue more rewards than intended while others accrue less.

hash Follow with Rhaydden, seaona and 3 others

Originally disclosed on Code4rena

Submitted by hash, also found by Rhaydden, seaona, lanrebayode77, lian886, and novamanbg

Incorrect reward distribution causing some pools to gain more while others to gain less.

Proof of Concept

The _massUpdatePools function always sets lastAllPoolUpdate = block.timestamp:

    function _massUpdatePools() internal {
        uint256 totalAP = totalAllocPoint;
        uint256 length = poolLength();
        for (uint256 i; i < length; ) {
            _updatePool(vaultInfo[registeredTokens[i]], totalAP);
            unchecked {
                i++;
            }
        }
        lastAllPoolUpdate = block.timestamp;
    }

But the individualPool update timestamp can become lower than block.timestamp if it surpasses the endRewardTime; i.e., the time in which the entire assets deposited is supposed to be depleted.

    function _updatePool(VaultInfo storage pool, uint256 _totalAllocPoint) internal {
        uint256 timestamp = block.timestamp;
        uint256 endReward = endRewardTime();
        if (endReward <= timestamp) {
            timestamp = endReward;
        }
        if (timestamp <= pool.lastRewardTime) {
            return;
        }

        (uint256 reward, uint256 newAccRewardPerShare) = _newRewards(pool, _totalAllocPoint);
        accountedRewards = accountedRewards + reward;
        pool.accRewardPerShare = pool.accRewardPerShare + newAccRewardPerShare;
        pool.lastRewardTime = timestamp;

Furthermore, the endRewardTime is calculated as newEndTime = (unclaimedRewards + extra) / rewardsPerSecond + lastAllPoolUpdate whenever the rewardsPerSecond is non-zero:

    function endRewardTime() public returns (uint256) {

        .....

        if (rewardsPerSecond == 0) {
            endingTime.estimatedTime = type(uint256).max;
            return type(uint256).max;
        } else {
            uint256 newEndTime = (unclaimedRewards + extra) / rewardsPerSecond + lastAllPoolUpdate;
            endingTime.estimatedTime = newEndTime;
            return newEndTime;
        }

If a _massUpdatePools call occurs when block.timestamp is greater than the endRewardTime, it will set the lastRewardTime of pools to be less than lastAllPoolUpdate; following which even if there are no more rewards, the new endRewardTime would be the timestamp of lastAllPoolUpdate. This will allow a pool to claim rewards worth (lastAllPoolUpdate - initialEndTime) * rewardPerSecond which is not an expected behaviour and not handled with the endRewardTime constant.

The above scenario can occur if the registerRewardDeposit function is invoked with a low amount of deposits (i.e., the deposited amount shouldn’t cause the new end time to be >= block.timestamp) when the endRewardTime has been surpassed.

Example:

  • pool A and B, 1:1 rewardRatio
  • rewardsPerSecond = 1
  • endRewardTime = 100
  • block.timestamp = 110

registerRewardDeposit is invoked such that the new endRewardTime (i.e., even after including the newly deposited assets) is 105.

Now, the first massUpdatePools call will result in:

  • lastRewardTime = 105 (endRewardTime)
  • lastAllPoolUpdate = 110

The entire rewards of the contract are used up, but when massUpdatePools gets invoked again (e.g., via claim -> _updateEmissions -> _massUpdatePools), the new endTime will be 110 (i.e., lastPoolUpdate + 0).

claim is called for pool A in the same block, and A’s lastRewardTime gets updated to 110 while B’s remain 105. At 120 registerRewardDeposit is invoked with a lot of assets and B will accrue a reward of (120 - 105)/2 while A will only accrue (120 - 110)/2.

Recommended Mitigation Steps

In case endTime > block.timestamp, can set the lastPoolUpdate to endTime or always ensure that the registerRewardDeposit function will only be called with amounts such that the above issue doesn’t occur.

Assessed type

Context

amarcu (LoopFi) confirmed


The original report as published on Code4rena, by hash. 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 global lastAllPoolUpdate is set to block.timestamp while each pool's lastRewardTime is capped at endRewardTime, so the two diverge and the next endRewardTime is computed from the later global value.

Impact

Rewards could be distributed incorrectly, with some pools gaining more and others less than they should.

What the attacker needed

RewardsPerSecond is non-zero; reward end time already passed; small reward deposit registered

The fix

The global last-update timestamp should be set to the reward end time when the end time is in the past, or deposits should be restricted so the new end time never falls behind the current block.

How it was found

  1. Have two pools with equal reward allocation and a reward end time already in the past relative to the current block.
  2. Call registerRewardDeposit with a small amount so the new end time is still below the current block timestamp.
  3. Trigger a mass pool update, which sets each pool's last reward time to the end time but the global timestamp to the current block.
  4. Trigger another mass update so the new end time becomes the global timestamp.
  5. Claim from one pool in the same block so its last reward time moves ahead of the other pool's.
  6. Later register a large deposit and observe the two pools accrue different reward amounts for the same period.

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.

A staking contract hands out rewards over time and tracks when each pool was last updated. One shared timestamp and each pool's own timestamp can get out of step, so when new rewards are added the maths credits different pools with different amounts of time. That means some pools receive too many rewards and others too few.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by hash

View all

27 more disclosed reports, best first.

More from LoopFi

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.