Bugzilla@Mozilla – Bug 455987
integer overflow in nsEscape
Last modified: 2009-01-28 07:32:57 PST
Summon comment box
+++ This bug was initially created as a clone of Bug #445117 +++ > /* XXX Hardcoded max entity len. The +1 is for the trailing null. */ > char *rv = (char *) nsMemory::Alloc(strlen(string) * 6 + 1); > > |* 6| overflows at about 683M on 32 bit platform. The issue Georgi noted in bug 445117 also occurs in xpcom/io/nsEscape.cpp -- the same pattern happens in nsEscapeHTML() and nsEscapeHTML2() I'm not sure if you can practically stuff 683M into the things that use nsEscapeHTML, maybe if the transport is gzipped. http://mxr.mozilla.org/mozilla/search?string=nsEscapeHTML
dveditz you mean something different from Bug 367736 ?
aaa, i see: http://mxr.mozilla.org/mozilla-central/source/xpcom/io/nsEscape.cpp#292 292 PRUnichar *resultBuffer = (PRUnichar *)nsMemory::Alloc(aSourceBufferLen * 293 6 * sizeof(PRUnichar) + sizeof(PRUnichar('\0')));
>I'm not sure if you can practically stuff 683M i suppose receiving it is possible. more of problems for exploiting this probably are: 1. 683 MB on the wire consumes much more VM so OOM may be hit 2. one must avoid crash. though if one does overlapping copy (the returned ptr is in low address) it may not crash.
in theory 683MB need not be on the wire - they may be javascript generated probably
Yeah, a document title in places, say, or something fed through the feed sanitizer.
This code got copied to nsPlacesImportExportService.cpp, apparently because the use of string apis in the nsEscape copy caused problems? http://bonsai.mozilla.org/cvsblame.cgi?file=mozilla/browser/components/places/src/nsPlacesImportExportService.cpp&rev=1.65#239 Dietrich: what's the story there? Is the situation different now so this code can call the standard escape routine? Simple enough to fix in both places, but if it's no longer necessary getting rid of duplicate code would be nice.
Created attachment 344247 [details] [review] prevent nsEscapeHTML overflow In addition to the patch I cleaned up a bunch of tabs. Will attach a -w patch next to make review easier.
Created attachment 344248 [details] [review] cvs diff -w version of above
Created attachment 344249 [details] [review] same for places
Comment on attachment 344247 [details] [review] prevent nsEscapeHTML overflow >Index: xpcom/io/nsEscape.cpp >+ char *rv = nsnull; >+ /* XXX Hardcoded max entity len. The +1 is for the trailing null. */ >+ PRUint32 len = PL_strlen(string); >+ if (len < (PR_UINT32_MAX / 6)) { >+ rv = (char *)nsMemory::Alloc( (len * 6) + 1 ); >+ } >+ char *ptr = rv; > >- return(rv); >+ if(rv) This would be a lot clearer if you took a shortcut path: if (len >= PR_UINT32_MAX / 6) return nsnull; char *rv = (char*) NS_Alloc((6 * len) + 1); if (!rv) return nsnull; ... conversion code This reduces the overall indent and makes the control flow more apparent. Also please use NS_Alloc instead of nsMemory::Alloc in all new code. r=me with those nits addressed
Created attachment 344700 [details] [review] nits fixed, what was checked in
Created attachment 344701 [details] [review] diff -w version of the above
checked in: http://hg.mozilla.org/mozilla-central/rev/c6d4904201b5
Comment on attachment 344700 [details] [review] nits fixed, what was checked in Approved for 1.8.1.18 and 1.9.0.4. a=ss
Checked into the 1.8 and 1.9.0 branches
Any word on a mechanism to exercise this code as we discussed the other day, Dan?
Comment on attachment 344700 [details] [review] nits fixed, what was checked in a=asac for 1.8.0 branch
This landed before 1.9.1 branched