musl-1.1.15-CVE.patch 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. From c3edc06d1e1360f3570db9155d6b318ae0d0f0f7 Mon Sep 17 00:00:00 2001
  2. From: Rich Felker <dalias@aerifal.cx>
  3. Date: Thu, 06 Oct 2016 22:34:58 +0000
  4. Subject: fix missing integer overflow checks in regexec buffer size computations
  5. most of the possible overflows were already ruled out in practice by
  6. regcomp having already succeeded performing larger allocations.
  7. however at least the num_states*num_tags multiplication can clearly
  8. overflow in practice. for safety, check them all, and use the proper
  9. type, size_t, rather than int.
  10. also improve comments, use calloc in place of malloc+memset, and
  11. remove bogus casts.
  12. ---
  13. diff --git a/src/regex/regexec.c b/src/regex/regexec.c
  14. index 16c5d0a..dd52319 100644
  15. --- a/src/regex/regexec.c
  16. +++ b/src/regex/regexec.c
  17. @@ -34,6 +34,7 @@
  18. #include <wchar.h>
  19. #include <wctype.h>
  20. #include <limits.h>
  21. +#include <stdint.h>
  22. #include <regex.h>
  23. @@ -206,11 +207,24 @@ tre_tnfa_run_parallel(const tre_tnfa_t *tnfa, const void *string,
  24. /* Allocate memory for temporary data required for matching. This needs to
  25. be done for every matching operation to be thread safe. This allocates
  26. - everything in a single large block from the stack frame using alloca()
  27. - or with malloc() if alloca is unavailable. */
  28. + everything in a single large block with calloc(). */
  29. {
  30. - int tbytes, rbytes, pbytes, xbytes, total_bytes;
  31. + size_t tbytes, rbytes, pbytes, xbytes, total_bytes;
  32. char *tmp_buf;
  33. +
  34. + /* Ensure that tbytes and xbytes*num_states cannot overflow, and that
  35. + * they don't contribute more than 1/8 of SIZE_MAX to total_bytes. */
  36. + if (num_tags > SIZE_MAX/(8 * sizeof(int) * tnfa->num_states))
  37. + goto error_exit;
  38. +
  39. + /* Likewise check rbytes. */
  40. + if (tnfa->num_states+1 > SIZE_MAX/(8 * sizeof(*reach_next)))
  41. + goto error_exit;
  42. +
  43. + /* Likewise check pbytes. */
  44. + if (tnfa->num_states > SIZE_MAX/(8 * sizeof(*reach_pos)))
  45. + goto error_exit;
  46. +
  47. /* Compute the length of the block we need. */
  48. tbytes = sizeof(*tmp_tags) * num_tags;
  49. rbytes = sizeof(*reach_next) * (tnfa->num_states + 1);
  50. @@ -221,10 +235,9 @@ tre_tnfa_run_parallel(const tre_tnfa_t *tnfa, const void *string,
  51. + (rbytes + xbytes * tnfa->num_states) * 2 + tbytes + pbytes;
  52. /* Allocate the memory. */
  53. - buf = xmalloc((unsigned)total_bytes);
  54. + buf = calloc(total_bytes, 1);
  55. if (buf == NULL)
  56. return REG_ESPACE;
  57. - memset(buf, 0, (size_t)total_bytes);
  58. /* Get the various pointers within tmp_buf (properly aligned). */
  59. tmp_tags = (void *)buf;
  60. --
  61. cgit v0.9.0.3-65-g4555