-
Notifications
You must be signed in to change notification settings - Fork 5
Update EtherStore.sol #19
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unchecked underflow in
|
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.
Re-entrancy and underflow in
withdrawenable balance inflation and Ether theft inEtherStoreDescription:
TL;DR:
The
withdrawfunction inEtherStoreis exposed to both reentrancy and underflow vulnerabilities, allowing an attacker to recursively withdraw funds while inflating their balance.The vulnerability arises because the
withdrawfunction first sends Ether tomsg.senderand only then updates the user's balance to-1, which, due to underflow in an unsigned integer, becomes2^256 - 1. This mis-sequencing permits reentrant calls, enabling an attacker to repeatedly withdraw funds.Details
The function retrieves the balance from the
balancesmapping and checks that it is greater than zero. It then transfers the balance to the caller using a low-level call:After the transfer, instead of zeroing out the balance, the contract sets
balances[msg.sender]to-1. Sincebalancesis of typeuint256, this assignment underflows, converting-1to2^256 - 1. Consequently, when the attacker invokes the function, the sequence of an external call before state update allows them to recursively callwithdrawseveral times. This breaks the intended invariant, as the balance becomes artificially inflated and the attacker can drain the contract's funds.Impact
Exploitation of this vulnerability can lead to complete depletion of the contract's Ether reserves. The attacker can repeatedly call
withdrawby taking advantage of the underflowed large balance, effectively compromising the integrity of the contract's accounting and resulting in significant financial loss.Mitigation Steps:
Update the state by setting
balances[msg.sender]to 0 before making the external call to prevent reentrancy.-Implement a reentrancy guard to restrict recursive calls to the
withdrawfunction.-Remove any erroneous assignments that lead to underflow, ensuring that balance updates accurately reflect withdrawals.