Skip to content
Business logic Audit finding Intermediate

User can pay less protected listing fees

The unlockProtectedListing function decreases the listing count before creating a checkpoint, so the utilisation rate used for the checkpoint is incorrect. When a user unlocks multiple protected listings, the fee for the second and later listings is lower than it should be, causing a loss of funds for the protocol.

dany.armstrong90 Follow with jsmi

Originally disclosed on Sherlock

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

Found by

dany.armstrong90, jsmi

Summary

ProtectedListings.unlockProtectedListing() function create checkpoint after decrease listingCount[_collection]. Therefore, when user unlock multiple protected listings, user will pay less fees for the second and thereafter listings than the first listing.

Vulnerability Detail

ProtectedListings.unlockProtectedListing() function is following.

Solidity
    function unlockProtectedListing(address _collection, uint _tokenId, bool _withdraw) public lockerNotPaused {
        // Ensure this is a protected listing
        ProtectedListing memory listing = _protectedListings[_collection][_tokenId];

        // Ensure the caller owns the listing
        if (listing.owner != msg.sender) revert CallerIsNotOwner(listing.owner);

        // Ensure that the protected listing has run out of collateral
        int collateral = getProtectedListingHealth(_collection, _tokenId);
        if (collateral < 0) revert InsufficientCollateral();

        // cache
        ICollectionToken collectionToken = locker.collectionToken(_collection);
        uint denomination = collectionToken.denomination();
        uint96 tokenTaken = _protectedListings[_collection][_tokenId].tokenTaken;

        // Repay the loaned amount, plus a fee from lock duration
        uint fee = unlockPrice(_collection, _tokenId) * 10 ** denomination;
        collectionToken.burnFrom(msg.sender, fee);

        // We need to burn the amount that was paid into the Listings contract
        collectionToken.burn((1 ether - tokenTaken) * 10 ** denomination);

        // Remove our listing type
311:    unchecked { --listingCount[_collection]; }

        // Delete the listing objects
        delete _protectedListings[_collection][_tokenId];

        // Transfer the listing ERC721 back to the user
        if (_withdraw) {
            locker.withdrawToken(_collection, _tokenId, msg.sender);
            emit ListingAssetWithdraw(_collection, _tokenId);
        } else {
            canWithdrawAsset[_collection][_tokenId] = msg.sender;
        }

        // Update our checkpoint to reflect that listings have been removed
325:    _createCheckpoint(_collection);

        // Emit an event
        emit ListingUnlocked(_collection, _tokenId, fee);
    }

As can be seen, the above function decrease listingCount[_collection] in L311 before creating checkpoint in L325. However, creating checkpoint uses utilization rate and the utilization rate depends on listingCount[_collection]. Since listingCount[_collection] is already decreased at L311, the utilization rate is calculated incorrect and so the checkpoint will be incorrect.

PoC: Add the following test code into ProtectedListings.t.sol.

Solidity
    function test_unlockProtectedListingError() public {
        erc721a.mint(address(this), 0);
        erc721a.mint(address(this), 1);
        
        erc721a.setApprovalForAll(address(protectedListings), true);

        uint[] memory _tokenIds = new uint[](2); _tokenIds[0] = 0; _tokenIds[1] = 1;

        // create protected listing for tokenId = 0 and tokenId = 1
        IProtectedListings.CreateListing[] memory _listings = new IProtectedListings.CreateListing[](1);
        _listings[0] = IProtectedListings.CreateListing({
            collection: address(erc721a),
            tokenIds: _tokenIds,
            listing: IProtectedListings.ProtectedListing({
                owner: payable(address(this)),
                tokenTaken: 0.4 ether,
                checkpoint: 0
            })
        });
        protectedListings.createListings(_listings);

        vm.warp(block.timestamp + 7 days);

        // unlock protected listing for tokenId = 0
        assertEq(protectedListings.unlockPrice(address(erc721a), 0), 402485479451875840);
        locker.collectionToken(address(erc721a)).approve(address(protectedListings), 402485479451875840);
        protectedListings.unlockProtectedListing(address(erc721a), 0, true);

        // unlock protected listing for tokenId = 0, but the unlock price for tokenId = 1 is 402055890410801920 < 402485479451875840 for tokenId = 0.
        assertEq(protectedListings.unlockPrice(address(erc721a), 1), 402055890410801920);
        locker.collectionToken(address(erc721a)).approve(address(protectedListings), 402055890410801920);
        protectedListings.unlockProtectedListing(address(erc721a), 1, true);
    }

