Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0xRajkumar - Attacker Can Harm Other Delegator by Strategic Delegating and UnDelegating #238

Open
sherlock-admin2 opened this issue Sep 23, 2024 · 0 comments

Comments

@sherlock-admin2
Copy link
Contributor

sherlock-admin2 commented Sep 23, 2024

0xRajkumar

High

Attacker Can Harm Other Delegator by Strategic Delegating and UnDelegating

Summary and Vulnerability Detail

In L2Staking, we have the delegateStake function, which allows a delegator to delegate their stake to a delegatee. Whenever a delegation is made, we calculate the effectiveEpoch by adding 1 to the currentEpoch. The delegator can also undelegate using undelegateStake, where the effectiveEpoch is calculated the same way as in delegateStake.

An attacker can delegate just 1 second before a new effectiveEpoch starts and then undelegate immediately after the new effectiveEpoch begins, without staying delegated for the entire epoch.

This may seem fair, but it harms other delegators who are staking for the entire epoch to earn rewards.

We also know that Epoch is of one day.

Scenario: The attacker delegates to the delegatee just 1 second before a new epoch starts and then undelegates as soon as the epoch begins, effectively not staying delegated for even 1 day. For example, with 1 second remaining before the new epoch, the attacker delegates and then undelegates right after the epoch starts, essentially delegating for only 1-2 seconds in an entire day.
However, the attacker still receives the same reward for their stake as someone who has been staking for the entire day. The attacker can exploit this by taking out a large loan for 1-2 seconds, effectively harming the rewards of other long-term delegators.

We should note that this happens because we are setting unclaimedEnd to effectiveEpoch - 1 instead of CurrentEpoch - 1 in notifyUndelegation.

    function notifyUndelegation(
        address delegatee,
        address delegator,
        uint256 effectiveEpoch,
        uint256 totalAmount,
        uint256 remainsNumber
    ) public onlyL2StakingContract {
        ...
        unclaimed[delegator].undelegated[delegatee] = true;
        unclaimed[delegator].unclaimedEnd[delegatee] = effectiveEpoch - 1;
    }

Let’s say when the user delegated, the effectiveEpoch was 2, but when they undelegate, the effectiveEpoch is 3. This means the currentEpoch at the time of undelegating is 2, which hasn't been completed yet. However, since we are setting unclaimedEnd to currentEpoch, the user will receive rewards for the current epoch without completing it.

Impact

The attacker can harm other users rewards by taking a large loan, making the impact high.

Tool used

Manual Review

Recommendation

I recommend that we should set unclaimedEnd to currentEpoch - 1.

    function notifyUndelegation(
        address delegatee,
        address delegator,
        uint256 effectiveEpoch,
        uint256 totalAmount,
        uint256 remainsNumber
    ) public onlyL2StakingContract {
        ...
        unclaimed[delegator].undelegated[delegatee] = true;
        unclaimed[delegator].unclaimedEnd[delegatee] = effectiveEpoch - 2; //@note HERE
    }

References

I will share a video by Owen where he explains this category of bugs: https://www.youtube.com/watch?v=-9VmITcdm3c

https://github.com/sherlock-audit/2024-08-morphl2/blob/main/morph/contracts/contracts/l2/staking/Distribute.sol#L112

@sherlock-admin3 sherlock-admin3 changed the title Crazy Jetblack Pigeon - Attacker Can Harm Other Delegator by Strategic Delegating and UnDelegating 0xRajkumar - Attacker Can Harm Other Delegator by Strategic Delegating and UnDelegating Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant