Skip to content

Commit 95cf26d

Browse files
[3.11] gh-149018: Use XML_SetHashSalt16Bytes in pyexpat/_elementtree when possible (GH-149023) (GH-155345)
(cherry picked from commit e37df2a) Co-authored-by: Stan Ulbrych <stan@python.org> (cherry picked from commit fc9b11f) (cherry picked from commit 24b8f12)
1 parent 0f54a85 commit 95cf26d

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
@@ -3655,8 +3655,12 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *target,
36553655
PyErr_NoMemory();
36563656
return -1;
36573657
}
3658-
/* expat < 2.1.0 has no XML_SetHashSalt() */
3659-
if (EXPAT(SetHashSalt) != NULL) {
3658+
// Prefer 16-byte entropy, only expat >= 2.8.0. See gh-149018
3659+
if (EXPAT(SetHashSalt16Bytes) != NULL) {
3660+
EXPAT(SetHashSalt16Bytes)(self->parser,
3661+
_Py_HashSecret.expat.hashsalt16);
3662+
}
3663+
else if (EXPAT(SetHashSalt) != NULL) {
36603664
EXPAT(SetHashSalt)(self->parser,
36613665
(unsigned long)_Py_HashSecret.expat.hashsalt);
36623666
}

Modules/pyexpat.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,10 @@ newxmlparseobject(pyexpat_state *state, const char *encoding,
14591459
Py_DECREF(self);
14601460
return NULL;
14611461
}
1462-
#if XML_COMBINED_VERSION >= 20100
1462+
#if XML_COMBINED_VERSION >= 20800
1463+
/* This feature was added upstream in libexpat 2.8.0. */
1464+
XML_SetHashSalt16Bytes(self->itself, _Py_HashSecret.expat.hashsalt16);
1465+
#elif XML_COMBINED_VERSION >= 20100
14631466
/* This feature was added upstream in libexpat 2.1.0. */
14641467
XML_SetHashSalt(self->itself,
14651468
(unsigned long)_Py_HashSecret.expat.hashsalt);
@@ -2310,6 +2313,11 @@ pyexpat_exec(PyObject *mod)
23102313
#else
23112314
capi.SetHashSalt = NULL;
23122315
#endif
2316+
#if XML_COMBINED_VERSION >= 20800
2317+
capi->SetHashSalt16Bytes = XML_SetHashSalt16Bytes;
2318+
#else
2319+
capi->SetHashSalt16Bytes = NULL;
2320+
#endif
23132321
#if XML_COMBINED_VERSION >= 20600
23142322
capi.SetReparseDeferralEnabled = XML_SetReparseDeferralEnabled;
23152323
#else

0 commit comments

Comments
 (0)