Skip to content

Commit fa94761

Browse files
StanFromIrelandtomcruiseqi
authored andcommitted
[3.10] gh-149018: Use XML_SetHashSalt16Bytes in pyexpat/_elementtree when possible (GH-149023)
(cherry picked from commit 24b8f12)
1 parent b286d98 commit fa94761

5 files changed

Lines changed: 26 additions & 7 deletions

File tree

Include/pyexpat.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ struct PyExpat_CAPI
5757
XML_Parser parser, unsigned long long activationThresholdBytes);
5858
XML_Bool (*SetAllocTrackerMaximumAmplification)(
5959
XML_Parser parser, float maxAmplificationFactor);
60+
/* might be NULL for expat < 2.8.0 */
61+
XML_Bool (*SetHashSalt16Bytes)(
62+
XML_Parser parser, const uint8_t entropy[16]);
6063
/* always add new stuff to the end! */
6164
};
62-

Include/pyhash.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ PyAPI_FUNC(Py_hash_t) _Py_HashBytes(const void*, Py_ssize_t);
3939
* pppppppp ssssssss ........ fnv -- two Py_hash_t
4040
* k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t
4141
* ........ ........ ssssssss djbx33a -- 16 bytes padding + one Py_hash_t
42-
* ........ ........ eeeeeeee pyexpat XML hash salt
42+
* eeeeeeee eeeeeeee eeeeeeee pyexpat XML hash salt
4343
*
4444
* memory layout on 32 bit systems
4545
* cccccccc cccccccc cccccccc uc
4646
* ppppssss ........ ........ fnv -- two Py_hash_t
4747
* k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t (*)
4848
* ........ ........ ssss.... djbx33a -- 16 bytes padding + one Py_hash_t
49-
* ........ ........ eeee.... pyexpat XML hash salt
49+
* eeeeeeee eeeeeeee eeee.... pyexpat XML hash salt
5050
*
5151
* (*) The siphash member may not be available on 32 bit platforms without
5252
* an unsigned int64 data type.
@@ -71,7 +71,9 @@ typedef union {
7171
Py_hash_t suffix;
7272
} djbx33a;
7373
struct {
74-
unsigned char padding[16];
74+
/* 16 bytes for XML_SetHashSalt16Bytes */
75+
uint8_t hashsalt16[16];
76+
/* 4/8 bytes for legacy XML_SetHashSalt */
7577
Py_hash_t hashsalt;
7678
} expat;
7779
} _Py_HashSecret_t;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improved protection against XML hash-flooding attacks in
2+
:mod:`xml.parsers.expat` and :mod:`xml.etree.ElementTree` when Python is
3+
compiled with libExpat 2.8.0 or later.

Modules/_elementtree.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3663,8 +3663,12 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *target,
36633663
PyErr_NoMemory();
36643664
return -1;
36653665
}
3666-
/* expat < 2.1.0 has no XML_SetHashSalt() */
3667-
if (EXPAT(SetHashSalt) != NULL) {
3666+
// Prefer 16-byte entropy, only expat >= 2.8.0. See gh-149018
3667+
if (EXPAT(SetHashSalt16Bytes) != NULL) {
3668+
EXPAT(SetHashSalt16Bytes)(self->parser,
3669+
_Py_HashSecret.expat.hashsalt16);
3670+
}
3671+
else if (EXPAT(SetHashSalt) != NULL) {
36683672
EXPAT(SetHashSalt)(self->parser,
36693673
(unsigned long)_Py_HashSecret.expat.hashsalt);
36703674
}

Modules/pyexpat.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,10 @@ newxmlparseobject(pyexpat_state *state, const char *encoding,
13791379
Py_DECREF(self);
13801380
return NULL;
13811381
}
1382-
#if XML_COMBINED_VERSION >= 20100
1382+
#if XML_COMBINED_VERSION >= 20800
1383+
/* This feature was added upstream in libexpat 2.8.0. */
1384+
XML_SetHashSalt16Bytes(self->itself, _Py_HashSecret.expat.hashsalt16);
1385+
#elif XML_COMBINED_VERSION >= 20100
13831386
/* This feature was added upstream in libexpat 2.1.0. */
13841387
XML_SetHashSalt(self->itself,
13851388
(unsigned long)_Py_HashSecret.expat.hashsalt);
@@ -2199,6 +2202,11 @@ pyexpat_exec(PyObject *mod)
21992202
capi.SetAllocTrackerActivationThreshold = NULL;
22002203
capi.SetAllocTrackerMaximumAmplification = NULL;
22012204
#endif
2205+
#if XML_COMBINED_VERSION >= 20800
2206+
capi.SetHashSalt16Bytes = XML_SetHashSalt16Bytes;
2207+
#else
2208+
capi.SetHashSalt16Bytes = NULL;
2209+
#endif
22022210

22032211
/* export using capsule */
22042212
PyObject *capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);

0 commit comments

Comments
 (0)