Skip to content

Commit 41b48e7

Browse files
stratakistiran
authored andcommitted
[3.5] bpo-34623: Use XML_SetHashSalt in _elementtree (#9933)
* bpo-34623: Use XML_SetHashSalt in _elementtree (GH-9146) The C accelerated _elementtree module now initializes hash randomization salt from _Py_HashSecret instead of libexpat's default CPRNG. Signed-off-by: Christian Heimes <[email protected]> https://bugs.python.org/issue34623 (cherry picked from commit cb5778f) Co-authored-by: Christian Heimes <[email protected]>
1 parent 1a2b52b commit 41b48e7

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

Include/pyexpat.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/* note: you must import expat.h before importing this module! */
55

6-
#define PyExpat_CAPI_MAGIC "pyexpat.expat_CAPI 1.0"
6+
#define PyExpat_CAPI_MAGIC "pyexpat.expat_CAPI 1.1"
77
#define PyExpat_CAPSULE_NAME "pyexpat.expat_CAPI"
88

99
struct PyExpat_CAPI
@@ -48,6 +48,8 @@ struct PyExpat_CAPI
4848
enum XML_Status (*SetEncoding)(XML_Parser parser, const XML_Char *encoding);
4949
int (*DefaultUnknownEncodingHandler)(
5050
void *encodingHandlerData, const XML_Char *name, XML_Encoding *info);
51+
/* might be none for expat < 2.1.0 */
52+
int (*SetHashSalt)(XML_Parser parser, unsigned long hash_salt);
5153
/* always add new stuff to the end! */
5254
};
5355

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CVE-2018-14647: The C accelerated _elementtree module now initializes hash
2+
randomization salt from _Py_HashSecret instead of libexpat's default CSPRNG.

Modules/_elementtree.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3282,6 +3282,11 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *html,
32823282
PyErr_NoMemory();
32833283
return -1;
32843284
}
3285+
/* expat < 2.1.0 has no XML_SetHashSalt() */
3286+
if (EXPAT(SetHashSalt) != NULL) {
3287+
EXPAT(SetHashSalt)(self->parser,
3288+
(unsigned long)_Py_HashSecret.expat.hashsalt);
3289+
}
32853290

32863291
if (target) {
32873292
Py_INCREF(target);

Modules/pyexpat.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,11 @@ MODULE_INITFUNC(void)
18821882
capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
18831883
capi.SetEncoding = XML_SetEncoding;
18841884
capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
1885+
#if XML_COMBINED_VERSION >= 20100
1886+
capi.SetHashSalt = XML_SetHashSalt;
1887+
#else
1888+
capi.SetHashSalt = NULL;
1889+
#endif
18851890

18861891
/* export using capsule */
18871892
capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);

0 commit comments

Comments
 (0)