User rewards stop accruing after any _writeCheckpoint calling action
A wrong array index in the _writeCheckpoint function caused every new balance checkpoint to record the user's voting status as false, even if they had previously voted. As a result, any deposit, withdrawal, or reward claim by a user who had voted stopped their rewards from accruing until they voted again.
Submitted by hyh, also found by smiling_heretic, unforgiven, and xiaoming90
Any user balance affecting action, i.e. deposit, withdraw/withdrawToken or getReward, calls _writeCheckpoint to update the balance records used for the earned reward estimation. The issue is that _writeCheckpoint always sets false to voted flag for the each new checkpoint due to wrong index used in the mapping access, while only voted periods are eligible for accruing the rewards.
This way any balance changing action of a voted user will lead to stopping of the rewards accrual for the user, until next vote will be cast. I.e. any action that has no relation to voting and should have only balance change as the reward accruing process impact, in fact removes any future rewards from the user until the next vote.
Setting the severity to be high as the impact here violates system logic and means next periods accrued rewards loss for a user.
Proof of Concept
_writeCheckpoint adds a new checkpoint if block.timestamp is not found in the last checkpoint:
function _writeCheckpoint(address account, uint balance) internal {
uint _timestamp = block.timestamp;
uint _nCheckPoints = numCheckpoints[account];
if (_nCheckPoints > 0 && checkpoints[account][_nCheckPoints - 1].timestamp == _timestamp) {
checkpoints[account][_nCheckPoints - 1].balanceOf = balance;
} else {
bool prevVoteStatus = (_nCheckPoints > 0) ? checkpoints[account][_nCheckPoints].voted : false;
checkpoints[account][_nCheckPoints] = Checkpoint(_timestamp, balance, prevVoteStatus);
numCheckpoints[account] = _nCheckPoints + 1;
}
}
However, instead of moving vote status from the previous checkpoint it records false to prevVoteStatus all the time as last status is checkpoints[account][_nCheckPoints-1].voted, while checkpoints[account][_nCheckPoints] isn’t created yet and is empty:
bool prevVoteStatus = (_nCheckPoints > 0) ? checkpoints[account][_nCheckPoints].voted : false;
Notice that checkpoints is a mapping and no range check violation happens:
/// @notice A record of balance checkpoints for each account, by index
mapping (address => mapping (uint => Checkpoint)) public checkpoints;
This will effectively lead to rewards removal on any user action, as earned() used in rewards estimation counts only voted periods:
if (_endIndex > 0) {
for (uint i = _startIndex; i < _endIndex; i++) {
Checkpoint memory cp0 = checkpoints[account][i];
Checkpoint memory cp1 = checkpoints[account][i+1];
(uint _rewardPerTokenStored0,) = getPriorRewardPerToken(token, cp0.timestamp);
(uint _rewardPerTokenStored1,) = getPriorRewardPerToken(token, cp1.timestamp);
if (cp0.voted) {
reward += cp0.balanceOf * (_rewardPerTokenStored1 - _rewardPerTokenStored0) / PRECISION;
}
}
}
Checkpoint memory cp = checkpoints[account][_endIndex];
uint lastCpWeeksVoteEnd = cp.timestamp - (cp.timestamp % (7 days)) + BRIBE_LAG + DURATION;
if (block.timestamp > lastCpWeeksVoteEnd) {
(uint _rewardPerTokenStored,) = getPriorRewardPerToken(token, cp.timestamp);
if (cp.voted) {
reward += cp.balanceOf * (rewardPerToken(token) - Math.max(_rewardPerTokenStored, userRewardPerTokenStored[token][account])) / PRECISION;
}
}
I.e. if a user has voted, then performed any of the operations that call _writeCheckpoint update: deposit, withdraw/withdrawToken or getReward, then this user will not have any rewards for the period between this operation and the next vote as all checkpoints that were created by _writeCheckpoint will have voted == false.
Recommended Mitigation Steps
Update the index to be _nCheckPoints-1:
- bool prevVoteStatus = (_nCheckPoints > 0) ? checkpoints[account][_nCheckPoints].voted : false;
+ bool prevVoteStatus = (_nCheckPoints > 0) ? checkpoints[account][_nCheckPoints - 1].voted : false;
pooltypes (Velodrome) disputed
Alex the Entreprenerd (judge) commented:
Due to 1 typo / oversight in the line
bool prevVoteStatus = (_nCheckPoints > 0) ? checkpoints[account][_nCheckPoints].voted : false;All future checkpoints are registered as havingvotedset to false.Due to a configuration choice (or mistake as detailed in other findings), having
votedset to false causes all rewards to not be receiveable.While the impact is loss of Yield (typically a medium finding), the finding shows how this bug will systematically impact all gauges for all users.
Because of that, I believe High Severity to be 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 an off-by-one error: the code read the voting status from the new checkpoint index instead of the previous one, so it always got the default false value.
Impact
Any user who had voted and then performed a balance-changing action would lose all rewards for the period until their next vote, affecting all gauges and all users.
What the attacker needed
User has voted at least once; user performs a balance-changing action
The fix
The index should be changed to _nCheckPoints-1 so the previous checkpoint's voted status is copied correctly.
How it was found
- A user votes, setting their checkpoint's voted flag to true.
- The user performs a balance-changing action (deposit, withdraw, or getReward), which calls _writeCheckpoint.
- _writeCheckpoint creates a new checkpoint but reads the voted flag from the wrong index, so it stores false.
- The earned() function only counts periods where the checkpoint's voted flag is true, so the user earns no rewards from that point until they vote again.
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.
This is a smart contract bug in a rewards system. When users change their balance, the contract saves a record of their balance and whether they had voted. A mistake in the code meant it always saved 'not voted' instead of copying the previous status. Since only 'voted' periods earn rewards, users lost rewards after any balance change until they voted again.
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.