Skip to content
Business logic Audit finding Intermediate

Unused rewards(because of totalSupply()==0 for some period) will be locked forever in VE3DRewardPool and BaseRewardPool

The reward pool contracts fail to distribute rewards during periods when totalSupply() is zero, because rewardPerToken() returns the stored value without accumulating rewards. As a result, rewards allocated for those periods remain stuck in the contract and are never paid to stakers.

unforgiven Follow

Originally disclosed on Code4rena

Submitted by unforgiven, also found by csanuragjain

https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/BaseRewardPool.sol#L152-L162

https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/VE3DRewardPool.sol#L170-L183

Impact

The VE3DRewardPool and BaseRewardPool contract is supposed to distribute rewards to stackers, but if in some period, totalSupply() was equal to 0, then for that time period, rewards will not added to rewardPerTokenStored and those period rewards would not distribute to any address and those rewards will stuck in contract forever.

Proof of Concept

This is notifyRewardAmount() code in BaseRewardPool contract: (VE3DRewardPool code is similar)

      function notifyRewardAmount(uint256 reward) internal updateReward(address(0)) {
        historicalRewards = historicalRewards.add(reward);
        if (block.timestamp >= periodFinish) {
            rewardRate = reward.div(duration);
        } else {
            uint256 remaining = periodFinish.sub(block.timestamp);
            uint256 leftover = remaining.mul(rewardRate);
            reward = reward.add(leftover);
            rewardRate = reward.div(duration);
        }
        currentRewards = reward;
        lastUpdateTime = block.timestamp;
        periodFinish = block.timestamp.add(duration);
        emit RewardAdded(reward);
    }

As you can see, in the line rewardRate = reward.div(duration); the value of rewardRate has been set to the division of available reward to duration. so if we distribute rewardRate amount in every second between stackers, then all rewards will be used by contract. contract uses updateReward() modifier to update rewardPerTokenStored (this variable keeps track of distributed tokens) and this modifier uses rewardPerToken() to update BaseRewardPool:

      modifier updateReward(address account) {
        rewardPerTokenStored = rewardPerToken();
        lastUpdateTime = lastTimeRewardApplicable();
        if (account != address(0)) {
            rewards[account] = earned(account);
            userRewardPerTokenPaid[account] = rewardPerTokenStored;
        }
        emit RewardUpdated(account, rewards[account], rewardPerTokenStored, lastUpdateTime);
        _;
    }

This is rewardPerToken() code in BaseRewardPool:

    function rewardPerToken() public view returns (uint256) {
        if (totalSupply() == 0) {
            return rewardPerTokenStored;
        }
        return
            rewardPerTokenStored.add(
                lastTimeRewardApplicable().sub(lastUpdateTime).mul(rewardRate).mul(1e18).div(
                    totalSupply()
                )
            );
    }

If for some period totalSupply() was 0 then contract won’t increase rewardPerTokenStored and those periods reward stuck in contract forever, because there is no mechanism to calculate them and withdraw them in contract. For example if operator deploy and initialize the pool immediately before others having a chance of stacking their tokens, and use queueNewRewards() to queue the rewards then The rewards for early period of pool will be locked forever.

Tools Used

VIM

Recommended Mitigation Steps

Add some mechanism to recalculate rewardRate or calculated undistributed rewards(calculated undistributed reward based on rewardRate and when totalSupply() is 0).

solvetony (veToken Finance) disagreed with severity and commented:

Recover function would solve this issue. Middle risk.

Alex the Entreprenerd (judge) decreased severity to Medium and commented:

The warden has found a valid problem, however a coded POC would have gone a long way.

In order to judge the issue I had to code it for myself to be able to demonstrate the problem.

Anyhow this is a Brownie dump of me setting up the contract (removing transferFrom to get it done rapidly) Showing how skipping 1/3 of reward duration will cause a loss of 1/3 of the yield.

Meaning that the accumulator used for rewards is not redistributing the old rewards

>>> x.lastTimeRewardApplicable()
0
>>> x.queueNewRewards(1e18, {"from": a[0]})
Transaction sent: 0x0b959c886d959038396aa0a9a82cef39c6bc817e4e48026068b242b0a46df81f
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 5
  BaseRewardPool.queueNewRewards confirmed   Block: 15208165   Gas used: 46822 (0.39%)

<Transaction '0x0b959c886d959038396aa0a9a82cef39c6bc817e4e48026068b242b0a46df81f'>
>>> x.lastTimeRewardApplicable()
1658703966
>>> chain.time()
1658703975
>>> 1658703966 - 1658703975
-9
>>> x.lastTimeRewardApplicable()
1658703966
>>> x.lastUpdateTime()
1658703966
>>> x.periodFinish()
1659308766
>>> 1658703966 - 1659308766
-604800
>>> chain.sleep(604800 // 3) \#\# Sleep for third of time
>>> chain.time()
1658905644
>>> 1658905644- 1659308766
-403122
>>> x.stake(1e18, {"from": a[0]})
Transaction sent: 0x4899faa962b45d2f746db4f045b9858fdd506185cecab019516f4b9cae4bfd37
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 6
  BaseRewardPool.stake confirmed   Block: 15208166   Gas used: 73201 (0.61%)

<Transaction '0x4899faa962b45d2f746db4f045b9858fdd506185cecab019516f4b9cae4bfd37'>
>>> x.balanceOf(a[0])
1000000000000000000
>>> x.earned(a[0])
0
>>> chain.sleep(x.duration())
>>> x.earned(a[0])
0
>>> x.getReward(a[0], False)
Transaction sent: 0xba12fac5aa76e59d51fa277245cd96db53d7ca2685bdf97abdee734550f102e8
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 7
  BaseRewardPool.getReward confirmed   Block: 15208167   Gas used: 57623 (0.48%)

<Transaction '0xba12fac5aa76e59d51fa277245cd96db53d7ca2685bdf97abdee734550f102e8'>
>>> history[-1].return_value
666502976190414339
>>>

Which means, that the warden has found a valid vulnerability and any time spent with a totalSupply of 0 will cause those rewards to be lost.

Because this is contingent on:

  • No deposits before adding rewards
  • Lasts only until a deposit has happened
  • Is related to loss of yield

I believe Medium Severity to be more appropriate.


The original report as published on Code4rena, by unforgiven. 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 reward calculation skips accumulation when totalSupply() is zero, and there is no mechanism to redistribute or recover the unallocated rewards.

Impact

Rewards allocated during periods with no staked tokens are permanently locked in the contract and cannot be claimed by anyone, resulting in a loss of yield for stakers.

What the attacker needed

Pool deployed and initialised before any deposits; Rewards queued while totalSupply() is zero; No stakers during that period

The fix

The contract should include a mechanism to recalculate rewardRate or recover undistributed rewards when totalSupply() is zero, as suggested; the text does not state that a fix was applied.

How it was found

  1. Deploy and initialise the pool before any staker deposits tokens.
  2. Queue new rewards using queueNewRewards().
  3. Wait for a period during which totalSupply() remains zero.
  4. Later, when stakers deposit and rewards are claimed, the rewards from the zero-supply 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.

These contracts pay out rewards over time to people who stake tokens. The amount each staker earns is based on a running total that only increases when there are staked tokens. If nobody has staked yet, that running total does not move, so the rewards for that empty period are never given out and stay locked in the contract.

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 unforgiven

View all

50 more disclosed reports, best first.

More from veToken Finance contest

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.