ERC721Votes : Token owners can double voting power through self delegation
The ERC721Votes contract's delegation logic incorrectly treats a first-time self-delegation as a transfer from the zero address, adding voting power without subtracting it. An attacker could double their voting power once by delegating to themselves.
Submitted by zkhorse, also found by berndartmueller, Ch_301, hxzy, hyh, MEP, pcarranzav, pfapostol, Picodes, and Solimander
The owner of one or many ERC721Votes tokens can double their voting power once (and only once) by delegating to their own address as their first delegation.
Scenario
This exploit relies on the initial default value of the delegation mapping in ERC721Votes, which is why it will only work once per address.
First, the token owner must call delegate or delegateBySig, passing their own address as the delegate:
/// @notice Delegates votes to an account
/// @param _to The address delegating votes to
function delegate(address _to) external {
_delegate(msg.sender, _to);
}
This calls into the internal _delegate function, with _from and _to both set to the token owner’s address:
/// @dev Updates delegate addresses
/// @param _from The address delegating votes from
/// @param _to The address delegating votes to
function _delegate(address _from, address _to) internal {
// Get the previous delegate
address prevDelegate = delegation[_from];
// Store the new delegate
delegation[_from] = _to;
emit DelegateChanged(_from, prevDelegate, _to);
// Transfer voting weight from the previous delegate to the new delegate
_moveDelegateVotes(prevDelegate, _to, balanceOf(_from));
}
Since this is the token owner’s first delegation, the delegation mapping does not contain a value for the _from address, and prevDelegate on L#181 will be set to address(0):
// Get the previous delegate
address prevDelegate = delegation[_from];
This function then calls into _moveDelegateVotes to transfer voting power. This time, _from is prevDelegate, equal to address(0); _to is the token owner’s address; and _amount is balanceOf(_from), the token owner’s current balance:
ERC721Votes#_moveDelegateVotes
/// @dev Transfers voting weight
/// @param _from The address delegating votes from
/// @param _to The address delegating votes to
/// @param _amount The number of votes delegating
function _moveDelegateVotes(
address _from,
address _to,
uint256 _amount
) internal {
unchecked {
// If voting weight is being transferred:
if (_from != _to && _amount > 0) {
// If this isn't a token mint:
if (_from != address(0)) {
// Get the sender's number of checkpoints
uint256 nCheckpoints = numCheckpoints[_from]++;
// Used to store the sender's previous voting weight
uint256 prevTotalVotes;
// If this isn't the sender's first checkpoint: Get their previous voting weight
if (nCheckpoints != 0) prevTotalVotes = checkpoints[_from][nCheckpoints - 1].votes;
// Update their voting weight
_writeCheckpoint(_from, nCheckpoints, prevTotalVotes, prevTotalVotes - _amount);
}
// If this isn't a token burn:
if (_to != address(0)) {
// Get the recipients's number of checkpoints
uint256 nCheckpoints = numCheckpoints[_to]++;
// Used to store the recipient's previous voting weight
uint256 prevTotalVotes;
// If this isn't the recipient's first checkpoint: Get their previous voting weight
if (nCheckpoints != 0) prevTotalVotes = checkpoints[_to][nCheckpoints - 1].votes;
// Update their voting weight
_writeCheckpoint(_to, nCheckpoints, prevTotalVotes, prevTotalVotes + _amount);
}
}
}
}
The if condition on L#203 is true, since _from is address(0), _to is the owner address, and _amount is nonzero:
// If voting weight is being transferred:
if (_from != _to && _amount > 0) {
Execution skips the if block on L#205-217, since _from is address(0):
// If this isn't a token mint:
if (_from != address(0)) {
// Get the sender's number of checkpoints
uint256 nCheckpoints = numCheckpoints[_from]++;
// Used to store the sender's previous voting weight
uint256 prevTotalVotes;
// If this isn't the sender's first checkpoint: Get their previous voting weight
if (nCheckpoints != 0) prevTotalVotes = checkpoints[_from][nCheckpoints - 1].votes;
// Update their voting weight
_writeCheckpoint(_from, nCheckpoints, prevTotalVotes, prevTotalVotes - _amount);
}
However, the if block on L#220-232 will execute and increase the voting power allocated to _to:
// If this isn't a token burn:
if (_to != address(0)) {
// Get the recipients's number of checkpoints
uint256 nCheckpoints = numCheckpoints[_to]++;
// Used to store the recipient's previous voting weight
uint256 prevTotalVotes;
// If this isn't the recipient's first checkpoint: Get their previous voting weight
if (nCheckpoints != 0) prevTotalVotes = checkpoints[_to][nCheckpoints - 1].votes;
// Update their voting weight
_writeCheckpoint(_to, nCheckpoints, prevTotalVotes, prevTotalVotes + _amount);
}
The token owner’s voting power has now been increased by an amount equal to their total number of tokens, without an offsetting decrease.
This exploit only works once: if a token owner subsequently delegates to themselves after their initial self delegation, prevDelegate will be set to a non-default value in _delegate, and the delegation logic will work as intended.
Impact
Malicious ERC21Votes owners can accrue more voting power than they deserve. Especially malicious owners may quietly acquire multiple tokens before doubling their voting power. In an early DAO with a small supply of tokens, the impact of this exploit could be significant.
Recommended Mitigation Steps
Make the delegates function public rather than external:
/// @notice The delegate for an account
/// @param _account The account address
function delegates(address _account) public view returns (address) {
address current = delegation[_account];
return current == address(0) ? _account : current;
}
Then, call this function rather than accessing the delegation mapping directly:
/// @dev Updates delegate addresses
/// @param _from The address delegating votes from
/// @param _to The address delegating votes to
function _delegate(address _from, address _to) internal {
// Get the previous delegate
address prevDelegate = delegates(_from);
// Store the new delegate
delegation[_from] = _to;
emit DelegateChanged(_from, prevDelegate, _to);
// Transfer voting weight from the previous delegate to the new delegate
_moveDelegateVotes(prevDelegate, _to, balanceOf(_from));
}
Note that the original NounsDAO contracts follow this pattern. (See here and here).
Test cases
(Put the following test cases in Gov.t.sol)
function test_delegate_to_self_doubles_voting_power() public {
mintVoter1();
assertEq(token.getVotes(address(voter1)), 1);
vm.startPrank(voter1);
token.delegate(address(voter1));
assertEq(token.getVotes(address(voter1)), 2);
}
function mintToken(uint256 tokenId) internal {
vm.prank(voter1);
auction.createBid{ value: 0.420 ether }(tokenId);
vm.warp(block.timestamp + auctionParams.duration + 1 seconds);
auction.settleCurrentAndCreateNewAuction();
}
function test_delegate_to_self_multiple_tokens_doubles_voting_power() public {
// An especially malicious user may acquire multiple tokens
// before doubling their voting power through this exploit.
mintVoter1();
mintToken(3);
mintToken(4);
mintToken(5);
mintToken(6);
assertEq(token.getVotes(address(voter1)), 5);
vm.prank(voter1);
token.delegate(address(voter1));
assertEq(token.getVotes(address(voter1)), 10);
}
Alex the Entreprenerd (judge) commented:
The warden has shown how, because of an incorrect assumption in reducing a non-existing previous delegate votes, through self-delegation, a user can double their voting power.
Because the finding shows how the delegation system is broken, and because governance is a core aspect (Secure funds, move funds, etc..) I agree with High Severity.
Due to multiple reports of this type, with various different attacks, mitigation is non-trivial.
kulkarohan (Nouns Builder) confirmed
Alex the Entreprenerd (judge) commented:
In contrast to the dangerous overflow, this finding (and its duplicates) has shown how the Delegation Mechanism can be used to double the voting power.
For that reason, the underlying issue being different, am choosing to leave this finding separate.
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 _delegate function reads the previous delegate directly from the delegation mapping, which defaults to address(0) for first-time delegators, instead of using a function that returns the account itself when no delegate is set.
Impact
A malicious token owner could double their voting power once, potentially gaining undue influence in governance, especially in a DAO with a small token supply.
What the attacker needed
Own at least one ERC721Votes token; have never delegated before
The fix
The delegates function should be made public and used in _delegate to return the account itself when no delegate is set, preventing the zero-address subtraction skip.
How it was found
- The token owner calls delegate or delegateBySig with their own address as the delegate.
- The internal _delegate function sets prevDelegate to address(0) because the delegation mapping has no entry for the owner.
- _moveDelegateVotes is called with _from as address(0), _to as the owner, and _amount as the owner's token balance.
- The function skips the subtraction block because _from is address(0), but executes the addition block for _to, increasing the owner's voting power by _amount.
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 this voting system, each token gives you one vote. When you delegate your votes to someone (including yourself), the system should move your votes from your previous delegate to the new one. But if you have never delegated before, the system mistakenly thinks your previous delegate is the zero address and adds your votes to the new delegate without removing them from anywhere, so you end up with double the votes.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.