Strengthening Solidity Invariant Generation: From Post- to Pre-Deployment

Kaushik, Kartik, Raju Halder, Samrat MondalarXiv 2024 · DOI · arXiv

TL;DR

This paper introduces InvSol, a novel framework for generating security invariants for Solidity smart contracts before they are deployed. Unlike existing tools that rely on past transaction data, InvSol analyzes code directly, offering better coverage, especially for loop invariants and newly deployed contracts, and significantly improving vulnerability detection.

What Problem Does This Paper Address?

Smart contracts, the backbone of decentralized applications on blockchains like Ethereum, are immutable once deployed. This means any bugs or security vulnerabilities can have catastrophic and irreversible consequences, often leading to significant financial losses. Ensuring the correctness and security of these contracts before they go live is paramount. A key technique for this is generating "invariants" – properties that should always hold true during a contract's execution. However, existing tools for generating these invariants primarily rely on analyzing historical transaction data from already deployed contracts. This approach has several limitations: it's ineffective for new or rarely used contracts lacking sufficient history, and it's inherently reactive, meaning vulnerabilities are only detected after deployment, when it might be too late to fix them due to immutability.

Methods

InvSol tackles this problem with a multi-stage approach:

  1. Transcompilation: Solidity smart contract code is translated into semantically equivalent Java code. This allows leveraging existing Java analysis tools.
  2. Post-Processing & Annotation: The generated Java code is augmented with custom classes to simulate blockchain-specific features (like msg.sender, gas, etc.) and then annotated with Java Modeling Language (JML) specifications. This step is crucial for enabling detailed invariant analysis.
  3. Test Case Generation: The EvoSuite test case generator is adapted to create comprehensive test suites for the annotated Java code. This process is enhanced to consider blockchain-specific aspects like gas consumption and state mutability.
  4. Invariant Generation:
    • Dynamic Invariant Explorer: Uses Daikon, a well-known dynamic invariant detection tool, enhanced with custom templates for Solidity constructs (e.g., GasUsage, StateAfterTxn, AccessCheck).
  5. GIN-DYN: A specialized module specifically designed to detect loop invariants, a capability lacking in previous tools.
  6. Static Code Auditing: A static verifier checks the candidate invariants generated in the previous step to ensure their correctness and reliability across all possible program states. If verification fails, the test generation process is iterated.

InvSol also introduces several novel invariant templates to address specific vulnerabilities like reentrancy, dataflow issues, gas control, and access control.

Datasets

InvSol was evaluated on several benchmark datasets:

  • Ethereum ETL Dataset (from Google BigQuery): A large collection of 493 smart contracts, categorized by type (ERC20, ERC721, etc.) and size, used to assess general invariant detection and loop invariant capabilities.
  • SmartBugs Curated Dataset: 143 vulnerable smart contracts, used to evaluate InvSol's effectiveness in detecting and mitigating known vulnerabilities.
  • Trace2Inv Benchmark Dataset: 42 smart contracts previously exploited in attacks, used for direct comparison with the Trace2Inv tool.
  • Newly Deployed Contracts: A subset of 37 contracts from the Ethereum ETL dataset with limited transaction history, specifically to test performance in scenarios with scarce data.

Key Insights and Ideas

  • Pre-deployment analysis is crucial: The immutability of blockchains makes post-deployment analysis of smart contracts inherently risky. InvSol's pre-deployment approach is a significant step towards proactive security.
  • Loop invariants are critical and underserved: Loops are fundamental to programming, and their incorrect implementation in smart contracts can lead to severe issues (e.g., draining a treasury). InvSol's ability to detect loop invariants is a key differentiator.
  • Transaction history is not always necessary: InvSol demonstrates that robust invariant generation can be achieved without relying on historical transaction data, making it effective for new or less-used contracts.
  • Custom templates enhance specificity: By creating specialized invariant templates for Solidity and blockchain features, InvSol can detect more nuanced vulnerabilities than generic tools.

Key Results

  • Superior Performance: InvSol significantly outperforms existing tools like InvCon, InvCon+, and Trace2Inv in terms of precision and recall for invariant detection.
  • Loop Invariant Proficiency: InvSol achieves high precision (93-100%) in detecting loop invariants across simple, nested, and dynamic loop structures.
  • Robustness with Limited History: InvSol maintains high recall scores (77-83%) even with minimal or no transaction history, outperforming tools that depend on it.
  • Vulnerability Detection: InvSol shows superior performance in detecting various vulnerabilities, particularly arithmetic errors and unchecked calls, compared to its predecessors. It also demonstrates better protection against access control vulnerabilities than Trace2Inv.
  • Time Efficiency: While invariant generation time increases with code complexity, InvSol's overall execution time remains manageable, with the Test Crafter module being the most time-consuming part.

Limitations and Future Work

  • Time Lock, Oracle Slippage, Money Flow: While InvSol's templates show promise, they are noted to fall short in detecting crucial invariants for these specific vulnerability types in some smart contracts.
  • Front Running Vulnerabilities: InvSol attempts to simulate multiple sequential transactions to address front-running, but this functionality requires further refinement for optimal performance.
  • Future Work: The authors plan to extend InvSol to support other smart contract languages.

Summary updated

Abstract

Invariants are essential for ensuring the security and correctness of Solidity smart contracts, particularly in the context of blockchain's immutability and decentralized execution. This paper introduces InvSol, a novel framework for pre-deployment invariant generation tailored specifically for Solidity smart contracts. Unlike existing solutions, namely InvCon, InvCon+, and Trace2Inv, that rely on post-deployment transaction histories on Ethereum mainnet, InvSol identifies invariants before deployment and offers comprehensive coverage of Solidity language constructs, including loops. Additionally, InvSol incorporates custom templates to effectively prevent critical issues such as reentrancy, out-of-gas errors, and exceptions during invariant generation. We rigorously evaluate InvSol using a benchmark set of smart contracts and compare its performance with state-of-the-art solutions. Our findings reveal that InvSol significantly outperforms these tools, demonstrating its effectiveness in handling new contracts with limited transaction histories. Notably, InvSol achieves a 15% improvement in identifying common vulnerabilities compared to InvCon+ and is able to address certain crucial vulnerabilities using specific invariant templates, better than Trace2Inv.
BibTeX
@misc{kartik2024strengthening,
  author = {Kaushik, Kartik and Raju Halder and Samrat Mondal},
  title = {{Strengthening Solidity Invariant Generation: From Post- to Pre-Deployment}},
  year = {2024},
  eprint = {2409.01804},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2409.01804},
}
Figure