saleReceiver and feeReceiver can steal refunds after sale has ended
After the last NFT in a Dutch auction was sold, the contract still allowed a zero-quantity buy at an arbitrary price, which re-triggered the end-of-sale payout logic. The sale receiver or fee receiver could use this to drain the overpayments that buyers were owed as refunds.
immeas Follow with AkshaySrivastav, cryptonue and 17 others
Originally disclosed on Code4rena
Submitted by immeas, also found by AkshaySrivastav, cryptonue, saian, minhquanym, 0xDecorativePineapple, gzeon, ey88, wait, pauliax, HollaDieWaldfee, jayphbee, bin2chen, evan, reassor, fs0c, hihen, hansfriese, 0x52, and aviggiano
https://github.com/code-423n4/2022-12-escher/blob/main/src/minters/LPDA.sol#L67-L68
https://github.com/code-423n4/2022-12-escher/blob/main/src/minters/LPDA.sol#L81-L88
First, lets go over how a buy happens.
A buyer can buy NFTs at a higher price and then once the auction ends they can use refund() to return the over payments. The effect is that they bought the NFTs at the lowest price (Lowest Price Dutch Auction).
Now, let’s move on to what happens when the sale ends:
The sale is considered ended when the last NFT is sold which triggers the payout to the seller and fee collector:
81: if (newId == temp.finalId) {
82: sale.finalPrice = uint80(price);
83: uint256 totalSale = price * amountSold;
84: uint256 fee = totalSale / 20;
85: ISaleFactory(factory).feeReceiver().transfer(fee);
86: temp.saleReceiver.transfer(totalSale - fee);
87: _end();
88: }
Earlier there’s also a check that you cannot continue buying once the currentId has reached finalId:
67: uint48 newId = amount + temp.currentId;
68: require(newId <= temp.finalId, "TOO MANY");
However, it is still possible to buy 0 NFTs for whichever price you want even after the sale has ended. Triggering the “end of sale” snippet again, since newId will still equal temp.finalId.
The attacker, saleReceiver (or feeReceiver), buys 0 NFTs for the delta between totalSale and the balance still in the contract (the over payments by buyers). If there is more balance in the contract than totalSales this can be iterated until the contract is empty.
The attacker has then stolen the over payments from the buyers.
A buyer can mitigate this by continuously calling refund() as the price lowers but that would incur a high gas cost.
Impact
saleReceiver or feeReceiver can steal buyers over payments after the sale has ended. Who gains the most depends on circumstances in the auction.
Proof of Concept
PoC test in test/LPDA.t.sol:
function test_BuyStealRefund() public {
sale = LPDA(lpdaSales.createLPDASale(lpdaSale));
edition.grantRole(edition.MINTER_ROLE(), address(sale));
// buy most nfts at a higher price
sale.buy{value: 9 ether}(9);
// warp to when price is lowest
vm.warp(block.timestamp + 1 days);
uint256 price = sale.getPrice(); // 0.9 eth
// buy last nft at lowest possible price
sale.buy{value: price}(1);
uint256 contractBalanceAfterEnd = address(sale).balance;
uint256 receiverBalanceAfterEnd = address(69).balance;
console.log("Sale end");
console.log("LPDA contract",contractBalanceAfterEnd); // ~ 0.9 eth
console.log("saleReceiver ",receiverBalanceAfterEnd); // ~9 - fee eth
// buy 0 nfts for the totalSales price - current balance
// totalSales: 9 eth - contract balance 0.9 eth = ~8.1 eth
uint256 totalSale = price * 10;
uint256 delta = totalSale - contractBalanceAfterEnd;
sale.buy{value: delta}(0);
console.log("after steal");
console.log("LPDA contract",address(sale).balance);
console.log("saleReceiver ",address(69).balance - receiverBalanceAfterEnd - delta); // ~0.45 eth stolen by receiver, 0.45 eth to fees
// buyer supposed to get back the ~0.9 eth
vm.expectRevert(); // EvmError: OutOfFund
sale.refund(); // nothing to refund
}
Tools Used
VS Code, Forge
Recommended Mitigation Steps
I can think of different options of how to mitigate this:
- Don’t allow buying 0 NFTs
- Don’t allow buying if
newId == finalIdsince the sale has ended
mehtaculous (Escher) disagreed with severity
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
The buy function only checked that the new item count did not exceed the final count, so a zero-amount buy after the sale ended still satisfied the condition and re-ran the payout branch.
Impact
The sale receiver or fee receiver could steal buyers' overpayments after the sale ended, with the split depending on the auction's circumstances.
What the attacker needed
Attacker is the saleReceiver or feeReceiver; auction has ended with overpayments held; contract balance exceeds total sale value
The fix
The contract should reject zero-quantity buys and reject any buy once the new item count equals the final count, since the sale has then ended.
How it was found
- Buy most of the NFTs at a higher price while the auction is running.
- Wait until the price has fallen to its lowest level.
- Buy the final NFT at the lowest price, which ends the sale and pays out the seller and fee collector.
- Call buy with a quantity of zero and send an amount equal to the total sale value minus the contract balance.
- Repeat until the contract balance is empty, then observe that a buyer's refund call fails.
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 a Dutch auction the price falls over time, so buyers often pay more early and get the difference back later through a refund function. The contract's check for 'the sale is over' only looked at how many items had been sold, not whether any were being bought, so a zero-item purchase still ran the payout code. That let the people who receive sale proceeds take the money set aside for those refunds.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.