2
1

0034-3-10-gh-98517-Fix-buffer-overflows-in-_sha3-module.patch 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. From 0e4e058602d93b88256ff90bbef501ba20be9dd3 Mon Sep 17 00:00:00 2001
  2. From: Theo Buehler <botovq@users.noreply.github.com>
  3. Date: Fri, 21 Oct 2022 21:26:01 +0200
  4. Subject: [PATCH] [3.10] gh-98517: Fix buffer overflows in _sha3 module
  5. (#98519)
  6. This is a port of the applicable part of XKCP's fix [1] for
  7. CVE-2022-37454 and avoids the segmentation fault and the infinite
  8. loop in the test cases published in [2].
  9. [1]: https://github.com/XKCP/XKCP/commit/fdc6fef075f4e81d6b1bc38364248975e08e340a
  10. [2]: https://mouha.be/sha-3-buffer-overflow/
  11. Regression test added by: Gregory P. Smith [Google LLC] <greg@krypto.org>
  12. [Retrieved from:
  13. https://github.com/python/cpython/commit/0e4e058602d93b88256ff90bbef501ba20be9dd3]
  14. Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
  15. ---
  16. Lib/test/test_hashlib.py | 9 +++++++++
  17. .../2022-10-21-13-31-47.gh-issue-98517.SXXGfV.rst | 1 +
  18. Modules/_sha3/kcp/KeccakSponge.inc | 15 ++++++++-------
  19. 3 files changed, 18 insertions(+), 7 deletions(-)
  20. create mode 100644 Misc/NEWS.d/next/Security/2022-10-21-13-31-47.gh-issue-98517.SXXGfV.rst
  21. diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py
  22. index 535f4aa3e04c..9aa6c1f0a3b6 100644
  23. --- a/Lib/test/test_hashlib.py
  24. +++ b/Lib/test/test_hashlib.py
  25. @@ -495,6 +495,15 @@ def test_case_md5_huge(self, size):
  26. def test_case_md5_uintmax(self, size):
  27. self.check('md5', b'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3')
  28. + @unittest.skipIf(sys.maxsize < _4G - 1, 'test cannot run on 32-bit systems')
  29. + @bigmemtest(size=_4G - 1, memuse=1, dry_run=False)
  30. + def test_sha3_update_overflow(self, size):
  31. + """Regression test for gh-98517 CVE-2022-37454."""
  32. + h = hashlib.sha3_224()
  33. + h.update(b'\x01')
  34. + h.update(b'\x01'*0xffff_ffff)
  35. + self.assertEqual(h.hexdigest(), '80762e8ce6700f114fec0f621fd97c4b9c00147fa052215294cceeed')
  36. +
  37. # use the three examples from Federal Information Processing Standards
  38. # Publication 180-1, Secure Hash Standard, 1995 April 17
  39. # http://www.itl.nist.gov/div897/pubs/fip180-1.htm
  40. diff --git a/Misc/NEWS.d/next/Security/2022-10-21-13-31-47.gh-issue-98517.SXXGfV.rst b/Misc/NEWS.d/next/Security/2022-10-21-13-31-47.gh-issue-98517.SXXGfV.rst
  41. new file mode 100644
  42. index 000000000000..2d23a6ad93c7
  43. --- /dev/null
  44. +++ b/Misc/NEWS.d/next/Security/2022-10-21-13-31-47.gh-issue-98517.SXXGfV.rst
  45. @@ -0,0 +1 @@
  46. +Port XKCP's fix for the buffer overflows in SHA-3 (CVE-2022-37454).
  47. diff --git a/Modules/_sha3/kcp/KeccakSponge.inc b/Modules/_sha3/kcp/KeccakSponge.inc
  48. index e10739deafa8..cf92e4db4d36 100644
  49. --- a/Modules/_sha3/kcp/KeccakSponge.inc
  50. +++ b/Modules/_sha3/kcp/KeccakSponge.inc
  51. @@ -171,7 +171,7 @@ int SpongeAbsorb(SpongeInstance *instance, const unsigned char *data, size_t dat
  52. i = 0;
  53. curData = data;
  54. while(i < dataByteLen) {
  55. - if ((instance->byteIOIndex == 0) && (dataByteLen >= (i + rateInBytes))) {
  56. + if ((instance->byteIOIndex == 0) && (dataByteLen-i >= rateInBytes)) {
  57. #ifdef SnP_FastLoop_Absorb
  58. /* processing full blocks first */
  59. @@ -199,10 +199,10 @@ int SpongeAbsorb(SpongeInstance *instance, const unsigned char *data, size_t dat
  60. }
  61. else {
  62. /* normal lane: using the message queue */
  63. -
  64. - partialBlock = (unsigned int)(dataByteLen - i);
  65. - if (partialBlock+instance->byteIOIndex > rateInBytes)
  66. + if (dataByteLen-i > rateInBytes-instance->byteIOIndex)
  67. partialBlock = rateInBytes-instance->byteIOIndex;
  68. + else
  69. + partialBlock = (unsigned int)(dataByteLen - i);
  70. #ifdef KeccakReference
  71. displayBytes(1, "Block to be absorbed (part)", curData, partialBlock);
  72. #endif
  73. @@ -281,7 +281,7 @@ int SpongeSqueeze(SpongeInstance *instance, unsigned char *data, size_t dataByte
  74. i = 0;
  75. curData = data;
  76. while(i < dataByteLen) {
  77. - if ((instance->byteIOIndex == rateInBytes) && (dataByteLen >= (i + rateInBytes))) {
  78. + if ((instance->byteIOIndex == rateInBytes) && (dataByteLen-i >= rateInBytes)) {
  79. for(j=dataByteLen-i; j>=rateInBytes; j-=rateInBytes) {
  80. SnP_Permute(instance->state);
  81. SnP_ExtractBytes(instance->state, curData, 0, rateInBytes);
  82. @@ -299,9 +299,10 @@ int SpongeSqueeze(SpongeInstance *instance, unsigned char *data, size_t dataByte
  83. SnP_Permute(instance->state);
  84. instance->byteIOIndex = 0;
  85. }
  86. - partialBlock = (unsigned int)(dataByteLen - i);
  87. - if (partialBlock+instance->byteIOIndex > rateInBytes)
  88. + if (dataByteLen-i > rateInBytes-instance->byteIOIndex)
  89. partialBlock = rateInBytes-instance->byteIOIndex;
  90. + else
  91. + partialBlock = (unsigned int)(dataByteLen - i);
  92. i += partialBlock;
  93. SnP_ExtractBytes(instance->state, curData, instance->byteIOIndex, partialBlock);