Off-by-one bug prevents the _compareMinMax() from detecting Chainlink aggregators’ circuit-breaking events
The oracle helper's min/max comparison used strict inequalities, so it did not revert when a Chainlink aggregator reported exactly its configured minimum or maximum answer. During a flash crash the feed would keep returning the minimum answer, and the check would pass, letting users borrow against a wrongly reported price.
The WiseLending protocol implements the OracleHelper::_compareMinMax() to detect the circuit-breaking events of Chainlink aggregators when an asset price goes outside of pre-determined min/max values. For instance, in case of a significant price drop (e.g., LUNA crash), the asset price reported by the Chainlink price feed will continue to be at the pre-determined minAnswer instead of the actual price.
The _compareMinMax()’s objective is to prevent such a crash event that would allow a user to borrow other assets with the wrongly reported asset price. For more, refer to the case of Venus Protocol and Blizz Finance in the crash of LUNA.
However, the current implementation of the _compareMinMax() got an off-by-one bug that would prevent the function from detecting the mentioned Chainlink aggregators’ circuit-breaking events. In other words, the function will not revert the transaction if the flash crash event occurs as expected.
Proof of Concept
In the flash crash event, the Chainlink price feed will continue to return the _answer at the pre-determined minAnswer instead of the actual price. In other words, the possible minimum value of the _answer would be minAnswer.
Since the _compareMinMax() does not include the case of _answer == minAnswer (also, _answer==maxAnswer), the function could not detect whether or not the crash event happens.
@Trust - This is a valid medium issue as the _compareMinMax() was implemented incorrectly.
Specifically, the _compareMinMax() does not include the edge cases _answer==minAnswer and _answer==maxAnswer. So, the function cannot detect the minAnswer or maxAnswer as expected.
To elaborate, for example, the minAnswer the aggregator can report for the LINK (one of the tokens in scope) is 100000000000000 (10 ** 14). Suppose, in the event of a flash crash, the price of the LINK token drops significantly below the minAnswer (below 10 ** 14).
However, the price feed will continue to report the pre-determined minAnswer (not the actual price). Since the _compareMinMax() does not include the case _answer==minAnswer, it cannot detect this flash crash event.
In other words, the least reported price (i.e., _answer) will be the pre-determined minAnswer, not the actual price. Thus, the _compareMinMax() will never enter the if case and revert the transaction as expected because the _answer will never be less than the aggregator’s minAnswer.
function _compareMinMax(
IAggregator _tokenAggregator,
int192 _answer
)
internal
view
{
int192 maxAnswer = _tokenAggregator.maxAnswer();
int192 minAnswer = _tokenAggregator.minAnswer();
@> if (_answer > maxAnswer || _answer < minAnswer) { //@audit -- The least reported price (i.e., `_answer`) will be the `minAnswer`. So, the function will never enter the " if " case
revert OracleIsDead();
}
}
An example reference is here. As you can see, their recommendation includes the edge cases _answer==minAnswer and _answer==maxAnswer.
Note: to view the provided image, please see the original comment here.
The impact demonstrated is an incorrect validation check, which in the case maxAnswer/minAnswer are used by the feed, will make detecting black swans fail. As the team assumes those protections are in place, Medium severity is appropriate.
@00xSEV - The least reported price will be the pre-determined minAnswer. If you do not include the case ==, the _compareMinMax() will never enter the if case and revert the transaction. You cannot detect the black swan events without it.
The reported price will never be less than the minAnswer. (Without the ==, the if condition will always be false).
function _compareMinMax(
IAggregator _tokenAggregator,
int192 _answer
)
internal
view
{
int192 maxAnswer = _tokenAggregator.maxAnswer();
int192 minAnswer = _tokenAggregator.minAnswer();
@> if (_answer > maxAnswer || _answer < minAnswer) { //@audit -- The least reported price (i.e., `_answer`) will be the `minAnswer`. So, the function will never enter the " if " case
revert OracleIsDead();
}
}
The original report as published on Code4rena, by serial-coder. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
The comparison used strict greater-than and less-than instead of greater-than-or-equal and less-than-or-equal, so the boundary values minAnswer and maxAnswer were not treated as circuit-breaking.
Impact
The protocol could fail to detect a flash crash and allow borrowing against a stale minimum price; the judge rated this Medium because the team assumed the protection was in place, though the TWAP comparison is optional and may not catch it.
What the attacker needed
Chainlink feed with min/max answers; flash crash below minAnswer; TWAP comparison not configured
The fix
The comparison was changed to use greater-than-or-equal and less-than-or-equal, and the project says it mitigated the issue.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
A price feed has a floor and a ceiling. If the real price falls below the floor, the feed keeps reporting the floor value instead of the true price. The code was meant to stop trading when that happens, but it only stopped when the reported value was strictly outside the floor or ceiling, not when it was exactly at them, so the safety check never fired.
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.