In April, I analyzed two Yubico C smartcard libraries using libFuzzer.
During this process, I found two vulnerabilities in libykpiv plus a few minor other security problems and a number of generic bugs.
Essentially, a malicious PIV (FIPS 201) smartcard can cause two sorts of issues during the host-initiated private RSA key generation, namely an out of bounds read on the host library stack (CVE-2020-13131) and a denial of service (CVE-2020-13132). Under some conditions, host process memory is copied into a corrupted RSA public key and returned by the library to the caller. The potential to obtain stack memory of the host via public keys is an unusual information leak.
The following article will describe the issues and how they were found.
I’m a freelance Security Consultant and currently available for new projects.
If you are looking for assistance to secure your projects or organization, contact me.
First, some background information on the automated error-finding approach and related challenges.
The libykpiv library source repository also contains the yubico-piv-tool CLI, which uses the library to provide a number of management functions of PIV smartcards and is the official tool to manage some PIV aspects of YubiKey devices. I decided to fuzz the library through this tool.
Broadly speaking, I replaced parts of the yubico-piv-tool programming so that libFuzzer is in control over all relevant data inputs. To achieve this, the command line parameters (argv), the smartcard responses (pcsc-lite backend) and the stdin-based interactive user inputs are (mostly) fed with fuzzer-controlled data. To explain this on a higher level: libFuzzer “decides” which PIV management operations are called, which data the user feeds in and how the virtual smartcard reacts to them. As a result, the fuzzer can test a broad range of states within the program for crashes and runtime errors without requiring purposefully written code for each management operation. As it is common during fuzzing, the AddressSanitizer, UndefinedBehaviorSanitizer and MemorySanitizer modules are used in varying configurations to provide enhanced error detection.
This fuzzing setup required several code iterations to work well and still progresses a lot slower when compared to targeted function-level fuzzing. However, it has the major advantage of staying close to the real-world code behavior (-> tests everything that is reachable through main() of the CLI tool) while automatically looking for problematic edge cases without extensive manual intervention. For example, the fuzzer will try to find and use undocumented command line flags and flag combinations, but will not call library functions in ways that are unreachable in the normal program. In general, this approach is complex and not suited for every target, but it can save a lot of work when compared to building and managing a dozen narrow, specialized fuzzer harnesses for individual functions. This is particularly useful during explorative work on a foreign codebase, as it was the case here. It might not be suited very well if you’re going for unit-test like coverage of each function.
To help the fuzzer exploration, I manually collected some interesting command line flag snippets in a dictionary file.
As usual, there are a number of general code changes that one has to perform on a program before it is usable with libFuzzer. In particular, the resulting binary
should not have side effects between individual runs (this involves resetting certain variables)
should not exhibit general stability issues during runtime (file writes, resource leaks, …)
each individual program run should be fast (no blocking input or sleep operations)
For yubico-piv-tool, this meant finding and fixing a number of bugs so that stable fuzzing operations ran longer than a few minutes. There are still some remaining memory leaks in the codebase, particularly in the OpenSSL handling, but I’m optimistic that Yubico developers will fix those in the next 1-2 releases.
Out of Bounds Read - CVE-2020-13131
The ykpiv_util_generate_key() function in the libykpiv host-side library is designed to trigger a private key generation within the PIV smartcard and return the corresponding public key that is parsed from the smartcard response.
Note: a number of USB hardware tokens such as the YubiKey 5 series also act as the PIV smartcard & reader, so this applies to them as well if the affected functionality is used.
Relevant code paths:
RSA1024 / RSA2048: affected
ECC P256 / ECC P384: not affected
Interestingly, the RSA path of the key generation function has some extra handling logic due to the infamous Return of Coppersmith’s attack on RSA keys that was present in the firmware of older YubiKey 4 devices through a flaw in an Infineon cryptography library. This is not relevant for the attack that is discussed here, though.
After checking the validity of some function parameters, _ykpiv_transfer_data() is called. This prompts the smartcard to actually generate the requested private key and store it internally in a specific key slot. If the operation is successful, the smartcard will return several data fields in its response that contain the corresponding public key in the form of the cryptographic key components.
The smartcard response is temporarily stored in an unsigned char data message buffer on the host, which will become relevant later.
Consider the following code section that parses the response for the RSA case:
The modulus field in the response has a variable length, therefore the _ykpiv_get_length(data_ptr, &len) function is used to parse the relevant length field for a matching memory allocation. Unfortunately, the resulting length of 0 to 65535 in the variable len is then used without any bounds checks in the following memory operations, which represents the essential memory handling flaw of this vulnerability. The unchecked length field allows an attacker to significantly over-report the actual length of the provided data fields in the smartcard response and cause an out of bounds read on the host through the memcpy() operation that reads from the data buffer.
As a consequence, uninitialized memory in data and up to ~64.5k bytes of stack memory behind data will be copied into the freshly allocated ptr_modulus buffer on the heap.
This memory will only be returned to the caller if the key generation function completes without errors, so it is in the interest of the attacker to make that happen.
The RSA handling of ykpiv_util_generate_key() contains two variable length fields: first the modulus (code shown above) and then the exponent.
Due to the way that data_ptr is incremented, attacking the modulus field is problematic and fails with a high probability since a specific followup check against *data_ptr contents has to succeed despite pointing to stack memory that is not controlled by the attacker.
The better approach for an attacker is to report a small length value on the modulus field and then over-report the length for the exponent field (cb_exp). The code for this second read operation is shown here:
This attack vector is much more reliable since there is no following check on *data_ptr values after this copy operation. Once the function returns with the return code YKPIV_OK, it depends on the caller of the libykpiv library function how the large information leak in *exp and the large *exp_len length is handled.
For the yubico-piv-tool CLI tool, the leaked data will be processed via OpenSSL bignum functions and copied as encoded ASN.1 binary data into the public key that is returned to the user (via stdout or file write).
It is plausible that this local information leak can lead to further exposure of the relevant data since public keys are designed to be copied across trust boundaries.
Other callers of libykpiv might reject the returned data due to the abnormal exponent length or unintentionally crash due to other memory issues.
Note that depending on the size specified by the attacker via the over-reported length field, it is possible that libykpiv will crash with a segfault if the read operation goes beyond the available stack space.
See the sections below for more thoughts on the practical impact.
Denial of Service - CVE-2020-13132
The ykpiv_util_generate_key() function has a second problematic code path in the RSA1024 / RSA2048 key generation.
In the cleanup section, free() is called on the wrong pointer if ptr_modulus is set:
The program will crash with a deadly signal if this code is reached.
Here is the verbose warning if the program is explicitly instrumented with the AddressSanitizer:
==20689==ERROR: AddressSanitizer: attempting free on address which was
not malloc()-ed: 0x7ffc74123880 in thread T0
ptr_modulus is usually NULL if there are obvious problems, and it is set back to NULL if the data parsing for the RSA case was successful.
This explains why the problem was not detected during regular testing: the edge case is only reached if a (simulated) smartcard responds with mostly good data to pass some of the function checks.
A malicious smartcard could do the following to trigger the issue during a key generation request:
report SW_SUCCESS on the data transfer and provide some response bytes
set TAG_RSA_MODULUS correctly
set a reasonable, small cb_modulus length (-> for ptr_modulus allocation) and some dummy data for the modulus
set TAG_RSA_EXPincorrectly to trigger goto Cleanup
-> program crash
Although it is present in the same code section, the crash is not directly an “insufficient length field validation” problem and distinct enough from the first issue to be treated separately.
Attack Scenario and Security Implications
As described in the previous sections, CVE-2020-13131 and CVE-2020-13132 are located in the ykpiv_util_generate_key() function of the host library.
They are exposed when libykpiv is used to request a new RSA private key generation from the smartcard.
During this particular operation, a malicious device that is recognized as the target smartcard can trigger the out of bounds read on the stack or crash the process.
Note that the vulnerabilities
are not reachable during regular PIV verification usage or other daily workflows that do not include key generation
will not be triggered by genuine smartcards (unless there is some transmission error)
are on the host side and do not affect the YubiKey device firmware code
For the typical Linux user with a YubiKey on their keychain and the occasional use of yubico-piv-tool for device configuration, the practical security impact of these issues is fairly small.
The same is true for users on Windows and other operating systems, from what I can tell.
If a particular public key generated via yubico-piv-tool is fully functional (for example, successful login with your smartcard) then it is unlikely to contain any leaked stack memory.
The CVE issues are of concern to automated systems that use libykpiv to provision externally provided smartcards (where a fake smartcard could be part of a batch of devices or physically plugged into the target machine), particularly if those systems call the configuration tools with secret values (management key, PIN, bash environment variables, other secret internal state) and rely on uninterrupted operation or expose the resulting public key to some third party.
I am not aware of any public product that does this. However, I would not be surprised if automated systems of this sort exist somewhere, perhaps in proprietary form, in order to provision or manage a collection of physical keys at some enterprise or institution that relies on PIV authentication.
Yubico has evaluated both the first and second issue with a CVSSv3.1 score of 4.3.
Proof Of Concept
The following patch for libykpiv simulates a malicious smartcard response to trigger CVE-2020-13131. Use it together with your target program and a physical PIV token to test the practical impact.
WARNING: use the following code at your own risk. Assume that this will PERMANENTLY overwrite data on the dummy smartcard device that you use.
Depending on the segmentation fault risks that an attacker is willing to take, most bash environment variables that are present during the program execution can be dumped as well, although with a considerable risk of crashing if the out of bounds read exceeds process boundaries.
Consider the following shell environment variable created with export SECRET_ENV_VARIABLE=SECRET_VALUE before running the vulnerable program. This is copied into the public key as well:
This sort of information disclosure is particularly concerning for automated systems, which are often configured to hold secrets in environment variables.
Note on Older Cryptography Standards
Some readers might have noticed that RSA1024 and RSA2048 key generations are possible in the code, but RSA4096 generation is not. In fact, the stronger RSA with 4096bit length is missing simply because the PIV standard defined by NIST does not include it. Therefore it is not Yubico’s fault that libykpiv cannot offer this variant, and it would require an update to the PIV standard to allow this.
However, it might be time to implement the recommendations by NIST (as outlined here by Yubico, current NIST document p.59) to disallow new generation of relatively weak cryptographic keys such as RSA1024 by default to discourage their usage. For example, yubico-piv-tool could implement an additional flag to allow the creation of RSA1024 if explicitly requested and error out otherwise. I recommend implementing this in a future release of the code.
In my opinion, it is somewhat concerning that this general vulnerability pattern can still be found in cryptography-related Yubico software in 2020.
I consider it very positive that Yubico’s host-side code is available as open-source. Among many other benefits for Yubico and the community, it allows easier code analysis by security researchers, which I consider very important for trustworthy cryptography. For example, the research presented here would not have happened without access to the source code. However, the previous history shows that code is not automatically audited “for free” in the necessary depth just because it is available in an open-source format, permissively licensed or used in a number of Unix and Windows driver contexts.
The Yubico engineers and developers that I’ve been in contact with are competent and very dedicated. They are bringing a lot of fixes and improvements to the code as part of the constant maintenance efforts. However, I think Yubico should really consider investing more resources into this topic and systematically weed out these types of issues, particularly memory safety issues in the C code. The behavior of their drivers and libraries should be trustworthy and sane regardless of whether the input is coming from a malicious device or not. Cryptography-related code requires a high level of robustness.
Improving this situation will require more eyes on the problem (= additional resources and developer time). An open bug bounty program with reasonably competitive rewards might help with this situation in the long run and bring in some creative approaches, but it obviously requires some engineering resources on its own for triage and resolution. At times, it may require spending extra developer time on low-quality reports. But while it is not a magic fix, I would personally welcome it as a reason to do more research on their code from time to time ;)
In parallel, it might make sense to speed up the efforts to move to memory-safe languages where the environment permits this (CLI vs. drivers), as it is already done with tools such as YubiKey Manager which is written in Python. However, this is a complex topic on its own and largely out of scope here.
I first contacted Yubico in April and reported a number of code issues, both via confidential disclosure emails to security@ as well as via the public GitHub issue tracker (after clearing them with the security contact).
During the disclosure process, I had the opportunity to coordinate closely on the subject of security assessments, evaluate the proposed security patches and give inputs for parts of the wording. This process was very positive - I value the trust and openness shown by the vendor by including me in their issue resolution. Although it is arguably also more unpaid work on the researcher side, it can be satisfying to participate in the low-level resolution efforts if the communication is very productive, which it was.
Notably, the originally planned disclosure date was postponed on short notice. Since the disclosure was still well within the usual 90-day time frame and there were no signs that the issues are extremely dangerous or actively misused, I had no objections. From a researcher perspective, shorter disclosure time frames are obviously preferable, but I understand the necessity to balance this with other software development concerns.
Overall, I am satisfied with the disclosure process.
Very late in the disclosure process (early July), I noticed the experimental yubikey-piv.rs project which had ported the affected Yubico C code to Rust.
As far as I’m aware, they are not affected by the information leak aspects of CVE-2020-13131 since the out of bounds read will be mitigated in Rust by halting the program. Due to timing and other considerations, they were not included in the coordinated disclosure process.