Skip to content
Memory corruption Disclosed report Advanced

Latent heap corruption on allocator mismatch in DecodeQOrBase64Str()

A function that decodes MIME header parameters allocates memory with one allocator and frees it with another, which can corrupt both heaps when the two allocators use separate heaps. An attacker could potentially trigger heap corruption by sending a crafted MIME header, but only in builds where the zone allocator is enabled.

mozillabugs Follow

Originally disclosed on Mozilla

  • Fixed in 7 months
  • Paid in 3 months
  • Disclosed after 7 months
Text
DecodeQOrBase64Str() (netwerk\mime\nsMIMEHeaderParamImpl.cpp) allocates memory on the PR heap, then frees the same memory on the global heap. If these are different heaps, this mismatch will corrupt both heaps.

The bug is that PL_Base64Decode(), called by line 1128, allocates |decodedText| using PR_MALLOC(), but line 1148 frees it using free():

	1120: nsresult DecodeQOrBase64Str(const char *aEncoded, size_t aLen, char aQOrBase64,
	1121:                             const char *aCharset, nsACString &aResult)
	1122: {
	1123:   char *decodedText;
	1124:   NS_ASSERTION(aQOrBase64 == 'Q' || aQOrBase64 == 'B', "Should be 'Q' or 'B'");
	1125:   if(aQOrBase64 == 'Q')
	1126:     decodedText = DecodeQ(aEncoded, aLen);
	1127:   else if (aQOrBase64 == 'B') {
	1128:     decodedText = PL_Base64Decode(aEncoded, aLen, nullptr);
	1129:   } else {
	1130:     return NS_ERROR_INVALID_ARG;
	1131:   }
	1132: 
	1133:   if (!decodedText) {
	1134:     return NS_ERROR_INVALID_ARG;
	1135:   }
	...
	1148:   free(decodedText);
	...
	1155: }


On Windows builds, these functions use the same heap, so this is only a theoretical bug there. If, however, |_PR_ZONE_ALLOCATOR| is defined in the build, and either the symbol |nspr_use_zone_allocator| or the environment variable |NSPR_USE_ZONE_ALLOCATOR| is defined to |1|, PR_MALLOC() uses a different heap from free().

It does not appear that any builds currently set |nspr_use_zone_allocator| or |NSPR_USE_ZONE_ALLOCATOR| to |1|.


However, it would be possible to cause NSPR to use a separate heap by setting the environment variable |NSPR_USE_ZONE_ALLOCATOR| under Linux, since |_PR_ZONE_ALLOCATOR| is defined in Linux builds indirectly via the definition of |_PR_PTHREADS| in obj-x86_64-pc-linux-gnu/config/external/nspr/pr/backend.mk , which contains the line

	3:	DEFINES += -DDEBUG=1 -D_NSPR_BUILD_ -DHAVE_POINTER_LOCALTIME_R -DHAVE_FCNTL_FILE_LOCKING -D_GNU_SOURCE -DLINUX -D_PR_PTHREADS

That declaration is then used by nsprpub/pr/include/private/primpl.h to define |_PR_ZONE_ALLOCATOR|:

	1872: #if defined(_PR_PTHREADS)
	1873: #define _PR_ZONE_ALLOCATOR
	1874: #endif


It appears that |NSPR_USE_ZONE_ALLOCATOR| is supposed to work, per https://bugzilla.mozilla.org/show_bug.cgi?id=200335 , https://bugzilla.mozilla.org/show_bug.cgi?id=196880 , but it won't with the above or similar bugs, as https://bugzilla.mozilla.org/show_bug.cgi?id=696599 shows.

The original report as published on Mozilla's Bugzilla, by mozillabugs. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 5 min read.

What the report shows

The target is Mozilla's networking code, specifically DecodeQOrBase64Str() in netwerk\mime\nsMIMEHeaderParamImpl.cpp. The function decodes a MIME header parameter that is either Q-encoded or Base64-encoded, depending on the aQOrBase64 argument.

The researcher found that the function allocates memory on one heap and frees it on another. The Base64 branch calls PL_Base64Decode(), which allocates the returned buffer with PR_MALLOC(), but the cleanup path releases that same buffer with free(). The report quotes the relevant lines:

Text
	1120: nsresult DecodeQOrBase64Str(const char *aEncoded, size_t aLen, char aQOrBase64,
	1121:                             const char *aCharset, nsACString &aResult)
	1122: {
	1123:   char *decodedText;
	1124:   NS_ASSERTION(aQOrBase64 == 'Q' || aQOrBase64 == 'B', "Should be 'Q' or 'B'");
	1125:   if(aQOrBase64 == 'Q')
	1126:     decodedText = DecodeQ(aEncoded, aLen);
	1127:   else if (aQOrBase64 == 'B') {
	1128:     decodedText = PL_Base64Decode(aEncoded, aLen, nullptr);
	1129:   } else {
	1130:     return NS_ERROR_INVALID_ARG;
	1131:   }
	1132:
	1133:   if (!decodedText) {
	1134:     return NS_ERROR_INVALID_ARG;
	1135:   }
	...
	1148:   free(decodedText);
	...
	1155: }

