Skip to content
Business logic Audit finding Intermediate

Borrower can lose partial fund during minting of Power Token as excess ETH are not refunded automatically

When a borrower mints Power Tokens using ETH, the exact collateral required is only known after an external swap, so excess ETH can remain in the router contract. Anyone can call refundETH() to take that excess ETH, and a MEV bot could frontrun the borrower's refund.

peakbolt Follow with rvierdiiev and adeolu

Originally disclosed on Code4rena

Submitted by peakbolt, also found by rvierdiiev and adeolu

src/periphery/LendgineRouter.sol#L142 src/periphery/LendgineRouter.sol#L87-L124 src/periphery/LendgineRouter.sol#L119-L123 src/periphery/Payment.sol#L44-L46

When the collateral/speculative token (Token1) is WETH, a borrower could mint Power Tokens and deposit the collateral tokens by sending ETH while calling the payable mint() function in LendgineRouter.sol.

The exact collateral amount required to be deposited by the borrower is only calculated during minting (due to external swap), which could be lesser than what the borrower has sent for the mint. This means that there will be excess ETH left in LengineRouter contract and they are not automatically refunded to the borrower.

Anyone that sees this opportunity can call refundETH() to retrieve the excess ETH.

The borrower could retrieve the remaining ETH with a separate call to refundETH(). However, as the calls are not atomic, it is possible for a MEV bot to frontrun the borrower and steal the ETH too.

Furthermore, there are no documentation and test cases that advise or handle this issue.

Proof of Concept

First, call payable mint() in LendgineRouter contract with the required ETH amount for collateral.

function mint(MintParams calldata params) external payable checkDeadline(params.deadline) returns (uint256 shares) {

src/periphery/LendgineRouter.sol#L142

LendgineRouter.mintCallback() will be triggered, which will perform the external swap of the borrowed token0 to token1 on uniswap. The collateralSwap value (token1) is only calculated and known after the successful swap. Both swapped token1 and borrowed token1 are then sent to Lendgine contract (msg.sender).

// swap all token0 to token1
uint256 collateralSwap = swap(
  decoded.swapType,
  SwapParams({
    tokenIn: decoded.token0,
    tokenOut: decoded.token1,
    amount: SafeCast.toInt256(amount0),
    recipient: msg.sender
  }),
  decoded.swapExtraData
);

// send token1 back
SafeTransferLib.safeTransfer(decoded.token1, msg.sender, amount1);

src/periphery/LendgineRouter.sol#L87-L124

After that, mintCallback() will continue to calculate the remaining token1 required to be paid by the borrower (collateralIn value).

Depending on the external swap, the collateralSwap (token1) value could be higher than expected, resulting in a lower collateralIn value. A small collateralIn value means that less ETH is required to be paid by the borrower (via the pay function), resulting in excess ETH left in the LengineRouter contract. However, the excess ETH is not automatically refunded by the mint() call.

Note: For WETH, the pay() uses the ETH balance deposited and wrap it before transferring to Lendgine contract.

// pull the rest of tokens from the user
uint256 collateralIn = collateralTotal - amount1 - collateralSwap;
if (collateralIn > decoded.collateralMax) revert AmountError();

pay(decoded.token1, decoded.payer, msg.sender, collateralIn);

src/periphery/LendgineRouter.sol#L119-L123

A MEV bot or anyone that see this opportunity can call refundETH() to retrieve the excess ETH.

function refundETH() external payable {
	if (address(this).balance > 0) SafeTransferLib.safeTransferETH(msg.sender, address(this).balance);
}

src/periphery/Payment.sol#L44-L46

Recommended Mitigation Steps

Automatically refund any excess ETH to the borrower.

kyscott18 (Numoen) acknowledged and commented:

We expect this issue to be mitigated by a user using the multicall feature of our contract. When expecting to receive eth or not spending the total amount of eth sent, a multicall should be called with the second call calling refundEth() to sweep up the rest of the eth left over in the contract. Because the multicall is atomic, no bot can frontrun the user.

This situation is also present in Uniswap V3 and there has been some debate about it. For me, the general consensus is that it is not an issue as refundEth() and multicall() are expected to be used, and not using this is the fault of the user.

berndartmueller (judge) decreased severity to Medium and commented:

It is the responsibility of the user to use the contracts appropriately (e.g. using multicall(..)) to make sure leftover funds are sent out. However, due to the lack of documentation to properly educate about the usage of multicall, I consider Medium severity to be appropriate.


The original report as published on Code4rena, by peakbolt. 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 mint function does not automatically refund excess ETH sent by the borrower, leaving it in the contract where anyone can claim it via refundETH().

Impact

A borrower could lose excess ETH to a MEV bot or anyone who calls refundETH() first, though the borrower can avoid this by using multicall to refund atomically.

What the attacker needed

Borrower sends more ETH than needed; Borrower does not use multicall to refund

The fix

The mint function should automatically refund any excess ETH to the borrower.

How it was found

  1. Call the payable mint() function on LendgineRouter with ETH for collateral.
  2. The mintCallback() performs an external swap, and the collateralSwap value is calculated.
  3. The remaining collateralIn is calculated and paid via pay().
  4. If collateralIn is less than the ETH sent, excess ETH remains in the router contract.
  5. Anyone can call refundETH() to retrieve the excess 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 send ETH to a smart contract, any extra ETH you send beyond what is needed is not automatically returned. In this case, the contract calculates the exact cost only after swapping tokens, so if the swap is favourable, you overpay and the leftover ETH stays in the contract. Anyone can then call a function to claim that leftover ETH for themselves.

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 peakbolt

View all

16 more disclosed reports, best first.

More from Numoen contest

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.