ChainlinkAggregator: binary search for roundId does not work correctly and Oracle can even end up temporarily DOSed
The binary search algorithm in ChainlinkAggregator fails to find the best roundId and can return 0, causing unintended position changes or a temporary denial of service (DOS) of the oracle. An attacker could exploit this to prevent users from closing trades, potentially leading to loss of funds.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-05-perennial-judging/issues/4
Found by
roguereddwarf
Summary
When a phase switchover occurs, it can be necessary that phases need to be searched for a roundId with a timestamp as close as possible but bigger than targetTimestamp.
Finding the roundId with the closest possible timestamp is necessary according to the sponsor to minimize the delay of position changes:
The binary search algorithm is not able to find this best roundId which thereby causes unintended position changes.
Also it can occur that the ChainlinkAggregator library is unable to find a valid roundId at all (as opposed to only not finding the "best").
This would cause the Oracle to be temporarily DOSed until there are more valid rounds.
Vulnerability Detail
Let's look at the binary search algorithm: https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-oracle/contracts/types/ChainlinkAggregator.sol#L123-L156
The part that we are particularly interested in is: https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-oracle/contracts/types/ChainlinkAggregator.sol#L139-L149
Let's say in a phase there's only one valid round (roundId=1) and the timestamp for this round is greater than targetTimestamp
We would expect the roundId that the binary search finds to be roundId=1.
The binary search loop is executed with minRoundId=1 and maxRoundId=1001.
All the above conditions can easily occur in reality, they represent the basic scenario under which this algorithm executes.
minRoundId and maxRoundId change like this in the iterations of the loop:
minRoundId=1
maxRoundId=1001
->
minRoundId=1
maxRoundId=501
->
minRoundId=1
maxRoundId=251
->
minRoundId=1
maxRoundId=126
->
minRoundId=1
maxRoundId=63
->
minRoundId=1
maxRoundId=32
->
minRoundId=1
maxRoundId=16
->
minRoundId=1
maxRoundId=8
->
minRoundId=1
maxRoundId=4
->
minRoundId=1
maxRoundId=2
Now the loop terminates because
minRoundId + 1 !< maxRoundId
Since we assumed that roundId=2 is invalid, the function returns 0 (maxTimestamp=type(uint256).max):
In the case that latestRound.roundId is equal to the roundId=1 (i.e. same phase and same round id which could not be found) there would be no other valid rounds that the ChainlinkAggregator can find which causes a temporary DOS.
Impact
As explained above this would result in sub-optimal and unintended position changes in the best case.
In the worst-case the Oracle can be temporarily DOSed, unable to find a valid roundId.
This means that users cannot interact with the perennial protocol because the Oracle cannot be synced. So they cannot close losing trades which is a loss of funds.
The DOS can occur since the while loop searching the phases does not terminate: https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-oracle/contracts/types/ChainlinkAggregator.sol#L88-L91
Code Snippet
Tool used
Manual Review
Recommendation
I recommend to add a check if minRoundId is a valid solution for the binary search.
If it is, minRoundId should be used to return the result instead of maxRoundId:
// If the found timestamp is not greater than target timestamp or no max was found, then the desired round does
// not exist in this phase
- if (maxTimestamp <= targetTimestamp || maxTimestamp == type(uint256).max) return 0;
+ if ((minTimestamp <= targetTimestamp || minTimestamp == type(uint256).max) && (maxTimestamp <= targetTimestamp || maxTimestamp == type(uint256).max)) return 0;
+ if (minTimestamp > targetTimestamp) {
+ return _aggregatorRoundIdToProxyRoundId(phaseId, uint80(minRoundId));
+ }
return _aggregatorRoundIdToProxyRoundId(phaseId, uint80(maxRoundId));
}
After applying the changes, the binary search only returns 0 if both minRoundId and maxRoundId are not a valid result.
If this line is passed we know that either of both is valid and we can use minRoundId if it is the better result.
Discussion
roguereddwarf
Escalate for 10 USDC
I think this should be a "High" severity finding. The binary search lies at the core of the protocol. All functionality for users to open / close trades and liquidations relies on the Chainlink oracle.
By not finding a valid roundId obviously many users are put at risk of losing funds (-> not being able to close trades).
Similarly when an unintended (i.e. sub-optimal) roundId is found this leads to a similar scenario where settlements / liquidations occur at unintended prices.
In summary, the fact that the binary search algorithm lies at the core of the protocol and there is a very direct loss of funds makes me think this should be "High" severity.
sherlock-admin
Escalate for 10 USDC
I think this should be a "High" severity finding. The binary search lies at the core of the protocol. All functionality for users to open / close trades and liquidations relies on the Chainlink oracle.
By not finding a valid
roundIdobviously many users are put at risk of losing funds (-> not being able to close trades).Similarly when an unintended (i.e. sub-optimal)
roundIdis found this leads to a similar scenario where settlements / liquidations occur at unintended prices.In summary, the fact that the binary search algorithm lies at the core of the protocol and there is a very direct loss of funds makes me think this should be "High" severity.
You've created a valid escalation for 10 USDC!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
securitygrid
Comment from watson:
Chainlink: ETH/USD has been deployed for 3 years, and the current phaseID is only 6. The binary search is only triggered when this condition is met, that is, when a new phaseID is generated. This is infrequent.
This is from the chainlink docs: phaseId is incremented each time the underlying aggregator implementation is updated.
KenzoAgada
The issue is dependent upon a Chainlink changing of phase (which is quite infrequent) and the new phase having only 1 round. The impact as stated in the finding is that in those conditions, the product flywheel is jammed until further rounds are issued. (Then, the algorithm will correctly return round 2.) So the impact is only very temporary and rare DOS (which can impact user funds).
I think escalation is invalid and medium severity is appropriate.
arjun-io
To add here - the binary search is also a backup solution to simply checking if roundId + 1 exists in the previous phase (code). So additionally the last seen round in the previous phase has to be the very last roundID in that phase
jacksanford1
Agree with Medium due to Kenzo's reasons (unlikely and temporary situation which only indirectly could result in user funds loss due to liquidations). cc @roguereddwarf
roguereddwarf
Agreed
jacksanford1
Result: Medium Unique Unlikely and temporary situation which only indirectly could result in user funds loss due to liquidations is part of the reason why this is not being upgraded to a High.
sherlock-admin2
Escalations have been resolved successfully!
Escalation status:
- roguereddwarf: rejected
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
The binary search loop terminates when minRoundId + 1 is not less than maxRoundId, and the code only checks maxRoundId for validity, ignoring minRoundId, so it can return 0 even when minRoundId is a valid solution.
Impact
In the best case, this causes sub-optimal and unintended position changes; in the worst case, the Oracle can be temporarily DOSed, preventing users from interacting with the protocol and closing losing trades, which is a loss of funds.
What the attacker needed
Chainlink phase switchover occurs; New phase has only one valid round; Last seen round in previous phase is the last roundId
The fix
The binary search should check if minRoundId is a valid solution and return it if so, instead of only checking maxRoundId.
How it was found
- A phase switchover occurs in Chainlink, requiring a search for a roundId with a timestamp close to but greater than targetTimestamp.
- The binary search is executed with minRoundId=1 and maxRoundId=1001.
- The loop iteratively updates minRoundId and maxRoundId until minRoundId + 1 is not less than maxRoundId.
- The loop terminates with minRoundId=1 and maxRoundId=2.
- Since roundId=2 is invalid, the function returns 0, indicating no valid round found.
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.
The protocol uses a price feed from Chainlink to determine when to update positions. When Chainlink switches to a new phase, the code searches for the latest price round using a binary search. The search has a bug: it can miss the correct round or fail entirely, which can temporarily stop the oracle from working and prevent users from closing trades.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.