It’s possible to borrow, redeem, transfer tokens and exit markets with outdated collateral prices and borrow interest
The Comptroller contract calculates collateral and borrow balances using stale oracle prices and unaccrued interest for markets other than the one being directly interacted with. An attacker could exploit this to borrow, redeem, or transfer more than allowed when a collateral token's price has fallen but not yet been updated.
0x73696d616f Follow with pontifex, 0xbepresent and 11 others
Originally disclosed on Code4rena
Submitted by 0x73696d616f, also found by pontifex, 0xbepresent, J4de, J4de, 0xkazim, peanuts, 0xadrii, rvierdiiev, rvierdiiev, rvierdiiev, volodya, rvierdiiev and rvierdiiev.
https://github.com/code-423n4/2023-05-venus/blob/main/contracts/Comptroller.sol#L199 https://github.com/code-423n4/2023-05-venus/blob/main/contracts/Comptroller.sol#L299 https://github.com/code-423n4/2023-05-venus/blob/main/contracts/Comptroller.sol#L324 https://github.com/code-423n4/2023-05-venus/blob/main/contracts/Comptroller.sol#L553 https://github.com/code-423n4/2023-05-venus/blob/main/contracts/Comptroller.sol#L1240 https://github.com/code-423n4/2023-05-venus/blob/main/contracts/Comptroller.sol#L1255 https://github.com/code-423n4/2023-05-venus/blob/main/contracts/Comptroller.sol#L578
Incorrect borrowBalance and token collateral values. This could lead to many different exploits, such as:
- Users with a collateral token that fell substantially in price can borrow another underlying token, whose price has not been updated, and earn profit.
- Users can borrow/redeem/transfer more if the interest/price was not updated.
Proof of Concept
In the Comptroller, the total collateral balance and borrow balance are calculated at _getHypotheticalLiquiditySnapshot(...). This function calculates these balances in the following loop:
for (uint256 i; i < assetsCount; ++i) {
VToken asset = assets[i];
// Read the balances and exchange rate from the vToken
(uint256 vTokenBalance, uint256 borrowBalance, uint256 exchangeRateMantissa) = _safeGetAccountSnapshot(
asset,
account
);
// Get the normalized price of the asset
Exp memory oraclePrice = Exp({ mantissa: _safeGetUnderlyingPrice(asset) });
// Pre-compute conversion factors from vTokens -> usd
Exp memory vTokenPrice = mul_(Exp({ mantissa: exchangeRateMantissa }), oraclePrice);
Exp memory weightedVTokenPrice = mul_(weight(asset), vTokenPrice);
// weightedCollateral += weightedVTokenPrice * vTokenBalance
snapshot.weightedCollateral = mul_ScalarTruncateAddUInt(
weightedVTokenPrice,
vTokenBalance,
snapshot.weightedCollateral
);
// totalCollateral += vTokenPrice * vTokenBalance
snapshot.totalCollateral = mul_ScalarTruncateAddUInt(vTokenPrice, vTokenBalance, snapshot.totalCollateral);
// borrows += oraclePrice * borrowBalance
snapshot.borrows = mul_ScalarTruncateAddUInt(oraclePrice, borrowBalance, snapshot.borrows);
// Calculate effects of interacting with vTokenModify
if (asset == vTokenModify) {
// redeem effect
// effects += tokensToDenom * redeemTokens
snapshot.effects = mul_ScalarTruncateAddUInt(weightedVTokenPrice, redeemTokens, snapshot.effects);
// borrow effect
// effects += oraclePrice * borrowAmount
snapshot.effects = mul_ScalarTruncateAddUInt(oraclePrice, borrowAmount, snapshot.effects);
}
}
As seen, the oracle price is not updated via calling updatePrice(...), nor is the borrow interest updated by calling AccrueInterest(...). Only the corresponding VToken that called the borrow(...), transfer(...) or redeem(...) has an updated price and interest, which could lead to critical inaccuracies for accounts with several VTokens.
Tools Used
Vscode, Hardhat
Recommended Mitigation Steps
Update the price and interest of every collateral, except the VToken that triggered the hook which has already been updated. Similarly to what is being done on healAccount(...):
for (uint256 i; i < userAssetsCount; ++i) {
userAssets[i].accrueInterest();
oracle.updatePrice(address(userAssets[i]));
}
Assessed type
Oracle
chechu (Venus) acknowledged via duplicate issue #88 and commented:
Oracle price of every market is updated on every action involving directly that market. We didn’t update the prices of every market (secondarily) used in some operations to save gas, assuming the price would be valid (there are mechanisms in the Oracles to avoid the use of old prices). We’ll monitor this topic to decide if our approach is enough or if we have to update the price of every market every time we invoke
_getHypotheticalLiquiditySnapshot.
0xean (judge) decreased severity to Medium
To share more info related to this topic:
As we said in #88, we assume the prices in the rest of the markets will be updated frequently because they are updated every time other users interact directly with these other markets
Moreover, “update” in our context only affects the TWAP oracle. As you can see here, our oracles system uses Chainlink, Binance Oracle, Pyth, and TWAP sources.
- Chainlink and Binance prices are not updated by Venus anyway. Chainlink and Binance update these price feeds following the classical rules of heartbeat and deviation (example here). In the Oracle system, we check the last time these prices were updated and discard them (reverting the TX) if they are staled.
- TWAP needs a proactive update, executed by Venus when the mentioned
oracle.updatePriceis invoked. If no one invokes this update function for an asset later used indirectly by another user, and the difference between the price offered by the TWAP oracle and the rest of the oracles (i.e. Chainlink) is greater than a threshold configure in our Oracle system, the TX will be also reverted. Could this generate a DoS? I suppose that potentially it can do it, but as soon as a user interacts with the “staled” asset in Venus the block will disappear.
@chechu - I believe I should mark #104 as a dupe of this as well. Since this issue talks about both prices updated and accruing interests.
we assume the prices in the rest of the markets will be updated frequently because they are updated every time other users interact directly with these other markets
This assumption I think has risks in which the wardens are calling out. It’s hard to say how real these risks are apriori without making assumptions about the usage of all the markets.
I think these issues should be batched together over concerns around
accrueInterestand price updates into a single M issue.
This assumption I think has risks in which the wardens are calling out. It’s hard to say how real these risks are apriori without making assumptions about the usage of all the markets.
@0xean - In my honest opinion, I think the risk is low, but I could understand the concern and the lack of a guarantee in the code.
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 Comptroller does not update oracle prices or accrue interest for all markets involved in a liquidity calculation, only for the market that triggered the action.
Impact
Users could borrow, redeem, or transfer more than allowed when prices or interest are stale, though the protocol argues that oracle staleness checks and frequent updates reduce the risk.
What the attacker needed
A collateral token whose price has fallen; the price or interest not yet updated
The fix
The Comptroller should update the price and accrue interest for every collateral asset involved in the calculation, not just the one that triggered the action.
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 protocol, your borrowing power depends on the value of your collateral. If the price of your collateral drops, you should be able to borrow less. This report says the code sometimes uses old prices for some assets, so a user could borrow more than they should before the price update happens.