SlideShare a Scribd company logo
PVS-Studio Static Analyzer as a Tool for
Protection against Zero-Day Vulnerabilities
Author: Ekaterina Nikiforova
Date: 28.11.2019
Tags: StaticAnalysis, Security
A Zero-day (0-day) vulnerability is a computer-software vulnerability introduced during the development
process and not yet discovered by the developers. Zero-day vulnerabilities can be exploited by hackers,
thus affecting the company's reputation. Developers should seek to minimize the number of defects
leading to such vulnerabilities. PVS-Studio, a static code analyzer for C, C++, C#, and Java code, is one of
the tools capable of detecting security issues.
Zero-day vulnerabilities
A Zero-day vulnerability (also known as 0-day vulnerability) is a computer-software vulnerability that is
unknown to, or unaddressed by, those who should be interested in mitigating the vulnerability
(including the vendor of the target software). Until the vulnerability is mitigated, hackers can exploit it
to adversely affect computer programs, data, additional computers or a network. The term means the
developers don't have a single day to fix the defect because no one knows about it yet. Some of the
well-known vendors and software products such as Adobe, Windows, Tor browser, and many others,
were affected by zero-day vulnerabilities in the past.
Some were lucky to have a vulnerability found and reported by people who were not going to exploit it.
The case of MacOS is one such example. In some other cases, the developers themselves produced a
patch with which, while adding new features, they also fixed a zero-day vulnerability without knowing it.
Others were less lucky though. For instance, not so long ago, Google Chrome had to urgently fix a
vulnerability that could be exploited to remotely execute arbitrary code.
The problem is you can't guarantee 100% protection against these vulnerabilities as you can't effectively
fight a threat you don't even know of. However, there are ways to make such defects less likely to occur
in your program – this will be the topic of this article, but we should take a look at some theory first.
Static analysis
Static analysis is a method of checking the source code of a software program using an analyzer without
executing the program itself and can be viewed as automated code review. Sometimes static analysis
can be much more effective than peer code review but can't completely replace it. I tried to summarize
the pros and cons of code review and static analysis relative to each other in the following table:
Code review Static analysis
Helps find not only trivial but also high-level bugs Helps find unfamiliar defects or vulnerabilities
Helps improve the program's architecture and
work out a consistent coding style
Helps find bugs not easily noticeable to the
human eye (e.g. typos)
Expensive Cheaper than code review
Takes up a lot of programmers' time. Breaks are
necessary as the reviewer's attention tends to
weaken quickly
False positives are unavoidable; the user has to
customize the analyzer
CVE and CWE
Common Vulnerabilities and Exposures (CVE) is a database of information-security vulnerabilities and
exposures. Its initial purpose was to organize known software defects into a coherent list. In the past,
most information-security tools were using their own databases and names for such defects, and it was
to bring order to that chaos and establish compatibility between different tools that the MITRE
Corporation developed CVE in 1999. However, CVE turned out to be insufficient for estimating code
security. Some other system was needed, with finer classification and more detailed descriptions. That's
how the Common Weakness Enumeration (CWE) came into existence. If a defect is listed in the CWE, it
may cause an exploitable vulnerability and get added to the CVE list as well. The Euler diagram below
shows the relations between the standards.
Some static analyzers can inform you if, for example, your project employs a library containing a
vulnerability. Knowing this, you can download a newer version of the library, with the defect fixed, to
make your code less susceptible to security threats caused by a mistake in someone else's code.
As the CVE and CWE standards were embraced by the developer community, they were also supported
by many information-security tools including static analyzers. Analyzers that support these
classifications can be viewed as SAST solutions. SAST (Static Application Security Testing) allows
developers to detect vulnerabilities in the source code of programs at the earliest stages of the software
development life cycle.
SAST is yet another practice to minimize the probability of zero-day vulnerabilities occurring in your
project. An analyzer supporting the CWE standard can tell you where a potential vulnerability is lurking
so that you could fix it to make your application more reliable and less likely to contain a 0-day threat.
There is a variety of SAST tools. I'll take the PVS-Studio analyzer as an example to show how these tools
can help fight vulnerabilities. Warnings of this analyzer are classified as CWE. Some examples are given
below.
PVS-Studio diagnostic message: CWE-561: Dead Code (V3021).
public string EncodeImage(....)
{
if (string.IsNullOrWhiteSpace(inputPath))
{
throw new ArgumentNullException("inputPath");
}
if (string.IsNullOrWhiteSpace(inputPath))
{
throw new ArgumentNullException("outputPath");
}
....
}
This code contains a typo: the conditions of both if statements check the same variable. The message
accompanying the exception suggests that the second condition should check the outputPath variable
instead. This mistake has made some part of the code unreachable.
Bugs like that might seem harmless, but this impression is wrong. Let's take a look at another trivial and
seemingly harmless bug that has to do with a duplicate goto statement.
This bug once caused a vulnerability in iOS.
The CVE-2014-1266 vulnerability: The SSLVerifySignedServerKeyExchange function in
libsecurity_ssl/lib/sslKeyExchange.c in the Secure Transport feature in the Data Security component in
Apple iOS 6.x before 6.1.6 and 7.x before 7.0.6, Apple TV 6.x before 6.0.2, and Apple OS X 10.9.x before
10.9.2 does not check the signature in a TLS Server Key Exchange message, which allows man-in-the-
middle attackers to spoof SSL servers by using an arbitrary private key for the signing step or omitting
the signing step.
static OSStatus
SSLVerifySignedServerKeyExchange(SSLContext *ctx,
bool isRsa,
SSLBuffer signedParams,
uint8_t *signature,
UInt16 signatureLen)
{
OSStatus err;
....
if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
goto fail;
if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
goto fail;
goto fail;
if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
goto fail;
....
fail:
SSLFreeBuffer(&signedHashes);
SSLFreeBuffer(&hashCtx);
return err;
}
Like in the first example, the duplicate goto here led to unreachable code: whatever the conditions of
the if statements, the second goto statement would be executed anyway. As a result, the signature
wouldn't be checked, the function would return 0, meaning the signature was OK, and the program
would receive a key from the server even if the signature check failed. This key is used to encrypt the
data being transmitted.
This trivial bug had drastic implications. The incident illustrates why there's no point speculating if this
or that CWE defect is dangerous or not – you just have to fix it for the sake of your code's safety.
By the way, PVS-Studio could have easily found this bug, reporting it with two CWE warnings at once:
• CWE-561 (V779): Dead Code
• CWE-483 (V640): Incorrect Block Delimitation
Here's another example. Long ago, in 2012, a security issue was discovered in MySQL, which could be
exploited by an attacker to enter the MySQL database. Below you will see the flawed code fragment,
where the vulnerability occurred.
The CVE-2012-2122 vulnerability: sql/password.c in Oracle MySQL 5.1.x before 5.1.63, 5.5.x before
5.5.24, and 5.6.x before 5.6.6, and MariaDB 5.1.x before 5.1.62, 5.2.x before 5.2.12, 5.3.x before 5.3.6,
and 5.5.x before 5.5.23, when running in certain environments with certain implementations of the
memcmp function, allows remote attackers to bypass authentication by repeatedly authenticating with
the same incorrect password, which eventually causes a token comparison to succeed due to an
improperly-checked return value.
typedef char my_bool;
my_bool
check_scramble(const char *scramble_arg, const char *message,
const uint8 *hash_stage2)
{
....
return memcmp(hash_stage2, hash_stage2_reassured, SHA1_HASH_SIZE);
}
The memcmp function returns a value of type int, while the check_scramble function returns a value of
type my_bool, which is in fact char. The int value gets implicitly cast to char, with the most significant
bits truncated. This caused about 1 out of 256 attempts to log in with an arbitrary password for a known
username to succeed.
Again, this CWE defect could have been neutralized and prevented from becoming a CVE vulnerability
much earlier, at the coding stage. For example, PVS-Studio reports it as CWE-197 (V642): Numeric
Truncation Error.
See the article "How Can PVS-Studio Help in the Detection of Vulnerabilities?" for further reading on the
topic.
Conclusion
You can't be 100% sure your program is safe from 0-day vulnerabilities. But you can still make them
much less likely to occur. This is done by using specialized SAST tools such as PVS-Studio. If your project
is found to contain defects classified as CWE issues, make sure to fix them. Even though few of CWE
defects end up on the CVE list, fixing them helps to secure your program from many potential threats.
References
1. Download and evaluate PVS-Studio
2. Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities
3. Classification of PVS-Studio warnings according to the Common Weakness Enumeration (CWE)
4. Classification of PVS-Studio warnings according to the SEI CERT Coding Standard

