Created attachment 326343 [details]
Crashes Mac trunk. May need to refresh several times.
Drew Yao from Apple Product Security reported this issue to security@mo. The attached testcase crashes trunk on Mac (after ~2-5 refreshes), but not Windows or Linux, afaict. The link to the Talkback report is provided above, and I'm doing a DEBUG build to hopefully provide a persistent call stack copy (should the Talkback report get deleted).
From Drew's email:
Free of uninitialized pointer in gfxQuartzSurface::~gfxQuartzSurface()
This probably only affects Mac OS X. It does not seem to affect Firefox 2, only Firefox 3.
gfxQuartzSurface::gfxQuartzSurface(const gfxSize& size, gfxImageFormat format,
: mSize(size), mForPrinting(aForPrinting)
unsigned int width = (unsigned int) floor(size.width);
unsigned int height = (unsigned int) floor(size.height);
if (!CheckSurfaceSize(gfxIntSize(width, height)))
return; <-- the gif triggers a failure, causing a return before mCGContext is initialized
cairo_surface_t *surf = cairo_quartz_surface_create
((cairo_format_t) format, width, height);
mCGContext = cairo_quartz_surface_get_cg_context (surf);
CGContextRelease(mCGContext); //<-- an uninitialized pointer can be released, without ever having been retained.
In Firefox 3 RC1 on Mac OS X 10.5.3 Intel,
open the attached gif file and refresh a few times. It should crash.
If you set the environment variable MallocScribble=1 first, it will always crash in CFRelease accessing 0xaaaaaaaa, indicating that it's trying to free uninitialized heap memory. man malloc to see the behavior of MallocScribble. It also does what MallocPreScribble claims to do.
Process: firefox-bin 
Version: 3.0pre (3.0pre)
Code Type: X86 (Native)
Parent Process: launchd 
Date/Time: 2008-06-05 16:19:58.990 -0700
OS Version: Mac OS X 10.5.3 (9D34)
Report Version: 6
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x00000000aaaaaaaf
Crashed Thread: 0
Thread 0 Crashed:
0 com.apple.CoreFoundation 0x955f15a4 CFRelease + 36
1 libthebes.dylib 0x12132afb gfxQuartzSurface::~gfxQuartzSurface() + 41
2 libthebes.dylib 0x1211632e gfxASurface::Release() + 182 (gfxASurface.cpp:102)
3 libgkgfxthebes.dylib 0x120db328 nsRefPtr<gfxASurface>::~nsRefPtr() + 28
4 libgkgfxthebes.dylib 0x120de88a nsThebesImage::Draw(nsIRenderingContext&, gfxRect const&, gfxRect const&, gfxRect const&) + 2318 (nsThebesImage.cpp:515)
5 libgklayout.dylib 0x122eb9ff nsLayoutUtils::DrawImage(nsIRenderingContext*, imgIContainer*, nsRect const&, nsRect const&, nsRect const*) + 2569 (nsLayoutUtils.cpp:2618)
6 libgklayout.dylib 0x1237eb68 nsImageFrame::PaintImage(nsIRenderingContext&, nsPoint, nsRect const&, imgIContainer*) + 326 (nsImageFrame.cpp:1185)
7 libgklayout.dylib 0x1237ecad nsDisplayImage::Paint(nsDisplayListBuilder*, nsIRenderingContext*, nsRect const&) + 95 (nsImageFrame.cpp:1167)
8 libgklayout.dylib 0x122c0b91 nsDisplayList::Paint(nsDisplayListBuilder*, nsIRenderingContext*, nsRect const&) const + 61 (nsDisplayList.cpp:295)
9 libgklayout.dylib 0x122c1dbb nsDisplayWrapList::Paint(nsDisplayListBuilder*, nsIRenderingContext*, nsRect const&) + 41 (nsDisplayList.cpp:694)
10 libgklayout.dylib 0x122c2a50 nsDisplayClip::Paint(nsDisplayListBuilder*, nsIRenderingContext*, nsRect const&) + 128 (nsDisplayList.cpp:888)
By filling the heap, an attacker may be able to control the pointer that gets freed, potentially leading to memory corruption and/or arbitrary code execution.
Created attachment 326360 [details]
Backtrace from GIF crash
This is a simple fix, patch coming up...
Created attachment 326419 [details] [diff] [review]
Created attachment 326420 [details] [diff] [review]
Patch rev. 1
We need to initialize mCGContext to NULL since it's used in the
destructor. It doesn't really matter in consumers since if we
leave early here mSurfaceValid is false.
I've checked the other Surface classes for similar problems and
they look fine. (I did find an (edge case) leak in gfxImageSurface
but I'll file that separately.)
CFRelease of NULL causes a crash.
> CFRelease of NULL causes a crash.
Thanks for the notice, but we're using CGContextRelease/Retain here
which handles NULL.
Comment on attachment 326420 [details] [diff] [review]
Patch rev. 1
Mats, are you going to be able to check this in before the freeze tonight, or should we make this checkin-needed?
CC'ing reed, as he's scheduled to check in a number of other fixes, and he's willing to land this as well.
Checking in gfx/thebes/src/gfxQuartzSurface.cpp;
/cvsroot/mozilla/gfx/thebes/src/gfxQuartzSurface.cpp,v <-- gfxQuartzSurface.cpp
new revision: 1.17; previous revision: 1.16
verified fixed on the 22.214.171.124 branch using Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:126.96.36.199pre) Gecko/2008070104 GranParadiso/3.0.1pre. I verified by loading the testcase and was not able to crash. The testcase still crashes on the trunk. Adding verified keyword.
Will check in the crash test when the bug is public.
Please use CVE-2008-2934 for this issue.
Can we open this bug now?
Landed the crash test on mozilla-central:
and CVS trunk:
The test unexpectedly caused an XError on Linux so I disabled
it for that platform for now (filed bug 455463).
verified fixed on the trunk using Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b1pre) Gecko/20080916020511 Minefield/3.1b1pre.