Wrong global lending limit check in _deposit function
The _deposit function compared the total supply of lender shares against the global lending limit instead of converting those shares to their underlying asset value. This allowed more assets to be lent than the protocol's global limit intended.
Aymen0909 Follow with linmiaomiao, KupiaSec and 4 others
Originally disclosed on Code4rena
Submitted by Aymen0909, also found by linmiaomiao, KupiaSec, befree3x, pynschon, kennedy1030, and Topmark
The _deposit function is invoked in both the deposit and mint functions when a user wants to lend assets. This function is intended to ensure that the total amount of assets lent does not exceed the protocol limit globalLendLimit.
function _deposit(
address receiver,
uint256 amount,
bool isShare,
bytes memory permitData
) internal returns (uint256 assets, uint256 shares) {
...
_mint(receiver, shares);
//@audit must convert totalSupply() to assets before comparing with globalLendLimit
if (totalSupply() > globalLendLimit) {
revert GlobalLendLimit();
}
if (assets > dailyLendIncreaseLimitLeft) {
revert DailyLendIncreaseLimit();
} else {
dailyLendIncreaseLimitLeft -= assets;
}
...
}
In the provided code snippet, the _deposit function checks the totalSupply() against the globalLendLimit limit. However, totalSupply() represents the lenders’ share amount and does not represent the actual asset amount lent. It must first be converted to assets using the _convertToAssets function.
This mistake is evident because in the maxDeposit function, the correct check is implemented:
function maxDeposit(address) external view override returns (uint256) {
(, uint256 lendExchangeRateX96) = `_calculateGlobalInterest()`;
uint256 value = _convertToAssets(
totalSupply(),
lendExchangeRateX96,
Math.Rounding.Up
);
if (value >= globalLendLimit) {
return 0;
} else {
return globalLendLimit - value;
}
}
Because the _deposit function performs the wrong check, it will allow more assets to be lent in the protocol. This is due to the fact that the lending exchange rate lastLendExchangeRateX96 will be greater than 1 (due to interest accrual), and so we will always have totalSupply() < _convertToAssets(totalSupply(), lendExchangeRateX96, Math.Rounding.Up). The only case this might not hold is when there is a significant bad debt after liquidation, which would not occur under normal circumstances.
Impact
Incorrect global lending checking in the _deposit function will result in more assets being lent than allowed by the protocol.
Tools Used
VS Code
Recommended Mitigation
To address this issue, the totalSupply() must be converted to an asset amount before checking it against globalLendLimit, as is done in the maxDeposit function:
function _deposit(
address receiver,
uint256 amount,
bool isShare,
bytes memory permitData
) internal returns (uint256 assets, uint256 shares) {
...
_mint(receiver, shares);
++ //@audit must convert totalSupply() to assets before comparing with globalLendLimit
++ uint256 value = _convertToAssets(totalSupply(), newLendExchangeRateX96, Math.Rounding.Up);
++ if (value > globalLendLimit) {
-- if (totalSupply() > globalLendLimit) {
revert GlobalLendLimit();
}
if (assets > dailyLendIncreaseLimitLeft) {
revert DailyLendIncreaseLimit();
} else {
dailyLendIncreaseLimitLeft -= assets;
}
...
}
Assessed type
Error
Fixed here.
Status: Unmitigated. Full details in reports from thank_you, b0g0 and ktg, and also included in the Mitigation Review section below.
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 used totalSupply() (share amount) directly instead of converting it to assets with _convertToAssets, as done in maxDeposit.
Impact
More assets could be lent than the globalLendLimit allows, potentially increasing protocol risk.
What the attacker needed
None
The fix
The check should convert totalSupply() to assets using _convertToAssets before comparing with globalLendLimit, as in maxDeposit.
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 protocol, users receive shares representing their deposit. The protocol has a cap on total assets lent. The code checked the number of shares against the cap, not the actual asset value, so the cap could be exceeded because shares are worth more than one asset unit over time.
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.