Skip to content
DoS Audit finding Intermediate

Rewards distribution can be disrupted by a early user

The reward accounting variable is stored as a 128-bit integer, and the calculation that updates it can exceed that range. An early user who wraps a tiny amount and then sends reward tokens to the contract could make the value overflow, disrupting reward distribution.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

https://github.com/code-423n4/2022-01-yield/blob/e946f40239b33812e54fafc700eb2298df1a2579/contracts/ConvexStakingWrapper.sol#L206-L224

function _calcRewardIntegral(
    uint256 _index,
    address[2] memory _accounts,
    uint256[2] memory _balances,
    uint256 _supply,
    bool _isClaim
) internal {
    RewardType storage reward = rewards[_index];

    uint256 rewardIntegral = reward.reward_integral;
    uint256 rewardRemaining = reward.reward_remaining;

    //get difference in balance and remaining rewards
    //getReward is unguarded so we use reward_remaining to keep track of how much was actually claimed
    uint256 bal = IERC20(reward.reward_token).balanceOf(address(this));
    if (_supply > 0 && (bal - rewardRemaining) > 0) {
        rewardIntegral = uint128(rewardIntegral) + uint128(((bal - rewardRemaining) * 1e20) / _supply);
        reward.reward_integral = uint128(rewardIntegral);
    }

reward.reward_integral is uint128, if a early user mint (wrap) just 1 Wei of convexToken, and make _supply == 1, and then tranferring 5e18 of reward_token to the contract.

As a result, reward.reward_integral can exceed type(uint128).max and overflow, causing the rewards distribution to be disrupted.

Recommendation

Consider wrap a certain amount of initial totalSupply, e.g. 1e8, and never burn it. And consider using uint256 instead of uint128 for reward.reward_integral. Also, consdier lower 1e20 down to 1e12.

alcueca (Yield) confirmed but disagreed with severity and commented:

Assets are not a direct risk if it is the first user disrupting the contract. We would need to redeploy better code, but that’s it. I suggest this is reported as medium, as merits for a DoS attack.

As suggested elsewhere, the right solution if uint128 is to be used in storage, is to cast up to uint256 for calculations, and then back to uint128 to store again.

iamsahu (Yield) resolved

Alex the Entreprenerd (judge) changed severity from High to Medium and commented:

The warden identified a way an early depositor can grief the system, I believe the finding to be valid, and because:

  • It would conditionally disrupt the system
  • It would “brick the contract” at the loss of the griefer
  • No additional funds would be at risk

I believe medium severity to be appropriate


The original report as published on Code4rena, by WatchPug. 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 integral is stored as a 128-bit integer while the calculation dividing incoming rewards by a tiny supply can produce a value larger than that type can hold.

Impact

Reward distribution could be disrupted and the contract would need redeploying, but no additional funds would be at risk and the griefer loses their own deposit.

What the attacker needed

Attacker is the first depositor; attacker can wrap a tiny amount; attacker holds reward tokens to send

The fix

The project resolved the issue; the suggested approach was to seed an initial supply that is never burned, use a wider integer for the integral, and cast up to 256 bits for calculations before storing back.

How it was found

  1. An early user wraps just one wei of the staking token so the total supply is one.
  2. The user transfers a large amount of the reward token to the contract.
  3. The reward integral calculation then exceeds the 128-bit maximum and overflows.

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 smart contract keeps track of how many reward tokens each person has earned using a running total. That total is stored in a small fixed-size number, so if it grows too large it wraps around to a wrong value. An attacker who is the first to deposit a tiny amount can make the total jump too high, breaking the reward maths for everyone.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by WatchPug

View all

197 more disclosed reports, best first.

More from Yield Witch v2 contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.