Skip to content
Smart contracts Audit finding Intermediate

selfdestruct may cause the funds to be lost

The FixedPrice and OpenEdition contracts use selfdestruct to end a sale, which destroys the contract code. If a buyer sends a transaction after the contract is destroyed, the transaction succeeds but the sent value is locked forever in the empty contract address.

bin2chen Follow with pauliax

Originally disclosed on Code4rena

Submitted by bin2chen, also found by pauliax

https://github.com/code-423n4/2022-12-escher/blob/5d8be6aa0e8634fdb2f328b99076b0d05fefab73/src/minters/FixedPrice.sol#L110

https://github.com/code-423n4/2022-12-escher/blob/5d8be6aa0e8634fdb2f328b99076b0d05fefab73/src/minters/OpenEdition.sol#L122

Impact

After the contract is destroyed, the subsequent execution of the contract’s #buy() will always succede, the msg.value will be locked in this address.

Proof of Concept

When FixedPrice.sol and OpenEdition.sol are finished, selfdestruct() will be executed to destroy the contract.

But there is a problem with this:

Suppose when Alice and Bob execute the purchase transaction at the same time, the transaction is in the memory pool (or Alice executes the transaction, but Bob is still operating the purchase in the UI, the UI does not know that the contract has been destroyed)

If Alice meets the finalId, the contract is destroyed after her transaction ends.

Note: “When there is no code at the address, the transaction will succeed, and the msg.value will be stored in the contract. Although no code is executed.”

After that, Bob’s transaction will be executed.

This way the msg.value passed by Bob is lost and locked forever in the address of this empty code.

Suggestion: don’t use selfdestruct, use modified state to represent that the contract has completed the sale.

Recommended Mitigation Steps

contract FixedPrice is Initializable, OwnableUpgradeable, ISale {
...
    function _end(Sale memory _sale) internal {
        emit End(_sale);
        ISaleFactory(factory).feeReceiver().transfer(address(this).balance / 20);
-       selfdestruct(_sale.saleReceiver);
+       sale.finalId = sale.currentId
+       sale.saleReceiver.transfer(address(this).balance);

    }

berndartmueller (judge) commented:

The warden demonstrates an issue that leads to loss of user funds in case the contract is destroyed with selfdestruct.

This clearly differentiates from the other submissions outlining the deprecated use of selfdestruct due to EIP-4758.

stevennevins (Escher) disputed and commented:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import "forge-std/Test.sol";
import {EscherTest, FixedPrice, FixedPriceFactory} from "test/FixedPrice.t.sol";

contract Issue296Test is EscherTest {
    FixedPrice.Sale public fixedSale;
    FixedPriceFactory public fixedSales;

    FixedPrice public sale;

    function setUp() public virtual override {
        super.setUp();
        fixedSales = new FixedPriceFactory();
        fixedSale = FixedPrice.Sale({
            currentId: uint48(0),
            finalId: uint48(10),
            edition: address(edition),
            price: uint96(uint256(1 ether)),
            saleReceiver: payable(address(69)),
            startTime: uint96(block.timestamp)
        });
        sale = FixedPrice(fixedSales.createFixedSale(fixedSale));
        edition.grantRole(edition.MINTER_ROLE(), address(sale));
        sale.buy{value: 10 ether}(10);
    }

    function test_RevertsWhenAfterSelfDestruct_Buy() public {
        vm.expectRevert();
        sale.buy{value: 1 ether}(1);
    }
}

Any purchases after the selfdestruct would revert as shown above. In order to accurately mock this in Foundry, you can’t perform the destruction within a test function since everything would be one large transaction and the code would still exist as far as Foundry is concerned because it’s all in the same call context. So you have to move the purchase that selfdestructs the contract into the setup function to demonstrate the behavior.

berndartmueller (judge) commented:

@stevennevins You’re right with your Foundry example. However, there are subtle nuances to this that make this finding valid.

In your Foundry test, the Solidity function call via sale.buy{value: 1 ether}(1) at the end, uses the extcodesize opcode to check that the contract that is about to be called actually exists and causes an exception if it does not.

However, in the above demonstrated PoC, if a transaction gets executed after the contract got destroyed (due to buying the final token id), the transaction does not revert and any value sent along with the transaction will be stored in the contract.

I was able to reproduce this behavior with a minimal example contract on Goerli via Remix:

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.7;

contract Sale  {
    function buy() external payable {
        _end();
    }

    function _end() internal {
        selfdestruct(payable(msg.sender));
    }
}

Calling the Sale.buy() function in Remix twice, once with a value of 1337 wei and once with 333 wei:

  1. https://goerli.etherscan.io/tx/0x138d2c061ed547ec5bfd4d7d9b03b519f92b5f63a74b73c45a1af6efe202727c
  2. https://goerli.etherscan.io/tx/0x7ca9032cf79f0a8e29da17061e7eeafb46edfddff96633dc9bbf5957f1aff142

As you can see, the first transaction will destroy the contract, 1337 wei is sent to the beneficiary (caller). The second transaction succeeds as well. However, the value of 333 wei is kept at the address of the destroyed contract.

After many considerations, I consider High to be the appropriate severity. Multiple parallel transactions (e.g. front-runners) will likely try to buy the last NFT IDs, whereas all transactions executed after the last successful buy will render the sent msg.value locked in the destroyed contract. Those users lost funds while not receiving any NFTs.


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

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 use of selfdestruct to end the sale, which removes the contract code but leaves the address able to receive Ether without any logic to handle it.

Impact

Users who buy after the contract is destroyed lose their funds, which become permanently locked, and they receive no NFT.

What the attacker needed

Contract uses selfdestruct to end sale; buyer sends transaction after destruction

The fix

The contract should not use selfdestruct; instead, it should mark the sale as ended by updating state and transfer the balance to the sale receiver.

How it was found

  1. Alice buys the final NFT, triggering selfdestruct and destroying the contract.
  2. Bob's purchase transaction is executed after the contract is destroyed.
  3. Bob's transaction succeeds but his sent value is locked in the empty contract address.

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 can be programmed to delete itself using a command called selfdestruct. After that, the address has no code, but it can still receive money. If someone sends money to that address by calling a function that no longer exists, the money gets stuck because there is no code to send it back.

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 bin2chen

View all

139 more disclosed reports, best first.

More from Escher contest

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.