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

WithdrawHook#userWithdrawLimitPerPeriod Limit max withdraw logic error #160

Closed
code423n4 opened this issue Dec 12, 2022 · 4 comments
Closed
Labels
3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working duplicate-310 satisfactory satisfies C4 submission criteria; eligible for awards upgraded by judge Original issue severity upgraded from QA/Gas by judge

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/prepo-io/prepo-monorepo/blob/3541bc704ab185a969f300e96e2f744a572a3640/apps/smart-contracts/core/contracts/WithdrawHook.sol#L66-L72

Vulnerability details

Impact

The maximum withdrawal value per Period for the user may be much smaller than userWithdrawLimitPerPeriod

Proof of Concept

WithdrawHook#hook() will limit the user's maximum withdrawal per Period. The logic is: when a new Period , the new Period starts counting from 0.
But there is a logic error that causes this: only the first user of the Period is recalculated, the other users are not recalculated, and the new Period follows the userToAmountWithdrawnThisPeriod[_sender] of the previous cycle

lastUserPeriodReset needs to be changed to mapping(address => uint256), independent for each user

  function hook(
    address _sender,
    uint256 _amountBeforeFee,
    uint256 _amountAfterFee
  ) external override onlyCollateral {
..

    if (lastUserPeriodReset + userPeriodLength < block.timestamp) {
      lastUserPeriodReset = block.timestamp; //***@audit All users share a common “lastUserPeriodReset” ***//
      userToAmountWithdrawnThisPeriod[_sender] = _amountBeforeFee;  //**@audit only first user of period,  counting from 0***//
    } else {
      require(userToAmountWithdrawnThisPeriod[_sender] + _amountBeforeFee <= userWithdrawLimitPerPeriod, "user withdraw limit exceeded");
      userToAmountWithdrawnThisPeriod[_sender] += _amountBeforeFee;
    }

  }

Tools Used

Recommended Mitigation Steps

lastUserPeriodReset needs to be changed to mapping(address => uint256), independent for each user

contract WithdrawHook is IWithdrawHook, TokenSenderCaller, SafeAccessControlEnumerable {
..
- uint256 private lastUserPeriodReset;
+ mapping(address => uint256) private lastUserPeriodReset;

...

  function hook(
    address _sender,
    uint256 _amountBeforeFee,
    uint256 _amountAfterFee
  ) external override onlyCollateral {
...

-   if (lastUserPeriodReset + userPeriodLength < block.timestamp) {
-     lastUserPeriodReset = block.timestamp;
-     userToAmountWithdrawnThisPeriod[_sender] = _amountBeforeFee;
-   } else {
-     require(userToAmountWithdrawnThisPeriod[_sender] + _amountBeforeFee <= userWithdrawLimitPerPeriod, "user withdraw limit exceeded");
-     userToAmountWithdrawnThisPeriod[_sender] += _amountBeforeFee;
-   }

+   if (lastUserPeriodReset[_sender] + userPeriodLength < block.timestamp) {
+     lastUserPeriodReset[_sender] = block.timestamp;
+     userToAmountWithdrawnThisPeriod[_sender] = _amountBeforeFee;
+   } else {
+     userToAmountWithdrawnThisPeriod[_sender] += _amountBeforeFee;
+   }
+   require(userToAmountWithdrawnThisPeriod[_sender] <= userWithdrawLimitPerPeriod, "user withdraw limit exceeded"); 

...

@code423n4 code423n4 added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working labels Dec 12, 2022
code423n4 added a commit that referenced this issue Dec 12, 2022
@hansfriese
Copy link

duplicate of #310

@c4-judge
Copy link
Contributor

Picodes marked the issue as duplicate of #310

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Jan 1, 2023
@c4-judge
Copy link
Contributor

c4-judge commented Jan 1, 2023

Picodes marked the issue as satisfactory

@c4-judge
Copy link
Contributor

c4-judge commented Jan 9, 2023

Picodes changed the severity to 3 (High Risk)

@c4-judge c4-judge added 3 (High Risk) Assets can be stolen/lost/compromised directly upgraded by judge Original issue severity upgraded from QA/Gas by judge and removed 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value labels Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working duplicate-310 satisfactory satisfies C4 submission criteria; eligible for awards upgraded by judge Original issue severity upgraded from QA/Gas by judge
Projects
None yet
Development

No branches or pull requests

3 participants