API3 oracle timestamp can be set to future timestamp and block API3 Oracle usage to make code revert in underflow
The Api3OracleReader contract subtracts the oracle timestamp from the current block timestamp without checking which is larger, so if the oracle timestamp is in the future the subtraction underflows and the transaction reverts. An attacker could not directly set the timestamp, but a normal relayer update within the allowed one-hour future window would make price lookups revert, blocking any code that depends on the oracle.
ladboy233 Follow with 0x73696d616f
Originally disclosed on Code4rena
Submitted by ladboy233, also found by 0x73696d616f
In the Api3OracleReader.sol, the code assumes tha the timestamp returned from oracle is always in the past.
function getPrice_e36(address _token) external view returns (uint price_e36) {
// load and check
DataFeedInfo memory dataFeedInfo = dataFeedInfos[_token];
_require(dataFeedInfo.dataFeedId != bytes32(0), Errors.DATAFEED_ID_NOT_SET);
_require(dataFeedInfo.maxStaleTime != 0, Errors.MAX_STALETIME_NOT_SET);
// get price and token's decimals
uint decimals = uint(IERC20Metadata(_token).decimals());
// return price per token with 1e18 precisions
// e.g. 1 BTC = 35000 * 1e18 in USD_e18 unit
(int224 price, uint timestamp) = IApi3ServerV1(api3ServerV1).readDataFeedWithId(dataFeedInfo.dataFeedId);
// check if the last updated is not longer than the max stale time
_require(block.timestamp - timestamp <= dataFeedInfo.maxStaleTime, Errors.MAX_STALETIME_EXCEEDED);
// return as [USD_e36 per wei unit]
price_e36 = (price.toUint256() * ONE_E18) / 10 ** decimals;
}
Note the check:
// e.g. 1 BTC = 35000 * 1e18 in USD_e18 unit
(int224 price, uint timestamp) = IApi3ServerV1(api3ServerV1).readDataFeedWithId(dataFeedInfo.dataFeedId);
// check if the last updated is not longer than the max stale time
_require(block.timestamp - timestamp <= dataFeedInfo.maxStaleTime, Errors.MAX_STALETIME_EXCEEDED);
If timestamp is greater than block.timestamp, the transaction will revert and block oracle lookup on APi3OracleReader.sol.
The relayer on api3 side can update both oracle price timestamp and value.
Let us go over how the price is updated in Api3 code:
The function readDataFeedWithID basically just read the data from struct _dataFeeds[beaconId], in this function
/// @notice Reads the data feed with ID
/// @param dataFeedId Data feed ID
/// @return value Data feed value
/// @return timestamp Data feed timestamp
function _readDataFeedWithId(
bytes32 dataFeedId
) internal view returns (int224 value, uint32 timestamp) {
DataFeed storage dataFeed = _dataFeeds[dataFeedId];
(value, timestamp) = (dataFeed.value, dataFeed.timestamp);
require(timestamp > 0, "Data feed not initialized");
}
When the relayer updates the oracle data, first we are calling processBeaconUpdate.
Note that is a modifier onlyValidateTimestamp.
function processBeaconUpdate(
bytes32 beaconId,
uint256 timestamp,
bytes calldata data
)
internal
onlyValidTimestamp(timestamp)
returns (int224 updatedBeaconValue)
{
updatedBeaconValue = decodeFulfillmentData(data);
require(
timestamp > _dataFeeds[beaconId].timestamp,
"Does not update timestamp"
);
_dataFeeds[beaconId] = DataFeed({
value: updatedBeaconValue,
timestamp: uint32(timestamp)
});
}
The check ensures that only when the timstamp is from more than 1 hour, the update reverts.
/// @dev Reverts if the timestamp is from more than 1 hour in the future
modifier onlyValidTimestamp(uint256 timestamp) virtual {
unchecked {
require(
timestamp < block.timestamp + 1 hours,
"Timestamp not valid"
);
}
_;
}
What does this mean?
The timestamp of an oracle can be set to the future within an hour, the relayer does not have to be malicious, it is a normal part of updating data.
Suppose the current timestamp is 10000
1 hour = 3600 seconds
If the relayer set timestamp to 12000, the price update will go through
But the code:
_require(block.timestamp - timestamp <= dataFeedInfo.maxStaleTime, Errors.MAX_STALETIME_EXCEEDED);
Will revert when current timestamp is 10001.
10001 - 12000 will revert in underflow.
Recommended Mitigation Steps
If the oracle timestamp comes from the future, the code should consider it not stale.
Can change the code to:
if (block.timestamp > timestamp) {
_require(block.timestamp - timestamp <= dataFeedInfo.maxStaleTime, Errors.MAX_STALETIME_EXCEEDED);
}
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
The staleness check subtracts the oracle timestamp from block.timestamp without first confirming the oracle timestamp is not in the future, so an unsigned subtraction underflows and reverts.
What the attacker needed
Oracle timestamp set up to 1 hour ahead; a caller reads the price via Api3OracleReader
The fix
The staleness check should only subtract when block.timestamp is greater than the oracle timestamp, treating a future timestamp as not stale.
How it was found
- The API3 relayer submits a price update with a timestamp up to one hour ahead of the current block time, which the API3 contract accepts.
- A user or contract calls getPrice_e36 on Api3OracleReader for that token.
- The reader subtracts the future timestamp from block.timestamp, the subtraction underflows, and the call reverts.
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 reads a price from an outside data feed, and that feed includes a timestamp saying when the price was last updated. The contract checks the price is fresh by subtracting that timestamp from the current time, but it does not check which number is bigger. If the feed's timestamp is slightly in the future, the subtraction goes below zero and the whole transaction fails, so anything relying on that price stops working.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.