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.
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:
ERC721Votes#delegate
This calls into the internal _delegate function, with _from and _to both set to the token owners address:
ERC721Votes#_delegate
Since this is the token owners 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):
ERC721Votes.sol#L180-L181
This function then calls into _moveDelegateVotes to transfer voting power. This time, _from is prevDelegate, equal to address(0); _to is the token owners address; and _amount is balanceOf(_from), the token owners current balance:
ERC721Votes#_moveDelegateVotes
The if condition on L#203 is true, since _from is address(0), _to is the owner address, and _amount is nonzero:
ERC721Votes.sol#L202-L203
Execution skips the if block on L#205-217, since _from is address(0):
ERC721Votes.sol#L205-L217
However, the if block on L#220-232 will execute and increase the voting power allocated to _to:
ERC721Votes.sol#L220-L232
The token owners 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.
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.
Make the delegates function public rather than external:
Then, call this function rather than accessing the delegation mapping directly:
Note that the original NounsDAO contracts follow this pattern. (See here and here).
(Put the following test cases in Gov.t.sol)
Alex the Entreprenerd (judge) commented:
kulkarohan (Nouns Builder) confirmed
Alex the Entreprenerd (judge) commented:
