Skip to content
Business logic Audit finding Intermediate

`cancelVesting` will potentially not give users unclaimed, vested funds, even if giveUnclaimed = true

In the cancelVesting function, the code sets the locked amount to zero on a memory copy of the user's vesting record rather than the stored record, so the later claimable calculation still treats the funds as locked. As a result, a user whose funds were all staked may receive nothing back even when the owner asks for unclaimed vested funds to be paid out.

s1ce Follow with zraxx and zzykxx

Originally disclosed on Sherlock

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

Found by

s1ce, zraxx, zzykxx

Summary

The purpose of cancelVesting is to cancel a vesting grant and potentially give users unclaimed but vested funds in the event that giveUnclaimed = true. However, due to a bug, in the event that the user had staked / locked funds, they will potentially not received the unclaimed / vested funds even if giveUnclaimed = true.

Vulnerability Detail

Here's the cancelVesting function in TrufVesting:

Solidity
function cancelVesting(uint256 categoryId, uint256 vestingId, address user, bool giveUnclaimed)
        external
        onlyOwner
{
        UserVesting memory userVesting = userVestings[categoryId][vestingId][user];

        if (userVesting.amount == 0) {
            revert UserVestingDoesNotExists(categoryId, vestingId, user);
        }

        if (userVesting.startTime + vestingInfos[categoryId][vestingId].period <= block.timestamp) {
            revert AlreadyVested(categoryId, vestingId, user);
        }

        uint256 lockupId = lockupIds[categoryId][vestingId][user];

        if (lockupId != 0) {
            veTRUF.unstakeVesting(user, lockupId - 1, true);
            delete lockupIds[categoryId][vestingId][user];
            userVesting.locked = 0;
        }

        VestingCategory storage category = categories[categoryId];

        uint256 claimableAmount = claimable(categoryId, vestingId, user);
        if (giveUnclaimed && claimableAmount != 0) {
            trufToken.safeTransfer(user, claimableAmount);

            userVesting.claimed += claimableAmount;
            category.totalClaimed += claimableAmount;
            emit Claimed(categoryId, vestingId, user, claimableAmount);
        }

        uint256 unvested = userVesting.amount - userVesting.claimed;

        delete userVestings[categoryId][vestingId][user];

        category.allocated -= unvested;

        emit CancelVesting(categoryId, vestingId, user, giveUnclaimed);
}

First, consider the following code:

Solidity
uint256 lockupId = lockupIds[categoryId][vestingId][user];

if (lockupId != 0) {
            veTRUF.unstakeVesting(user, lockupId - 1, true);
            delete lockupIds[categoryId][vestingId][user];
            userVesting.locked = 0;
}

First the locked / staked funds will essentially be un-staked. The following line of code: userVesting.locked = 0; exists because there is a call to uint256 claimableAmount = claimable(categoryId, vestingId, user); afterwards, and in the event that there were locked funds that were unstaked, these funds should now potentially be claimable if they are vested (but if locked is not set to 0, then the vested funds will potentially not be deemed claimable by the claimable function).

However, because userVesting is memory rather than storage, this doesn't end up happening (so userVesting.locked = 0; is actually a bug). This means that if a user is currently staking all their funds (so all their funds are locked), and cancelVesting is called, then they will not receive any funds back even if giveUnclaimed = true. This is because the claimable function (which will access the unaltered userVestings[categoryId][vestingId][user]) will still think that all the funds are currently locked, even though they are not as they have been forcibly unstaked.

Impact

When cancelVesting is called, a user may not receive their unclaimed, vested funds.

Code Snippet

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

Tool used

Manual Review

Recommendation

Change userVesting.locked = 0; to userVestings[categoryId][vestingId][user].locked = 0;

Discussion

ryuheimat

Fixed. Updated userVesting type to storage type to fix https://github.com/sherlock-audit/2023-12-truflation-judging/issues/192 Considered initial release period and cliff for vesting end validation to fix https://github.com/sherlock-audit/2023-12-truflation-judging/issues/11

Extra: Added admin permission, and allow several admins to be able to set vesting info and user vestings. https://github.com/truflation/truflation-contracts/pull/4

mstpr

Fix and new admin functionality LGTM

ryuheimat

Fix and new admin functionality LGTM

Thank you, merging PR.

The original report as published on Sherlock, by s1ce. 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 user's vesting record is loaded into a memory variable, so the assignment that clears the locked amount never updates the stored state read by the claimable calculation.

Impact

A user may not receive their unclaimed, vested funds when the grant is cancelled, even if giveUnclaimed is true.

What the attacker needed

Owner calls cancelVesting; user's funds are fully staked

The fix

The project changed the userVesting variable to a storage reference so the locked amount is cleared in the stored record; the fix was confirmed and merged.

How it was found

  1. The owner calls cancelVesting for a user whose funds are all staked, with giveUnclaimed set to true.
  2. The contract unstakes the user's locked funds and clears the lockup identifier.
  3. The contract sets the locked amount to zero only on the in-memory copy of the record.
  4. The claimable calculation reads the unchanged stored record and treats all funds as still locked.
  5. No unclaimed vested funds are transferred to the user.

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 a user over time. When the owner cancels a grant, the contract first unstakes the user's locked tokens and then tries to pay out whatever has already vested. The bug is that the contract updates a temporary copy of the user's record instead of the saved one, so the payout calculation still believes the tokens are locked and pays nothing.

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