In the above test code, we can see that user paid less fees for tokenId = 1 than tokenId = 0.

Impact

Users will pay less fees. It meanas loss of funds for the protocol.

Code Snippet

https://github.com/sherlock-audit/2024-08-flayer/blob/main/flayer/src/contracts/ProtectedListings.sol#L287-L329

Tool used

Manual Review

Recommendation

Change the order of decreasing listingCount[_collection] and creating checkpoint in ProtectedListings.unlockProtectedListing() function as follows.

Solidity
    function unlockProtectedListing(address _collection, uint _tokenId, bool _withdraw) public lockerNotPaused {
        // Ensure this is a protected listing
        ProtectedListing memory listing = _protectedListings[_collection][_tokenId];

        // Ensure the caller owns the listing
        if (listing.owner != msg.sender) revert CallerIsNotOwner(listing.owner);

        // Ensure that the protected listing has run out of collateral
        int collateral = getProtectedListingHealth(_collection, _tokenId);
        if (collateral < 0) revert InsufficientCollateral();

        // cache
        ICollectionToken collectionToken = locker.collectionToken(_collection);
        uint denomination = collectionToken.denomination();
        uint96 tokenTaken = _protectedListings[_collection][_tokenId].tokenTaken;

        // Repay the loaned amount, plus a fee from lock duration
        uint fee = unlockPrice(_collection, _tokenId) * 10 ** denomination;
        collectionToken.burnFrom(msg.sender, fee);

        // We need to burn the amount that was paid into the Listings contract
        collectionToken.burn((1 ether - tokenTaken) * 10 ** denomination);

        // Remove our listing type
--      unchecked { --listingCount[_collection]; }

        // Delete the listing objects
        delete _protectedListings[_collection][_tokenId];

        // Transfer the listing ERC721 back to the user
        if (_withdraw) {
            locker.withdrawToken(_collection, _tokenId, msg.sender);
            emit ListingAssetWithdraw(_collection, _tokenId);
        } else {
            canWithdrawAsset[_collection][_tokenId] = msg.sender;
        }

        // Update our checkpoint to reflect that listings have been removed
        _createCheckpoint(_collection);
++      unchecked { --listingCount[_collection]; }

        // Emit an event
        emit ListingUnlocked(_collection, _tokenId, fee);
    }

The original report as published on Sherlock, by dany.armstrong90. 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

The function decrements listingCount before calling _createCheckpoint, so the checkpoint uses a lower listing count and thus an incorrect utilisation rate.

Impact

Users pay less in fees than they should, which means a loss of funds for the protocol.

What the attacker needed

Own at least two protected listings; wait until listings can be unlocked

The fix

The order should be changed so that the checkpoint is created before decrementing listingCount.

How it was found

  1. Mint two NFTs and approve the protected listings contract to manage them.
  2. Create protected listings for both token IDs with the same tokenTaken amount.
  3. Advance time by 7 days.
  4. Unlock the first listing and observe the unlock price.
  5. Unlock the second listing and observe that its unlock price is lower than the first.

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 charges a fee to unlock a protected listing, and that fee depends on how many listings exist. The contract updates the count before recalculating the fee, so the fee is calculated using the wrong number. This lets someone pay less than they should when unlocking several listings in a row.

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.

More by dany.armstrong90

View all

4 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.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.