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