UniV2ClassDex.sol#uniClassSell() Tokens with fee on transfer are not fully supported
The uniClassSell function in a Uniswap V2-style DEX wrapper trusted the output amount calculated by getAmountOut instead of measuring the actual received amount, so tokens with transfer fees could cause the user to receive less than the specified minimum. An attacker could exploit this to cause value leakage through increased slippage.
Submitted by WatchPug
function uniClassSell(DexInfo memory dexInfo,
address buyToken,
address sellToken,
uint sellAmount,
uint minBuyAmount,
address payer,
address payee
) internal returns (uint buyAmount){
address pair = getUniClassPair(buyToken, sellToken, dexInfo.factory);
IUniswapV2Pair(pair).sync();
(uint256 token0Reserves, uint256 token1Reserves,) = IUniswapV2Pair(pair).getReserves();
sellAmount = transferOut(IERC20(sellToken), payer, pair, sellAmount);
uint balanceBefore = IERC20(buyToken).balanceOf(payee);
dexInfo.fees = getPairFees(dexInfo, pair);
if (buyToken < sellToken) {
buyAmount = getAmountOut(sellAmount, token1Reserves, token0Reserves, dexInfo.fees);
IUniswapV2Pair(pair).swap(buyAmount, 0, payee, "");
} else {
buyAmount = getAmountOut(sellAmount, token0Reserves, token1Reserves, dexInfo.fees);
IUniswapV2Pair(pair).swap(0, buyAmount, payee, "");
}
require(buyAmount >= minBuyAmount, 'buy amount less than min');
uint bought = IERC20(buyToken).balanceOf(payee).sub(balanceBefore);
return bought;
}
While uniClassBuy() correctly checks the actually received amount by comparing the before and after the balance of the receiver, uniClassSell() trusted the result given by getAmountOut(). This makes uniClassSell() can result in an output amount fewer than minBuyAmount.
Recommendation
Change to:
function uniClassSell(DexInfo memory dexInfo,
address buyToken,
address sellToken,
uint sellAmount,
uint minBuyAmount,
address payer,
address payee
) internal returns (uint bought){
address pair = getUniClassPair(buyToken, sellToken, dexInfo.factory);
IUniswapV2Pair(pair).sync();
(uint256 token0Reserves, uint256 token1Reserves,) = IUniswapV2Pair(pair).getReserves();
sellAmount = transferOut(IERC20(sellToken), payer, pair, sellAmount);
uint balanceBefore = IERC20(buyToken).balanceOf(payee);
dexInfo.fees = getPairFees(dexInfo, pair);
if (buyToken < sellToken) {
buyAmount = getAmountOut(sellAmount, token1Reserves, token0Reserves, dexInfo.fees);
IUniswapV2Pair(pair).swap(buyAmount, 0, payee, "");
} else {
buyAmount = getAmountOut(sellAmount, token0Reserves, token1Reserves, dexInfo.fees);
IUniswapV2Pair(pair).swap(0, buyAmount, payee, "");
}
uint bought = IERC20(buyToken).balanceOf(payee).sub(balanceBefore);
require(bought >= minBuyAmount, 'buy amount less than min');
}
ColaM12 (OpenLeverage) confirmed and resolved
0xleastwood (judge) commented:
Agree with this finding! Uniswap token swaps are meant to support all types of tokens. It does seem possible for there to be
payerto experience increased slippage because the check operates ongetAmountOut()and not theboughtoutput.It’s fair to say that this will lead to value leakage, so I think
mediumseverity is justified.
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 function used the theoretical output from getAmountOut for the minimum amount check instead of the actual balance change of the receiver.
Impact
Users selling fee-on-transfer tokens could receive less than their specified minimum buy amount, leading to value leakage through increased slippage.
What the attacker needed
Token with transfer fee; user sells such token via uniClassSell
The fix
The function should measure the actual received amount by comparing the buyToken balance before and after the swap, and use that for the minimum amount check.
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.
When you swap tokens on a decentralised exchange, you set a minimum amount you are willing to receive. Some tokens take a fee when transferred, so the amount you actually get is less than what the exchange calculates. This code checked the calculated amount against your minimum, not the real amount you received, so you could end up with less than you agreed to.
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.