in notifyRewardAmount() of VE3DRewardPool and BaseRewardPool some tokes will be locked and not distributed becasue of rounding error
The reward calculation in two reward pool contracts divides the reward amount by the duration, and integer rounding leaves a small remainder that is never distributed or recoverable. An attacker cannot directly exploit this, but the rounding error causes some reward tokens to be permanently locked in the contract.
Originally disclosed on Code4rena
Submitted by unforgiven
Impact
Function notifyRewardAmount() calculates rewardRate for reward token/s in VE3DRewardPool and BaseRewardPool. to calculate rewardRate it divides reward amount to duration but because of the rounding error in division, some of reward amount wouldn’t get distributed and stuck in contract (rewardRate * duration < reward). and contract don’t redistributes them or don’t have any mechanism to recover them. This bug can be more damaging if the precision of rewardToken is low or token price is high.
Proof of Concept
This is notifyRewardAmount() code in BaseRewardPool: (contract VE3DRewardPool 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 it sets rewardRate = reward.div(duration); and this is where the rounding error happens. and even if contract distributes all in all the duration it will distribute rewardRate * duration which can be lower than reward and the extra reward amount will stuck in contract. This is queueNewRewards() code which calls notifyRewardAmount():
function queueNewRewards(uint256 _rewards) external returns (bool) {
require(msg.sender == operator, "!authorized");
_rewards = _rewards.add(queuedRewards);
if (block.timestamp >= periodFinish) {
notifyRewardAmount(_rewards);
queuedRewards = 0;
return true;
}
//et = now - (finish-duration)
uint256 elapsedTime = block.timestamp.sub(periodFinish.sub(duration));
//current at now: rewardRate * elapsedTime
uint256 currentAtNow = rewardRate * elapsedTime;
uint256 queuedRatio = currentAtNow.mul(1000).div(_rewards);
//uint256 queuedRatio = currentRewards.mul(1000).div(_rewards);
if (queuedRatio < newRewardRatio) {
notifyRewardAmount(_rewards);
queuedRewards = 0;
} else {
queuedRewards = _rewards;
}
return true;
}
As you can see it queues rewardToken and when the reward amount reach some point it calls notifyRewardAmount() and set queuedRewards to 0x0. notifyRewardAmount() will set rewardRate based on reward amount but because of the rounding error some of the reward token 0 =< unused < duration will stuck in contract and pool will not distribute it. if the token has low precision or has higher price then this amount value can be very big because notifyRewardAmount() can be called multiple times.
Tools Used
VIM
Recommended Mitigation Steps
add extra amount to queuedRewards so it would be distributed on next notifyRewardAmount() or add other mechanism to recover it.
solvetony (veToken Finance) disagreed with severity and commented:
Will be fixed by two solutions, adding precision and by adding another function. Middle risk.
Alex the Entreprenerd (judge) decreased severity to Medium and commented:
The warden has shown how, due to rounding errors,
rewardRatemay round down and cause a certain amount of tokens not to be distributed.In lack of a way for the
operatorto re-queue the undistributed rewards, those tokens will be lost.Because we know
durationis604800we can see that therewardRatemax loss is604799.Meaning the potential max dust due to rounding down can be upwards of a number with 12 decimals
Because the finding is limited to loss of Yield, I believe Medium Severity to be more appropriate.
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 truncation when computing rewardRate, with no mechanism to carry the remainder forward or recover it.
Impact
Some reward tokens remain locked in the contract and are never distributed; the loss is limited to yield and can be larger for low-precision or high-value tokens.
What the attacker needed
None
The fix
The team said it would be fixed by adding precision and an additional function to recover or re-queue the undistributed rewards.
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.
When a contract splits a reward over time, it calculates a rate by dividing the total by the number of seconds. Computers using whole numbers throw away any remainder, so a tiny amount of the reward is never paid out and stays stuck in the contract forever.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.