Collateral can be locked in BigBang contract when debtStartPoint is nonzero
When a non-ETH BigBang market is registered with a nonzero debtStartPoint and the first borrower borrows less than that value, the getDebtRate function underflows and reverts, blocking all functions that accrue interest. As a result, the borrower's collateral becomes locked because repay and liquidate cannot proceed.
Submitted by zzzitron, also found by minhtrng, RedOneN, kutugu, bin2chen, 0xSky, 0xrugpull_detector, mojito_auditor, plainshift, KIntern_NA, carrotsmuggler, zzebra83, 0xRobocop, and chaduke
Following conditions have to be met for this issue to happen:
- This issue occurs when the BigBang market is not an ETH market.
Penrose.registerBigBang()being called withdataparam wheredata.debtStartPointis nonzero.- The first borrower borrows using
BigBang.borrow(), with function paramamount(borrow amount) has to be less thandebtStartPoint.
Now BigBang.getDebtRate() will always revert and the collateral from the first borrower is locked, because BigBang.getDebtRate() is used in BigBang._accrue(), and BigBang._accrue() is used in every function that involves totalBorrow like in BigBang.liquidate(), BigBang.repay().
The reason for the revert is that in BigBang.getDebtRate(), totalBorrow.elastic which gets assigned to the variable _currentDebt (line 186 BigBang.sol) will not be 0, and then on line 192 in the BigBang contract, the _currentDebt is smaller than debtStartPoint which causes the revert.
As a consequence the collateral is trapped as repay or liquidate requires to call accrue before hand.
Proof of Concept
The following gist contains a proof of concept to demonstrate this issue. A non-ETH bigbang market (wbtc market) is deployed with Penrose::registerBigBang. Note that the debtStartPoint parameter in the init data is non-zero (set to be 1e18).
First we set up the primary eth market: Some weth is minted and deposited to the ETH market. Then some assets were borrowed against the collateral. This is necessary condition for this bug to happen, which is the ETH market to have some borrowed asset. However, this condition is very likely to be fulfilled, as the primary ETH market would be deployed before any non-eth market.
Now, an innocent user is adding collateral and borrows in the non-eth market (the wbtc market). The issue occurs when the user borrows less than the debtStartPoint. If the user should borrow less than the debtStartPoint, the BigBang::accrue will revert and the collateral is trapped in this Market.
https://gist.github.com/zzzitron/a6d6377b73130819f15f1e5a2e2a2ba9
The bug happens here in the line 192 in the BigBang.
179 /// @notice returns the current debt rate
180 function getDebtRate() public view returns (uint256) {
181 if (_isEthMarket) return penrose.bigBangEthDebtRate(); // default 0.5%
182 if (totalBorrow.elastic == 0) return minDebtRate;
183
184 uint256 _ethMarketTotalDebt = BigBang(penrose.bigBangEthMarket())
185 .getTotalDebt();
186 uint256 _currentDebt = totalBorrow.elastic;
187 uint256 _maxDebtPoint = (_ethMarketTotalDebt *
188 debtRateAgainstEthMarket) / 1e18;
189
190 if (_currentDebt >= _maxDebtPoint) return maxDebtRate;
191
192 uint256 debtPercentage = ((_currentDebt - debtStartPoint) *
193 DEBT_PRECISION) / (_maxDebtPoint - debtStartPoint);
194 uint256 debt = ((maxDebtRate - minDebtRate) * debtPercentage) /
195 DEBT_PRECISION +
196 minDebtRate;
197
Recommended Mitigation Steps
Consider adding a require statement to BigBang.borrow() to make sure that the borrow amount has to be >= debtStartPoint.
// BigBang
// borrow
247 require(amount >= debtStartPoint);
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 getDebtRate calculation subtracts debtStartPoint from the current debt without checking that the current debt is at least debtStartPoint, causing an arithmetic underflow when the first borrow is smaller than debtStartPoint.
Impact
The first borrower's collateral is locked because repay and liquidate both require accrue, which reverts.
What the attacker needed
Non-ETH BigBang market; debtStartPoint set above zero; first borrow below debtStartPoint; ETH market has some debt
The fix
The borrow function should require that the borrow amount is at least debtStartPoint.
How it was found
- Deploy a non-ETH BigBang market through Penrose.registerBigBang with a nonzero debtStartPoint.
- Ensure the primary ETH market has some borrowed assets.
- Have a user add collateral and borrow an amount less than debtStartPoint.
- Observe that getDebtRate reverts, so accrue fails and repay or liquidate cannot be called.
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 market, interest is recalculated whenever someone repays or is liquidated. Here, a setting called debtStartPoint was set above zero, and the first person borrowed less than that amount. The interest calculation then tries to subtract a larger number from a smaller one, which the programming language rejects, so every action that needs interest to be updated fails and the collateral cannot be withdrawn.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.