Select Page

Smart contracts are the most popular buzzword you would come across in discussions about blockchain and web3. The arrival of Ethereum introduced smart contract programmability in blockchain networks, thereby paving the path for the creation of dApps. However, smart contracts are just like any other program with code. Therefore, you can find vulnerabilities in smart contracts due to errors in code. In addition, smart contracts are used in dApps for automating financial transactions without involving third parties. The value of transactions managed by smart contracts is an appealing factor for attackers to compromise smart contract security. One small smart contract vulnerability could lead to losses amounting to millions of dollars. As the popularity of blockchain and smart contracts increases, users would need the assurance of security in dApps. However, vulnerabilities such as arithmetic underflow and overflow could have a detrimental impact on smart contract security.

Smart contracts present another vulnerability in the form of their presence on public blockchains. The code of smart contracts is transparent to anyone on the blockchain network. On top of it, every device in the network includes a copy of the updated version of the smart contracts. The underflow and overflow vulnerabilities in smart contracts emerge due to improper management of mathematical operations. Arithmetic underflow and overflow are common attack vectors for smart contracts that arise from improper specifications for integer types. Let us learn more about underflow and overflow vulnerabilities and how they affect smart contracts.

Excited to learn about the critical vulnerabilities and security risks in smart contract development, Enroll now in the Smart Contracts Security Course

The Concept of Underflow and Overflow

Before you find the answers to “What is overflow and underflow attacks on smart contracts?” you should understand the terms. You should learn about the data storage, processing, and modification mechanisms on computers for understanding overflow and underflow. In the domain of computing, you would find numbers in the binary form, in the series of 0s and 1s.

The size of numbers in most of the computing systems is fixed. For example, 32-bit integers could store values ranging from -2,147,483,648 to 2,147,483,647. When the processing leads to an output with a number outside the range, you will encounter issues of underflow or overflow. 

The integer overflow attack smart contract vulnerabilities happen when the result of a calculation is larger than the maximum storage limit of the allocated space. For instance, adding 1 to the maximum value for a 32-bit integer, then it would lead to overflow. As a result, the number would round up to the minimum value of the specific integer type.

In the case of underflow, the calculation could generate a number smaller than the minimum value for the allocated space. For instance, subtraction of 1 from minimum limit on the value of a 32-bit integer would lead to underflow. The result of underflow leads the number to wrap around to maximum value of a specific integer type.

Build your identity as a certified blockchain expert with 101 Blockchains’ Blockchain Certifications designed to provide enhanced career prospects.

What is Overflow Attack in Smart Contracts?

The definition of overflow and underflow concepts in computing provides a fundamental impression of their impact on execution of programs. Smart contracts are just like any other computer program and store data in binary format. The explanation for smart contracts vulnerabilities like underflow and overflow bears a resemblance to traditional computing concepts.

Let us assume that a smart contract works on 256-bit unsigned integers or uint256. The maximum value that can be allotted to the integer on smart contracts is 2256-1. While it is a significantly large value, the smart contract could also have transactions where the value is outside the specified range. 

Arithmetic overflow happens in situations where the result of a specific mathematical operation is larger than the maximum value it could store. In the case of uint256 data type, you can expect the smart contract overflow vulnerability when the contract executes code that leads to a value bigger than 2256-1.

Prior to the Solidity 8.0 compiler version, executions that generated numbers which are beyond the specified range in the data type of the function would not throw exceptions. The result of overflow is wrap-around, which happens when increasing the biggest possible integer leads to continuing from smallest possible integer value. 

Let us assume an example of a smart contract that stores balance by using uint8 values. Upon executing a function with input that increases the balance beyond the maximum value, i.e., 255, the generated number would wrap around. The balance would change to the next lowest possible value, i.e., 0 in Solidity smart contracts prior to the 8.0 version. 

One of the most noticeable examples of integer overflow attack smart contract vulnerabilities is the Beauty Chain attack of 2018. The hacker passed a random large number into a function that was responsible for calculating the withdrawal amount of the smart contract. It led to an integer overflow, and the hacker was able to overcome the barriers of verification that could have prevented withdrawal of a larger token amount than the balance.

Start learning Smart Contracts and its development tools with World’s first Smart Contracts Skill Path with quality resources tailored by industry experts Now!

What is Underflow Attack in Smart Contracts?

As the name implies, the smart contract underflow attack is the exact opposite of overflow attacks, albeit with similar patterns. The underflow attack happens when a transaction execution generates a value that is lower than the specified limit for the concerned data type. Underflow leads the calculation to wrap around, and it would begin from the next biggest value possible. You can assume the simple example of a contract for changing the balance. When you call the decrease function after the balance rounds up to zero, it will cause the contract function to generate the maximum value of 255 as the outcome.

One of the notable examples of underflow smart contract vulnerability is the Proof of Weak Hands hack in 2018. It led to a loss of 866 ETH and showed a prominent example of the outcomes of ignoring safeguards for arithmetic underflow. The hacker implemented an underflow attack on the token balance smart contract during the transfer of tokens. Due to the attack, the account had the maximum amount of tokens, which allowed the attacker to siphon away a large amount from the smart contract.

Curious to understand the complete smart contract development lifecycle? Enroll now in the Smart Contracts Development Course

What are the Implications of Overflow and Underflow Attacks?

