-
Notifications
You must be signed in to change notification settings - Fork 5
Add totalDeposits tracking and minor improvements #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
luis-immunefi
wants to merge
1
commit into
main
Choose a base branch
from
luis-immunefi-patch-27
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reentrancy vulnerability and inconsistent accounting in EtherStore's withdraw function
Description:
TL;DR:
The EtherStore contract's
withdrawfunction callsmsg.senderexternally before updating state, enabling a reentrancy attack that drains funds and corrupts thetotalDepositsaccounting.The
withdrawfunction in theEtherStorecontract is vulnerable because it performs an external call tomsg.senderbefore resetting the caller's balance and updating thetotalDepositsvariable. This ordering allows an attacker to reenter the function and repeatedly drain Ether, while also causing inconsistencies in internal accounting.Details
In the
EtherStorecontract, thewithdrawfunction first retrieves the caller's balance and then verifies that it is greater than zero. However, the function then performs an external call viamsg.sender.call{value: bal}('')before updating the user's balance to zero and decrementing thetotalDepositscounter. As a result, if an attacker is able to reenter the function during the external call, the balance would not have been set to zero yet, allowing additional withdrawals. This reentrancy vulnerability is exacerbated by the inconsistent state update, where thetotalDepositsis only modified after the external call, leading to potential discrepancies in any protocol logic relying on this variable. The core issue stems from the absence of a reentrancy guard and the incorrect ordering of state updates relative to external interactions.Example snippet from
EtherStore.sol:Impact
Exploitation of this vulnerability could allow an attacker to repeatedly invoke the
withdrawfunction and drain the contract's ETH balance. Moreover, the delayed update tototalDepositsintroduces an inconsistency that could compromise any logic depending on that variable, potentially affecting protocol invariants and resulting in financial losses.Mitigation Steps:
Implement a reentrancy guard to prevent recursive calls to the
withdrawfunction.-Reorder state updates to occur before any external calls, following the checks-effects-interactions pattern.
-Eliminate or properly manage the redundant
totalDepositsvariable to prevent future accounting discrepancies.