Missing slippage protection on buy and sell
The buy and sell functions in the FRouter contract do not check the minimum amount of tokens a user receives, so trades can execute at a much worse price than expected. An attacker could front-run a transaction to manipulate reserves and cause the user to receive significantly fewer tokens.
Submitted by EPSec, also found by 0x1982us, 0x60scs, 0xbc000, 0xterrah, adam-idarrha, b1n4ry, bareli, BlackAdam, BowTiedOriole, bytesguard, chupinexx, classic-k, CoheeYang, Damola0x, danzero, Ejineroz, Eniwealth, gregom, harsh123, jamshed, Jeremias, Kweks, mihailvichev, ngo, NHristov, PolarizedLight, PotEater, pro_king, Rorschach, SeveritySquad, Shinobi, shui, slowbugmayor, ThanatOS, TheCarrot, Vemus, YouCrossTheLineAlfie, and zubyoz
Finding description
The sell and buy functions in the FRouter contract lack a slippage check to ensure that the amountOut (the amount of tokens received after the trade) is not less than a certain percentage of the expected amountOut. Slippage occurs when the price of a token changes between the time a transaction is submitted and when it is executed, often due to low liquidity or high volatility.
Impact
- User Losses: Without a slippage check, users may receive significantly fewer tokens than expected, especially in volatile markets or low-liquidity pools.
- Front-Running Attacks: Malicious actors can exploit the lack of a slippage check by front-running transactions, manipulating the reserves to cause unfavorable price changes for the user.
- Reduced Trust: Users may lose confidence in the platform if they experience unexpected losses due to slippage, harming the platform’s reputation.
- Economic Exploits: Arbitrageurs or bots could exploit the lack of slippage protection to profit at the expense of users, draining liquidity from the pool.
Proof of Concept
- Deploy the
FRoutercontract and initialize it with a factory and asset token. - Set up a pair with low liquidity between
TokenAand the asset token. - Call the
sellorbuyfunction with a largeamountInduring a period of high volatility or low liquidity. - Observe that the
amountOutreceived is significantly lower than expected, with no mechanism to revert the transaction.
Recommended Mitigation Steps
To address this issue, add a slippage check in both the sell and buy functions. The functions should accept a minAmountOut parameter, which specifies the minimum acceptable amountOut. If the actual amountOut is less than minAmountOut, the transaction should revert.
Example fix for the sell function:
function sell(
uint256 amountIn,
address tokenAddress,
address to,
uint256 minAmountOut // Add a parameter for slippage protection
) public nonReentrant onlyRole(EXECUTOR_ROLE) returns (uint256, uint256) {
require(tokenAddress != address(0), "Zero addresses are not allowed.");
require(to != address(0), "Zero addresses are not allowed.");
address pairAddress = factory.getPair(tokenAddress, assetToken);
IFPair pair = IFPair(pairAddress);
IERC20 token = IERC20(tokenAddress);
uint256 amountOut = getAmountsOut(tokenAddress, address(0), amountIn);
// Ensure the amountOut is greater than or equal to minAmountOut
require(amountOut >= minAmountOut, "Slippage exceeded");
token.safeTransferFrom(to, pairAddress, amountIn);
uint fee = factory.sellTax();
uint256 txFee = (fee * amountOut) / 100;
uint256 amount = amountOut - txFee;
address feeTo = factory.taxVault();
pair.transferAsset(to, amount);
pair.transferAsset(feeTo, txFee);
pair.swap(amountIn, 0, 0, amountOut);
if (feeTo == taxManager) {
IBondingTax(taxManager).swapForAsset();
}
return (amountIn, amountOut);
}
Similarly, apply the same logic to the buy function by adding a minAmountOut parameter and checking it against the calculated amountOut. This will protect users from unexpected losses due to slippage and improve the overall trading experience.
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 could receive significantly fewer tokens than expected, and malicious actors could front-run transactions to profit at the user's expense.
What the attacker needed
Low liquidity or high volatility; attacker can front-run transactions
The fix
The functions should accept a minAmountOut parameter and revert if the actual amountOut is less than that minimum.
How it was found
- Deploy the FRouter contract and initialise it with a factory and asset token.
- Set up a pair with low liquidity between TokenA and the asset token.
- Call the sell or buy function with a large amountIn during a period of high volatility or low liquidity.
- Observe that the amountOut received is significantly lower than expected, with no mechanism to revert the transaction.
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 trade tokens on a decentralised exchange, the price can change between the moment you send your transaction and the moment it is processed. Without a rule that says 'I will not accept less than X tokens', you might get a much worse deal. This report says the contract has no such rule, so users can lose money.
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.