Skip to content
Business logic Audit finding Intermediate

Options can be exercised preemptively due to timing delays

The option exercise cooldown was checked using block.timestamp while reward accounting used epoch numbers, so a user could exercise just after the time cooldown but before the epoch incremented. This let them claim rewards from the same epoch they joined, potentially draining rewards owed to others.

carrotsmuggler Follow with GalloDaSballo and deadrxsezzz

Originally disclosed on Code4rena

Submitted by carrotsmuggler, also found by GalloDaSballo and deadrxsezzz

When a user participates in the options system via TapiocaOptionBroker.sol, they mint an otap position in exchange for their tolp position. The time of creation is recorded, and the user is allowed to exercise the option only after a full epoch duration, which is a week.

 if (block.timestamp < oTAPPosition.entry + EPOCH_DURATION) {
        revert OneEpochCooldown();
    }

The variable netDepositedForEpoch records how much liquidity the system expects to hold in any given epoch, and is used to calculate the rewards for the users. On calling participate, this variable is increased for the next epoch, and decreased on the expiration epoch.

netDepositedForEpoch[epoch + 1][lock.sglAssetID] += int256(uint256(lock.ybShares));
netDepositedForEpoch[lastEpoch + 1][lock.sglAssetID] -= int256(uint256(lock.ybShares));

The issue is this accounting is done via epoch, while the cooldown is accounted via block.timestamp. So if a user participates at the very beginning of an epoch, their option will be exercisable after 7 days, which is very close to the time when the epoch number can be incremented. If the user is able to call exerciseOption before the epoch number is incremented, they can even participate in the current epoch! This is because the epoch number has not yet been updated, but the EPOCH_DURATION has already passed.

In this scenario, the user gets access to the rewards of the very same epoch they participated in, which should be impossible. They can also cause the contract to run out of rewards, as they are claiming rewards they shouldn’t be owed. The contract calculates rewards based on the current net deposit, which hasn’t been updated yet since the epoch number hasn’t been updated yet.

uint256 eligibleTapAmount = muldiv(tOLPLockPosition.ybShares, gaugeTotalForEpoch, netAmount);
eligibleTapAmount -= oTAPCalls[_oTAPTokenID][cachedEpoch];

Thus the user can take out rewards based on the wrong netAmount, which can lead to other users not getting their rewards, since the eligibleAmount is decreased by users not owed any rewards.

Proof of Concept

The attack is carried out in the following steps. Lets assume the current epoch has JUST started, and is epoch 1. Let’s assume the netAmount for this epoch is 100.

  1. Alice calls participate. The netAmount[2] (next epoch) is increased by her deposit amount of 100.
  2. Alice waits 1 week.
  3. Alice then calls exerciseOption. Lets assume newEpoch() hasn’t been called yet.
  4. Alice passes the OneEpochCooldown check. Alice gets assigned rewards. Alice’s rewards are = 100 * gaugeTotalForEpoch / netAmount[1], which is = 100 * total / 100 = total.
  5. The other depositors who hadn’t collected their rewards yet cannot collect them anymore. This is because there are not enough tokens for the rewards anymore. This is because the contract used netAmount[1] as the denominator while there are more than netAmount[1] shareholders collecting rewards.

Since this allows users to collect rewards in the same epoch and break the accounting, and can be timed very effectively, this is a high severity issue.

Recommended Mitigation Steps

The main issue is that the OneEpochCooldown is not measured in epochs and is measured in time instead, which will not necessarily always coincide. Can be fixed in a variety of methods:

  1. Check OneEpochCooldown based on the epoch number, not the timestamp.
  2. When calling exerciseOption, check if the epoch has completed. Similar to what is done in the participate function
if (_timestampToWeek(block.timestamp) > epoch) revert AdvanceEpochFirst();

Assessed type

Invalid Validation

0xRektora (Tapioca) confirmed via duplicate Issue #35

LSDan (judge) decreased severity to Medium

cryptotechmaker (Tapioca) commented via duplicate Issue #35:

PR here.


The original report as published on Code4rena, by carrotsmuggler. 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 cooldown was enforced with a timestamp comparison while reward eligibility and accounting used an epoch counter that could lag behind the timestamp.

Impact

A user could claim rewards from the same epoch they joined, which could leave other depositors unable to collect their rewards because the contract may not hold enough tokens.

What the attacker needed

A position in the options system; timing near the start of an epoch; epoch not yet advanced after one week

The fix

The cooldown should be checked against the epoch number rather than the timestamp, or exerciseOption should require that the epoch has been advanced first.

How it was found

  1. Alice calls participate at the very start of an epoch, increasing the next epoch's net deposit.
  2. Alice waits one week.
  3. Alice calls exerciseOption before the epoch number has been updated.
  4. Alice passes the cooldown check and receives rewards calculated with the current epoch's net deposit.

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.

An epoch is a fixed time period, like a week, used to group rewards. The contract checked that a week had passed using the clock, but it calculated rewards using the epoch number, which only changes when someone triggers an update. If that update was late, a user could take rewards from the same week they joined, which should not be allowed.

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 carrotsmuggler

View all

42 more disclosed reports, best first.

More from Tapioca Invitational

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.