Chainlink oracle data feed is not sufficiently validated and can return stale price
The Oracle contract used Chainlink's deprecated latestAnswer and only checked that the price was positive, so it could accept a stale price. This could cause inaccurate credit and withdrawal limit calculations, potentially allowing a user's debt to be wrongly liquidated.
Submitted by rbserver, also found by d3e4, TomJ, pashov, sorrynotsorry, Aymen0909, c7e7eff, horsefacts, pedroais, minhtrng, dipp, 0xc0ffEE, Chom, immeas, imare, Olivierdem, Jeiwan, cccz, hansfriese, bin2chen, elprofesor, 141345, tonisives, catchup, 0xNazgul, Rolezn, Ruhum, Franfran, Wawrdog, idkwhatimdoing, carlitox477, Lambda, peanuts, saneryee, djxploit, eierina, cuteboiz, martin, M4TZ1P, Jujic, rokinot, ladboy233, codexploder, 0x1f8b, joestakey, leosathya, rvierdiiev, and 8olidity
Calling the Oracle contract’s viewPrice or getPrice function executes uint price = feeds[token].feed.latestAnswer() and require(price > 0, "Invalid feed price"). Besides that Chainlink’s latestAnswer function is deprecated, only verifying that price > 0 is true is also not enough to guarantee that the returned price is not stale. Using a stale price can cause the calculations for the credit and withdrawal limits to be inaccurate, which, for example, can mistakenly consider a user’s debt to be under water and unexpectedly allow the user’s debt to be liquidated.
To avoid using a stale answer returned by the Chainlink oracle data feed, according to Chainlink’s documentation:
- The
latestRoundDatafunction can be used instead of the deprecatedlatestAnswerfunction. roundIdandansweredInRoundare also returned. “You can checkansweredInRoundagainst the currentroundId. IfansweredInRoundis less thanroundId, the answer is being carried over. IfansweredInRoundis equal toroundId, then the answer is fresh.”- “A read can revert if the caller is requesting the details of a round that was invalid or has not yet been answered. If you are deriving a round ID without having observed it before, the round might not be complete. To check the round, validate that the timestamp on that round is not 0.”
https://github.com/code-423n4/2022-10-inverse/blob/main/src/Oracle.sol#L78-L105
function viewPrice(address token, uint collateralFactorBps) external view returns (uint) {
if(fixedPrices[token] > 0) return fixedPrices[token];
if(feeds[token].feed != IChainlinkFeed(address(0))) {
// get price from feed
uint price = feeds[token].feed.latestAnswer();
require(price > 0, "Invalid feed price");
// normalize price
uint8 feedDecimals = feeds[token].feed.decimals();
uint8 tokenDecimals = feeds[token].tokenDecimals;
uint8 decimals = 36 - feedDecimals - tokenDecimals;
uint normalizedPrice = price * (10 ** decimals);
uint day = block.timestamp / 1 days;
// get today's low
uint todaysLow = dailyLows[token][day];
// get yesterday's low
uint yesterdaysLow = dailyLows[token][day - 1];
// calculate new borrowing power based on collateral factor
uint newBorrowingPower = normalizedPrice * collateralFactorBps / 10000;
uint twoDayLow = todaysLow > yesterdaysLow && yesterdaysLow > 0 ? yesterdaysLow : todaysLow;
if(twoDayLow > 0 && newBorrowingPower > twoDayLow) {
uint dampenedPrice = twoDayLow * 10000 / collateralFactorBps;
return dampenedPrice < normalizedPrice ? dampenedPrice: normalizedPrice;
}
return normalizedPrice;
}
revert("Price not found");
}
https://github.com/code-423n4/2022-10-inverse/blob/main/src/Oracle.sol#L112-L144
function getPrice(address token, uint collateralFactorBps) external returns (uint) {
if(fixedPrices[token] > 0) return fixedPrices[token];
if(feeds[token].feed != IChainlinkFeed(address(0))) {
// get price from feed
uint price = feeds[token].feed.latestAnswer();
require(price > 0, "Invalid feed price");
// normalize price
uint8 feedDecimals = feeds[token].feed.decimals();
uint8 tokenDecimals = feeds[token].tokenDecimals;
uint8 decimals = 36 - feedDecimals - tokenDecimals;
uint normalizedPrice = price * (10 ** decimals);
// potentially store price as today's low
uint day = block.timestamp / 1 days;
uint todaysLow = dailyLows[token][day];
if(todaysLow == 0 || normalizedPrice < todaysLow) {
dailyLows[token][day] = normalizedPrice;
todaysLow = normalizedPrice;
emit RecordDailyLow(token, normalizedPrice);
}
// get yesterday's low
uint yesterdaysLow = dailyLows[token][day - 1];
// calculate new borrowing power based on collateral factor
uint newBorrowingPower = normalizedPrice * collateralFactorBps / 10000;
uint twoDayLow = todaysLow > yesterdaysLow && yesterdaysLow > 0 ? yesterdaysLow : todaysLow;
if(twoDayLow > 0 && newBorrowingPower > twoDayLow) {
uint dampenedPrice = twoDayLow * 10000 / collateralFactorBps;
return dampenedPrice < normalizedPrice ? dampenedPrice: normalizedPrice;
}
return normalizedPrice;
}
revert("Price not found");
}
https://github.com/code-423n4/2022-10-inverse/blob/main/src/Market.sol#L344-L347
function getCreditLimitInternal(address user) internal returns (uint) {
uint collateralValue = getCollateralValueInternal(user);
return collateralValue * collateralFactorBps / 10000;
}
https://github.com/code-423n4/2022-10-inverse/blob/main/src/Market.sol#L323-L327
function getCollateralValueInternal(address user) internal returns (uint) {
IEscrow escrow = predictEscrow(user);
uint collateralBalance = escrow.balance();
return collateralBalance * oracle.getPrice(address(collateral), collateralFactorBps) / 1 ether;
}
https://github.com/code-423n4/2022-10-inverse/blob/main/src/Market.sol#L353-L363
function getWithdrawalLimitInternal(address user) internal returns (uint) {
IEscrow escrow = predictEscrow(user);
uint collateralBalance = escrow.balance();
if(collateralBalance == 0) return 0;
uint debt = debts[user];
if(debt == 0) return collateralBalance;
if(collateralFactorBps == 0) return 0;
uint minimumCollateral = debt * 1 ether / oracle.getPrice(address(collateral), collateralFactorBps) * 10000 / collateralFactorBps;
if(collateralBalance <= minimumCollateral) return 0;
return collateralBalance - minimumCollateral;
}
Proof of Concept
The following steps can occur for the described scenario.
- Alice calls the
depositAndBorrowfunction to deposit some WETH as the collateral and borrows some DOLA against the collateral. - Bob calls the
liquidatefunction for trying to liquidate Alice’s debt. Because the Chainlink oracle data feed returns an up-to-date price at this moment, thegetCreditLimitInternalfunction calculates Alice’s credit limit accurately, which does not cause Alice’s debt to be under water. Hence, Bob’sliquidatetransaction reverts. - After some time, Bob calls the
liquidatefunction again for trying to liquidate Alice’s debt. This time, because the Chainlink oracle data feed returns a positive but stale price, thegetCreditLimitInternalfunction calculates Alice’s credit limit inaccurately, which mistakenly causes Alice’s debt to be under water. - Bob’s
liquidatetransaction is executed successfully so he gains some of Alice’s WETH collateral. Alice loses such WETH collateral amount unexpectedly because her debt should not be considered as under water if the stale price was not used.
Tools Used
VSCode
Recommended Mitigation Steps
Oracle.sol#L82-L83 and Oracle.sol#L116-L117 can be updated to the following code.
(uint80 roundId, int256 answer, , uint256 updatedAt, uint80 answeredInRound) = feeds[token].feed.latestRoundData();
require(answeredInRound >= roundId, "answer is stale");
require(updatedAt > 0, "round is incomplete");
require(answer > 0, "Invalid feed answer");
uint256 price = uint256(answer);
08xmt (Inverse) confirmed and commented:
Fixed in https://github.com/InverseFinance/FrontierV2/pull/19
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
Impact
A stale price could cause a user's debt to be incorrectly considered under water, allowing an attacker to liquidate the user's collateral and causing the user to lose funds unexpectedly.
What the attacker needed
A user has deposited collateral and borrowed; the oracle returns a stale but positive price; an attacker calls liquidate
The fix
The contract was fixed by using latestRoundData and checking that answeredInRound is at least roundId, updatedAt is greater than zero, and answer is positive.
How it was found
- Alice deposits WETH as collateral and borrows DOLA using depositAndBorrow.
- Bob tries to liquidate Alice's debt but the transaction reverts because the price is up-to-date and the debt is not under water.
- Later, Bob tries again and the oracle returns a positive but stale price, making Alice's debt appear under water.
- Bob's liquidation succeeds and he gains some of Alice's WETH collateral.
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 relied on an outside price feed but did not check whether the price was recent. If the price was old, the contract could miscalculate how much a user could borrow or withdraw, and someone might wrongly lose their collateral through liquidation.