viewPrice doesn’t always report dampened price
The Oracle contract's viewPrice function does not apply the dampened price mechanism until getPrice is called, so read-only functions that rely on viewPrice return incorrect values. This could lead to users withdrawing wrong amounts or liquidating incorrect debt, or cause reverts.
Submitted by Jeiwan
Oracle’s viewPrice function doesn’t report a dampened price until getPrice is called and today’s price is updated. This will impact the public read-only functions that call it:
- getCollateralValue;
- getCreditLimit (calls
getCollateralValue); - getLiquidatableDebt (calls
getCreditLimit); - getWithdrawalLimit.
These functions are used to get on-chain state and prepare values for write calls (e.g. calculate withdrawal amount before withdrawing or calculate a user’s debt that can be liquidated before liquidating it). Thus, wrong values returned by these functions can cause withdrawal of a wrong amount or liquidation of a wrong debt or cause reverts.
Proof of Concept
// src/test/Oracle.t.sol
function test_viewPriceNoDampenedPrice_AUDIT() public {
uint collateralFactor = market.collateralFactorBps();
uint day = block.timestamp / 1 days;
uint feedPrice = ethFeed.latestAnswer();
//1600e18 price saved as daily low
oracle.getPrice(address(WETH), collateralFactor);
assertEq(oracle.dailyLows(address(WETH), day), feedPrice);
vm.warp(block.timestamp + 1 days);
uint newPrice = 1200e18;
ethFeed.changeAnswer(newPrice);
//1200e18 price saved as daily low
oracle.getPrice(address(WETH), collateralFactor);
assertEq(oracle.dailyLows(address(WETH), ++day), newPrice);
vm.warp(block.timestamp + 1 days);
newPrice = 3000e18;
ethFeed.changeAnswer(newPrice);
//1200e18 should be twoDayLow, 3000e18 is current price. We should receive dampened price here.
// Notice that viewPrice is called before getPrice.
uint viewPrice = oracle.viewPrice(address(WETH), collateralFactor);
uint price = oracle.getPrice(address(WETH), collateralFactor);
assertEq(oracle.dailyLows(address(WETH), ++day), newPrice);
assertEq(price, 1200e18 * 10_000 / collateralFactor);
// View price wasn't dampened.
assertEq(viewPrice, 3000e18);
}
Recommended Mitigation Steps
Consider this change:
--- a/src/Oracle.sol
+++ b/src/Oracle.sol
@@ -89,6 +89,9 @@ contract Oracle {
uint day = block.timestamp / 1 days;
// get today's low
uint todaysLow = dailyLows[token][day];
+ if(todaysLow == 0 || normalizedPrice < todaysLow) {
+ todaysLow = normalizedPrice;
+ }
// get yesterday's low
uint yesterdaysLow = dailyLows[token][day - 1];
// calculate new borrowing power based on collateral factor
Well written report that explains the impact of this unlike the others. Will leave open for review.
08xmt (Inverse) confirmed and commented:
Fixed in https://github.com/InverseFinance/FrontierV2/pull/18.
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
Wrong values returned by these functions can cause withdrawal of a wrong amount or liquidation of a wrong debt or cause reverts.
What the attacker needed
None
The fix
The issue was fixed by updating the daily low in viewPrice before calculating the dampened price.
How it was found
- Set the price feed to 1600e18 and call getPrice to record the daily low.
- Advance time by one day, change the price to 1200e18, and call getPrice to record the new daily low.
- Advance time by another day, change the price to 3000e18.
- Call viewPrice and observe it returns 3000e18 instead of the dampened price.
- Call getPrice and observe it returns the dampened price (1200e18 adjusted by collateral factor).
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 has a function that shows the current price, but it doesn't apply a safety feature that limits how much the price can change in a day. Other functions that read this price get the wrong number, which could cause users to take out too much money or liquidate someone incorrectly.
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.