Skip to content
Business logic Audit finding Intermediate

xERC4626.sol Some users may not be able to withdraw until rewardsCycleEnd the due to underflow in beforeWithdraw()

The xERC4626 contract's beforeWithdraw function subtracts the withdrawal amount from storedTotalAssets, which does not include unlocked rewards from the current cycle. If a user tries to withdraw more than storedTotalAssets, the subtraction underflows and reverts, temporarily locking funds until the rewards cycle ends.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

xERC4626.sol#L65-L68

function beforeWithdraw(uint256 amount, uint256 shares) internal virtual override {
    super.beforeWithdraw(amount, shares);
    storedTotalAssets -= amount;
}

xERC4626.sol#L78-L87

function syncRewards() public virtual {
    uint192 lastRewardAmount_ = lastRewardAmount;
    uint32 timestamp = block.timestamp.safeCastTo32();

    if (timestamp < rewardsCycleEnd) revert SyncError();

    uint256 storedTotalAssets_ = storedTotalAssets;
    uint256 nextRewards = asset.balanceOf(address(this)) - storedTotalAssets_ - lastRewardAmount_;

    storedTotalAssets = storedTotalAssets_ + lastRewardAmount_; // SSTORE
    ...

storedTotalAssets is a cached value of total assets which will only include the unlockedRewards when the whole cycle ends.

This makes it possible for storedTotalAssets -= amount to revert when the withdrawal amount exceeds storedTotalAssets, as the withdrawal amount may include part of the unlockedRewards in the current cycle.

Proof of Concept

Given:

  • rewardsCycleLength = 100 days
  • Alice deposit() 100 TRIBE tokens;
  • The owner transferred 100 TRIBE tokens as rewards and called syncRewards();
  • 1 day later, Alice redeem() with all shares, the transaction will revert at xERC4626.beforeWithdraw().

Alice’s shares worth 101 TRIBE at this moment, but storedTotalAssets = 100, making storedTotalAssets -= amount reverts due to underflow.

  1. Bob deposit() 1 TRIBE tokens;
  2. Alice withdraw() 101 TRIBE tokens, storedTotalAssets becomes 0;
  3. Bob can’t even withdraw 1 wei of TRIBE token, as storedTotalAssets is now 0.

If there are no new deposits, both Alice and Bob won’t be able to withdraw any of their funds until rewardsCycleEnd.

Recommended Mitigation Steps

Consider changing to:

function beforeWithdraw(uint256 amount, uint256 shares) internal virtual override {
    super.beforeWithdraw(amount, shares);
    uint256 _storedTotalAssets = storedTotalAssets;
    if (amount >= _storedTotalAssets) {
        uint256 _totalAssets = totalAssets();
        // _totalAssets - _storedTotalAssets == unlockedRewards
        lastRewardAmount -= _totalAssets - _storedTotalAssets;
        lastSync = block.timestamp;
        storedTotalAssets = _totalAssets - amount;
    } else {
        storedTotalAssets = _storedTotalAssets - amount;
    }
}

Joeysantoro (xTRIBE) confirmed, but disagreed with High severity, and commented:

This is a valid issue, although the risk is probably medium as the affected user could simply wait until the end of the cycle, and this would only occur in an extreme complete withdrawal of the contract.

As a soft mitigation, I would prefer to simply override maxWithdraw to return storedTotalAssets_.

0xean (judge) decreased severity to Medium and commented:

I agree with the sponsor here. Assets are not directly lost.

3 — High: Assets can be stolen/lost/compromised directly (or indirectly if there is a valid attack path that does not have hand-wavy hypotheticals).

2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.

So a medium severity seems more appropriate.


The original report as published on Code4rena, by WatchPug. 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 cached storedTotalAssets does not include unlocked rewards from the current cycle, so subtracting a withdrawal that includes those rewards causes an underflow.

Impact

Users may be unable to withdraw their funds until the rewards cycle ends, but assets are not directly lost.

What the attacker needed

Rewards cycle active; user withdraws more than storedTotalAssets

The fix

The code should be changed to handle withdrawals that exceed storedTotalAssets by adjusting lastRewardAmount and storedTotalAssets based on totalAssets, or by overriding maxWithdraw to return storedTotalAssets.

How it was found

  1. Alice deposits 100 TRIBE tokens.
  2. The owner transfers 100 TRIBE tokens as rewards and calls syncRewards().
  3. One day later, Alice tries to redeem all her shares, but the transaction reverts in beforeWithdraw().
  4. Bob deposits 1 TRIBE token.
  5. Alice withdraws 101 TRIBE tokens, making storedTotalAssets zero.
  6. Bob cannot withdraw even 1 wei because storedTotalAssets is zero.

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.

This is a smart contract bug where a savings vault tracks a cached total of deposits but not the rewards earned during the current period. When someone tries to withdraw more than that cached total, the code tries to subtract a larger number from a smaller one, which causes an error and stops the withdrawal. So users cannot access their full balance until the reward period ends.

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 WatchPug

View all

197 more disclosed reports, best first.

More from xTRIBE contest

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.