RTokenAsset price oracle can return a huge but valid high price when any underlying collateral’s price oracle timeout
When any underlying collateral's price oracle times out, the RTokenAsset price oracle can return a very high but valid price instead of an invalid one. This inflated price could distort the value of collateral that depends on the RToken's price.
Originally disclosed on Code4rena
Submitted by ronnyx2017
https://github.com/reserve-protocol/protocol/blob/9ee60f142f9f5c1fe8bc50eef915cf33124a534f/contracts/plugins/assets/RTokenAsset.sol#L163-L175 https://github.com/reserve-protocol/protocol/blob/9ee60f142f9f5c1fe8bc50eef915cf33124a534f/contracts/plugins/assets/RTokenAsset.sol#L53-L69 https://github.com/reserve-protocol/protocol/blob/9ee60f142f9f5c1fe8bc50eef915cf33124a534f/contracts/p1/BasketHandler.sol#L329-L351
The RTokenAsset is an implementation of interface IRTokenOracle to work as a oracle price feed for the little RToken. RTokenAsset implements the latestPrice function to get the oracle price and saved time from the cachedOracleData, which is updated by _updateCachedPrice function:
function _updateCachedPrice() internal {
(uint192 low, uint192 high) = price();
require(low != 0 && high != FIX_MAX, "invalid price");
cachedOracleData = CachedOracleData(
(low + high) / 2,
block.timestamp,
basketHandler.nonce(),
backingManager.tradesOpen(),
backingManager.tradesNonce()
);
}
The _updateCachedPrice gets the low and high prices from price(), and updates the oracle price to (low + high) / 2. And it checks low != 0 && high != FIX_MAX.
The RTokenAsset.price just uses the return of tryPrice as the low price and high price, if tryPrice reverts, it will return (0, FIX_MAX), which is an invalid price range for the oracle price check above. But if there is any underlying collateral’s price oracle reverts, for example oracle timeout, the RTokenAsset.price will return a valid but untrue (low, high) price range, which can be described as low = true_price * A1 and high = FIX_MAX * A2, A1 is bh.quantity(oracle_revert_coll) / all quantity for a BU and A2 is the BasketRange.top / RToken totalSupply.
Impact
The RToken oracle price will be about FIX_MAX / 2 when any underlying collateral’s price oracle is timeout. It is significantly more than the actual price. It will lead to a distortion in the price of collateral associated with the RToken, for example CurveStableRTokenMetapoolCollateral:
pairedAssetRegistry = IRToken(address(pairedToken)).main().assetRegistry();
function tryPairedPrice()
...
{
return pairedAssetRegistry.toAsset(pairedToken).price();
}
Proof of Concept
RToken.tryPrice gets the BU (low, high) price from basketHandler.price() first. BasketHandler._price(false) core logic:
for (uint256 i = 0; i < len; ++i) {
uint192 qty = quantity(basket.erc20s[i]);
(uint192 lowP, uint192 highP) = assetRegistry.toAsset(basket.erc20s[i]).price();
low256 += qty.safeMul(lowP, RoundingMode.FLOOR);
if (high256 < FIX_MAX) {
if (highP == FIX_MAX) {
high256 = FIX_MAX;
} else {
high256 += qty.safeMul(highP, RoundingMode.CEIL);
}
}
}
And the IAsset.price() should not revert. If the price oracle of the asset reverts, it just returns (0,FIX_MAX). In this case, the branch will enter high256 += qty.safeMul(highP, RoundingMode.CEIL); first. And it won’t revert for overflow because the Fixed.safeMul will return FIX_MAX directly if any param is FIX_MAX:
function safeMul(
...
) internal pure returns (uint192) {
if (a == FIX_MAX || b == FIX_MAX) return FIX_MAX;
So the high price is FIX_MAX, and the low price is reduced according to the share of qty.
Return to the RToken.tryPrice, the following codes uses basketRange() to calculate the low and high price for BU:
BasketRange memory range = basketRange(); // {BU}
// {UoA/tok} = {BU} * {UoA/BU} / {tok}
low = range.bottom.mulDiv(lowBUPrice, supply, FLOOR);
high = range.top.mulDiv(highBUPrice, supply, CEIL);
And the only thing has to be proofed is range.top.mulDiv(highBUPrice, supply, CEIL) should not revert for overflow in unit192. Now highBUPrice = FIX_MAX, according to the Fixed.mulDiv, if range.top <= supply it won’t overflow. And for passing the check in the RToken._updateCachedPrice(), the high price should be lower than FIX_MAX. So it needs to ensure range.top < supply.
The max value of range.top is basketsNeeded which is defined in RecollateralizationLibP1.basketRange(ctx, reg):
if (range.top > basketsNeeded) range.top = basketsNeeded;
And the basketsNeeded:RToken supply is 1:1 at the beginning. If the RToken has experienced a haircut or the RToken is undercollateralized at present, the basketsNeeded can be lower than RToken supply.
Recommended Mitigation Steps
Add a BU price valid check in the RToken.tryPrice:
function tryPrice() external view virtual returns (uint192 low, uint192 high) {
(uint192 lowBUPrice, uint192 highBUPrice) = basketHandler.price(); // {UoA/BU}
require(lowBUPrice != 0 && highBUPrice != FIX_MAX, "invalid price");
Assessed type
Context
cccz (judge) decreased severity to Medium and commented:
External requirement with oracle errors.
Enforce (
0, FIX_MAX) as “unpriced” during oracle timeout. PR: https://github.com/reserve-protocol/protocol/pull/917
Status: Mitigation confirmed. Full details in reports from ronnyx2017, RaymondFam and bin2chen.
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 RToken price calculation does not check that the basket price is valid before using it, so a timeout in one collateral oracle leads to an inflated high price.
Impact
The RToken oracle price could be about FIX_MAX / 2, significantly higher than the actual price, which may distort the price of collateral associated with the RToken.
What the attacker needed
An underlying collateral's price oracle times out; RToken is undercollateralized or has had a haircut
The fix
The issue was mitigated by enforcing (0, FIX_MAX) as 'unpriced' during oracle timeout.
How it was found
- An underlying collateral's price oracle times out and returns an invalid price range.
- The basket handler combines the invalid range with other prices, producing a high price of FIX_MAX.
- The RToken price calculation uses this high price and the basket range to compute a final high price that is below FIX_MAX but very large.
- The cached oracle price is updated with this inflated value.
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 price oracle is a service that tells a smart contract the current price of an asset. If one part of a basket of assets fails to report a price, the system should mark the whole price as invalid, but here it produced a very high price instead. That wrong price could make other contracts value the RToken incorrectly.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.