Using The SEI CERT Secure Cod- Ing Standard To Reduce Vulnera- Bilities

Transcription

Linköping University Department of Computer and Information ScienceMaster’s thesis, 30 ECTS Datateknik2021 LIU-IDA/LITH-EX-A--21/018--SEUsing the SEI CERT Secure Coding Standard to Reduce VulnerabilitiesJohan FischCarl HaglundSupervisors : Senyang Huang, Rahul Hiran, Ioannis AvgouleasExaminer : Andrei GurtovLinköpings universitetSE–581 83 Linköping 46 13 28 10 00 , www.liu.se

UpphovsrättDetta dokument hålls tillgängligt på Internet - eller dess framtida ersättare - under 25 år från publiceringsdatum under förutsättning att inga extraordinära omständigheter uppstår.Tillgång till dokumentet innebär tillstånd för var och en att läsa, ladda ner, skriva ut enstaka kopior för enskilt bruk och att använda det oförändrat för ickekommersiell forskning och för undervisning. Överföring av upphovsrätten vid en senare tidpunkt kan inte upphäva detta tillstånd. All annananvändning av dokumentet kräver upphovsmannens medgivande. För att garantera äktheten, säkerheten och tillgängligheten finns lösningar av teknisk och administrativ art.Upphovsmannens ideella rätt innefattar rätt att bli nämnd som upphovsman i den omfattning somgod sed kräver vid användning av dokumentet på ovan beskrivna sätt samt skydd mot att dokumentetändras eller presenteras i sådan form eller i sådant sammanhang som är kränkande för upphovsmannens litterära eller konstnärliga anseende eller egenart.För ytterligare information om Linköping University Electronic Press se förlagets hemsidahttp://www.ep.liu.se/.CopyrightThe publishers will keep this document online on the Internet - or its possible replacement - for aperiod of 25 years starting from the date of publication barring exceptional circumstances.The online availability of the document implies permanent permission for anyone to read, to download, or to print out single copies for his/hers own use and to use it unchanged for non-commercialresearch and educational purpose. Subsequent transfers of copyright cannot revoke this permission.All other uses of the document are conditional upon the consent of the copyright owner. The publisherhas taken technical and administrative measures to assure authenticity, security and accessibility.According to intellectual property law the author has the right to be mentioned when his/her workis accessed as described above and to be protected against infringement.For additional information about the Linköping University Electronic Press and its proceduresfor publication and for assurance of document integrity, please refer to its www home page:http://www.ep.liu.se/. Johan FischCarl Haglund

AbstractSecurity is a critical part of every software developed today and it will be even moreimportant going forward when more devices are getting connected to the internet. By striving to improve the quality of the code, in particular the security aspects, there might be areduction in the number of vulnerabilities and improvements of the software developed.By looking at issues from past problems and studying the code in question to see whetherit follows the SEI CERT secure coding standards, it is possible to tell if compliance to thisstandard would be helpful to reduce future problems. In this thesis an analysis of vulnerabilities, written in C and C , reported in Common Vulnerabilities and Exposures (CVE),will be done to verify whether applying the SEI CERT secure coding standard will helpreduce vulnerabilities. This study also evaluates the SEI CERT rule coverage of three different static analysis tools, Rosecheckers, PVS-Studio and CodeChecker by executing themon these vulnerabilities. By using three different metrics, true positive, false negative andthe run time. The results of the study are promising since it shows that compliance to theSEI CERT standard does indeed reduce vulnerabilities. Of the analyzed vulnerabilities itwas found that about 60% of these could have been avoided, if the standard had been followed. The results of the tools were of great interest as well, it showed that the tools did notperform as well as the manual analysis, however, all of them found some SEI CERT rule violations in different areas. Conclusively, a combination of manual analysis and these threestatic analysis tools would have resulted in the highest number of vulnerabilities avoided.

