Reenterancy in _sendSherRewardsToOwner()
The contract deletes the reward mapping after making an external token transfer, so if the token allows a callback the attacker can re-enter and claim the same reward repeatedly. This could drain the contract's entire SHER balance.
Originally disclosed on Code4rena
Submitted by kirk-baird
This is a reentrancy vulnerability that would allow the attacker to drain the entire SHER balance of the contract.
Note: this attack requires gaining control of execution sher.transfer() which will depend on the implementation of the SHER token. Control may be gained by the attacker if the contract implements ERC777 or otherwise makes external calls during transfer().
Proof of Concept
See _sendSherRewards
function _sendSherRewardsToOwner(uint256 _id, address _nftOwner) internal {
uint256 sherReward = sherRewards_[_id];
if (sherReward == 0) return;
// Transfers the SHER tokens associated with this NFT ID to the address of the NFT owner
sher.safeTransfer(_nftOwner, sherReward);
// Deletes the SHER reward mapping for this NFT ID
delete sherRewards_[_id];
}
Here sherRewards are deleted after the potential external call is made in sher.safeTransfer(). As a result if an attacker reenters this function sherRewards_ they will still maintain the original balance of rewards and again transfer the SHER tokens.
As _sendSherRewardsToOwner() is internal the attack can be initiated through the external function ownerRestake() see here.
Steps to produce the attack:
- Deploy attack contract to handle reenterancy
- Call
initialStake()from the attack contract with the smallestperiod - Wait for
periodamount of time to pass - Have the attack contract call
ownerRestake(). The attack contract will gain control of the (See note above about control flow). This will recursively callownerRestake()until the balance ofSherlockis 0 or less than the user’s reward amount. Then allow reentrancy loop to unwind and complete.
Recommended Mitigation Steps
Reentrancy can be mitigated by one of two solutions.
The first option is to add a reentrancy guard like nonReentrant the is used in SherlockClaimManager.sol.
The second option is to use the checks-effects-interactions pattern. This would involve doing all validation checks and state changes before making any potential external calls. For example the above function could be modified as follows.
function _sendSherRewardsToOwner(uint256 _id, address _nftOwner) internal {
uint256 sherReward = sherRewards_[_id];
if (sherReward == 0) return;
// Deletes the SHER reward mapping for this NFT ID
delete sherRewards_[_id];
// Transfers the SHER tokens associated with this NFT ID to the address of the NFT owner
sher.safeTransfer(_nftOwner, sherReward);
}
Additionally the following functions are not exploitable however should be updated to use the check-effects-interations pattern.
Sherlock._redeemShares()should do_transferTokensOut()last.Sherlock.initialStake()should dotoken.safeTransferFrom(msg.sender, address(this), _amount);lastSherClaim.add()should dosher.safeTransferFrom(msg.sender, address(this), _amount);after updatinguserClaimsSherlockProtocolManager.depositToActiveBalance()should dotoken.safeTransferFrom(msg.sender, address(this), _amount);after updatingactiveBalances
Evert0x (Sherlock) confirmed, but disagreed with High severity and commented:
Good find. I think it’s med-risk as it depends on the implementation of SHER token (does it allow callbacks).
Jack the Pug (judge) decreased severity to Medium and commented:
Downgrade to
Medas the SHER token is a known token that currently comes with no such hooks like ERC777.
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
The attacker could drain the entire SHER balance of the contract, but only if the SHER token makes external calls during transfer (for example ERC777); the judge downgraded severity because the known SHER token has no such hooks.
What the attacker needed
SHER token must allow callbacks during transfer; Attacker deploys a malicious contract; Attacker stakes with the smallest period
The fix
The report recommends adding a reentrancy guard or using the checks-effects-interactions pattern by deleting the reward before transferring the tokens.
How it was found
- Deploy an attack contract that handles the reentrancy.
- Call initialStake() from the attack contract with the smallest period.
- Wait for the period to pass.
- Have the attack contract call ownerRestake(), which re-enters recursively until the contract balance is zero or below the reward amount, then unwinds.
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 reentrancy bug happens when a contract sends funds or calls another contract before it updates its own records. The other contract can call back into the original function before the record is updated, so it looks like the reward is still owed. Here the reward is deleted only after the token transfer, so a malicious token could trigger the function again and again.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.