Numerous errors when calculating the TVL for the MorphoBlue connector
The MorphoBlue connector's TVL calculation had three errors: it ignored token decimals, added borrow amounts instead of subtracting them, and failed to update the registry for the loan token on withdrawal. These errors caused incorrect share and token amounts for depositors and withdrawers.
Submitted by Zac (View additional reports submitted by other wardens)
There are numerous issues when calculating the TVL for the MorphoBlue connector
- decimals are not considered in the function
convertCToLthis means that if the loanToken and collateralToken have a different number of decimals the TVL calculation will be wrong. - borrowAmount is added rather than subtracted when calculating the TVL. This means that any borrowing will increase the TVL and any repaying will decrease the TVL
- When withdrawing the function
_updateTokenInRegistryis only called for the collateralToken and not the loanToken. This means that if the loanToken is withdrawn the TVL will be less than it should be.
If the TVL is incorrect, users will recieve an incorrect number of shares when depositing and an incorrect number of base tokens when withdrawing. The below table shows the possible scenarios.
User | TVL lower than expected | TVL higher than expected |
Deposits | Recieves more shares than should be entitled to | Recieves fewer base tokens than should be entitled to |
Withdraws | Recieves less shares than should be entitled to | Recieves more base tokens than should be entitled to |
Proof of Concept
The below test can be added to MorphoBlue.t.sol.
It shows each of the 3 points above causing the TVL calculation to be incorrect.
function testSupplyBorrowRepayWithdraw() public {
uint256 amount = 1000 * 1e6;
uint256 amount_dai = 1000 * 1e18;
console.log("----------- Test Deposit -----------");
_dealWhale(baseToken, address(connector), USDC_Whale, amount);
_dealERC20(DAI, address(connector), amount_dai);
vm.startPrank(owner);
assertEq(
address(morphoBlueContract), address(connector.morphoBlue()), "MorphoBlue address is not set correctly"
);
connector.supply(amount, usdcDaiMarketId, true);
uint256 tvl = accountingManager.TVL();
console.log("TVL: %s", tvl); // tvl = 1000000000
connector.supply(amount_dai, usdcDaiMarketId, false);
tvl = accountingManager.TVL();
console.log("TVL before borrow: %s", tvl); // tvl = 1000000000001000000000
// NOTE: Due to DAI having a different number of decimals than USDC, the TVL increases by a very large amount
// correct TVL = 2000000000
connector.borrow(amount / 2, usdcDaiMarketId);
tvl = accountingManager.TVL();
console.log("TVL after borrow: %s", tvl); // 1000000000002000000000
// NOTE: The TVL increases because the borrow is added instead of subtracted from the TVL
connector.repay(amount / 2, usdcDaiMarketId);
tvl = accountingManager.TVL();
console.log("TVL after repay: %s", tvl); // 1000000000001000000000
connector.withdraw(amount, usdcDaiMarketId, true);
tvl = accountingManager.TVL();
console.log("TVL after withdraw 1: %s", tvl); // 1000000000000000000000
connector.withdraw(amount_dai, usdcDaiMarketId, false);
tvl = accountingManager.TVL();
console.log("TVL after withdraw 2: %s", tvl); // 999945369
// NOTE: The TVL is half of what it should be because the withdraw function does not call _updateTokenInRegistry for the loanToken
vm.stopPrank();
}
Tools Used
Manual Review, Foundry
Recommended Mitigation Steps
Each of the 3 points above must be fixed individually for the TVL calculation to be correct. Each issue can be fixed as follows:
- For the function
convertCToLthe result should be divided by 10**(collateralTokenDecimals) and multiplied by 10**(loanTokenDecimals). - The borrow amount should be subtracted rather than added in the
_getPositionTVLfunction. https://github.com/code-423n4/2024-04-noya/blob/9c79b332eff82011dcfa1e8fd51bad805159d758/contracts/connectors/MorphoBlueConnector.sol#L132 - call
_updateTokenInRegistryfor the loanToken in thewithdrawfunction. https://github.com/code-423n4/2024-04-noya/blob/9c79b332eff82011dcfa1e8fd51bad805159d758/contracts/connectors/MorphoBlueConnector.sol#L58
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 TVL calculation logic in the MorphoBlue connector failed to account for token decimals, treated borrows as additions rather than liabilities, and omitted updating the token registry for the loan token on withdrawal.
What the attacker needed
Access to the connector; tokens with different decimals; borrowing and withdrawing
The fix
The fix should adjust the conversion for token decimals, subtract borrow amounts in the TVL calculation, and call _updateTokenInRegistry for the loan token on withdrawal.
How it was found
- Supply loan token to the MorphoBlue market.
- Supply collateral token to the MorphoBlue market.
- Borrow part of the loan token.
- Repay the borrowed amount.
- Withdraw the collateral token.
- Withdraw the loan token.
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.
This report is about a smart contract that calculates the total value locked (TVL) for a lending position. The calculation had three mistakes: it didn't adjust for tokens with different decimal places, it added borrowed amounts instead of subtracting them, and it forgot to update the list of tracked tokens when a loan token was withdrawn. As a result, users could get the wrong number of shares when depositing or the wrong amount of tokens when withdrawing.
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.