Possibly reentrancy attacks in _distributeETHRewardsToUserForToken function
The reward-distribution function sends ETH to the recipient before updating the claimed amount, so a contract recipient can re-enter several pool and vault functions and receive rewards more than once. The report lists five entry points where this could let an attacker steal funds or take advantage of other users.
rotcivegaf Follow with datapunk, 0x4non and 1 other
Originally disclosed on Code4rena
Submitted by rotcivegaf, also found by datapunk, 0x4non, and clems4ever
https://github.com/code-423n4/2022-11-stakehouse/blob/4b6828e9c807f2f7c569e6d721ca1289f7cf7112/contracts/liquid-staking/SyndicateRewardsProcessor.sol#L51-L73 https://github.com/code-423n4/2022-11-stakehouse/blob/4b6828e9c807f2f7c569e6d721ca1289f7cf7112/contracts/liquid-staking/GiantMevAndFeesPool.sol#L146-L167 https://github.com/code-423n4/2022-11-stakehouse/blob/4b6828e9c807f2f7c569e6d721ca1289f7cf7112/contracts/liquid-staking/GiantPoolBase.sol#L66-L90 https://github.com/code-423n4/2022-11-stakehouse/blob/4b6828e9c807f2f7c569e6d721ca1289f7cf7112/contracts/liquid-staking/StakingFundsVault.sol#L66-L104 https://github.com/code-423n4/2022-11-stakehouse/blob/4b6828e9c807f2f7c569e6d721ca1289f7cf7112/contracts/liquid-staking/StakingFundsVault.sol#L110-L143 https://github.com/code-423n4/2022-11-stakehouse/blob/4b6828e9c807f2f7c569e6d721ca1289f7cf7112/contracts/liquid-staking/StakingFundsVault.sol#L314-L340
The root of the problem is in the _distributeETHRewardsToUserForToken which makes a call to distribute the ether rewards. With this call, the recipient can execute an reentrancy attack calling several times the different function to steal founds or take advantage of other users/protocol.
Proof of Concept
This functions use the _distributeETHRewardsToUserForToken:
beforeTokenTransfer, GiantMevAndFeesPool contract:
The contract GiantLP use the GiantMevAndFeesPool contract as transferHookProcessor and when use the functions _mint, _burn, transferFrom and transfer of the ERC20, the function beforeTokenTransfer implemented in the GiantMevAndFeesPool bring a possibility to make a reentrancy attack because in the function _distributeETHRewardsToUserForToken implemented in the GiantMevAndFeesPool make a call to the _recipient
A contract can call the function transfer of GiantLP contract several time, transfer an amount from and to self, as the update of the claimed would not be done until, it is executed the function _afterTokenTransfer of the GiantLP contract, the due amount calculated in _distributeETHRewardsToUserForToken of SyndicateRewardsProcessor contract and the lastInteractedTimestamp of GiantLP contract will be incorrect
withdrawLPTokens, GiantPoolBase contract:
The possibility of the reentrancy is given when call function _onWithdraw, this function implemented in GiantMevAndFeesPool contract uses _distributeETHRewardsToUserForToken and this one call the recipient making the possibility of the reentrancy, breaking the code of L76-L89
batchDepositETHForStaking, StakingFundsVault contract:
The possibility of the reentrancy is given when call function _distributeETHRewardsToUserForToken, this function call the recipient making the possibility of the reentrancy, breaking the code of L76-L89
depositETHForStaking, StakingFundsVault contract:
The possibility of the reentrancy is given when call function _distributeETHRewardsToUserForToken, this function call the recipient making the possibility of the reentrancy, breaking the code of L136-L142
beforeTokenTransfer, StakingFundsVault contract:
The possibility of the reentrancy is given when call function _distributeETHRewardsToUserForToken in L333 and L337, this function call the recipient making the possibility of the reentrancy, breaking the code of L343-L351
Recommended Mitigation Steps
One possibility is to wrap(deposit) ether in WETH and transfer as ERC20 token.
Another is to add nonReentrant guard to the functions:
beforeTokenTransfer, GiantMevAndFeesPool contractwithdrawLPTokens, GiantPoolBase contractbatchDepositETHForStaking, StakingFundsVault contractdepositETHForStaking, StakingFundsVault contractbeforeTokenTransfer, StakingFundsVault contract
File: contracts/liquid-staking/GiantMevAndFeesPool.sol
@@ -143,7 +143,7 @@ contract GiantMevAndFeesPool is ITransferHookProcessor, GiantPoolBase, Syndicate
}
/// @notice Allow giant LP token to notify pool about transfers so the claimed amounts can be processed
- function beforeTokenTransfer(address _from, address _to, uint256) external {
+ function beforeTokenTransfer(address _from, address _to, uint256) external nonReentrant {
require(msg.sender == address(lpTokenETH), "Caller is not giant LP");
updateAccumulatedETHPerLP();
File: contracts/liquid-staking/GiantPoolBase.sol
@@ -66,7 +66,7 @@ contract GiantPoolBase is ReentrancyGuard {
/// @notice Allow a user to chose to withdraw vault LP tokens by burning their giant LP tokens. 1 Giant LP == 1 vault LP
/// @param _lpTokens List of LP tokens being owned and being withdrawn from the giant pool
/// @param _amounts List of amounts of giant LP being burnt in exchange for vault LP
- function withdrawLPTokens(LPToken[] calldata _lpTokens, uint256[] calldata _amounts) external {
+ function withdrawLPTokens(LPToken[] calldata _lpTokens, uint256[] calldata _amounts) external nonReentrant {
uint256 amountOfTokens = _lpTokens.length;
require(amountOfTokens > 0, "Empty arrays");
require(amountOfTokens == _amounts.length, "Inconsistent array lengths");
File: contracts/liquid-staking/StakingFundsVault.sol
@@ -66,7 +66,7 @@ contract StakingFundsVault is
/// @notice Batch deposit ETH for staking against multiple BLS public keys
/// @param _blsPublicKeyOfKnots List of BLS public keys being staked
/// @param _amounts Amounts of ETH being staked for each BLS public key
- function batchDepositETHForStaking(bytes[] calldata _blsPublicKeyOfKnots, uint256[] calldata _amounts) external payable {
+ function batchDepositETHForStaking(bytes[] calldata _blsPublicKeyOfKnots, uint256[] calldata _amounts) external payable nonReentrant {
uint256 numOfValidators = _blsPublicKeyOfKnots.length;
require(numOfValidators > 0, "Empty arrays");
require(numOfValidators == _amounts.length, "Inconsistent array lengths");
@@ -110,7 +110,7 @@ contract StakingFundsVault is
/// @notice Deposit ETH against a BLS public key for staking
/// @param _blsPublicKeyOfKnot BLS public key of validator registered by a node runner
/// @param _amount Amount of ETH being staked
- function depositETHForStaking(bytes calldata _blsPublicKeyOfKnot, uint256 _amount) public payable returns (uint256) {
+ function depositETHForStaking(bytes calldata _blsPublicKeyOfKnot, uint256 _amount) public payable nonReentrant returns (uint256) {
require(liquidStakingNetworkManager.isBLSPublicKeyBanned(_blsPublicKeyOfKnot) == false, "BLS public key is banned or not a part of LSD network");
require(
getAccountManager().blsPublicKeyToLifecycleStatus(_blsPublicKeyOfKnot) == IDataStructures.LifecycleStatus.INITIALS_REGISTERED,
@@ -312,7 +312,7 @@ contract StakingFundsVault is
}
/// @notice before an LP token is transferred, pay the user any unclaimed ETH rewards
- function beforeTokenTransfer(address _from, address _to, uint256) external override {
+ function beforeTokenTransfer(address _from, address _to, uint256) external override nonReentrant {
address syndicate = liquidStakingNetworkManager.syndicate();
if (syndicate != address(0)) {
LPToken token = LPToken(msg.sender);
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
Impact
A contract recipient could re-enter and receive rewards repeatedly, potentially stealing funds or taking advantage of other users and the protocol.
What the attacker needed
Attacker contract receives ETH rewards; attacker holds giant LP or vault LP tokens
The fix
The report recommends wrapping deposits in WETH and transferring as an ERC20, or adding a nonReentrant guard to the five listed functions; the project confirmed the issue.
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 smart contract can be written so that when it receives ether, it immediately runs more code. Here the pool pays out rewards before it records that the payment was made, so the receiving contract can call back into the pool and be paid again. This is called a reentrancy attack, and it can drain funds that belong to other users.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.