The evidence in the report is the allocator mismatch visible in the source. The platform records the severity as low, with no CVSS score, no weakness classification and no bounty listed. The report was disclosed on 2 November 2018.

Root cause

The mistake is an allocator pairing error. PL_Base64Decode() returns memory obtained from the NSPR heap via PR_MALLOC(), but the caller releases it with the C runtime free(). When both calls resolve to the same underlying heap, nothing goes wrong. When they do not, the allocator's bookkeeping is corrupted on both sides.

The report is careful about when the two heaps actually differ. On Windows builds the functions use the same heap, so the bug is described as theoretical there. The split only happens when _PR_ZONE_ALLOCATOR is defined in the build and either the symbol nspr_use_zone_allocator or the environment variable NSPR_USE_ZONE_ALLOCATOR is set to 1.

The researcher then checked whether any build meets those conditions and found no evidence that any build currently sets nspr_use_zone_allocator or NSPR_USE_ZONE_ALLOCATOR to 1. That is why the issue is latent in the configurations examined.

There is, however, a path to reach the split heap on Linux. _PR_ZONE_ALLOCATOR is defined indirectly through _PR_PTHREADS, which Linux builds set in obj-x86_64-pc-linux-gnu/config/external/nspr/pr/backend.mk:

Text
	3:	DEFINES += -DDEBUG=1 -D_NSPR_BUILD_ -DHAVE_POINTER_LOCALTIME_R -DHAVE_FCNTL_FILE_LOCKING -D_GNU_SOURCE -DLINUX -D_PR_PTHREADS

That macro is consumed by nsprpub/pr/include/private/primpl.h:

Text
	1872: #if defined(_PR_PTHREADS)
	1873: #define _PR_ZONE_ALLOCATOR
	1874: #endif

So on Linux the compile-time half of the condition is satisfied, and the report says it would be possible to cause NSPR to use a separate heap by setting the NSPR_USE_ZONE_ALLOCATOR environment variable, though it will not work with this or similar bugs. The report notes that the variable is supposed to work, citing Mozilla bugs 200335 and 196880, but that it will not with this or similar bugs, citing bug 696599.

Impact

The report's own framing is that this is a latent heap corruption issue. If the allocator mismatch is triggered, both heaps are corrupted. The report does not demonstrate that outcome and does not describe an attack path from a crafted MIME header to code execution.

The practical impact depends entirely on the build and runtime configuration. In the configurations the researcher examined, the two allocators coincide, so the bug does not manifest. The report does not claim that any released product is affected.

How this kind of bug works

Memory allocators keep metadata around each block: size classes, free lists, arena or zone ownership. Freeing a pointer into the wrong allocator means that metadata is read and rewritten in the wrong place. The result is usually a crash, but a carefully shaped sequence of allocations and frees can turn the corruption into controlled writes.

The bug class appears whenever two allocation interfaces coexist in one codebase: a library allocator such as NSPR's PR_MALLOC() and the C runtime's malloc()/free(), a custom pool and the system heap, or a debug allocator and the release one. It is easy to miss because in the default build both interfaces often wrap the same underlying allocator, so tests pass. The defect only surfaces under a build flag, an environment variable or a platform where the implementations diverge.

Defences are structural: never mix allocation and deallocation interfaces, wrap every allocation in a matching free function, and use RAII or smart pointers so ownership is tied to the allocating API. Build configurations that separate heaps, such as zone allocators and sanitizer allocators, are valuable because they turn these latent mismatches into immediate, reproducible failures.

How to test for it

This is primarily a source review technique, though it can be confirmed dynamically.

  1. Identify every place where a buffer returned by a library allocation function is released. Grep for the library's free counterpart and for plain free() and delete in the same files.
  2. For each hit, trace the allocation back to its origin. If the producer is a library call, check the library's documentation for which deallocator it requires.
  3. Look for build flags, environment variables or platform macros that switch the allocator, as the report does with _PR_ZONE_ALLOCATOR and NSPR_USE_ZONE_ALLOCATOR.
  4. To confirm dynamically, build or run the target with the alternate allocator enabled and exercise the code path with input that reaches the mismatched free. A positive result is a crash or sanitizer report naming the mismatched allocation and free sites.
  5. Common false positives: the library's allocator may be a thin wrapper over the same heap, so the mismatch is harmless in that configuration; a macro may redefine free() to the library's deallocator; or the buffer may be copied and the original freed by the library itself.

