﻿# PVS\-Studio static analyzer as a tool for protection against zero\-day vulnerabilities

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\.

![0689_0day/image1.png](https://import.viva64.com/docx/blog/0689_0day/image1.png)

## Zero\-day vulnerabilities

A [Zero\-day vulnerability](https://en.wikipedia.org/wiki/Zero-day_(computing)) \(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](https://securelist.com/sykipot-exploits-an-adobe-flash-zero-day/29760/), [Windows](https://www.bleepingcomputer.com/news/security/new-windows-zero-day-bug-helps-delete-any-file-exploit-available/), 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](https://securityaffairs.co/wordpress/86543/hacking/synthetic-clicks-macos-0day.html) 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\.

![0689_0day/image2.png](https://import.viva64.com/docx/blog/0689_0day/image2.png)

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](https://pvs-studio.com/en/pvs-studio/sast/) 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](https://cwe.mitre.org/data/definitions/561.html): Dead Code \([V3021](https://pvs-studio.com/en/docs/warnings/v3021/)\)\.

```cpp
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](https://cve.mitre.org/cgi-bin/cvename.cgi?name=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\.

```cpp
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](https://cwe.mitre.org/data/definitions/561.html) \([V779](https://pvs-studio.com/en/docs/warnings/v779/)\): Dead Code
* [CWE\-483](https://cwe.mitre.org/data/definitions/483.html) \([V640](https://pvs-studio.com/en/docs/warnings/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](https://cve.mitre.org/cgi-bin/cvename.cgi?name=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\.

```cpp
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](https://cwe.mitre.org/data/definitions/197.html) \([V642](https://pvs-studio.com/en/docs/warnings/v642/)\): Numeric Truncation Error\.

See the article "[How can PVS\-Studio help in the detection of vulnerabilities?](https://pvs-studio.com/en/blog/posts/cpp/0514/)" 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](https://pvs-studio.com/en/pvs-studio/download/)
1. [Technologies used in the PVS\-Studio code analyzer for finding bugs and potential vulnerabilities](https://pvs-studio.com/en/blog/posts/cpp/0592/)
1. [Classification of PVS\-Studio warnings according to the Common Weakness Enumeration \(CWE\)](https://pvs-studio.com/en/pvs-studio/sast/cwe/)
1. [Classification of PVS\-Studio warnings according to the SEI CERT Coding Standard](https://pvs-studio.com/en/pvs-studio/sast/cert/)