You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
CVE-2012-0876 (hash table collisions CPU usage DoS) for embedded copy of expat #58442
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/gpshead'closed_at=<Date2012-03-15.01:28:27.440>created_at=<Date2012-03-09.00:56:30.606>labels= ['expert-XML', 'release-blocker']
title='CVE-2012-0876 (hash table collisions CPU usage DoS) for embedded copy of expat'updated_at=<Date2012-03-18.19:40:10.000>user='https://github.com/davidmalcolm'
I'm attaching a patch which minimally backports the hash-collision fix part of expat 2.1.0 to the embedded copy of expat in the CPython source tree, and which adds a call to XML_SetHashSalt() to pyexpat when creating parsers. It reuses part of the hash secret from Py_HashSecret.
Oddly, test_sax fails once this patch is applied (using 3.1). debugging now.
test_sax
test test_sax failed -- Traceback (most recent call last):
File "/home/greg/sandbox/python/cpython/3.1/Lib/xml/sax/expatreader.py", line 207, in feedself._parser.Parse(data, isFinal)
xml.parsers.expat.ExpatError: unbound prefix: line 1, column 59
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/greg/sandbox/python/cpython/3.1/Lib/test/test_sax.py", line 310, in test_5027_1
parser.parse(test_xml)
File "/home/greg/sandbox/python/cpython/3.1/Lib/xml/sax/expatreader.py", line 107, in parse
xmlreader.IncrementalParser.parse(self, source)
File "/home/greg/sandbox/python/cpython/3.1/Lib/xml/sax/xmlreader.py", line 123, in parseself.feed(buffer)
File "/home/greg/sandbox/python/cpython/3.1/Lib/xml/sax/expatreader.py", line 211, in feedself._err_handler.fatalError(exc)
File "/home/greg/sandbox/python/cpython/3.1/Lib/xml/sax/handler.py", line 38, in fatalErrorraise exception
xml.sax._exceptions.SAXParseException: <unknown>:1:59: unbound prefix
uploaded an updated patch (against 3.1) with the changes from r1.168 to r1.170 xmlparse.c from the expat project. it fixes the test_sax issue.
there is one other thing that needs fixing (next patch update).
The test for the hash seed being == 0 that falls back to using the expat provided trivial time() based seed undesirable. We want a hash seed of 0 to be "disabled" matching the old behavior. this might require adding a flag indicating if the hash seed has been initialized or not.
I'm also going to look at the possibility of using the Python interpreter's prefix and suffix values in some way rather than just prefix to avoid a potential of exposing the seed.
one that sets the hash seed via the environment variable to a different value for two subprocesses that parse and re-emit an xml document to confirm that all of the xml attributes are present but emitted in a different order indicating that attribute hash randomization was in effect is needed.
The existing pyexpat API doesn't give me a way to test if hash randomization is actually working so I'm going ahead without a specific test case for this.
Attributes are either reported to xmlparser.SameElementHandler in a dictionary (unordered) or are reported in a list in the order they appeared on the element depending on the xmlparser.ordered_attributes bool.
I'm opening another issue to track updating the embedded copy of expat within Python.
FWIW, Python 2.7 & 3.2 and later support a --with-system-expat option which is what I'd *hope* that any OS distro is building their Python with rather than using the older out of date embedded copy of expat (which appears to be derived from expat 2.0.0).
Activity
davidmalcolm commentedon Mar 9, 2012
Expat 2.1.0 Beta was recently announced:
http://mail.libexpat.org/pipermail/expat-discuss/2012-March/002768.html
which contains (among other things) a fix for a hash-collision denial-of-service attack (CVE-2012-0876)
I'm attaching a patch which minimally backports the hash-collision fix part of expat 2.1.0 to the embedded copy of expat in the CPython source tree, and which adds a call to XML_SetHashSalt() to pyexpat when creating parsers. It reuses part of the hash secret from Py_HashSecret.
birkenfeld commentedon Mar 9, 2012
I hope this can be integrated during the PyCon sprints?
benjaminp commentedon Mar 9, 2012
Since this has been approved upstream and the Python change is minimal, I think this can just be applied.
pitrou commentedon Mar 9, 2012
Note sure I understand: XML_SetHashSalt() takes a parser argument, but the hash secret is global?
amauryfa commentedon Mar 9, 2012
No, the salt is stored on the parser. See the line:
+#define hash_secret_salt (parser->m_hash_secret_salt)
Yes, expat code is confusing.
gpshead commentedon Mar 12, 2012
reviewing now.
gpshead commentedon Mar 12, 2012
Oddly, test_sax fails once this patch is applied (using 3.1). debugging now.
Arfrever commentedon Mar 13, 2012
Maybe it's related to: https://sourceforge.net/tracker/?func=detail&aid=3500861&group_id=10127&atid=110127
(But I think that --with-system-expat should be recommended.)
gpshead commentedon Mar 13, 2012
sweet, thanks for the reference. that really looks like the problem.
gpshead commentedon Mar 14, 2012
uploaded an updated patch (against 3.1) with the changes from r1.168 to r1.170 xmlparse.c from the expat project. it fixes the test_sax issue.
there is one other thing that needs fixing (next patch update).
The test for the hash seed being == 0 that falls back to using the expat provided trivial time() based seed undesirable. We want a hash seed of 0 to be "disabled" matching the old behavior. this might require adding a flag indicating if the hash seed has been initialized or not.
I'm also going to look at the possibility of using the Python interpreter's prefix and suffix values in some way rather than just prefix to avoid a potential of exposing the seed.
gpshead commentedon Mar 14, 2012
A test case for this is also needed.
one that sets the hash seed via the environment variable to a different value for two subprocesses that parse and re-emit an xml document to confirm that all of the xml attributes are present but emitted in a different order indicating that attribute hash randomization was in effect is needed.
gpshead commentedon Mar 14, 2012
The existing pyexpat API doesn't give me a way to test if hash randomization is actually working so I'm going ahead without a specific test case for this.
Attributes are either reported to xmlparser.SameElementHandler in a dictionary (unordered) or are reported in a list in the order they appeared on the element depending on the xmlparser.ordered_attributes bool.
6 remaining items
python-dev commentedon Mar 15, 2012
New changeset 9c8d066013ea by Barry Warsaw in branch '2.6':
http://hg.python.org/cpython/rev/9c8d066013ea
gpshead commentedon Mar 15, 2012
One issue has been identified when compiling with --system-expat. if the system expat library does not have the hash salt support, compilation breaks.
fixing now.
gpshead commentedon Mar 15, 2012
configure --with-system-expat was introduced in 2.7 and 3.2 so 2.6 and 3.1 are good to go for release candidates.
patch tests are running now.
python-dev commentedon Mar 15, 2012
New changeset b2d4a6a9463e by Gregory P. Smith in branch '3.2':
Fixes bpo-14234: fix for the previous commit, keep compilation when
http://hg.python.org/cpython/rev/b2d4a6a9463e
New changeset db27b7353400 by Gregory P. Smith in branch 'default':
Fixes bpo-14234: fix for the previous commit, keep compilation when
http://hg.python.org/cpython/rev/db27b7353400
New changeset cb72aa8a8008 by Gregory P. Smith in branch '2.7':
Fixes bpo-14234: fix for the previous commit, keep compilation when
http://hg.python.org/cpython/rev/cb72aa8a8008
gpshead commentedon Mar 15, 2012
okay. it is time to cut the rc2 release candidates with these changes.
python-dev commentedon Mar 15, 2012
New changeset 04ff6e206b98 by Gregory P. Smith in branch '2.7':
Fixes Issue bpo-14234: CVE-2012-0876: Randomize hashes of xml attributes
http://hg.python.org/cpython/rev/04ff6e206b98
New changeset ada6bfbeceb8 by Gregory P. Smith in branch '2.7':
Fixes bpo-14234: fix for the previous commit, keep compilation when
http://hg.python.org/cpython/rev/ada6bfbeceb8
jimjjewett commentedon Mar 15, 2012
Looking at http://sourceforge.net/projects/expat/files/expat/2.1.0/, so long as XML_ATTR_INFO isn't defined at compile time, the changes are all considered bugfixes, and the XML_SetHashSalt is the only other changed API.
Is a potential Denial of Service really worse than a crash, such as these fixed bugs:
http://sourceforge.net/tracker/?func=detail&aid=2894085&group_id=10127&atid=110127
http://sourceforge.net/tracker/?func=detail&aid=1990430&group_id=10127&atid=110127
gpshead commentedon Mar 16, 2012
I'm opening another issue to track updating the embedded copy of expat within Python.
FWIW, Python 2.7 & 3.2 and later support a --with-system-expat option which is what I'd *hope* that any OS distro is building their Python with rather than using the older out of date embedded copy of expat (which appears to be derived from expat 2.0.0).
python-dev commentedon Mar 18, 2012
New changeset cf7337a49a07 by Georg Brandl in branch '3.2':
Transplant from main repo d6c197edd99b: Fixes Issue bpo-14234: CVE-2012-0876: Randomize hashes of xml attributes
http://hg.python.org/cpython/rev/cf7337a49a07
New changeset d54508a86a5d by Gregory P. Smith in branch '3.2':
Fixes bpo-14234: fix for the previous commit, keep compilation when
http://hg.python.org/cpython/rev/d54508a86a5d