The calculation of assetsMax is incorrect.
The _undeploy function calculates assetsMax using stale totalSupplyAssets and totalSupplyShares from Morpho storage, ignoring accrued interest. This underestimates assetsMax, so a user withdrawing an amount that includes interest may trigger the branch that withdraws all shares, taking other users' assets.
Submitted by shaflow2
Finding description and impact
In the _undeploy function, assetsMax is incorrectly calculated because the contract directly retrieves totalSupplyAssets and totalSupplyShares from _morpho storage without accounting for the accrued interest over time. This leads to an underestimation of assetsMax, which may allow users to withdraw more assets than they should, causing losses to other users.
Proof of Concept
/// @notice Retrieves the current balance of the managed asset in the Morpho protocol.
/// @return The current balance of the managed asset.
function _getBalance() internal view override returns (uint256) {
return _morpho.expectedSupplyAssets(_marketParams, address(this));
}
In the StrategySupplyMorpho contract, when retrieving assets, the expectedSupplyAssets function is used, which considers accrued interest and fees from the elapsed time since the last update. This ensures that the withdraw and redeem functions calculate assets, including unaccounted interest.
function _undeploy(uint256 amount) internal override returns (uint256) {
Id id = _marketParams.id();
uint256 assetsWithdrawn = 0;
uint256 totalSupplyAssets = _morpho.totalSupplyAssets(id);
uint256 totalSupplyShares = _morpho.totalSupplyShares(id);
uint256 shares = _morpho.supplyShares(id, address(this));
uint256 assetsMax = shares.toAssetsDown(totalSupplyAssets, totalSupplyShares);
if (amount >= assetsMax) {
(assetsWithdrawn, ) = _morpho.withdraw(_marketParams, 0, shares, address(this), address(this));
} else {
(assetsWithdrawn, ) = _morpho.withdraw(_marketParams, amount, 0, address(this), address(this));
}
return assetsWithdrawn;
}
However, in the _undeploy function, the calculation of assetsMax does not account for the accrued interest and fees over time. This may result in assetsMax being underestimated compared to its actual value. On the other hand, the amount parameter includes accrued interest and fees, which can lead to the function entering the wrong branch. If the function mistakenly enters the second branch, it may incorrectly convert all _morpho shares in the strategy to assets and send them to the withdrawer. In this case, the strategy’s assets will be 0, but the vault shares will still remain in the vault. The remaining shareholders in the vault will not be able to normally claim assets.
Example:
-
Initial Deposits:
user1deposits1e16assets into the vault and receives1e16shares.user2deposits1e20assets and receives1e20shares. -
Interest Accumulation: After some time, the
_morphostrategy generates interest. The vault’stotalAssets()now returns1e20 + 1e16 + 1e17assets. -
Redeem by user2:
user2decides to redeem all their shares. When calculatingwithdrawAmount = (shares * totalAssets()) / totalSupply(),totalAssets()includes the interest. The calculatedwithdrawAmountis passed to the_undeployfunction. In_undeploy, the maximum amount of assets that can be converted from the current_morphoshares (assetsMax) is calculated. However, since the calculation ofassetsMaxdoes not account for the interest,assetsMax < withdrawAmount.As a result, the strategy withdraws all
_morphoshares, converts them into assets, and sends them touser2. This meansuser2inadvertently receives both their own principal and interest as well asuser1’s principal and interest. -
Abnormal State: Now, the strategy holds no
_morphoshares, sototalAssets()returns0. However, the vault still hasuser1’s1e16shares. This creates an abnormal state in the vault.
Recommended mitigation steps
When calculating assetsMax, consider the accrued interest and fees that have not been updated.
function _undeploy(uint256 amount) internal override returns (uint256) {
Id id = _marketParams.id();
uint256 assetsWithdrawn = 0;
- uint256 totalSupplyAssets = _morpho.totalSupplyAssets(id);
- uint256 totalSupplyShares = _morpho.totalSupplyShares(id);
- uint256 shares = _morpho.supplyShares(id, address(this));
- uint256 assetsMax = shares.toAssetsDown(totalSupplyAssets, totalSupplyShares);
+ uint256 assetsMax = _morpho.expectedSupplyAssets(_marketParams, address(this));
if (amount >= assetsMax) {
(assetsWithdrawn, ) = _morpho.withdraw(_marketParams, 0, shares, address(this), address(this));
} else {
(assetsWithdrawn, ) = _morpho.withdraw(_marketParams, amount, 0, address(this), address(this));
}
return assetsWithdrawn;
}
chefkenji (BakerFi) confirmed
Status: Mitigation confirmed. Full details in reports from 0xlemon and shaflow2.
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 contract uses raw totalSupplyAssets and totalSupplyShares from Morpho storage instead of the interest-adjusted expectedSupplyAssets when computing the maximum withdrawable assets.
Impact
A user could withdraw more assets than they should, causing losses to other users and leaving the vault in an abnormal state where remaining shareholders cannot normally claim assets.
What the attacker needed
Users have deposited into the vault; interest has accrued in Morpho; a user redeems all their shares
The fix
The calculation of assetsMax should use expectedSupplyAssets to include accrued interest and fees, and the project mitigated this via a pull request.
How it was found
- user1 deposits 1e16 assets and receives 1e16 shares; user2 deposits 1e20 assets and receives 1e20 shares.
- After some time, the Morpho strategy generates interest, so the vault's totalAssets() becomes 1e20 + 1e16 + 1e17.
- user2 redeems all shares; withdrawAmount is calculated using totalAssets() including interest.
- In _undeploy, assetsMax is computed without interest, so assetsMax < withdrawAmount.
- The strategy withdraws all Morpho shares, converts them to assets, and sends them to user2.
- The strategy holds no Morpho shares, totalAssets() returns 0, but user1's shares remain, creating an abnormal state.
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.
A smart contract lets people deposit money and earn interest. When one person withdraws, the contract miscalculates how much their shares are worth because it forgets about interest earned over time. This can let that person take more than their fair share, leaving others unable to get their money back.
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.