Users may withdraw more tokens than they originally deposited by setting the _amount parameter to type(uint256).max. This vulnerability stems from the incorrect assumption that setting _amount to type(uint256).max means the user wants to withdraw their entire balance, without considering potential balance increases since the original deposit.
When _amount is type(uint256).max, the toWithdraw variable is set to the user's entire balance (balanceOf(_account)) instead of the original deposited amount. This leads to the entire balance being burned and transferred to the user, even if they had only deposited a smaller amount.
The reason for this vulnerability is that the code assumes setting _amount to type(uint256).max means the user wants to withdraw their entire balance. However, it does not consider that the user's balance may have increased since their original deposit due to rewards or other deposits.
StakingPool.sol# https://github.com/Cyfrin/2024-09-stakelink/blob/f5824f9ad67058b24a2c08494e51ddd7efdbb90b/contracts/core/StakingPool.sol#L142-L165
The bug is connected to the logic in the withdraw function that handles the case when _amount is type(uint256).max. Instead of limiting the withdrawal to the original deposited amount, it allows withdrawing the full balance.
Imagine a User:
User deposits a non-zero amount of tokens into the pool using the deposit function.
User calls the withdraw function with _amount set to type(uint256).max.
The withdraw function burns and transfers the user's entire balance, even if it exceeds the original deposited amount.
Users could withdraw more tokens than they originally deposited, effectively stealing tokens from the pool. It can lead to the depletion of the pool's funds and loss of assets for other users and the protocol.
Vs Code
By adding min(balanceOf(_account), _amount), the code ensures that even if type(uint256).max is passed, the withdrawal amount is capped at the minimum of the user's balance and the original deposit amount. This prevents users from withdrawing more tokens than they deposited.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.