Skip to content

Commit 2dbcc53

Browse files
[3.12] gh-149018: Use XML_SetHashSalt16Bytes in pyexpat/_elementtree when possible (GH-149023)
(cherry picked from commit fc9b11f) Co-authored-by: Stan Ulbrych <stan@python.org> (cherry picked from commit 24b8f12)
1 parent 063d455 commit 2dbcc53

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

Modules/pyexpat.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,10 @@ newxmlparseobject(pyexpat_state *state, const char *encoding,
14691469
Py_DECREF(self);
14701470
return NULL;
14711471
}
1472-
#if XML_COMBINED_VERSION >= 20100
1472+
#if XML_COMBINED_VERSION >= 20800
1473+
/* This feature was added upstream in libexpat 2.8.0. */
1474+
XML_SetHashSalt16Bytes(self->itself, _Py_HashSecret.expat.hashsalt16);
1475+
#elif XML_COMBINED_VERSION >= 20100
14731476
/* This feature was added upstream in libexpat 2.1.0. */
14741477
XML_SetHashSalt(self->itself,
14751478
(unsigned long)_Py_HashSecret.expat.hashsalt);
@@ -2338,6 +2341,11 @@ pyexpat_exec(PyObject *mod)
23382341
#else
23392342
capi->SetHashSalt = NULL;
23402343
#endif
2344+
#if XML_COMBINED_VERSION >= 20800
2345+
capi->SetHashSalt16Bytes = XML_SetHashSalt16Bytes;
2346+
#else
2347+
capi->SetHashSalt16Bytes = NULL;
2348+
#endif
23412349
#if XML_COMBINED_VERSION >= 20600
23422350
capi->SetReparseDeferralEnabled = XML_SetReparseDeferralEnabled;
23432351
#else

0 commit comments

Comments
 (0)