Skip to content
Smart contracts Audit finding Advanced

Quorum overflow in `CollectionShutdown` leads to complete drain of contract's funds

The CollectionShutdown contract casts quorum votes to uint88, which can overflow when the collection token denomination is high, making quorumVotes much smaller than intended. An attacker can then claim a disproportionately large share of the contract's funds, draining its entire balance.

0xc0ffEE Follow with Audinarey, Ragnarok and 8 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-08-flayer-judging/issues/146

Found by

0xc0ffEE, Audinarey, Ragnarok, Ruhum, almantare, araj, asui, blockchain555, h2134, kuprum, zzykxx

Summary

CollectionShutdown.sol#150 and CollectionShutdown.sol#L247 cast quorum votes to uint88 as follows:

Solidity
uint totalSupply = params.collectionToken.totalSupply();
if (totalSupply > MAX_SHUTDOWN_TOKENS * 10 ** params.collectionToken.denomination()) revert TooManyItems();


// Set our quorum vote requirement
params.quorumVotes = uint88(totalSupply * SHUTDOWN_QUORUM_PERCENT / ONE_HUNDRED_PERCENT);

The problem with the above is that it may overflow:

  • collectionToken.denomination() may max 9
  • MAX_SHUTDOWN_TOKENS == 4
  • SHUTDOWN_QUORUM_PERCENT / ONE_HUNDRED_PERCENT == 1/2
  • Collection tokens are minted in Locker as follows:
    • token.mint(_recipient, tokenIdsLength * 1 ether * 10 ** token.denomination());

E.g. with totalSupply == 0.6190 ether * 10**9, we have that the check still passes, but:

  • totalSupply * SHUTDOWN_QUORUM_PERCENT / ONE_HUNDRED_PERCENT = 0.3095 ether * 10**9
  • type(uint88).max ~= 0.309485 ether * 10**9
  • uint88(0.3095 ether * 10**9) ~= 0.000015 ether * 10**9

Upon collection shutdown, tokens are sold on Sudoswap, and the funds thus obtained are distributed among the claimants. The claimed amount is then divided by quorumVotes as follows:

Solidity
uint amount = params.availableClaim * claimableVotes / (params.quorumVotes * ONE_HUNDRED_PERCENT / SHUTDOWN_QUORUM_PERCENT);

Thus, when dividing by a much smaller quorumVotes, the claimant receives much more than they are eligible for: in the PoC it's 20647 ether though only 1 ether has been received from sales.

Root Cause

CollectionShutdown.sol#150 and CollectionShutdown.sol#L247 downcast the quorum votes to uint88, which may overflow.

Internal pre-conditions

  1. The collection token denomination needs to be sufficiently large to cause overflow
  2. The amount of shutdown votes needs to be sufficiently large to cause overflow.

External pre-conditions

none

Attack Path

  1. A user creates a collection with denomination 9
  2. The user holding 0.6190 ether * 10**9 of the collection token (i.e. less than 1 NFT) starts collection shutdown.
    • At that point the quorum of votes overflows, and becomes much smaller
  3. Collection shutdown is executed normally.
  4. Tokens are sold on Sudoswap.
    • In the PoC they are sold for 1 ether.
  5. User claims the balance. Due to the overflow, they receive much more than what their NFTs were worth.
    • In the PoC user receives 20647 ether though only 1 ether has been received from sales.

Impact

The protocol suffers unbounded losses (the whole balance of CollectionShutdown contract can be drained.

PoC

Drop this test to CollectionShutdown.t.sol and execute with forge test --match-test test_QuorumOverflow:

Solidity
function test_QuorumOverflow() public {
    locker.createCollection(address(erc721c), 'Test Collection', 'TEST', 9);

    // Initialize our collection, without inflating `totalSupply` of the {CollectionToken}
    locker.setInitialized(address(erc721c), true);

    // Set our collection token for ease for reference in tests
    collectionToken = locker.collectionToken(address(erc721c));

    // Approve our shutdown contract to use test suite's tokens
    collectionToken.approve(address(collectionShutdown), type(uint).max);

    // Give some initial balance to CollectionShutdown contract
    vm.deal(address(collectionShutdown), 30000 ether);
    
    vm.startPrank(address(locker));
    // Suppose address(1) holds 0.6190 ether
    // in a collection token with denomination 9
    collectionToken.mint(address(1), 0.6190 ether * 10**9);
    vm.stopPrank();

    // Start collection shutdown from address(1)
    vm.startPrank(address(1));
    collectionToken.approve(address(collectionShutdown), 0.6190 ether * 10**9);
    collectionShutdown.start(address(erc721c));
    vm.stopPrank();

    // Mint NFTs into our collection {Locker} and process the execution
    uint[] memory tokenIds = _mintTokensIntoCollection(erc721c, 3);
    collectionShutdown.execute(address(erc721c), tokenIds);

    // Mock the process of the Sudoswap pool liquidating the NFTs for ETH.
    vm.startPrank(SUDOSWAP_POOL);
    // Transfer the specified tokens away from the Sudoswap position to simulate a purchase
    for (uint i; i < tokenIds.length; ++i) {
        erc721c.transferFrom(SUDOSWAP_POOL, address(5), i);
    }
    // Ensure the sudoswap pool has enough ETH to send
    deal(SUDOSWAP_POOL, 1 ether);
    // Send ETH from the Sudoswap Pool into the {CollectionShutdown} contract
    (bool sent,) = payable(address(collectionShutdown)).call{value: 1 ether}('');
    require(sent, 'Failed to send {CollectionShutdown} contract');
    vm.stopPrank();

    // Get our start balances so that we can compare to closing balances from claim
    uint startBalanceAddress = payable(address(1)).balance;

    // address(1) now can claim
    collectionShutdown.claim(address(erc721c), payable(address(1)));

    // Due to quorum overflow, address(1) now holds ~ 20647 ether
    assertApproxEqRel(payable(address(1)).balance - startBalanceAddress, 20647 ether, 0.01 ether);
}

Mitigation

Employ the appropriate type and cast for quorumVotes, e.g. uint92.

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

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 downcast of quorum votes to uint88 causes an overflow when the total supply is large enough, resulting in a much smaller quorumVotes value.

Impact

The whole balance of the CollectionShutdown contract can be drained, causing unbounded losses to the protocol.

What the attacker needed

Collection token denomination is high (e.g., 9); attacker holds some collection tokens; attacker can start collection shutdown

The fix

The quorumVotes variable should use a larger integer type, such as uint92, to prevent overflow.

How it was found

  1. Create a collection with denomination 9.
  2. Mint a small amount of collection tokens (less than one NFT) to an address.
  3. Start collection shutdown from that address, causing quorumVotes to overflow.
  4. Execute the shutdown and sell the NFTs on Sudoswap for a small amount of ETH.
  5. Claim the balance, receiving far more ETH than the sale proceeds.

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.

A smart contract calculates a voting threshold by multiplying a large number and then storing it in a small container, which overflows and wraps around to a tiny value. Because the payout formula divides by this threshold, the attacker receives far more money than they should, emptying the contract.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by 0xc0ffEE

View all

27 more disclosed reports, best first.

More from Flayer

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.