Function _execBuyNftFromMarket() fails to check the actual ETH Balance in the contract after executing the trade
The function _execBuyNftFromMarket() checks only ETH balance changes, not WETH, so leftover WETH approved to the marketplace can be stolen by another user. An attacker could set amount to zero and use the leftover WETH to buy an NFT, effectively stealing that WETH.
minhquanym Follow with adriro
Originally disclosed on Code4rena
Submitted by minhquanym, also found by adriro
In the function _execBuyNftFromMarket(), if the user chooses to use WETH, the function deposits ETH and approves the amount of WETH to the marketplace. After executing the trade at the marketplace, the function checks that the balance decrease is correct in the end. However, this check only accounts for ETH changes, not WETH changes, which is incorrect. If the trade did not use the full amount of WETH approved to the marketplace, some leftover WETH will remain in the contract. This amount of WETH/ETH will be locked in the contract, even though it should belong to the borrower who was able to get a good offer to buy the NFT at a lower price.
if (useToken == 0) {
// use ETH
// solhint-disable-next-line avoid-low-level-calls
(success, ) = marketplace.call{value: amount}(tradeData);
} else if (useToken == 1) {
// use WETH
weth.deposit{value: amount}();
weth.approve(marketplace, amount);
// solhint-disable-next-line avoid-low-level-calls
// @audit might not use all amount approved, cause ETH to locked in the protocol
(success, ) = marketplace.call(tradeData);
}
if (!success) {
revert Errors.MartketplaceFailedToTrade();
}
// verify that the declared NFT is acquired and the balance decrease is correct
if (IERC721(collection).ownerOf(tokenId) != address(this) || balanceBefore - address(this).balance != amount) {
revert Errors.InvalidNFTBuy();
}
Proof of Concept
Consider the following scenario:
- Alice (borrower 1) calls
buyNftFromMarket()to acquire an NFT with a price of 100 WETH. However, she sets the amount to 105 WETH, so the contract deposits and approves 105 WETH to the marketplace. After the trade, there are still 5 WETH approved to the marketplace. - Bob (borrower 2) sees the opportunity. He has a much cheaper lien, so he also calls
buyNftFromMarket()to acquire an NFT with a price of 5 WETH. He specifies theuseToken = 0. However, he sets theamount = 0and actually uses the 5 WETH left in step 1 of Alice to acquire the NFT. The result is Bob is able to steal 5 WETH approved to the marketplace.
Recommended Mitigation Steps
Consider accounting for the WETH when checking balance changes in _execBuyNftFromMarket().
Assessed type
Invalid Validation
Marked as primary to credit pointing out an interesting scenario in the PoC. Mitigation is well written at #24.
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 balance check after the trade only compares ETH balances and ignores WETH balances, so leftover WETH approved to the marketplace is not detected.
Impact
An attacker could steal leftover WETH that remains approved to the marketplace after a trade, as demonstrated in the proof of concept.
What the attacker needed
A prior trade leaves leftover WETH approved; Attacker can call buyNftFromMarket()
The fix
The function should account for WETH when checking balance changes in _execBuyNftFromMarket().
How it was found
- Alice calls buyNftFromMarket() with amount 105 WETH for an NFT priced at 100 WETH, depositing and approving 105 WETH to the marketplace.
- After the trade, 5 WETH remain approved to the marketplace.
- Bob calls buyNftFromMarket() with useToken = 0 and amount = 0 for an NFT priced at 5 WETH.
- Bob's trade uses the 5 WETH left from Alice, allowing him to acquire the NFT without spending his own funds.
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 you buy something with a token like WETH, you often approve a spending limit. If the actual price is lower, the leftover approval stays. Here, the contract only checked its ETH balance, not its WETH balance, so it missed that leftover WETH was still approved. Another user could then spend that leftover WETH to buy an NFT for themselves.
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.