Skip to content
Business logic Audit finding Intermediate

User can receive more rewards through a mistake in the withdrawal logic

The withdrawal function resets the user's reward debt before calculating the difference, causing the full generated rewards to be cached instead of the unclaimed portion. An attacker can withdraw part of their shares to receive more reward tokens than they are owed.

joestakey Follow with cccz, usmannk and 13 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-02-olympus-judging/issues/13

Found by

joestakey, cccz, usmannk, Bahurum, Dug, ABA, psy4n0n, chaduke, carrot, minhtrng, jonatascm, GimelSec, ak1, RaymondFam, Ruhum, rvierdiiev

Summary

In the withdraw() function of the SingleSidedLiquidityVault the contract updates the reward state. Because of a mistake in the calculation, the user is assigned more rewards than they're supposed to.

Vulnerability Detail

When a user withdraws their funds, the _withdrawUpdateRewardState() function checks how many rewards those LP shares generated. If that amount is higher than the actual amount of reward tokens that the user claimed, the difference between those values is cached and the amount the user claimed is set to 0. That way they receive the remaining shares the next time they claim.

But, the contract resets the number of reward tokens the user claimed before it computes the difference. That way, the full amount of reward tokens the LP shares generated are added to the cache.

Here's an example:

  1. Alice deposits funds and receives 1e18 shares
  2. Alice receives 1e17 rewards and claims those funds immediately
  3. Time passes and Alice earns 5e17 more reward tokens
  4. Instead of claiming those tokens, Alice withdraws 5e17 (50% of her shares)

That executes _withdrawUpdateRewardState() with lpAmount_ = 5e17 and claim = false:

