Submitted by zzzitron
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/Market.sol#L415-L421 
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/Market.sol#L385-L399 
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/bigBang/BigBang.sol#L781-L785 
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/singularity/SGLLiquidation.sol#L80-L87
An user can borrow via BigBang::borrow when there is no collateral amount from the users 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.
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
The result of the test is:
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.
The problem stems from the calculation error, where multiplies the users collateral share with EXCHANGE_RATE_PRECISION and collateralizationRate before calling yieldBox.toAmount.
It will give inflated amount, resulting in false solvency.
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/Market.sol#L415-L421
The same calculation happens in the Market::_computeMaxBorrowableAmount
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/Market.sol#L385-L399
In the BigBangs liquidating logic (e.i. in the BigBang::_updateBorrowAndCollateralShare), the conversion from the share of collateral to the asset is calculated correctly:
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/bigBang/BigBang.sol#L781-L785
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.
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/singularity/SGLLiquidation.sol#L70-L90
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).
0xRektora (Tapioca) confirmed
