blob: 3c0d6622966dc781846533d2332a2b3b16378e78 [file] [log] [blame]
Brad Bishop6f8dcde2018-10-16 10:47:12 +08001From 3ffc80959f01f9fde548f1632694b9f950c2dd7c Mon Sep 17 00:00:00 2001
2From: Christian Heimes <christian@python.org>
3Date: Tue, 18 Sep 2018 15:13:09 +0200
4Subject: [PATCH] [2.7] bpo-34623: Use XML_SetHashSalt in _elementtree
5 (GH-9146) (GH-9394)
6
7The C accelerated _elementtree module now initializes hash randomization
8salt from _Py_HashSecret instead of libexpat's default CPRNG.
9
10Signed-off-by: Christian Heimes <christian@python.org>
11
12https://bugs.python.org/issue34623.
13(cherry picked from commit cb5778f00ce48631c7140f33ba242496aaf7102b)
14
15Co-authored-by: Christian Heimes <christian@python.org>
16
17
18
19https://bugs.python.org/issue34623
20
21Upstream-Status: Backport
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080022CVE: CVE-2018-14647
Brad Bishop6f8dcde2018-10-16 10:47:12 +080023Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
24---
25 Include/pyexpat.h | 4 +++-
26 Misc/NEWS.d/next/Security/2018-09-10-16-05-39.bpo-34623.Ua9jMv.rst | 2 ++
27 Modules/_elementtree.c | 5 +++++
28 Modules/pyexpat.c | 5 +++++
29 4 files changed, 15 insertions(+), 1 deletion(-)
30 create mode 100644 Misc/NEWS.d/next/Security/2018-09-10-16-05-39.bpo-34623.Ua9jMv.rst
31
32diff --git a/Include/pyexpat.h b/Include/pyexpat.h
33index 5340ef5..3fc5fa5 100644
34--- a/Include/pyexpat.h
35+++ b/Include/pyexpat.h
36@@ -3,7 +3,7 @@
37
38 /* note: you must import expat.h before importing this module! */
39
40-#define PyExpat_CAPI_MAGIC "pyexpat.expat_CAPI 1.0"
41+#define PyExpat_CAPI_MAGIC "pyexpat.expat_CAPI 1.1"
42 #define PyExpat_CAPSULE_NAME "pyexpat.expat_CAPI"
43
44 struct PyExpat_CAPI
45@@ -43,6 +43,8 @@ struct PyExpat_CAPI
46 XML_Parser parser, XML_UnknownEncodingHandler handler,
47 void *encodingHandlerData);
48 void (*SetUserData)(XML_Parser parser, void *userData);
49+ /* might be none for expat < 2.1.0 */
50+ int (*SetHashSalt)(XML_Parser parser, unsigned long hash_salt);
51 /* always add new stuff to the end! */
52 };
53
54diff --git a/Misc/NEWS.d/next/Security/2018-09-10-16-05-39.bpo-34623.Ua9jMv.rst b/Misc/NEWS.d/next/Security/2018-09-10-16-05-39.bpo-34623.Ua9jMv.rst
55new file mode 100644
56index 0000000..31ad92e
57--- /dev/null
58+++ b/Misc/NEWS.d/next/Security/2018-09-10-16-05-39.bpo-34623.Ua9jMv.rst
59@@ -0,0 +1,2 @@
60+The C accelerated _elementtree module now initializes hash randomization
61+salt from _Py_HashSecret instead of libexpat's default CSPRNG.
62diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
63index 1d316a1..a19cbf7 100644
64--- a/Modules/_elementtree.c
65+++ b/Modules/_elementtree.c
66@@ -2574,6 +2574,11 @@ xmlparser(PyObject* self_, PyObject* args, PyObject* kw)
67 PyErr_NoMemory();
68 return NULL;
69 }
70+ /* expat < 2.1.0 has no XML_SetHashSalt() */
71+ if (EXPAT(SetHashSalt) != NULL) {
72+ EXPAT(SetHashSalt)(self->parser,
73+ (unsigned long)_Py_HashSecret.prefix);
74+ }
75
76 ALLOC(sizeof(XMLParserObject), "create expatparser");
77
78diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
79index 2b4d312..1f8c0d7 100644
80--- a/Modules/pyexpat.c
81+++ b/Modules/pyexpat.c
82@@ -2042,6 +2042,11 @@ MODULE_INITFUNC(void)
83 capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
84 capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
85 capi.SetUserData = XML_SetUserData;
86+#if XML_COMBINED_VERSION >= 20100
87+ capi.SetHashSalt = XML_SetHashSalt;
88+#else
89+ capi.SetHashSalt = NULL;
90+#endif
91
92 /* export using capsule */
93 capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
94--
952.7.4
96