User loses remaining rewards in GiantMevAndFeesPool when new deposits happen because _onDepositETH() set claimed[][] to max without transferring user remaining rewards
When a user deposits more ETH into the GiantMevAndFeesPool, the contract sets their claimed rewards to the maximum based on their new balance without first paying out any unclaimed rewards. This causes the user to permanently lose those previously earned rewards.
Originally disclosed on Code4rena
Submitted by unforgiven
https://github.com/code-423n4/2022-11-stakehouse/blob/4b6828e9c807f2f7c569e6d721ca1289f7cf7112/contracts/liquid-staking/GiantMevAndFeesPool.sol#L195-L204 https://github.com/code-423n4/2022-11-stakehouse/blob/4b6828e9c807f2f7c569e6d721ca1289f7cf7112/contracts/liquid-staking/GiantPoolBase.sol#L33-L48
When depositETH() is called in giant pool it calls _onDepositETH() which calls _setClaimedToMax() to make sure new ETH stakers are not entitled to ETH earned by but this can cause users to lose their remaining rewards when they deposits. code should first transfer user remaining rewards when deposit happens.
Proof of Concept
This is depositETH() code in GiantPoolBase:
/// @notice Add ETH to the ETH LP pool at a rate of 1:1. LPs can always pull out at same rate.
function depositETH(uint256 _amount) public payable {
require(msg.value >= MIN_STAKING_AMOUNT, "Minimum not supplied");
require(msg.value == _amount, "Value equal to amount");
// The ETH capital has not yet been deployed to a liquid staking network
idleETH += msg.value;
// Mint giant LP at ratio of 1:1
lpTokenETH.mint(msg.sender, msg.value);
// If anything extra needs to be done
_onDepositETH();
emit ETHDeposited(msg.sender, msg.value);
}
As you can see it increase user lpTokenETH balance and then calls _onDepositETH(). This is _onDepositETH() and _setClaimedToMax() code in GiantMevAndFeesPool contract:
/// @dev On depositing on ETH set claimed to max claim so the new depositor cannot claim ETH that they have not accrued
function _onDepositETH() internal override {
_setClaimedToMax(msg.sender);
}
/// @dev Internal re-usable method for setting claimed to max for msg.sender
function _setClaimedToMax(address _user) internal {
// New ETH stakers are not entitled to ETH earned by
claimed[_user][address(lpTokenETH)] = (accumulatedETHPerLPShare * lpTokenETH.balanceOf(_user)) / PRECISION;
}
As you can see the code set claimed[msg.sender][address(lpTokenETH] to maximum value so the user wouldn’t be entitled to previous rewards but if user had some remaining rewards in contract he would lose those rewards can’t withdraw them. these are the steps:
user1deposit10ETH to giant pool andaccumulatedETHPerLPSharevalue is2andclaimed[user1][lpTokenETH]would be10 * 2 = 20.- some time passes and
accumulatedETHPerLPShareset to4anduser1has10 * 4 - 20 = 20unclaimed ETH rewards (the formula in the code:balance * rewardPerShare - claimed). userdeposit5ETH to giant pool andaccumulatedETHPerLPShareis4so the code would call_onDepositETH()which calls_setClaimedToMaxwhich setsclaimed[user1][lpTokenETH]to15 * 4 = 60.user1new remaining ETH reward would be15 * 4 - 60 = 0. anduser1won’t receive his rewards because when he deposits contract don’t transfer remaining rewards and set claim to max so user loses his funds.
Tools Used
VIM
Recommended Mitigation Steps
When deposit happens, contract should first send remaining rewards, then increase the user’s balance and then set the user claim to max.
vince0656 (Stakehouse) confirmed
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 deposit handler updates the user's claimed rewards to the maximum based on their new balance without first transferring any unclaimed rewards.
Impact
A user permanently loses any unclaimed rewards when they make a new deposit.
What the attacker needed
User has unclaimed rewards; user makes a new deposit
The fix
The contract should transfer the user's remaining rewards before increasing their balance and then setting their claim to max.
How it was found
- User1 deposits 10 ETH when accumulatedETHPerLPShare is 2, setting claimed to 20.
- Later accumulatedETHPerLPShare becomes 4, giving user1 20 unclaimed rewards.
- User1 deposits 5 more ETH, triggering _onDepositETH which sets claimed to 60.
- User1's new unclaimed reward becomes 0, losing the previous 20.
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 user earns rewards over time in a pool. If they add more money to the pool, the contract recalculates how much they have already been paid, but it forgets to actually send them the rewards they had earned so far. Those rewards are then lost.
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.