Skip to content
Business logic Audit finding Intermediate

_deductFees() is incompatible with tokens that revert on zero value transfers

The fee-deduction helper computes a fee amount that can round down to zero for small amounts, then still calls a token transfer for that zero value. On tokens that revert on zero-value transfers, this makes the main functionality of the affected contracts revert.

d3e4 Follow

Originally disclosed on Code4rena

Submitted by d3e4

All main functionality risks reverting with tokens that revert on zero value transfers, via a transfer in Common._deductFees().

Proof of Concept

In Common._deductFees()

if (params.feeX64 == 0) {
    revert NoFees();
}

if (params.amount0 > 0) {
    feeAmount0 = FullMath.mulDiv(params.amount0, params.feeX64, Q64);
    amount0Left = params.amount0 - feeAmount0;
    SafeERC20.safeTransfer(IERC20(params.token0), FEE_TAKER, feeAmount0);
}
if (params.amount1 > 0) {
    feeAmount1 = FullMath.mulDiv(params.amount1, params.feeX64, Q64);
    amount1Left = params.amount1 - feeAmount1;
    SafeERC20.safeTransfer(IERC20(params.token1), FEE_TAKER, feeAmount1);
}
if (params.amount2 > 0) {
    feeAmount2 = FullMath.mulDiv(params.amount2, params.feeX64, Q64);
    amount2Left = params.amount2 - feeAmount2;
    SafeERC20.safeTransfer(IERC20(params.token2), FEE_TAKER, feeAmount2);
}

if 0 < params.feeX64 * params.amountX < Q64 then feeAmountX = 0. In this case the SafeERC20.safeTransfer() reverts on a token which reverts on zero transfers.

_deductFees() is used throughout the functionality of V3Automation and V3Utils.

Recommended Mitigation Steps

if (feeAmount0 > 0) {
    SafeERC20.safeTransfer(IERC20(params.token0), FEE_TAKER, feeAmount0);
}

etc.

Assessed type

ERC20

Haupc (Krystal DeFi) confirmed


The original report as published on Code4rena, by d3e4. 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 code performs a token transfer unconditionally whenever the input amount is positive, without checking that the computed fee amount is greater than zero.

Impact

All main functionality of V3Automation and V3Utils could revert for tokens that revert on zero-value transfers, whenever the fee rounds down to zero.

What the attacker needed

Token reverts on zero-value transfers; fee rounds down to zero for the amount

The fix

The transfer should be wrapped in a check that the computed fee amount is greater than zero before calling safeTransfer.

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 tokens refuse to process a transfer of zero tokens and throw an error instead. Here the code works out a fee that can round down to nothing, but still tries to send that nothing, so the whole operation fails. That means normal use of the contracts breaks for 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 d3e4

View all

19 more disclosed reports, best first.

More from Krystal DeFi Invitational

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.