Skip to content
Business logic Audit finding Intermediate

lastRPS could be set to 0 accidentally

The reward distribution contract stores the current rewardsPerSecond in lastRPS when a distribution period ends, but if claim() is called again after the period ends, lastRPS is overwritten with zero. This prevents new reward deposits from restarting the distribution automatically, disrupting reward payouts until the owner manually resets the rate.

0xpiken Follow with zzebra83 and rscodes

Originally disclosed on Code4rena

Submitted by 0xpiken, also found by zzebra83 and rscodes

New reward distribution can not start automatically when lastRPS is set to 0 accidentally.

Proof of Concept

When ChefIncentivesController#claim() is called to vest reward for eligible user, _updateEmissions() is invoked first. This function checks if the current reward distribution has ended and, if so, stores the value of rewardsPerSecond into lastRPS for future use:

    function _updateEmissions() internal {
        if (block.timestamp > endRewardTime()) {
            _massUpdatePools();
@>          lastRPS = rewardsPerSecond;
@>          rewardsPerSecond = 0;
            return;
        }
        setScheduledRewardsPerSecond();
    }

When new rewards are deposited, the cached value in lastRPS should be restored to rewardsPerSecond to restart the reward distribution.

    function registerRewardDeposit(uint256 _amount) external onlyOwner {
        depositedRewards = depositedRewards + _amount;
        _massUpdatePools();
        if (rewardsPerSecond == 0 && lastRPS > 0) {
@>          rewardsPerSecond = lastRPS;
        }
        emit RewardDeposit(_amount);
    }

However, if somehow claim() is called twice continually when the current reward distribution ends, lastRPS will be set to 0 and registerRewardDeposit() can not restart new reward distribution.

Copy below codes to ChefIncentivesController.t.sol and run forge test --match-test test_setLastRPStoZero:

    function test_setLastRPStoZero() public {
        address alice = makeAddr("alice");
        address bob = makeAddr("bob");
        _excludeContracts(alice);
        _excludeContracts(bob);
        uint rps = incentivesController.rewardsPerSecond();
        incentivesController.addPool(address(0x1), 1000);
        incentivesController.addPool(address(0x2), 1000);
        incentivesController.setRewardsPerSecond(rps, true);
        loopToken.mint(address(incentivesController), 1000 ether);
        uint256 rewardAmount = 1000 ether;
        incentivesController.registerRewardDeposit(rewardAmount);

        incentivesController.start();

        vm.warp(block.timestamp + 30 days);

        address[] memory vaults = new address[](2);
        vaults[0] = address(0x1);
        vaults[1] = address(0x2);

        vm.mockCall(
            mockEligibilityDataProvider,
            abi.encodeWithSelector(EligibilityDataProvider.isEligibleForRewards.selector, alice),
            abi.encode(true)
        );

        vm.mockCall(
            mockEligibilityDataProvider,
            abi.encodeWithSelector(EligibilityDataProvider.refresh.selector, alice),
            abi.encode(true)
        );

        vm.mockCall(
            mockEligibilityDataProvider,
            abi.encodeWithSelector(EligibilityDataProvider.getDqTime.selector, alice),
            abi.encode(0)
        );

        vm.mockCall(
            mockEligibilityDataProvider,
            abi.encodeWithSelector(EligibilityDataProvider.isEligibleForRewards.selector, bob),
            abi.encode(true)
        );

        vm.mockCall(
            mockEligibilityDataProvider,
            abi.encodeWithSelector(EligibilityDataProvider.refresh.selector, bob),
            abi.encode(true)
        );

        vm.mockCall(
            mockEligibilityDataProvider,
            abi.encodeWithSelector(EligibilityDataProvider.getDqTime.selector, bob),
            abi.encode(0)
        );

        vm.mockCall(
            mockEligibilityDataProvider,
            abi.encodeWithSelector(IEligibilityDataProvider.lastEligibleStatus.selector, alice),
            abi.encode(true)
        );
        vm.mockCall(
            mockEligibilityDataProvider,
            abi.encodeWithSelector(IEligibilityDataProvider.lastEligibleStatus.selector, bob),
            abi.encode(true)
        );
        vm.prank(address(0x1));
        incentivesController.handleActionAfter(alice, 500 ether, 1000 ether);
        vm.prank(address(0x2));
        incentivesController.handleActionAfter(bob, 500 ether, 1000 ether);

        vm.warp(block.timestamp + 30 days);

        vm.mockCall(
            mockMultiFeeDistribution,
            abi.encodeWithSelector(IMultiFeeDistribution.vestTokens.selector, alice, 1000 ether),
            abi.encode(true)
        );
        vm.mockCall(
            mockMultiFeeDistribution,
            abi.encodeWithSelector(IMultiFeeDistribution.vestTokens.selector, bob, 1000 ether),
            abi.encode(true)
        );
        //@audit-info rewardsPerSecond is 1e16 before claim for alice
        assertEq(incentivesController.lastRPS(), 0);
        assertEq(incentivesController.rewardsPerSecond(), 10000000000000000);
        incentivesController.claim(alice, vaults);
        //@audit-info rewardsPerSecond is set to 0, and its previous value is stored in lastRPS for future use
        assertEq(incentivesController.lastRPS(), 10000000000000000);
        assertEq(incentivesController.rewardsPerSecond(), 0);
        incentivesController.claim(bob, vaults);
        //@audit-info however, lastRPS is updated to 0 when claim() is called again for bob.
        assertEq(incentivesController.lastRPS(), 0);
        assertEq(incentivesController.rewardsPerSecond(), 0);
        //@audit-info new reward deposit can not restart distribution
        loopToken.mint(address(incentivesController), 1000 ether);
        incentivesController.registerRewardDeposit(1000 ether);
        assertEq(incentivesController.rewardsPerSecond(), 0);
    }

