Skip to content

Commit 24b8f12

Browse files
gh-149018: Use XML_SetHashSalt16Bytes in pyexpat/_elementtree when possible (#149023)
1 parent bc1be4f commit 24b8f12

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
@@ -27,14 +27,14 @@ _Py_HashPointerRaw(const void *ptr)
2727
* pppppppp ssssssss ........ fnv -- two Py_hash_t
2828
* k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t
2929
* ........ ........ ssssssss djbx33a -- 16 bytes padding + one Py_hash_t
30-
* ........ ........ eeeeeeee pyexpat XML hash salt
30+
* eeeeeeee eeeeeeee eeeeeeee pyexpat XML hash salt
3131
*
3232
* memory layout on 32 bit systems
3333
* cccccccc cccccccc cccccccc uc
3434
* ppppssss ........ ........ fnv -- two Py_hash_t
3535
* k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t (*)
3636
* ........ ........ ssss.... djbx33a -- 16 bytes padding + one Py_hash_t
37-
* ........ ........ eeee.... pyexpat XML hash salt
37+
* eeeeeeee eeeeeeee eeee.... pyexpat XML hash salt
3838
*
3939
* (*) The siphash member may not be available on 32 bit platforms without
4040
* an unsigned int64 data type.
@@ -58,7 +58,9 @@ typedef union {
5858
Py_hash_t suffix;
5959
} djbx33a;
6060
struct {
61-
unsigned char padding[16];
61+
/* 16 bytes for XML_SetHashSalt16Bytes */
62+
uint8_t hashsalt16[16];
63+
/* 4/8 bytes for legacy XML_SetHashSalt */
6264
Py_hash_t hashsalt;
6365
} expat;
6466
} _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
@@ -3735,8 +3735,12 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *target,
37353735
PyErr_NoMemory();
37363736
return -1;
37373737
}
3738-
/* expat < 2.1.0 has no XML_SetHashSalt() */
3739-
if (EXPAT(st, SetHashSalt) != NULL) {
3738+
// Prefer 16-byte entropy, only expat >= 2.8.0. See gh-149018
3739+
if (EXPAT(st, SetHashSalt16Bytes) != NULL) {
3740+
EXPAT(st, SetHashSalt16Bytes)(self->parser,
3741+
_Py_HashSecret.expat.hashsalt16);
3742+
}
3743+
else if (EXPAT(st, SetHashSalt) != NULL) {
37403744
EXPAT(st, SetHashSalt)(self->parser,
37413745
(unsigned long)_Py_HashSecret.expat.hashsalt);
37423746
}

Modules/pyexpat.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,10 @@ newxmlparseobject(pyexpat_state *state, const char *encoding,
15331533
Py_DECREF(self);
15341534
return NULL;
15351535
}
1536-
#if XML_COMBINED_VERSION >= 20100
1536+
#if XML_COMBINED_VERSION >= 20800
1537+
/* This feature was added upstream in libexpat 2.8.0. */
1538+
XML_SetHashSalt16Bytes(self->itself, _Py_HashSecret.expat.hashsalt16);
1539+
#elif XML_COMBINED_VERSION >= 20100
15371540
/* This feature was added upstream in libexpat 2.1.0. */
15381541
XML_SetHashSalt(self->itself,
15391542
(unsigned long)_Py_HashSecret.expat.hashsalt);
@@ -2427,6 +2430,11 @@ pyexpat_exec(PyObject *mod)
24272430
#else
24282431
capi->SetHashSalt = NULL;
24292432
#endif
2433+
#if XML_COMBINED_VERSION >= 20800
2434+
capi->SetHashSalt16Bytes = XML_SetHashSalt16Bytes;
2435+
#else
2436+
capi->SetHashSalt16Bytes = NULL;
2437+
#endif
24302438
#if XML_COMBINED_VERSION >= 20600
24312439
capi->SetReparseDeferralEnabled = XML_SetReparseDeferralEnabled;
24322440
#else

0 commit comments

Comments
 (0)