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.
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.
// 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.
@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:
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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
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
Alice buys the final NFT, triggering selfdestruct and destroying the contract.
Bob's purchase transaction is executed after the contract is destroyed.
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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
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.