Closed Bug 372309 Opened 14 years ago Closed 14 years ago

Crash in [@SetArrayElement] using canvas

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
critical

Tracking

()

VERIFIED FIXED

People

(Reporter: sharparrow1, Assigned: crowderbt)

References

Details

(Keywords: fixed1.8.0.15, testcase, verified1.8.1.8, Whiteboard: [sg:critical?])

Attachments

(3 files, 6 obsolete files)

Attached file Testcase (not simplified) (obsolete) —
WARNING: Frame IP not in any known module. Following frames may be wrong.
0xcdcdcd20
js3250!SetArrayElement(struct JSContext * cx = 0x02d80d58, struct JSObject * obj = 0x03afa5c0, unsigned long index = 0x12e5dc, long v = 511)+0x5e [c:\mozilla\mozilla\js\src\jsarray.c @ 299]
js3250!InitArrayElements(struct JSContext * cx = 0x000c57d1, struct JSObject * obj = 0x03afa5c0, unsigned long start = 0x62be8, unsigned long end = 0x9c400, long * vector = 0x03afae9c)+0x39 [c:\mozilla\mozilla\js\src\jsarray.c @ 738]
js3250!InitArrayObject(struct JSContext * cx = 0x03afae9c, struct JSObject * obj = 0x03afa5c0, unsigned long length = 0x9c400, long * vector = 0x08490040)+0x61 [c:\mozilla\mozilla\js\src\jsarray.c @ 762]
js3250!js_NewArrayObject(struct JSContext * cx = 0x002c5dd7, unsigned long length = 0xff000000, long * vector = 0x0012e7e4)+0x31 [c:\mozilla\mozilla\js\src\jsarray.c @ 1997]
gklayout!nsCanvasRenderingContext2D::GetImageData(void)+0x2fc [c:\mozilla\mozilla\content\canvas\src\nscanvasrenderingcontext2d.cpp @ 3077]
xpcom_core!NS_InvokeByIndex(class nsISupports * that = 0x063ff9c8, unsigned int methodIndex = 0x37, unsigned int paramCount = 0, struct nsXPTCVariant * params = 0x0012e6b8)+0x27 [c:\mozilla\mozilla\xpcom\reflect\xptcall\src\md\win32\xptcinvoke.cpp @ 102]
xpc3250!XPCWrappedNative::CallMethod(class XPCCallContext * ccx = 0x00b78aa8, XPCWrappedNative::CallMode mode = 12028584 (No matching enumerant))+0x7f2 [c:\mozilla\mozilla\js\src\xpconnect\src\xpcwrappednative.cpp @ 2251]

Seems like an evil enough crash that it might be worth filing as a security bug.

