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