Skip to content

Commit ea70712

Browse files
[3.10] gh-149018: Use XML_SetHashSalt16Bytes in pyexpat/_elementtree when possible (GH-149023) (#155611)
* [3.10] gh-149018: Use `XML_SetHashSalt16Bytes` in `pyexpat`/`_elementtree` when possible (GH-149023) (GH-155345) (cherry picked from commit e37df2a) (cherry picked from commit 95cf26d) Co-authored-by: Stan Ulbrych <stan@python.org> (cherry picked from commit fc9b11f) (cherry picked from commit 24b8f12) * Fix SetHashSalt16Bytes assignment in pyexpat.c
1 parent b0006c6 commit ea70712

5 files changed

Lines changed: 26 additions & 6 deletions

File tree

Include/pyexpat.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ struct PyExpat_CAPI
6262
XML_Parser parser, unsigned long long activationThresholdBytes);
6363
XML_Bool (*SetBillionLaughsAttackProtectionMaximumAmplification)(
6464
XML_Parser parser, float maxAmplificationFactor);
65+
/* might be NULL for expat < 2.8.0 */
66+
XML_Bool (*SetHashSalt16Bytes)(
67+
XML_Parser parser, const uint8_t entropy[16]);
6568
/* always add new stuff to the end! */
6669
};
6770

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
@@ -1453,7 +1453,10 @@ newxmlparseobject(pyexpat_state *state, const char *encoding,
14531453
Py_DECREF(self);
14541454
return NULL;
14551455
}
1456-
#if XML_COMBINED_VERSION >= 20100
1456+
#if XML_COMBINED_VERSION >= 20800
1457+
/* This feature was added upstream in libexpat 2.8.0. */
1458+
XML_SetHashSalt16Bytes(self->itself, _Py_HashSecret.expat.hashsalt16);
1459+
#elif XML_COMBINED_VERSION >= 20100
14571460
/* This feature was added upstream in libexpat 2.1.0. */
14581461
XML_SetHashSalt(self->itself,
14591462
(unsigned long)_Py_HashSecret.expat.hashsalt);
@@ -2261,6 +2264,11 @@ pyexpat_exec(PyObject *mod)
22612264
#else
22622265
capi.SetHashSalt = NULL;
22632266
#endif
2267+
#if XML_COMBINED_VERSION >= 20800
2268+
capi.SetHashSalt16Bytes = XML_SetHashSalt16Bytes;
2269+
#else
2270+
capi.SetHashSalt16Bytes = NULL;
2271+
#endif
22642272
#if XML_COMBINED_VERSION >= 20600
22652273
capi.SetReparseDeferralEnabled = XML_SetReparseDeferralEnabled;
22662274
#else

0 commit comments

Comments
 (0)