Skip to content
Smart contracts Audit finding Intermediate

Expiration calculation overflows if call option duration ≥ 195 days

A vault's duration in days is stored as an 8-bit integer, and when multiplied by the number of seconds in a day, the result overflows if the duration is 195 days or more. This causes the option purchase to revert, making the vault unusable and locking the creator's funds until they withdraw.

hickuphh3 Follow

Originally disclosed on Code4rena

Submitted by hickuphh3, also found by BondiPestControl, GimelSec, sseefried, and VAD37

vault.durationDays is of type uint8, thus allowing a maximum value of 255. 1 days = 86400, thus fitting into a uint24. Solc creates a temporary variable to hold the result of the intermittent multiplication vault.durationDays * 1 days using the data type of the larger operand.

In this case, the intermittent data type used would be uint24, which has a maximum value of 2**24 - 1 = 16777215. The maximum number allowable before overflow achieved is therefore (2**24 - 1) / 86400 = 194.

Proof of Concept

Insert this test case into BuyOption.t.sol

function testCannotBuyDueToOverflow() public {
  vm.startPrank(babe);
  bayc.mint(babe, 2);
  // duration of 195 days
  vaultId = c.createVault(2, address(bayc), premiumIndex, 195, strikeIndex, 0, Cally.TokenType.ERC721);
  vm.stopPrank();

  vm.expectRevert(stdError.arithmeticError);
  c.buyOption{value: premium}(vaultId);
}

Then run

forge test --match-contract TestBuyOption --match-test testCannotBuyDueToOverflow

Tidbit

This was the 1 high-severity bug that I wanted to mention at the end of the C4 TrustX showcase but unfortunately could not due to a lack of time :( It can be found in the vulnerable lottery contract on L39. Credits to Pauliax / Thunder for the recommendation and raising awareness of this bug =p

Reference

Article

Recommended Mitigation Steps

Cast the multiplication into uint32.

vault.currentExpiration = uint32(block.timestamp) + uint32(vault.durationDays) * 1 days;

outdoteth (Cally) confirmed and commented:

Agree that this is high risk - a user can unintentionally create vaults that would never have been able to have been filled and result in them losing funds because the vault creation was useless. They then also have to initiate a withdraw and then actually withdraw before they can create another vault.

In terms of gas prices at 100 gwei (which it frequently was a few months ago) the total gas cost of this bug/incorrect vault creation is not insignificant.

outdoteth (Cally) resolved:

This issue is fixed here; https://github.com/outdoteth/cally/pull/3

HardlyDifficult (judge) decreased severity to Medium and commented:

This is an easy way someone could create a vault where it’s not possible to buy an option, and without using an unreasonably high duration value. If this were to occur, the vault creator could immediately initiateWithdraw and then withdraw. No time delay is required and the only funds lost is the gas cost of those 3 transactions.

Lowering to 2 (Medium) since there’s an easy recovery and no assets lost.


The original report as published on Code4rena, by hickuphh3. 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 multiplication of a uint8 duration by 1 days is performed in a uint24 temporary variable, which overflows for durations of 195 days or more.

Impact

A user can unintentionally create a vault that can never be filled, losing the gas cost of creation and requiring a withdraw before creating another vault; the judge noted easy recovery with no assets lost.

What the attacker needed

Ability to create a vault; duration of 195 days or more

The fix

The multiplication should be cast to uint32 to prevent overflow.

How it was found

  1. Create a vault with a duration of 195 days.
  2. Attempt to buy an option from that vault.
  3. The transaction reverts with an arithmetic error.

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 stores the vault duration as a small number (up to 255 days). When it calculates the expiration time, it multiplies that number by the seconds in a day. If the duration is 195 days or more, the result is too big for the temporary storage type used, so the calculation wraps around to a small number. This makes buying an option fail, and the vault creator cannot use that vault.

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 hickuphh3

View all

43 more disclosed reports, best first.

More from Cally contest

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.