AcknowledgmentsWe would like to thank Ericsson and their employees that have been involved in our work.A special thanks goes out to Rahul Hiran, our supervisor at Ericsson. Without his interestingideas and help throughout the whole process, the results of the thesis would not have beenthe same. We would also like to thank the developers of the tool CodeChecker at Ericsson,especially Daniel Krupp who took the time to have a meeting with us and explain more aboutthe tool. Appreciation also goes out to Linköping University. We would like to thank oursupervisors Senyang Huang and Ioannis Avgouleas as well as our examiner Andrei Gurtovwho have assisted us with the thesis writing and provided us with interesting and valuablethoughts about the area.iv

ContentsAbstractiiiAcknowledgmentsivContentsvList of FiguresviiList of TablesixListings12345Introduction1.1 Motivation . . . . .1.2 Aim . . . . . . . . .1.3 Research questions1.4 Delimitations . . .x.12222Theory2.1 Secure software development . .2.2 CVE . . . . . . . . . . . . . . . . .2.3 SEI CERT Coding Standard . . .2.4 SEI CERT C Coding Standard . .2.5 SEI CERT C Coding Standard2.6 CVSS . . . . . . . . . . . . . . . .2.7 Static Analysis Tools . . . . . . .2.8 Programming languages . . . . .4455511111113Related Work3.1 Secure coding . . . . . . . . . . .3.2 Benefits of coding standards . . .3.3 Evaluation of static analysis tools3.4 Collection of vulnerabilities . . .1515161718Method4.1 Approach . . . . . . . . . . . . . . . . . . . .4.2 Gathering of vulnerabilities in CVE . . . . .4.3 Analyzing vulnerabilities in CVE . . . . . .4.4 Gathering rule specific CVE vulnerabilities4.5 Analyzing rule specific CVE vulnerabilities4.6 Case studies . . . . . . . . . . . . . . . . . .20202021252626Results5.1 Gathering of vulnerabilities in CVE . . . . . . . . . . . . . . . . . . . . . . . . . .2828.v

5.2Analyzing vulnerabilities in CVE . . . . . . . . . . . . . . . . . . . . . . . . . . .286Discussion6.1 Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.2 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.3 The work in a wider context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .414144477Conclusion7.1 How can vulnerabilities be reduced in the early phase of software development?7.2 To what extent does SEI CERT compliance help reduce vulnerabilities? . . . . .7.3 What tools can help complying with the SEI CERT secure coding standard? . .7.4 Future work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4848484949Bibliography50A Script for gathering EXP34-C CVE vulnerabilities.54B Script to gather C CVE:s55C C CVE:s56D C CVE:s58E Rule Specific CVE:s60F Rule Specific figuresF.1 ARR30-C . . . .F.2 EXP33-C . . . .F.3 EXP34-C . . . .F.4 FIO47-C . . . .F.5 INT30-C . . . .F.6 INT32-C . . . .F.7 INT33-C . . . .F.8 MEM30-C . . .F.9 MEM35-C . . .F.10 STR31-C . . . .vi.6363656667686969707172

