Skip to content
Business logic Audit finding Intermediate

Transaction revert if the baseToken does not support 0 value transfer when charging changeFee

The PrivatePool contract charges a change fee and a flash loan fee by transferring the fee amount from the caller, even when the calculated fee is zero. If the base token is an ERC20 that reverts on zero-value transfers, the transaction fails, preventing users from using the change or flash loan features.

ladboy233 Follow with adriro, peanuts and 3 others

Originally disclosed on Code4rena

Submitted by ladboy233, also found by adriro, peanuts, jpserrat, 0xLanterns, and chaduke

https://github.com/code-423n4/2023-04-caviar/blob/cd8a92667bcb6657f70657183769c244d04c015c/src/PrivatePool.sol#L423

https://github.com/code-423n4/2023-04-caviar/blob/cd8a92667bcb6657f70657183769c244d04c015c/src/PrivatePool.sol#L651

When call change via the PrivatePool.sol, the caller needs to the pay the change fee,

	// calculate the fee amount
	(feeAmount, protocolFeeAmount) = changeFeeQuote(inputWeightSum);
}

// ~~~ Interactions ~~~ //

if (baseToken != address(0)) {
	// transfer the fee amount of base tokens from the caller
	ERC20(baseToken).safeTransferFrom(
		msg.sender,
		address(this),
		feeAmount
	);

calling changeFeeQuote(inputWeightSum)

function changeFeeQuote(
	uint256 inputAmount
) public view returns (uint256 feeAmount, uint256 protocolFeeAmount) {
	// 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;

	feeAmount = (inputAmount * feePerNft) / 1e18;
	protocolFeeAmount =
		(feeAmount * Factory(factory).protocolFeeRate()) /
		10_000;
}

if the feeAmount is 0,

the code below would revert if the ERC20 token does not support 0 value transfer

ERC20(baseToken).safeTransferFrom(
	msg.sender,
	address(this),
	feeAmount
);

According to https://github.com/d-xo/weird-erc20#revert-on-zero-value-transfers

Some tokens (e.g. LEND) revert when transferring a zero value amount.

Same issue happens when charging the flashloan fee

    function flashLoan(
        IERC3156FlashBorrower receiver,
        address token,
        uint256 tokenId,
        bytes calldata data
    ) external payable returns (bool) {
        // check that the NFT is available for a flash loan
        if (!availableForFlashLoan(token, tokenId))
            revert NotAvailableForFlashLoan();

        // calculate the fee
        uint256 fee = flashFee(token, tokenId);

        // if base token is ETH then check that caller sent enough for the fee
        if (baseToken == address(0) && msg.value < fee)
            revert InvalidEthAmount();

        // transfer the NFT to the borrower
        ERC721(token).safeTransferFrom(
            address(this),
            address(receiver),
            tokenId
        );

        // call the borrower
        bool success = receiver.onFlashLoan(
            msg.sender,
            token,
            tokenId,
            fee,
            data
        ) == keccak256("ERC3156FlashBorrower.onFlashLoan");

        // check that flashloan was successful
        if (!success) revert FlashLoanFailed();

        // transfer the NFT from the borrower
        ERC721(token).safeTransferFrom(
            address(receiver),
            address(this),
            tokenId
        );

        // transfer the fee from the borrower
        if (baseToken != address(0))
            ERC20(baseToken).transferFrom(msg.sender, address(this), fee);

        return success;
    }

Note the code:

if (baseToken != address(0))
            ERC20(baseToken).transferFrom(msg.sender, address(this), fee);

If the fee is 0 and baseToken revert in 0 value transfer, the user cannot use flashloan.

Recommended Mitigation Steps

We recommend the protocol check if the feeAmount is 0 before performing transfer.

if(feeAmount > 0) {
	ERC20(baseToken).safeTransferFrom(
		msg.sender,
		address(this),
		feeAmount
	);
}

outdoteth (Caviar) acknowledged

Alex the Entreprenerd (judge) commented:

The Warden has shown an edge case, when fee are 0 the call to safeTransfer is still performed, this can cause certain ERC20s to revert.

Because the PrivatePools are meant to work with ERC20s, and this revert is conditional on the specific token implementation, I agree with Medium Severity.


The original report as published on Code4rena, by ladboy233. 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

Impact

Users cannot use the change or flash loan features when the base token reverts on zero-value transfers, because the transaction reverts.

What the attacker needed

Base token reverts on zero-value transfers; calculated fee is zero

The fix

The protocol should check that the fee amount is greater than zero before performing the transfer.

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.

Some digital tokens (ERC20s) are programmed to reject any transfer of zero tokens, which causes an error. The pool always tries to collect a fee, even when the fee works out to zero, so with such tokens the whole action fails. This means users cannot use the pool's change or flash loan features with those tokens.

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 ladboy233

View all

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