Skip to content
Business logic Audit finding Intermediate

The time available for a canceled withdrawal should not impact future unstaking processes

A cancelled withdrawal request remains in the queue and its old availability time is used as a floor for later withdrawal requests, so after the owner reduces the unstaking delay, a user who cancels and unstakes again still has to wait the old, longer delay. This can lock a user's funds for the original delay period even though the delay was shortened.

ether_sky Follow with Bauchibred

Originally disclosed on Code4rena

Submitted by ether_sky, also found by Bauchibred

Lines of Code

https://github.com/code-423n4/2024-07-reserve/blob/3f133997e186465f4904553b0f8e86ecb7bbacbf/contracts/p1/StRSR.sol#L279 https://github.com/code-423n4/2024-07-reserve/blob/3f133997e186465f4904553b0f8e86ecb7bbacbf/contracts/p1/StRSR.sol#L658 https://github.com/code-423n4/2024-07-reserve/blob/3f133997e186465f4904553b0f8e86ecb7bbacbf/contracts/p1/StRSR.sol#L368

Impact

When stakers want to unstake their StRSR, they cannot withdraw RSR immediately. Instead, each withdrawal enters a queue and will become available after the unstaking delay period. This queue operates on a FIFO basis, meaning earlier withdrawals are processed before later ones. Stakers can also cancel their withdrawals, and the RSR will be restaked immediately. However, even canceled withdrawals can still impact future withdrawal requests.

Proof of Concept

Suppose the current unstakingDelay is set to 1 day, and a user decides to unstake some amount of StRSR at time T (line 279).

function unstake(uint256 stakeAmount) external {
    address account = _msgSender();
    require(stakes[era][account] >= stakeAmount, "insufficient balance");
    _payoutRewards();
    _burn(account, stakeAmount);

    uint256 newStakeRSR = (FIX_ONE_256 * totalStakes + (stakeRate - 1)) / stakeRate;
    uint256 rsrAmount = stakeRSR - newStakeRSR;
    stakeRSR = newStakeRSR;

279:    (uint256 index, uint64 availableAt) = pushDraft(account, rsrAmount);
}

This request enters a queue, which we assume is empty at this point, making the withdrawal available at T + 1 day (line 658).

function pushDraft(address account, uint256 rsrAmount)
    internal
    returns (uint256 index, uint64 availableAt)
{
    CumulativeDraft[] storage queue = draftQueues[draftEra][account];
    index = queue.length;  // index = 0

    uint192 oldDrafts = index != 0 ? queue[index - 1].drafts : 0; // 0
    uint64 lastAvailableAt = index != 0 ? queue[index - 1].availableAt : 0; // 0
658:    availableAt = uint64(block.timestamp) + unstakingDelay; // T + 1 day

    if (lastAvailableAt > availableAt) {
        availableAt = lastAvailableAt;
    }
    queue.push(CumulativeDraft(uint176(oldDrafts + draftAmount), availableAt));
}

Now, let’s assume the user cancels this withdrawal at time T for testing purposes. Instead of removing the withdrawal from the queue, we simply increase the first available ID, which is a good approach and is easy to implement (line 368).

function cancelUnstake(uint256 endId) external {
    uint256 firstId = firstRemainingDraft[draftEra][account];
    CumulativeDraft[] storage queue = draftQueues[draftEra][account];
    if (endId == 0 || firstId >= endId) return;

    require(endId <= queue.length, "index out-of-bounds");

368:    firstRemainingDraft[draftEra][account] = endId;
}

Even though the queue technically still contains one item (the canceled withdrawal request), we can consider the queue empty in terms of available withdrawals.

Later, the owner updates the unstakingDelay to 1 hour. The user attempts to unstake again, expecting to withdraw just 1 hour later, which should be possible. However, the canceled withdrawal still impacts this new request.

In line 654, the index is 1 , and in line 657, lastAvailableAt is set to T + 1 day due to the canceled withdrawal. As a result, in line 658, the availableAt for the new withdrawal is calculated as T + 1 hour, but since this is less than T + 1 day, the new withdrawal is also pushed to be available 1 day later.

function pushDraft(address account, uint256 rsrAmount)
    internal
    returns (uint256 index, uint64 availableAt)
{
    CumulativeDraft[] storage queue = draftQueues[draftEra][account];
654:    index = queue.length;  // index = 1

    uint192 oldDrafts = index != 0 ? queue[index - 1].drafts : 0; // 0
657:    uint64 lastAvailableAt = index != 0 ? queue[index - 1].availableAt : 0; // T + 1 day
658:    availableAt = uint64(block.timestamp) + unstakingDelay; // T + 1 hour

    if (lastAvailableAt > availableAt) {
        availableAt = lastAvailableAt;
    }
    queue.push(CumulativeDraft(uint176(oldDrafts + draftAmount), availableAt));
}

This situation is clearly unfair to the user.

The availableAt of canceled withdrawals should not be considered when determining the availability of new withdrawals.

