Uninitialized variable leads to invalid/arbitrary memory read in S/MIME decryption
- Fixed in 16 days
- Paid in 27 days
- Disclosed after 16 days
Created attachment 9274927 POC file
This is a bug I encountered in the decryption process of CMS messages in NSS, triggerable by passing a malicious message to NSS_CMSDecoder_Update. This flow is used by Thunderbird and Evolution when attempting to read S/MIME encrypted emails.
In NSS_CMSDigestContext_FinishSingle, variable dp is supposed to be initialized by a successful call to NSS_CMSDigestContext_FinishMultiple, and then dereferenced and passed to SECITEM_CopyItem as the third argument:
SECStatus
NSS_CMSDigestContext_FinishSingle(NSSCMSDigestContext *cmsdigcx,
PLArenaPool *poolp,
SECItem *digest)
{
...
SECItem **dp;
...
/* get the digests into arena, then copy the first digest into poolp */
rv = NSS_CMSDigestContext_FinishMultiple(cmsdigcx, arena, &dp);
if (rv == SECSuccess) {
/* now copy it into poolp */
rv = SECITEM_CopyItem(poolp, digest, dp[0]);
}
...
However, NSS_CMSDigestContext_FinishMultiple can return SECSuccess without initializing the passed dp argument. This happens in case cmsdigcx->saw_contents == 0:
/* no contents? do not finish digests */
if (digestsp == NULL || !cmsdigcx->saw_contents) {
rv = SECSuccess;
goto cleanup;
}
I'm not sure exactly what in the input causes the program to arrive to this state, but it seems to be some bug in the ASN.1 parsing code.
When this happens, dp remains uninitialized when SECITEM_CopyItem is called, and one of following may happen:
1. Crash in NSS_CMSDigestContext_FinishSingle when attempting to read dp[0].
2. Crash in SECITEM_CopyItem when attempting to read from pointer dp[0]->data.
3. If stack memory is properly groomed by an attacker, valid memory of dp[0]->len bytes will be read from pointer dp[0]->data into an object.
I reproduced this on Thunderbird and Evolution (.p7m email attachment), and on cmsutil compiled from trunk.
Steps to reproduce:
$ cmsutil -D -d . -i poc.cms
MSan log:
==3958692==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x7ffff75a00eb in NSS_CMSDigestContext_FinishSingle ./lib/smime/cmsdigest.c:258:46
#1 0x7ffff759b3f0 in NSS_CMSDigestedData_Decode_AfterData ./lib/smime/cmsdigdata.c:187:14
#2 0x7ffff7594f84 in nss_cms_after_data ./lib/smime/cmsdecode.c:367:18
#3 0x7ffff758da0a in nss_cms_decoder_notify ./lib/smime/cmsdecode.c:185:21
#4 0x7ffff77eed10 in sec_asn1d_notify_after ./lib/util/secasn1d.c:437:5
#5 0x7ffff77cea44 in sec_asn1d_next_in_sequence ./lib/util/secasn1d.c:2078:5
#6 0x7ffff77b2e35 in SEC_ASN1DecoderUpdate_Util ./lib/util/secasn1d.c:2823:17
#7 0x7ffff758e7a2 in NSS_CMSDecoder_Update ./lib/smime/cmsdecode.c:683:14
#8 0x4ab1c3 in decode ./cmd/smimetools/cmsutil.c:191:10
#9 0x4a678a in main ./cmd/smimetools/cmsutil.c:1455:24
#10 0x7ffff6e35fcf in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#11 0x7ffff6e3607c in __libc_start_main csu/../csu/libc-start.c:409:3
#12 0x420db4 in _start (./dist/Debug/bin/cmsutil+0x420db4)
Uninitialized value was created by an allocation of 'dp' in the stack frame of function 'NSS_CMSDigestContext_FinishSingle'
#0 0x7ffff759fa90 in NSS_CMSDigestContext_FinishSingle ./lib/smime/cmsdigest.c:242
SUMMARY: MemorySanitizer: use-of-uninitialized-value ./lib/smime/cmsdigest.c:258:46 in NSS_CMSDigestContext_FinishSingle
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A high-severity memory corruption bug in Libraries. Mozilla made it public on 20 May 2022 as CVE-2022-31741.
About 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.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.