Integer overflow in CipherUpdate
An integer overflow in OpenSSL's CipherUpdate function caused a negative output length to be returned when processing a very large input buffer. An attacker could potentially trigger memory access outside the intended buffer region, typically resulting in a crash.
Originally disclosed on HackerOne
- Triaged in 39 hours
- Fixed in 38 days
- Paid in 38 days
- Disclosed after 39 days
Summary:
I reported an integer overflow to the OpenSSL security list on Dec 13, 2020 and it was fixed in OpenSSL 1.1.1j. Reporting it here for the bounty. It was assigned CVE-2021-23840 (https://nvd.nist.gov/vuln/detail/CVE-2021-23840) which NVD rated CVSS 7.5. Amusingly, the same bug (worked around by my library pyca/cryptography before 1.1.1j was released) was assigned CVE-2020-36242 (https://nvd.nist.gov/vuln/detail/CVE-2020-36242), which received a 9.1 CVSS from NVD.
Steps To Reproduce:
The below is a reproducer for prior to 1.1.1j.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <openssl/evp.h>
int main() {
int res;
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
assert(ctx != NULL);
unsigned char key[] = "0000000000000000";
unsigned char iv[] = "0000000000000000";
res = EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv, 1);
assert(res == 1);
int intmax = 2147483647;
void *inbuf = malloc(intmax);
void *outbuf = malloc((size_t)2147483648);
int outlen = 0;
unsigned char data[] = "0";
res = EVP_CipherUpdate(ctx, outbuf, &outlen, data, 1);
printf("Processed %i bytes, outlen: %i, res: %i\n", 1, outlen, res);
assert(res == 1);
outlen = 0;
res = EVP_CipherUpdate(ctx, outbuf, &outlen, (unsigned char
*)inbuf, intmax);
assert(res == 1);
printf("Processed %i bytes, outlen: %i, res: %i\n", intmax, outlen, res);
}
Impact
This returned negative output length, which, when combined with common use of pointer arithmetic in buffers results in accessing incorrect regions of memory (typically this would manifest as a segfault due to the size of the negative value, but that is not guaranteed).
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely an integer overflow when the output length is computed for a very large input, allowing the length to wrap to a negative value.
Impact
The negative output length could lead to accessing incorrect memory regions, typically manifesting as a segfault, though that is not guaranteed.
What the attacker needed
Ability to pass a very large input to CipherUpdate
The fix
It was fixed in OpenSSL 1.1.1j.
How it was found
- Create a cipher context and initialise it with AES-128-CBC using a key and IV.
- Allocate an input buffer of the maximum signed integer size and a larger output buffer.
- Call CipherUpdate once with a single byte of data.
- Call CipherUpdate again with the very large input buffer and observe the returned output length.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
OpenSSL is a widely used library that handles encryption. When it processes data, it reports how many bytes it produced as a number. Here, with an enormous input, that number wrapped around to a negative value, so code using it could look at the wrong part of memory and crash.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.