memory overflow in UTF8ToNewUnicode

RESOLVED FIXED in mozilla1.8beta1

Status

()

Core
String
RESOLVED FIXED
13 years ago
11 years ago

People

(Reporter: wind li, Assigned: Christopher Aillon (sabbatical, not receiving bugmail))

Tracking

({fixed-aviary1.0.1, fixed1.4.5, fixed1.7.6})

Trunk
mozilla1.8beta1
fixed-aviary1.0.1, fixed1.4.5, fixed1.7.6
Points:
---
Dependency tree / graph
Bug Flags:
blocking1.7.6 +
blocking-aviary1.0.1 +
blocking1.8b +
blocking-aviary1.5 +

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: [sg:fix])

Attachments

(3 attachments)

(Reporter)

Description

13 years ago
User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.6) Gecko/20040113
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.6) Gecko/20040113

in UTF8ToNewUnicode
>copy_string(aSource.BeginReading(start), aSource.EndReading(end),
>                calculator);
if aSource include none-UTF8 chachacter such as 0xFC 0xDF 
calculator.Length() == 0
>PRUnichar *result = NS_STATIC_CAST(PRUnichar*,
>        nsMemory::Alloc(sizeof(PRUnichar) * (calculator.Length() + 1)));
length of result = 2
>
>    ConvertUTF8toUTF16 converter(result);
>    copy_string(aSource.BeginReading(start), aSource.EndReading(end),
                converter).write_terminator();
if aSource is like "something(0xFC 0xDF)" result will be "something". Danger.

A walk through is to remove the mLength = 0; in
xpcom/string/public/nsUTF8Utils.h:256

Reproducible: Always
Steps to Reproduce:
1.
2.
3.

Comment 1

13 years ago
why would non utf8 characters be in a class clearly labeled utf8?
Component: XPCOM → String
(In reply to comment #1)
> why would non utf8 characters be in a class clearly labeled utf8?

It happens ;-) See bug 236941 for a recent example.
that sounds more like a bug in the caller
(Reporter)

Comment 4

13 years ago
Created attachment 147104 [details] [diff] [review]
remove mLength = 0;
(Reporter)

Updated

13 years ago
Attachment #147104 - Flags: review?(scc)
Confirming bug. Exploitability would depend on the ability of an attacker to get
bad UTF8 into this section of code, but there are enough places where we parse
UTF8 off the web that there's probably some easy ways.

I'm not sure I like the proposed fix. Instead of returning a known error value
(0) it would return a partial length. Then we have to hope ConvertUTF8toUTF16
fails no later than CalculateUTF8Length. It seems better for UTF8toNewUnicode to
check for a zero length before allocating and deal with the error at that level.
Status: UNCONFIRMED → NEW
Ever confirmed: true
Whiteboard: [sg:fix] heap overrun
I'd expect UTF-8 off the web to go to a different place, namely intl's
nsUTF8ToUnicode...
Blocks: 278184, 278186
Flags: blocking1.8b?
Flags: blocking-aviary1.1?
Assignee: dougt → string
Blocks: 278185
Flags: blocking1.7.6?

Updated

13 years ago
Flags: blocking1.8b?
Flags: blocking1.8b+
Flags: blocking-aviary1.1?
Flags: blocking-aviary1.1+
Flags: blocking-aviary1.0.1?

Comment 7

13 years ago
callion to try and work on this a bit.  can use some help
Flags: blocking1.7.6?
Flags: blocking1.7.6+
Flags: blocking-aviary1.0.1?
Flags: blocking-aviary1.0.1+

Updated

13 years ago
Assignee: string → caillon

Updated

13 years ago
Whiteboard: [sg:fix] heap overrun → [sg:fix] heap overrun - eta 2/14
Created attachment 174183 [details] [diff] [review]
Fix attempt

Another fix for the issue, handling the zero-length given by the calculator.

The question to ask is do we protect against this when we clearly state that
UTF8ToNewUnicode takes a UTF-8 string?	Sure, callers should get fixed, but we
probably shouldn't stomp memory if they hand us garbage...

I'm also wondering if the previously submitted patch (removing the mLength = 0
in the calculatr) is correct, since the converter will in fact write up to the
invalid character and hand back a string of that length.  It makes sense to
have the converter and calculator match, I think, with their output length.