SOL
    function _withdrawUpdateRewardState(uint256 lpAmount_, bool claim_) internal {
        uint256 numInternalRewardTokens = internalRewardTokens.length;
        uint256 numExternalRewardTokens = externalRewardTokens.length;

        // Handles accounting logic for internal and external rewards, harvests external rewards
        uint256[] memory accumulatedInternalRewards = _accumulateInternalRewards();
        uint256[] memory accumulatedExternalRewards = _accumulateExternalRewards();
        for (uint256 i; i < numInternalRewardTokens;) {
            _updateInternalRewardState(i, accumulatedInternalRewards[i]);
            if (claim_) _claimInternalRewards(i);

            // Update reward debts so as to not understate the amount of rewards owed to the user, and push
            // any unclaimed rewards to the user's reward debt so that they can be claimed later
            InternalRewardToken memory rewardToken = internalRewardTokens[i];
            // @audit In our example, rewardDebtDiff = 3e17 (total rewards are 6e17 so 50% of shares earned 50% of reward tokens)
            uint256 rewardDebtDiff = lpAmount_ * rewardToken.accumulatedRewardsPerShare;

            // @audit 3e17 > 1e17
            if (rewardDebtDiff > userRewardDebts[msg.sender][rewardToken.token]) {

                // @audit userRewardDebts is set to 0 (original value was 1e17, the number of tokens that were already claimed)
                userRewardDebts[msg.sender][rewardToken.token] = 0;
                // @audit cached amount = 3e17 - 0 = 3e17.
                // Alice is assigned 3e17 reward tokens to be distributed the next time they claim
                // The remaining 3e17 LP shares are worth another 3e17 reward tokens.
                // Alice already claimed 1e17 before the withdrawal.
                // Thus, Alice receives 7e17 reward tokens instead of 6e17
                cachedUserRewards[msg.sender][rewardToken.token] +=
                    rewardDebtDiff - userRewardDebts[msg.sender][rewardToken.token];
            } else {
                userRewardDebts[msg.sender][rewardToken.token] -= rewardDebtDiff;
            }

            unchecked {
                ++i;
            }
        }

Impact

A user can receive more reward tokens than they should by abusing the withdrawal system.

Code Snippet

The issue is that userRewardDebts is set to 0 before it's used in the calculation of cachedUserRewards: https://github.com/sherlock-audit/2023-02-olympus/blob/main/src/policies/lending/abstracts/SingleSidedLiquidityVault.sol#L566-L619

SOL
    function _withdrawUpdateRewardState(uint256 lpAmount_, bool claim_) internal {
        uint256 numInternalRewardTokens = internalRewardTokens.length;
        uint256 numExternalRewardTokens = externalRewardTokens.length;

        // Handles accounting logic for internal and external rewards, harvests external rewards
        uint256[] memory accumulatedInternalRewards = _accumulateInternalRewards();
        uint256[] memory accumulatedExternalRewards = _accumulateExternalRewards();

        for (uint256 i; i < numInternalRewardTokens; ) {
            _updateInternalRewardState(i, accumulatedInternalRewards[i]);
            if (claim_) _claimInternalRewards(i);

            // Update reward debts so as to not understate the amount of rewards owed to the user, and push
            // any unclaimed rewards to the user's reward debt so that they can be claimed later
            InternalRewardToken memory rewardToken = internalRewardTokens[i];
            uint256 rewardDebtDiff = lpAmount_ * rewardToken.accumulatedRewardsPerShare;

            if (rewardDebtDiff > userRewardDebts[msg.sender][rewardToken.token]) {
                userRewardDebts[msg.sender][rewardToken.token] = 0;
                cachedUserRewards[msg.sender][rewardToken.token] +=
                    rewardDebtDiff -
                    userRewardDebts[msg.sender][rewardToken.token];
            } else {
                userRewardDebts[msg.sender][rewardToken.token] -= rewardDebtDiff;
            }

            unchecked {
                ++i;
            }
        }

        for (uint256 i; i < numExternalRewardTokens; ) {
            _updateExternalRewardState(i, accumulatedExternalRewards[i]);
            if (claim_) _claimExternalRewards(i);

            // Update reward debts so as to not understate the amount of rewards owed to the user, and push
            // any unclaimed rewards to the user's reward debt so that they can be claimed later
            ExternalRewardToken memory rewardToken = externalRewardTokens[i];
            uint256 rewardDebtDiff = lpAmount_ * rewardToken.accumulatedRewardsPerShare;

            if (rewardDebtDiff > userRewardDebts[msg.sender][rewardToken.token]) {
                userRewardDebts[msg.sender][rewardToken.token] = 0;
                cachedUserRewards[msg.sender][rewardToken.token] +=
                    rewardDebtDiff -
                    userRewardDebts[msg.sender][rewardToken.token];
            } else {
                userRewardDebts[msg.sender][rewardToken.token] -= rewardDebtDiff;
            }

            unchecked {
                ++i;
            }
        }
    }

Tool used

Manual Review

Recommendation

First calculate cachedUserRewards then reset userRewardDebts.

The original report as published on Sherlock, by joestakey. 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

Likely the contract sets the user's reward debt to zero before using it to compute the difference, so the subtraction uses zero instead of the actual debt.

Impact

A user can receive more reward tokens than they should by abusing the withdrawal system.

What the attacker needed

A user with LP shares; user has unclaimed rewards; user withdraws part of their shares

The fix

The contract should first calculate `cachedUserRewards` and then reset `userRewardDebts`.

How it was found

  1. Alice deposits funds and receives shares.
  2. Alice earns rewards and claims them immediately.
  3. More rewards accrue over time.
  4. Alice withdraws half of her shares without claiming the new rewards.
  5. The withdrawal function resets her reward debt to zero before calculating the difference.
  6. The full generated rewards are added to her cached rewards, so she can later claim more than she earned.

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 withdraw from a savings pool, the contract tracks how many rewards you have already received. A mistake in the order of operations makes it forget your previous claims, so it credits you again for rewards you already took. This lets you withdraw a portion of your deposit and end up with more rewards than you earned.

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 joestakey

View all

10 more disclosed reports, best first.

More from Olympus RBS 2.0

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.