Out-of-bounds write when passing an output buffer smaller than the block size to NSC_EncryptUpdate
In NSC_EncryptUpdate, when the output buffer is smaller than the block size, the function passes the block size as the maximum output length to the underlying cipher update, causing a small out-of-bounds write. This also corrupts the maxout variable, leading to an arbitrarily large out-of-bounds write in a subsequent update call.
Originally disclosed on Mozilla
- Fixed in 47 days
- Paid in 52 days
- Disclosed after 47 days
In `NSC_EncryptUpdate`, suppose that the `*pulEncryptedPartLen` parameter is smaller than the block size.
We assume the following path through the function:
'''
...
if (crv != CKR_OK) // false
...
if (!pEncryptedPart) { // false
...
if (context->doPad) { // true
/* deal with previous buffered data */
if (context->padDataLength != 0) { // true
/* fill in the padded to a full block size */
for (...) {
// this loop continues until context->padDataLength == context->blockSize
// for this to happen, it's sufficient that ulPartLen >= context->blockSize
// i.e., that there are at least context->blockSize bytes of input
// or more precisely, this will happen when
// ulPartLen + context->padDataLength >= context->blockSize
}
/* not enough data to encrypt yet? then return */
if (context->padDataLength != context->blockSize) { // false, due to above
...
}
/* encrypt the current padded data */
rv = (*context->update)(context->cipherInfo, pEncryptedPart,
&padoutlen, context->blockSize, context->padBuf,
context->blockSize);
'''
In the call to `context->update`, the fourth parameter indicates the maximum length which can be written to the output buffer `pEncryptedPart`. This parameter is set to be `context->blockSize`, even though only `*pulEncryptedPartLen` bytes remain in the output buffer, and we were assuming that `*pulEncryptedPartLen` was less than the block size.
There are two ramifications of this: first, we can have a small out-of-bounds write here. The call to `context->update` can write up to about `context->blockSize` bytes off of the end of the output buffer.
Second, `context->update` will write to the variable `padoutlen` indicating the number of bytes written. Supposing `context->update` does write the allowed maximum of `context->blockSize` bytes to the buffer, we will have `padoutlen == context->blockSize`. Then:
'''
pEncryptedPart += padoutlen; // advancing the pEncryptedPart pointer beyond the end of the buffer
maxout -= padoutlen;
'''
Since `maxout` was initialized to be `*pulEncryptedPartLen` and is thus smaller than the block size in this case, the subtraction will wrap around and give a very large positive value for `maxout` due to its unsigned type.
The subsequent use of `maxout` here:
'''
/* do it: NOTE: this assumes buf size in is >= buf size out! */
rv = (*context->update)(context->cipherInfo, pEncryptedPart,
&outlen, maxout, pPart, ulPartLen);
'''
will allow `context->update` to write up to `maxout` many bytes, that is, effectively arbitrarily many bytes to `pEncryptedPart`. This is an out-of-bounds write arbitrarily far beyond the buffer.
I think the first call to `context->update` should pass `maxout` rather than `context->blockSize` as the fourth parameter, so that the maximum output length is `maxout` rather than `context->blockSize`. I.e.,
'''
rv = (*context->update)(context->cipherInfo, pEncryptedPart,
- &padoutlen, context->blockSize, context->padBuf,
+ &padoutlen, maxout, context->padBuf,
context->blockSize);
'''
The related function `NSC_DecryptUpdate` already does this: it uses `maxout` for the maximum-output-length parameter to `context->update` every time it calls `context->update`.
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
The function passes the block size as the maximum output length to the cipher update instead of the actual remaining buffer size, and later uses an underflowed unsigned variable as the maximum length.
Impact
An attacker could trigger a small out-of-bounds write and, due to integer underflow, an arbitrarily large out-of-bounds write, potentially leading to memory corruption or code execution.
What the attacker needed
Attacker can control output buffer size; victim uses vulnerable library
The fix
The first call to context->update should pass maxout instead of context->blockSize as the maximum 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.
This is a bug in a cryptographic library. When a program asks to encrypt data but provides a small output buffer, the library incorrectly tells the encryption routine it can write more bytes than the buffer can hold. This can cause memory corruption, potentially crashing the program or allowing an attacker to execute code.
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.