Please add below test to the test/ZZStRSR.test.ts

    describe('PushDraft Test', () => {
      it('Should use current unstakingDelay', async () => {
        // old unstakingDelay is 1 day
        const oldUnstakingDelay = 3600 * 24
        await stRSR.connect(owner).setUnstakingDelay(oldUnstakingDelay)  
        const amount: BigNumber = bn('100e18')
        await rsr.connect(addr1).approve(stRSR.address, amount)
        await stRSR.connect(addr1).stake(amount)

        const draftEra = 1
        const availableAtOfFirst = await getLatestBlockTimestamp() + oldUnstakingDelay + 1
        /**
         * Unstaking request enter a queue, and withdrawal become available 1 day later
         */
        await expect(stRSR.connect(addr1).unstake(amount))
          .emit(stRSR, 'UnstakingStarted')
          .withArgs(0, draftEra, addr1.address, amount, amount, availableAtOfFirst)

        /**
         * Cancel the unstaking to eliminate any pending withdrawals
         */
        await stRSR.connect(addr1).cancelUnstake(1)

        // new unstakingDelay is 1 hour
        const newUnstakingDelay = 3600
        await stRSR.connect(owner).setUnstakingDelay(newUnstakingDelay)  

        await rsr.connect(addr2).approve(stRSR.address, amount)
        await stRSR.connect(addr2).stake(amount)

        const availableAtOfFirstOfUser2 = await getLatestBlockTimestamp() + newUnstakingDelay + 1
        /**
         * Unstaking request enter a queue, and withdrawal become available 1 hour later for a second user
         */
        await expect(stRSR.connect(addr2).unstake(amount))
          .emit(stRSR, 'UnstakingStarted')
          .withArgs(0, draftEra, addr2.address, amount, amount, availableAtOfFirstOfUser2)

        /**
         * Although the first unstaking was canceled, its available time still impacts subsequent unstaking requests
         */
        await expect(stRSR.connect(addr1).unstake(amount))
          .emit(stRSR, 'UnstakingStarted')
          .withArgs(1, draftEra, addr1.address, amount, amount, availableAtOfFirst)
      })
    })

Recommended Mitigation Steps

function pushDraft(address account, uint256 rsrAmount)
    internal
    returns (uint256 index, uint64 availableAt)
{
    CumulativeDraft[] storage queue = draftQueues[draftEra][account];
    index = queue.length;

    uint192 oldDrafts = index != 0 ? queue[index - 1].drafts : 0;
   
-    uint64 lastAvailableAt = index != 0 ? queue[index - 1].availableAt : 0;
+    uint64 lastAvailableAt = index != 0 && firstRemainingDraft[draftEra][account] < index ? queue[index - 1].availableAt : 0;

    availableAt = uint64(block.timestamp) + unstakingDelay;

    if (lastAvailableAt > availableAt) {
        availableAt = lastAvailableAt;
    }
    queue.push(CumulativeDraft(uint176(oldDrafts + draftAmount), availableAt));
}

akshatmittal (Reserve) commented:

Issue confirmed.

Although, want to downgrade to Low given the requirements, actions and impact.

cccz (judge) decreased severity to Low and commented:

Considering the low likelihood (owner reduces unstakingDelay, user cancels withdrawals before that) and the earlier the cancellation, the lower the impact, it will be downgraded to Low.

ether_sky (warden) commented:

Hi @cccz - thanks for your review.

Let me clarify the impact and likelihood again. As seen in the code below, the minimum unstaking delay is 2 minutes, and the maximum is 1 year. There’s a significant difference between these two values.

uint48 private constant MIN_UNSTAKING_DELAY = 60 * 2; // {s} 2 minutes
uint48 private constant MAX_UNSTAKING_DELAY = 60 * 60 * 24 * 365; // {s} 1 year

Suppose the current unstaking delay is set to the 1 year. All stakers are operating under this delay now. For various reasons, the admin decides to reduce the unstaking delay to a shorter period, say 1 month. Stakers will realize they can withdraw their funds after 1 month instead of 1 year. They can cancel their existing withdrawal requests and attempt to unstake again. However, the original 1-year delay still applies to all stakers, meaning they cannot withdraw their funds after just 1 month—even if they cancel their old withdrawals. Their funds remain locked for 1 year.

Considering the low likelihood (owner reduces unstakingDelay, user cancels withdrawals before that) and the earlier the cancellation, the lower the impact, it will be downgraded to Low

Regarding the likelihood, users do not need to cancel their withdrawals before the owner changes the unstaking delay. Even if they cancel their withdrawals anytime after the delay is changed, the old delay still impacts all stakers.

As for the impact, this situation results in users’ funds being locked and creates a DoS issue.

Given these points, I believe the impact and likelihood are at least medium. I would appreciate it if you could reconsider this issue.

cccz (judge) increased severity to Medium and commented:

When the owner reduces unstakingDelay, the user will not be able to apply the latest unstakingDelay even if they cancel the previous withdrawal. If this is correct, I tend to raise it to Medium.

Reserve mitigated

Status: Mitigation confirmed. Full details in reports from Bauchibred, ether_sky and RadiantLabs.


The original report as published on Code4rena, by ether_sky. 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 pushDraft function uses the last queue entry's availableAt as a floor even when that entry has been cancelled and is before firstRemainingDraft.

Impact

A user's funds could remain locked for the original, longer unstaking delay even after the owner shortens it, creating a denial-of-service for that user.

What the attacker needed

Owner reduces unstaking delay; user cancels a prior withdrawal; user unstakes again

The fix

The pushDraft function should ignore the last queue entry's availableAt when that entry has been cancelled, so the current unstaking delay applies.

How it was found

  1. Set the unstaking delay to one day and stake an amount.
  2. Unstake, which queues a withdrawal available one day later.
  3. Cancel the unstake, which advances the first remaining draft index.
  4. Change the unstaking delay to one hour.
  5. Unstake again and observe the new withdrawal is still available one day later, not one hour later.

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.

When you unstake, your request joins a queue and becomes available after a waiting period. If you cancel, the request is skipped but not removed, and its old availability time is still used as a minimum for your next request. So if the waiting period is later shortened, your new request can still be stuck with the old, longer wait.

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 ether_sky

View all

11 more disclosed reports, best first.

More from Reserve Core

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.