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.
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:
- The user swaps
5 ETHfor some Yt byswapETHForYt. (i.e.msg.value = 5 ETH) - The estimated amount of WETH required to issue the PT and YT is
4 WETH(i.e.wethDeposit = 4 WETH). - The PT are finally swapped to
2 WETH(i.e.wethReceived = 2 WETH). - The
feeAmountof flash loan is0.1 WETH.
Then:
repayAmount = wethDeposit + feeAmounts[0] = 4 WETH + 0.1 WETH = 4.1 WETHspent = repayAmount - wethReceived = 4.1 WETH - 2 WETH = 2.1 WETHremaining = 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.
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
Impact
Incorrect checking can cause swapETHForYt to fail unexpectedly, breaking the core functionality of the contract.
Code Snippet
Tool used
Manual Review
Recommendation
- 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.
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
- User calls `swapETHForYt` with 5 ETH.
- The contract takes a flash loan to deposit 4 WETH and issue PT and YT.
- The PT are swapped for 2 WETH.
- The flash loan fee is 0.1 WETH, so repayAmount is 4.1 WETH.
- spent is calculated as 2.1 WETH, leaving remaining as 2.9 WETH.
- 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.