Skip to content
Business logic Audit finding Intermediate

timeToEpochEnd calculates backwards, breaking protocol math

The timeToEpochEnd function in a smart contract returns zero when the epoch is still ongoing, instead of the remaining time. This causes all liquidations to be treated as future-epoch payouts, leading to incorrect accounting and potential reverts.

hansfriese Follow with 0xNazgul, obront and 1 other

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-10-astaria-judging/issues/50

Found by

hansfriese, 0xNazgul, obront, 0xRajeev

Summary

When a lien is liquidated, it calls timeToEpochEnd() to determine if a liquidation accountant should be deployed and we should adjust the protocol math to expect payment in a future epoch. Because of an error in the implementation, all liquidations that will pay out in the current epoch are set up as future epoch liquidations.

Vulnerability Detail

The liquidate() function performs the following check to determine if it should set up the liquidation to be paid out in a future epoch:

Solidity
if (PublicVault(owner).timeToEpochEnd() <= COLLATERAL_TOKEN.auctionWindow())

This check expects that timeToEpochEnd() will return the time until the epoch is over. However, the implementation gets this backwards:

Solidity
function timeToEpochEnd() public view returns (uint256) {
  uint256 epochEnd = START() + ((currentEpoch + 1) * EPOCH_LENGTH());

  if (epochEnd >= block.timestamp) {
    return uint256(0);
  }

  return block.timestamp - epochEnd;
}

If epochEnd >= block.timestamp, that means that there IS remaining time in the epoch, and it should perform the calculation to return epochEnd - block.timestamp. In the opposite case, where epochEnd <= block.timestamp, it should return zero.

The result is that the function returns 0 for any epoch that isn't over. Since 0 < COLLATERAL_TOKEN.auctionWindow()), all liquidated liens will trigger a liquidation accountant and the rest of the accounting for future epoch withdrawals.

Impact

Accounting for a future epoch withdrawal causes a number of inconsistencies in the protocol's math, the impact of which vary depending on the situation. As a few examples:

  • It calls decreaseEpochLienCount(). This has the effect of artificially lowering the number of liens in the epoch, which will cause the final liens paid off in the epoch to revert (and will let us process the epoch earlier than intended).
  • It sets the payee of the lien to the liquidation accountant, which will pay out according to the withdrawal ratio (whereas all funds should be staying in the vault).
  • It calls increaseLiquidationsExpectedAtBoundary(), which can throw off the math when processing the epoch.

Code Snippet

https://github.com/sherlock-audit/2022-10-astaria/blob/main/src/PublicVault.sol#L562-L570

https://github.com/sherlock-audit/2022-10-astaria/blob/main/src/AstariaRouter.sol#L388-L415

Tool used

Manual Review

Recommendation

Fix the timeToEpochEnd() function so it calculates the remaining time properly:

Solidity
function timeToEpochEnd() public view returns (uint256) {
  uint256 epochEnd = START() + ((currentEpoch + 1) * EPOCH_LENGTH());

  if (epochEnd <= block.timestamp) {
    return uint256(0);
  }

  return epochEnd - block.timestamp; //
}

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

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 condition in timeToEpochEnd is inverted: it returns zero when epochEnd is in the future, and returns a positive value when the epoch has already ended.

Impact

All liquidations are incorrectly set up for future epoch payouts, causing inconsistencies such as artificially lowering lien counts, misdirecting payee funds, and throwing off epoch processing math.

What the attacker needed

None

The fix

The timeToEpochEnd function should be fixed to return zero only when the epoch has ended, and otherwise return the remaining time.

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 function that should tell how much time is left in a period (epoch) gives the wrong answer: it says zero when there is still time left. Because of that, the contract mistakenly thinks every liquidation will be paid later, not now, which messes up its internal accounting.

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 hansfriese

View all

65 more disclosed reports, best first.

More from Astaria

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.