ERC721Checkpointable: delegateBySig allows the user to vote to address 0, which causes the user to permanently lose his vote and cannot transfer his NFT.
The delegateBySig function allowed a user to delegate their votes to the zero address, unlike the delegate function which prevented this. This caused the user's votes to be permanently lost and prevented them from transferring or burning their NFT due to an underflow in the _moveDelegates function.
Submitted by cccz, also found by berndartmueller, bin2chen, csanuragjain, IEatBabyCarrots, jayphbee, KIntern_NA, Lambda, and zzzitron
In the ERC721Checkpointable contract, when the user votes with the delegate function, the delegatee will not be address 0.
function delegate(address delegatee) public {
if (delegatee == address(0)) delegatee = msg.sender;
return _delegate(msg.sender, delegatee);
}
However, there is no such restriction in the delegateBySig function, which allows the user to vote to address 0.
function delegateBySig(
address delegatee,
uint256 nonce,
uint256 expiry,
uint8 v,
bytes32 r,
bytes32 s
) public {
bytes32 domainSeparator = keccak256(
abi.encode(DOMAIN_TYPEHASH, keccak256(bytes(name())), getChainId(), address(this))
);
bytes32 structHash = keccak256(abi.encode(DELEGATION_TYPEHASH, delegatee, nonce, expiry));
bytes32 digest = keccak256(abi.encodePacked('\x19\x01', domainSeparator, structHash));
address signatory = ecrecover(digest, v, r, s);
require(signatory != address(0), 'ERC721Checkpointable::delegateBySig: invalid signature');
require(nonce == nonces[signatory]++, 'ERC721Checkpointable::delegateBySig: invalid nonce');
require(block.timestamp <= expiry, 'ERC721Checkpointable::delegateBySig: signature expired');
return _delegate(signatory, delegatee);
}
If user A votes to address 0 in the delegateBySig function, _delegates[A] will be address 0, but the delegates function will return the address of user A and getCurrentVotes(A) will return 0.
function _delegate(address delegator, address delegatee) internal {
/// @notice differs from `_delegate()` in `Comp.sol` to use `delegates` override method to simulate auto-delegation
address currentDelegate = delegates(delegator);
_delegates[delegator] = delegatee;
...
function delegates(address delegator) public view returns (address) {
address current = _delegates[delegator];
return current == address(0) ? delegator : current;
}
Later, if user A votes to another address or transfers NFT, the _moveDelegates function will fail due to overflow, which makes user A lose votes forever and cannot transfer NFT.
function _moveDelegates(
address srcRep,
address dstRep,
uint96 amount
) internal {
if (srcRep != dstRep && amount > 0) {
if (srcRep != address(0)) {
uint32 srcRepNum = numCheckpoints[srcRep];
uint96 srcRepOld = srcRepNum > 0 ? checkpoints[srcRep][srcRepNum - 1].votes : 0;
uint96 srcRepNew = sub96(srcRepOld, amount, 'ERC721Checkpointable::_moveDelegates: amount underflows'); // auditor : overflow here
_writeCheckpoint(srcRep, srcRepNum, srcRepOld, srcRepNew);
}
On the other hand, since the burn function also fails, this can also be used to prevent the NFT from being burned by the minter
function burn(uint256 nounId) public override onlyMinter {
_burn(nounId);
emit NounBurned(nounId);
}
...
function _burn(uint256 tokenId) internal virtual {
address owner = ERC721.ownerOf(tokenId);
_beforeTokenTransfer(owner, address(0), tokenId);
...
function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId
) internal override {
super._beforeTokenTransfer(from, to, tokenId);
/// @notice Differs from `_transferTokens()` to use `delegates` override method to simulate auto-delegation
_moveDelegates(delegates(from), delegates(to), 1);
}
Proof of Concept
Recommended Mitigation Steps
Consider requiring in the delegateBySig function that delegatee cannot be address 0.
function delegateBySig(
address delegatee,
uint256 nonce,
uint256 expiry,
uint8 v,
bytes32 r,
bytes32 s
) public {
+ require(delegatee != address(0));
eladmallel (Nouns DAO) confirmed and commented:
We agree this is a bug that has existed since Nouns launched, and plan to fix the bug with the suggested requirement that delegatee should not be address(0).
Worth noting that this fix will not have a positive effect on Nouns, as the token is already deployed and not upgradable.
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
Likely the delegateBySig function lacked a check to prevent the delegatee from being the zero address, unlike the delegate function.
Impact
The user permanently loses their votes and cannot transfer or burn their NFT, and the burn function also fails, preventing the minter from burning the NFT.
What the attacker needed
User must sign a delegation message; User must call delegateBySig with delegatee 0
The fix
The delegateBySig function should be fixed by requiring that the delegatee is not the zero address.
How it was found
- User A calls delegateBySig with delegatee set to address 0.
- The _delegates mapping for user A is set to address 0.
- The delegates function returns user A's address instead of 0, and getCurrentVotes(A) returns 0.
- Later, when user A tries to vote for another address or transfer the NFT, _moveDelegates fails due to underflow.
- User A permanently loses votes and cannot transfer the NFT.
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 NFT voting system, users can delegate their voting power to someone else. The normal delegate function prevents delegating to the zero address (which means no one), but a second function that allows delegation with a signature did not have this check. If a user used that function to delegate to the zero address, their votes were lost and they could no longer transfer or burn their NFT because the system tried to subtract votes that were not there.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.