Leverage borrowing with stale rate can atomically create bad debt with no prior positions and no investment
The protocol allows opening leveraged positions and removing collateral using a stale exchange rate within a validity window. An attacker could atomically create bad debt by exploiting a market price drop larger than the collateralisation buffer, without any prior position or investment.
Source: https://github.com/sherlock-audit/2024-02-tapioca-judging/issues/79
Found by
hyh
Summary
Leverage buying, borrow and collateral removal can increase riskness of a position, but are allowed to be performed with a stale exchange rate within rateValidDuration both in BB ansd SGL. This can provide a way for creating bad debt whenever actual rate dropped more than FEE_PRECISION - collateralizationRate (25%).
Particularly, an attacker can atomically extract value from the protocol without having any prior positions via leverage buying and then collateral removal.
Vulnerability Detail
Whenever Oracle reported rate is stale (oracle.get(oracleData) doesn't return an updated value), while market rate has dropped more than FEE_PRECISION - collateralizationRate (FEE_PRECISION scale), it is possible to atomically open borrow posiiton, buy collateral from the market and then remove extra collateral from the system with no prior positions and no investment.
The possibility of opening new positions, expecially leveraged ones, with a stale rate isn't required for BB or SGL core functionality, it constitutes a possible attack vector with very low business value of this possibility by itself.
Impact
When a collateral can be bought from the market at a rate lower than Oracle reported stale rate by more than FEE_PRECISION - collateralizationRate, the difference between rate mismatch and this buffer can be extracted from the protocol by anyone with no prepositioning or investment needed.
The probability of such a drop combined with Oracle staleness can be estimated as low, but once this happens given the absense of barriers to entry the attack will be carried out with high probability. The impact itself if direct loss of protocol principal funds as bad debt will be atomically created this way, which has to be covered by other assets of the system thereafter.
Likelihood: Low + Impact: High = Severity: Medium.
Code Snippet
updateExchangeRate() allows for stale rate within rateValidDuration:
function updateExchangeRate() public returns (bool updated, uint256 rate) {
(updated, rate) = oracle.get(oracleData);
if (updated) {
require(rate != 0, "Market: invalid rate");
exchangeRate = rate;
rateTimestamp = block.timestamp;
emit LogExchangeRate(rate);
} else {
>> require(rateTimestamp + rateValidDuration >= block.timestamp, "Market: rate too old");
// Return the old rate if fetching wasn't successful & rate isn't too old
rate = exchangeRate;
}
}
solvent check is used a the only control for a number of operations:
modifier solvent(address from, bool liquidation) {
updateExchangeRate();
_accrue();
_;
require(_isSolvent(from, exchangeRate, liquidation), "Market: insolvent");
}
Including atomic opening of the leveraged borrow position both in BB and SGL:
function buyCollateral(address from, uint256 borrowAmount, uint256 supplyAmount, bytes calldata data)
external
optionNotPaused(PauseType.LeverageBuy)
>> solvent(from, false)
notSelf(from)
returns (uint256 amountOut)
function buyCollateral(address from, uint256 borrowAmount, uint256 supplyAmount, bytes calldata data)
external
optionNotPaused(PauseType.LeverageBuy)
>> solvent(from, false)
notSelf(from)
returns (uint256 amountOut)
Tool used
Manual Review
Recommendation
Consider introducing another level of control and allowing leverage operations and new positions opening only when the Oracle reported rate is current, e.g.:
- function updateExchangeRate() public returns (bool updated, uint256 rate) {
+ function updateExchangeRate(bool updateRequired) public returns (bool updated, uint256 rate) {
(updated, rate) = oracle.get(oracleData);
if (updated) {
require(rate != 0, "Market: invalid rate");
exchangeRate = rate;
rateTimestamp = block.timestamp;
emit LogExchangeRate(rate);
} else {
- require(rateTimestamp + rateValidDuration >= block.timestamp, "Market: rate too old");
+ require(!updateRequired && rateTimestamp + rateValidDuration >= block.timestamp, "Market: rate too old");
// Return the old rate if fetching wasn't successful & rate isn't too old
rate = exchangeRate;
}
}
liquidation flag can be dropped from solvent modifier since it is used only with liquidation == false (while _isSolvent() is being called directly on liquidations both in BB and SGL), and replaced with the updateRequired flag proposed, e.g.:
- modifier solvent(address from, bool liquidation) {
+ modifier solvent(address from, bool updateRequired) {
- updateExchangeRate();
+ updateExchangeRate(updateRequired);
_accrue();
_;
- require(_isSolvent(from, exchangeRate, liquidation), "Market: insolvent");
+ require(_isSolvent(from, exchangeRate, false), "Market: insolvent");
}
All risk increase operations, i.e. leverage buying, borrow and collateral removal can utilize solvent modifiers with updateRequired == true, enforcing the Oracle reading to be current:
function buyCollateral(address from, uint256 borrowAmount, uint256 supplyAmount, bytes calldata data)
external
optionNotPaused(PauseType.LeverageBuy)
- solvent(from, false)
+ solvent(from, true)
notSelf(from)
returns (uint256 amountOut)
function borrow(address from, address to, uint256 amount)
external
optionNotPaused(PauseType.Borrow)
notSelf(to)
- solvent(from, false)
+ solvent(from, true)
returns (uint256 part, uint256 share)
function removeCollateral(uint256 share)
external
optionNotPaused(PauseType.RemoveCollateral)
- solvent(msg.sender, false)
+ solvent(msg.sender, true)
function borrow(uint256 amount)
external
optionNotPaused(PauseType.Borrow)
- solvent(msg.sender, false)
+ solvent(msg.sender, true)
function borrow(address from, address to, uint256 amount)
external
optionNotPaused(PauseType.Borrow)
- solvent(from, false)
+ solvent(from, true)
notSelf(to)
returns (uint256 part, uint256 share)
function removeCollateral(address from, address to, uint256 share)
external
optionNotPaused(PauseType.RemoveCollateral)
- solvent(from, false)
+ solvent(from, true)
allowedBorrow(from, share)
notSelf(to)
function buyCollateral(address from, uint256 borrowAmount, uint256 supplyAmount, bytes calldata data)
external
optionNotPaused(PauseType.LeverageBuy)
- solvent(from, false)
+ solvent(from, true)
notSelf(from)
returns (uint256 amountOut)
All the other solvent(from, false) instances can stay intact.
Discussion
sherlock-admin4
The protocol team fixed this issue in PR/commit https://github.com/Tapioca-DAO/Tapioca-bar/pull/357.
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 protocol allows risk-increasing operations (leverage buy, borrow, remove collateral) to proceed with a stale oracle rate as long as it is within the validity duration, without requiring a fresh rate.
Impact
The difference between the stale rate and the market rate beyond the buffer could be extracted, creating bad debt that must be covered by other system assets; the probability of such a drop combined with oracle staleness is low, but the impact is high.
What the attacker needed
Stale oracle rate within validity window; market price drop > collateralisation buffer; no prior position or investment needed
The fix
The protocol team fixed this issue in a pull request, likely by requiring a current oracle rate for risk-increasing operations.
How it was found
- Wait until the oracle rate is stale but within the validity window and the market rate has dropped more than the collateralisation buffer.
- Atomically open a leveraged borrow position using the stale rate.
- Buy collateral from the market at the lower market price.
- Remove extra collateral from the system, leaving bad debt.
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.
A stale exchange rate means the system uses an old price instead of the current one. If the real price has dropped a lot, an attacker can borrow and buy collateral at the old, higher price, then remove the extra collateral, leaving the protocol with a loss (bad debt).
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.