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:
- When
transitionEpochis executed beforesetEpochDurationby an external agent (front-running). - When
transitionEpochis executed aftersetEpochDurationby 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
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
- Advance time by one epoch duration.
- Call transitionEpoch, then call setEpochDuration with a new duration.
- Advance time by the new duration and call transitionEpoch again; observe the epoch count.
- In a separate scenario, advance time by one epoch duration.
- Call setEpochDuration with the new duration, then call transitionEpoch.
- 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.