Skip to content
Business logic Audit finding Advanced

SecondSwap_Marketplace vesting listing order affects how much the vesting buyers can claim at a given step

When a vesting is listed on the marketplace, the vesting manager contract inherits the seller's already-claimed step count if it has no prior allocation for that plan. A buyer with no prior allocation who purchases a later listing can therefore inherit that claimed-step count and be unable to claim at the current step, so the order in which listings are created changes what buyers can claim.

0xloscar01 Follow with 0xaudron, 0xc0ffEE and 28 others

Originally disclosed on Code4rena

Submitted by 0xloscar01, also found by 0xaudron, 0xc0ffEE, 0xc0ffEE, 0xEkko, 0xgremlincat, 0xNirix, 0xrex, 4rdiii, Agontuk, anchabadze, BenRai, BenRai, curly, foufrix, jkk812812, jkk812812, jsonDoge, jsonDoge, KupiaSec, KupiaSec, Kyosi, macart224, NexusAudits, nslavchev, Sabit, seerether, shaflow2, sl1, web3km, and y0ng0p3

When a vesting is listed, the vesting is transferred to the SecondSwap_VestingManager contract. With no previous listings, the contract “inherits” the stepsClaimed from the listed vesting:

https://github.com/code-423n4/2024-12-secondswap/blob/main/contracts/SecondSwap_StepVesting.sol#L288-L290