More Related Content

What's hot (20)

PDF
A Boring Article About a Check of the OpenSSL Project
Andrey Karpov
 
PDF
New Year PVS-Studio 6.00 Release: Scanning Roslyn
PVS-Studio
 
PDF
Hacking ingress
Eran Goldstein
 
PDF
Crash Analysis with Reverse Taint
marekzmyslowski
 
DOCX
TestDrivenDeveloment
Antonio Tapper
 
PDF
17726 bypassing-phpids-0.6.5
Attaporn Ninsuwan
 
PDF
Exception handling
Garuda Trainings
 
PPTX
Production Debugging at Code Camp Philly
Brian Lyttle
 
DOCX
Selenium interview-questions-freshers
Naga Mani
 
ODP
Java code coverage with JCov. Implementation details and use cases.
Alexandre (Shura) Iline
 
PDF
PVS-Studio for Visual C++
PVS-Studio
 
PPT
C# Exceptions Handling
sharqiyem
 
PDF
Selenium Automation Testing Interview Questions And Answers
Ajit Jadhav
 
PPTX
Junit and cactus
Himanshu
 
DOCX
Asynchronyin net
Soacat Blogspot
 
PDF
Popular Approaches to Preventing Code Injection Attacks are Dangerously Wrong
Waratek Ltd
 
