code-423n4 / 2022-01-xdefi-findings

0 stars 0 forks source link

`merge` can fail due to tokenId collisions #132

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

cmichel

Vulnerability details

The XDEFIDistribution.merge function burns tokens, which decreases the ERC721Enumerable.totalSupply() and the _generateNewTokenId function returns a token ID as the concatenation of the points and totalSupply() + 1:

function _generateNewTokenId(uint256 points_) internal view returns (uint256 tokenId_) {
    // Points is capped at 128 bits (max supply of XDEFI for 10 years locked), total supply of NFTs is capped at 128 bits.
    return (points_ << uint256(128)) + uint128(totalSupply() + 1);
}

If a user is unlucky it can happen that there is a collision and they cannot merge their tokens as the _safeMint fails when trying to mint an already existing token ID.

POC

Impact

Token merges can fail

Recommended Mitigation Steps

You probably don't want totalSupply() to ever decrease. Either don't call _burn and use a different way of invalidating these tokens. Or just use a counter for the lower bits of tokenId instead of totalSupply() + 1.

deluca-mike commented 2 years ago

Valid and duplicate of #17