Skip to content
Business logic Audit finding Intermediate

maxSellPercent can be buypassed by selling previously bought vestings at a later time

The vesting contract's sell limit calculation incorrectly includes all previously bought tokens, allowing a user to sell more unvested tokens than the maxSellPercent allows. An attacker could bypass the intended limit and sell a larger portion of locked tokens.

BenRai Follow with ABAIKUNANBAEV, Agontuk and 20 others

Originally disclosed on Code4rena

Submitted by BenRai, also found by ABAIKUNANBAEV, Agontuk, AshishLach, attentioniayn, chupinexx, EaglesSecurity, escrow, franfran20, gesha17, itsabinashb, jsonDoge, NexusAudits, nikhil840096, nitinaimshigh, parishill24, Rhaydden, Samueltroydomi, Saurabh_Singh, typicalHuman, typicalHuman, wickie0x, and zzebra83

https://github.com/code-423n4/2024-12-secondswap/blob/b9497bcf5100046a169276cb6b351ebc0eddc2cc/contracts/SecondSwap_VestingManager.sol#L127-L134

Because the maxSellPercent can be bypassed by selling previously bought vestings at a later time, the core functionality to limit the amount of unvested tokens which can be sold is broken.

Proof of Concept

The SecondSwap_VestingManager contract manages the vesting and selling of tokens through the listVesting() function. This function checks if the vesting plan is sellable and verifies the user’s available tokens before allowing a sale.

However, the logic for calculating the sellLimit is flawed.

The relevant code snippet is as follows:

uint256 sellLimit = userAllocation.bought;
uint256 currentAlloc = SecondSwap_Vesting(plan).total(seller);
if (currentAlloc + userAllocation.sold > userAllocation.bought) {
sellLimit +=
     ((currentAlloc + userAllocation.sold - userAllocation.bought) vestingSettings[plan].maxSellPercent) /
     BASE;
}

The sellLimit is calculated for the case a ´maxSellPercent´ is set for the vesting. The ´maxSellPercent´ should limit the amount of unvested tokens that can be sold. E.g. if ´maxSellPercent´ is set to 20%, only 20% of the unvested tokens should be sellable. So if we have vesting with a total of 1000 tokens and 10 steps and the maxSellPercent is set to 20%, in the beginning of the vesting when all tokens are still locked only 20% of the max tokens (200 tokens) should be sellable. When 5 steps are unlocked, only 20% of the remaining locked tokens (500 tokens * 20% = 100 tokens) should be sellable.

The issue arises from the fact that all previously bought tokens can be sold at a later point in time:

uint256 sellLimit = userAllocation.bought;

This can result in more tokens than intended to be sellable.

Scenario Illustration

Vesting has 1000 tokens and 10 steps, maxSellPercent is set to 20%:

  1. Alice has an allocation of 100 tokens and she buys all listed (1000 – 100) * 20% = 1900 * 20% = 180 listed tokens before the first step/unlock => her bought value is now 180, her total allocation is 280 tokens and her release rate is 28 tokens per step.
  2. Time passes and the first 4 steps are unlocked allowing Alice to claim 4*28 = 112 tokens leaving her with a remaining allocation of 280 – 112 = 168.
  3. After 4 unlocked steps the max amount of tokens sellable should be (1000 - 400) * 20% = 600 * 20% = 120 tokens. But since Alice has a bought value of 180 tokens she can sell all her remaining 168 unlocked tokens breaking the 20% ´maxSellPercent´ limit.

Recommended Mitigation Steps

To prevent more locked tokens to be sellable than specified in ´maxSellPercent´ consider adding a stepsBought to the Allocation struct to be able to adjust the bought value according to the steps already claimed by the user:

    struct Allocation {
        uint256 bought;
+       uint256 stepsBought;
        uint256 sold;
    }

The stepsBought value would be adjusted each time a user buys or sells a vesting and would be set to the current stepsClaimed value of the buyer. In addition, for sells, the bought amount would also need to be reduced by the already claimed amount.

Buy a vesting:

  • Buyer buys 100 tokens and the stepsBought value is set to his current stepsClaimed value. This way we know for which steps the bought value will be claimable. e.g stepsBought is 5 => bought value was allocated to step 6 to 10

Sell a vesting:

  • Buyer claims 2 more periods and wants to sell tokens
  • The bought part of the sellLimit is determined by calculating the bought amount for each step and reducing the original boughtamount by the steps already claimed:
uint256 boughtAmountPerStep = userAllocation.bought / (stepsBought - SecondSwap_Vesting(plan).numOfSteps());`
uint256 claimedStepsSinceBuy = SecondSwap_Vesting(plan)._vestings(seller).stepsClaimed – stepsBought;
uint256 sellLimit = userAllocation.bought – (boughtAmountPerStep * claimedStepsSinceBuy)

For this to work:

  1. The bought amount must be reduced to the calculated sellLimit
  2. We need to sell bought allocations first before selling own allocations. Therefore the bought amount must be reduced by the amount which should be sold. Only when the bought amount reaches 0, the sold amount should be increased.

The result would be that the sold amount represents only the amount a user sold of his initial allocation. In addition the Allocation struct also needs a stepsSold variable which can be used to adjust/reduce the sold amount according to the claimed steps of the seller/buyer.

TechticalRAM (SecondSwap) confirmed


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

The sell limit calculation uses the total bought amount without adjusting for steps already claimed, so previously bought tokens remain fully sellable later.

Impact

A user could sell more unvested tokens than the maxSellPercent allows, breaking the intended limit on locked token sales.

What the attacker needed

A vesting plan with maxSellPercent set; user has bought tokens from others; some steps have unlocked

The fix

The Allocation struct should track steps bought and adjust the bought amount based on claimed steps, and bought allocations should be sold first.

How it was found

  1. Alice buys all listed tokens before the first unlock, increasing her bought value.
  2. Time passes and some steps unlock, allowing Alice to claim tokens.
  3. Alice can sell all her remaining unlocked tokens, exceeding the maxSellPercent limit.

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.

A vesting contract gradually releases tokens over time and may limit how many locked tokens you can sell. The bug means that if you bought tokens earlier, those bought tokens are always counted as sellable, even after some have unlocked, so you can sell more than the allowed percentage of locked tokens.

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 BenRai

View all

13 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.

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.