Fetched price from the oracle is not stored in xRenzoDeposit
The xRenzoDeposit contract reads a fresh price from the oracle during deposits but never writes it back to the stored lastPrice and lastPriceTimestamp variables. If the oracle is later removed, deposits fall back to the stale stored price, and the divergence check compares new updates against that stale value, which can wrongly reject legitimate price updates.
fyamf Follow with Fassi_Security
Originally disclosed on Code4rena
Submitted by fyamf, also found by Fassi_Security
Failure to store the fetched price from the oracle in the storage variables of the xRenzoDeposit contract can result in several important issues.
Proof of Concept
Price Fetching on Deposit: When a deposit is made in xRenzoDeposit, the getMintRate function fetches the price of ezETH.
function deposit(
uint256 _amountIn,
uint256 _minOut,
uint256 _deadline
) external nonReentrant returns (uint256) {
//....
return _deposit(_amountIn, _minOut, _deadline);
}
function _deposit(
uint256 _amountIn,
uint256 _minOut,
uint256 _deadline
) internal returns (uint256) {
//......
// Fetch price and timestamp of ezETH from the configured price feed
(uint256 _lastPrice, uint256 _lastPriceTimestamp) = getMintRate();
//......
}
https://github.com/code-423n4/2024-04-renzo/blob/main/contracts/Bridge/L2/xRenzoDeposit.sol#L204
Oracle vs. Storage: If an oracle is defined, the function retrieves the price from the oracle. Otherwise, it reads the price from the storage variable lastPrice, which is updated by the updatePrice function.
function getMintRate() public view returns (uint256, uint256) {
// revert if PriceFeedNotAvailable
if (receiver == address(0) && address(oracle) == address(0)) revert PriceFeedNotAvailable();
if (address(oracle) != address(0)) {
(uint256 oraclePrice, uint256 oracleTimestamp) = oracle.getMintRate();
return
oracleTimestamp > lastPriceTimestamp
? (oraclePrice, oracleTimestamp)
: (lastPrice, lastPriceTimestamp);
} else {
return (lastPrice, lastPriceTimestamp);
}
}
function updatePrice(uint256 _price, uint256 _timestamp) external override {
if (msg.sender != receiver) revert InvalidSender(receiver, msg.sender);
_updatePrice(_price, _timestamp);
}
function _updatePrice(uint256 _price, uint256 _timestamp) internal {
//.....
lastPrice = _price;
lastPriceTimestamp = _timestamp;
//.....
}
First Issue - Inaccurate Price Usage: The first issue arises when the oracle is used to fetch the price but is not stored in the storage variables. Subsequent calls to getMintRate after the oracle is undefined may use the outdated price from storage instead of the most recent one fetched from the oracle. This leads to inaccurate pricing and potential financial losses. For example:
- Suppose, the price is updated to 1 ether now, so the storage variables are
lastPrice = 1 etherandlastPriceTimestamp = k(k is the timestamp of now). - Now, the oracle is defined by owner.
- 3 hours later, a deposit is made, so
getMintRateuses the oracle to fetch the price and returns the values from the oracle. For example the oracle returnsoraclePrice = 1.02 etherandoracleTimestamp = k + 3 hours. - The owner sets the orcale undefined again by setting its address to zero.
- After 2 hours, another deposit is made. So,
getMintRateuses the storage variable again (because the oracle address is zero) to fetch the price and reads the storage variables. So, it returnslastPrice = 1 etherandlastPriceTimestamp = k, instead of the recent fetched price by the oracle1.02 etherandk + 3 hours.
It shows that although the recent fetched price and timestamp is 1.02 ether and k + 3 hours, respectively, getMintRate returns the price of the time when they were stored in the storage variables during updating the price about 5 hours before.
Second Issue - Divergence Check: Another issue is that when the price is updated, the to-be-updated price is compared with the lastPrice. If, the oracle is used to fetch the price in between the updates, since the fetched price from the oracle is not stored in lastPrice variable, the to-be-updated price is compared with price stored in the storage variable before the oracle fetch. This undermines the trustworthiness of the divergence check mechanism.
function _updatePrice(uint256 _price, uint256 _timestamp) internal {
//....
// Check for price divergence - more than 10%
if (
(_price > lastPrice && (_price - lastPrice) > (lastPrice / 10)) ||
(_price < lastPrice && (lastPrice - _price) > (lastPrice / 10))
) {
revert InvalidOraclePrice();
}
//...
}
Third Issue - Manual Price Updates: The third issue arises when the price update fails due to inaccurate divergence checks. For example:
- Suppose the oracle is already defined and the price is updated to 1 ether now, so:
lastPrice = 1 etherandlastPriceTimestamp = k(k is the timestamp now) - After 5 hours, a deposit is made, so
getMintRateuses the oracle to fetch the price and returns the values from the oracle. For example:oraclePrice = 1.11 etherandoracleTimestamp = k + 5 hours. - After 1 hour, again the update price is called (because the on-chain transactions show that the interval for updates is 6 hours) to update the price. Suppose, this to-be-update price is
1.12 ether. But, since it compares1.12 etherwith1 etherat timek, its divergence is 12 percent, and it will revert. While, in reality, the divergence is between1.12 etherand1.11ether (the recent price fetched from the oracle) which is 0.9 percent.
In such cases, manual intervention is required by the admin to update the price, which adds complexity and overhead.
function updatePriceByOwner(uint256 price) external onlyOwner {
return _updatePrice(price, block.timestamp);
}
Recommended Mitigation Steps
To address these issues, it’s recommended to modify the getMintRate function to store the fetched price from the oracle in the storage variables. This ensures that the most recent price is used for calculations.
function getMintRate() public view returns (uint256, uint256) {
// revert if PriceFeedNotAvailable
if (receiver == address(0) && address(oracle) == address(0)) revert PriceFeedNotAvailable();
if (address(oracle) != address(0)) {
(uint256 oraclePrice, uint256 oracleTimestamp) = oracle.getMintRate();
if(oracleTimestamp > lastPriceTimestamp){
lastPrice = oraclePrice;
lastPriceTimestamp = oracleTimestamp;
return (oraclePrice, oracleTimestamp);
} else {
return (lastPrice, lastPriceTimestamp);
}
} else {
return (lastPrice, lastPriceTimestamp);
}
}
Assessed type
Oracle
jatinj615 (Renzo) acknowledged and commented:
In practical, we are not using both configurations together. But yeah, can be acknowledged. In my opinion, this is a low severity not a high.
alcueca (judge) decreased severity to Medium and commented:
The first issue described by the warden can be described as a governance error.
@alcueca - I agree with @jatinj615 here that this is a QA issue.
The first issue can be described as a governance issue as you said.
The second issue would be an issue in case of concurrent use of both the pull and push oracle configurations together as @jatinj615 pointed out, but that was never the intention here. The contract is working as designed, which is by only performing the divergence check on the push mechanism, against previous updates via the same mechanism.
The third issue is an inherent tradeoff of the divergence check - there is no way to prevent this issue while keeping the check, and it can be easily mitigated by the owner calling
updatePriceByOwner()in incremental steps of 10% as pointed out by the warden.
The first issue is a governance issue, as agreed.
There is no documentation on the intended use of the code. The assumption from the original warden that both oracles would be used simultaneously is not an outrageous one, I assumed the same. Under that assumption, the second issue pointed out is valid.
On the third issue, I can see how the code is expected to halt due to large price changes, with an admin calling
updatePriceByOwnerto validate that the changes are real.
Note: For full discussion, see here.
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 getMintRate function is declared view and returns the oracle price without writing it to lastPrice and lastPriceTimestamp, so the stored price only changes through the separate updatePrice path.
Impact
Deposits could be priced with an outdated stored value, and the divergence check could wrongly reject legitimate price updates, forcing manual admin intervention; the judge treated the first issue as a governance error and the second as valid only under the assumption that both oracle configurations are used together.
What the attacker needed
Owner configures and later removes the oracle; both oracle modes used together
The fix
The getMintRate function should be changed to write the fetched oracle price and timestamp into the storage variables when the oracle timestamp is newer.
How it was found
- Set the stored price and timestamp through the price update function.
- Configure the oracle address so deposits fetch the price from the oracle.
- Make a deposit so getMintRate returns the oracle price without saving it.
- Remove the oracle by setting its address to zero.
- Make another deposit and observe that the stale stored price is used instead of the recent oracle price.
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 smart contract keeps a saved copy of the price of its token. Sometimes it asks an outside price service (an oracle) for a newer price, but it does not save that newer number. If the outside service is switched off later, the contract goes back to using the old saved number, which may be hours out of date. That can make deposits use the wrong price and can make safety checks reject valid updates.
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.