_writeCheckpoint does not write to storage on same block
The checkpoint-writing function in a vote-escrow delegation contract updated a memory copy instead of storage when overwriting a checkpoint in the same block, so the second delegation in that block was not saved. An attacker could repeatedly delegate and remove a token in one transaction to multiply their delegation power.
Submitted by async, also found by 0xA5DF, 0xpiglet, 0xsanson, ak1, DimitarDimitrov, Dravee, ElKu, IllIllI, JohnSmith, kenzo, and scaraven
VoteEscrowDelegation.sol#L101-L108
In VoteEscrowDelegation._writeCheckpoint, when the checkpoint is overwritten in the same block the new value is set with memory oldCheckpoint and thus is never written to storage.
Checkpoint memory oldCheckpoint = checkpoints[toTokenId][nCheckpoints - 1];
if (nCheckpoints > 0 && oldCheckpoint.fromBlock == block.number) {
oldCheckpoint.delegatedTokenIds = _delegatedTokenIds;
}
Users that remove and delegate a token (or call delegate on the same token twice) in the same block will only have their first delegation persisted.
Proof of Concept
- User delegates a
tokenIdby callingdelegate. - In the same block, the user decides to delgate the same token to a different token ID and calls
delegateagain which calls_writeCheckpoint. Since this is the second transaction in the same block the if statement in the code block above executes and stores_delegatedTokenIdsinmemory oldCheckpoint, thus not persisting the array of_delegatedTokenIdsin the checkpoint.
Recommended Mitigation Steps
Define the oldCheckpoint variable as a storage pointer:
Checkpoint storage oldCheckpoint = checkpoints[toTokenId][nCheckpoints - 1];
Just want to add to the impact (in case the judges consider to decrease severity), in my report of this bug (#625) I’ve mentioned a more severe impact:
An attacker can use this to multiplying his delegation power endlessly, by adding a delegation and removing it in the same block (using a contract to run those 2 functions in the same tx). The delegation will succeed but the removal will fail, this way each time this runs the user delegates again the same token.
Fixed. Ref: https://github.com/golom-protocol/contracts/commit/74b2e718f6ae9da815b52242a44451527d60d1ae
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 checkpoint variable was declared as a memory pointer rather than a storage pointer, so assignments to it did not persist.
Impact
Users who remove and delegate in the same block only have their first delegation persisted, and an attacker could multiply delegation power endlessly by adding and removing a delegation in the same transaction.
What the attacker needed
Ability to call delegate twice in one block; a contract to run both calls in one transaction
How it was found
- User delegates a token by calling delegate.
- In the same block, the user calls delegate again for the same token to a different token ID.
- The second call reaches the same-block branch and writes to a memory copy, so the new delegation is not saved.
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 smart contract stores data in two places: permanent storage and temporary memory. This code changed a temporary copy, so the change was thrown away. That meant a second action in the same block was silently ignored, letting someone repeat an action to gain extra voting power.
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.