Skip to content
Smart contracts Audit finding Intermediate

reward tokens could get lost due to rounding down

The reward-token deposit and withdrawal functions divide the token amount by the per-epoch allocation and round the result down, so any remainder is not counted towards the reward schedule. This means a portion of deposited reward tokens can be stranded and never paid out to stakers.

gpersoon Follow

Originally disclosed on Code4rena

Submitted by gpersoon, also found by hickuphh3, xYrYuYx, and jonah1005

Impact

The function depositRewardTokens divides the “amount” of tokens by allocatedTokensPerEpoch to calculate the endEpoch. When “amount” isn’t a multiple of allocatedTokensPerEpoch the result of the division will be rounded down, effectively losing a number of tokens for the rewards.

For example if allocatedTokensPerEpoch is set to 3e18 and “amount” is 100e18 then endEpoch will be increased with 33e18 and the last 1e18 tokens are lost.

A similar problem occurs here:

  • in setAllocatedTokensPerEpoch(), with the recalculation of endEpoch
  • in takeOutRewardTokens(), with the retrieval of tokens
  • in _stake(), when initializing endEpoch (e.g. when endEpoch==0)

Proof of Concept

Recommended Mitigation Steps

In depositRewardTokens() add, in the beginning of function, before the if statement:

require(amount % allocatedTokensPerEpoch == 0,"Not multiple");

In takeOutRewardTokens() add:

require(amount % allocatedTokensPerEpoch == 0,"Not multiple");

Update setAllocatedTokensPerEpoch() to something like:

if (`endEpoch` != 0) {
...
uint128 futureRewards = ...
require(futureRewards % amount ==0,"Not multiple");
...\
} else { // to prevent issues with \_stake()
require(rewardsLocked % allocatedTokensPerEpoch==0,"Not multiple");
}

kitti-katy (Covalent) confirmed:

Agreed, the original assumption was that the owner would always make sure the take out and deposit amount is multiple of emission rate. But yes, this is good to add the check. Also it is not that risky since the emission rate wouldn’t be that high per epoch and the loss will always be less than the emission rate.

GalloDaSballo (judge) commented:

Agree with the finding, since it’s a rounding error the max loss in rewards can at most be 1 less than the denominator

That said, this is a Medium Severity Finding as per the doc: 2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.

Where in this case the rounding is a way to leak value (loss of yield)

The original report as published on Code4rena, by gpersoon. 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 integer division truncates the remainder when converting a token amount into a number of epochs, and the code never accounts for the leftover tokens.

Impact

Reward tokens worth less than one epoch's allocation could be lost on each deposit, withdrawal or rate change, though the loss is bounded by the emission rate and the owner is assumed to supply multiples of it.

What the attacker needed

Owner supplies non-multiple amounts; reward rate set per epoch

The fix

The report recommends adding checks that amounts are exact multiples of the per-epoch allocation in the deposit, withdrawal and rate-setting functions, and handling the staking initialisation case.

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 pays out rewards at a fixed rate per period. When someone adds reward tokens, the contract works out how many periods the tokens cover by dividing the total by the rate, and it throws away any leftover fraction. Those leftover tokens stay in the contract but are never scheduled for payout, so they are effectively lost.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by gpersoon

View all

47 more disclosed reports, best first.

More from Covalent contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.