64-bit Memory corruption/Integer overflow in RegExp replace
Originally disclosed on Mozilla
- Fixed in 36 days
- Paid in 2.6 years
- Disclosed after 36 days
The 1.9.2 and 2.0 branches are vulnerable to an integer overflow when performing replace() with a regular expression on a 64-bit system with more than 4 GB of memory.
The attached script (js shell) demonstrates a segmentation fault caused by a memcpy gone wild. (WARNING: Do not run this if you have less than or exactly 4 GB of memory, this will consume ~ 4 GB because a string of length 2^31 is required which in UTF-16 is twice the length...).
I consider the vulnerability to be exploitable if the memory/architecture conditions are met because the snprintf should allow you arbitrary writes before the actual buffer with the proper amount of memory. Nevertheless, due to the amount of RAM an attack is not realistic. However, if there is some way to achieve some kind of optimized storage for repeating strings with the effect that the string has the given length but does not consume the full memory, then exploitation might get much easier. I don't know if there are such possibilities in the current implementation.
Here is a gdb backtrace:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6f4001b in memcpy () from /lib/libc.so.6
(gdb) bt
#0 0x00007ffff6f4001b in memcpy () from /lib/libc.so.6
#1 0x00000000005632a5 in DoReplace (cx=0xaa51c0, res=0xaaa130, rdata=..., chars=0x7ffaf6930010) at jsstr.cpp:2241
#2 0x0000000000563421 in ReplaceCallback (cx=0xaa51c0, res=0xaaa130, count=32768, p=0x7fffffffbad0) at jsstr.cpp:2268
#3 0x0000000000561ede in DoMatch (cx=0xaa51c0, res=0xaaa130, vp=0x7ffff6abf0a0, str=0x7ffff690f1a0, rep=..., callback=0x5632af <ReplaceCallback>, data=0x7fffffffbad0,
flags=REPLACE_ARGS) at jsstr.cpp:1847
#4 0x0000000000563e0c in str_replace_regexp (cx=0xaa51c0, argc=2, vp=0x7ffff6abf0a0, rdata=...) at jsstr.cpp:2436
#5 0x0000000000564683 in js::str_replace (cx=0xaa51c0, argc=2, vp=0x7ffff6abf0a0) at jsstr.cpp:2589
#6 0x00000000004c5368 in js::CallJSNative (cx=0xaa51c0, native=0x56421b <js::str_replace(JSContext*, unsigned int, js::Value*)>, argc=2, vp=0x7ffff6abf0a0)
at jscntxtinlines.h:684
#7 0x00000000006c28da in js::Interpret (cx=0xaa51c0, entryFrame=0x7ffff6abf048, inlineCallCount=0, interpMode=JSINTERP_NORMAL) at jsinterp.cpp:4748
#8 0x00000000004c14f7 in js::RunScript (cx=0xaa51c0, script=0xac6530, fp=0x7ffff6abf048) at jsinterp.cpp:657
#9 0x00000000004c2873 in js::Execute (cx=0xaa51c0, chain=0x7ffff6903048, script=0xac6530, prev=0x0, flags=0, result=0x0) at jsinterp.cpp:1005
#10 0x000000000042cdd1 in JS_ExecuteScript (cx=0xaa51c0, obj=0x7ffff6903048, script=0xac6530, rval=0x0) at jsapi.cpp:4828
#11 0x000000000040524f in Process (cx=0xaa51c0, obj=0x7ffff6903048, filename=0x7fffffffdcda "min.js", forceTTY=0) at js.cpp:453
#12 0x0000000000406048 in ProcessArgs (cx=0xaa51c0, obj=0x7ffff6903048, argv=0x7fffffffd9a0, argc=2) at js.cpp:871
#13 0x000000000040f3a6 in Shell (cx=0xaa51c0, argc=2, argv=0x7fffffffd9a0, envp=0x7fffffffd9b8) at js.cpp:5370
#14 0x000000000040f56c in main (argc=2, argv=0x7fffffffd9a0, envp=0x7fffffffd9b8) at js.cpp:5478
If you need me to run further debug commands on this, let me know (I have the necessary hardware available).
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A memory corruption bug in JavaScript Engine. Mozilla made it public on 14 January 2011.
About 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.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.