List of Figures2.1Abstract syntax tree generated for the code in Listing 2.9 . . . . . . . . . . . . . . .134.14.24.34.44.5Description of a CVE vulnerability. . . . . . . . . . . . . . . . . . . . . . .Example of PVS-Studio output. . . . . . . . . . . . . . . . . . . . . . . . .Example of Rosecheckers output. . . . . . . . . . . . . . . . . . . . . . . .Rules that Rosecheckers covers for memory management [rose source].Example of CodeChecker HTML output. . . . . . . . . . . . . . . . . . 5.135.145.155.165.175.185.195.20SEI CERT C Rule vs. No Rule distribution for the 60 CVE:s analyzed. . . . . . . . .SEI CERT C Rule distribution for the 38 CVE:s where a rule could be found. . . . .Risk level distribution for the 16 different rules found during C analysis. . . . . . .Number of SEI CERT C violations found per rule during C analysis. . . . . . . . .Percentages of violations found per rule during C analysis. . . . . . . . . . . . . . .Violations found in relation to size during C analysis. . . . . . . . . . . . . . . . . .SEI CERT C Rule vs. No Rule distribution for the 60 CVE:s analyzed. . . . . . .SEI CERT C Rule distribution for the 37 CVE:s where a rule could be found. . .Risk level distribution for the 12 different rules found during C analysis. . . . .Number of SEI CERT C violations found per rule during C analysis. . . . . .Percentages of violations found per rule during C analysis. . . . . . . . . . . . .Static analysis tools Run time comparison. . . . . . . . . . . . . . . . . . . . . . . . .PVS & Rosecheckers Run time in relation to project size. . . . . . . . . . . . . . . .PVS & Rosecheckers Run time in relation to number of files. . . . . . . . . . . . . .CodeChecker Run time in relation to project size. . . . . . . . . . . . . . . . . . . . .CodeChecker Run time in relation to number of files. . . . . . . . . . . . . . . . . .Rule specific violations found per static analysis tool. . . . . . . . . . . . . . . . . .Rule specific project size in relation to found violations per static analysis tool. . .Rule specific project run time in relation to size per static analysis tool. . . . . . . .Rule specific number of violations found in relation to CVSS per static analysis 2F.3F.4F.5F.6F.7F.8F.9F.10F.11F.12F.13F.14ARR30-C Size related to run time. . . . . . . . . . . . . .ARR30-C Size related to number of found violations. .ARR30-C CVSS related to number of found violations.EXP33-C Size related to run time. . . . . . . . . . . . . .EXP33-C Size related to number of found violations. . .EXP33-C CVSS related to number of found violations. .EXP34-C Size related to run time. . . . . . . . . . . . . .EXP34-C Size related to number of found violations. . .EXP34-C CVSS related to number of found violations. .FIO47-C Size related to run time. . . . . . . . . . . . . .FIO47-C Size related to number of found violations. . .FIO47-C CVSS related to number of found violations. .INT30-C Size related to run time. . . . . . . . . . . . . .INT30-C Size related to number of found violations. . .6364646565656666666767676868vii.

27F.28INT30-C CVSS related to number of found violations. .INT32-C Size related to run time. . . . . . . . . . . . . .INT33-C Size related to run time. . . . . . . . . . . . . .INT33-C Size related to number of found violations. . .INT33-C CVSS related to number of found violations. .MEM30-C Size related to run time. . . . . . . . . . . . .MEM30-C Size related to number of found violations. .MEM30-C CVSS related to number of found violations.MEM35-C Size related to run time. . . . . . . . . . . . .MEM35-C Size related to number of found violations. .MEM35-C CVSS related to number of found violations.STR31-C Size related to run time. . . . . . . . . . . . . .STR31-C Size related to number of found violations. . .STR31-C CVSS related to number of found violations. .viii.6869696970707071717172727273

List of Tables2.12.22.32.4Likelihood table in a risk assessment. . . . . .Severity table in a risk assessment. . . . . . .Remediation Cost table in a risk assessment.Possible levels in a risk assessment. . . . . . .77784.1Rules tested in tools analysis. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265.15.25.35.45.55.6Rules tested in C analysis. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .True positive and False negative for C analysis. . . . . . . . . . . . . . . . . .Rules tested in C analysis. . . . . . . . . . . . . . . . . . . . . . . . . . . .True positive and False negative for C analysis. . . . . . . . . . . . . . . .Found violations per tool for each Size range during Rule specific analysis.True positive and False negative for Rule specific analysis. . . . . . . . . . .293232373840C.1 CVE:s tested in C CVE analysis. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56D.1 CVE:s tested in C CVE analysis. . . . . . . . . . . . . . . . . . . . . . . . . . . . .58E.1 CVE:s tested in Rule Specific CVE analysis. . . . . . . . . . . . . . . . . . . . . . . .60ix.

