Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Portion of the reward remains undistributed if the first staker arrives after notifyReward call #9

Closed
c4-bot-5 opened this issue Feb 25, 2024 · 3 comments
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working duplicate-369 🤖_06_group AI based duplicate group recommendation unsatisfactory does not satisfy C4 submission criteria; not eligible for awards

Comments

@c4-bot-5
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2024-02-uniswap-foundation/blob/main/src/UniStaker.sol#L584

Vulnerability details

Impact

Portion of the reward remains unused in the contract if the first staker arrives after notifyReward call.

Proof of Concept

When notifyReward is called the staking contract calculates the reward rate per second and saves the end of the distribution timestamp.
https://github.com/code-423n4/2024-02-uniswap-foundation/blob/main/src/UniStaker.sol#L570

    // We checkpoint the accumulator without updating the timestamp at which it was updated, because
    // that second operation will be done after updating the reward rate.
    rewardPerTokenAccumulatedCheckpoint = rewardPerTokenAccumulated();

    if (block.timestamp >= rewardEndTime) {
      scaledRewardRate = (_amount * SCALE_FACTOR) / REWARD_DURATION;
    } else {
      uint256 _remainingReward = scaledRewardRate * (rewardEndTime - block.timestamp);
      scaledRewardRate = (_remainingReward + _amount * SCALE_FACTOR) / REWARD_DURATION;
    }

>>  rewardEndTime = block.timestamp + REWARD_DURATION;
    lastCheckpointTime = block.timestamp;

Let's say we need to distribute 10 ETH in 30 days, reward rate will be 10 * 10e18 / (86400 * 30) = 3858024691358 tokens per second. If the first staker arrives only after one day he will start accumulating rewards with a 86400 seconds delay, thus 3858024691358 * 86400 = 0.3 ETH will stay undistributed because distribution will end on day 30.

Coded POC for UniStaker.t.sol

  function test_NotAllDistributed() public {
    address alice = address(0xa11cE);
    _mintGovToken(alice, 1000e18);
    _mintTransferAndNotifyReward(10 ether);
    vm.warp(block.timestamp + 86400);
    // first staker after 1 day
    _stake(alice, 1, address(0xde1e6a7eE), alice);
    vm.warp(block.timestamp + (86400 * 300));
    uint256 reward = uniStaker.unclaimedReward(alice);
    assert(reward != 10 ether);
    console.log("REWARD: ", reward);
    // 0.3 ETH undistributed
    console.log("UNDISTRIBUTED: ", 10 ether - reward);
  }

Tools Used

Foundry

Recommended Mitigation Steps

Consider setting rewardEndTime value during the first stake after notifyReward if totalStaked == 0

Assessed type

Other

@c4-bot-5 c4-bot-5 added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working labels Feb 25, 2024
c4-bot-1 added a commit that referenced this issue Feb 25, 2024
@c4-bot-12 c4-bot-12 added the 🤖_06_group AI based duplicate group recommendation label Mar 5, 2024
@c4-judge c4-judge added the primary issue Highest quality submission among a set of duplicates label Mar 6, 2024
@c4-judge
Copy link
Contributor

c4-judge commented Mar 6, 2024

MarioPoneder marked the issue as primary issue

This was referenced Mar 6, 2024
@c4-judge
Copy link
Contributor

c4-judge commented Mar 7, 2024

MarioPoneder marked the issue as duplicate of #369

@c4-judge c4-judge closed this as completed Mar 7, 2024
@c4-judge c4-judge added duplicate-369 and removed primary issue Highest quality submission among a set of duplicates labels Mar 7, 2024
@c4-judge
Copy link
Contributor

MarioPoneder marked the issue as unsatisfactory:
Invalid

@c4-judge c4-judge added the unsatisfactory does not satisfy C4 submission criteria; not eligible for awards label Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working duplicate-369 🤖_06_group AI based duplicate group recommendation unsatisfactory does not satisfy C4 submission criteria; not eligible for awards
Projects
None yet
Development

No branches or pull requests

3 participants