Price Oracle Manipulation is a critical vulnerability in smart contracts that rely on external data feeds (oracles) to fetch prices or other information. In decentralized finance (DeFi), oracles are used to provide real-world data, such as asset prices, to smart contracts. However, if the data provided by the oracle is manipulated, it can result in incorrect contract behavior. Attackers can exploit oracles by manipulating the data they supply, leading to devastating consequences such as unauthorized withdrawals, excessive leverage, or even draining liquidity pools. Proper safeguards and validation mechanisms are essential to prevent this type of attack. Example (Vulnerable contract):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IPriceFeed {
function getLatestPrice() external view returns (int);
}
contract PriceOracleManipulation {
address public owner;
IPriceFeed public priceFeed;
constructor(address _priceFeed) {
owner = msg.sender;
priceFeed = IPriceFeed(_priceFeed);
}
function borrow(uint256 amount) public {
int price = priceFeed.getLatestPrice();
require(price > 0, "Price must be positive");
// Vulnerability: No validation or protection against price manipulation
uint256 collateralValue = uint256(price) * amount;
// Borrow logic based on manipulated price
// If an attacker manipulates the oracle, they could borrow more than they should
}
function repay(uint256 amount) public {
// Repayment logic
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IPriceFeed {
function getLatestPrice() external view returns (int);
}
contract PriceOracleManipulation {
address public owner;
IPriceFeed public priceFeed;
int public minPrice = 1000; // Set minimum acceptable price
int public maxPrice = 2000; // Set maximum acceptable price
constructor(address _priceFeed) {
owner = msg.sender;
priceFeed = IPriceFeed(_priceFeed);
}
function borrow(uint256 amount) public {
int price = priceFeed.getLatestPrice();
require(price > 0 && price >= minPrice && price <= maxPrice, "Price manipulation detected");
uint256 collateralValue = uint256(price) * amount;
// Borrow logic based on valid price
}
function repay(uint256 amount) public {
// Repayment logic
}
}