Off-by-One error. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Fixed Off-by-One error. . . . . . . . . . . . . . . . . . . . . . . . . . .Accessing freed memory. . . . . . . . . . . . . . . . . . . . . . . . . .No longer accessing freed memory. . . . . . . . . . . . . . . . . . . .Format string bug. . . . . . . . . . . . . . . . . . . . . . . . . . . . .No longer contains a Format string bug. . . . . . . . . . . . . . . . .Integer overflow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Fixed Integer Overflow. . . . . . . . . . . . . . . . . . . . . . . . . .Abstract syntax tree example code. . . . . . . . . . . . . . . . . . . .Python script for extracting C vulnerabilities. . . . . . . . . . . . . .Commands for PVS-Studio analysis. . . . . . . . . . . . . . . . . . .Python script for adding PVS-Studio student license comment. . . .Command for getting the docker container. . . . . . . . . . . . . . .Example command for running Rosecheckers analysis on the rtp.cJanus-gateway project. . . . . . . . . . . . . . . . . . . . . . . . . . .4.6 Command for setting up and running CodeChecker . . . . . . . . .4.7 Git diff for CVE-2020-14033. . . . . . . . . . . . . . . . . . . . . . . .4.8 Git diff for CVE-2018-9304. . . . . . . . . . . . . . . . . . . . . . . . .4.9 Line with problematic code for CVE-2019-9113. . . . . . . . . . . . .A.1 Python script for extracting EXP34-C CVE vulnerabilities. . . . . . .B.1 Python script for extracting C vulnerabilities. . . . . . . . . . . .x. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .file in. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .the. . . . . . . . . . . . . . .88999101010132122232323242727275455

1IntroductionIn a society that is constantly moving forward and where the amount of connected devicesis increasing each day, the danger of cyber attacks is rising and the need for a defense ismore important than ever [15]. This potential danger is a significant issue, in both privateand public sectors, where involved parties need to consider different security aspects such aswhich systems to defend, the expected frequency of cyber attacks and what type of securityto invest in. As Ijaz Ahmad et al. explain in their article "Security for 5G and Beyond" [1], therise of 5G and the massive growth of connected devices that come with it have also openedup for more security threats. The emerging 6G standard, which will be introduced in about10 years and change the society we live in, also comes with challenging security threats thatneeds to be taken care of [36].One way to address the issue of software vulnerabilities and as such cyber attacks is to introduce secure coding standards into the software development and maintenance of the code[44]. Mark Grover et al. [25] conclude in their study that cyber attacks will increase over timeand that introducing secure coding is an effective countermeasure. By applying these typesof standards, the programmers will be encouraged to follow a collection of guidelines thatare established to make the software more secure. There are a great number of different standards and guidelines that can be followed depending on the type of programming languageused. For example, the SEI CERT Secure Coding Standard [27] and MISRA C [6].Static analysis tools are one way to identify the existing vulnerabilities within the code.These tools can also help with complying to a specific coding standard, since the purpose ofthe tools is to give warnings where the code is non-compliant with the standard. There aremultiple studies that have been conducted showing that static analysis tools result in bothfalse and correct warnings, called false positive and true positive. Studies like Jiang Zhenget al.’s [54], also shows that tools are a good way to observe what mistakes occur most often.They found that "possible use of NULL pointer" stood for 45.92% of all the vulnerabilities.While it can be discussed whether one static analysis tool is better than the other in regardsto violations found, it is equally important to include the run time of the tool as well as theamount of projects the tool actually can be run on. Therefore, an evaluation of different toolsis of great interest when deciding on what tool to use in a project.1

