Chainlink Oracles may return stale prices or may be unusable when aggregator roundId is less than 50
The contract used the full proxy roundId (which combines a phase ID and an aggregator round counter) when fetching historical prices, so subtracting a flat number could cross into a different phase and cause calls to revert. This could make new price feeds unusable or prevent recalibration, potentially allowing stale prices to be returned.
Originally disclosed on Code4rena
Submitted by nonseodion
WiseLending calibrates Oracles to get a heartbeat which it uses for checking the staleness of prices returned from the Oracle.
To calibrate, it fetches between 3-50 inclusive historical prices and picks the second largest update time among those prices. It calls _getIterationCount() to know the number of historical prices it’ll use. If the current _latestAggregatorRoundId is less than 50 (MAX_ROUND_COUNT) it uses _latestAggregatorRoundId else it uses 50.
res = _latestAggregatorRoundId < MAX_ROUND_COUNT
? _latestAggregatorRoundId
: MAX_ROUND_COUNT;
The issue with the snippet above is that _latestAggregatorRoundId will always be greater than 50 so the number of historical prices it uses will always be 50.
It’s always greater than 50 because it is fetched from the aggregator’s proxy contract. The roundIds returned from the proxy are a combination of the current aggregator’s roundId and phaseId. Check Chainlink docs for more info. getLatestRoundId() returns the roundId.
(
roundId
,
,
,
,
) = priceFeed[_tokenAddress].latestRoundData();
The roundId returned is used in the _recalibratePreview() function below to get previous roundIds. The iterationCount as we already mentioned will always be 50.
OracleHelper.sol#L620C1-L630C15
uint80 i = 1;
uint256 currentDiff;
uint256 currentBiggest;
uint256 currentSecondBiggest;
❌ while (i < iterationCount) {
uint256 currentTimestamp = _getRoundTimestamp(
_tokenAddress,
❌ latestRoundId - i
);
The problem with the above call is that the argument, latestRoundId-1 above may not have valid data for some rounds. So calls to the Chainlink Oracle for those rounds will revert.
This may occur because of the way proxy roundIds work.
Example:
- If the proxy returns 0x40000000000000010 as
roundId. - The
phaseIdis 4 (roundId>> 64). - The aggregator
roundIdis 16 (uint64(roundId)). - After 16 iterations in
_recalibratePreview(), thelatestRoundIdwill have a value of0x40000000000000000. When the price feed is called, with thisroundId, it will revert because it does not exist.
Check Chainlink docs for more info.
Thus, if the aggregator roundId derived from the proxy roundId is less than 50, _recalibratePreview() will revert. The caller will have to wait until it is greater than 50.
Impact
- For new price feeds, OracleHub won’t be able to set a heartbeat until the aggregator
roundIdis greater than 50. So the new price feed would be unusable for that period. - For price feeds that already have a heartbeat, they won’t be able to recalibrate during the period the aggregator
roundIdis less than 50. Which may allow the price feed return stale prices.
In both cases above the max amount of time user can wait for is 50x the official Chainlink heartbeat for the price feed, i.e. price feed heartbeat * 50.
For the BTC/ETH price feed this would be 50 days (24 hours * 50).
Recommended Mitigation Steps
Consider deriving the aggregator roundId from the proxy roundId and using that instead of the proxy roundId.
- res = _latestAggregatorRoundId < MAX_ROUND_COUNT
- ? _latestAggregatorRoundId
+ res = uint64(_latestAggregatorRoundId) < MAX_ROUND_COUNT
+ ? uint64(_latestAggregatorRoundId)
: MAX_ROUND_COUNT;
vonMangoldt (Wise Lending) commented:
I don’t understand the recommended mitigation steps. Its just typecasting uint64 to a uint80.
Referring to this, it is clear the
roundIdneeds to be trimmed to uint64.
@Trust - I was checking and came to the realization that the Chainlink Documentation only explains how the proxy queries the
getRoundDataon the Aggregator.However, all this logic is implemented on the Proxy itself, the
AggregatorProxy.getRoundData()is on charge of trimming theroundIddown touint64before querying the aggregator, but the proxy itself receives theroundIdas anuint80. Thus, the WiseOracle contract is correctly integrated with the Chainlink contract. All the required logic to query the data from the aggregator is contained within the chainlink proxy, any contract interacting with the proxy doesn’t need to trim the receivedroundId.Based on the Chainlink contracts and the way how WiseOracle queries the
getRoundData(), I think there is not any issue at all. The integration looks perfectly fine, and there is no need to implement any changes. If theroundIdwere to be trimmed to uint64, it would disrupt the queries and it would be querying a total different values; therefore, this report looks to be invalid.
I believe the root cause of the issue is correct. It is not safe to use
latestRoundId - iin calculations, as thelatestRoundIdis crafted of aphaseIDand a uint64 counter. By decreasing by a flat amount, we could find ourselves querying for an incorrectphaseID. Essentially, it is a logical overflow not detected because it occurs in internal data of a larger data type (uint80).
I see, then the root cause seems to be correct, but is the recommendation incomplete? If so, what would it be the correct way to mitigate this issue?
At this point, I’m not arguing the validity of the report, but rather want to understand what needs to be done to fully mitigate the root cause of this issue.
Mitigated 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 code did not separate the phase ID from the aggregator round counter before doing arithmetic on the roundId.
Impact
New price feeds could be unusable and existing feeds could return stale prices for up to 50 times the official heartbeat, though the report does not demonstrate an actual stale price being served.
What the attacker needed
Aggregator roundId less than 50; price feed with heartbeat set
The fix
The issue was mitigated by deriving the aggregator roundId from the proxy roundId before using it in calculations.
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 feed reports data in numbered rounds. The number you see from the main contract is actually two numbers packed together: a phase number and a round number. The code treated it as one simple counter and subtracted from it, which could accidentally point to a round that does not exist, making the call fail.
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.