Exploring EVM Attacks: How to Secure Your Blockchain
By. Lance Ennen

Exploring EVM Attacks: How to Secure Your Blockchain
Hello, I’m Lance Ennen, a passionate Web3 developer. I spend most of my time designing and developing decentralized applications (dApps), and today, I want to share with you some insights on a topic I find fascinating and crucial in the blockchain world - Ethereum Virtual Machine (EVM) attacks.
Understanding and mitigating these attacks is essential to ensure the security of your blockchain. So, let's dive right in.
What is EVM?
The Ethereum Virtual Machine (EVM) is the runtime environment for smart contracts in Ethereum. It's completely isolated from the mainnet, which means each smart contract running inside the EVM has no access to network, filesystem, or other processes.
However, this isolation doesn't make EVM immune to vulnerabilities and attacks. Let's explore some of them.
Common EVM Attacks
1. Re-Entrancy Attack
Re-entrancy attack is perhaps the most infamous EVM attack, thanks to the DAO attack in 2016 which resulted in a loss of around $60 million.
function withdraw() public {
uint amount = balances[msg.sender];
(bool success, ) = msg.sender.call.value(amount)("");
require(success);
balances[msg.sender] = 0;
}
In the above code, the balance is set to zero after the external call. If the call stack allows the called contract to call back to the original contract, the original balance will still be in place, leading to multiple withdrawals. This is the essence of a re-entrancy attack.
2. Arithmetic Over/Underflows
In Solidity, the maximum value for uint256 is 2^256-1
and the minimum is 0
. If you add one to the maximum value or subtract one from the minimum, you find yourself in a situation called overflow and underflow respectively.
Consider the following code:
function underflow() public {
uint under = 0;
under--; // under is now 2^256-1
}
If not properly checked and prevented, arithmetic over/underflows can lead to serious security vulnerabilities.
3. Gas Limit and Loops
In Ethereum, loops consume gas. If a loop runs for a long time, it could exceed the block gas limit and fail.
for (uint i=0; i<array.length; i++) {
// Some computation
}
This can become a security issue if your contract logic depends on the loop to execute completely.
Securing Your Blockchain
Now that we've looked at some of the common EVM vulnerabilities, let's discuss strategies to secure your blockchain.
1. Use the Checks-Effects-Interactions Pattern
In the re-entrancy attack, the issue arises from interacting with another contract before finalizing the state of the current contract. The Checks-Effects-Interactions pattern suggests you should make any state changes before calling other contracts.
function withdraw() public {
uint amount = balances[msg.sender];
balances[msg.sender] = 0;
(bool success, ) = msg.sender.call.value(amount)("");
require(success);
}
2. Prevent Arithmetic Over/Underflows
SafeMath library, which comes with OpenZeppelin, is a great tool to prevent arithmetic overflows and underflows in your contracts. It contains methods for all arithmetic operations that throw an error whenever an operation overflows or underflows.
3. Be Careful with Loops
For loops, try to limit their size or use patterns that don't require loops, like the withdrawal pattern. Instead of iterating over an array or mapping, let users or contracts interact with your contract individually.
Conclusion
Blockchain security is a critical aspect that every developer must take into account. By understanding common EVM attacks and how to prevent them, we can build safer, more reliable dApps. Always remember - the code you write is responsible for real value, so it's worth taking the extra steps to ensure its security.
Stay tuned for more insights into the world of blockchain!