Skip to content

Commit 8667817

Browse files
botovqgpshead
authored andcommitted
00392: CVE-2022-37454: Fix buffer overflows in _sha3 module
This is a port of the applicable part of XKCP's fix [1] for CVE-2022-37454 and avoids the segmentation fault and the infinite loop in the test cases published in [2]. [1]: XKCP/XKCP@fdc6fef [2]: https://mouha.be/sha-3-buffer-overflow/ (cherry picked from commit 0e4e058) Co-authored-by: Gregory P. Smith [Google LLC] <[email protected]>
1 parent b030834 commit 8667817

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

Lib/test/test_hashlib.py

+9
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,15 @@ def test_case_md5_huge(self, size):
418418
def test_case_md5_uintmax(self, size):
419419
self.check('md5', b'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3')
420420

421+
@unittest.skipIf(sys.maxsize < _4G - 1, 'test cannot run on 32-bit systems')
422+
@bigmemtest(size=_4G - 1, memuse=1, dry_run=False)
423+
def test_sha3_update_overflow(self, size):
424+
"""Regression test for gh-98517 CVE-2022-37454."""
425+
h = hashlib.sha3_224()
426+
h.update(b'\x01')
427+
h.update(b'\x01'*0xffff_ffff)
428+
self.assertEqual(h.hexdigest(), '80762e8ce6700f114fec0f621fd97c4b9c00147fa052215294cceeed')
429+
421430
# use the three examples from Federal Information Processing Standards
422431
# Publication 180-1, Secure Hash Standard, 1995 April 17
423432
# http://www.itl.nist.gov/div897/pubs/fip180-1.htm
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port XKCP's fix for the buffer overflows in SHA-3 (CVE-2022-37454).

Modules/_sha3/kcp/KeccakSponge.inc

+8-7
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ int SpongeAbsorb(SpongeInstance *instance, const unsigned char *data, size_t dat
171171
i = 0;
172172
curData = data;
173173
while(i < dataByteLen) {
174-
if ((instance->byteIOIndex == 0) && (dataByteLen >= (i + rateInBytes))) {
174+
if ((instance->byteIOIndex == 0) && (dataByteLen-i >= rateInBytes)) {
175175
#ifdef SnP_FastLoop_Absorb
176176
/* processing full blocks first */
177177

@@ -199,10 +199,10 @@ int SpongeAbsorb(SpongeInstance *instance, const unsigned char *data, size_t dat
199199
}
200200
else {
201201
/* normal lane: using the message queue */
202-
203-
partialBlock = (unsigned int)(dataByteLen - i);
204-
if (partialBlock+instance->byteIOIndex > rateInBytes)
202+
if (dataByteLen-i > rateInBytes-instance->byteIOIndex)
205203
partialBlock = rateInBytes-instance->byteIOIndex;
204+
else
205+
partialBlock = (unsigned int)(dataByteLen - i);
206206
#ifdef KeccakReference
207207
displayBytes(1, "Block to be absorbed (part)", curData, partialBlock);
208208
#endif
@@ -281,7 +281,7 @@ int SpongeSqueeze(SpongeInstance *instance, unsigned char *data, size_t dataByte
281281
i = 0;
282282
curData = data;
283283
while(i < dataByteLen) {
284-
if ((instance->byteIOIndex == rateInBytes) && (dataByteLen >= (i + rateInBytes))) {
284+
if ((instance->byteIOIndex == rateInBytes) && (dataByteLen-i >= rateInBytes)) {
285285
for(j=dataByteLen-i; j>=rateInBytes; j-=rateInBytes) {
286286
SnP_Permute(instance->state);
287287
SnP_ExtractBytes(instance->state, curData, 0, rateInBytes);
@@ -299,9 +299,10 @@ int SpongeSqueeze(SpongeInstance *instance, unsigned char *data, size_t dataByte
299299
SnP_Permute(instance->state);
300300
instance->byteIOIndex = 0;
301301
}
302-
partialBlock = (unsigned int)(dataByteLen - i);
303-
if (partialBlock+instance->byteIOIndex > rateInBytes)
302+
if (dataByteLen-i > rateInBytes-instance->byteIOIndex)
304303
partialBlock = rateInBytes-instance->byteIOIndex;
304+
else
305+
partialBlock = (unsigned int)(dataByteLen - i);
305306
i += partialBlock;
306307

307308
SnP_ExtractBytes(instance->state, curData, instance->byteIOIndex, partialBlock);

0 commit comments

Comments
 (0)