CVE-2020-12762.patch 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. From 099016b7e8d70a6d5dd814e788bba08d33d48426 Mon Sep 17 00:00:00 2001
  2. From: Tobias Stoeckmann <tobias@stoeckmann.org>
  3. Date: Mon, 4 May 2020 19:41:16 +0200
  4. Subject: [PATCH 1/3] Protect array_list_del_idx against size_t overflow.
  5. If the assignment of stop overflows due to idx and count being
  6. larger than SIZE_T_MAX in sum, out of boundary access could happen.
  7. It takes invalid usage of this function for this to happen, but
  8. I decided to add this check so array_list_del_idx is as safe against
  9. bad usage as the other arraylist functions.
  10. Upstream-Status: Backport [https://github.com/json-c/json-c/commit/31243e4d1204ef78be34b0fcae73221eee6b83be]
  11. CVE: CVE-2020-12762
  12. Signed-off-by: Chee Yang Lee <chee.yang.lee@intel.com>
  13. ---
  14. arraylist.c | 3 +++
  15. 1 file changed, 3 insertions(+)
  16. diff --git a/arraylist.c b/arraylist.c
  17. index 12ad8af6d3..e5524aca75 100644
  18. --- a/arraylist.c
  19. +++ b/arraylist.c
  20. @@ -136,6 +136,9 @@ int array_list_del_idx(struct array_list *arr, size_t idx, size_t count)
  21. {
  22. size_t i, stop;
  23. + /* Avoid overflow in calculation with large indices. */
  24. + if (idx > SIZE_T_MAX - count)
  25. + return -1;
  26. stop = idx + count;
  27. if (idx >= arr->length || stop > arr->length)
  28. return -1;
  29. From 77d935b7ae7871a1940cd827e850e6063044ec45 Mon Sep 17 00:00:00 2001
  30. From: Tobias Stoeckmann <tobias@stoeckmann.org>
  31. Date: Mon, 4 May 2020 19:46:45 +0200
  32. Subject: [PATCH 2/3] Prevent division by zero in linkhash.
  33. If a linkhash with a size of zero is created, then modulo operations
  34. are prone to division by zero operations.
  35. Purely protective measure against bad usage.
  36. ---
  37. linkhash.c | 3 +++
  38. 1 file changed, 3 insertions(+)
  39. diff --git a/linkhash.c b/linkhash.c
  40. index 7ea58c0abf..f05cc38030 100644
  41. --- a/linkhash.c
  42. +++ b/linkhash.c
  43. @@ -12,6 +12,7 @@
  44. #include "config.h"
  45. +#include <assert.h>
  46. #include <limits.h>
  47. #include <stdarg.h>
  48. #include <stddef.h>
  49. @@ -499,6 +500,8 @@ struct lh_table *lh_table_new(int size, lh_entry_free_fn *free_fn, lh_hash_fn *h
  50. int i;
  51. struct lh_table *t;
  52. + /* Allocate space for elements to avoid divisions by zero. */
  53. + assert(size > 0);
  54. t = (struct lh_table *)calloc(1, sizeof(struct lh_table));
  55. if (!t)
  56. return NULL;
  57. From d07b91014986900a3a75f306d302e13e005e9d67 Mon Sep 17 00:00:00 2001
  58. From: Tobias Stoeckmann <tobias@stoeckmann.org>
  59. Date: Mon, 4 May 2020 19:47:25 +0200
  60. Subject: [PATCH 3/3] Fix integer overflows.
  61. The data structures linkhash and printbuf are limited to 2 GB in size
  62. due to a signed integer being used to track their current size.
  63. If too much data is added, then size variable can overflow, which is
  64. an undefined behaviour in C programming language.
  65. Assuming that a signed int overflow just leads to a negative value,
  66. like it happens on many sytems (Linux i686/amd64 with gcc), then
  67. printbuf is vulnerable to an out of boundary write on 64 bit systems.
  68. ---
  69. linkhash.c | 7 +++++--
  70. printbuf.c | 19 ++++++++++++++++---
  71. 2 files changed, 21 insertions(+), 5 deletions(-)
  72. diff --git a/linkhash.c b/linkhash.c
  73. index f05cc38030..51e90b13a2 100644
  74. --- a/linkhash.c
  75. +++ b/linkhash.c
  76. @@ -580,9 +580,12 @@ int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v, con
  77. {
  78. unsigned long n;
  79. - if (t->count >= t->size * LH_LOAD_FACTOR)
  80. - if (lh_table_resize(t, t->size * 2) != 0)
  81. + if (t->count >= t->size * LH_LOAD_FACTOR) {
  82. + /* Avoid signed integer overflow with large tables. */
  83. + int new_size = INT_MAX / 2 < t->size ? t->size * 2 : INT_MAX;
  84. + if (t->size == INT_MAX || lh_table_resize(t, new_size) != 0)
  85. return -1;
  86. + }
  87. n = h % t->size;
  88. diff --git a/printbuf.c b/printbuf.c
  89. index 976c12dde5..00822fac4f 100644
  90. --- a/printbuf.c
  91. +++ b/printbuf.c
  92. @@ -15,6 +15,7 @@
  93. #include "config.h"
  94. +#include <limits.h>
  95. #include <stdio.h>
  96. #include <stdlib.h>
  97. #include <string.h>
  98. @@ -65,10 +66,16 @@ static int printbuf_extend(struct printbuf *p, int min_size)
  99. if (p->size >= min_size)
  100. return 0;
  101. -
  102. - new_size = p->size * 2;
  103. - if (new_size < min_size + 8)
  104. + /* Prevent signed integer overflows with large buffers. */
  105. + if (min_size > INT_MAX - 8)
  106. + return -1;
  107. + if (p->size > INT_MAX / 2)
  108. new_size = min_size + 8;
  109. + else {
  110. + new_size = p->size * 2;
  111. + if (new_size < min_size + 8)
  112. + new_size = min_size + 8;
  113. + }
  114. #ifdef PRINTBUF_DEBUG
  115. MC_DEBUG("printbuf_memappend: realloc "
  116. "bpos=%d min_size=%d old_size=%d new_size=%d\n",
  117. @@ -83,6 +90,9 @@ static int printbuf_extend(struct printbuf *p, int min_size)
  118. int printbuf_memappend(struct printbuf *p, const char *buf, int size)
  119. {
  120. + /* Prevent signed integer overflows with large buffers. */
  121. + if (size > INT_MAX - p->bpos - 1)
  122. + return -1;
  123. if (p->size <= p->bpos + size + 1)
  124. {
  125. if (printbuf_extend(p, p->bpos + size + 1) < 0)
  126. @@ -100,6 +110,9 @@ int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
  127. if (offset == -1)
  128. offset = pb->bpos;
  129. + /* Prevent signed integer overflows with large buffers. */
  130. + if (len > INT_MAX - offset)
  131. + return -1;
  132. size_needed = offset + len;
  133. if (pb->size < size_needed)
  134. {