Skip to content

Commit d16eaf3

Browse files
stratakistiran
authored andcommitted
[3.4] bpo-34623: Use XML_SetHashSalt in _elementtree (#9953)
* 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 cd1d5c5 commit d16eaf3

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
@@ -3259,6 +3259,11 @@ xmlparser_init(PyObject *self, PyObject *args, PyObject *kwds)
32593259
PyErr_NoMemory();
32603260
return -1;
32613261
}
3262+
/* expat < 2.1.0 has no XML_SetHashSalt() */
3263+
if (EXPAT(SetHashSalt) != NULL) {
3264+
EXPAT(SetHashSalt)(self_xp->parser,
3265+
(unsigned long)_Py_HashSecret.expat.hashsalt);
3266+
}
32623267

32633268
if (target) {
32643269
Py_INCREF(target);

Modules/pyexpat.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,11 @@ MODULE_INITFUNC(void)
18571857
capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
18581858
capi.SetEncoding = XML_SetEncoding;
18591859
capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
1860+
#if XML_COMBINED_VERSION >= 20100
1861+
capi.SetHashSalt = XML_SetHashSalt;
1862+
#else
1863+
capi.SetHashSalt = NULL;
1864+
#endif
18601865

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

0 commit comments

Comments
 (0)