1.1. Motivation1.1MotivationDifferent coding standards have for a long time been suggested to increase the security, reliability and overall quality [27, 13]. Currently there is not much empirical evidence backingthese statements, and studies have even shown that conformance to all rules in a specificstandard may result in more introduced faults [11]. This is beyond doubt an interesting areaand more effort needs to be put into these types of questions. Therefore, the motivation andmain focus of this thesis seeks to establish how compliance to different rules of SEI CERTCoding Standards can be used to reduce the amount of vulnerabilities and as such improvethe quality of the code.1.1.1EricssonThis thesis is conducted at Ericsson, which is a leading company of Information and Communication Technology. Ericsson works with technology ranging from networks and digitalservices, to managed services and emerging business. This leads to Ericsson having to dealwith thousands of lines of code every day. As a part of this, Ericsson needs to handle all vulnerabilities related to the code. A recurring part of Ericsson’s development process is to writetrouble reports (TRs) when problems occur. Since this is taking a substantial amount of time,this is something Ericsson wants to minimize. By investigating whether using the SEI CERTstandard is beneficial, Ericsson hopes to reduce the vulnerabilities, meaning the workload inregards to TRs. Ericsson could further improve the quality of their products by writing moresecure code.1.2AimThis thesis aims to test if compliance to the SEI CERT secure coding standard can help reducevulnerabilities. This can be achieved by analyzing vulnerabilities, both manually and withstatic analysis tools, reported in Common Vulnerabilities and Exposures (CVE) [17], a publicdatabase where a significant number of vulnerabilities from different software projects arereported. A secondary aim of this thesis is to evaluate different static analysis tools in regardsto SEI CERT coverage and performance.1.3Research questionsTo achieve the aim of the thesis, the following research questions will be answered:RQ1. How can vulnerabilities be reduced in the early phase of software development?RQ2. To what extent does SEI CERT compliance help reduce vulnerabilities?RQ3. What static analysis tools can help complying with the SEI CERT secure coding standard?1.4DelimitationsWhen analyzing what tools can help complying with SEI CERT, the time it takes to run thetools on the projects is an important factor and it was decided to limit this by not includingprojects that are very large in size, such as the Linux kernel. The tools analyzed were limitedto three different static analysis tools, mainly because the selected ones were free to use andbecause they cover many SEI CERT rules. Both of these limitations were also due to the 20week time limit for the research, study and report to be finished.2

1.4. DelimitationsAnother delimitation is that this thesis only looks at the C and C SEI CERT codingstandards and not the Android, Java or Perl standards. This is primarily because it wasrequested by Ericsson, but also due to the limited time for the thesis to be completed.3

2TheoryIn this chapter, theory about Secure software development, CVE, and the SEI CERT CodingStandard will be given. After that, CVSS will be briefly presented as well as the static analysistools used.2.1Secure software developmentIn this subsection, secure software development (also referred to as secure coding interchangeably throughout this thesis) and how it can be applied to a project will be presented.The reason secure coding, at this moment, is a prevailing topic and more important than everbefore to devote to, is because of the ever-present threat of cyber attacks. As more and moresystems are getting connected to the internet people need to consider the risks and if protecting the system is cost-effective [10]. As Pawani Porambage et al. talk about in the article "TheQuest for Privacy in the Internet of Things" [37], the big increase of the Internet of Things(IoT) is a reason to why the developers need to take the privacy of their users into consideration and why they need to develop secure and trustworthy software that protects the usersin any case.Secure software development is what developers need to adhere to, to make sure thatthe software is protected from vulnerabilities. By neglecting this, it could result in loss ofimportant data, denial of services, company secrets getting leaked or damage to the system.The software development life cycle (SDLC) is a model that includes different processes ofthe life cycle of the development of a software project [8]. This model consists of six steps, therequirement phase, the architecture and design phase, the implementation phase, the testingphase, the deployment phase and the maintenance phase. Secure coding is not only appliedin the implementation phase, instead this is something that should be considered throughoutthe whole life cycle. Security requirements should start being established as early as in therequirement phase. In the architecture and design phase, risk analysis should be exercised.The implementation phase might contain risk-based security testing and static analysis, anddeployment and verification phase could for example include risk analysis and penetrationtesting.There are many ways developers can make their systems more secure against threats.Commonly used practices could for example be to follow a specific secure coding standard,4

