Skip to content

Commit fc9b11f

Browse files
[3.13] gh-149018: Use XML_SetHashSalt16Bytes in pyexpat/_elementtree when possible (GH-149023)
(cherry picked from commit 24b8f12)
1 parent 19bc391 commit fc9b11f

5 files changed

Lines changed: 26 additions & 6 deletions

File tree

Include/internal/pycore_pyhash.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ PyAPI_FUNC(Py_hash_t) _Py_HashBytes(const void*, Py_ssize_t);
3030
* pppppppp ssssssss ........ fnv -- two Py_hash_t
3131
* k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t
3232
* ........ ........ ssssssss djbx33a -- 16 bytes padding + one Py_hash_t
33-
* ........ ........ eeeeeeee pyexpat XML hash salt
33+
* eeeeeeee eeeeeeee eeeeeeee pyexpat XML hash salt
3434
*
3535
* memory layout on 32 bit systems
3636
* cccccccc cccccccc cccccccc uc
3737
* ppppssss ........ ........ fnv -- two Py_hash_t
3838
* k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t (*)
3939
* ........ ........ ssss.... djbx33a -- 16 bytes padding + one Py_hash_t
40-
* ........ ........ eeee.... pyexpat XML hash salt
40+
* eeeeeeee eeeeeeee eeee.... pyexpat XML hash salt
4141
*
4242
* (*) The siphash member may not be available on 32 bit platforms without
4343
* an unsigned int64 data type.
@@ -61,7 +61,9 @@ typedef union {
6161
Py_hash_t suffix;
6262
} djbx33a;
6363
struct {
64-
unsigned char padding[16];
64+
/* 16 bytes for XML_SetHashSalt16Bytes */
65+
uint8_t hashsalt16[16];
66+
/* 4/8 bytes for legacy XML_SetHashSalt */
6567
Py_hash_t hashsalt;
6668
} expat;
6769
} _Py_HashSecret_t;

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

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
@@ -3696,8 +3696,12 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *target,
36963696
PyErr_NoMemory();
36973697
return -1;
36983698
}
3699-
/* expat < 2.1.0 has no XML_SetHashSalt() */
3700-
if (EXPAT(st, SetHashSalt) != NULL) {
3699+
// Prefer 16-byte entropy, only expat >= 2.8.0. See gh-149018
3700+
if (EXPAT(st, SetHashSalt16Bytes) != NULL) {
3701+
EXPAT(st, SetHashSalt16Bytes)(self->parser,
3702+
_Py_HashSecret.expat.hashsalt16);
3703+
}
3704+
else if (EXPAT(st, SetHashSalt) != NULL) {
37013705
EXPAT(st, SetHashSalt)(self->parser,
37023706
(unsigned long)_Py_HashSecret.expat.hashsalt);
37033707
}

Modules/pyexpat.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,10 @@ newxmlparseobject(pyexpat_state *state, const char *encoding,
14611461
Py_DECREF(self);
14621462
return NULL;
14631463
}
1464-
#if XML_COMBINED_VERSION >= 20100
1464+
#if XML_COMBINED_VERSION >= 20800
1465+
/* This feature was added upstream in libexpat 2.8.0. */
1466+
XML_SetHashSalt16Bytes(self->itself, _Py_HashSecret.expat.hashsalt16);
1467+
#elif XML_COMBINED_VERSION >= 20100
14651468
/* This feature was added upstream in libexpat 2.1.0. */
14661469
XML_SetHashSalt(self->itself,
14671470
(unsigned long)_Py_HashSecret.expat.hashsalt);
@@ -2323,6 +2326,11 @@ pyexpat_exec(PyObject *mod)
23232326
#else
23242327
capi->SetHashSalt = NULL;
23252328
#endif
2329+
#if XML_COMBINED_VERSION >= 20800
2330+
capi->SetHashSalt16Bytes = XML_SetHashSalt16Bytes;
2331+
#else
2332+
capi->SetHashSalt16Bytes = NULL;
2333+
#endif
23262334
#if XML_COMBINED_VERSION >= 20600
23272335
capi->SetReparseDeferralEnabled = XML_SetReparseDeferralEnabled;
23282336
#else

0 commit comments

Comments
 (0)