CVE-2018-17942.patch 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. From e91600a7aae3bafbefbe13abf771e61badd16286 Mon Sep 17 00:00:00 2001
  2. From: Changqing Li <changqing.li@windriver.com>
  3. Date: Tue, 16 Oct 2018 14:26:11 +0800
  4. Subject: [PATCH] vasnprintf: Fix heap memory overrun bug.
  5. Reported by Ben Pfaff <blp@cs.stanford.edu> in
  6. <https://lists.gnu.org/archive/html/bug-gnulib/2018-09/msg00107.html>.
  7. * lib/vasnprintf.c (convert_to_decimal): Allocate one more byte of
  8. memory.
  9. * tests/test-vasnprintf.c (test_function): Add another test.
  10. Upstream-Status: Backport [http://git.savannah.gnu.org/gitweb/?p=gnulib.git;
  11. a=commitdiff;h=278b4175c9d7dd47c1a3071554aac02add3b3c35]
  12. CVE: CVE-2018-17942
  13. Signed-off-by: Changqing Li <changqing.li@windriver.com>
  14. ---
  15. ChangeLog | 8 ++++++++
  16. lib/vasnprintf.c | 4 +++-
  17. tests/test-vasnprintf.c | 19 ++++++++++++++++++-
  18. 3 files changed, 29 insertions(+), 2 deletions(-)
  19. diff --git a/ChangeLog b/ChangeLog
  20. index 9864353..5ff76a3 100644
  21. --- a/ChangeLog
  22. +++ b/ChangeLog
  23. @@ -1,3 +1,11 @@
  24. +2018-09-23 Bruno Haible <bruno@clisp.org>
  25. + vasnprintf: Fix heap memory overrun bug.
  26. + Reported by Ben Pfaff <blp@cs.stanford.edu> in
  27. + <https://lists.gnu.org/archive/html/bug-gnulib/2018-09/msg00107.html>.
  28. + * lib/vasnprintf.c (convert_to_decimal): Allocate one more byte of
  29. + memory.
  30. + * tests/test-vasnprintf.c (test_function): Add another test.
  31. +
  32. 2017-08-21 Paul Eggert <eggert@cs.ucla.edu>
  33. vc-list-files: port to Solaris 10
  34. diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c
  35. index 2e4eb19..45de49f 100644
  36. --- a/lib/vasnprintf.c
  37. +++ b/lib/vasnprintf.c
  38. @@ -860,7 +860,9 @@ convert_to_decimal (mpn_t a, size_t extra_zeroes)
  39. size_t a_len = a.nlimbs;
  40. /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
  41. size_t c_len = 9 * ((size_t)(a_len * (GMP_LIMB_BITS * 0.03345f)) + 1);
  42. - char *c_ptr = (char *) malloc (xsum (c_len, extra_zeroes));
  43. + /* We need extra_zeroes bytes for zeroes, followed by c_len bytes for the
  44. + digits of a, followed by 1 byte for the terminating NUL. */
  45. + char *c_ptr = (char *) malloc (xsum (xsum (extra_zeroes, c_len), 1));
  46. if (c_ptr != NULL)
  47. {
  48. char *d_ptr = c_ptr;
  49. diff --git a/tests/test-vasnprintf.c b/tests/test-vasnprintf.c
  50. index 2dd869f..ff68d5c 100644
  51. --- a/tests/test-vasnprintf.c
  52. +++ b/tests/test-vasnprintf.c
  53. @@ -53,7 +53,24 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
  54. ASSERT (result != NULL);
  55. ASSERT (strcmp (result, "12345") == 0);
  56. ASSERT (length == 5);
  57. - if (size < 6)
  58. + if (size < 5 + 1)
  59. + ASSERT (result != buf);
  60. + ASSERT (memcmp (buf + size, &"DEADBEEF"[size], 8 - size) == 0);
  61. + if (result != buf)
  62. + free (result);
  63. + }
  64. + /* Note: This test assumes IEEE 754 representation of 'double' floats. */
  65. + for (size = 0; size <= 8; size++)
  66. + {
  67. + size_t length;
  68. + char *result;
  69. + memcpy (buf, "DEADBEEF", 8);
  70. + length = size;
  71. + result = my_asnprintf (buf, &length, "%2.0f", 1.6314159265358979e+125);
  72. + ASSERT (result != NULL);
  73. + ASSERT (strcmp (result, "163141592653589790215729350939528493057529598899734151772468186268423257777068536614838678161083520756952076273094236944990208") == 0);
  74. + ASSERT (length == 126);
  75. + if (size < 126 + 1)
  76. ASSERT (result != buf);
  77. ASSERT (memcmp (buf + size, &"DEADBEEF"[size], 8 - size) == 0);
  78. if (result != buf)
  79. --
  80. 2.7.4