There are multiple issues with the decimal conversions between the vault and the strategy
The StrategyLeverage contract mishandles decimal conversions between the vault's 18-decimal shares and tokens with other decimals, causing incorrect share minting, withdrawals, swaps, and debt adjustments. This makes the system incompatible with tokens that do not use 18 decimals, potentially leading to loss of funds or broken functionality.
Submitted by shaflow2, also found by 0xlemon, 0xpiken, ABAIKUNANBAEV, klau5, and shaflow2
Finding description and impact
The StrategyLeverage contract has multiple incorrect decimal handling issues, causing the system to not support tokens with decimals other than 18.
Proof of Concept
First, the vault contract’s share decimal is set to 18, as recommended by the ERC4626 standard. Ideally, the vault’s share decimal should reflect the underlying token’s decimal. Otherwise, conversions through convertToShares and convertToAssets would be required.
In StrategyLeverage, we can see that all calls to totalAssets() are converted to 18 decimals for share calculations.
Under the above premise, the contract has multiple decimal handling errors, making it incompatible with tokens that use decimals other than 18:
-
The
_deployfunction should return the amount in the system’s 18-decimal format, rather than the token’s native decimal format.function _depositInternal(uint256 assets, address receiver) private returns (uint256 shares) { ... uint256 deployedAmount = _deploy(assets); // Calculate shares to mint shares = total.toBase(deployedAmount, false); // Prevent inflation attack for the first deposit if (total.base == 0 && shares < _MINIMUM_SHARE_BALANCE) { revert InvalidShareBalance(); } // Mint shares to the receiver _mint(receiver, shares); // Emit deposit event emit Deposit(msg.sender, receiver, assets, shares); }The
_deployfunction is used to calculate shares, so it should return the amount in the system’s 18-decimal format. However, the strategy always returns the amount in the token’s native decimal format. To address this, the_pendingAmountin the_supplyBorrowfunction should be converted to 18-decimal format. -
In the
_redeemInternalprocess, thewithdrawAmountpassed to_undeployis in 18-decimal format (sincetotalAssetsreturns 18-decimal values).function _redeemInternal( uint256 shares, address receiver, address holder, bool shouldRedeemETH ) private returns (uint256 retAmount) { if (shares == 0) revert InvalidAmount(); if (receiver == address(0)) revert InvalidReceiver(); if (balanceOf(holder) < shares) revert NotEnoughBalanceToWithdraw(); // Transfer shares to the contract if sender is not the holder if (msg.sender != holder) { if (allowance(holder, msg.sender) < shares) revert NoAllowance(); transferFrom(holder, msg.sender, shares); } // Calculate the amount to withdraw based on shares uint256 withdrawAmount = (shares * totalAssets()) / totalSupply(); if (withdrawAmount == 0) revert NoAssetsToWithdraw();
@> uint256 amount = _undeploy(withdrawAmount); …
Therefore, in the `undeploy` process, `deltaCollateralAmount` is in 18-decimal format. It is directly packed into `data` and passed to `_repayAndWithdraw` during the callback.
As a result, the `_withdraw` functions in `StrategyLeverageAAVEv3` and `StrategyLeverageMorphoBlue` should convert the input `amount` from 18-decimal format to the token's actual decimal format. Otherwise, the wrong amount will be withdrawn.
3. In the `_undeploy` process, `deltaDebt` and fees should be converted from 18-decimal format to the `debtToken`'s actual decimal format.
4. The `_convertToCollateral` and `_convertToDebt` functions expect the `amount` parameter to be in 18-decimal format, as required for calculations by `_toDebt` and `_toCollateral` using the oracle. However, before proceeding with the swap, the amount needs to be converted to the respective token's actual decimal format.
Additionally, `_convertToCollateral` receives the token's original decimal `amount` during the deploy process, leading to incorrect calculations by the oracle.
```solidity
/**
* @dev Internal function to convert the specified amount from Debt Token to the underlying collateral asset cbETH, wstETH, rETH.
*
* This function is virtual and intended to be overridden in derived contracts for customized implementation.
*
* @param amount The amount to convert from debtToken.
* @return uint256 The converted amount in the underlying collateral.
*/
function _convertToCollateral(uint256 amount) internal virtual returns (uint256) {
uint256 amountOutMinimum = 0;
if (getMaxSlippage() > 0) {
uint256 wsthETHAmount = _toCollateral(
IOracle.PriceOptions({maxAge: getPriceMaxAge(), maxConf: getPriceMaxConf()}),
amount,
false
);
amountOutMinimum = (wsthETHAmount * (PERCENTAGE_PRECISION - getMaxSlippage())) / PERCENTAGE_PRECISION;
}
// 1. Swap Debt Token -> Collateral Token
(, uint256 amountOut) = swap(
ISwapHandler.SwapParams(
_debtToken, // Asset In
_collateralToken, // Asset Out
ISwapHandler.SwapType.EXACT_INPUT, // Swap Mode
amount, // Amount In
amountOutMinimum, // Amount Out
bytes("") // User Payload
)
);
return amountOut;
}
/**
* @dev Internal function to convert the specified amount to Debt Token from the underlying collateral.
*
* This function is virtual and intended to be overridden in derived contracts for customized implementation.
*
* @param amount The amount to convert to Debt Token.
* @return uint256 The converted amount in Debt Token.
*/
function _convertToDebt(uint256 amount) internal virtual returns (uint256) {
uint256 amountOutMinimum = 0;
if (getMaxSlippage() > 0) {
uint256 ethAmount = _toDebt(
IOracle.PriceOptions({maxAge: getPriceMaxAge(), maxConf: getPriceMaxConf()}),
amount,
false
);
amountOutMinimum = (ethAmount * (PERCENTAGE_PRECISION - getMaxSlippage())) / PERCENTAGE_PRECISION;
}
// 1.Swap Colalteral -> Debt Token
(, uint256 amountOut) = swap(
ISwapHandler.SwapParams(
_collateralToken, // Asset In
_debtToken, // Asset Out
ISwapHandler.SwapType.EXACT_INPUT, // Swap Mode
amount, // Amount In
amountOutMinimum, // Amount Out
bytes("") // User Payload
)
);
return amountOut;
}
- The
_convertToCollateraland_convertToDebtfunctions default to returning theamountin the token’s actual decimal format. However, certain parts of the code assume they return the amount in 18-decimal format, leading to potential miscalculations. - The
_adjustDebtfunction should convert the flash loan amount from 18-decimal format to the token’s original decimal format. - The
_payDebtfunction will receive an amount in 18-decimal format, but when performing the swap, the amount is not converted to the token’s actual decimal format. This can lead to incorrect calculations during the swap process.
Recommended mitigation steps
It is recommended to align the vault’s decimals with the underlying token’s decimals instead of using 18 decimals. This alignment can significantly reduce the complexity of decimal conversions throughout the system.
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 vault's share decimals are fixed at 18 while the underlying token's decimals are not consistently converted, so functions mix 18-decimal and native-decimal amounts.
Impact
The system may not support tokens with decimals other than 18, leading to incorrect share calculations, wrong withdrawal amounts, and faulty swaps or debt adjustments.
What the attacker needed
Token decimals not 18; vault share decimals fixed at 18
The fix
The recommendation is to align the vault's decimals with the underlying token's decimals instead of using 18 decimals, and the project mitigated the issue via a pull request.
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.
Smart contracts often represent token amounts as whole numbers with a fixed number of decimal places. This report says the vault assumes every token uses 18 decimals, but some tokens use fewer or more, so the maths for deposits, withdrawals, and swaps goes wrong. That can mean users get the wrong number of shares or the contract tries to move the wrong amount of tokens.