Formal Analysis of Reentrancy Vulnerabilities in Smart Contract Based on CPN
Abstract
:1. Introduction
- We propose a formal verification for detecting reentrancy vulnerability, introducing the concepts of data attributes, key data elements and key transitions.
- We leverage CPN Tools to hierarchically model the simplified TheDAO contract and describe smart contracts from the perspectives of data flow and control flow.
- We not only discover the logic loopholes at the Solidity language level, but also find the malicious attack behavior against custom specifications of contacts.
- We verify our results on the Remix platform.
2. Background
2.1. Ethereum and Smart Contract
2.2. Features of Smart Contracts
2.3. Colored Petri Net
- (1)
- is a finite set of places.
- (2)
- is a finite set of transitions.
- (3)
- is a set of directed arcs and .
- (4)
- is a finite set of color set types.
- (5)
- is a finite set of type variables, and for .
- (6)
- is the color function, which is the mapping from place set to color set such that .
- (7)
- is the guard function, which is the mapping such that and .
- (8)
- is an arc expression function, which is the mapping such that and , where is the place connected to the arc and represents the polymorphic set.
- (9)
- is the initial function of , which is the mapping such that , and .
- (1)
- For the transition , if , then we use to denote that is enabled at , denoted by .
- (2)
- If , a new marking can be produced through notation , denoted by . For :
2.4. The Reentrancy Attack
3. Related Work
3.1. Reentracy Vulnerability Detection
3.2. Formal Verification of Smart Contract
4. Property Specification and Model Building
4.1. Attribute Specifications
- (1)
- Investors must ensure sufficient Ether when exchanging Ether for DAO tokens, and can withdraw at any time without affecting the operation of the contract;
- (2)
- Investors can only perform one of the two operations of deposit (ether to DAO coin) or withdraw (DAO coin to Ether) at a time;
- (3)
- When investors apply for deposit and withdrawal, the two balances of investors and the public wallet of crowdfunding should be updated in real time;
- (4)
- When the DAO coin balance in the investor’s contract account is insufficient, the investor cannot apply to use the DAO coin to exchange Ether;
- (5)
- Before and after the investor conducts any transaction, the investor’s DAO coin balance and the sum of the Ether balance after conversion should be consistent.
4.2. Top-Lever Model
4.3. Attack-Free Model
- ①
- S1 [Start > S2.
- ②
- S2 [D_Judge > S3.
- ③
- S3 [Deposit > S4.
- ①
- S1 [Start > S5.
- ②
- S5 [T_Judge > S6.
- ③
- S6 [Fallback > S7.
- ④
- S7 [withdraw > S8.
4.4. Attacker Model
- P[Account]:1`6
- P[Bank]:1`0
- P[Cfd]:1`25
- P[Gas_Need]1`1
- P[Deposit_To]:1`5
- P[T_Gas]:1`1
- P[T_Take]:1`5
- ①
- S6 [Attack > S7’
- ②
- S7’ [withdraw > S8’
5. Experimental Analysis of Reentrancy Model
5.1. Non-Secure State Reachability Analysis
5.2. CPN Tools State Space Analysis
5.3. CPN Tools Status Report Analysis
5.4. CPN Tools State Diagram Analysis
5.5. Conclusions Verification Based on the Remix Platform
5.6. Comparison with State-of-the-Art Methods
- TP (True Positive): the number of actual vulnerabilities that are correctly identified by the tool.
- FP (False Positive): the number of non-existent vulnerabilities that are incorrectly identified by the tool.
- FN (False Negative): the number of actual vulnerabilities that are missed by the tool.
- TN (True Negative): the number of non-existent vulnerabilities that are correctly identified by the tool.
6. Conclusions and Future Work
Author Contributions
Funding
Data Availability Statement
Conflicts of Interest
References
- Nakamoto, S. Bitcoin: A peer-to-peer electronic cash system. Decentralized Bus. Rev. 2018, 1, 21260. [Google Scholar]
- Crosby, M.; Pattanayak, P.; Verma, S.; Kalyanaraman, V. Blockchain technology: Beyond bitcoin. Appl. Innov. 2016, 2, 71. [Google Scholar]
- Buterin, V. A next-generation smart contract and decentralized application platform. White Pap. 2014, 3, 2–11. [Google Scholar]
- Blass, E.O.; Kerschbaum, F. Strain: A secure auction for blockchains. In European Symposium on Research in Computer Security; Springer: Cham, Switzerland, 2018; pp. 87–110. [Google Scholar]
- Zou, W.; Lo, D.; Kochhar, P.S.; Dinh Le, X.-B.; Xia, X.; Feng, Y.; Chen, Z.; Xu, B. Smart contract development: Challenges and opportunities. IEEE Trans. Softw. Eng. 2019, 47, 2084–2106. [Google Scholar] [CrossRef]
- Wang, S.; Ouyang, L.; Yuan, Y.; Ni, X.; Han, X.; Wang, F.-Y. Blockchain-enabled smart contracts: Architecture, applications, and future trends. IEEE Trans. Syst. Man Cybern. Syst. 2019, 49, 2266–2277. [Google Scholar] [CrossRef]
- Szabo, N. Formalizing and securing relationships on public networks. First Monday 1997, 2, 9. [Google Scholar] [CrossRef]
- Szabo, N. Smart contracts: Building blocks for digital markets. EXTROPY J. Transhumanist Thought 1996, 18, 28. [Google Scholar]
- Finding the greedy, prodigal, and suicidal contracts at scale. In Proceedings of the 34th Annual Computer Security Applications Conference, San Juan, PR, USA, 3–7 December 2018; Volume 1, pp. 653–663.
- Mehar, M.I.; Shier, C.L.; Giambattista, A.; Gong, E.; Fletcher, G.; Sanayhie, R.; Kim, H.M.; Laskowski, M. Understanding a revolutionary and flawed grand experiment in blockchain: The DAO attack. J. Cases Inf. Technol. 2019, 21, 19–32. [Google Scholar] [CrossRef]
- Usman, T.A.; Selçuk, A.A.; Özarslan, S. An Analysis of Ethereum Smart Contract Vulnerabilities. In Proceedings of the 2021 International Conference on Information Security and Cryptology (ISCTURKEY), Ankara, Turkey, 2–3 December 2021; pp. 99–104. [Google Scholar]
- Wang, Y.; Gou, G.; Liu, C.; Cui, M.; Li, Z.; Xiong, G. Survey of security supervision on blockchain from the perspective of technology. J. Inf. Secur. Appl. 2021, 60, 102859–102871. [Google Scholar] [CrossRef]
- Ferreira Torres, C.; Iannillo, A.K.; Gervais, A. The eye of horus: Spotting and analyzing attacks on ethereum smart contracts. In International Conference on Financial Cryptography and Data Security; Springer: Berlin/Heidelberg, Germany, 2021; pp. 33–52. [Google Scholar]
- Liu, Z.; Qian, P.; Wang, X.; Zhuang, Y.; Qiu, L.; Wang, X. Combining graph neural networks with expert knowledge for smart contract vulnerability detection. IEEE Trans. Knowl. Data Eng. 2021, 35, 1296–1310. [Google Scholar] [CrossRef]
- Gehlot, V.; Nigro, C. An introduction to systems modeling and simulation with colored petri nets. In Proceedings of the 2010 Winter Simulation Conference, Baltimore, MD, USA, 5–8 December 2010; pp. 104–118. [Google Scholar]
- Tolmach, P.; Li, Y.; Lin, S.W.; Liu, Y.; Li, Z. A survey of smart contract formal specification and verification. ACM Comput. Surv. 2021, 54, 1–38. [Google Scholar] [CrossRef]
- Pierro, G.A.; Tonelli, R. Paso: A web-based parser for solidity language analysis. In Proceedings of the 2020 IEEE International Workshop on Blockchain Oriented Software Engineering (IWBOSE), London, ON, Canada, 18 February 2020; pp. 16–21. [Google Scholar]
- Schneidewind, C.; Grishchenko, I.; Scherer, M.; Maffei, M. eThor: Practical and provably sound static analysis of ethereum smart contracts. In Proceedings of the 2020 ACM SIGSAC Conference on Computer and Communications Security, Virtual, 9–13 November 2020; pp. 621–640. [Google Scholar]
- Peterson, J.L. Petri nets. ACM Comput. Surv. 1977, 9, 223–252. [Google Scholar] [CrossRef]
- Jensen, K.; Kristensen, L.M. Colored Petri nets: A graphical language for formal modeling and validation of concurrent systems. Commun. ACM 2015, 58, 61–70. [Google Scholar] [CrossRef]
- Jensen, K.; Christensen, S.; Kristensen, L.M. CPN tools state space manual. In Department of Computer Science; University of Aarhus: Aarhus, Denmark, 2006. [Google Scholar]
- Jensen, K. Colored Petri Nets: Basic Concepts, Analysis Methods and Practical Use; Springer Science & Business Media: Berlin/Heidelberg, Germany, 1996. [Google Scholar]
- DuPont, Q. Experiments in algorithmic governance: A history and ethnography of “The DAO,” a failed decentralized autonomous organization. In Bitcoin and Beyond: Cryptocurrencies, Blockchains and Global Governance; Routledge: London, UK, 2017; pp. 157–177. [Google Scholar] [CrossRef]
- Jentzsch, C. Decentralized Autonomous Organization to Automate Governance. White Pap. 2016. Available online: https://lawofthelevel.lexblogplatformthree.com/wp-content/uploads/sites/187/2017/07/WhitePaper-1.pdf (accessed on 3 February 2023).
- Hard-Fork. 2020. Available online: https://www.investopedia.com/terms/h/hard-fork.asp (accessed on 28 January 2023).
- Liu, C.; Liu, H.; Cao, Z.; Chen, Z.; Chen, B.; Roscoe, B. Reguard: Finding reentrancy bugs in smart contracts. In Proceedings of the IEEE/ACM 40th International Conference on Software Engineering: Companion (ICSE-Companion), Gothenburg, Sweden, 27 May 2018; pp. 65–68. [Google Scholar]
- Feng, X.; Wang, Q.; Zhu, X.; Wen, S. Bug searching in smart contract. arXiv 2019, arXiv:1905.00799. [Google Scholar]
- Rodler, M.; Li, W.; Karame, G.O.; Davi, L. Sereum: Protecting existing smart contracts against re-entrancy attacks. arXiv 2018, arXiv:1812.05934. [Google Scholar]
- Yu, R.; Shu, J.; Yan, D.; Jia, X. ReDetect: Reentrancy Vulnerability Detection in Smart Contracts with High Accuracy. In Proceedings of the 17th International Conference on Mobility, Sensing and Networking (MSN), Exeter, UK, 13–15 December 2021; pp. 412–419. [Google Scholar]
- Ye, J.; Ma, M.; Lin, Y.; Xue, Y.; Sui, Y.; Peng, T. Clairvoyance: Cross-contract static analysis for detecting practical reentrancy vulnerabilities in smart contracts. In Proceedings of the IEEE/ACM 42nd International Conference on Software Engineering: Companion Proceedings (ICSE-Companion), Virtual Event, Australia, 21–25 December 2020; pp. 274–275. [Google Scholar]
- Chinen, Y.; Yanai, N.; Cruz, J.P.; Okamura, S. RA: Hunting for re-entrancy attacks in ethereum smart contracts via static analysis. In Proceedings of the IEEE International Conference on Blockchain (Blockchain), Rhodes, Greece, 2–6 November 2020; pp. 327–336. [Google Scholar]
- Samreen, N.F.; Alalfi, M.H. Reentrancy vulnerability identification in ethereum smart contracts. In Proceedings of the IEEE International Workshop on Blockchain Oriented Software Engineering (IWBOSE), London, ON, Canada, 18 February 2020; pp. 22–29. [Google Scholar]
- Qian, P.; Liu, Z.; He, Q.; Zimmermann, R.; Wang, X. Towards automated reentrancy detection for smart contracts based on sequential models. IEEE Access 2020, 8, 19685–19695. [Google Scholar] [CrossRef]
- Bai, X.; Cheng, Z.; Duan, Z.; Hu, K. Formal modeling and verification of smart contracts. In Proceedings of the 2018 7th International Conference on Software and Computer Applications, Kuantan, Malaysia, 8–10 February 2018; pp. 322–326. [Google Scholar]
- Qu, M.; Huang, X.; Chen, X.; Wang, Y.; Ma, X.; Liu, D. Formal Verification of Smart Contracts from the Perspective of Concurrency. International Conference on Smart Blockchain; Springer: Cham, Switzerland, 2018; pp. 32–43. [Google Scholar]
- Amani, S.; Bégel, M.; Bortin, M.; Staples, M. Towards verifying ethereum smart contract bytecode in Isabelle/HOL. In Proceedings of the 7th ACM SIGPLAN International Conference on Certified Programs and Proofs, Los Angeles, CA, USA, 8–9 January 2018; pp. 66–77. [Google Scholar]
- Sun, T.; Yu, W. A formal verification framework for security issues of blockchain smart contracts. Electronics 2020, 9, 255. [Google Scholar] [CrossRef]
- Kalra, S.; Goel, S.; Dhawan, M.; Sharma, S. Zeus: Analyzing safety of smart contracts. Ndss 2018, 1, 1–12. [Google Scholar] [CrossRef]
- Park, W.S.; Lee, H.; Choi, J.Y. Formal Modeling of Smart Contract-based Trading System. In Proceedings of the 2021 23rd International Conference on Advanced Communication Technology (ICACT), PyeongChang, Republic of Korea, 7–10 February 2021; pp. 48–52. [Google Scholar]
- Dharanikota, S.; Mukherjee, S.; Bhardwaj, C.; Rastogi, A.; Lal, A. Celestial: A smart contracts verification framework. Proc. 2021 Form. Methods Comput. Aided Des. 2021, 2, 133–142. [Google Scholar]
- Liu, Z.; Liu, J. Formal verification of blockchain smart contract based on colored petri net models. In Proceedings of the 2019 IEEE 43rd Annual Computer Software and Applications Conference (COMPSAC), Milwaukee, WI, USA, 15–19 July 2019; pp. 555–560. [Google Scholar]
- Duo, W.; Xin, H.; Xiaofeng, M. Formal analysis of smart contract based on colored petri nets. IEEE Intell. Syst. 2020, 35, 19–30. [Google Scholar] [CrossRef]
- Garfatta, I.; Klai, K.; Graïet, M.; Gaaloul, W. A Solidity-to-CPN Approach Towards Formal Verification of Smart Contracts. In Proceedings of the 2021 IEEE 30th International Conference on Enabling Technologies: Infrastructure for Collaborative Enterprises (WETICE), Bayonne, France, 27–29 October 2021; pp. 69–74. [Google Scholar]
- Garfatta, I.; Klai, K.; Graïet, M.; Gaaloul, W. Model checking of vulnerabilities in smart contracts: A solidity-to-CPN approach. In Proceedings of the 37th ACM/SIGAPP Symposium on Applied Computing, Virtual, 25–29 April 2022; pp. 316–325. [Google Scholar]
- Dwivedi, V.; Norta, A. A legal-relationship establishment in smart contracts: Ontological semantics for programming-language development. In Proceedings of the Advances in Computing and Data Sciences: 5th International Conference (ICACDS), Nashik, India, 23–24 April 2021; pp. 660–676. [Google Scholar]
- Dwivedi, V.; Pattanaik, V.; Deval, V.; Dixit, A.; Norta, A.; Draheim, D. Legally enforceable smart-contract languages: A systematic literature review. ACM Comput. Surv. 2021, 54, 1–34. [Google Scholar] [CrossRef]
- Jain, S.M. Introduction to Remix IDE. A Brief Introduction to Web3: Decentralized Web Fundamentals for App Development; Apress: Berkeley, CA, USA, 2022; pp. 89–126. [Google Scholar]
- Gupta, V.; Kumar, A.; Pathak, A. Empirical review of automated analysis tools on 47,587 Ethereum smart contracts. J. King Saud Univ. -Comput. Inf. Sci. 2020, 1, 273–282. [Google Scholar]
- He, Y.; Liu, G.; Xiang, D.; Sun, J.; Yan, C.; Jiang, C. Verifying the Correctness of Workflow Systems Based on Workflow Net With Data Constraints. IEEE Access 2018, 6, 11412–11423. [Google Scholar] [CrossRef]
Type | Annotation |
---|---|
P[place] | Input data flow places |
T[transition] | Input control flow transitions |
CS[colset] | Color set name |
IM[place] | Initial place value |
M[place] | Local place marking value |
ARC[node1,node2] | Bound transmission value on the arc, node1, node2 as input and output node |
Type | Annotation |
---|---|
P[Account] | Investor Account |
P[Gas_Need] | Consumed gas |
P[Deposit_To] | Plan to deposit balance |
P[deposit] | Deposit balance |
P[Old_bal0] | Investor account original balance |
P[Bank] | Investor account balance |
P[Cfd] | Crowdfunding balance |
Old_bal | Gas_Need | Deposit_To | Deposit | Old_bal0 | Account | Bank | Cfd | |
---|---|---|---|---|---|---|---|---|
Start D_Judge Deposit | Money | -Gas | -Money | Money | Money | Account | Bank | Cfd |
Oldbal1 | T_Gas | T_Take | T_Old | … | T_Old0 | T_Bank0 | T_Take0 | T_cft0 | |
---|---|---|---|---|---|---|---|---|---|
Take T_Judge Fallback withdraw | Money -Money | -Gas | -Take | Money -Money | Money -Money | Money -Money | Money -Money | Money -Money |
T_Old | T_bank | T_take | T_cft | … | T_cft0 | Account | Bank | Cfd | |
---|---|---|---|---|---|---|---|---|---|
Fallback Attack withdraw | -Money | -Money | -Money | Money | Money | Account | Bank | Cfd |
Deposit Layer | Account | Bank | Cfd |
---|---|---|---|
Before Execution | 6 | 0 | 25 |
After Execution | 1 | 5 | 30 |
WithDraw Layer | Account | Bank | Cfd |
---|---|---|---|
Before Execution | 1 | 5 | 30 |
After Execution | 6 | 0 | 25 |
Attack Model | Account | Bank | Cfd |
---|---|---|---|
Before Execution | 6 | 0 | 25 |
During Execution | 1 | 5 | 30 |
After Execution | 16 | 0 | 15 |
Tool | TP | FP | FN | TN | Accuracy | TPR | FPR |
---|---|---|---|---|---|---|---|
Oyente | 20 | 5 | 10 | 15 | 70% | 66.7% | 25% |
Mythril | 12 | 6 | 18 | 14 | 52% | 40% | 33.3% |
Slither | 10 | 10 | 12 | 18 | 56% | 31.2% | 35.7% |
CPN | 35 | 0 | 0 | 15 | 100% | 100% | 100% |
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2023 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https://creativecommons.org/licenses/by/4.0/).
Share and Cite
He, Y.; Dong, H.; Wu, H.; Duan, Q. Formal Analysis of Reentrancy Vulnerabilities in Smart Contract Based on CPN. Electronics 2023, 12, 2152. https://doi.org/10.3390/electronics12102152
He Y, Dong H, Wu H, Duan Q. Formal Analysis of Reentrancy Vulnerabilities in Smart Contract Based on CPN. Electronics. 2023; 12(10):2152. https://doi.org/10.3390/electronics12102152
Chicago/Turabian StyleHe, Yaqiong, Hanjie Dong, Huaiguang Wu, and Qianheng Duan. 2023. "Formal Analysis of Reentrancy Vulnerabilities in Smart Contract Based on CPN" Electronics 12, no. 10: 2152. https://doi.org/10.3390/electronics12102152
APA StyleHe, Y., Dong, H., Wu, H., & Duan, Q. (2023). Formal Analysis of Reentrancy Vulnerabilities in Smart Contract Based on CPN. Electronics, 12(10), 2152. https://doi.org/10.3390/electronics12102152