Skip to content
Memory corruption Disclosed report Advanced

Latent overflow in AppendUTF16toUTF8() could cause buffer overrun

An integer overflow in the AppendUTF16toUTF8() string helper could make it allocate a buffer smaller than the data it then writes, leading to a buffer overrun. The bug is described as latent because no current caller appends to a destination string long enough to trigger it.

q1 Follow

Originally disclosed on Mozilla

  • Fixed in 14 days
  • Paid in 27 days
  • Disclosed after 14 days
Text
AppendUTF16toUTF8() (xpcom\string\nsReadableUtils.cpp) can experience an integer overflow. If it does, it allocates a buffer too short for the data that it writes into it.

This bug appears to be latent because there appear to be no callers that append to a destination string that is long enough to trigger the bug. (Most callers, e.g., NS_ConvertUTF16toUTF8(), append to a zero-length destination string).

The bug is in line 192:

177: bool
178: AppendUTF16toUTF8(const nsAString& aSource, nsACString& aDest,
179:                   const mozilla::fallible_t& aFallible)
180: {
181:   nsAString::const_iterator source_start, source_end;
182:   CalculateUTF8Size calculator;
183:   copy_string(aSource.BeginReading(source_start),
184:               aSource.EndReading(source_end), calculator);
185: 
186:   uint32_t count = calculator.Size();
187: 
188:   if (count) {
189:     uint32_t old_dest_length = aDest.Length();
190: 
191:     // Grow the buffer if we need to.
192:     if (!aDest.SetLength(old_dest_length + count, aFallible)) {
193:       return false;
194:     }
195: 
196:     // All ready? Time to convert
197: 
198:     ConvertUTF16toUTF8 converter(aDest.BeginWriting() + old_dest_length);
199:     copy_string(aSource.BeginReading(source_start),
200:                 aSource.EndReading(source_end), converter);
201: ...

The problem arises because a single UTF-16 character can be represented by up to 3 UTF-8 characters.

Imagine that |aSource| is as long as a UTF-16 nsAString can be (0x3FFFFFF9 characters [1]), and that it contains only characters that are represented by 3 UTF-8 characters (e.g., 0xe000). In this case, the resulting string is 3*0x3FFFFFF9 = 0xBFFFFFEB UTF-8 characters long. Since CalculateURF8Size::write() [2] -- which calculates this quantity -- does not actually attempt to write anything, its result is not limited by the maximum size of a UTF-8 string as enforced by [1] (which would be 0x7FFFFFF5 characters).

This means that, if |aDest| is also at least 0x100000000-0xBFFFFFEB = 0x40000015 characters long -- well within the limit for a UTF-8 strong -- the addition on line 192 overflows, causing allocation of a buffer that is too small to contain the resulting string.

This bug is still present in trunk: http://hg.mozilla.org/mozilla-central/file/tip/xpcom/string/nsReadableUtils.cpp .


[1] This limit is enforced by nsTSubstring_CharT::MutatePrep().

[2] In xpcom\string\nsUTF8Utils.h.

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

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

Likely the length addition of the existing destination length and the computed UTF-8 size is done in a 32-bit unsigned integer without checking for overflow before resizing the buffer.

Impact

If a caller ever appended to a sufficiently long destination string, the undersized allocation could cause a buffer overrun; the text states no current caller triggers it.

What the attacker needed

A caller appending to a very long destination string; source string of maximum length with 3-byte UTF-8 characters

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.

When a program converts text from one internal format to another, it first works out how much space the result needs and then reserves that much memory. Here the size calculation could wrap around to a small number, so too little memory was reserved and the conversion would write past the end of it. The author notes that no current code path actually reaches this situation, so it is a hidden flaw rather than one being exploited.

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.

More by q1

View all

71 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.