Skip to content
Business logic Audit finding Advanced

Multiple vote checkpoints per block will lead to incorrect vote accounting

The ERC721Votes contract records multiple checkpoints with the same timestamp when voting power changes occur in the same block, causing getPastVotes to return an incorrect value. An attacker could manipulate their voting power to be higher or lower than intended, potentially leading to governance takeover.

berndartmueller Follow

Originally disclosed on Code4rena

Submitted by berndartmueller, also found by 0x52, 0xSky, bin2chen, cccz, Chom, davidbrai, elprofesor, izhuer, m9800, PwnPatrol, and rvierdiiev

Voting power for each NFT owner is persisted within timestamp-dependent checkpoints. Every voting power increase or decrease is recorded. However, the implementation of ERC721Votes creates separate checkpoints with the same timestamp for each interaction, even when the interactions happen in the same block/timestamp.

Impact

Checkpoints with the same timestamp will cause issues within the ERC721Votes.getPastVotes(..) function and will return incorrect votes for a given _timestamp.

Proof of Concept

lib/token/ERC721Votes.sol#L252-L253

/// @dev Records a checkpoint
/// @param _account The account address
/// @param _id The checkpoint id
/// @param _prevTotalVotes The account's previous voting weight
/// @param _newTotalVotes The account's new voting weight
function _writeCheckpoint(
    address _account,
    uint256 _id,
    uint256 _prevTotalVotes,
    uint256 _newTotalVotes
) private {
    // Get the pointer to store the checkpoint
    Checkpoint storage checkpoint = checkpoints[_account][_id];

    // Record the updated voting weight and current time
    checkpoint.votes = uint192(_newTotalVotes);
    checkpoint.timestamp = uint64(block.timestamp);

    emit DelegateVotesChanged(_account, _prevTotalVotes, _newTotalVotes);
}

Consider the following example and the votes checkpoint snapshots:

Note: Bob owns a smart contract used to interact with the protocol

Transaction 0: Bob’s smart contract receives 1 NFT through minting (1 NFT equals 1 vote)

Checkpoint Index | Timestamp | Votes |

0 | 0 | 1 |

Transaction 1: Bob’s smart contract receives one more NFT through minting

Checkpoint Index | Timestamp | Votes |

0 | 0 | 1 |

1 | 1 | 2 |

Transaction 1: Within the same transaction 1, Bob’s smart-contract delegates 2 votes to Alice

Checkpoint Index | Timestamp | Votes |

0 | 0 | 1 |

1 | 1 | 2 |

2 | 1 | 0 |

Transaction 1: Again within the same transaction 1, Bob’s smart contract decides to reverse the delegation and self-delegates

Checkpoint Index | Timestamp | Votes |

0 | 0 | 1 |

1 | 1 | 2 |

2 | 1 | 0 |

3 | 1 | 2 |

Transaction 1: Bob’s smart contract buys one more NFT

Checkpoint Index | Timestamp | Votes |

0 | 0 | 1 |

1 | 1 | 2 |

2 | 1 | 0 |

3 | 1 | 2 |

4 | 2 | 3 |

Bob now wants to vote (via his smart contract) on a governance proposal that has been created on timeCreated = 1 (timestamp 1).

Internally, the Governor._castVote function determines the voter’s weight by calling getVotes(_voter, proposal.timeCreated).

governance/governor/Governor.sol#L275

weight = getVotes(_voter, proposal.timeCreated);

getVotes calls ERC721.getPastVotes internally:

governance/governor/Governor.sol#L462

function getVotes(address _account, uint256 _timestamp) public view returns (uint256) {
    return settings.token.getPastVotes(_account, _timestamp);
}

ERC721.getPastVotes(..., 1) tries to find the checkpoint within the while loop:

Iteration | low | middle | high |

0 | 0 | 2 | 4 |

The middle checkpoint with index 2 matches the given timestamp 1 and returns 0 votes. This is incorrect, as Bob has 2 votes. Bob is not able to vote properly.

(Please be aware that this is just one of many examples of how this issue can lead to incorrect vote accounting. In other cases, NFT owners could have more voting power than they are entitled to)

Recommended mitigation steps

Consider batching multiple checkpoints writes per block/timestamp similar to how NounsDAO records checkpoints.

Alex the Entreprenerd (judge) commented:

The Warden has shown how the checkpoint math can be gamed, opening up governance to flashloan exploits, infinite voting power and overall breaking all of governance quorum and execution thresholds.

Because any attacker can spam create checkpoints, to manipulate the result of the Binary Search, they can manipulate their balance to make the Governor think it’s way higher than intended.

Mitigation requires ensuring that the only balance recorded for a block is the latest value (end of flashloan so the balance goes back down).

Because the finding breaks accounting, allowing governance takeover, and the invariants of ERC721Votes are broken (votes are not what they are), I agree with High Severity.

kulkarohan (Nouns Builder) confirmed


The original report as published on Code4rena, by berndartmueller. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 _writeCheckpoint function always creates a new checkpoint even if one already exists for the current block timestamp, instead of updating the existing checkpoint.

Impact

An attacker could manipulate their voting power to be higher or lower than intended, potentially leading to governance takeover, as the judge noted.

What the attacker needed

Attacker controls a smart contract that interacts with the…; Attacker can perform multiple voting power changes in one…

The fix

The code should be fixed by batching multiple checkpoint writes per block/timestamp, similar to how NounsDAO records checkpoints.

How it was found

  1. Bob's smart contract receives an NFT via minting, creating a checkpoint with 1 vote at timestamp 0.
  2. In the same transaction (timestamp 1), Bob's contract receives another NFT, creating a second checkpoint with 2 votes at timestamp 1.
  3. Still in the same transaction, Bob's contract delegates 2 votes to Alice, creating a third checkpoint with 0 votes at timestamp 1.
  4. Bob's contract then self-delegates, creating a fourth checkpoint with 2 votes at timestamp 1.
  5. Bob's contract buys one more NFT, creating a fifth checkpoint with 3 votes at timestamp 2.
  6. Bob tries to vote on a proposal created at timestamp 1; getPastVotes finds the checkpoint with index 2 (timestamp 1, votes 0) and returns 0 instead of 2.

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 report is about a voting system for an NFT project. When someone's voting power changes, the system saves a snapshot with the current time. If several changes happen in the same block (same time), multiple snapshots share the same timestamp. Later, when checking past voting power, the system picks the wrong snapshot, so the vote count is wrong. This could let someone cheat in governance votes.

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.

More by berndartmueller

View all

97 more disclosed reports, best first.

More from Nouns Builder contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.