PDF
Top trending selenium interview questions
Rock Interview
 
PPTX
Chapter 5
siragezeynu
 
PDF
How PVS-Studio does the bug search: methods and technologies
PVS-Studio
 
PDF
Effectiveness of AV in Detecting Web Application Backdoors
n|u - The Open Security Community
 
A Boring Article About a Check of the OpenSSL Project
Andrey Karpov
 
New Year PVS-Studio 6.00 Release: Scanning Roslyn
PVS-Studio
 
Hacking ingress
Eran Goldstein
 
Crash Analysis with Reverse Taint
marekzmyslowski
 
TestDrivenDeveloment
Antonio Tapper
 
17726 bypassing-phpids-0.6.5
Attaporn Ninsuwan
 
Exception handling
Garuda Trainings
 
Production Debugging at Code Camp Philly
Brian Lyttle
 
Selenium interview-questions-freshers
Naga Mani
 
Java code coverage with JCov. Implementation details and use cases.
Alexandre (Shura) Iline
 
PVS-Studio for Visual C++
PVS-Studio
 
C# Exceptions Handling
sharqiyem
 
Selenium Automation Testing Interview Questions And Answers
Ajit Jadhav
 
Junit and cactus
Himanshu
 
Asynchronyin net
Soacat Blogspot
 
Popular Approaches to Preventing Code Injection Attacks are Dangerously Wrong
Waratek Ltd
 
Top trending selenium interview questions
Rock Interview
 
Chapter 5
siragezeynu
 
How PVS-Studio does the bug search: methods and technologies
PVS-Studio
 
Effectiveness of AV in Detecting Web Application Backdoors
n|u - The Open Security Community
 

Similar to PVS-Studio Static Analyzer as a Tool for Protection against Zero-Day Vulnerabilities (20)

PDF
How Can PVS-Studio Help in the Detection of Vulnerabilities?
PVS-Studio
 
PDF
What's the Difference Between Static Analysis and Compiler Warnings?
Andrey Karpov
 
PDF
PVS-Studio advertisement - static analysis of C/C++ code
PVS-Studio
 
PDF
Static Analysis: From Getting Started to Integration
Andrey Karpov
 
PDF
We continue checking Microsoft projects: analysis of PowerShell
PVS-Studio
 
PDF
Secure coding-guidelines
Trupti Shiralkar, CISSP
 
PDF
An Ideal Way to Integrate a Static Code Analyzer into a Project
PVS-Studio
 
PDF
HPX and PVS-Studio
PVS-Studio
 
PDF
nullcon 2011 - Reversing MicroSoft patches to reveal vulnerable code
n|u - The Open Security Community
 
PDF
Generic attack detection engine
Vikrant Kansal
 
PDF
Difficulties of comparing code analyzers, or don't forget about usability
Andrey Karpov
 
PDF
Difficulties of comparing code analyzers, or don't forget about usability
PVS-Studio
 
PDF
Difficulties of comparing code analyzers, or don't forget about usability
PVS-Studio
 
PDF
VCCFinder: Finding Potential Vulnerabilities in Open-Source Projects to Assis...
Stefano Dalla Palma
 
PDF
Microsoft opened the source code of Xamarin.Forms. We couldn't miss a chance ...
PVS-Studio
 
PDF
PVS-Studio advertisement - static analysis of C/C++ code
Andrey Karpov
 
PDF
Looking for Bugs in MonoDevelop
PVS-Studio
 
