Skip to content
Business logic Audit finding Advanced

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.

000000 Follow with 0xAlix2, 0xc0ffEE and 16 others

Originally disclosed on Sherlock

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

  1. Anybody calls withdraw or withdrawSimple, it will reduce the asset's totalSupply.supplyShares incorrectly.

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:

Solidity
// 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);
    }
  }

The original report as published on Sherlock, by 000000. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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

  1. 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.

More by 000000

View all

32 more disclosed reports, best first.

More from ZeroLend One

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.