Skip to content
Business logic Audit finding Intermediate

Flash loan fee is incorrect in Private Pool contract

The flash loan fee in the Private Pool contract was incorrectly set to the raw changeFee value without scaling, so a changeFee of 25 resulted in a flash loan fee of only 25 wei instead of the intended 0.0025 ETH. An attacker could borrow NFTs via flash loans for almost no fee.

adriro Follow with GT_Blockchain, Josiah and 20 others

Originally disclosed on Code4rena

Submitted by adriro, also found by GT_Blockchain, Josiah, Aymen0909, anodaram, KrisApostolov, minhtrng, rbserver, giovannidisiena, wintermute, jpserrat, 0xRobocop, 0xNorman, aviggiano, shaka, Voyvoda, bin2chen, RaymondFam, ElKu, sashik_eth, ToonVH, SpicyMeatball, and climber2002

Private Pools support NFT borrowing using flash loans. Users that decide to use this feature have to pay a flash loan fee to the owner of the pool.

The contract has a changeFee variable that is used to configure the fee for changing NFTs, and this variable is also used to determine the fee for flash loans. In the case of a change operation, the value is interpreted as an amount with 4 decimals, and the token is the base token of the pool. This means that, for example, if the base token is ETH, a changeFee value of 25 should be interpreted as a fee of 0.0025 ETH for change operation.

However, as we can see in this following snippet, the flashFee function just returns the value of changeFee without any scaling or modification.

https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L750-L752

750:     function flashFee(address, uint256) public view returns (uint256) {
751:         return changeFee;
752:     }

This means that, following the previous example, a changeFee value of 25 will result in 0.0025 ETH for change operation, but just 25 wei for flash loans.

The documentation hints that this value should also be scaled to 4 decimals in the case of the flash loan fee, but in any case this is clearly an incorrect setting of the flash loan fee.

Proof of Concept

In the following test, the pool is configured with a changeFee value of 25, and Alice is able to execute a flash loan by just paying 25 wei.

Note: the snippet shows only the relevant code for the test. Full test file can be found here.

function test_PrivatePool_flashLoan_IncorrectFee() public {
    // Setup pool
    PrivatePool privatePool = new PrivatePool(
        address(factory),
        address(royaltyRegistry),
        address(stolenNftOracle)
    );
    uint56 changeFee = 25;
    privatePool.initialize(
        address(0), // address _baseToken,
        address(milady), // address _nft,
        100e18, // uint128 _virtualBaseTokenReserves,
        10e18, // uint128 _virtualNftReserves,
        changeFee, // uint56 _changeFee,
        0, // uint16 _feeRate,
        bytes32(0), // bytes32 _merkleRoot,
        false, // bool _useStolenNftOracle,
        false // bool _payRoyalties
    );

    uint256 tokenId = 0;
    milady.mint(address(privatePool), tokenId);

    // Alice executes a flash loan
    vm.startPrank(alice);

    FlashLoanBorrower flashLoanBorrower = new FlashLoanBorrower();

    // Alice just sends 25 wei!
    vm.deal(alice, changeFee);
    privatePool.flashLoan{value: changeFee}(flashLoanBorrower, address(milady), tokenId, "");

    vm.stopPrank();
}

Recommended Mitigation Steps

The flashFee function should properly scale the value of the changeFee variable, similar to how it is implemented in changeFeeQuote.

outdoteth (Caviar) confirmed and mitigated:

Fixed in: https://github.com/outdoteth/caviar-private-pools/pull/6

Proposed fix is to exponentiate the changeFee to get the correct flashFee in the same way that changeFee is exponentiated in change().

function flashFee(address, uint256) public view returns (uint256) {
    // multiply the changeFee to get the fee per NFT (4 decimals of accuracy)
    uint256 exponent = baseToken == address(0) ? 18 - 4 : ERC20(baseToken).decimals() - 4;
    uint256 feePerNft = changeFee * 10 ** exponent;
    return feePerNft;
}

Alex the Entreprenerd (judge) commented:

First of all FlashLoan Fees don’t have to scale.

That said, the code and the codebase point to wanting to offer a fee that scales based on the amounts loaned. For this nuanced reason, given that the Sponsor has confirmed and mitigated with a scaling fee, I believe that the most appropriate severity is Medium.

Status: Mitigation confirmed. Full details in reports from rbserver, KrisApostolov, and rvierdiiev.


The original report as published on Code4rena, by adriro. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 flashFee function returned the changeFee variable directly without scaling it to account for the token's decimals, unlike the changeFeeQuote function which properly scales the fee.

Impact

The pool owner received far less fee than intended for flash loans, potentially allowing cheap borrowing of NFTs.

What the attacker needed

A PrivatePool with flash loans enabled; An NFT deposited in the pool; Ability to deploy a contract to receive the flash loan

The fix

The flashFee function should scale the changeFee value similarly to changeFeeQuote, by exponentiating it based on the base token's decimals.

How it was found

  1. Deploy a PrivatePool with a changeFee of 25.
  2. Mint an NFT to the pool.
  3. As Alice, deploy a FlashLoanBorrower contract.
  4. Call flashLoan on the pool with 25 wei as the fee.

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 flash loan is a loan that must be borrowed and repaid within the same transaction. The pool charged a fee for this service, but due to a mistake, the fee was calculated as a tiny number of wei (the smallest unit of ETH) instead of the intended amount. This meant users could borrow NFTs very cheaply, depriving the pool owner of fees.

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 adriro

View all

57 more disclosed reports, best first.

More from Caviar Private Pools

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.