Flashlender.sol#flashLoan() should use mintProfit() to mint fees, as the current implementation may lead to locked up WETH in PoolV3
The flashLoan function in Flashlender.sol incorrectly uses repayCreditAccount to add fees, which does not update expectedLiquidity, causing WETH to be locked because withdrawals may underflow when expectedLiquidity is updated. An attacker could cause a denial of service by making withdrawals revert, trapping funds.
Submitted by pkqs90, also found by 0xAlix2, hearmen, web3km, Bigsam, lian886, Infect3d, Bauchibred, crypticdefense, Spearmint, grearlake, Ruhum, karsar, and hash
When using flashlender to create loans, there is small amount of fees. The fees are sent to PoolV3 and is spread out to the lpETH stakers.
The issue is, the current implementation uses pool.repayCreditAccount() to add the profit, by setting the profit == fees. This is incorrect, and the correct implementation should be to use pool.mintProfit() to send the fees.
Both implementation mints the extra fees as lpETH to the StakingLPEth.sol contract, which is correct. However, the current implementation does NOT update the expectedLiquidity, since it assumes the profit is a part of the interest that should be paid by debt borrowers, and is already included, which is incorrect.
We can see the mintProfit() function also adds the amount of fees to the expectedLiquidity, since this is a different kind of fees than debt interest.
The impact of this issue is that the WETH amount in PoolV3 would be larger than expectedLiquidity. Since lpETH:WETH is always 1:1, when users want to withdraw the WETH, it may revert due to underflow when updating expectedLiquidity. An example is:
- Currently there is 100 lpETH, 100 WETH,
expectedLiquidity == 100WETH inside PoolV3. - Flashloan fees of 10 WETH comes in, lpETH is 110 lpETH, WETH is 110 WETH, but
expectedLiquidityis still 100 WETH. - All lpETH users try to withdraw their WETH. Note that there is enough WETH in PoolV3, but since during withdraw, the
expectedLiquidityis also updated, so only 100 WETH is allow for withdraw, or else there would be an integer underflow.
Note that CDPVault also uses mintProfit() to send the extra liquidation penalty as profit to PoolV3.
https://github.com/code-423n4/2024-07-loopfi/blob/main/src/CDPVault.sol#L569
Flashlender.sol:
function flashLoan(
IERC3156FlashBorrower receiver,
address token,
uint256 amount,
bytes calldata data
) external override nonReentrant returns (bool) {
if (token != address(underlyingToken)) revert Flash__flashLoan_unsupportedToken();
uint256 fee = wmul(amount, protocolFee);
uint256 total = amount + fee;
pool.lendCreditAccount(amount, address(receiver));
emit FlashLoan(address(receiver), token, amount, fee);
if (receiver.onFlashLoan(msg.sender, token, amount, fee, data) != CALLBACK_SUCCESS)
revert Flash__flashLoan_callbackFailed();
// reverts if not enough Stablecoin have been send back
underlyingToken.transferFrom(address(receiver), address(pool), total);
@> pool.repayCreditAccount(total - fee, fee, 0);
// @BUG. SHOULD USE pool.mintProfit().
return true;
}
PoolV3.sol:
function repayCreditAccount(
uint256 repaidAmount,
uint256 profit,
uint256 loss
)
external
override
creditManagerOnly // U:[LP-2C]
whenNotPaused // U:[LP-2A]
nonReentrant // U:[LP-2B]
{
if (profit > 0) {
_mint(treasury, convertToShares(profit)); // U:[LP-14B]
} else if (loss > 0) {
...
}
_updateBaseInterest({
@> expectedLiquidityDelta: -loss.toInt256(),
availableLiquidityDelta: 0,
checkOptimalBorrowing: false
}); // U:[LP-14B,14C,14D]
}
function mintProfit(uint256 amount) external creditManagerOnly {
_mint(treasury, amount);
_updateBaseInterest({
@> expectedLiquidityDelta: amount.toInt256(),
availableLiquidityDelta: 0,
checkOptimalBorrowing: false
}); // U:[LP-14B,14C,14D]
}
function _withdraw(
address receiver,
address owner,
uint256 assetsSent,
uint256 assetsReceived,
uint256 amountToUser,
uint256 shares
) internal {
if (msg.sender != owner) _spendAllowance({owner: owner, spender: msg.sender, amount: shares}); // U:[LP-8,9]
_burn(owner, shares); // U:[LP-8,9]
_updateBaseInterest({
> expectedLiquidityDelta: -assetsSent.toInt256(),
> availableLiquidityDelta: -assetsSent.toInt256(),
checkOptimalBorrowing: false
}); // U:[LP-8,9]
// @INTEGER UNDERFLOW WOULD OCCUR HERE.
IERC20(underlyingToken).safeTransfer({to: receiver, value: amountToUser}); // U:[LP-8,9]
if (assetsSent > amountToUser) {
unchecked {
IERC20(underlyingToken).safeTransfer({to: treasury, value: assetsSent - amountToUser}); // U:[LP-8,9]
}
}
emit Withdraw(msg.sender, receiver, owner, assetsReceived, shares); // U:[LP-8,9]
}
Recommended Mitigation Steps
Use mintProfit() to mint flashloan fees instead.
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 flashLoan function uses repayCreditAccount with profit set to fees, which does not increase expectedLiquidity, whereas mintProfit would correctly update it.
Impact
The WETH amount in PoolV3 becomes larger than expectedLiquidity, so users may be unable to withdraw their WETH because the transaction reverts due to integer underflow, locking funds.
What the attacker needed
Flash loans are used; fees accumulate in PoolV3
The fix
The flashLoan function should use pool.mintProfit() to mint the fees instead of repayCreditAccount.
How it was found
- A flash loan is taken, generating fees.
- The fees are sent to PoolV3 via repayCreditAccount, which does not update expectedLiquidity.
- The actual WETH balance in PoolV3 becomes larger than expectedLiquidity.
- Users attempt to withdraw their WETH, but the withdrawal reverts due to integer underflow when updating expectedLiquidity.
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.
In a lending pool, the contract tracks how much liquidity it expects to have. When flash loans are taken, fees are added, but the code adds them in a way that doesn't update this expected amount. Later, when users try to withdraw, the contract's accounting breaks and withdrawals fail, locking funds.
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.