Metamorphic Testing for Smart Contract Vulnerabilities Detection

Jiahao Li

TL;DR

Researchers have developed a novel approach to detecting smart contract vulnerabilities by applying Metamorphic Testing (MT). By focusing on inconsistencies in gas consumption and account interactions rather than just static code patterns, their method achieved a 100% detection rate with zero false positives on a verified benchmark, significantly outperforming industry-standard tools like Slither and Mythril.

What Problem Does This Paper Address?

Smart contracts are the backbone of decentralized finance (DeFi), but they are notoriously difficult to secure. Traditional security tools generally fall into two camps: static analysis (which scans code without running it) and dynamic analysis (which tests the code during execution).

The gap lies in the "Oracle Problem." Most tools rely on predefined vulnerability patterns—essentially a checklist of "bad code." If a bug doesn't perfectly match a pattern, the tool misses it (a false negative). Conversely, if a safe piece of code looks like a pattern, the tool flags it (a false positive). This paper addresses these inaccuracies by moving away from pattern matching and toward property-based testing, looking for abnormal behavior when execution conditions change.

Methods

The authors propose using Metamorphic Testing (MT). In MT, you don't necessarily need to know the "correct" output for a single test case. Instead, you look for a Metamorphic Relation (MR)—a relationship that should hold true across multiple related inputs.

The researchers defined five specific MRs categorized into two groups:

  1. Gas Allocation (MR1.x): These relations manipulate the "gas limit" (the budget for computational effort). For a bug-free contract, increasing the gas limit beyond the required amount shouldn't change the transaction's outcome or its actual gas consumption. If it does, it suggests "extra" unexpected instructions (like a reentrancy loop) are being executed.
  2. Account Switching (MR2.x): These relations swap a simple user account (EOA) for various "Agent Contracts" with specific fallback functions (e.g., functions that consume massive gas, trigger recursive calls, or throw exceptions). A robust contract should handle these different account types consistently. If switching to a recursive "Agent" changes the balance in an unexpected way, a reentrancy vulnerability is likely present.

The framework works by dynamically executing transactions on the Goerli Testnet and comparing the outputs of these source and follow-up transactions to see if any MRs are violated.

Datasets

The approach was evaluated using a benchmark of 67 manually annotated smart contracts.

  • These contracts were originally flagged by ContractFuzzer.
  • The authors manually verified them, finding that only 38 were truly vulnerable, while 29 were false alarms.
  • The vulnerabilities covered three major categories: Reentrancy, Gasless Send, and Exception Disorder.

Key Insights and Ideas

  • Behavior over Syntax: The "aha moment" here is that vulnerabilities often manifest as environmental sensitivities. A reentrancy bug might look fine in a static scan, but it reveals itself the moment you swap a standard user for a malicious contract that tries to "call back" into the victim.
  • The Power of Inconsistency: By focusing on inconsistency (e.g., "Why did this transaction succeed with an EOA but fail with a high-gas contract account?"), the researchers can detect bugs that don't fit a standard "signature."
  • Agent Contracts as Probes: Using specialized "Agent Contracts" to mimic different malicious behaviors (recursive calls, forced exceptions) allows for a highly controlled and automated way to probe a target contract's defenses.

Key Results

The authors compared their MT-based tool against three state-of-the-art tools: ContractFuzzer, Slither, and Mythril.

  • Detection Rate (TPR): The MT approach and ContractFuzzer both achieved 100%, while Slither and Mythril missed about 21% of the vulnerabilities.
  • False Alarm Rate (FDR): This is where the MT approach shined. It reported 0% false positives. In contrast, ContractFuzzer had a staggering 43.28% false alarm rate, and Mythril had 9.09%.
  • Precision: Because the MT approach relies on actual execution and violation of logical properties, it proved much more reliable than tools relying on static patterns or simple fuzzing.

Limitations and Future Work

While the results are impressive, the paper notes some areas for growth:

  • Scope of Vulnerabilities: The current study focuses on three specific types of vulnerabilities. While the authors claim the MRs can detect a "wide range," further work is needed to define MRs for other issues like integer overflows or front-running.
  • Heuristic Gas Estimation: The tool relies on a heuristic to estimate intrinsic gas costs, which can be inaccurate if a transaction contains complex internal calls.
  • Execution Overhead: Because this is a dynamic approach requiring multiple executions on a testnet (or a local EVM), it is inherently slower than a simple static scan. Future work could involve optimizing this execution pipeline.

Summary updated

Abstract

Despite the rapid growth of smart contracts, they are suffering numerous security vulnerabilities due to the absence of reliable development and testing. In this article, we apply the metamorphic testing technique to detect smart contract vulnerabilities. Based on the anomalies we observed in vulnerable smart contracts, we define five metamorphic relations to detect abnormal gas consumption and account interaction inconsistency of the target smart contract. Through dynamically executing transactions and checking the final violation of metamorphic relations, we determine whether a smart contract is vulnerable. We evaluate our approach on a benchmark of 67 manually annotated smart contracts. The experimental results show that our approach achieves a higher detection rate (TPR, true positive rate) with a lower misreport rate (FDR, false discovery rate) than the other three state-of-the-art tools. These results further suggest that metamorphic testing is a promising method for detecting smart contract vulnerabilities.
BibTeX
@misc{li2023metamorphic,
  author = {Jiahao Li},
  title = {{Metamorphic Testing for Smart Contract Vulnerabilities Detection}},
  year = {2023},
  eprint = {2303.03179},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2303.03179},
}
Figure