Skip to content
Smart contracts Audit finding Advanced

Intra-epoch rateAtTarget updates in CollateralTracker._updateInterestRate() allow compounding interest rate errors

The interest rate target was updated on every call to _accrueInterest even within the same 4-second epoch, while the epoch counter only advanced when time had passed. An attacker could trigger multiple calls in one block to compound the rate error and manipulate the borrow index, leading to incorrect debt calculations.

merlinboii Follow with Dulgiq

Originally disclosed on Code4rena

Submitted by merlinboii, also found by Dulgiq

Finding description

The CollateralTracker._accrueInterest() incorrectly updates s_marketState.rateAtTarget() on every call, even when multiple operations occur within the same epoch. This creates a compounding error where the interest rate calculation uses incorrect time references.

The protocol uses epochs of 4 seconds (block.timestamp >> 2) to batch interest calculations. However, there’s a critical mismatch in how rateAtTarget and marketEpoch are updated:

When block.timestamp falls between epoch boundaries, the elapsed time calculation in RiskEngine._borrowRate() becomes non-zero even though the epoch hasn’t advanced:

➜ uint256 block_timestamp = 1767776587
➜ uint256 currentEpoch = block_timestamp >> 2
➜ uint256 previousTime = currentEpoch << 2
➜ previousTime
Type: uint256
├ Hex: 0x695e2148
├ Hex (full word): 0x00000000000000000000000000000000000000000000000000000000695e2148
└ Decimal: 1767776584
➜ block_timestamp - previousTime <-- elapsed time
Type: uint256
├ Hex: 0x3
├ Hex (full word): 0x0000000000000000000000000000000000000000000000000000000000000003
└ Decimal: 3 <-- elapsed time

The issue can be summarized as follows:

  1. The s_marketState.rateAtTarget() gets updated immediately in CollateralTracker._updateInterestRate() and then update again in CollateralTracker._accrueInterest().
  2. However, s_marketState.marketEpoch() is only updated when deltaTime > 0 in CollateralTracker._calculateCurrentInterestState().
  3. Within the same epoch (currentEpoch == previousEpoch), deltaTime = 0, so the epoch doesn’t advance.
  4. But rateAtTarget still updates based on the non-zero elapsed calculation.

RiskEngine.sol L2187-L2255

Original code block has been omitted. View complete submission.

Impact

The borrowIndex becomes manipulated through repeated rateAtTarget updates within the same epoch. This index determines user interest payments and position solvency, leading to incorrect debt calculations.

Each call uses the previously manipulated rateAtTarget as the starting point, causing compounding errors. An attacker can trigger multiple calls within a 4-second window to amplify the effect. (This can happen through unintentional manipulation as well, simply by triggering CollateralTracker._accrueInterest() multiple times within the same block).

Although the elapsed time is capped at less than 4 seconds, which may appear insignificant, the impact is magnified because the rate calculation also depends on utilization. In a single transaction, the highest utilization value is used, allowing this mechanism to be exploited for a greater effect. (See the proof of concept: test_audit_accrueInterest_manipulateRateAtTarget_combineAttack.)

function _updateInterestRate() internal returns (uint128) {
    (uint128 avgRate, uint256 endRateAtTarget) = riskEngine().updateInterestRate(
@>      _poolUtilizationWad(),
        s_marketState
    );
    s_marketState = s_marketState.updateRateAtTarget(uint40(endRateAtTarget));
    return avgRate;
}

Recommended mitigation steps

Update rateAtTarget only if there is actual time passed. If the rate has already been updated within the current epoch, the update should be skipped.

Proof of Concept

Setup:

  • Put the snippet below into the protocol test suite: test/foundry/core/CollateralTracker.t.sol:CollateralTrackerTest

  • Run test:

    • forge test --mt test_audit_accrueInterest_manipulateRateAtTarget_stateProof -vvv
    • forge test --mt test_audit_accrueInterest_manipulateRateAtTarget_combineAttack -vvv
  • Note: The helper functions need to be added for the PoC to run (attached in the The full coded PoC section)

Results of running the PoC:

View detailed Proof of Concept

Panoptic mitigated:

Compute elapsed-time for IRM from epoch delta.

Status: Mitigation confirmed. Full details in reports from Valves, edoscoba, and Nyx.


The original report as published on Code4rena, by merlinboii. 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

Impact

The borrow index could be manipulated, leading to incorrect debt calculations and affecting position solvency; the effect is magnified because the rate also depends on utilization.

What the attacker needed

Ability to call _accrueInterest multiple times; same 4-second epoch

The fix

The protocol mitigated the issue by computing elapsed time for the interest rate model from the epoch delta, and the mitigation was confirmed.

How it was found

  1. Call CollateralTracker._accrueInterest() multiple times within the same 4-second epoch.
  2. Observe that rateAtTarget is updated each time even though the epoch does not advance.
  3. The borrowIndex becomes manipulated through the compounded rate updates.

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 calculates interest in short time windows called epochs. A variable that sets the interest rate was being changed even when no time had passed, so calling the function repeatedly in one block made the rate drift further each time. This could make borrowers' debt figures wrong.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by merlinboii

View all

9 more disclosed reports, best first.

More from Panoptic: Next Core

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.