What is a reentrancy attack in Solidity? – Alchemy
A reentrancy attack in Solidity repeatedly withdraws funds from a smart contract and transfers them to an unauthorized contract until the funds have been exhausted. The attack occurs during the execution cycle on the blockchain when bad actors find an exploitable smart contract. Reentrancy attacks have liquidated millions of dollars from DAOs and blockchain protocols.
The following article explains the mechanics of a reentrancy attack, two types of reentrancy attacks, and the preventive measures Solidity developers can take to secure a smart contract from vulnerabilities on the Ethereum and Solana blockchains.
Reentrancy attacks occur when a smart contract function temporarily gives up control flow of the transaction by making an external call to a contract that is sometimes written by unknown or possibly hostile actors. This permits the latter contract to make a recursive call back to the primary smart contract function to drain its funds.
The execution cycle of a smart contract on the Ethereum blockchains checks the balance, sends the funds, and then updates the balance. While the smart contract is in escrow, bad actors can make another call to withdraw funds. The cycle repeats until all funds are effectively drained.
A reentrancy attack creates a recursive process that transfers funds between two smart contracts, the vulnerable contract and the malicious contract. Here are the steps of a reentrancy attack:
The diagram below illustrates the attack scenario:
There are two types of reentrancy attacks: a single function and cross-function reentrancy attack.
A single reentrancy attack occurs when the vulnerable function is the same function the attacker is trying to recursively call. Single reentrancy attacks are simpler and easier to prevent than cross-function reentrancy attacks.
A cross-function reentrancy attack is feasible only when a vulnerable function shares state with another function that has a desirable effect for the attacker.Cross-function attacks are harder to detect and more difficult to prevent.
A cross-contract reentrancy attack occurs when a state from one contract is called in another before it is fully updated. Cross-contract reentrancy attacks usually occur when multiple contracts manually share the same variable and some update the shared variable insecurely.
The following prominent reentrancy attacks further illustrate how bad actors have exploited vulnerabilities in blockchain protocols: The DAO hack, Lendf.me, and Cream Finance.
The Ethereum DAO was hacked for approximately $60 million in Ether. Ethereums DAO was designed as an investment fund where network members could vote on investment decisions directly.
The DAO raised roughly $150 million, but experts and community participants expressed concerns surrounding the smart contract's security that was storing funds. The funds were locked in a smart contract vulnerable to a reentrancy attack due to a recursive call bug in the source code. Before the developer team fixed the problem, a hacker executed an attack and drained the contract.
In April 2020, a bad actor stole $25 million using a reentrancy attack from the Lendf.me protocol, a decentralized finance protocol for lending operations on the Ethereum network.
The protocol developers overlooked the fact that ERC-777 tokens contain a callback function that notifies users when money has been sent or received. Hackers exploited the vulnerability by instituting a malicious smart contract as the recipient and draining the Lendf.me protocol of 99.5% of its funds.
In October 2021, a bad actor stole over $130 million worth of ERC-20 and CREAM liquidity protocol (LP) tokens by using a reentrancy attack on the protocols flash loan feature. The root cause of the exploit was the erroneous integration of AMP into the CREAM finance protocol.
Developing a rigorous blockchain security framework is critical to prevent and mitigate potential damage from a reentrancy attack. The following anti-reentrancy best practices will help developers and the broader web3 community secure their funds:checks, effects and interactions (CEI), reentrancy guards, pull payments, and gas limits.
The CEI process is a rudimentary method to prevent reentrancy. Checks refer to the truthfulness of the condition, effects refer to state modifications that result from interaction, and interactions refer to transactions between functions or contracts.
Potential security risks and loopholes associated with placing executive effects before interactions are an important consideration for developers.
A reentrancy guard or mutex can be created as a function or function modifier. A boolean lock is placed around the function call that is vulnerable to reentrancy. This implies that the initial state of locked is false, however, it is set to true immediately before the vulnerable function execution begins and is then quickly set back to false after termination.
A more secure end-to-end transaction is by using the pull payment methodology. The pull payment process mandates using an intermediary escrow to send funds and avoids direct contact with a potentially hostile contact.
By sending funds via an intermediary escrow, the smart contract's funds are protected from a reentrancy attack. The escrow could be subject to reentrancy if it manages funds for multiple accounts. The CEI pattern and reentrancy guard should be implemented where appropriate.
Gas limits are not an optimal method to avert an attacker since gas costs depend on Ethereums opcodes, which are subject to change. On the other hand, smart contract code is immutable.
Understanding the difference between the send, transfer, and call functions is important. Send and transfer are effectively the same, but the transfer will revert if the transaction fails and send will not.
Unlike send and transfer, the call function does not have a gas limit and will forward its gas to execute multi-contract transactions. Unfortunately, this also means that reentrancy attacks are possible.
The execution cycle of a smart contract on Ethereum is not a foolproof process. Bad actors exploit the blockchain by implementing reentrancy attacks to transfer and drain funds from vulnerable smart contracts. Taking preventive measures will ensure Soldiity developers safer execution cycles and the protection of their blockchain protocols.
To learn about Solidity smart contract development best practices, join Alchemy's free, 7-week Ethereum Developer Bootcamp. Originally a $3,000 certification course, Alchemy University's bootcamp is the premier place to learn Solidity for free.
If developers are new to development in general, Alchemy's 3-week JavaScript crash course is a great prerequisite before starting an Ethereum bootcamp.
Read the original:
What is a reentrancy attack in Solidity? - Alchemy
- Snap Specs Review: Standalone AR Glasses Are Neat, All The Bugs Are Not - Engadget - December 5th, 2026
- The 9 States in America With the Highest Risk of Bed Bugs, Ranked - Mental Floss - December 3rd, 2026
- Bed bugs found at 2 North Royalton schools, prompting partial closure - FOX 8 News - December 3rd, 2026
- Bed bugs discovered in office at FEMA headquarters in DC - ABC7 New York - November 27th, 2026
- Police shut cell at Sheffield station after reports of bed bugs - The Star - November 27th, 2026
- Over 1,000 emergency employees told to work from home after FEMA's Washington DC office was found infeste - The Times of India - November 27th, 2026
- Meet the Bugs That Live in Your Home - Nautilus | Science Connected - November 25th, 2026
- I have bed bugs and my landlord refuses to act - The Times - November 25th, 2026
- Trumps Disaster Agency Hit With Skin-Crawling Revelations - The Daily Beast - November 25th, 2026
- Bed bugs found in FEMA Headquarters office prompts building closure - WJLA - November 23rd, 2026
- FEMA is dealing with its own emergency: bed bugs - WTOP News - November 23rd, 2026
- FEMA facing bed bug infestation - KPLC 7 News - November 23rd, 2026
- Trumps FEMA headquarters dealing with bed bug outbreak with staffers ordered to stay home - The Independent - November 23rd, 2026
- Cypermethrin and Imidacloprid Combination Clears Bed Bugs in Tehran Field Trial - Bioengineer.org - November 23rd, 2026
- Exclusive | Bedbug infestation plaguing NYC 911 call center - New York Post - November 23rd, 2026
- FEMA is dealing with its own emergency: bed bugs - CNN - November 23rd, 2026
- How To Clean Your Suitcase After TravelingAnd Why You Should Never Skip This Step - Southern Living - November 23rd, 2026
- FEMA Barely Exists at This Point, but at Least Its Also Completely Infested with Bed Bugs - Jezebel - November 23rd, 2026
- In 2025, a 24-year-old Montreal woman died mysteriously at an Istanbul hotel; food poisoning was suspecte - The Times of India - November 23rd, 2026
- Bed bug infestation gets out of control inside Brooklyn 911 call center - The Weekly Times - November 23rd, 2026
- FEMA faces bed bug infestation - KXLY.com - November 23rd, 2026
- Baby dies after home filled with poisonous pesticides used to kill bed bugs as pest control boss is jailed - The Irish Sun - November 23rd, 2026
- Bed bugs discovered in office at FEMA headquarters in DC - ABC News - Breaking News, Latest News and Videos - November 21st, 2026
- FEMA facing bed bug infestation - KCRG - November 21st, 2026
- Stop bed bugs before your guests find them - Asian Hospitality - November 21st, 2026
- FEMA hit with bed bugs - KXLY.com - November 21st, 2026
- A bed bug infestation at FEMA's Washington, D.C., headquarters sent nearly 1,000 staffers home Thursday for a "building-wide" pest control... - November 21st, 2026
- FEMA facing bed bug infestation - KMVT - November 21st, 2026
- Safe extermination of bedbugs - CBC - November 21st, 2026
- Reliable Revenue in the Year Ahead - Pest Control Technology - November 21st, 2026
- FEMAs Washington base infested with bedbugs what it means for staff - The Independent - November 21st, 2026
- FEMA Staff Ordered to Stay Home Due to Bed Bug Infestation - The Daily Beast - November 21st, 2026
- Baby dies after home filled with poisonous pesticides used to kill bed bugs as pest control boss is jailed - The Sun - November 21st, 2026
- Bedbugs conquered Britain this man is fighting back by letting them bite him - The Telegraph - November 19th, 2026
- Bed bugs found at Fort Simpson high school prompt health inspection - Cabin Radio - November 19th, 2026
- Rats, bed bugs and mould: New rules planned for Reading council homes - Reading Today Online - November 17th, 2026
- Its the End Of The World As We Know It - The Trek - November 17th, 2026
- The pest Ill never forget: the bedbugs that bit me and worried Jake Gyllenhaal - The Guardian - November 17th, 2026
- Cumberland Council deals with 3,499 complaints about pests in three years - Whitehaven News - November 15th, 2026
- Bed bugs arent just a poverty problem, new study reveals - Earth.com - November 13th, 2026
- 32 Deeply Unsettling Facts About The World That Make Me Wish I Could Unlearn How To Read - BuzzFeed - November 11th, 2026
- Bedbug-killing drying room - a practical initiative of the Navy. - Vietnam.vn - November 11th, 2026
- Dont let the bed bugs bite! Chinas viral hotel map draws 200,000 users and angry hoteliers - Malay Mail - November 11th, 2026
- 12 Solidity smart contract security best practices - Alchemy - November 11th, 2026
- Qantas to Cut 850 Jobs and Outsource Roles to Accenture - SSBCrack - November 11th, 2026
- Dont let the bed bugs bite! Chinas viral hotel map draws 200,000 users and angry hoteliers - Yahoo - November 9th, 2026
- British entomologist sleeps with thousands of bedbugs for scientific research - 24 News HD - November 9th, 2026
- Meet researcher who subjects himself to being bitten by thousands of bedbugs - Pulse Nigeria - November 9th, 2026
- The scientist who sleeps with thousands of bed bugs, letting them drink his blood - NationalWorld - November 9th, 2026
- The man who sleeps with thousands of bedbugs for science - Metro.co.uk - November 9th, 2026
- Experts address what happens when luggage and shoes are put on the bed - The Independent - November 9th, 2026
- Fella sleeps with 60k blood-sucking bed bugs in garden lab - and has feeding days - Daily Star - November 7th, 2026
- Insect fanatic straps bedbugs to himself in the name of science - Gloucestershire Live - November 7th, 2026
- Scientist studies bed bugs - by sleeping with thousands of them in garden lab - Yahoo - November 7th, 2026
- A school in Camagey is closed due to bedbugs following complaints from parents of the children - CiberCuba - November 5th, 2026
- Power BI - Get Data From SharePoint List / Library - November 5th, 2026
- How bad is it to put your luggage on the bed? We asked germ experts. - The Washington Post - November 3rd, 2026
- Let the bedbugs bite: How one scientist has chosen to sleep with critters for the past five years - and straps some of the 60,000 he has bred on his... - November 3rd, 2026
- Indiana Makes a National Top 10 List Thatll Make You Itch - WKFR - November 3rd, 2026
- Noah Kahan 2027 concert dates: Where to get tickets to 'The Last of the Bugs' world tour - Yahoo - November 3rd, 2026
- UK woman finds 40 dead bedbugs in hotel bed, claims she suffered over 100 bites - India Today - November 3rd, 2026
- The scientist who chooses to sleep with bed bugs - BBC - November 3rd, 2026
- Scientist chooses to sleep with bedbugs for years to show what they're really like and has surprising results - UNILAD - November 3rd, 2026
- The man who chooses to sleep with bedbugs - Yahoo News Australia - November 1st, 2026
- The Gloucestershire man who chooses to sleep with bedbugs - BBC - November 1st, 2026
- Bed Bugs Cause Severe Skin Reactions After Hotel Stay, With Medical Guidance on Bites and Treatment - - November 1st, 2026
- Flea or Mosquito Bite? Bug Experts Explain the Key to Telling Them Apart - Prevention - November 1st, 2026
- Putting the Bite on Biting Pests with Coconut Oil Derivatives - USDA (.gov) - October 29th, 2026
- Oberlin library cleared of bedbugs in Raleigh - CBS 17 - October 29th, 2026
- Mum vows never to use Holiday Inn again after Exeter stay leaves her in A&E - Devon Live - October 28th, 2026
- The Problem of Celebrating Mints on Pillows, When You Have Bugs in the Bed - TAPinto - October 28th, 2026
- Tiny web-dwelling bug discovered in China is a brand-new species - Bioengineer.org - October 28th, 2026
- Oakmont Carnegie Library temporarily closed after bed bugs found in part of building - WPXI - October 28th, 2026
- Bed bug cases are on the rise: How to best protect your mattress from the bugs amid the country wide spike - Good Housekeeping - October 28th, 2026
- It Looks Like a Still Image From Paranormal Activity: Man Sleeps on Water Jugs for 2 Months to Escape Bedbugs in Texas - The Nerd Stash - October 28th, 2026
- Bedbugs force prison cells to close as 3,800 spaces lost in overcrowding crisis - Daily Express - October 28th, 2026
- Bedbug infestation forces closure of cells at Norfolk prison amid overcrowding crisis - EDP24 - October 28th, 2026
- Bedbugs force prisons to leave cells empty - The Telegraph - October 26th, 2026
- Bedbugs force prisons to leave cells empty - The Telegraph - October 26th, 2026
- Almost 3,800 prison cells left empty by bedbugs, asbestos and diseases - The Independent - October 26th, 2026