How it is fixed

Hunting notes

Allocator mismatches cluster in code that bridges two libraries: NSS and NSPR in Mozilla code, OpenSSL and the C runtime elsewhere, custom pools in parsers and protocol handlers. Header parsing, MIME decoding, certificate handling and anything that calls a third-party decode routine are good places to look.

Recon signals include mixed use of PR_MALLOC/free, CRYPTO_malloc/free, or pool APIs alongside standard allocation in the same translation unit, and build files that define allocator-switching macros. Because the bug is often latent, the strongest report shows the exact allocation and free sites, explains the configuration that separates the heaps, and, where possible, includes a crash or sanitizer trace from a build with that configuration enabled. Without that, reviewers are likely to treat the finding as theoretical.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 7 min read.

What happened, in plain words

This report is about a bug in Mozilla's networking code, the part of the browser that handles things like email and web content. The specific function is DecodeQOrBase64Str() in netwerk\mime\nsMIMEHeaderParamImpl.cpp. Its job is to decode a MIME header parameter, which is a small piece of text attached to a message or file that tells software how to interpret it. The parameter can be encoded in one of two ways, Q-encoding or Base64, and the function picks the right decoder based on the aQOrBase64 argument.

To understand the bug, you need to know what a heap is. When a program needs memory at runtime, it asks an allocator for a block, and later it gives the block back. The allocator keeps records of which blocks are in use. A heap is the pool of memory that one allocator manages. If a program takes a block from one allocator and hands it back to a different one, the second allocator's records get confused, and so do the first one's. That is called heap corruption.

The researcher found exactly that mismatch. The Base64 branch calls PL_Base64Decode(), which reserves the returned buffer using PR_MALLOC(), a Mozilla allocation function. But the cleanup path releases the same buffer with free(), the standard C function. The report quotes the relevant lines:

Text
	1120: nsresult DecodeQOrBase64Str(const char *aEncoded, size_t aLen, char aQOrBase64,
	1121:                             const char *aCharset, nsACString &aResult)
	1122: {
	1123:   char *decodedText;
	1124:   NS_ASSERTION(aQOrBase64 == 'Q' || aQOrBase64 == 'B', "Should be 'Q' or 'B'");
	1125:   if(aQOrBase64 == 'Q')
	1126:     decodedText = DecodeQ(aEncoded, aLen);
	1127:   else if (aQOrBase64 == 'B') {
	1128:     decodedText = PL_Base64Decode(aEncoded, aLen, nullptr);
	1129:   } else {
	1130:     return NS_ERROR_INVALID_ARG;
	1131:   }
	1132:
	1133:   if (!decodedText) {
	1134:     return NS_ERROR_INVALID_ARG;
	1135:   }
	...
	1148:   free(decodedText);
	...
	1155: }

Line by line: line 1120 declares the function and its inputs. Line 1123 declares a pointer to hold the decoded text. Line 1124 checks that the encoding type is either 'Q' or 'B'. Lines 1125 to 1131 choose the decoder: DecodeQ() for Q-encoding, PL_Base64Decode() for Base64, and an error for anything else. Lines 1133 to 1135 handle the case where decoding failed. Line 1148 is the problem: it frees the buffer with free(), even though line 1128 obtained it from PL_Base64Decode().

The evidence in the report is the mismatch visible in the source. The platform records the severity as low, with no CVSS score, no weakness classification and no bounty listed. The report was disclosed on 2 November 2018.

Why the two heaps are usually the same

The report is careful about when the mismatch actually matters. On Windows builds, both functions use the same heap, so the bug is described as theoretical there. The split only happens when _PR_ZONE_ALLOCATOR is defined in the build and either the symbol nspr_use_zone_allocator or the environment variable NSPR_USE_ZONE_ALLOCATOR is set to 1. A build is the compiled version of the software, and an environment variable is a named setting the operating system passes to a program when it starts.

The researcher checked whether any build meets those conditions and found no evidence that any build currently sets nspr_use_zone_allocator or NSPR_USE_ZONE_ALLOCATOR to 1. That is why the issue is latent: it exists in the code but does not trigger in the configurations examined.

There is, however, a path to the split heap on Linux. _PR_ZONE_ALLOCATOR is defined indirectly through _PR_PTHREADS, which Linux builds set in obj-x86_64-pc-linux-gnu/config/external/nspr/pr/backend.mk:

Text
	3:	DEFINES += -DDEBUG=1 -D_NSPR_BUILD_ -DHAVE_POINTER_LOCALTIME_R -DHAVE_FCNTL_FILE_LOCKING -D_GNU_SOURCE -DLINUX -D_PR_PTHREADS

That macro is then used by nsprpub/pr/include/private/primpl.h:

Text
	1872: #if defined(_PR_PTHREADS)
	1873: #define _PR_ZONE_ALLOCATOR
	1874: #endif