I also took a look at ToNewUTF8String which I thought might have had the same
issue, though I don't believe it does after reading through its respective
Convert and Calculate classes.
Attachment #147104 - Attachment is obsolete: true
Attachment #174183 - Flags: review?(dbaron)
Whiteboard: [sg:fix] heap overrun - eta 2/14 → [sg:fix] heap overrun - eta 2/14 [need review dbaron]
Comment on attachment 147104 [details] [diff] [review]
remove mLength = 0;

The converter and the calculator should definitely match.  It looks to me like
this makes them do so, so r=dbaron on this patch.  It would probably be good to
add comments to the header of all 4 classes (the two converters and the two
calculators) about that (i.e., that ConvertUTF8toUTF16 and CalculateUTF8Length
should match and that ConvertUTF16toUTF8 and CalculateUTF8Size should match).
Attachment #147104 - Attachment is obsolete: false
Attachment #147104 - Flags: superreview?(darin)
Attachment #147104 - Flags: review?(scc)
Attachment #147104 - Flags: review+
Comment on attachment 174183 [details] [diff] [review]
Fix attempt

I prefer the other approach.
Attachment #174183 - Flags: review?(dbaron) → review-
Comment on attachment 147104 [details] [diff] [review]
remove mLength = 0;

sr=dveditz
Attachment #147104 - Flags: superreview?(darin) → superreview+
Comment on attachment 147104 [details] [diff] [review]
remove mLength = 0;

a=dveditz for landing everywhere
Attachment #147104 - Flags: approval1.8b+
Attachment #147104 - Flags: approval1.7.6+
Attachment #147104 - Flags: approval-aviary1.0.1+
Whiteboard: [sg:fix] heap overrun - eta 2/14 [need review dbaron] → [sg:fix] need checkin
If I'm not mistaken, the convertor and calculator will still not match in the
case of an incomplete multi-byte sequence (e.g. 110xxxxx not followed by
10xxxxxx, or 1110xxxx not followed by two of 10xxxxxx, etc.).

If by "match" you just mean the convertor should never write more than the
length returned by the calculator, that's fine.
(In reply to comment #9)
> (From update of attachment 147104 [details] [diff] [review] [edit])
> The converter and the calculator should definitely match.  It looks to me like
> this makes them do so, so r=dbaron on this patch.  It would probably be good to
> add comments to the header of all 4 classes (the two converters and the two
> calculators) about that (i.e., that ConvertUTF8toUTF16 and CalculateUTF8Length
> should match and that ConvertUTF16toUTF8 and CalculateUTF8Size should match).

It would also be good to have comments explaining which errors the converter
considers fatal and which it considers recoverable by emitting a REPLACEMENT
CHARACTER, and why. I assume that the distinction is between byte sequences that
look like they are in another encoding (fatal) and byte sequences that look like
UTF-8 with errors (recoverable), but that's only a guess.
Actually, though, there's another case where the computed length could be less
-- which is if we actually get a 5-byte or 6-byte UTF-8 sequence.  It looks like
we'll attempt to fit it into a surrogate pair even though it doesn't fit.
Er, actually, never mind.  We do check for that case.
Fix checked in to trunk, 2005-02-17 12:17 -0800.
Fix checked in to MOZILLA_1_7_BRANCH, 2005-02-17 12:24 -0800.
Fix checked in to AVIARY_1_0_1_20050124_BRANCH, 2005-02-17 12:29 -0800.

Thanks for the patch.
Status: NEW → RESOLVED
Last Resolved: 12 years ago
Keywords: fixed-aviary1.0.1, fixed1.7.6
Resolution: --- → FIXED
Whiteboard: [sg:fix] need checkin → [sg:fix]
Target Milestone: --- → mozilla1.8beta1

Comment 18

12 years ago
Does anyone have a testcase we can use to verify this fix?
Group: security

Comment 19

12 years ago
Created attachment 187760 [details] [diff] [review]
Patch for 1.4 branch
Attachment #187760 - Flags: superreview?(dveditz)
Comment on attachment 187760 [details] [diff] [review]
Patch for 1.4 branch

sr=dveditz
Attachment #187760 - Flags: superreview?(dveditz) → superreview+

Comment 21

12 years ago
Checking in nsUTF8Utils.h;
/cvsroot/mozilla/xpcom/string/public/nsUTF8Utils.h,v  <--  nsUTF8Utils.h
new revision: 1.3.2.1; previous revision: 1.3
done

Updated

12 years ago
Keywords: fixed1.4.5
You need to log in before you can comment on or make changes to this bug.