Skip to content
DoS Audit finding Intermediate

Underflow in claimable DOSing claim Function

A rounding error in the vesting contract can cause the number of steps claimed to exceed the total number of steps, leading to an underflow when calculating claimable steps. This prevents users who purchase additional tokens from claiming them, locking their funds.

BugPull Follow with 0xrex, 0XRolko and 7 others

Originally disclosed on Code4rena

Submitted by BugPull, also found by 0xrex, 0XRolko, BugPull, Drynooo, KupiaSec, montecristo, Ryonen, SmartAuditPro, and TheFabled

https://github.com/code-423n4/2024-12-secondswap/blob/214849c3517eb26b31fe194bceae65cb0f52d2c0/contracts/SecondSwap_StepVesting.sol#L172-L181

https://github.com/code-423n4/2024-12-secondswap/blob/214849c3517eb26b31fe194bceae65cb0f52d2c0/contracts/SecondSwap_StepVesting.sol#L196-L199

A user who buys vesting tokens after fully claiming their allocation at the end of the vesting period will be unable to claim the newly acquired tokens.

in some cases due to rounding issues the - stepsClaimed > numOfSteps

In claimable function claimableSteps is calculated as follow:

174:@>       uint256 claimableSteps = currentStep - vesting.stepsClaimed;

For that the user cannot claim newly purchased amounts.

Impact

  • The claim function becomes unavailable.
  • The funds are stuck.
  • Users who purchase additional amounts are unable to claim their tokens.

Proof of Concept

Scenario

  1. Bob identifies a desirable vesting token and buys 10,000 tokens.
  2. The vesting period ends (currentTime > endTime), and Bob has claimed all funds.
  3. Due to rounding issues during creation, stepsClaimed > numOfSteps.
  4. Bob purchases additional tokens.
  5. Bob is unable to claim the newly purchased tokens due to an incorrect check in claimable.

Numerical Example

Assumptions:

  • _endTime - _startTime = 1000
  • numOfSteps = 110
  • stepDuration = (_endTime - _startTime) / numOfSteps = 9

Calculation:

  1. The vesting period ends, and Bob claims all funds using the claim function, which calls claimable:
File: SecondSwap_StepVesting.sol
172:         uint256 elapsedTime = currentTime - startTime;
173:         uint256 currentStep = elapsedTime / stepDuration;
174:@>       uint256 claimableSteps = currentStep - vesting.stepsClaimed;
175:
176:         uint256 claimableAmount;
177:
178:@>       if (vesting.stepsClaimed + claimableSteps >= numOfSteps) {
179:             //[BUG FIX] user can buy more than they are allocated
180:             claimableAmount = vesting.totalAmount - vesting.amountClaimed;
181:@>           return (claimableAmount, claimableSteps);
182:         }
  1. Using the numbers:

    • currentStep = 1000 / 9 = 111
    • claimableSteps = 111 - 100 = 11
  2. In claim, the value of claimableSteps is added to stepsClaimed:

File: SecondSwap_StepVesting.sol
193:     function claim() external {
//    CODE
198:@>       vesting.stepsClaimed += claimableSteps;
199:         vesting.amountClaimed += claimableAmount;

This results in:

  • vesting.stepsClaimed = 111
  • numOfSteps = 110

After Purchasing More Tokens:

Assuming the issue in the create function is mitigated as stated in another report:

--   if (numOfSteps - _vestings[_beneficiary].stepsClaimed != 0)
++   if (numOfSteps > _vestings[_beneficiary].stepsClaimed){

Bob successfully buys additional tokens. However, when he tries to claim them:

File: SecondSwap_StepVesting.sol
161:     function claimable(address _beneficiary) public view returns (uint256, uint256) {
//    CODE
172:         uint256 elapsedTime = currentTime - startTime;
173:         uint256 currentStep = elapsedTime / stepDuration;
174:@>       uint256 claimableSteps = currentStep - vesting.stepsClaimed;

The function always reverts due to the incorrect check.

Recommended Mitigation Steps

Apply the following correction to the claimable function:

--       uint256 claimableSteps = currentStep - vesting.stepsClaimed;
++       uint256 claimableSteps = currentStep > vesting.stepsClaimed ? 0 : currentStep - vesting.stepsClaimed;

Koolex (judge) commented:

As per the sponsor, it is a very rare case. Based on this and the input provided, this is a Medium severity. Setting this as the main submission.


The original report as published on Code4rena, by BugPull. 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 subtraction `currentStep - vesting.stepsClaimed` underflows when `stepsClaimed` exceeds `currentStep` due to rounding in step calculations.

Impact

The claim function becomes unavailable, so users who purchase additional tokens cannot claim them and their funds are stuck.

What the attacker needed

User has fully claimed vesting tokens; Rounding causes stepsClaimed > numOfSteps; User purchases additional tokens

The fix

The claimable function should be fixed to handle the case where stepsClaimed exceeds currentStep, for example by returning zero claimable steps instead of underflowing.

How it was found

  1. Bob buys 10,000 vesting tokens.
  2. The vesting period ends and Bob claims all funds.
  3. Due to rounding, stepsClaimed becomes greater than numOfSteps.
  4. Bob purchases additional tokens.
  5. Bob tries to claim the new tokens but the transaction reverts.

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.

In a vesting contract, tokens are released over time in steps. A rounding mistake can make the contract think more steps have passed than actually exist. When a user later buys more tokens, the contract tries to subtract a larger number from a smaller one, which causes an error and stops them from claiming their tokens.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by BugPull

View all

4 more disclosed reports, best first.

More from SecondSwap

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.