Skip to content
Business logic Audit finding Intermediate

CashManager.setEpochDuration functions has inconsistent output

The setEpochDuration function updates the epoch duration without first transitioning the current epoch, so the outcome depends on whether an external agent front-runs or back-runs the transaction. This can change the recorded duration and epoch number of recent epochs.

AkshaySrivastav Follow with bin2chen

Originally disclosed on Code4rena

Submitted by AkshaySrivastav, also found by bin2chen

The CashManager contract contains setEpochDuration function which is used by MANAGER_ADMIN role to update the epochDuration parameter.

  function setEpochDuration(uint256 _epochDuration) external onlyRole(MANAGER_ADMIN) {
    uint256 oldEpochDuration = epochDuration;
    epochDuration = _epochDuration;
    emit EpochDurationSet(oldEpochDuration, _epochDuration);
  }

The result of the setEpochDuration function execution can be impacted any external agent. The epochDuration is a crucial parameter of CashManager contract which determines the length of epochs in the contract.

The issue here is that the setEpochDuration function updates the epochDuration value without invoking the transitionEpoch function first.

This leads to two different end results and scenarios:

  1. When transitionEpoch is executed before setEpochDuration by an external agent (front-running).
  2. When transitionEpoch is executed after setEpochDuration by an external agent (back-running).

In these two different cases, the duration and epoch number of last few passed epochs can be impacted differently. The result becomes dependent upon the wish of the external agent.

The exact impact is demonstrated in the PoC below.

Proof of Concept

New test cases were added to forge-tests/cash/cash_manager/Setters.t.sol file.

  function test_bug_inconsistentOutputOf_setEpochDuration_Case1() public {
    // skip 1 epoch duration
    vm.warp(block.timestamp + 1 days);

    // here the setEpochDuration() txn is frontrunned by issuing the transitionEpoch() txn
    cashManager.transitionEpoch();
    // this is the setEpochDuration() txn which was frontrunned
    cashManager.setEpochDuration(2 days);
    assertEq(cashManager.currentEpoch(), 1);

    vm.warp(block.timestamp + 2 days);
    cashManager.transitionEpoch();
    assertEq(cashManager.currentEpoch(), 2);   // number of epochs after 3 days is 2
  }
  function test_bug_inconsistentOutputOf_setEpochDuration_Case2() public {
    // skip 1 epoch duration
    vm.warp(block.timestamp + 1 days);

    // here we wait for the setEpochDuration() to be validated on the network
    cashManager.setEpochDuration(2 days);
    // then we backrun the setEpochDuration() txn with transitionEpoch() txn
    cashManager.transitionEpoch();
    assertEq(cashManager.currentEpoch(), 0);

    vm.warp(block.timestamp + 2 days);
    cashManager.transitionEpoch();
    assertEq(cashManager.currentEpoch(), 1);   // number of epochs after 3 days is 1
  }

Recommended Mitigation Steps

The transitionEpoch function should be executed before executing the setEpochDuration function so that the values for passed epochs are recorded in a consistent way. This can be done by adding the updateEpoch modifier.

  function setEpochDuration(uint256 _epochDuration) external updateEpoch onlyRole(MANAGER_ADMIN) {
    uint256 oldEpochDuration = epochDuration;
    epochDuration = _epochDuration;
    emit EpochDurationSet(oldEpochDuration, _epochDuration);
  }

ali2251 (Ondo Finance) confirmed

ypatil12 (Ondo Finance) resolved


The original report as published on Code4rena, by AkshaySrivastav. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 setEpochDuration function updates the epoch duration without first calling transitionEpoch, so the state of the current epoch is not finalised before the duration changes.

Impact

The duration and epoch number of recent epochs can differ depending on whether an external agent front-runs or back-runs the setEpochDuration transaction, making the result dependent on that agent's choice.

What the attacker needed

MANAGER_ADMIN role calls setEpochDuration; external agent can front-run or back-run

The fix

The setEpochDuration function should call transitionEpoch before updating the duration, for example by adding the updateEpoch modifier.

How it was found

  1. Advance time by one epoch duration.
  2. Call transitionEpoch, then call setEpochDuration with a new duration.
  3. Advance time by the new duration and call transitionEpoch again; observe the epoch count.
  4. In a separate scenario, advance time by one epoch duration.
  5. Call setEpochDuration with the new duration, then call transitionEpoch.
  6. Advance time by the new duration and call transitionEpoch again; observe a different epoch count.

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 keeps track of time in chunks called epochs. The function that changes how long an epoch lasts does not first close the current epoch, so someone else can send a transaction just before or just after to change the result. This means the same change can produce different epoch histories depending on timing.

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.

More by AkshaySrivastav

View all

16 more disclosed reports, best first.

More from Ondo Finance

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.