Breaking down the ‘critical’ OpenSSL vulnerability
Tags: applicationsecurity, vulnerabilities
Published: 2022-11-14 15:34:38

---
title: Breaking down the ‘critical’ OpenSSL vulnerability
published: true
date: 2022-11-04 18:44:54 UTC
tags: ApplicationSecurity,Vulnerabilities
canonical_url: https://snyk.io/blog/breaking-down-openssl-vulnerability/
---

On November 1st 2022, the OpenSSL team released an [advisory](https://www.openssl.org/news/secadv/20221101.txt) detailing two high severity vulnerabilities — CVE-2022-3602 and CVE-2022-3786. This was pre-announced as a critical bug, but later downgraded to high for the actual release. This could still be problematic though, OpenSSL is one of the predominant encryption libraries and is underpinning a significant portion of the internet’s TLS protected communications. 

In this post we’ll break down the two OpenSSL vulnerabilities — with a focus on the technical details for CVE-2022-3602 — and look at whether or not the level of attention this received is warranted, if this is really a `heartbleed 2.0`, and how concerned we should actually be. 

## The advisory

The advisory which outlines these bugs describes two buffer overflow / overrun vulnerabilities in the X.509 certificate validation, specifically within the [name constraint](https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.10) validation. These issues affect OpenSSL 3.0.0 to 3.0.6, and users of OpenSSL 3 should update to 3.0.7. IHowever, it’s important to not panic. According to [Censys](https://censys.io/critical-vulnerability-in-openssl/), as of October 2022, a few more than 7,000 hosts out of nearly 1.8 million (0.4%) run a version greater than or equal to version 3.0.0. The most deployed version of OpenSSL within the vulnerable range is 3.0.1, with around 50% of all hosts. Additionally OpenSSL is not the only option for SSL/TLS implementations — with alternatives such as NSS, BoringSSL, SChannel, and LibreSSL. Additionally, there are certain conditions for this specific bug, which even further reduce the number of impacted systems from those running vulnerable versions of OpenSSL 3.

So, how do we approach understanding this situation? Let’s start with the [commit](https://github.com/openssl/openssl/commit/3b421ebc64c7b52f1b9feb3812bdc7781c784332) that fixed the more serious issue (CVE-2022-3602), which has the potential to lead to Remote Code Execution and see what information we can obtain.

[**CVE-2022-3602**](https://security.snyk.io/vuln/SNYK-UNMANAGED-OPENSSL-3090874)

Interestingly, the commit is only changing a single line within the codebase!

![Code commit for CVE-2022-3602, that only involved changing a single line within the codebase.](https://snyk.io/wp-content/uploads/blog-openssl-breakdown-single-line-change-1240x379.jpg)

Based on the above change, this looks a lot like a classic “off by one” bug in `ossl_punycode_decode`, which is confirmed in the commit message — “_An off by one error in the punycode decoder allowed for a single unsigned int overwrite of a buffer, which could cause a crash and possible code execution._“

[**CVE-2022-3786**](https://security.snyk.io/vuln/SNYK-UNMANAGED-OPENSSL-3092519)

A secondary buffer overflow in the `ossl_a2ulabel` function, with a severity of high, was disclosed and fixed in this [commit](https://github.com/openssl/openssl/commit/680e65b94c916af259bfdc2e25f1ab6e0c7a97d6). Unlike CVE-2022-3602, this issue was always rated as high severity, and wasn’t downgraded. Despite this flaw allowing an attacker to overflow and write to a larger region of memory, it is not possible for an attacker to control what data is written. In this case, only the `.` character (decimal 46) can be written, which restricts the exploit possibilities to memory corruption using the specified character, causing a Denial of Service. 

After looking at the two issues, it’s clear that CVE-2022-3602 — which can allow for Remote Code Execution — is the more interesting vulnerability. So from here on, we will focus on it.

## Off by one

We won’t go into detail on what off by one vulnerabilities are exactly and how to exploit them, as this is already covered well in other places ([Fortify](https://vulncat.fortify.com/en/detail?id=desc.internal.cpp.buffer_overflow_off_by_one), [Daniel Slater](https://www.youtube.com/watch?v=wUPujx-f_Js)), but some basic knowledge is required to understand the OpenSSL bug in question. Off by one vulnerabilities occur when length checking conditions are implemented incorrectly. An easy way to visualize this is to think about a fixed size buffer and a for loop that failed to take the zero-index into account. 

![Example of code with an off by one vulnerability.](https://snyk.io/wp-content/uploads/blog-openssl-breakdown-off-by-one.jpg)

In the above example, we have two things that go wrong. First, we no longer get our NULL byte to terminate our string from the program arguments when the size exceeds 32 — allowing the following `printf` to read beyond the bounds of `buffer`. More importantly however, we are writing a single arbitrary byte outside the bounds of `buffer`. Generally, the off by one can result in the following scenarios:

- Overwrite the saved frame pointer of the previous function, thus allowing application flow to be redirected when the function returns
- Overwriting an adjacent variable on the stack next to the destination buffer

## Back to OpenSSL CVE-2022-3206

The patch for CVE-2022-3206 reveals a fairly similar off by one to our textbook example above. The `ossl_punycode_decode` function incorrectly uses the `greater than` operator instead of `greater than or equal to` at line 184 — which results in a loop termination condition not returning at the correct condition, and writing an additional unsigned integer outside the bounds of the allocated space. 

So, when is this vulnerable function invoked? `ossl_punycode_decode` provides punycode decoding functionality, which allows unicode characters to be represented in the limited ASCII character subset supported for domain names — e.g. `ŠÑÝĶ.io` is encoded to `xn--iday5n4f.io`. In order for untrusted input to be decoded by this function the following need to apply:

- A malicious CA or intermediary certificate must include a name constraint field with punycode.
- The leaf certificate must contain a `SubjectAlternateName (SAN) otherName` field, which specifies a `SmtpUTF8Mailbox` string.

When these conditions are satisfied, the punycode from the name constraint field will be parsed by the vulnerable `ossl_punycode_decode` function.

### Server-Side Attacks

For this to affect servers running OpenSSL 3, the server must be configured to accept client certificates. Client certificate authentication is not used in the majority of scenarios, so most servers will be unaffected (due to most services typically using a form based approach only). If you are accepting client certificates the attack path may be viable. However, the punycode decoding occurs after certificate validation, so a valid certificate chain or a configuration where signature errors are ignored is required. 

It could well be the case that a private CA or intermediary is an untrusted party and could be positioned to sign valid certificates that are trusted by the server — for example, this is fairly common when authenticating API requests from external web services. But if client certificates are issued by a trusted party, then exploitation should not be possible based on the current information available.

### Client-Side Attacks

A TLS client using OpenSSL 3 could be affected when connecting to a malicious server. The same constraints for a valid certificate chain or certificate errors to be ignored apply here. While it can be common to ignore certificate errors in TLS clients, this is generally not the default.

## I meet the attack requirements, how likely is exploitation?

Since there is a vulnerability here, OpenSSL libraries should be patched to 3.0.7 to fix this issue. In most cases, the outcome would be Denial of Service. However, exploitation to Remote Code Execution is likely to be non-trivial. These conditions allow for an arbitrary write of four bytes, which doesn’t provide an attacker with many opportunities. The security labs team at Datadog have already explored exploitation of this issue in [detail](https://securitylabs.datadoghq.com/articles/openssl-november-1-vulnerabilities/#notes-on-exploitability-leading-to-rce), and even corrupting important memory regions to cause a Denial of Service is difficult on Linux based systems. Modern memory protection mechanisms such as PIE, padding, and stack canaries / cookies significantly reduce the likelihood of a working RCE exploit.

## Detecting memory corruption bugs

When dealing with unmanaged languages such as C/C++ it’s very easy for subtle defects to materialize in detrimental ways, although it doesn’t look like this bug is a major concern — this could have been detected with dynamic fuzz testing. 

To catch these OpenSSL bugs with a fuzzer, two approaches can be taken:

1. Create a test harness for the vulnerable functions `ossl_punycode_decode` and `ossl_a2ulabel` within crypto/punycode.c and fuzz them directly. This approach is good in case one knows what they’re looking for.
2. Create a client-server setup with a crafted certificate, and pass through the entire OpenSSL certificate parsing and validation. This is a general approach, which has a significant setup overhead compared to the first one.

Since it’s significantly easier, we opted for the first option. Here’s an example for the test file created to catch CVE-2022-3602 with [libFuzzer](https://llvm.org/docs/LibFuzzer.html) — a coverage-guided, in-process fuzzing engine:

```
#include <stdio.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/err.h>
#include "fuzzer.h"
#include "internal/nelem.h"
#include <crypto/punycode.h>
 
#define MAX_OUTPUT_BUFF 1024
 
static BIO *bio_out;
 
int FuzzerInitialize(int *argc, char ***argv)
{
   OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
   ERR_clear_error();
   CRYPTO_free_ex_index(0, -1);
   return 1;
}
 
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{  
   unsigned int output_buf[MAX_OUTPUT_BUFF];
   unsigned int bsize = OSSL_NELEM(output_buf);
   (void)ossl_punycode_decode(buf, len, output_buf, &bsize);
   ERR_clear_error();
   return 0;
}
 
void FuzzerCleanup(void)
{
    BIO_free(bio_out);
}
```

After running it for a bit on `ossl_punycode_decode` from version 3.0.6, we found a crash in the vulnerable function:

```
==9024==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffeb247ede0 at pc 0x5615d1499782 bp 0x7ffeb247da50 sp 0x7ffeb247d220
WRITE of size 264 at 0x7ffeb247ede0 thread T0
    #0 0x5615d1499781 in __asan_memmove (/home/user/openssl/fuzz/punycode+0xd7d781) (BuildId: 546d1cbf454c87a04c514633876bd5307eab38b2)
    #1 0x5615d155c023 in ossl_punycode_decode /home/supriza/openssl/crypto/punycode.c:188:9
    #2 0x5615d14d76de in FuzzerTestOneInput /home/supriza/openssl/fuzz/punycode.c:42:11
```

Applying the patch that fixes the issue prevents the fuzzer from crashing, further validating the finding.

If you would like to get familiar with libfuzzer to protect your own native code, or just experiment with OpenSSL fuzzing, you can find our fuzzing setup [on GitHub](https://github.com/supriza/openssl-v3.0.7-cve-fuzzing). It would be interesting to demonstrate how this bug can be caught in a real, client-server SSL certificate validation scenario with a more complete fuzzing setup.

## The bottom line

The recent pre-announcement of the OpenSSL 3 vulnerabilities certainly caused a lot of concern, but now that the details are available we are thankful this is not a “heartbleed 2.0” style bug like many anticipated. While there are still two high severity vulnerabilities here, these are not internet breaking bugs and we do not expect to see widespread exploitation. Organizations regular patch and vulnerability management procedures can be followed here to update to OpenSSL 3.0.7 or later within a timely manner. Additionally, tools such as [Snyk](https://snyk.io/) can help teams identify vulnerable instances of OpenSSL (and many other security issues) for patching. [Get started with a free forever Snyk account today](https://app.snyk.io/login).

## Identify OpenSSL for free

Create a Snyk account to find and fix vulnerabilities, such as OpenSSL, in your code, dependencies, containers, and cloud infrastructure.

[Sign up for free](https://app.snyk.io/login?cta=sign-up&loc=body&page=breaking-down-the-critical-openssl-vulnerability)
<!-- /.block -->