Status DataClose notification

OpenEden Disclosed Report

Audit Report

maxStalePeriod single variable is harmful for checking multiple price feeds with different heartbeats

Target

https://github.com/OpenEdenHQ/openeden.usdoexpress.audit/tree/f3f31d2ac15e3253cba342229f9d05495f95d6fd

Vulnerability details

In the AssetRegistry contract, the maxStalePeriod variable is used to check the staleness of price feeds. The vulnerability arises when the contract uses multiple price feeds with different heartbeats. This makes the maxStalePeriod no longer effective for staleness checking.

    // @audit-issue max stale period is a single variable; it doesn't match
    // multiple price feeds because the price feeds have different heartbeats
    function setMaxStalePeriod(uint256 newStalePeriod) external onlyRole(MAINTAINER_ROLE) {
        if (newStalePeriod == 0) revert AssetRegistryInvalidStalePeriod(newStalePeriod);
        maxStalePeriod = newStalePeriod;
        emit MaxStalePeriodUpdated(newStalePeriod);
    }

Scenario:

  1. The contract uses price feeds for ETH and BNB, with ETH having a one-hour heartbeat and BNB having a 30-minute heartbeat.
  2. Admin sets the maxStalePeriod to 30 minutes.
  3. If ETH passes a 40-minute phase, the transaction will revert for ETH.
  4. Then, the admin sets the maxStalePeriod to one hour.
  5. If BNB passes 40 minutes (indicating a stale price), the transaction does not revert when it should.
  6. Conclusion: A single variable for checking the staleness of price feeds is ineffective for multiple price feeds with different heartbeats.

Validation steps

// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.18;

import {Test, console2} from "forge-std/Test.sol";
import "../contracts/interfaces/IPriceFeed.sol";
import "../contracts/extensions/AssetRegistry.sol";
import "lib/openzeppelin-contracts/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "../contracts/mock/MockERC20.sol";

contract MockOracle is IPriceFeed {
    int256 public price;
    uint8 public override decimals = 8;
    uint80 public roundId = 1;
    uint256 public updatedAt;

    constructor(int256 _price) {
        price = _price;
        updatedAt = block.timestamp;
    }

    function latestRoundData() external view override returns (uint80, int256, uint256, uint256, uint80) {
        return (roundId, price, 0, updatedAt, roundId);
    }

    function setPrice(int256 _price) external {
        price = _price;
        updatedAt = block.timestamp;
        roundId++;
    }

    function latestAnswer() external view override returns (uint256) {
        return uint256(price);
    }
}

contract AssetRegistryTest is Test {
    MockOracle ETHUSD;
    MockOracle BNBUSD;

    AssetRegistry assetRegistry;

    address admin = makeAddr("admin");
    address user = makeAddr("user");
    address ETH; 
    address BNB;

    function setUp() external {
        // Setting price feeds
        ETHUSD = new MockOracle(3000e8);
        BNBUSD = new MockOracle(1000e8);

        ETH = address(new MockERC20("Ethereum", "ETH"));
        BNB = address(new MockERC20("Binance", "BNB"));

        assetRegistry = new AssetRegistry();

        // Deploy proxy UUPS
        ERC1967Proxy proxyContract = new ERC1967Proxy(address(assetRegistry), "");
        assetRegistry = AssetRegistry(address(proxyContract));

        IAssetRegistry.AssetConfig memory assetConfigETH = IAssetRegistry.AssetConfig({
            asset: ETH,
            isSupported: true,
            priceFeed: address(ETHUSD)
        });

        IAssetRegistry.AssetConfig memory assetConfigBNB = IAssetRegistry.AssetConfig({
            asset: BNB,
            isSupported: true,
            priceFeed: address(BNBUSD)
        });

        // Set asset config
        vm.startPrank(admin);
        assetRegistry.initialize(admin);
        assetRegistry.setAssetConfig(assetConfigETH);
        assetRegistry.setAssetConfig(assetConfigBNB);
        vm.stopPrank();
    }

    function testMaxStalePeriodIsNotUseful() external {
        // Case 1: Stale max period is set to one hour
        vm.prank(admin);
        assetRegistry.setMaxStalePeriod(1 hours);

        // Advance time
        skip(40 minutes);

        // BNB heartbeat is 30 minutes; in this case, the BNB price is currently stale
        // and this operation should revert, but in reality, it doesn't
        uint valueBNB = assetRegistry.convertFromUnderlying(BNB, 1 ether);
        uint valueETH = assetRegistry.convertFromUnderlying(ETH, 1 ether);

        console2.log("ETH value with correct price: ", valueETH);
        console2.log("BNB value with stale price: ", valueBNB);

        // Case 2: Stale max period is set to 30 minutes
        vm.prank(admin);
        assetRegistry.setMaxStalePeriod(30 minutes);

        // Price update from heartbeat
        BNBUSD.setPrice(1100e8);
        ETHUSD.setPrice(3200e8);

        // Advance time
        skip(40 minutes);

        // Since the BNB heartbeat is 30 minutes, the operation for BNB should revert, and it does as expected
        vm.expectRevert();
        valueBNB = assetRegistry.convertFromUnderlying(BNB, 1 ether);

        // On the other hand, ETH has a one-hour heartbeat; in this case, this operation should not revert, but due to the max stale period being only 30 minutes, this operation unexpectedly reverts
        vm.expectRevert();
        valueETH = assetRegistry.convertFromUnderlying(ETH, 1 ether);
    }
}
Ran 1 test for test/AssetRegistry.t.sol:AssetRegistryTest
[PASS] testMaxStalePeriodIsNotUseFul() (gas: 130821)
Logs:
  ETH value with correct price:  3000000000000000000000
  BNB value with stale price:  1000000000000000000000

Attachments

Attachments are hidden

This information is not publicly available.

Comments are hidden

This information is not publicly available.