On a very very quick read, the bug is that EVP_EncodeBlock malfunctions if the input is larger than ~1.5GB. EVP_EncodeBlock is a function that encodes base64.
https://docs.openssl.org/1.0.2/man3/EVP_EncodeInit/
As a seasoned C programmer, I can immediately observe that the whole API is bad: the caller is expected to calculate the buffer length on their own, using a nontrivial formula, and the caller does not pass in the output buffer length, so the implementation cannot validate it. But apparently OpenSSL 4 also implemented it wrong and wrote (possibly far) past the end of the buffer.
You should probably assume that, if you are running anything that can be convinced to use this API to encode a very long blob as base64, then you are in trouble. I don’t know why a modern server would do this.
BTW, OpenSSL seems to be a rather low quality implementation of a very low quality set of APIs. Stay away.
edit: Fixed my size threshold, hopefully. I was thinking of unsigned ints, but this is a signed int. I have not looked at the code, just the description.
> BTW, OpenSSL seems to be a rather low quality implementation of a very low quality set of APIs. Stay away.
We’ve known this for well over two decade since heartbleed yet for some reason Linux distros haven’t migrated off of it
I like to declare that industry coding standards such as MISRA exist precisely because code bases like OpenSSL (specifically) exist, as it's such a good example of breaking every sensible rule in the standards.
Even the discussion in the PR about not handling the errors in all call-sites is revealing imo.
Many OpenSSL APIs are terribly designed.
> BTW, OpenSSL seems to be a rather low quality implementation of a very low quality set of APIs. Stay away.
Yep, this is not news. https://en.wikipedia.org/wiki/LibreSSL was forked off OpenSSL now more than ten years ago to address exactly these kind of issues, and more (at the price of occasionally taking some drastic measures)
But somehow we're still here.
This is really embarrassing because this isn't even ancient code.
OpenSSL has been around a long time and has a lot of legacy code, but this is in OpenSSL 4.x only -- stuff that was written in the last ~year.
If they don't have the manpower to write new code safely they should just stick to maintaining what they've already written.
I've been annoyed by writing code against OpenSSL's APIs before, precisely because it expects me to provide "appropriately sized buffers" without documenting the correct way to compute the correct size very well.
> EVP_EncodeBlock() encodes a full block of input data in f and of length dlen and stores it in t. For every 3 bytes of input provided 4 bytes of output data will be produced. If dlen is not divisible by 3 then the block is encoded as a final block of data and the output is padded such that it is always divisible by 4. Additionally a NUL terminator character will be added. For example if 16 bytes of input data is provided then 24 bytes of encoded data is created plus 1 byte for a NUL terminator (i.e. 25 bytes in total). The length of the data generated without the NUL terminator is returned from the function.
My god, you only see this type of horrendous API in C don't you.
>As a seasoned C programmer, I can immediately observe that the whole API is bad: the caller is expected to calculate the buffer length in their own, using a nontrivial formula, and the caller does not pass in the output buffer length, so the implementation cannot validate it. But apparently OpenSSL 4 also implemented it wrong and wrote (possibly far) past the end of the buffer.
Many such cases.
I expect we'll see some fun libyuv bugs soon in applications which decode media. The APIs are begging for misuse.
https://chromium.googlesource.com/libyuv/libyuv/+/refs/heads...
> BTW, OpenSSL seems to be a rather low quality implementation of a very low quality set of APIs. Stay away.
Well, it depends. if in the coming weeks many more Heartbleed-level vulnerabilities would be exposed, {Amazon, Google, OpenAI, Anthropic} can just rewrite it in Rust, formally verified.
There does not exist such implementation as of now as OpenSSL was good enough, AI may expose so many security flaws it would no longer be good enough.
What's obnoxious is the WG14 standard committee could have at any time in the last 40 years provided a standard slice and buffer type that would go a long way to discourage trash API's like this one.
I swear it's de rigueur in C that if an operation requires 5 parameters to insure the operation is error free than the function shall only take 4 parameters.
Rewriting the function with idiot mode disabled
int EVP_EncodeUpdate(EVP_ENCODE_CTX ctx, buffer_t out, const slice_t in);
> I can immediately observe that the whole API is bad
I see this is your first time reading OpenSSL code
> BTW, OpenSSL seems to be a rather low quality implementation of a very low quality set of APIs. Stay away.
Well sure, but the world doesn't seem to be able to agree on, or put resources into, a strong contender.
I'm usually for heterogeneity and diversity in the software ecosystem, but for cryptography I'd be happy with a single high-quality "anointed path". Because cryptography.
One of the ‘rewrite it in rust’ projects that should really cause anyone to be offended IMHO.
if the input is larger than ~1.5GB.
...on a system where ints are 32 bits. The bottom line is that this doesn't appear to be easily exploitable and IMHO it should be the callers of EVP_EncodeBlock which need to be inspected to determine the actual risk.
using a nontrivial formula
It's base64. 4/3 expansion; round it up and add padding. What's "nontrivial" about that?
and the caller does not pass in the output buffer length, so the implementation cannot validate it
Not necessary, because the caller is responsible for allocation.
> BTW, OpenSSL seems to be a rather low quality implementation of a very low quality set of APIs.
Fully agree with your assessment above that line, but damn, this is a strong statement for a library this ubiquitous. You wonder; how could this happen? And yet, as an open source developer myself, I know how little people care about code quality.