Recommended Mitigation Steps

lastRPS should not be updated when rewardsPerSecond is 0:

    function _updateEmissions() internal {
        if (block.timestamp > endRewardTime()) {
            _massUpdatePools();
-           lastRPS = rewardsPerSecond;
+           if (rewardsPerSecond != 0) {
+               lastRPS = rewardsPerSecond;
+           }
            rewardsPerSecond = 0;
            return;
        }
        setScheduledRewardsPerSecond();
    }

Assessed type

Invalid Validation

amarcu (LoopFi) confirmed

0xAlix2 (warden) commented:

@Koolex - The report shows a valid scenario where lastRPS could end up being 0; however, this is intended, if the rewardsPerSecond is set to 0 then so does lastRPS, as it represents last RPS, used during refill after reserve empty.

Moreover, the report claims that new rewards can’t be registered by calling registerRewardDeposit, because of the 0 value of lastRPS. However, the owner could simply call setRewardsPerSecond to reset it, and everything will continue to work as expected. Hence, this is invalid.

zzebra83 (warden) commented:

I agree with some of your points, and I think your conclusion is based off how this report is written and also the mitigation it suggests.

When the admin calls setRewardsPerSecond, their intention could be to persist an RPS value for all epochs going forward. This RPS value will then be used when calculating rewards claimable by users. However, by calling the claim function more than once as mentioned in this report, both RPS and last RPS reset to 0; hence, reward distribution is effectively DOS’d.

The only way this can then be fixed is if the admin is made aware of the issue and then sets RPS value again by calling setRewardsPerSecond like you mentioned. But the issue is not fixed and will keep on happening and reward distribution to users will keep on getting disrupted and so on. Medium severity is appropriate, in my opinion.

Koolex (judge) commented:

Thank you for the input. Stays as-is.


The original report as published on Code4rena, by 0xpiken. 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 _updateEmissions function unconditionally sets lastRPS to rewardsPerSecond even when rewardsPerSecond is zero, overwriting the cached non-zero rate.

Impact

New reward deposits cannot automatically restart distribution, disrupting reward payouts until the owner manually resets the rate; the report notes this could happen accidentally.

What the attacker needed

Reward distribution period has ended; claim() called at least twice

The fix

The code should be fixed so that lastRPS is only updated when rewardsPerSecond is not zero.

How it was found

  1. Set up the contract with pools and a reward rate, then start distribution.
  2. Advance time past the end of the reward period.
  3. Call claim() for one user, which sets lastRPS to the previous rate and rewardsPerSecond to zero.
  4. Call claim() again for another user, which sets lastRPS to zero.
  5. Deposit new rewards and observe that rewardsPerSecond remains 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 that pays out rewards over time. It remembers the last reward rate in a variable called lastRPS so it can restart payouts when new rewards are added. A bug causes that remembered rate to be erased if someone claims rewards twice after a period ends, so new rewards don't automatically start paying out again.

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 0xpiken

View all

29 more disclosed reports, best first.

More from LoopFi

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.