ERC20ConvictionScore._writeCheckpoint` does not write to storage on same block
The smart contract function that writes conviction score checkpoints uses a memory copy instead of a storage pointer, so when a checkpoint is updated in the same block, the new value is not saved. This means users who update their conviction score multiple times in one block only have their first score persisted, and it can be exploited to inflate conviction scores or cause loss of conviction NFTs.
Submitted by cmichel
In ERC20ConvictionScore._writeCheckpoint, when the checkpoint is overwritten (checkpoint.fromBlock == blockNumber), the new value is set to the memory checkpoint structure and never written to storage.
// @audit this is MEMORY, setting new convictionScore doesn't write to storage
Checkpoint memory checkpoint = checkpoints[user][nCheckpoints - 1];
if (nCheckpoints > 0 && checkpoint.fromBlock == blockNumber) {
checkpoint.convictionScore = newCS;
}
Users that have their conviction score updated several times in the same block will only have their first score persisted.
POC
- User updates their conviction with
updateConvictionScore(user) - In the same block, the user now redeems an NFT conviction using
acquireConviction(id). This calls_increaseConvictionScore(user, amount)which calls_writeCheckpoint(..., prevConvictionScore + amount). The updated checkpoint is not written to storage, and the user lost their conviction NFT. (The conviction/governance totals might still be updated though, leading to a discrepancy.)
Impact
Users that have their conviction score updated several times in the same block will only have their first score persisted.
This also applies to the total conviction scores TOTAL_CONVICTION_SCORE and TOTAL_GOVERNANCE_SCORE (see _updateConvictionTotals) which is a big issue as these are updated a lot of times each block.
It can also be used for inflating a user’s conviction by first calling updateConvictionScore and then creating conviction tokens with tokenizeConviction. The _resetConviction will not actually reset the user’s conviction.
Recommended Mitigation Steps
Define the checkpoint variable as a storage pointer:
Checkpoint storage checkpoint = checkpoints[user][nCheckpoints - 1];
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 checkpoint variable is declared as memory instead of storage, so modifications are not persisted.
Impact
Users that have their conviction score updated several times in the same block will only have their first score persisted, and this can be used to inflate a user's conviction or cause loss of conviction NFTs.
What the attacker needed
User must perform multiple conviction updates in one block
The fix
Define the checkpoint variable as a storage pointer instead of a memory variable.
How it was found
- User updates their conviction with `updateConvictionScore(user)`
- In the same block, the user redeems an NFT conviction using `acquireConviction(id)`
- This calls `_increaseConvictionScore(user, amount)` which calls `_writeCheckpoint(..., prevConvictionScore + amount)`
- The updated checkpoint is not written to storage, and the user lost their conviction NFT
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.
In a smart contract, data can be stored permanently on the blockchain (storage) or temporarily in memory during a function call. This bug uses a temporary copy, so changes made to it are lost when the function ends. As a result, if a user does several actions in the same block, only the first change to their score is saved, which can be exploited to gain extra score or lose tokens.
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.