Attacker can inflate stake rewards as he wants
The reward calculation used the account's token balance instead of the staked amount, so an attacker could transfer stake tokens between accounts to inflate rewards. This allowed claiming rewards multiple times for the same stake.
KrisRenZo Follow with dany.armstrong90 and mstpr-brainbot
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/146
Found by
KrisRenZo, dany.armstrong90, mstpr-brainbot
Summary
FeeRewardsProcess.sol#updateAccountFeeRewards function uses balance of account as amount of stake tokens.
Since it is possible to transfer stake tokens to any accounts, attacker can flash loan other's stake tokens to inflate stake rewards.
Vulnerability Detail
FeeRewardsProcess.sol#updateAccountFeeRewards function is the following.
function updateAccountFeeRewards(address account, address stakeToken) public {
StakingAccount.Props storage stakingAccount = StakingAccount.load(account);
StakingAccount.FeeRewards storage accountFeeRewards = stakingAccount.getFeeRewards(stakeToken);
FeeRewards.MarketRewards storage feeProps = FeeRewards.loadPoolRewards(stakeToken);
if (accountFeeRewards.openRewardsPerStakeToken == feeProps.getCumulativeRewardsPerStakeToken()) {
return;
}
63: uint256 stakeTokens = IERC20(stakeToken).balanceOf(account);
if (
stakeTokens > 0 &&
feeProps.getCumulativeRewardsPerStakeToken() - accountFeeRewards.openRewardsPerStakeToken >
feeProps.getPoolRewardsPerStakeTokenDeltaLimit()
) {
accountFeeRewards.realisedRewardsTokenAmount += (
stakeToken == CommonData.getStakeUsdToken()
? CalUtils.mul(
feeProps.getCumulativeRewardsPerStakeToken() - accountFeeRewards.openRewardsPerStakeToken,
stakeTokens
)
: CalUtils.mulSmallRate(
feeProps.getCumulativeRewardsPerStakeToken() - accountFeeRewards.openRewardsPerStakeToken,
stakeTokens
)
);
}
accountFeeRewards.openRewardsPerStakeToken = feeProps.getCumulativeRewardsPerStakeToken();
stakingAccount.emitFeeRewardsUpdateEvent(stakeToken);
}
Balance of account is used as amount of stake tokens in L63.
But since the stake tokens can be transferred to any other account, attacker can inflate stake token rewards by flash loan.
Example:
- User has two account:
account1,account2. - User has staked 1000 ETH in
account1and 1000 ETH inaccount2. - After a period of time, user transfer 1000 xETH from
account2toaccount1and claim rewards foraccount1. - Now, attacker can claim rewards twice for
account1. - In the same way, attacker can claim rewards twice for
account2too.
Impact
Attacker can inflate stake rewards as he wants using this vulnerability.
Code Snippet
Tool used
Manual Review
Recommendation
Use stakingAccount.stakeTokenBalances[stakeToken].stakeAmount instead of stake token balance as follows.
function updateAccountFeeRewards(address account, address stakeToken) public {
StakingAccount.Props storage stakingAccount = StakingAccount.load(account);
StakingAccount.FeeRewards storage accountFeeRewards = stakingAccount.getFeeRewards(stakeToken);
FeeRewards.MarketRewards storage feeProps = FeeRewards.loadPoolRewards(stakeToken);
if (accountFeeRewards.openRewardsPerStakeToken == feeProps.getCumulativeRewardsPerStakeToken()) {
return;
}
-- uint256 stakeTokens = IERC20(stakeToken).balanceOf(account);
++ uint256 stakeTokens = stakingAccount.stakeTokenBalances[stakeToken].stakeAmount;
if (
stakeTokens > 0 &&
feeProps.getCumulativeRewardsPerStakeToken() - accountFeeRewards.openRewardsPerStakeToken >
feeProps.getPoolRewardsPerStakeTokenDeltaLimit()
) {
accountFeeRewards.realisedRewardsTokenAmount += (
stakeToken == CommonData.getStakeUsdToken()
? CalUtils.mul(
feeProps.getCumulativeRewardsPerStakeToken() - accountFeeRewards.openRewardsPerStakeToken,
stakeTokens
)
: CalUtils.mulSmallRate(
feeProps.getCumulativeRewardsPerStakeToken() - accountFeeRewards.openRewardsPerStakeToken,
stakeTokens
)
);
}
accountFeeRewards.openRewardsPerStakeToken = feeProps.getCumulativeRewardsPerStakeToken();
stakingAccount.emitFeeRewardsUpdateEvent(stakeToken);
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/19
sherlock-admin2
The Lead Senior Watson signed off on the fix.
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 function used the account's token balance instead of the recorded staked amount to calculate rewards.
Impact
An attacker could inflate staking rewards arbitrarily by moving tokens between accounts.
What the attacker needed
Two accounts; staked tokens; ability to transfer stake tokens
The fix
The fix was to use the recorded staked amount instead of the token balance.
How it was found
- Have two accounts with staked tokens.
- Transfer stake tokens from one account to the other.
- Claim rewards for the receiving account.
- Repeat to claim rewards multiple times for the same tokens.
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.
In a staking system, rewards are usually based on how many tokens you have locked up. Here, the code mistakenly looked at the total tokens in your wallet, not the locked amount. So someone could move tokens around between accounts to make it look like they had more staked than they really did, and earn extra rewards.
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.