Unexpected Ether - Smart Contract Security Field Guide (2024)

Forcing a smart contract to hold an Ether balance can influence its internal accounting and security assumptions. There are multiple ways a smart contract can receive Ether. The hierarchy is as follows:

  1. Check whether a payable external receive function is defined.
  2. If not, check whether a payable external fallback function is defined.
  3. Revert.

The precedence of each function is explained in this great graphic from the Solidity by Example article:

 1 2 3 4 5 6 7 8 910111213
Which function is called fallback() or receive()? send Ether | msg.data is empty? / \ yes no / \receive() exists? fallback() / \ yes no / \ receive() fallback()

Consider the following example:

 1 2 3 4 5 6 7 8 9101112
pragma solidity ^0.8.13;contract Vulnerable { receive() external payable { revert(); } function somethingBad() external { require(address(this).balance > 0); // Do something bad }}

The contract's logic seemingly disallows direct payments and prevents "something bad" from happening. However, calling revert in both fallback and receive cannot prevent the contract from receiving Ether. The following techniques can be used to force-feed Ether to a smart contract.

Selfdestruct

When the SELFDESTRUCT opcode is called, funds of the calling address are sent to the address on the stack, and execution is immediately halted. Since this opcode works on the EVM-level, Solidity-level functions that might block the receipt of Ether will not be executed.

Pre-calculated Deployments

Additionally, the target address of newly deployed smart contracts is generated deterministically. The address generation can be looked up in any EVM implementation, such as the py-evm reference implementation by the Ethereum Foundation:

12
def generate_contract_address(address: Address, nonce: int) -> Address: return force_bytes_to_address(keccak(rlp.encode([address, nonce])))

An attacker can send funds to this address before the deployment has happened. This is also illustrated by this 2017 Underhanded Solidity Contest submission.

Mitigation

The above effects illustrate that relying on exact comparisons to the contract's Ether balance is unreliable. The smart contract's business logic must consider that the actual balance associated with it can be higher than the internal accounting's value.

Generally, using the contract's balance as a guard is not advisable.

Unexpected Ether - Smart Contract Security Field Guide (2024)
Top Articles
Dukascopy Bank
Przerwa techniczna
Food King El Paso Ads
Main Moon Ilion Menu
Inducement Small Bribe
Ffxiv Shelfeye Reaver
Chatiw.ib
Rabbits Foot Osrs
Samsung 9C8
Derpixon Kemono
Prices Way Too High Crossword Clue
Identogo Brunswick Ga
104 Whiley Road Lancaster Ohio
Billionaire Ken Griffin Doesn’t Like His Portrayal In GameStop Movie ‘Dumb Money,’ So He’s Throwing A Tantrum: Report
Mikayla Campinos Laek: The Rising Star Of Social Media
Ubg98.Github.io Unblocked
Myhr North Memorial
Isaidup
Doublelist Paducah Ky
Shreveport City Warrants Lookup
Weve Got You Surrounded Meme
Which Sentence is Punctuated Correctly?
Skycurve Replacement Mat
1636 Pokemon Fire Red U Squirrels Download
4.231 Rounded To The Nearest Hundred
United E Gift Card
Myra's Floral Princeton Wv
Abga Gestation Calculator
RFK Jr., in Glendale, says he's under investigation for 'collecting a whale specimen'
Steven Batash Md Pc Photos
What Are Digital Kitchens & How Can They Work for Foodservice
School Tool / School Tool Parent Portal
How Much Is Mink V3
Craigslist Lakeside Az
Page 5662 – Christianity Today
Überblick zum Barotrauma - Überblick zum Barotrauma - MSD Manual Profi-Ausgabe
Legit Ticket Sites - Seatgeek vs Stubhub [Fees, Customer Service, Security]
2 Pm Cdt
Fwpd Activity Log
Craigslist - Pets for Sale or Adoption in Hawley, PA
Traumasoft Butler
Sig Mlok Bayonet Mount
Exam With A Social Studies Section Crossword
Arnesons Webcam
Quaally.shop
Wgu Admissions Login
UWPD investigating sharing of 'sensitive' photos, video of Wisconsin volleyball team
Zeeks Pizza Calories
Displacer Cub – 5th Edition SRD
Bonecrusher Upgrade Rs3
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 5882

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.