To get a trial key
fill out the form below
Team License (standard version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
PVS-Studio Static Analyzer as a Tool fo…

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

Nov. 28, 2019

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

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.

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 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:

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

Popular related articles
Static analysis as part of the development process in Unreal Engine

Date: 06.27.2017

Author: Andrey Karpov

Unreal Engine continues to develop as new code is added and previously written code is changed. What is the inevitable consequence of ongoing development in a project? The emergence of new bugs in th…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: 07.31.2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: 11.21.2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…
The Last Line Effect

Date: 05.31.2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…
PVS-Studio ROI

Date: 01.30.2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
The Evil within the Comparison Functions

Date: 05.19.2017

Author: Andrey Karpov

Perhaps, readers remember my article titled "Last line effect". It describes a pattern I've once noticed: in most cases programmers make an error in the last line of similar text blocks. Now I want t…
The Ultimate Question of Programming, Refactoring, and Everything

Date: 04.14.2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: 10.22.2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
Free PVS-Studio for those who develops open source projects

Date: 12.22.2018

Author: Andrey Karpov

On the New 2019 year's eve, a PVS-Studio team decided to make a nice gift for all contributors of open-source projects hosted on GitHub, GitLab or Bitbucket. They are given free usage of PVS-Studio s…
Appreciate Static Code Analysis!

Date: 10.16.2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…

Comments (0)

Next comments

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept