Skip to content
Business logic Audit finding Intermediate

_getNextFriday() returns wrong value when timestamp is between Monday 12am and 8am

The `_getNextFriday` function incorrectly returns the same week's Friday when the input timestamp falls between Monday 12am and 8am, instead of the following Friday. This causes option expiry dates to be set incorrectly, potentially disrupting the protocol.

yixxas Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-09-knox-judging/issues/47

Found by

yixxas

Summary

_getNextFriday(Monday) should return next Friday 8am timestamp. But if timestamp is on a Monday 12am - 8am, it will wrongly return the same week Friday.

Vulnerability Detail

friday8am - timestamp < 4 days check fails when the timestamp is between Monday 12am to Monday 8am, since friday8am - timestamp > 4 days. It will wrongly return the same week Friday instead of next Friday.

Impact

_setOptionParameters() relies on _getNextFriday() to set option's expiry. When the timestamp is between Monday 12am and Monday 8am, expiry date will be wrong causing malfunction of protocol.

Code Snippet

VaultInternal.sol#L746-L759

Solidity
    function _getNextFriday(uint256 timestamp) internal pure returns (uint256) {
        // dayOfWeek = 0 (sunday) - 6 (saturday)
        uint256 dayOfWeek = ((timestamp / 1 days) + 4) % 7;
        uint256 nextFriday = timestamp + ((7 + 5 - dayOfWeek) % 7) * 1 days;
        uint256 friday8am = nextFriday - (nextFriday % (24 hours)) + (8 hours);

        // If the timestamp is on a Friday or between Monday-Thursday
        // return Friday of the following week
        if (timestamp >= friday8am || friday8am - timestamp < 4 days) {
            friday8am += 7 days;
        }
        return friday8am;
    }
}

Tool used

Manual Review

Recommendation

Add 8 hours to the check when doing subtraction of friday8am - timestamp.

Diff
    function _getNextFriday(uint256 timestamp) internal pure returns (uint256) {
        // dayOfWeek = 0 (sunday) - 6 (saturday)
        uint256 dayOfWeek = ((timestamp / 1 days) + 4) % 7;
        uint256 nextFriday = timestamp + ((7 + 5 - dayOfWeek) % 7) * 1 days;
        uint256 friday8am = nextFriday - (nextFriday % (24 hours)) + (8 hours);

        // If the timestamp is on a Friday or between Monday-Thursday
        // return Friday of the following week
-       if (timestamp >= friday8am || friday8am - timestamp < 4 days) {
+       if (timestamp >= friday8am || friday8am - timestamp < (4 days + 8 hours)) {
            friday8am += 7 days;
        }
        return friday8am;
    }
}

Discussion

0xCourtney

https://github.com/KnoxFinance/knox-contracts/pull/82

rcstanciu

Reply from @arbitrary-CodeBeholder


I might consider changing the docstring of _getNextFriday to something like * @notice returns the next approaching Friday 8AM UTC timestamp + 7 days I think it's somewhat clearer, but that is often subjective. Other than that it looks good

The original report as published on Sherlock, by yixxas. 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 `friday8am - timestamp < 4 days` fails to account for the 8-hour offset, so for timestamps between Monday 12am and 8am the difference exceeds 4 days and the function does not add a week.

Impact

Option expiry dates will be set incorrectly when the timestamp is between Monday 12am and 8am, causing malfunction of the protocol.

What the attacker needed

None

The fix

The check should add 8 hours to the 4-day threshold, i.e., compare against `4 days + 8 hours`.

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 calculates the next Friday at 8am has a bug: if you call it early on a Monday morning (before 8am), it gives you the Friday of that same week instead of the next week's Friday. This means options could expire on the wrong date, which might break the protocol's logic.

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 yixxas

View all

13 more disclosed reports, best first.

More from Knox 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.