Upon changing of delegate, VoteDelegation updates both the previous and the current checkpoint
The VoteEscrowDelegation contract incorrectly updates both the previous and current checkpoints when a delegate is changed or removed, because it modifies a storage reference to the latest checkpoint before writing a new one. This leads to incorrect vote counting, allowing retroactive manipulation of voting power.
Submitted by kenzo, also found by 0xA5DF, 0xpiglet, 0xsanson, arcoun, Bahurum, and IllIllI
The contract is accidently editing both the previous and current checkpoint when changing/removing a delegate.
Impact
Incorrect counting of votes.
Proof of Concept
If in delegate the delegate already has checkpoints, the function will grab the latest checkpoint, and add the tokenId to it. Note that it changes the storage variable.
if (nCheckpoints > 0) {
Checkpoint storage checkpoint = checkpoints[toTokenId][nCheckpoints - 1];
checkpoint.delegatedTokenIds.push(tokenId);
_writeCheckpoint(toTokenId, nCheckpoints, checkpoint.delegatedTokenIds);
It then calls _writeCheckpoint, which will add a new checkpoint if there’s no checkpoint created for this block yet:
Checkpoint memory oldCheckpoint = checkpoints[toTokenId][nCheckpoints - 1];
if (nCheckpoints > 0 && oldCheckpoint.fromBlock == block.number) {
oldCheckpoint.delegatedTokenIds = _delegatedTokenIds;
} else {
checkpoints[toTokenId][nCheckpoints] = Checkpoint(block.number, _delegatedTokenIds);
numCheckpoints[toTokenId] = nCheckpoints + 1;
}
Therefore, if this function has created a new checkpoint with the passed _delegatedTokenIds, we already saw that the previous function has already added tokenId to the previous checkpoint, so now both the new checkpoint and the previous checkpoint will have tokenId in them.
This is wrong as it updates an earlier checkpoint with the latest change.
The same situation happens in removeDelegation.
Recommended Mitigation Steps
When reading the latest checkpoint:
Checkpoint storage checkpoint = checkpoints[toTokenId][nCheckpoints - 1];
Change the storage to memory. This way it will not affect the previous checkpoint, but will pass the correct updated array to _writeCheckpoint, which will then write/update the correct checkpoint.
zeroexdead (Golom) confirmed and commented:
Fixed
delegate(): https://github.com/golom-protocol/contracts/commit/8a8c89beea22cd57f4ffaf3d0defcce863e9657fFixed
removeDelegation(): https://github.com/golom-protocol/contracts/commit/72350b0a3bdae4f21e2f015327037080f6bab867
LSDan (judge) increased severity to High and commented:
I went back and forth on if this was a duplicate of H-04 (#169) or not. The two issues are so similar it’s hard to pull them apart. Ultimately I do see the difference, mainly that this version of the issue results in a retroactive manipulation of voting power whereas the other issue allows the creation of infinite voting power. I’m upgrading this to high risk because it effectively destroys the integrity of the voting system which impacts every aspect of the protocol which is subject to vote.
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
The code uses a storage reference to the latest checkpoint and modifies it directly before writing a new checkpoint, so the old checkpoint is also changed.
Impact
This could lead to incorrect vote counting and retroactive manipulation of voting power, undermining the integrity of the voting system.
What the attacker needed
Delegate already has checkpoints; call delegate or removeDelegation
The fix
The issue was fixed by changing the storage reference to a memory reference when reading the latest checkpoint.
How it was found
- Call delegate when the delegate already has checkpoints, so the function grabs the latest checkpoint and adds the tokenId to it.
- The function then calls _writeCheckpoint, which creates a new checkpoint if none exists for the current block.
- Because the previous checkpoint was modified in storage, both the new and previous checkpoints now contain the tokenId.
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 where changing who you delegate your votes to accidentally alters past records of delegation. As a result, the same token can appear in multiple checkpoints, making the vote count wrong and letting someone tamper with voting power after the fact.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.