So on Linux the compile-time half of the condition is satisfied, and the report says it would be possible to cause NSPR to use a separate heap by setting the NSPR_USE_ZONE_ALLOCATOR environment variable, though it will not work with this or similar bugs. The report notes that the variable is supposed to work, citing Mozilla bugs 200335 and 196880, but that it will not with this or similar bugs, citing bug 696599.

The report's own framing is that this is a latent heap corruption issue. If the mismatch is triggered, both heaps are corrupted. The report does not demonstrate that outcome and does not describe an attack path from a crafted MIME header to code execution. The practical impact depends entirely on the build and runtime configuration. In the configurations the researcher examined, the two allocators coincide, so the bug does not manifest. The report does not claim that any released product is affected.

In general, how this kind of bug works

Memory allocators keep records around each block: sizes, lists of free blocks, and which zone or arena owns them. Freeing a pointer into the wrong allocator means those records are read and rewritten in the wrong place. The result is usually a crash, but a carefully shaped sequence of allocations and frees can turn the corruption into controlled writes.

This bug class appears whenever two allocation interfaces coexist in one codebase: a library allocator such as NSPR's PR_MALLOC() and the C runtime's malloc()/free(), a custom pool and the system heap, or a debug allocator and the release one. It is easy to miss because in the default build both interfaces often wrap the same underlying allocator, so tests pass. The defect only surfaces under a build flag, an environment variable or a platform where the implementations diverge.

Defences are structural: never mix allocation and deallocation interfaces, wrap every allocation in a matching free function, and use RAII or smart pointers so ownership is tied to the allocating API. RAII is a pattern where a resource is released automatically when the object that owns it goes out of scope. Build configurations that separate heaps, such as zone allocators and sanitizer allocators, are valuable because they turn these latent mismatches into immediate, reproducible failures.

In general, how to test for it

This is primarily a source review technique, though it can be confirmed dynamically.

  1. Identify every place where a buffer returned by a library allocation function is released. Search for the library's free counterpart and for plain free() and delete in the same files.
  2. For each hit, trace the allocation back to its origin. If the producer is a library call, check the library's documentation for which deallocator it requires.
  3. Look for build flags, environment variables or platform macros that switch the allocator, as the report does with _PR_ZONE_ALLOCATOR and NSPR_USE_ZONE_ALLOCATOR.
  4. To confirm dynamically, build or run the target with the alternate allocator enabled and exercise the code path with input that reaches the mismatched free. A positive result is a crash or sanitizer report naming the mismatched allocation and free sites.
  5. Common false positives: the library's allocator may be a thin wrapper over the same heap, so the mismatch is harmless in that configuration; a macro may redefine free() to the library's deallocator; or the buffer may be copied and the original freed by the library itself.

In general, how it is fixed

In general, where to look

Allocator mismatches cluster in code that bridges two libraries: NSS and NSPR in Mozilla code, OpenSSL and the C runtime elsewhere, custom pools in parsers and protocol handlers. Header parsing, MIME decoding, certificate handling and anything that calls a third-party decode routine are good places to look.

Recon signals include mixed use of PR_MALLOC/free, CRYPTO_malloc/free, or pool APIs alongside standard allocation in the same translation unit, and build files that define allocator-switching macros. Because the bug is often latent, the strongest report shows the exact allocation and free sites, explains the configuration that separates the heaps, and, where possible, includes a crash or sanitizer trace from a build with that configuration enabled. Without that, reviewers are likely to treat the finding as theoretical.

Glossary

  • MIME header parameter: A small piece of text attached to a message or file that tells software how to interpret it.
  • Q-encoding / Base64: Two ways of writing text using only safe characters so it can travel through systems that expect plain text.
  • Heap: The pool of memory that an allocator manages at runtime.
  • Allocator: The part of a program that hands out and takes back blocks of memory.
  • Heap corruption: Damage to an allocator's records, caused by misusing memory, which can lead to crashes or worse.
  • PR_MALLOC(): Mozilla's NSPR function for reserving memory.
  • free(): The standard C function for releasing memory.
  • NSPR: Mozilla's cross-platform runtime library, which provides its own memory functions.
  • Build: The compiled version of the software produced from source code.
  • Environment variable: A named setting the operating system passes to a program when it starts.
  • Macro: A named piece of code that the compiler substitutes before building.
  • Latent bug: A defect that exists in the code but does not trigger in the configurations examined.
  • RAII: A pattern where a resource is released automatically when the object that owns it goes out of scope.
  • Sanitizer: A tool that instruments a build to detect memory errors at runtime.
  • CVSS: A standard scoring system for the severity of a vulnerability.
  • Proof of concept: A small demonstration that a bug can actually be triggered.

More by mozillabugs

View all

41 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.