Closed Bug 2082614 Opened 1 month ago Closed 13 days ago

Out-of-bounds reads in macOS Apple Mail `.emlx` importer due to signed-to-unsigned length conversion

Categories

(MailNews Core :: Import, defect)

defect

Tracking

(thunderbird_esr153 wontfix, thunderbird162 wontfix)

RESOLVED FIXED
162 Branch
Tracking Status
thunderbird_esr153 --- wontfix
thunderbird162 --- wontfix

People

(Reporter: theodoreholloway008, Assigned: mkmelin)

References

Details

Attachments

(1 file, 1 external file)

Attached file evidence.zip —

Steps to reproduce

Note: This issue was investigated jointly by researchers Dmitri Kaslov and rootkiTed.

We reviewed Thunderbird’s macOS-specific Apple Mail .emlx importer in nsEmlxHelperUtils.mm.

The importer reads the message length using:

uint64_t numberOfBytesToRead =
    strtol((char*)[data bytes], &startOfMessageData, 10);

if (numberOfBytesToRead <= 0 || !startOfMessageData) {
    // Error handling
}

It later calculates a pointer and writes the message using the attacker-controlled length:

const char* startOfXMLMetadata =
    startOfMessageData + numberOfBytesToRead;

rv = aOut->Write(startOfMessageData,
                 (uint32_t)numberOfBytesToRead,
                 &actualBytesWritten);

We created malformed .emlx files containing the following length fields:

  • -5
  • -1x
  • 9999999999

We tested these files using an AddressSanitizer-enabled Objective-C++ harness that reproduces the importer’s parsing and message-writing behavior.

On macOS, the harness can be compiled with:

clang++ -fsanitize=address -fno-omit-frame-pointer -g -O1 \
  -fobjc-arc -framework Foundation \
  emlx_asan_harness.mm -o emlx_harness

The test cases can then be run with:

./emlx_harness poc_benign.emlx
./emlx_harness poc_neg5.emlx
./emlx_harness poc_neg1x.emlx
./emlx_harness poc_trunc.emlx

We also reproduced the underlying behavior on Ubuntu using a product-like C++ harness. That harness models the same parsing logic, exact-size non-null-terminated input buffer, 32-bit length truncation, and downstream mailbox-output scanning behavior.

The Ubuntu commands were:

chmod +x run_f003_productlike_linux.sh
./run_f003_productlike_linux.sh

The affected helper is only compiled when Thunderbird uses the Cocoa widget toolkit:

if CONFIG["MOZ_WIDGET_TOOLKIT"] == "cocoa":
    UNIFIED_SOURCES += [
        "nsAppleMailImport.cpp",
        "nsEmlxHelperUtils.mm",
    ]

Therefore, the affected Thunderbird feature is specific to macOS. The Ubuntu reproduction validates the unsafe parsing and downstream memory-access behavior, but it does not mean that Linux Thunderbird contains the Apple Mail importer.

We have not yet reproduced the issue through the complete Thunderbird macOS graphical import workflow. The attached sanitizer results reproduce the vulnerable code path and its downstream behavior in a focused harness.

Actual results

Malformed message lengths are accepted because the result of strtol() is stored in an unsigned 64-bit integer.

For a negative value such as -5, the signed result is converted into a very large unsigned value. Consequently, this validation does not reject it:

if (numberOfBytesToRead <= 0)

The importer then performs pointer arithmetic using the invalid length:

startOfMessageData + numberOfBytesToRead

It also casts the 64-bit value to uint32_t before passing it to the output stream:

(uint32_t)numberOfBytesToRead

This creates multiple unsafe cases:

  • -5 becomes a very large unsigned value and causes an out-of-bounds read immediately after the small input allocation.
  • -1x becomes UINT64_MAX, then truncates to UINT32_MAX, causing the downstream mailbox scanner to search far beyond the input buffer.
  • 9999999999 exceeds 32 bits and truncates to 1410065407, again causing the downstream scanner to access memory far beyond the supplied .emlx data.

AddressSanitizer reported:

  • A heap-buffer-overflow for poc_neg5.emlx.
  • A segmentation fault in the downstream memchr/std::find scanning path for poc_neg1x.emlx.
  • A segmentation fault in the same scanning path for poc_trunc.emlx.
  • No sanitizer error for the benign control file.

The input buffer is also not guaranteed to be null-terminated. Calling strtol() directly on [data bytes] can therefore read beyond the buffer when the length field consists entirely of digits without an in-bounds terminating character.

The demonstrated security impact is an out-of-bounds read and likely application crash during import of a crafted Apple Mail .emlx file. We have not demonstrated code execution or disclosure of memory contents.

Expected results

Thunderbird should safely reject an .emlx file when its declared message length is:

  • Negative or zero.
  • Not completely and unambiguously parsed.
  • Larger than the bytes remaining in the input buffer.
  • Too large for the type accepted by the downstream output API.
  • Missing an in-bounds delimiter or terminator.

The length field should be parsed using an explicitly bounded range rather than by calling strtol() on a buffer that is not guaranteed to be null-terminated.

Before pointer arithmetic or writing any data, Thunderbird should verify that the parsed length:

  1. Is positive.
  2. Fits within the remaining .emlx input.
  3. Does not overflow any pointer or integer calculation.
  4. Fits within the downstream write-length type without truncation.

Malformed .emlx input should produce a clean import error and must not cause any out-of-bounds memory access or application crash.

Going to open up. It's hard to exploit and crash during import is the highest impact.
But also, turns out import is actually broken since ages (from bug 1933395) so the code wouldn't be reached.

Assignee: nobody → mkmelin+mozilla
Group: mail-core-security
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true

Parse the message length of .emlx files within the bounds of the file data, and
reject lengths that don't fit the remaining data.

Target Milestone: --- → 162 Branch

Pushed by kaie@kuix.de:
https://hg.mozilla.org/comm-central/rev/4a476f6f315b
Improve .emlx header parsing in the Apple Mail importer. r=aleca

Status: ASSIGNED → RESOLVED
Closed: 13 days ago
Resolution: --- → FIXED

(In reply to Magnus Melin [:mkmelin] from comment #1)

But also, turns out import is actually broken since ages (from bug 1933395) so the code wouldn't be reached.

You mean bug 688166?

See Also: → 688166

I only found bug 1978740 at the time. Probably a duplicate of that.

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: