undefined behavior results in negative allocation size
The XSLT format-number function calculates a buffer size using a value that can become infinity after multiplication, leading to an undefined integer cast and a negative allocation size. An attacker could supply a crafted XSLT stylesheet to trigger this, potentially causing a crash or memory corruption.
- Fixed in 20 days
- Paid in 25 days
- Disclosed after 20 days
Created attachment 8965481
moz_format_number_undefined_behaviour.xsl
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
Steps to reproduce:
I've audited the the xslt format-number function implementation and found a corner case the results in a negative bufsize allocation (which is sign extended and casted to size_t on a call to new array[]).
the bug lies in the file:
mozilla-central/source/dom/xslt/xslt/txFormatNumberFunctionCall.cpp+273
the buggy code is:
value = fabs(value) * multiplier;
// Prefix
nsAutoString res(prefix);
int bufsize;
if (value > 1)
bufsize = (int)log10(value) + 30;
else
bufsize = 1 + 30;
char* buf = new char[bufsize];
what happens is that the the value is checked earlier in the function to not be INF/-INF/NaN but is later multiplied by a possible multiplier that can cause it to turn into INF.
when that happens, log10(INF) returns INF which when casted to int which is undefined (will result on x86 as a large negative number) and later on will be used as a size for allocation.
steps to repro:
browse to: https://www.w3schools.com/xml/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex2
insert into the xsl table the text supplied in the attached file
Actual results:
(int)log10(value) is undefined when value is INF/-INF/NaN
the resulting value is used afterwards as an allocation initialization.
Expected results:
there should've been a check for invalid double values after the multiplication (or drop the multiplication altogether).
i'd recommend to drop the multiplication or add another test after multiplication.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The code checks for infinite or NaN values before a multiplication, but the multiplication can still produce infinity, and the subsequent cast of log10(infinity) to int is undefined, leading to a negative buffer size.
Impact
An attacker could cause a negative buffer allocation, which may lead to a crash or potentially exploitable memory corruption.
What the attacker needed
Victim visits a crafted XSLT page
The fix
The multiplication should be removed or an additional check for invalid double values should be added after the multiplication.
How it was found
- Browse to the provided XSLT testing page.
- Insert the supplied XSLT content into the XSL table.
- Observe the resulting behaviour (crash or undefined allocation).
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A number formatting function in a web browser's XSLT engine can be tricked into creating a buffer with a negative size. This happens because a value that was checked to be finite is later multiplied and becomes infinite, and converting infinity to an integer is undefined. The result is an unsafe memory allocation that could crash the browser or worse.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.