Analysis of Solidity Compiler Vulnerabilities and Security Prevention Practices

Analysis of Solidity Compiler Vulnerabilities and Response Strategies

The compiler is one of the basic components of modern computer systems. It is a special type of computer program that is responsible for converting source code written in high-level programming languages, which are easy for humans to understand and write, into instruction codes that can be executed by the underlying CPU or bytecode virtual machine.

While most developers and security experts typically focus more on the security of application code, the security of the compiler itself is equally important. As a type of computer program, compilers can also have security vulnerabilities, which in certain cases may pose serious security risks. For example, when a browser compiles and parses JavaScript front-end code, vulnerabilities in the JavaScript parsing engine may allow attackers to exploit these vulnerabilities to achieve remote code execution when users visit malicious websites, ultimately gaining control over the victim's browser or even the entire operating system.

The Solidity compiler is no exception; there are security vulnerabilities across multiple different versions.

Solidity Compiler Vulnerability

The main function of the Solidity compiler is to convert the smart contract code written by developers into executable instruction code for the Ethereum Virtual Machine (EVM). These EVM instruction codes are packaged and uploaded to the Ethereum network through transactions, and are ultimately parsed and executed by the EVM.

It is important to note that vulnerabilities in the Solidity compiler are different from vulnerabilities in the EVM itself. EVM vulnerabilities refer to security issues that arise when the virtual machine executes instructions. Since attackers can upload arbitrary code to the Ethereum network, this code will ultimately run in every Ethereum P2P client program. If there are security vulnerabilities in the EVM, it could impact the entire Ethereum network, leading to network denial of service (DoS) or even allowing attackers to take control of the entire blockchain. However, due to the relatively simple design of the EVM and the infrequent updates to its core code, the likelihood of such issues occurring is low.

A Solidity compiler vulnerability refers to issues that arise when the compiler converts Solidity code into EVM code. Unlike the case where a browser compiles and runs Javascript on the user's client computer, the compilation process for Solidity only occurs on the smart contract developer's computer and is not executed on the Ethereum network. Therefore, Solidity compiler vulnerabilities do not directly affect the Ethereum network itself.

A major danger of Solidity compiler vulnerabilities is that they may result in the generated EVM code not matching the expectations of the smart contract developers. Since smart contracts on Ethereum often involve users' cryptocurrency assets, any bugs in the smart contracts caused by the compiler could lead to the loss of user assets, resulting in serious consequences.

Developers and contract auditors may focus on issues related to the logical implementation of contract code, as well as security problems at the Solidity level such as reentrancy and integer overflow. However, it is difficult to detect vulnerabilities in the Solidity compiler solely through auditing the logic of the contract source code. It is necessary to analyze in combination with specific compiler versions and specific code patterns to determine whether the smart contract is affected by compiler vulnerabilities.

Solidity Compiler Vulnerability Analysis and Response Measures

Example of Solidity Compiler Vulnerability

Here are a few real examples of Solidity compiler vulnerabilities, demonstrating their specific forms, causes, and harms.

SOL-2016-9 HighOrderByteCleanStorage

The vulnerability exists in earlier versions of the Solidity compiler (>=0.1.6 <0.4.4).

Consider the following code:

solidity contract C { uint32 a = 0x1234; uint32 b = 0; function f() public { a += 1; } function run() public view returns (uint) { return b; } }

The storage variable b has not been modified, so the run() function should return the default value 0. However, in the code generated by the vulnerable compiler version, run() actually returns 1.

It is difficult for ordinary developers to identify the issues in the above code through simple code reviews. Although this example is relatively simple and may not have particularly serious consequences, if the variable b is used for critical purposes such as permission verification or asset accounting, this inconsistency with expectations could lead to serious security risks.

The root of this problem lies in the fact that EVM uses a stack-based virtual machine, where each element in the stack is 32 bytes in size (i.e., the size of a uint256 variable). Each slot in the underlying storage is also 32 bytes in size. The Solidity language supports various data types smaller than 32 bytes, such as uint32, and the compiler needs to perform appropriate clean-up operations on the high bits of these types of variables to ensure data correctness. In the aforementioned situation, when addition causes an integer overflow, the compiler did not correctly clean up the high bits of the result, resulting in the high bit of 1 being written into the storage after the overflow, ultimately overwriting the variable b that follows variable a, causing the value of variable b to be modified to 1.

SOL-2022-4 InlineAssemblyMemorySideEffects

The vulnerability exists in compiler versions >=0.8.13 <0.8.15. Consider the following code:

solidity contract C { function f() public pure returns (uint) { assembly { mstore(0, 0x42) } uint x; assembly { x := mload(0) } return x; } }

The Solidity compiler does not simply translate the Solidity language into EVM code; it also performs in-depth control flow and data analysis to implement various compilation optimization processes aimed at reducing the size of the generated code and optimizing gas consumption during execution. Such optimization operations are common in compilers for various high-level languages, but due to the complexity of the scenarios that need to be considered, bugs or security vulnerabilities can easily arise.

The vulnerability in the above code arises from this type of optimization operation. The compiler assumes that if there is code in a certain function that modifies the data at memory offset 0, but there is no subsequent use of that data anywhere else, then the code that modifies memory 0 can be removed directly, thereby saving gas and not affecting the subsequent program logic.

