Function `executeMintToTreasury` will incorrectly reduce the `supplyShares`, therefore prevent the last users from withdrawing
The function executeMintToTreasury incorrectly reduces totalSupply.supplyShares without updating any user's balance, causing a mismatch between total shares and the sum of individual shares. This can lead to an underflow when the last users try to withdraw, locking their funds.
Source: https://github.com/sherlock-audit/2024-06-new-scope-judging/issues/375
Found by
000000, 0xAlix2, 0xc0ffEE, A2-security, Honour, KupiaSec, Nihavent, Obsidian, Tendency, Valy001, Varun_05, coffiasd, ether_sky, iamnmt, imsrybr0, lemonmon, silver_eth, stuart_the_minion, trachev
Summary
The incorrect reduction in PoolLogic::executeMintToTreasury will cause failure of some (likely to be the last) user's withdrawal, and the fund will be locked.
Root Cause
The totalSupply.supplyShares is supposed to be the sum of balances.supplyShares, as they are always updated in tandem:
https://github.com/sherlock-audit/2024-06-new-scope/blob/main/zerolend-one/contracts/core/pool/configuration/PositionBalanceConfiguration.sol#L94-L95 https://github.com/sherlock-audit/2024-06-new-scope/blob/main/zerolend-one/contracts/core/pool/configuration/PositionBalanceConfiguration.sol#L48-L49
If this is not held, then some users will not be able to withdraw their collateral, as the totalSupply.supplyShares will underflow and revert.
However in the PoolLogic::executeMintToTreasury updates the totalSupply.supplyShares without updating any user's balance. It is because it incorrectly assumes that there is share to be burned, even though the accrued amount was never really minted to the treasury (in that the treasury's share balance was not added). If it is so, that share should be burned from the treasury.
Also, for example, when premium is added via flashloan, the premium is counted as underlyingBalance: https://github.com/sherlock-audit/2024-06-new-scope/blob/main/zerolend-one/contracts/core/pool/logic/FlashLoanLogic.sol#L118
Therefore, when the underlying asset is transferred out via executeMintToTreasury, the underlyingBalance should be updated accordingly.
Internal pre-conditions
Non-zero accruedToTreasuryShares
External pre-conditions
No response
Attack Path
- Anybody calls
withdraworwithdrawSimple, it will reduce theasset'stotalSupply.supplySharesincorrectly.
impact
The last user(s) who is trying to withdraw will fail, and their fund will be locked
PoC
No response
Mitigation
Suggestion of mitigation:
// PoolLogic.sol
function executeMintToTreasury(
DataTypes.ReserveSupplies storage totalSupply,
mapping(address => DataTypes.ReserveData) storage reservesData,
address treasury,
address asset
) external {
DataTypes.ReserveData storage reserve = reservesData[asset];
uint256 accruedToTreasuryShares = reserve.accruedToTreasuryShares;
if (accruedToTreasuryShares != 0) {
reserve.accruedToTreasuryShares = 0;
uint256 normalizedIncome = reserve.getNormalizedIncome();
uint256 amountToMint = accruedToTreasuryShares.rayMul(normalizedIncome);
IERC20(asset).safeTransfer(treasury, amountToMint);
- totalSupply.supplyShares -= accruedToTreasuryShares;
+ totalSupply.underlyingBalance -= amountToMint;
emit PoolEventsLib.MintedToTreasury(asset, amountToMint);
}
}
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 function reduces totalSupply.supplyShares without burning shares from the treasury or updating underlyingBalance, breaking the invariant that total shares equal the sum of user shares.
Impact
The last user(s) attempting to withdraw will fail and their funds will be locked.
What the attacker needed
Non-zero accruedToTreasuryShares
The fix
The function should not reduce totalSupply.supplyShares; instead it should reduce totalSupply.underlyingBalance by the amount minted.
How it was found
- Anybody calls withdraw or withdrawSimple, which reduces the asset's totalSupply.supplyShares incorrectly.
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, shares represent ownership of deposited assets. The total number of shares should always equal the sum of all users' shares. This report says a function that sends fees to the treasury wrongly reduces the total share count without reducing anyone's actual shares, so later the numbers don't add up and the last users can't withdraw.
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.