Unused ETH is not refunded when a limit price is set
When a user sells ETH with a limit price, the swap may be partially executed, but the router does not refund the unspent ETH, leaving it in the contract. Anyone can then withdraw that ETH via the public refundETH function.
Submitted by Jeiwan, also found by 0x1f8b
Router.sol#L90-L91
Impact
Users may lose a portion of ETH when setting a limit price and selling ETH.
Proof of Concept
The Router contract (Router.sol#L18) is a higher level contract that will be used by the majority of the users.
The contract allows to swap tokens using Pool contracts. One extra feature it adds is the ability to swap native coins (e.g. ETH) for ERC20 tokens so users don’t need to wrap native coins manually (Router.sol#L88-L91):
function pay(IERC20 token, address payer, address recipient, uint256 value) internal {
if (IWETH9(address(token)) == WETH9 && address(this).balance >= value) {
WETH9.deposit{value: value}();
WETH9.transfer(recipient, value);
The Pool contract allows to set a limit price during swapping: a price, upon reaching which, swapping will stop at (Pool.sol#L636-L641). When a limit price is set and reached, the swap is executed partially: the input amount specified in the call to swap is spent partially (Pool.sol#L565); the output amount is calculated only based on the input amount spent. This mechanism allows traders to execute a swap only until a certain price is reached.
The combination of the above facts allows a bug: when user sells a native coin (e.g. ETH), specifies full input amount, and sets a limit price, the actualinput amount will be smaller due to the limit price being set, but the full input amount will be spent by the user. The Router won’t refund the remaining amount to the user and it’ll be left in the contract. Anyone (MEV bots, most likely) will be able to withdraw it via the public refundETH function (Router.sol#L80-L82).
Consider this exploit scenario:
- Alice wants to sell 1 ETH and buy some token TKN. However Alice wants her trade to be executed before the price X.
- Alice calls the
exactInputSinglefunction ofRouter, sets the sqrtPriceLimitD18 argument to the price X, and sends 1 ETH along with the transaction. - The router executes the swap via an ETH-TKN pool. The swap gets interrupted when the price X is reached.
- Before reaching the price X, only 0.7 ETH of Alice were consumed to convert them to 100 TKN.
- Alice receives 100 TKN while spending 1 ETH, the router contract keeps holding the remaining 0.3 ETH.
- A MEV bot withdraws the 0.3 ETH by calling the
refundETHfunction.
The below PoC reproduces this scenario:
// router-v1/test/Router.ts
// describe("#swap exact in", () => {
it("doesn't refund ETH [AUDIT]", async () => {
let pool: string = await getEthBPool(0.05, 2);
const preEthBalance = await ethBalances();
const prePoolBalance = await balances(pool);
const preOwnerBalance = await balances(await owner.getAddress());
// User swaps ETH for token B and sets a limit price.
await router.exactInputSingle({
tokenIn: weth9.address,
tokenOut: tokenB.address,
pool: pool,
recipient: await owner.getAddress(),
deadline: maxDeadline,
amountIn: floatToFixed(1),
amountOutMinimum: 0,
sqrtPriceLimitD18: "1077931889220708752" // (beforePoolPrice + afterPoolPrice) / 2
},
{ value: floatToFixed(1) }
);
const postPoolBalance = await balances(pool);
const postEthBalance = await ethBalances();
const postOwnerBalance = await balances(await owner.getAddress());
// User has spent the entire input amount (1 ETH).
expect(postEthBalance.owner - preEthBalance.owner).to.be.approximately(
-1,
0.001
);
// User has bought some tokens B. The amount wasn't paid by the entire 1 ETH because the
// limit price has interrupted the swap.
expect(postOwnerBalance.tokenB - preOwnerBalance.tokenB).to.eq(0.41609887980257554);
// The remaining input amount has been left in the router and wasn't refunded.
expect(postEthBalance.router - preEthBalance.router).to.eq(0.49373912843213397);
// A MEV bot immediately withdraws the ETH remained from the swap.
const preMEVBotBalance = await ethers.provider.getBalance(await addr1.getAddress());
await router.connect(addr1).refundETH();
const postMEVBotBalance = await ethers.provider.getBalance(await addr1.getAddress());
expect(postMEVBotBalance.sub(preMEVBotBalance).toString()).to.eq("493710755432133969");
});
Recommended Mitigation Steps
Consider refunding unspent ETH to users.
gte620v (Maverick) disputed and commented:
I have analyzed the report and do not consider it valid.
kirk-baird (judge) decreased severity to Medium and commented:
I consider it to be a valid Medium.
A note on this issue is that the recommendation is not desirable. A more appropriate recommendation is to increase documentation to encourage users to do a MultiCall.
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 may lose a portion of their ETH, which can be taken by anyone, such as MEV bots.
What the attacker needed
User sells ETH with a limit price; swap is partially executed; attacker calls refundETH
The fix
The report recommends refunding unspent ETH, but the judge suggested instead increasing documentation to encourage users to use MultiCall.
How it was found
- Alice calls exactInputSingle with a limit price and sends 1 ETH.
- The swap stops early at the limit price, using only part of the ETH.
- Alice receives tokens but the unused ETH stays in the router.
- A MEV bot calls refundETH and withdraws the leftover ETH.
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 ETH for a token, you send ETH to a router contract. If you set a limit price, the swap might stop early, so not all your ETH is used. The bug is that the router keeps the unused ETH instead of returning it, and anyone can later take it.
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.