Skip to content
Business logic Audit finding Intermediate

Rewards for initial period can be lost in all of the synthetix derivative contracts

The reward contracts start the reward period when the owner calls notifyRewardAmount, but if no one has staked yet, the rewards for the initial period are not distributed and remain stuck in the contract. An attacker could exploit this by delaying their stake to cause a portion of rewards to be lost, though the report does not describe a direct theft.

BugBusters Follow

Originally disclosed on Code4rena

Submitted by BugBusters

Lines of code

https://github.com/code-423n4/2023-06-lybra/blob/7b73ef2fbb542b569e182d9abf79be643ca883ee/contracts/lybra/miner/stakerewardV2pool.sol#L132-L150 https://github.com/code-423n4/2023-06-lybra/blob/7b73ef2fbb542b569e182d9abf79be643ca883ee/contracts/lybra/miner/ProtocolRewardsPool.sol#L227-L240 https://github.com/code-423n4/2023-06-lybra/blob/7b73ef2fbb542b569e182d9abf79be643ca883ee/contracts/lybra/miner/EUSDMiningIncentives.sol#L226-L242

Impact

Rewards in the synthetix derivative contracts (EUSDMinningIncentives.sol, ProtocolRewardsPool.sol and stakerRewardsV2Pool.sol) are initiated when the owner calls the notifyRewardAmount. This function calculates the reward rate per second and also records the start of the reward period. This has an edge case where rewards are not counted for the initial period of time until there is at least one participant.

Proof of Concept

Look at the code for stakerrewardV2Pool.sol (other files have somewhat similar logic too), derived from the synthetix:

    function notifyRewardAmount(uint256 _amount) external onlyOwner updateReward(address(0)) {
        if (block.timestamp >= finishAt) {
            rewardRatio = _amount / duration;
        } else {
            uint256 remainingRewards = (finishAt - block.timestamp) * rewardRatio;
            rewardRatio = (_amount + remainingRewards) / duration;
        }

        require(rewardRatio > 0, "reward ratio = 0");

        finishAt = block.timestamp + duration;
        updatedAt = block.timestamp;
        emit NotifyRewardChanged(_amount, block.timestamp);
    }

    function _min(uint256 x, uint256 y) private pure returns (uint256) {
        return x <= y ? x : y;
    }
}

The intention here, is to calculate how many tokens should be rewarded by unit of time (second) and record the span of time for the reward cycle. However, this has an edge case where rewards are not counted for the initial period of time until there is at least one participant (in this case, a holder of BathTokens). During this initial period of time, the reward rate will still apply but as there isn’t any participant, then no one will be able to claim these rewards and these rewards will be lost and stuck in the system.

This is a known vulnerability that has been covered before. The following reports can be used as a reference for the described issue:

As described by the 0xmacro blogpost, this can play out as the following:

Let’s consider that you have a StakingRewards contract with a reward duration of one month seconds (2592000):

Block N Timestamp = X

You call notifyRewardAmount() with a reward of one month seconds (2592000) only. The intention is for a period of a month, 1 reward token per second should be distributed to stakers.

  • State :

    • rewardRate = 1
    • periodFinish = X + 2592000

Block M Timestamp = X + Y

Y time has passed and the first staker stakes some amount:

  1. stake()
  2. updateReward rewardPerTokenStored = 0 lastUpdateTime = X + Y

Hence, for this staker, the clock has started from X+Y, and they will accumulate rewards from this point.

Please note, that the periodFinish is X + rewardsDuration, not X + Y + rewardsDuration. Therefore, the contract will only distribute rewards until X + rewardsDuration, losing Y * rewardRate => Y * 1 inside of the contract, as rewardRate = 1 (if we consider the above example).

Now, if we consider delay (Y) to be 30 minutes, then:

Only 2590200 (2592000-1800) tokens will be distributed and these 1800 tokens will remain unused in the contract until the next cycle of notifyRewardAmount().

Tools Used

Manual Review

Recommended Mitigation Steps

A possible solution to the issue would be to set the start and end time for the current reward cycle when the first participant joins the reward program (i.e. when the total supply is greater than zero) instead of starting the process in the notifyRewardAmount.

0xean (judge) decreased severity to Medium

LybraFinance confirmed


The original report as published on Code4rena, by BugBusters. 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

Impact

A portion of rewards equal to the time between notifyRewardAmount and the first stake multiplied by the reward rate could be lost and remain stuck in the contract.

What the attacker needed

Owner calls notifyRewardAmount; no stakers at that time; delay before first stake

The fix

The start and end time of the reward cycle should be set when the first participant joins (i.e., when total supply is greater than zero) instead of in notifyRewardAmount.

How it was found

  1. Owner calls notifyRewardAmount to start a reward period with a given duration.
  2. Some time passes before any user stakes.
  3. The first user stakes and rewards start accumulating from that point.
  4. The reward period still ends at the originally set finish time, so rewards for the initial period are not distributed.

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.

In many reward contracts, the owner sets a reward rate and a finish time. If no one has staked yet, the time keeps passing but no rewards are given out, so those rewards are effectively lost. This report points out that the reward period should start when the first person stakes, not when the owner sets the rewards.

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 BugBusters

View all

3 more disclosed reports, best first.

More from Lybra Finance

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.