Another important aspect of an introduction to vulnerabilities in smart contracts points to their impact. The implications of underflow and overflow vulnerabilities in smart contracts could help you understand the urgency of addressing these issues. Let us look at the effect of overflow and underflow attacks in smart contracts individually.

The impact of overflow leads to financial loss, instability of the contract, and exploitation. Overflow attacks in smart contracts can lead to discrepancies in financial applications, which could lead to loss of funds. In addition, it can also destabilize the intended function of the smart contract, and it would behave in an unpredictable manner. 

Subsequently, users are likely to lose their trust in the smart contract. Another common implication of overflow attacks is the ability of hackers to exploit overflow vulnerabilities to perform unauthorized actions in the contract. Hackers generally use overflow vulnerability to manipulate the balances and withdraw larger amounts from the contract. 

The responses to “What is overflow and underflow attacks on smart contracts?” also shed light on the impact of underflow. Underflow vulnerabilities could lead to incorrect results and loss of data. Underflow could lead to incorrect calculations that could modify the contract logic and desired outcomes. In addition, unexpected behavior due to underflow vulnerability could lead to issues in data management and loss of funds. Just like overflow, underflow could also help in manipulating balances of smart contracts or triggering unauthorized actions.

Want to understand the importance of smart contracts audits? Check out Smart Contract Audit Presentation

What are the Challenges for Detecting Arithmetic Overflow?

The most crucial concern about arithmetic overflow in smart contracts is the process of detecting the vulnerability. As one of the notable smart contracts vulnerabilities, overflow could also present some noticeable challenges for detecting the vulnerability. One of the biggest challenges for detecting overflow in smart contracts is the lack of indications for integer overflow.

You can find such indications in different programming languages. However, EVM does not support such functionalities. Therefore, you can only identify the vulnerability after an overflow attack has been implemented. Repeating the transaction execution process would help in identifying possibilities of an overflow.

The smart contract overflow vulnerability is also visible in the case of smart contracts, which involve multiplication and exponent operations. On the other hand, you should also remember to avoid false positives. In the case of certain compilers, you can find overflow conditions for running some functions. As a result, it is difficult to determine whether the smart contract has an actual error or an intentional situation.

Another challenge for detecting the overflow vulnerability is the lack of any types on the byte code level. You can find declarations for the data types of signed and unsigned integers only in high-level programming languages. The lack of Solidity source code for smart contracts could create difficulties in determining the data type of the integers.

Which Tools Can Help in Scanning Overflow and Underflow Vulnerability?

The review of arithmetic vulnerabilities in smart contract and their impact on smart contract functionality show that developers should identify them before they cause any major damage. You can find a broad range of tools for scanning underflow and overflow vulnerabilities. Some of the popular tools include Mythril, Securify, and Slither. Mythril is a popular open-source tool that can help in detecting different types of vulnerabilities for smart contracts.

The platform can be integrated with renowned development environments, and you can also use it directly as a command-line tool. Slither is also a popular open-source tool for detecting underflow and overflow vulnerabilities in smart contracts created with Solidity. You can also access Slither directly through the command line. On top of it, Securify also serves as an ideal option for detecting Solidity contract vulnerabilities through a web-based interface.

Want to get an in-depth understanding of Solidity concepts? Enroll now in Solidity Fundamentals Course

How Can You Mitigate Underflow and Overflow Vulnerabilities?

Once you have detected the underflow or overflow vulnerabilities, you can only think about the effective measures for incident response. On the other hand, implementing safeguards against underflow and overflow vulnerabilities can help in avoiding their negative impact. Here are some of the proven strategies for mitigation of overflow and underflow vulnerabilities. 

The SafeMath library can help in performing arithmetic operations that can help in avoiding integer underflow and overflow vulnerabilities. It is a reliable tool to avoid integer overflow attack smart contract security issues alongside underflow problems. OpenZeppelin offers the library in its smart contract development repository.

The repo includes contracts that you can import to the smart contract code, and the SafeMath library is one of the contracts. After Solidity has introduced the compiler version 8.0, it has introduced in-built checks for integer overflow and underflow. It can help in checking for overflow and underflow vulnerability while using SafeMath and Solidity. On the other hand, compilers that are less than 8.0 would require the library to detect underflow or overflow.

  • Data Validation and Verification

Another important recommendation to fight against the smart contract underflow attack or overflow attacks points to comprehensive validation. You should also check the variables and inputs associated with arithmetic operations. In addition, you should also ensure the validity of input values and compliance with contract requirements.

The next effective recommendation for fighting against overflow and underflow vulnerabilities is bounds checking. It helps in ensuring that arithmetic operations do not go beyond the predefined bounds or limits. You should check input values to verify that they are in the acceptable range before using the calculations.

Start your journey to becoming an expert in Web3 security with the guidance of industry experts with Web3 Security Expert Career Path

Conclusion 

The review of the underflow and overflow vulnerabilities in smart contracts provides prominent insights into their impact. Underflow and overflow vulnerabilities could help hackers siphon assets away from smart contracts without making any deposits. On top of it, underflow and overflow attacks in smart contracts could trigger unexpected behavior from smart contracts.

Arithmetic underflow and overflow attacks are the result of the output of specific calculations by smart contracts exceeding the specified value for the concerned data type. Interestingly, you can fight against these vulnerabilities by using Solidity compiler version 8.0 and more. In addition, testing and auditing of smart contract code before deployment can also help in avoiding the concerns of underflow and overflow.

Unlock your career with 101 Blockchains' Learning Programs

Share it on social networks