Front-running admin setPrice call allows a single compromised oracle to set any price, allowing the oracle manipulator to drain all protocol funds
The NFT price oracle allows a single compromised feeder to set an arbitrary price for a newly added asset by front-running the admin's setPrice call, because the first price update bypasses validity checks. This lets the attacker inflate the asset's value, deposit it as collateral, and borrow all protocol funds.
carlitox477 Follow with Rolezn, Jeiwan and 4 others
Originally disclosed on Code4rena
Submitted by carlitox477, also found by Rolezn, Jeiwan, imare, 141345, 0xDave, and nicobevi
https://github.com/code-423n4/2022-11-paraspace/blob/c6820a279c64a299a783955749fdc977de8f0449/paraspace-core/contracts/misc/NFTFloorOracle.sol#L195-L216 https://github.com/code-423n4/2022-11-paraspace/blob/c6820a279c64a299a783955749fdc977de8f0449/paraspace-core/contracts/misc/NFTFloorOracle.sol#L356-L364 https://github.com/code-423n4/2022-11-paraspace/blob/c6820a279c64a299a783955749fdc977de8f0449/paraspace-core/contracts/misc/NFTFloorOracle.sol#L402-L407 https://github.com/code-423n4/2022-11-paraspace/blob/c6820a279c64a299a783955749fdc977de8f0449/paraspace-core/contracts/misc/NFTFloorOracle.sol#L376-L386
The only way to update an NFT price is through _finalizePrice, which is called just by function setPrice.
Current code forces the admin to call function setPrice in order to update the price, but to call this function the current implementation requires that the asset is not paused.
What would happen if the admin setPrice was frontrunned by a feeder? The feeder who make the call will be allowed to set any price for the asset without any restriction. This obligates the protocol to consider next scenario:
- One feeder key or control has been compromised
- There is a new asset that the admin want to add to start feeding
- The new asset is being sold right now in a marketplace
Then, after admin calls addAssets and setPause functions, setPrice function can be frontrunned by the compromised feeder in order to set the price of the new asset to any price they want. This would allow the feeder’s address controller to drain all protocol funds.
Impact
Oracle decentralization can be bypassed, allowing setPrice function to be frontrunned by potencial oracle feeder (or person in control of oracle feeder), allowing the frontrunner to drain all protocol funds
This can happen because oracle decentralization control measures (requiring more than 3 oracle feeder to be deployed due to MIN_ORACLES_NUM value) can be bypassed.
Proof of Concept
-
Eve gets full control of one NFTFloorOracle feeder
-
Eve programs a bot to monitor when the admin of NFTFloorOracle contract calls
addAssetsandsetPausefunction -
ApeBoard lunch a new NFT collection called MONKEY
-
Some MONKEY collection tokens are in sale in Opean Sea
-
Paraspace decide to allow MONKEY collection to be used as collateral in the protocol and calls
addAssetsto add the new asset feed -
Paraspace admin decide to call
setPauseto be able to callsetPricefunction -
Eve’s program detect the new NFT addition and the correspondin unpausing, then proceeds to:
- Buy one token of MONKEY collection
- Call
setPricethrough the feeder he control, and set the asset price to the maximum value possible - Deposit the NFT in the protocol
- Borrow all the funds from the protocol
It is important to notice:
function setPrice(address _asset, uint256 _twap)
public
onlyRole(UPDATER_ROLE)
onlyWhenAssetExisted(_asset)
whenNotPaused(_asset)
{
bool dataValidity = false;
// @audit This if block won't be executed by controlled feeder
if (hasRole(DEFAULT_ADMIN_ROLE, msg.sender)) {
_finalizePrice(_asset, _twap);
return;
}
// @audit This can be bypassed giving the default twap value of the asset is zero
dataValidity = _checkValidity(_asset, _twap);
require(dataValidity, "NFTOracle: invalid price data");
// @audit add price to raw feeder storage, never reverts
_addRawValue(_asset, _twap);
uint256 medianPrice;
// set twap price only when median value is valid
// @audit _combine will return (true, _twap)
(dataValidity, medianPrice) = _combine(_asset, _twap);
if (dataValidity) {
// @audit Here the price is set, given dataValidity== true
_finalizePrice(_asset, medianPrice);
}
}
function _checkValidity(address _asset, uint256 _twap)
internal
view
returns (bool)
{
// @audit the attacker will send a value higher than zero
require(_twap > 0, "NFTOracle: price should be more than 0");
PriceInformation memory assetPriceMapEntry = assetPriceMap[_asset];
uint256 _priorTwap = assetPriceMapEntry.twap;
uint256 _updatedAt = assetPriceMapEntry.updatedAt;
uint256 priceDeviation;
//@audit first price is always valid as long as _twap > 0, which is the case for the attacker who wants to maximize NFT value in order to drain protocol funds
if (_priorTwap == 0 || _updatedAt == 0) {
// @audit dataValidity in setPrice will be set to true
return true;
}
// Rest of function code...
}
function _combine(address _asset, uint256 _twap)
internal
view
returns (bool, uint256)
{
FeederRegistrar storage feederRegistrar = assetFeederMap[_asset];
uint256 currentBlock = block.number;
//@audit first time the asset price is set any input parameter will return (true, _twap)
if (assetPriceMap[_asset].twap == 0) {
return (true, _twap);
}
//Rest of function...
}
Recommended Mitigation Steps
- Do not allow to unpause the asset unless its prices is different from zero.
- Force the admin to set the first price for all new assets added.
First step is easy, just a simple modification to setPause function:
function setPause(address _asset, bool _flag)
external
onlyRole(DEFAULT_ADMIN_ROLE)
{
assetFeederMap[_asset].paused = _flag;
+ if(_flag){
+ require(assetFeederMap[_asset].twap != 0, ERROR_CANNOT_UNPAUSE_IF_PRICE_EQ_ZERO);
+ }
emit AssetPaused(_asset, _flag);
}
This step forces to modify setPrice function and add a new function which might be called setEmergencyOrFirstPrice
// NftFloorOracle.sol
// Function to add
function setEmergencyOrFirstPrice(address _asset, uint256 _twap)
public
onlyRole(DEFAULT_ADMIN_ROLE)
onlyWhenAssetExisted(_asset)
{
require(_twap > 0, ERROR_TWAP_CANNOT_BE_ZERO());
_finalizePrice(_asset, _twap);
}
Step 2 is included in the previous solution, giving we only allow to unpause the asset in case assetFeederMap[_asset].twap != 0.
Doing this allows to simplify setPrice in order to save gas:
// New setPrice function
function setPrice(address _asset, uint256 _twap)
public
onlyRole(UPDATER_ROLE)
onlyWhenAssetExisted(_asset)
whenNotPaused(_asset)
{
- bool dataValidity = false;
- if (hasRole(DEFAULT_ADMIN_ROLE, msg.sender)) {
- _finalizePrice(_asset, _twap);
- return;
- }
- dataValidity = _checkValidity(_asset, _twap);
bool dataValidity = _checkValidity(_asset, _twap);
require(dataValidity, "NFTOracle: invalid price data");
// add price to raw feeder storage
_addRawValue(_asset, _twap);
uint256 medianPrice;
// set twap price only when median value is valid
(dataValidity, medianPrice) = _combine(_asset, _twap);
if (dataValidity) {
_finalizePrice(_asset, medianPrice);
}
}
Notes
- The scenario is focus on describing how front running setPrice function call lead to as many single points failures as feeders registered in the contract. Single point failures are described in this Chainlink document (point 3)
- If there is at least one feeder who works almost independently from ParaSpace protocol and is multisigned by people (not a governance contract) I consider this issue should be considered as high, given that all of them can have realistic motivations to drain protocol funds in their favour.
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 first price update for a new asset bypasses the normal validity and median checks, so a single feeder can set any price when the asset is unpaused.
Impact
A single compromised feeder could drain all protocol funds by front-running the admin's setPrice call for a new asset.
What the attacker needed
Compromised oracle feeder key; new asset added and unpaused; attacker buys one NFT of the asset
The fix
The asset should not be unpaused until its price is non-zero, and the admin should set the first price for all new assets.
How it was found
- Eve gains control of one NFTFloorOracle feeder.
- Eve monitors the admin's calls to addAssets and setPause.
- A new NFT collection is added and unpaused by the admin.
- Eve buys one token of the new collection.
- Eve calls setPrice through the controlled feeder to set the maximum price.
- Eve deposits the NFT and borrows all protocol funds.
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 oracle is a service that tells a smart contract how much an asset is worth. Here, the oracle lets any one of its authorised feeders set the first price for a new asset without cross-checking it against other feeders. If one feeder is compromised, they can set a fake high price, use the overvalued asset as collateral, and borrow more money than the asset is really worth.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.