To reproduce, load up two images into the testcase (don't know of any images off the top of my head on b.m.o to refer to; you can save the testcase and put the urls of two local files in.)  Then, click the compare button.  Then, click it again.  Watch it explode.  (Testcase could probably be reduced more, but I haven't done it yet.)
Hmm, should have simplified before posting originally... pretty simple :(
Attachment #256958 - Attachment is obsolete: true
Attachment #256961 - Attachment description: Simplified testcase → Simplified testcase (WARNING: CRASHES INSTANTLY)
bz thinks, and I agree, that this is a problem of the new array object not having a root and InitArrayElements CHECK_OPERATION_LIMIT potentially yielding a GC (or many other) operations which smash the array's newborn root (if there ever is one in this case??).  I'll take a stab at a patch...

Since it is a GC-hazard, I feel that it most certainly should be considered a critical security bug.
Assignee: nobody → crowder
Severity: normal → critical
Component: Layout: Canvas → JavaScript Engine
Whiteboard: [sg:critical?]
Trunk-only, though.

/be
Blocks: 309926
Blocks: 310405
No longer blocks: 309926
Attached patch temp root for array object (obsolete) — Splinter Review
Not sure if this is correct, but seems to be the common technique.  Whoever the array object is passed to will be responsible for rooting it again.
Attachment #256968 - Flags: review?(brendan)
(In reply to comment #4)
> Created an attachment (id=256968) [details]
> temp root for array object
> 
> Not sure if this is correct, but seems to be the common technique.  Whoever the
> array object is passed to will be responsible for rooting it again.
> 

For complete safety it is necessary to put obj into newborn array to restore invariant that the callback can break.
Attached patch better behavior (obsolete) — Splinter Review
Thanks for the feedback, Igor.
Attachment #256968 - Attachment is obsolete: true
Attachment #256975 - Flags: review?(igor)
Attachment #256968 - Flags: review?(brendan)
Comment on attachment 256975 [details] [diff] [review]
better behavior

> JSObject *
> js_NewArrayObject(JSContext *cx, jsuint length, jsval *vector)
> {
...
>+    tvr.u.value = OBJECT_TO_JSVAL(obj);
>     if (!InitArrayObject(cx, obj, length, vector)) {
>         cx->weakRoots.newborn[GCX_OBJECT] = NULL;
>         return NULL;
>     }
>+    JS_POP_TEMP_ROOT(cx, &tvr);
>+
>+    /* Set newborn root, in case we lost it.  */
>+    cx->weakRoots.newborn[GCX_OBJECT] = obj;
>     return obj;

Nit: the if (!InitArrayObject) part can be written as:

if (!InitArrayObject(cx, obj, length, vector))
  obj = NULL;
Attachment #256975 - Flags: review?(igor) → review+
Comment on attachment 256975 [details] [diff] [review]
better behavior

>+    JS_PUSH_TEMP_ROOT_OBJECT(cx, obj, &tvr);
>+    tvr.u.value = OBJECT_TO_JSVAL(obj);
>     if (!InitArrayObject(cx, obj, length, vector)) {
>         cx->weakRoots.newborn[GCX_OBJECT] = NULL;
>         return NULL;

Non-nit: this will leave tvr pushed.
Attachment #256975 - Flags: review+ → review-
Attached patch igor's nit (obsolete) — Splinter Review
This is the patch I will land this evening, barring further feedback.  Thanks, Igor.
Attachment #256975 - Attachment is obsolete: true
Attachment #256976 - Flags: review+
Attached patch tweak to kill a compiler warning (obsolete) — Splinter Review
Attachment #256976 - Attachment is obsolete: true
Attachment #256977 - Flags: review+
Attached patch with the right patch this time (obsolete) — Splinter Review
Sorry for the bugspam
Attachment #256977 - Attachment is obsolete: true
Attachment #256978 - Flags: review+
(In reply to comment #10)
> Created an attachment (id=256977) [details]
> tweak to kill a compiler warning
> 

The diff shows that this patch and the previous are the same.
Attachment #256978 - Flags: review+
The last patch doesn't have the additional issue of leaving tvr pushed, either.
Status: NEW → ASSIGNED
Comment on attachment 256978 [details] [diff] [review]
with the right patch this time

>+    JS_PUSH_TEMP_ROOT_OBJECT(cx, obj, &tvr);
>+    tvr.u.value = OBJECT_TO_JSVAL(obj);

The macro sets u.value for you.

>+    if (!InitArrayObject(cx, obj, length, vector))
>+        obj = NULL;
>+    JS_POP_TEMP_ROOT(cx, &tvr);
>+
>+    /* Set/clear newborn root, in case we lost it.  */
>+    cx->weakRoots.newborn[GCX_OBJECT] = (JSGCThing *) obj;

Need to get rid of newborn roots, but that's another bug (on Igor's list IIRC).

/be
Attached patch trust the macro!Splinter Review
ugh, I went and found that macro so I could get rid of that line then didn't get rid of the line.  Juggling too many patches today.  Thanks, Brendan
Attachment #256978 - Attachment is obsolete: true
Attachment #256989 - Flags: review+
Flags: blocking1.9?
Whiteboard: [sg:critical?] → [sg:critical?] 1.9-only
jsarray.c: 3.107
Status: ASSIGNED → RESOLVED
Closed: 14 years ago
Resolution: --- → FIXED
Flags: in-testsuite+
QA Contact: layout.canvas → general
verified fixed 1.9.0 20070320 win/mac*/linux
Status: RESOLVED → VERIFIED
Flags: wanted1.8.1.x-
Flags: wanted1.8.0.x-
Whiteboard: [sg:critical?] 1.9-only → [sg:critical?] post 1.8-branch
Group: security
/cvsroot/mozilla/js/tests/js1_5/extensions/regress-372309.js,v  <--  regress-372309.js
initial revision: 1.1
The bug exists on 1.8.1 branch, for details see bug 360701.
Blocks: 360701
Flags: blocking1.8.0.14?
Whiteboard: [sg:critical?] post 1.8-branch → [sg:critical?]
Comment on attachment 256989 [details] [diff] [review]
trust the macro!

The patch must go to 1.8.1 branch to fix the GC hazard there.
Attachment #256989 - Flags: approval1.8.1.8?
Flags: blocking1.8.0.14? → blocking1.8.1.8?
Flags: blocking1.8.1.8? → blocking1.8.1.8+
Comment on attachment 256989 [details] [diff] [review]
trust the macro!

approved for 1.8.1.8, a=dveditz for release-drivers
Attachment #256989 - Flags: approval1.8.1.8? → approval1.8.1.8+
I checked in the patch from comment 15 to MOZILLA_1_8_BRANCH:

Checking in jsarray.c;
/cvsroot/mozilla/js/src/jsarray.c,v  <--  jsarray.c
new revision: 3.58.2.26; previous revision: 3.58.2.25
done
Keywords: fixed1.8.1.8
verified fixed 1.8.1.8 using the testcase from comment #1 and Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.8pre) Gecko/2007092903 BonEcho/2.0.0.8pre on Fedora F7 and Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.8pre) Gecko/20070927 BonEcho/2.0.0.8pre ID:2007092704 - no crash on testcase

-> Adding verified keyword
Comment on attachment 256989 [details] [diff] [review]
trust the macro!

a=asac for 1.8.0.15
Attachment #256989 - Flags: approval1.8.0.15+
please land on 1.8.0 branch.
Flags: blocking1.8.0.15+
Keywords: checkin-needed
MOZILLA_1_8_0_BRANCH:

Checking in js/src/jsarray.c;
/cvsroot/mozilla/js/src/jsarray.c,v  <--  jsarray.c
new revision: 3.58.2.10.2.13; previous revision: 3.58.2.10.2.12
done
You need to log in before you can comment on or make changes to this bug.