Calling swapAVAXForExactTokens function while sending excess amount cannot refund such excess amount
The swapAVAXForExactTokens function is supposed to refund any excess AVAX sent, but the refund calculation subtracts the sent amount from the required amount, causing an underflow revert whenever more than the required amount is sent. An attacker could not exploit this for gain, but ordinary users sending extra AVAX would have their transactions fail.
When calling the swapAVAXForExactTokens function, if (msg.value > amountsIn[0]) _safeTransferAVAX(_to, amountsIn[0] - msg.value) is executed, which is for refunding any excess amount sent in; this is confirmed by this function’s comment as well. However, executing amountsIn[0] - msg.value will always revert when msg.value > amountsIn[0] is true. Developers who has the design of the swapAVAXForExactTokens function in mind could develop front-ends and contracts that will send excess amount when calling the swapAVAXForExactTokens function. Hence, the users, who rely on these front-ends and contracts for interacting with the swapAVAXForExactTokens function will always find such interactions being failed since calling this function with the excess amount will always revert. As a result, the user experience becomes degraded, and the usability of the protocol becomes limited.
The finding is valid, sending more than necessary will revert.
No loss to end-users was shown, and the revert will prevent the tx from finishing.
Because of that, considering the fact that a accurate measure could be calculated, meaning that functionality can be side-stepped, despite recommending fixing, I think the finding is QA - Low Severity as the revert is self-inflicted.
Per discussion from backstage I’ve re-reviewed the finding.
I have poked around with the test provided and tweaked it a little, this is the last one I have
function testSwapAVAXForExactTokensIsUnableToRefund() public {
uint256 amountOut = 1e18;
(uint256 amountIn, ) = router.getSwapIn(pairWavax, amountOut, false);
IERC20[] memory tokenList = new IERC20[](2);
tokenList[0] = wavax;
tokenList[1] = token6D;
uint256[] memory pairVersions = new uint256[](1);
pairVersions[0] = DEFAULT_BIN_STEP;
vm.deal(DEV, 50 * amountIn + 500);
// Although the swapAVAXForExactTokens function supposes to refund any excess sent,
// calling it reverts when sending more than amountIn
// because executing _safeTransferAVAX(_to, amountsIn[0] - msg.value) results in arithmetic underflow
vm.expectRevert(stdError.arithmeticError);
router.swapAVAXForExactTokens{value: amountIn + 1}(amountOut, pairVersions, tokenList, DEV, block.timestamp);
router.swapAVAXForExactTokens{value: amountIn}(amountOut, pairVersions, tokenList, DEV, block.timestamp);
vm.expectRevert(stdError.arithmeticError);
router.swapAVAXForExactTokens{value: amountIn + 100000}(amountOut, pairVersions, tokenList, DEV, block.timestamp);
}
Ultimately we can conclude that any amount below amountIn will result in a revert due to insufficient amountOut, and any amount above amountIn will revert due to the highlighted overflow.
The function is a routing function, which is meant to allow some degree of slippage.
My original downgrade was based on the idea that the function can run, however, after it being flagged, I must agree that the availability of the function is impaired in the majority of ordinary cases (a .10% / .5% slippage is expected under most operations / FEs).
For this reason, I agree with upgrading back to Medium Severity.
I’d like to thank @shung and @hyh for their feedback.
The original report as published on Code4rena, by rbserver. 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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Impact
Users sending more than the required AVAX will have their transactions revert, degrading user experience and limiting protocol usability; no loss of funds was shown.
What the attacker needed
User sends more AVAX than required; calling swapAVAXForExactTokens
The fix
The refund calculation should be corrected to subtract the required amount from the sent amount.
How it was found
Calculate the required input amount for a desired output using getSwapIn.
Prepare token list and pair bin steps for the swap.
Send more AVAX than the required input amount when calling swapAVAXForExactTokens.
Observe that the transaction reverts with an arithmetic underflow error.
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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
When you call a function that swaps one cryptocurrency for another, you might send a bit more than needed, expecting the extra to be returned. Here, the code that returns the extra does the subtraction in the wrong order, so it tries to subtract a larger number from a smaller one. That causes the whole transaction to fail, so you lose nothing but the swap never happens.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.