This optimization strategy itself is not problematic, but in the specific implementation of the Solidity compiler code, such optimizations are only applied to individual assembly blocks. In the aforementioned PoC code, the write and access to memory 0 exist in two different assembly blocks, while the compiler only analyzes and optimizes the individual assembly block. Since there are no read operations after writing to memory 0 in the first assembly block, the compiler determines that this write instruction is redundant and removes it, which leads to a bug. In the vulnerable version, the function f( will return a value of 0, while the correct value that the above code should return is 0x42.

) SOL-2022-6 AbiReencodingHeadOverflowWithStaticArrayCleanup

The vulnerability affects compilers in the range of >= 0.5.8 < 0.8.16. Consider the following code:

solidity contract C { function f###string( calldata a[1] external pure returns )string memory( { return abi.decode)abi.encode(a(, )string([1])); } }

Under normal circumstances, the variable a returned by the above code should be "aaaa". However, in the vulnerable version, it returns an empty string "".

The cause of the vulnerability is that Solidity incorrectly performed clean up on certain data when performing abi.encode operations on arrays of calldata type, resulting in modifications to adjacent data and causing inconsistencies in the encoded and decoded data.

It is important to note that Solidity implicitly performs abi.encode on parameters when making external calls and emitting events, so the likelihood of the aforementioned vulnerability occurring is higher than one might intuitively feel.

![Analysis of Solidity Compiler Vulnerabilities and Countermeasures][0]https://img-cdn.gateio.im/webp-social/moments-c97428f89ed62d5ad8551cdb2ba30867.webp(

Security Recommendations

After analyzing the threat model of Solidity compiler vulnerabilities and reviewing historical vulnerabilities, we offer the following recommendations to developers and security personnel.

) For Developers:

  1. Use a newer version of the Solidity compiler. While new versions may also introduce new security issues, the known security issues are typically fewer than those in older versions.

  2. Improve unit test cases. Most compiler-level bugs can cause the execution results of the code to be inconsistent with expectations. These types of issues are difficult to detect through code reviews, but they can easily be exposed during the testing phase. Therefore, by increasing code coverage, such issues can be minimized.

  3. Try to avoid using inline assembly, complex operations such as ABI encoding and decoding for multi-dimensional arrays and complex structures. Avoid pursuing flashy techniques and blindly using new language features and experimental functions without a clear need. According to the analysis of historical vulnerabilities, most vulnerabilities are related to inline assembly, ABI encoders, and similar operations. Compilers are more prone to bugs when handling complex language features. On the other hand, developers may also encounter misunderstandings when using new features, leading to security issues.

to security personnel:

  1. When conducting a security audit of Solidity code, do not overlook the security risks that may be introduced by the Solidity compiler. The corresponding check item in the Smart Contract Weakness Classification ###SWC( is SWC-102: Outdated Compiler Version.

  2. In the internal SDL development process, urge the development team to upgrade the Solidity compiler version, and consider introducing automated checks for the compiler version in the CI/CD process.

  3. However, there is no need to panic excessively about compiler vulnerabilities; most compiler vulnerabilities are only triggered under specific code patterns, and not all contracts compiled with a vulnerable version of the compiler necessarily have security risks. The actual security impact needs to be assessed based on the specific project circumstances.

Practical Resources

  • Security alert articles regularly published by the Solidity team
  • The official Solidity repository regularly updates the bug list.
  • List of bugs for each version of the compiler. This can be used to automatically check the compiler version during the CI/CD process and prompt for any security vulnerabilities present in the current version. can indicate the security vulnerabilities present in the current version of the compiler.

![Analysis and Countermeasures of Solidity Compiler Vulnerabilities])https://img-cdn.gateio.im/webp-social/moments-84f5083d8748f2aab71fd92671d999a7.webp(

Summary

This article starts with the basic concepts of compilers and introduces vulnerabilities in the Solidity compiler, analyzing the security risks that may arise in actual Ethereum development environments. Finally, it provides several practical security recommendations for developers and security personnel. By understanding these vulnerabilities and taking corresponding preventive measures, we can better protect the security of smart contracts and reduce the potential risk of asset loss.

ETH-6.73%
View Original
This page may contain third-party content, which is provided for information purposes only (not representations/warranties) and should not be considered as an endorsement of its views by Gate, nor as financial or professional advice. See Disclaimer for details.
  • Reward
  • 7
  • Share
Comment
0/400
GateUser-a606bf0cvip
· 13h ago
Exploit exploiting vulnerabilities, follow me to be a core person.
View OriginalReply0
0xLuckboxvip
· 07-31 00:35
Logic flaws and stuff really hurt my head. I'm out, I'm out.
View OriginalReply0
LidoStakeAddictvip
· 07-30 09:56
Overflow, the code still needs to be modified.
View OriginalReply0
StablecoinArbitrageurvip
· 07-30 09:26
*adjusts glasses* hmm... statistically speaking, compiler risks are severely underpriced in defi tvl calculations
Reply0
BoredStakervip
· 07-30 09:24
When can we speak human language!
View OriginalReply0
ArbitrageBotvip
· 07-30 09:24
Are we getting into the pit of compilers again?
View OriginalReply0
APY追逐者vip
· 07-30 09:15
Only when hunting for gas fees did I remember the compiler vulnerability.
View OriginalReply0
Trade Crypto Anywhere Anytime
qrCode
Scan to download Gate app
Community
English
  • 简体中文
  • English
  • Tiếng Việt
  • 繁體中文
  • Español
  • Русский
  • Français (Afrique)
  • Português (Portugal)
  • Bahasa Indonesia
  • 日本語
  • بالعربية
  • Українська
  • Português (Brasil)