Malicious users can steal reward tokens via re-entrancy attack
The vault's reward-claiming logic lacked re-entrancy protection, so a malicious user could re-enter the claim function during a reward token transfer and receive more reward tokens than entitled. This allowed the attacker to steal reward tokens from the vault and other shareholders.
Bob's debt (VaultStorage.getAccountRewardDebt()[rewardToken][Bob]) is 100 (100 shares * 1.0)
Assume that at T2:
Bob attempts to redeem 90 vault shares
Current rewardsPerVaultShare is 2.0
When Line 211 below is executed, the vaultSharesBefore will be set to 100 vault shares. The _claimAccountRewards function will be executed in Line 212, and it will execute the _claimRewardToken function internally.
File: VaultRewarderLib.sol
202: /// @notice Called by the vault during enter and exit vault to update the account reward claims.
203: function updateAccountRewards(
204: address account,
205: uint256 vaultShares,
206: uint256 totalVaultSharesBefore,
207: bool isMint
208: ) external {
209: // Can only be called via enter or exit vault
210: require(msg.sender == address(Deployments.NOTIONAL));
211: uint256 vaultSharesBefore = _getVaultSharesBefore(account);
212: _claimAccountRewards(
213: account,
214: totalVaultSharesBefore,
215: vaultSharesBefore,
216: isMint ? vaultSharesBefore + vaultShares : vaultSharesBefore - vaultShares
217: );
218: }
Within the _claimRewardToken function, the _getRewardsToClaim function will be executed to compute the number of reward tokens that Bob is entitled to. Based on the formula within the _getRewardsToClaim function, Bob is entitled 100 reward tokens.
In Line 306 below, Bob's debt (VaultStorage.getAccountRewardDebt()[rewardToken][Bob]) will be updated to 20 ( vaultSharesAfter * rewardsPerVaultShare = 10 shares * 2.0 = 20). Note that vaultSharesAfter is 10 shares because Bob withdraws 90 shares from his initial 100 shares.
In Line 316 below, 100 reward tokens will be transferred to Bob.
File: VaultRewarderLib.sol
295: function _claimRewardToken(
296: address rewardToken,
297: address account,
298: uint256 vaultSharesBefore,
299: uint256 vaultSharesAfter,
300: uint256 rewardsPerVaultShare
301: ) internal returns (uint256 rewardToClaim) {
302: rewardToClaim = _getRewardsToClaim(
303: rewardToken, account, vaultSharesBefore, rewardsPerVaultShare
304: );
305:
306: VaultStorage.getAccountRewardDebt()[rewardToken][account] = (
307: (vaultSharesAfter * rewardsPerVaultShare) /
308: uint256(Constants.INTERNAL_TOKEN_PRECISION)
309: );
310:
311: if (0 < rewardToClaim) {
312: // Ignore transfer errors here so that any strange failures here do not
313: // prevent normal vault operations from working. Failures may include a
314: // lack of balances or some sort of blacklist that prevents an account
315: // from receiving tokens.
316: try IEIP20NonStandard(rewardToken).transfer(account, rewardToClaim) {
317: bool success = TokenUtils.checkReturnCode();
318: if (success) {
319: emit VaultRewardTransfer(rewardToken, account, rewardToClaim);
320: } else {
321: emit VaultRewardTransfer(rewardToken, account, 0);
322: }
323: // Emits zero tokens transferred if the transfer fails.
324: } catch {
325: emit VaultRewardTransfer(rewardToken, account, 0);
326: }
327: }
Assume that the reward token contains a hook or callback. As a result, the control will be passed back to Bob. Note that there are no restrictions on the type of reward tokens in the context of this audit.
Bob can re-enter the vault and execute the claimAccountRewards function, which is not guarded against re-entrancy. When Line 197 is executed, the totalVaultSharesBefore will still remain 100 vault shares because the execution _redeemFromNotional function has not been completed yet. Thus, the number of vault shares has not been updated on Notional side yet. The _claimRewardToken function, followed by _getRewardsToClaim will be executed again internally.
File: VaultRewarderLib.sol
193: /// @notice Callable by an account to claim their own rewards, we know that the vault shares have
194: /// not changed in this transaction because the contract has not been called by Notional
195: function claimAccountRewards(address account) external override {
196: require(msg.sender == account);
197: uint256 totalVaultSharesBefore = VaultStorage.getStrategyVaultState().totalVaultSharesGlobal;
198: uint256 vaultSharesBefore = _getVaultSharesBefore(account);
199: _claimAccountRewards(account, totalVaultSharesBefore, vaultSharesBefore, vaultSharesBefore);
200: }
Based on the formula within the _getRewardsToClaim function, Bob is entitled 180 reward tokens.
The vault will transfer an additional 180 reward tokens to Bob again, which is incorrect. In this case, Bob has stolen 180 reward tokens from the vault and other shareholders.
Add re-entrancy guard on the claimAccountRewards function to prevent anyone from re-entering the vault under any circumstance.
Discussion
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
0xmystery commented:
Devoid of coded POC to substantiate exploit
xiaoming9090
Escalate.
This issue should be a valid High.
The lead judge mentioned that the issue was "Devoid of coded POC to substantiate exploit" and marked it as invalid. However, the POC in the report is already sufficient to demonstrate that the vulnerability mentioned in the report could lead to a loss of assets. Thus, it should be valid.
sherlock-admin3
Escalate.
This issue should be a valid High.
The lead judge mentioned that the issue was "Devoid of coded POC to substantiate exploit" and marked it as invalid. However, the POC in the report is already sufficient to demonstrate that the vulnerability mentioned in the report could lead to a loss of assets. Thus, it should be valid.
You've created a valid escalation!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
mystery0x
Escalate.
This issue should be a valid High.
The lead judge mentioned that the issue was "Devoid of coded POC to substantiate exploit" and marked it as invalid. However, the POC in the report is already sufficient to demonstrate that the vulnerability mentioned in the report could lead to a loss of assets. Thus, it should be valid.
Will have the sponsors look into this finding to decide whether or not their reward tokens will have hook entailed. It seems to me the sponsors would have specified any of these weird tokens requiring non-reentrant visibility in the 2nd question of the contest readme Details if they had been adopted.
WangSecurity
The protocol didn't specify which tokens will be used as rewards, so we have to assume only the standard tokens without any weird traits will be used. Hence, we should assume tokens with hooks or callbacks allowing for reentrancy won't be used. Planning to reject the escalation and leave the issue as it is.
xiaoming9090
The protocol didn't specify which tokens will be used as rewards, so we have to assume only the standard tokens without any weird traits will be used. Hence, we should assume tokens with hooks or callbacks allowing for reentrancy won't be used. Planning to reject the escalation and leave the issue as it is.
@WangSecurity The contracts are meant to handle tokens that Notional protocol will receive from other protocols or projects (e.g., grants). Thus, it is not possible for Notional to predict what kind of tokens they will receive in the future. To be on the safe side, we should assume the worst-case scenario where it might be possible that some of the reward tokens might contain hook or callback, and necessary measures should be implemented to guard against potential re-entrancy attack.
@jeffywu You might want to have a look at this. Thanks.
jeffywu
I agree with @xiaoming9090's assessment, this is a valid issue. Some reward tokens may hold callback hooks that we are unaware of.
0502lian
In my opinion, even if there is reentrancy, there won’t be any loss. If my understanding is incorrect, please correct me.
@xiaoming9090 @jeffywu
Thank you!
The loss due to reentrancy is based on the description by @xiaoming9090 , ‘When Line 197 is executed, the totalVaultSharesBefore will still remain 100 vault shares because the execution of the _redeemFromNotional function has not been completed yet. Thus, the number of vault shares has not been updated on the Notional side yet.’
However, after research, I found that totalVaultSharesBefore is updated first and then _redeemFromNotional() is called.
Because all data is updated before token transfer, There is nothing can be done in re-entrancy.
xiaoming9090
During the re-entrancy, Bob's vault shares should have decreased from 100 vault shares to 10 vault shares since he withdraws 90 vault shares. However, it remains at 100 vault shares. This is because vault account shares are not updated in storage until the vault complete its exit here. Thus, the reward calculation in the re-entrancy will be incorrect.
There is a minor typo in this paragraph. Refer to the update below. Apart from that, the rest, including the math calculation, is correct.
Diff
- Line 197 is executed, the totalVaultSharesBefore will still remain 100 vault shares because the execution _redeemFromNotional function has not been completed yet.
+ Line 198 is executed, the vaultSharesBefore will still remain 100 vault shares because the execution _redeemFromNotional function has not been completed yet.
0502lian
During the re-entrancy, Bob's vault shares should have decreased from 100 vault shares to 10 vault shares since he withdraws 90 vault shares. However, it remains at 100 vault shares. This is because vault account shares are not updated in storage until the vault complete its exit here. Thus, the reward calculation in the re-entrancy will be incorrect.
There is a minor typo in this paragraph. Refer to the update below. Apart from that, the rest, including the math calculation, is correct.
Diff
- Line 197 is executed, the totalVaultSharesBefore will still remain 100 vault shares because the execution _redeemFromNotional function has not been completed yet.
+ Line 198 is executed, the vaultSharesBefore will still remain 100 vault shares because the execution _redeemFromNotional function has not been completed yet.
Then I think you are right. User's vault account shares indeed updates after _redeemFromNotional(). If the reward token is ERC777 , it could be a re-entrancy attack.
novaman33
How are these in scope since the answer of the question in readMe is:
`
If you are integrating tokens, are you allowing only whitelisted tokens to work with the codebase or any complying with the standard? Are they assumed to have certain properties, e.g. be non-reentrant? Are there any types of weird tokens you want to integrate?
How are these in scope since the answer of the question in readMe is: ` If you are integrating tokens, are you allowing only whitelisted tokens to work with the codebase or any complying with the standard? Are they assumed to have certain properties, e.g. be non-reentrant? Are there any types of weird tokens you want to integrate?
Note that there are two areas where ERC20 tokens will be used:
Vault's assets
Reward tokens
The above-listed tokens (weETH, USDe, sUSDe, rsETH) are the vault's asset tokens. Anyone who reviews the vault code will clearly understand it. Thus, it is understandable from the sponsor's point of view that this information refers only to the vault's asset tokens, and not the reward tokens.
As mentioned in my earlier comments, the reward-related contracts meant to handle tokens that Notional protocol will receive from other protocols or projects (e.g., grants). Thus, it is not possible for Notional to predict what kind of tokens they will receive in the future. Thus, it makes sense for the protocol not to narrow down the reward tokens accepted at this point.
In addition, in the sponsor's earlier comment, they already shared the view that some reward tokens might contain callback hooks that they are unaware of.
novaman33
The question is regarding any strange ERC20 tokens. Their answer is a list of tokens that does include some rebasing tokens. I agree that there are two areas where ERC20 tokens are used, but I do not agree that by any means this means that all the other tokens are supported(from this answer). Given Sherlock's hierarchy of truth, both this and #61 should be invalid, as sponsors comments in judging cannot change the scope.
WangSecurity
About the reward tokens. The reason why they weren't specified in the README is because they're not set by the admins of Notional. The reward tokens are the tokens that other protocols, which Notional integrates with, use to pay out the rewards/grants. Hence, I believe it's enough contextual evidence that the protocol indeed needs to work with any type of reward tokens.
Therefore, I believe this issue is valid. Even though it's only possible with tokens with hooks, the report shows how the attacker gets almost 200% more shares than they should've got. Hence, I believe high severity is appropriate, planning to accept the escalation.
The original report as published on Sherlock, by xiaoming90. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the reward-claiming function `claimAccountRewards` lacked a re-entrancy guard, and the vault shares were not updated before transferring reward tokens, so a re-entrant call used stale share balances.
Impact
Reward tokens can be stolen by malicious users, but only if the reward token contains a hook or callback enabling re-entrancy.
What the attacker needed
Reward token has a hook or callback; attacker holds vault shares
The fix
The protocol team fixed this issue by adding a re-entrancy guard to the `claimAccountRewards` function.
How it was found
Bob holds 100 vault shares and has a reward debt of 100.
Bob initiates redemption of 90 vault shares while rewards per share is 2.0.
The vault calculates Bob's reward as 100 tokens and updates his debt to 20.
The vault transfers 100 reward tokens to Bob; if the token has a hook, control returns to Bob.
Bob re-enters the vault and calls `claimAccountRewards`.
The vault still sees Bob's old share balance of 100 and pays an additional 180 reward 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.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
When you withdraw from a vault, the vault calculates and sends you reward tokens. If the reward token itself can call back into the vault during that transfer (like a token with a hook), the attacker can call the claim function again before the first withdrawal finishes. Because the vault still thinks the attacker has the old, larger share balance, it pays out extra rewards.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.