Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
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, 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


Comments (0)

Next comments next comments
close comment form
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam