Skip to content
Business logic Audit finding Intermediate

Incorrect checking in `receiveFlashLoan` can cause `swapETHForYt` to fail unexpectedly

The flash loan repayment check in `receiveFlashLoan` compares the repayment amount against the remaining ETH after accounting for the swap, rather than against the total ETH sent by the user. This can cause `swapETHForYt` to revert even when the user has sent enough ETH to cover the repayment, breaking the core functionality.

Drynooo Follow with Ironsidesec, KupiaSec and 3 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-05-napier-update-judging/issues/36

Found by

Drynooo, Ironsidesec, KupiaSec, Varun_05, no, ydlee

Summary

The following checking in receiveFlashLoad is incorrect, causing swapETHForYt to fail.

Solidity
File: metapool-router/src/MetapoolRouter.sol

333:        if (repayAmount > remaining) revert Errors.MetapoolRouterInsufficientETHRepay(); // Can't repay the flash loan

Vulnerability Detail

In line 333, the check here is to ensure that the ETH sent by the user is enough to cover the repayAmount. Thus the check should revert if repayAmount > TransientStorage.tloadU256(TSLOT_CB_DATA_VALUE) , instead of repayAmount > remaining.

Let's assume that:

  1. The user swaps 5 ETH for some Yt by swapETHForYt. (i.e. msg.value = 5 ETH)
  2. The estimated amount of WETH required to issue the PT and YT is 4 WETH (i.e. wethDeposit = 4 WETH).
  3. The PT are finally swapped to 2 WETH (i.e. wethReceived = 2 WETH).
  4. The feeAmount of flash loan is 0.1 WETH.

Then:

  1. repayAmount = wethDeposit + feeAmounts[0] = 4 WETH + 0.1 WETH = 4.1 WETH
  2. spent = repayAmount - wethReceived = 4.1 WETH - 2 WETH = 2.1 WETH
  3. remaining = 5 WETH - spent = 5 WETH - 2.1 WETH = 2.9 WETH

Line 333 in receiveFlashLoan reverts as repayAmount > remaining, and the swapETHForYt fails. But the user pays more than required to swap. The fail is not expected.

Solidity
File: metapool-router/src/MetapoolRouter.sol

323:        // Calculate the amount of ETH spent in the swap
324:        uint256 repayAmount = wethDeposit + feeAmounts[0];
325:        uint256 spent = repayAmount - wethReceived; // wethDeposit + feeAmounts[0] - wethReceived
326:
327:        // Revert if the ETH spent exceeds the specified maximum
328:        if (spent > TransientStorage.tloadU256(TSLOT_CB_DATA_MAX_ETH_SPENT)) {
329:            revert Errors.MetapoolRouterExceededLimitETHIn();
330:        }
331:
332:        uint256 remaining = TransientStorage.tloadU256(TSLOT_CB_DATA_VALUE) - spent;
333:@>      if (repayAmount > remaining) revert Errors.MetapoolRouterInsufficientETHRepay(); // Can't repay the flash loan

https://github.com/sherlock-audit/2024-05-napier-update/blob/main/metapool-router/src/MetapoolRouter.sol#L323-L333

Impact

Incorrect checking can cause swapETHForYt to fail unexpectedly, breaking the core functionality of the contract.

Code Snippet

https://github.com/sherlock-audit/2024-05-napier-update/blob/main/metapool-router/src/MetapoolRouter.sol#L323-L333

Tool used

Manual Review

Recommendation

Solidity
-        if (repayAmount > remaining) revert Errors.MetapoolRouterInsufficientETHRepay();
+        if (repayAmount > TransientStorage.tloadU256(TSLOT_CB_DATA_VALUE)) revert Errors.MetapoolRouterInsufficientETHRepay();

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/napierfi/metapool-router/pull/28

sherlock-admin2

The Lead Senior Watson signed off on the fix.

The original report as published on Sherlock, by Drynooo. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 check uses `remaining` (ETH sent minus spent) instead of the total ETH sent (`TSLOT_CB_DATA_VALUE`), so it incorrectly reverts when the repayment amount exceeds the leftover ETH but not the total ETH provided.

Impact

Incorrect checking can cause `swapETHForYt` to fail unexpectedly, breaking the core functionality of the contract.

What the attacker needed

User calls swapETHForYt with ETH

The fix

The check should compare `repayAmount` against the total ETH sent (`TSLOT_CB_DATA_VALUE`) instead of `remaining`; the protocol team fixed this issue.

How it was found

  1. User calls `swapETHForYt` with 5 ETH.
  2. The contract takes a flash loan to deposit 4 WETH and issue PT and YT.
  3. The PT are swapped for 2 WETH.
  4. The flash loan fee is 0.1 WETH, so repayAmount is 4.1 WETH.
  5. spent is calculated as 2.1 WETH, leaving remaining as 2.9 WETH.
  6. The check `repayAmount > remaining` (4.1 > 2.9) reverts, causing the swap to 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 ETH for tokens, the contract takes a flash loan (a temporary loan) to help with the swap. It must check that you sent enough ETH to repay that loan. The check uses the wrong number: it compares the loan amount to what's left after the swap instead of the total ETH you sent, so it can wrongly reject your transaction even if you sent enough.

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 Drynooo

View all

4 more disclosed reports, best first.

More from Napier Finance - LST/LRT Integrations

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.