debtToMint incorrectly treats feeAdjustment decimals
The _debtToMint function incorrectly treats feeAdjustment as having no decimals, causing the calculated wSqueethToMint to be zero. This makes sqthToSell zero, so depositAuction cannot fill any market buy orders and the auction fails.
Source: https://github.com/sherlock-audit/2022-11-opyn-judging/issues/236
Found by
keccak123, hyh
Summary
_debtToMint() will return 0 decimals amounts and sqthToSell in depositAuction() will be insignificant, leading to ignoring the market orders used and depositing auction to be void as no external funding will be brought in.
Vulnerability Detail
feeAdjustment = _calcFeeAdjustment() is (squeethEthPrice * feeRate) / 10000 and have 18 decimals.
wSqueethToMint = (_amount * debt) / (collateral + (debt * feeAdjustment)) will have 36 decimals in numerator and the same 36 in denominator, yielding 0 decimals figure. That figure is sqthToSell, so no market buying orders will be ever filled.
Impact
depositAuction() will malfunction all the time, either reverting or producing less WETH and less CRAB than desired, i.e. there will be no deposit auction as market order part is needed to bring in the liquidity to be distributed.
Setting the severity to be high as this is system malfunction with material impact and no prerequisites.
Code Snippet
feeAdjustment is treated as if it has no decimals:
https://github.com/sherlock-audit/2022-11-opyn/blob/main/crab-netting/src/CrabNetting.sol#L476-L485
/**
* @dev calculates wSqueeth minted when amount is deposited
* @param _amount to deposit into crab
*/
function _debtToMint(uint256 _amount) internal view returns (uint256) {
uint256 feeAdjustment = _calcFeeAdjustment();
(,, uint256 collateral, uint256 debt) = ICrabStrategyV2(crab).getVaultDetails();
uint256 wSqueethToMint = (_amount * debt) / (collateral + (debt * feeAdjustment));
return wSqueethToMint;
}
while it has 18 decimals:
https://github.com/sherlock-audit/2022-11-opyn/blob/main/crab-netting/src/CrabNetting.sol#L795-L800
function _calcFeeAdjustment() internal view returns (uint256) {
uint256 feeRate = IController(sqthController).feeRate();
if (feeRate == 0) return 0;
uint256 squeethEthPrice = IOracle(oracle).getTwap(ethSqueethPool, sqth, weth, sqthTwapPeriod, true);
return (squeethEthPrice * feeRate) / 10000;
}
As sqthToSell to be insignificant, there will be no Squeeth selling at all:
https://github.com/sherlock-audit/2022-11-opyn/blob/main/crab-netting/src/CrabNetting.sol#L491-L504
function depositAuction(DepositAuctionParams calldata _p) external onlyOwner {
_checkOTCPrice(_p.clearingPrice, false);
/**
* step 1: get eth from mm
* step 2: get eth from deposit usdc
* step 3: crab deposit
* step 4: flash deposit
* step 5: send sqth to mms
* step 6: send crab to depositors
*/
uint256 initCrabBalance = IERC20(crab).balanceOf(address(this));
uint256 initEthBalance = address(this).balance;
uint256 sqthToSell = _debtToMint(_p.totalDeposit);
This renders sqth buying orders block void, i.e. it will be always _p.orders[0].quantity >= remainingToSell:
https://github.com/sherlock-audit/2022-11-opyn/blob/main/crab-netting/src/CrabNetting.sol#L504-L524
uint256 sqthToSell = _debtToMint(_p.totalDeposit);
// step 1 get all the eth in
uint256 remainingToSell = sqthToSell;
for (uint256 i = 0; i < _p.orders.length; i++) {
require(_p.orders[i].isBuying, "auction order not buying sqth");
require(_p.orders[i].price >= _p.clearingPrice, "buy order price less than clearing");
_checkOrder(_p.orders[i]);
if (_p.orders[i].quantity >= remainingToSell) {
IWETH(weth).transferFrom(
_p.orders[i].trader, address(this), (remainingToSell * _p.clearingPrice) / 1e18
);
remainingToSell = 0;
break;
} else {
IWETH(weth).transferFrom(
_p.orders[i].trader, address(this), (_p.orders[i].quantity * _p.clearingPrice) / 1e18
);
remainingToSell -= _p.orders[i].quantity;
}
}
require(remainingToSell == 0, "not enough buy orders for sqth");
Tool used
Manual Review
Recommendation
Consider adding decimals treatment, for example:
https://github.com/sherlock-audit/2022-11-opyn/blob/main/crab-netting/src/CrabNetting.sol#L476-L485
/**
* @dev calculates wSqueeth minted when amount is deposited
* @param _amount to deposit into crab
*/
function _debtToMint(uint256 _amount) internal view returns (uint256) {
uint256 feeAdjustment = _calcFeeAdjustment();
(,, uint256 collateral, uint256 debt) = ICrabStrategyV2(crab).getVaultDetails();
- uint256 wSqueethToMint = (_amount * debt) / (collateral + (debt * feeAdjustment));
+ uint256 wSqueethToMint = (_amount * debt) / (collateral + (debt * feeAdjustment) / 1e18);
return wSqueethToMint;
}
Discussion
thec00n
Nice find. Fix lgtm.
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 feeAdjustment value, which has 18 decimals, was used in a division without scaling, causing the result to be zero.
Impact
depositAuction() will malfunction all the time, either reverting or producing less WETH and less CRAB than desired, as no market orders will be filled.
What the attacker needed
None
The fix
The fix is to divide the feeAdjustment term by 1e18 to account for its decimals.
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 smart contracts, numbers often have fixed decimal places. Here, a fee value with 18 decimals was used in a calculation as if it had none, making the result zero. That zero stopped an auction from working because no one could buy the tokens being sold.
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.