PDF
Accord.Net: Looking for a Bug that Could Help Machines Conquer Humankind
PVS-Studio
 
PDF
Analysis of Godot Engine's Source Code
PVS-Studio
 
PDF
The Little Unicorn That Could
PVS-Studio
 
How Can PVS-Studio Help in the Detection of Vulnerabilities?
PVS-Studio
 
What's the Difference Between Static Analysis and Compiler Warnings?
Andrey Karpov
 
PVS-Studio advertisement - static analysis of C/C++ code
PVS-Studio
 
Static Analysis: From Getting Started to Integration
Andrey Karpov
 
We continue checking Microsoft projects: analysis of PowerShell
PVS-Studio
 
Secure coding-guidelines
Trupti Shiralkar, CISSP
 
An Ideal Way to Integrate a Static Code Analyzer into a Project
PVS-Studio
 
HPX and PVS-Studio
PVS-Studio
 
nullcon 2011 - Reversing MicroSoft patches to reveal vulnerable code
n|u - The Open Security Community
 
Generic attack detection engine
Vikrant Kansal
 
Difficulties of comparing code analyzers, or don't forget about usability
Andrey Karpov
 
Difficulties of comparing code analyzers, or don't forget about usability
PVS-Studio
 
Difficulties of comparing code analyzers, or don't forget about usability
PVS-Studio
 
VCCFinder: Finding Potential Vulnerabilities in Open-Source Projects to Assis...
Stefano Dalla Palma
 
Microsoft opened the source code of Xamarin.Forms. We couldn't miss a chance ...
PVS-Studio
 
PVS-Studio advertisement - static analysis of C/C++ code
Andrey Karpov
 
Looking for Bugs in MonoDevelop
PVS-Studio
 
Accord.Net: Looking for a Bug that Could Help Machines Conquer Humankind
PVS-Studio
 
Analysis of Godot Engine's Source Code
PVS-Studio
 
The Little Unicorn That Could
PVS-Studio
 
Ad

More from Andrey Karpov (20)

PDF
60 антипаттернов для С++ программиста
Andrey Karpov
 
PDF
60 terrible tips for a C++ developer
Andrey Karpov
 
PPTX
Ошибки, которые сложно заметить на code review, но которые находятся статичес...
Andrey Karpov
 
PDF
PVS-Studio in 2021 - Error Examples
Andrey Karpov
 
PDF
PVS-Studio in 2021 - Feature Overview
Andrey Karpov
 
PDF
PVS-Studio в 2021 - Примеры ошибок
Andrey Karpov
 
PDF
PVS-Studio в 2021
Andrey Karpov
 
PPTX
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
Andrey Karpov
 
PPTX
Best Bugs from Games: Fellow Programmers' Mistakes
Andrey Karpov
 
PPTX
Does static analysis need machine learning?
Andrey Karpov
 
PPTX
Typical errors in code on the example of C++, C#, and Java
Andrey Karpov
 
PPTX
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
Andrey Karpov
 
PPTX
Game Engine Code Quality: Is Everything Really That Bad?
Andrey Karpov
 
PPTX
C++ Code as Seen by a Hypercritical Reviewer
Andrey Karpov
 
PPTX
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
Andrey Karpov
 
PPTX
Static Code Analysis for Projects, Built on Unreal Engine
Andrey Karpov
 
PPTX
Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
Andrey Karpov
 
PPTX
The Great and Mighty C++
Andrey Karpov
 
PPTX
Static code analysis: what? how? why?
Andrey Karpov
 
PDF
Zero, one, two, Freddy's coming for you
Andrey Karpov
 
60 антипаттернов для С++ программиста
Andrey Karpov
 
60 terrible tips for a C++ developer
Andrey Karpov
 
Ошибки, которые сложно заметить на code review, но которые находятся статичес...
Andrey Karpov
 
PVS-Studio in 2021 - Error Examples
Andrey Karpov
 
PVS-Studio in 2021 - Feature Overview
Andrey Karpov
 
PVS-Studio в 2021 - Примеры ошибок
Andrey Karpov
 
PVS-Studio в 2021
Andrey Karpov
 
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
Andrey Karpov
 
Best Bugs from Games: Fellow Programmers' Mistakes
Andrey Karpov
 
Does static analysis need machine learning?
Andrey Karpov
 
Typical errors in code on the example of C++, C#, and Java
Andrey Karpov
 
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
Andrey Karpov
 