@>        if (_vestings[_beneficiary].totalAmount == 0) {
            _vestings[_beneficiary] = Vesting({
@>            stepsClaimed: _stepsClaimed,
            ...

Suppose the stepsClaimed amount is positive. In that case, further listing allocations will be mixed with the previous one, meaning the “inherited” stepsClaimed amount will be present in the listings transferred from the SecondSwap_VestingManager contract to users with no allocation that buy listings through SecondSwap_Marketplace::spotPurchase.

This condition creates two scenarios that affect how much the user can claim:

Assuming for both scenarios that there are no listings yet for a given vesting plan.

Scenario 1:

  1. First listing has no claimedSteps
  2. Second listing has claimedSteps

Since the first listing has no claimedSteps, users with no previous vestings allocation can buy any of the listings and their listing won’t have claimed steps, allowing them to claim immediately after their purchase.

Scenario 2:

  1. First listing has claimedSteps
  2. Second listing has no claimedSteps

Due to the first listing having a positive claimedSteps amount, users with no previous vesting allocations will have their vestings inherit the claimedSteps, meaning they won’t be able to claim if they are on the current step corresponding to claimedSteps.

Proof of Concept

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {Test, console} from "../lib/forge-std/src/Test.sol";
import {Vm} from "../lib/forge-std/src/Test.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {SecondSwap_Marketplace} from "../contracts/SecondSwap_Marketplace.sol";
import {SecondSwap_MarketplaceSetting} from "../contracts/SecondSwap_MarketplaceSetting.sol";
import {SecondSwap_VestingManager} from "../contracts/SecondSwap_VestingManager.sol";
import {SecondSwap_VestingDeployer} from "../contracts/SecondSwap_VestingDeployer.sol";
import {SecondSwap_WhitelistDeployer} from "../contracts/SecondSwap_WhitelistDeployer.sol";
import {SecondSwap_StepVesting} from "../contracts/SecondSwap_StepVesting.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract SecondSwap_MarketplaceTest is Test {
    ERC1967Proxy marketplaceProxy;
    ERC1967Proxy vestingManagerProxy;
    ERC1967Proxy vestingDeployerProxy;
    SecondSwap_Marketplace marketplaceImplementation;
    SecondSwap_VestingManager vestingManagerImplementation;
    SecondSwap_VestingDeployer vestingDeployerImplementation;
    SecondSwap_VestingManager vestingManager;
    SecondSwap_Marketplace marketplace;
    SecondSwap_VestingDeployer vestingDeployer;
    SecondSwap_MarketplaceSetting marketplaceSetting;
    SecondSwap_WhitelistDeployer whitelistDeployer;
    MockERC20 marketplaceToken;
    MockERC20 vestingToken;
    MockUSDT usdt;
    address vestingSeller1 = makeAddr("vestingSeller1");
    address vestingSeller2 = makeAddr("vestingSeller2");
    address vestingBuyer = makeAddr("vestingBuyer");

    function setUp() public {
        marketplaceImplementation = new SecondSwap_Marketplace();
        vestingManagerImplementation = new SecondSwap_VestingManager();
        vestingDeployerImplementation = new SecondSwap_VestingDeployer();
        whitelistDeployer = new SecondSwap_WhitelistDeployer();

        marketplaceToken = new MockERC20("Marketplace Token", "MTK");
        vestingToken = new MockERC20("Vesting Token", "VTK");
        usdt = new MockUSDT();

        vestingManagerProxy = new ERC1967Proxy(
            address(vestingManagerImplementation),
            abi.encodeWithSignature("initialize(address)", address(this))
        );

        vestingManager = SecondSwap_VestingManager(
            address(vestingManagerProxy)
        );

        vestingDeployerProxy = new ERC1967Proxy(
            address(vestingDeployerImplementation),
            abi.encodeWithSignature(
                "initialize(address,address)",
                address(this),
                address(vestingManager)
            )
        );

        vestingDeployer = SecondSwap_VestingDeployer(
            address(vestingDeployerProxy)
        );

        marketplaceSetting = new SecondSwap_MarketplaceSetting({
            _feeCollector: address(this),
            _s2Admin: address(this),
            _whitelistDeployer: address(whitelistDeployer),
            _vestingManager: address(vestingManager),
            _usdt: address(usdt)
        });
        marketplaceProxy = new ERC1967Proxy(
            address(marketplaceImplementation),
            abi.encodeWithSignature(
                "initialize(address,address)",
                address(marketplaceToken),
                address(marketplaceSetting)
            )
        );
        marketplace = SecondSwap_Marketplace(address(marketplaceProxy));

        vestingDeployer.setTokenOwner(address(vestingToken), address(this));
        vestingManager.setVestingDeployer(address(vestingDeployer));
        vestingManager.setMarketplace(address(marketplace));
        vestingToken.mint(address(this), 10e18 * 2);
        marketplaceToken.mint(vestingBuyer, 10e18);

    }

    // 1. First listing has no claimedSteps
    // 2. Second listing has claimedSteps
    function testCase1() public {
        vm.recordLogs();

        vestingDeployer.deployVesting({
            tokenAddress: address(vestingToken),
            startTime: block.timestamp,
            endTime: block.timestamp + 200 days,
            steps: 200,
            vestingId: "1"
        });

        Vm.Log[] memory entries = vm.getRecordedLogs();
        assertEq(entries.length, 3);

        // Check the event signature
        assertEq(
            entries[2].topics[0],
            keccak256("VestingDeployed(address,address,string)")
        );
        assertEq(entries[2].emitter, address(vestingDeployer));

        (address deployedVesting, ) = abi.decode(
            entries[2].data,
            (address, string)
        );

        SecondSwap_StepVesting vesting = SecondSwap_StepVesting(
            deployedVesting
        );

        vestingToken.approve(address(vesting), 10e18);

        vesting.createVesting({
            _beneficiary: vestingSeller1,
            _totalAmount: 10e18
        });

        // After 10 days, vestingSeller1 lists 10% of the total amount
        vm.warp(block.timestamp + 10 days);
        uint256 amount = (10e18 * 1000) / 10000; // 10 percent of the total amount

        vm.startPrank(vestingSeller1);
        marketplace.listVesting({
            _vestingPlan: address(vesting),
            _amount: amount,
            _price: 1e18,
            _discountPct: 0,
            _listingType: SecondSwap_Marketplace.ListingType.SINGLE,
            _discountType: SecondSwap_Marketplace.DiscountType.NO,
            _maxWhitelist: 0,
            _currency: address(marketplaceToken),
            _minPurchaseAmt: amount,
            _isPrivate: false
        });

        // vestingSeller1 claims
        vesting.claim();

        //vestingSeller1 lists another 10% of the total amount
        marketplace.listVesting({
            _vestingPlan: address(vesting),
            _amount: amount,
            _price: 1e18,
            _discountPct: 0,
            _listingType: SecondSwap_Marketplace.ListingType.SINGLE,
            _discountType: SecondSwap_Marketplace.DiscountType.NO,
            _maxWhitelist: 0,
            _currency: address(marketplaceToken),
            _minPurchaseAmt: amount,
            _isPrivate: false
        });
        vm.stopPrank();

        // At this point, the SecondSwap_VestingManager contract has 0 stepsClaimed

// vestingBuyer buys the second listed vesting
        vm.startPrank(vestingBuyer);
        marketplaceToken.approve(address(marketplace), amount + (amount * marketplaceSetting.buyerFee()));
        marketplace.spotPurchase({
            _vestingPlan: address(vesting),
            _listingId: 1,
            _amount: amount,
            _referral: address(0)
        });
        vm.stopPrank();

        (uint256 vestingBuyerClaimableAmount, ) = vesting.claimable(vestingBuyer);

        console.log("Buyer claimable amount: ", vestingBuyerClaimableAmount);
    }

    // 1. First listing has claimedSteps
    // 2. Second listing has no claimedSteps
    function testCase2() public {
        vm.recordLogs();

        vestingDeployer.deployVesting({
            tokenAddress: address(vestingToken),
            startTime: block.timestamp,
            endTime: block.timestamp + 200 days,
            steps: 200,
            vestingId: "1"
        });

        Vm.Log[] memory entries = vm.getRecordedLogs();
        assertEq(entries.length, 3);

        // Check the event signature
        assertEq(
            entries[2].topics[0],
            keccak256("VestingDeployed(address,address,string)")
        );
        assertEq(entries[2].emitter, address(vestingDeployer));

        (address deployedVesting, ) = abi.decode(
            entries[2].data,
            (address, string)
        );

        SecondSwap_StepVesting vesting = SecondSwap_StepVesting(
            deployedVesting
        );

        vestingToken.approve(address(vesting), 10e18 * 2);

        vesting.createVesting({
            _beneficiary: vestingSeller1,
            _totalAmount: 10e18
        });

        vesting.createVesting({
            _beneficiary: vestingSeller2,
            _totalAmount: 10e18
        });

        // After 10 days, vestingSeller1 claims and then lists 10% of the total amount
        vm.warp(block.timestamp + 10 days);
        uint256 amount = (10e18 * 1000) / 10000; // 10 percent of the total amount

        vm.startPrank(vestingSeller1);
        // vestingSeller1 claims
        vesting.claim();

        marketplace.listVesting({
            _vestingPlan: address(vesting),
            _amount: amount,
            _price: 1e18,
            _discountPct: 0,
            _listingType: SecondSwap_Marketplace.ListingType.SINGLE,
            _discountType: SecondSwap_Marketplace.DiscountType.NO,
            _maxWhitelist: 0,
            _currency: address(marketplaceToken),
            _minPurchaseAmt: amount,
            _isPrivate: false
        });
        vm.stopPrank();

        //vestingSeller2 lists 10% of the total amount. Has not claimed yet
        vm.prank(vestingSeller2);
        marketplace.listVesting({
            _vestingPlan: address(vesting),
            _amount: amount,
            _price: 1e18,
            _discountPct: 0,
            _listingType: SecondSwap_Marketplace.ListingType.SINGLE,
            _discountType: SecondSwap_Marketplace.DiscountType.NO,
            _maxWhitelist: 0,
            _currency: address(marketplaceToken),
            _minPurchaseAmt: amount,
            _isPrivate: false
        });

        // At this point, the SecondSwap_VestingManager contract has stepsClaimed

        // vestingBuyer buys the second listed vesting
        vm.startPrank(vestingBuyer);
        marketplaceToken.approve(address(marketplace), amount + (amount * marketplaceSetting.buyerFee()));
        marketplace.spotPurchase({
            _vestingPlan: address(vesting),
            _listingId: 1,
            _amount: amount,
            _referral: address(0)
        });
        vm.stopPrank();

        (uint256 vestingBuyerClaimableAmount, ) = vesting.claimable(vestingBuyer);

        console.log("Buyer claimable amount: ", vestingBuyerClaimableAmount);
    }
}

contract MockERC20 is ERC20 {
    constructor(string memory name, string memory symbol) ERC20(name, symbol) {}

    function mint(address account, uint amount) external {
        _mint(account, amount);
    }
}

contract MockUSDT is ERC20 {
    constructor() ERC20("Tether USD", "USDT") {}

    function mint(address account, uint amount) external {
        _mint(account, amount);
    }

    function decimals() public pure override returns (uint8) {
        return 6;
    }
}

Steps to reproduce:

  1. Run npm i --save-dev @nomicfoundation/hardhat-foundry in the terminal to install the hardhat-foundry plugin.
  2. Add require("@nomicfoundation/hardhat-foundry"); to the top of the hardhat.config.js file.
  3. Run npx hardhat init-foundry in the terminal.
  4. Create a file “StepVestingTest.t.sol” in the “test/” directory and paste the provided PoC.
  5. Run forge test --mt testCase1 in the terminal.
  6. Run forge test --mt testCase2 in the terminal.

Recommended mitigation steps

Add a virtual total amount to the manager contract on each vesting plan deployed.

TechticalRAM (SecondSwap) confirmed


The original report as published on Code4rena, by 0xloscar01. 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 vesting manager contract copies the seller's stepsClaimed into its own vesting record when it has no existing allocation, so state from one listing leaks into later listings for the same plan.

Impact

Depending on listing order, a buyer with no prior allocation may be unable to claim at the current step because the inherited claimed-step count is too high.

What the attacker needed

A deployed vesting plan; two sellers listing the same plan; a buyer with no prior allocation

The fix

The report recommends adding a virtual total amount to the manager contract for each vesting plan deployed, and the project confirmed the finding.

How it was found

  1. Deploy a vesting plan and create a vesting for a seller.
  2. Advance time and have the seller claim, then list part of the vesting.
  3. Have a second seller list part of the same plan without having claimed.
  4. Have a buyer with no prior allocation purchase the second listing.
  5. Check the buyer's claimable amount and observe the effect of the inherited claimed steps.

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 plan releases tokens in steps over time, and a counter records how many steps a person has already claimed. When someone sells part of their vesting on the marketplace, the contract that holds the sold portion copies the seller's counter if it has no record of its own. A buyer who later receives that portion can end up with a counter that is too high, so they cannot claim tokens they should be entitled to at that point.

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 0xloscar01

View all

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