Skip to content
Business logic Audit finding Intermediate

V3Proxy swapTokensForExactETH does not send back to the caller the unused input tokens

The V3Proxy swapTokensForExactETH function takes the maximum input amount from the caller but does not return the unused portion after the swap. An attacker could exploit this to lock excess tokens in the contract, causing users to lose funds.

3docSec Follow with Fulum, Limbooo and 3 others

Originally disclosed on Code4rena

Submitted by 3docSec, also found by Fulum, Limbooo, DanielArmstrong, T1MOH, and Krace

The V3Proxy swapTokensForExactETH function swaps an unspecified amount of a given ERC-20 for a specified amount of the native currency. After the swap happens, however, the difference between the amount taken from the caller (amountInMax) and the actual swapped amount (amounts[0]) is not given back to the caller and remains locked in the contract.

Impact

Any user of the swapTokensForExactETH will always pay amountInMax for swaps even if part of it was not used for the swap. This part is lost, locked in the V3Proxy contract.

Proof of Concept

  • Call swapTokensForExactETH with an excessively high amountInMax
  • Check that any extra input tokens are sent back - this check will fail
    function testV3ProxyKeepsTheChange() public {
        IQuoter q = IQuoter(0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6);
        ISwapRouter r = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);

        V3Proxy v3proxy = new V3Proxy(r, q, 500);
        vm.label(address(v3proxy), "V3Proxy");

        address[] memory path = new address[](2);
        path[0] = address(USDC);
        path[1] = address(WETH);

        address[] memory path2 = new address[](2);
        path2[0] = address(WETH);
        path2[1] = address(USDC);

        // fund Alice
        vm.prank(tokenWhale);
        USDC.transfer(alice, 1870e6);

        // Alice initiates a swap
        uint256[] memory amounts;
        uint256 balanceUsdcBefore = USDC.balanceOf(alice);
        uint256 balanceBefore = alice.balance;
        vm.startPrank(alice);
        USDC.approve(address(v3proxy), 1870e6);
        amounts = v3proxy.swapTokensForExactETH(1e18, 1870e6, path, alice, block.timestamp);

        // we check if the swap was done well
        require(amounts[0] < 1870e6);
        require(amounts[1] == 1e18);
        require(alice.balance == balanceBefore + amounts[1]);
        // the following check fails, but would pass if swapTokensForExactETH
        // sent back the excess tokens
        require(USDC.balanceOf(alice) == balanceUsdcBefore - amounts[0],
            "Unused input tokens were not sent back!");
    }

Recommended Mitigation Steps

Send back the excess tokens:

    function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline) payable external returns (uint[] memory amounts) {
        require(path.length == 2, "Direct swap only");
        require(path[1] == ROUTER.WETH9(), "Invalid path");
        ERC20 ogInAsset = ERC20(path[0]);
        ogInAsset.safeTransferFrom(msg.sender, address(this), amountInMax);
        ogInAsset.safeApprove(address(ROUTER), amountInMax);
        amounts = new uint[](2);
        amounts[0] = ROUTER.exactOutputSingle(ISwapRouter.ExactOutputSingleParams(path[0], path[1], feeTier, address(this), deadline, amountOut, amountInMax, 0));
        amounts[1] = amountOut;
        ogInAsset.safeApprove(address(ROUTER), 0);
        IWETH9 weth = IWETH9(ROUTER.WETH9());
        acceptPayable = true;
        weth.withdraw(amountOut);
        acceptPayable = false;
        payable(msg.sender).call{value: amountOut}("");
+        ogInAsset.safeTransfer(msg.sender, amountInMax - amounts[0]);
        emit Swap(msg.sender, path[0], path[1], amounts[0], amounts[1]);
    }

Keref (Good Entry) confirmed and commented:

See PR#2.

Good Entry Mitigated:

Send back unused funds to user. PR: https://github.com/GoodEntry-io/ge/pull/2

Status: Mitigation confirmed. Full details in reports from kutugu, xuwinnie and 3docSec.


The original report as published on Code4rena, by 3docSec. 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 function fails to transfer the difference between amountInMax and the actual swapped amount back to the caller after the swap.

Impact

Any user of swapTokensForExactETH will always pay amountInMax even if part was not used, and that part is lost, locked in the V3Proxy contract.

What the attacker needed

User calls swapTokensForExactETH; User sets amountInMax higher than needed

The fix

The function should send back the excess tokens by transferring amountInMax minus amounts[0] to the caller.

How it was found

  1. Call swapTokensForExactETH with an excessively high amountInMax
  2. Check that any extra input tokens are sent back - this check will fail

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, you often set a maximum amount you're willing to spend. The contract takes that maximum upfront but only uses part of it for the swap. The leftover should be returned to you, but here it isn't, so your tokens get stuck in the contract.

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 3docSec

View all

15 more disclosed reports, best first.

More from Good Entry

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.