2.2. CVEe.g. the SEI CERT Secure Coding Standard, or adhere to a special SDL method. The nextsection will introduce the CVE database and describe some of its components.2.2CVEThe Common Vulnerabilities and Exposures, or CVE, is a program and a database that waslaunched in 1999 [17]. CVE aims to gather known vulnerabilities from different softwareprojects. CVE consists of countless CVE records, these records contain, amongst other things,a CVE ID number, a description, and a section of references. The CVE records also have thevulnerability analysis and the description that often includes references to the source of thevulnerability. The reference section usually contains links, for example, a GitHub repositorylink or a report of the vulnerability on the developers product website. Because of the highusability of CVE, CVE has become the industry standard for vulnerability reports [18].There are a few other databases, such as the IBM X-Force Exchange [26] and SecurityFocus [45], that also collect and show vulnerabilities. The IBM X-Force Exchange usually has alink to a CVE record in the vulnerability record, if there is one for the vulnerability. Vulnerability records in the IBM X-Force Exchange have tags, making the searching for particularvulnerability types quite easy.2.3SEI CERT Coding StandardThe SEI CERT Secure Coding Standard includes five different components where each of themconsists of guidelines about secure coding in that specific programming language or area:1. SEI CERT C Coding Standard2. SEI CERT C Coding Standard3. SEI CERT Oracle Coding Standard for Java4. Android Secure Coding Standard5. SEI CERT Perl Coding StandardIn particular, CERT stands for Computer Emergency Response Team and is a programthat is part of the Software Engineering Institute (SEI) at Carnegie Mellon University [14,p. xxvii]. Originally the CERT program was created to help teams of experts communicateduring security emergencies, however this is no longer the sole purpose of CERT. They nowproduce analysis in different security areas as well as provide standards for secure codingpractices.Next, theory about the SEI CERT C standard will be introduced. All the sections explainedfor the C standard can be applied for the SEI CERT C standard and most of the SEI CERTC rules are included in the C standard as well. Therefore the C section will be shorter,as otherwise there would be a lot of repetition.2.4SEI CERT C Coding StandardThe CERT C Secure Coding Standard, referred to as CERT C standard at times, is developedby SEI and the goal of the standard is to make it easier to develop safe, reliable, and securesystems [27]. Compliance to the CERT C standard will make sure that the system is moresecure and reliable on a code level, but this is not always enough since there might existcritical design flaws in the system design, which is not something that SEI CERT directlyaddresses. In systems where safety is of utmost importance, the requirements are usuallystricter than that of the CERT C standard.5

2.4. SEI CERT C Coding StandardThe different rules in the SEI CERT C Secure Coding Standard consist of a few parts. Atitle to shortly describe the rule and a description that is a bit more specific and explains therequirements of the rule. There are also code examples, both non-compliant and compliantexamples. The guidelines also consist of recommendations to help guide the programmersto a more secure and reliable code. These recommendations do not need to be followed inthe same way that a rule does and a violation of a recommendation does not automaticallymean that the code is insecure or bad. To check for compliance to the SEI CERT C codingstandard it is most efficient to have a static analysis tool setup, explained in Section 2.7, but itcan also be done manually. However, manual analysis takes a lot more time than automatedtools analysis.The SEI CERT C Secure Coding Standard is meant to make project members change theway they think about secure coding in software development. By adhering to this standardthe team can create the highest form of value, and also gain knowledge that will be useful fora long time in future work.2.4.1Scope of SEI CERT CAs for now, the SEI CERT C Secure Coding Standard focuses mainly on version C11 (ISO/IEC9899:2011), but it can also be practiced in previous versions. The difference between theversions may lead to ambiguities, therefore it is important when following the standard, tolook for notations about how the standard would affect a specific version.Some of the issues that are not addressed in the CERT C standard are the coding style andrules that are seen as controversial. The reason for this is that coding style is usually subjective and it is extremely difficult to create a style guide that is in agreement with everyone.Therefore coding style is skipped completely in the CERT C standard. For a similar reason,controversial rules are skipped. Since there is no broader consensus on these controversialrules CERT have decided to not include these at all.2.4.2ValidationCompliance with the SEI CERT C Secure Coding Standard can be checked with different staticanalysis tools. The reason to use these is because of the increased complexity of a programwith thousands lines of code. The static analysis tools can not be applied to enforce all of theguidelines, since some of the rules are only meant to be descriptive, for example, "MSC41-C.Never hard code sensitive information".A static tool will in most cases not be able to tell whether a program is following a specificguideline or a set of rules. The reason for that is because it is computationally infeasible totell if a program is pursuing a specific rule or recommendation. When deciding which staticanalysis tool to use there are certain aspects that should be taken into consideration. Oneof these aspects is the completeness, which means that no false positives are reported, a

will be done to verify whether applying the SEI CERT secure coding standard will help reduce vulnerabilities. This study also evaluates the SEI CERT rule coverage of three dif-ferent static analysis tools, Rosecheckers, PVS-Studio and CodeChecker by executing them on these vulnerabilities.