DeFiFoundry
20,000 USDC
View results
Submission Details
Severity: medium
Invalid

No increase of totalStaked and totalVestedStaked in stake()::FjordStaking.sol and stakeVested()::FjordStaking.sol

Summary

If there is a decrease of totalStaked and totalVestedStaked when unstaking or unstakingVested, it means it should be an increase of totalStaked and totalVestedStaked when staking or stakingVested.
But there is not.

Vulnerability Details

In unstake()::FjordStaking.sol, there is a decrease of totalStakedwhich represents the total amount that is staked.

/// @notice Total staked
uint256 public totalStaked;
...
dr.staked -= _amount;
if (currentEpoch != _epoch) {
totalStaked -= _amount; // <===== decrease totalStaked when unstaking
userData[msg.sender].totalStaked -= _amount;
} else {
// unstake immediately
newStaked -= _amount;
}
...

https://github.com/Cyfrin/2024-08-fjord/blob/main/src/FjordStaking.sol#L471

Same thing for totalStaked and totalVestedStaked in unstakeVested()::FjordStaking.sol:

...
dr.vestedStaked -= amount;
if (currentEpoch != data.epoch) {
totalStaked -= amount; // <===== decrease totalStaked when unstaking
totalVestedStaked -= amount; // <===== decrease totalVestedStaked when unstaking
userData[streamOwner].totalStaked -= amount;
} else {
// unstake immediately
newStaked -= amount;
newVestedStaked -= amount;
}
...

https://github.com/Cyfrin/2024-08-fjord/blob/main/src/FjordStaking.sol#L531

=> But when we look at the code of stake()::FjordStaking.sol and stakeVested()::FjordStaking.sol, there isn't any increase of totalStaked nor totalVestedStaked.

https://github.com/Cyfrin/2024-08-fjord/blob/main/src/FjordStaking.sol#L368-L439

Impact

Wrong calculation of totalStaked and totalVestedStaked could break the protocol.

Tools Used

Github, VisualCode, Foundry.

Recommendations

Add an increase of totalStaked and totalVestedStaked by the _amount when stake() or stakeVested() to keep a good track of the total amount staked.

Updates

Lead Judging Commences

inallhonesty Lead Judge
9 months ago
inallhonesty Lead Judge 9 months ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.