Game Engine Code Quality: Is Everything Really That Bad?
Andrey Karpov
 
C++ Code as Seen by a Hypercritical Reviewer
Andrey Karpov
 
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
Andrey Karpov
 
Static Code Analysis for Projects, Built on Unreal Engine
Andrey Karpov
 
Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
Andrey Karpov
 
The Great and Mighty C++
Andrey Karpov
 
Static code analysis: what? how? why?
Andrey Karpov
 
Zero, one, two, Freddy's coming for you
Andrey Karpov
 
Ad

Recently uploaded (20)

PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 

PVS-Studio Static Analyzer as a Tool for Protection against Zero-Day Vulnerabilities

  • 1. PVS-Studio Static Analyzer as a Tool for Protection against Zero-Day Vulnerabilities Author: Ekaterina Nikiforova Date: 28.11.2019 Tags: StaticAnalysis, Security A Zero-day (0-day) vulnerability is a computer-software vulnerability introduced during the development process and not yet discovered by the developers. Zero-day vulnerabilities can be exploited by hackers, thus affecting the company's reputation. Developers should seek to minimize the number of defects leading to such vulnerabilities. PVS-Studio, a static code analyzer for C, C++, C#, and Java code, is one of the tools capable of detecting security issues. Zero-day vulnerabilities A Zero-day vulnerability (also known as 0-day vulnerability) is a computer-software vulnerability that is unknown to, or unaddressed by, those who should be interested in mitigating the vulnerability (including the vendor of the target software). Until the vulnerability is mitigated, hackers can exploit it to adversely affect computer programs, data, additional computers or a network. The term means the developers don't have a single day to fix the defect because no one knows about it yet. Some of the well-known vendors and software products such as Adobe, Windows, Tor browser, and many others, were affected by zero-day vulnerabilities in the past. Some were lucky to have a vulnerability found and reported by people who were not going to exploit it. The case of MacOS is one such example. In some other cases, the developers themselves produced a patch with which, while adding new features, they also fixed a zero-day vulnerability without knowing it.
  • 2. Others were less lucky though. For instance, not so long ago, Google Chrome had to urgently fix a vulnerability that could be exploited to remotely execute arbitrary code. The problem is you can't guarantee 100% protection against these vulnerabilities as you can't effectively fight a threat you don't even know of. However, there are ways to make such defects less likely to occur in your program – this will be the topic of this article, but we should take a look at some theory first. Static analysis Static analysis is a method of checking the source code of a software program using an analyzer without executing the program itself and can be viewed as automated code review. Sometimes static analysis can be much more effective than peer code review but can't completely replace it. I tried to summarize the pros and cons of code review and static analysis relative to each other in the following table: Code review Static analysis Helps find not only trivial but also high-level bugs Helps find unfamiliar defects or vulnerabilities Helps improve the program's architecture and work out a consistent coding style Helps find bugs not easily noticeable to the human eye (e.g. typos) Expensive Cheaper than code review Takes up a lot of programmers' time. Breaks are necessary as the reviewer's attention tends to weaken quickly False positives are unavoidable; the user has to customize the analyzer CVE and CWE Common Vulnerabilities and Exposures (CVE) is a database of information-security vulnerabilities and exposures. Its initial purpose was to organize known software defects into a coherent list. In the past, most information-security tools were using their own databases and names for such defects, and it was to bring order to that chaos and establish compatibility between different tools that the MITRE Corporation developed CVE in 1999. However, CVE turned out to be insufficient for estimating code security. Some other system was needed, with finer classification and more detailed descriptions. That's how the Common Weakness Enumeration (CWE) came into existence. If a defect is listed in the CWE, it may cause an exploitable vulnerability and get added to the CVE list as well. The Euler diagram below shows the relations between the standards.
  • 3. Some static analyzers can inform you if, for example, your project employs a library containing a vulnerability. Knowing this, you can download a newer version of the library, with the defect fixed, to make your code less susceptible to security threats caused by a mistake in someone else's code. As the CVE and CWE standards were embraced by the developer community, they were also supported by many information-security tools including static analyzers. Analyzers that support these classifications can be viewed as SAST solutions. SAST (Static Application Security Testing) allows developers to detect vulnerabilities in the source code of programs at the earliest stages of the software development life cycle. SAST is yet another practice to minimize the probability of zero-day vulnerabilities occurring in your project. An analyzer supporting the CWE standard can tell you where a potential vulnerability is lurking so that you could fix it to make your application more reliable and less likely to contain a 0-day threat. There is a variety of SAST tools. I'll take the PVS-Studio analyzer as an example to show how these tools can help fight vulnerabilities. Warnings of this analyzer are classified as CWE. Some examples are given below. PVS-Studio diagnostic message: CWE-561: Dead Code (V3021). public string EncodeImage(....) { if (string.IsNullOrWhiteSpace(inputPath)) { throw new ArgumentNullException("inputPath"); } if (string.IsNullOrWhiteSpace(inputPath)) { throw new ArgumentNullException("outputPath"); } .... } This code contains a typo: the conditions of both if statements check the same variable. The message accompanying the exception suggests that the second condition should check the outputPath variable instead. This mistake has made some part of the code unreachable. Bugs like that might seem harmless, but this impression is wrong. Let's take a look at another trivial and seemingly harmless bug that has to do with a duplicate goto statement. This bug once caused a vulnerability in iOS. The CVE-2014-1266 vulnerability: The SSLVerifySignedServerKeyExchange function in libsecurity_ssl/lib/sslKeyExchange.c in the Secure Transport feature in the Data Security component in Apple iOS 6.x before 6.1.6 and 7.x before 7.0.6, Apple TV 6.x before 6.0.2, and Apple OS X 10.9.x before 10.9.2 does not check the signature in a TLS Server Key Exchange message, which allows man-in-the- middle attackers to spoof SSL servers by using an arbitrary private key for the signing step or omitting the signing step. static OSStatus SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams, uint8_t *signature, UInt16 signatureLen) {
  • 4. OSStatus err; .... if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0) goto fail; if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) goto fail; goto fail; if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0) goto fail; .... fail: SSLFreeBuffer(&signedHashes); SSLFreeBuffer(&hashCtx); return err; } Like in the first example, the duplicate goto here led to unreachable code: whatever the conditions of the if statements, the second goto statement would be executed anyway. As a result, the signature wouldn't be checked, the function would return 0, meaning the signature was OK, and the program would receive a key from the server even if the signature check failed. This key is used to encrypt the data being transmitted. This trivial bug had drastic implications. The incident illustrates why there's no point speculating if this or that CWE defect is dangerous or not – you just have to fix it for the sake of your code's safety. By the way, PVS-Studio could have easily found this bug, reporting it with two CWE warnings at once: • CWE-561 (V779): Dead Code • CWE-483 (V640): Incorrect Block Delimitation Here's another example. Long ago, in 2012, a security issue was discovered in MySQL, which could be exploited by an attacker to enter the MySQL database. Below you will see the flawed code fragment, where the vulnerability occurred. The CVE-2012-2122 vulnerability: sql/password.c in Oracle MySQL 5.1.x before 5.1.63, 5.5.x before 5.5.24, and 5.6.x before 5.6.6, and MariaDB 5.1.x before 5.1.62, 5.2.x before 5.2.12, 5.3.x before 5.3.6, and 5.5.x before 5.5.23, when running in certain environments with certain implementations of the memcmp function, allows remote attackers to bypass authentication by repeatedly authenticating with the same incorrect password, which eventually causes a token comparison to succeed due to an improperly-checked return value. typedef char my_bool; my_bool check_scramble(const char *scramble_arg, const char *message, const uint8 *hash_stage2) { .... return memcmp(hash_stage2, hash_stage2_reassured, SHA1_HASH_SIZE); } The memcmp function returns a value of type int, while the check_scramble function returns a value of type my_bool, which is in fact char. The int value gets implicitly cast to char, with the most significant
  • 5. bits truncated. This caused about 1 out of 256 attempts to log in with an arbitrary password for a known username to succeed. Again, this CWE defect could have been neutralized and prevented from becoming a CVE vulnerability much earlier, at the coding stage. For example, PVS-Studio reports it as CWE-197 (V642): Numeric Truncation Error. See the article "How Can PVS-Studio Help in the Detection of Vulnerabilities?" for further reading on the topic. Conclusion You can't be 100% sure your program is safe from 0-day vulnerabilities. But you can still make them much less likely to occur. This is done by using specialized SAST tools such as PVS-Studio. If your project is found to contain defects classified as CWE issues, make sure to fix them. Even though few of CWE defects end up on the CVE list, fixing them helps to secure your program from many potential threats. References 1. Download and evaluate PVS-Studio 2. Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities 3. Classification of PVS-Studio warnings according to the Common Weakness Enumeration (CWE) 4. Classification of PVS-Studio warnings according to the SEI CERT Coding Standard