Gentoo Archives: gentoo-commits

From: Thomas Deutschmann <whissi@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] repo/gentoo:master commit in: dev-libs/json-c/files/, dev-libs/json-c/
Date: Thu, 14 May 2020 21:43:53
Message-Id: 1589492616.bff018bc9e26a181b25250edd90192b22736fd02.whissi@gentoo
1 commit: bff018bc9e26a181b25250edd90192b22736fd02
2 Author: Jakov Smolic <jakov.smolic <AT> sartura <DOT> hr>
3 AuthorDate: Tue May 12 14:58:39 2020 +0000
4 Commit: Thomas Deutschmann <whissi <AT> gentoo <DOT> org>
5 CommitDate: Thu May 14 21:43:36 2020 +0000
6 URL: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=bff018bc
7
8 dev-libs/json-c: fix security vulnerabilities
9
10 Prevent integer overflow and out of boundary write on malicious input.
11
12 Closes: https://bugs.gentoo.org/722150
13 Package-Manager: Portage-2.3.89, Repoman-2.3.20
14 Signed-off-by: Jakov Smolic <jakov.smolic <AT> sartura.hr>
15 Signed-off-by: Luka Perkov <luka.perkov <AT> sartura.hr>
16 Closes: https://github.com/gentoo/gentoo/pull/15767
17 Signed-off-by: Thomas Deutschmann <whissi <AT> gentoo.org>
18
19 .../json-c/files/json-c-0.14_security-fix.patch | 155 +++++++++++++++++++++
20 dev-libs/json-c/json-c-0.14-r2.ebuild | 53 +++++++
21 2 files changed, 208 insertions(+)
22
23 diff --git a/dev-libs/json-c/files/json-c-0.14_security-fix.patch b/dev-libs/json-c/files/json-c-0.14_security-fix.patch
24 new file mode 100644
25 index 00000000000..69a0bc75471
26 --- /dev/null
27 +++ b/dev-libs/json-c/files/json-c-0.14_security-fix.patch
28 @@ -0,0 +1,155 @@
29 +From 099016b7e8d70a6d5dd814e788bba08d33d48426 Mon Sep 17 00:00:00 2001
30 +From: Tobias Stoeckmann <tobias@××××××××××.org>
31 +Date: Mon, 4 May 2020 19:41:16 +0200
32 +Subject: [PATCH 1/3] Protect array_list_del_idx against size_t overflow.
33 +
34 +If the assignment of stop overflows due to idx and count being
35 +larger than SIZE_T_MAX in sum, out of boundary access could happen.
36 +
37 +It takes invalid usage of this function for this to happen, but
38 +I decided to add this check so array_list_del_idx is as safe against
39 +bad usage as the other arraylist functions.
40 +---
41 + arraylist.c | 3 +++
42 + 1 file changed, 3 insertions(+)
43 +
44 +diff --git a/arraylist.c b/arraylist.c
45 +index 12ad8af6d3..e5524aca75 100644
46 +--- a/arraylist.c
47 ++++ b/arraylist.c
48 +@@ -136,6 +136,9 @@ int array_list_del_idx(struct array_list *arr, size_t idx, size_t count)
49 + {
50 + size_t i, stop;
51 +
52 ++ /* Avoid overflow in calculation with large indices. */
53 ++ if (idx > SIZE_T_MAX - count)
54 ++ return -1;
55 + stop = idx + count;
56 + if (idx >= arr->length || stop > arr->length)
57 + return -1;
58 +
59 +From 77d935b7ae7871a1940cd827e850e6063044ec45 Mon Sep 17 00:00:00 2001
60 +From: Tobias Stoeckmann <tobias@××××××××××.org>
61 +Date: Mon, 4 May 2020 19:46:45 +0200
62 +Subject: [PATCH 2/3] Prevent division by zero in linkhash.
63 +
64 +If a linkhash with a size of zero is created, then modulo operations
65 +are prone to division by zero operations.
66 +
67 +Purely protective measure against bad usage.
68 +---
69 + linkhash.c | 3 +++
70 + 1 file changed, 3 insertions(+)
71 +
72 +diff --git a/linkhash.c b/linkhash.c
73 +index 7ea58c0abf..f05cc38030 100644
74 +--- a/linkhash.c
75 ++++ b/linkhash.c
76 +@@ -12,6 +12,7 @@
77 +
78 + #include "config.h"
79 +
80 ++#include <assert.h>
81 + #include <limits.h>
82 + #include <stdarg.h>
83 + #include <stddef.h>
84 +@@ -499,6 +500,8 @@ struct lh_table *lh_table_new(int size, lh_entry_free_fn *free_fn, lh_hash_fn *h
85 + int i;
86 + struct lh_table *t;
87 +
88 ++ /* Allocate space for elements to avoid divisions by zero. */
89 ++ assert(size > 0);
90 + t = (struct lh_table *)calloc(1, sizeof(struct lh_table));
91 + if (!t)
92 + return NULL;
93 +
94 +From d07b91014986900a3a75f306d302e13e005e9d67 Mon Sep 17 00:00:00 2001
95 +From: Tobias Stoeckmann <tobias@××××××××××.org>
96 +Date: Mon, 4 May 2020 19:47:25 +0200
97 +Subject: [PATCH 3/3] Fix integer overflows.
98 +
99 +The data structures linkhash and printbuf are limited to 2 GB in size
100 +due to a signed integer being used to track their current size.
101 +
102 +If too much data is added, then size variable can overflow, which is
103 +an undefined behaviour in C programming language.
104 +
105 +Assuming that a signed int overflow just leads to a negative value,
106 +like it happens on many sytems (Linux i686/amd64 with gcc), then
107 +printbuf is vulnerable to an out of boundary write on 64 bit systems.
108 +---
109 + linkhash.c | 7 +++++--
110 + printbuf.c | 19 ++++++++++++++++---
111 + 2 files changed, 21 insertions(+), 5 deletions(-)
112 +
113 +diff --git a/linkhash.c b/linkhash.c
114 +index f05cc38030..51e90b13a2 100644
115 +--- a/linkhash.c
116 ++++ b/linkhash.c
117 +@@ -580,9 +580,12 @@ int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v, con
118 + {
119 + unsigned long n;
120 +
121 +- if (t->count >= t->size * LH_LOAD_FACTOR)
122 +- if (lh_table_resize(t, t->size * 2) != 0)
123 ++ if (t->count >= t->size * LH_LOAD_FACTOR) {
124 ++ /* Avoid signed integer overflow with large tables. */
125 ++ int new_size = INT_MAX / 2 < t->size ? t->size * 2 : INT_MAX;
126 ++ if (t->size == INT_MAX || lh_table_resize(t, new_size) != 0)
127 + return -1;
128 ++ }
129 +
130 + n = h % t->size;
131 +
132 +diff --git a/printbuf.c b/printbuf.c
133 +index 976c12dde5..00822fac4f 100644
134 +--- a/printbuf.c
135 ++++ b/printbuf.c
136 +@@ -15,6 +15,7 @@
137 +
138 + #include "config.h"
139 +
140 ++#include <limits.h>
141 + #include <stdio.h>
142 + #include <stdlib.h>
143 + #include <string.h>
144 +@@ -65,10 +66,16 @@ static int printbuf_extend(struct printbuf *p, int min_size)
145 +
146 + if (p->size >= min_size)
147 + return 0;
148 +-
149 +- new_size = p->size * 2;
150 +- if (new_size < min_size + 8)
151 ++ /* Prevent signed integer overflows with large buffers. */
152 ++ if (min_size > INT_MAX - 8)
153 ++ return -1;
154 ++ if (p->size > INT_MAX / 2)
155 + new_size = min_size + 8;
156 ++ else {
157 ++ new_size = p->size * 2;
158 ++ if (new_size < min_size + 8)
159 ++ new_size = min_size + 8;
160 ++ }
161 + #ifdef PRINTBUF_DEBUG
162 + MC_DEBUG("printbuf_memappend: realloc "
163 + "bpos=%d min_size=%d old_size=%d new_size=%d\n",
164 +@@ -83,6 +90,9 @@ static int printbuf_extend(struct printbuf *p, int min_size)
165 +
166 + int printbuf_memappend(struct printbuf *p, const char *buf, int size)
167 + {
168 ++ /* Prevent signed integer overflows with large buffers. */
169 ++ if (size > INT_MAX - p->bpos - 1)
170 ++ return -1;
171 + if (p->size <= p->bpos + size + 1)
172 + {
173 + if (printbuf_extend(p, p->bpos + size + 1) < 0)
174 +@@ -100,6 +110,9 @@ int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
175 +
176 + if (offset == -1)
177 + offset = pb->bpos;
178 ++ /* Prevent signed integer overflows with large buffers. */
179 ++ if (len > INT_MAX - offset)
180 ++ return -1;
181 + size_needed = offset + len;
182 + if (pb->size < size_needed)
183 + {
184
185 diff --git a/dev-libs/json-c/json-c-0.14-r2.ebuild b/dev-libs/json-c/json-c-0.14-r2.ebuild
186 new file mode 100644
187 index 00000000000..19454d4c940
188 --- /dev/null
189 +++ b/dev-libs/json-c/json-c-0.14-r2.ebuild
190 @@ -0,0 +1,53 @@
191 +# Copyright 1999-2020 Gentoo Authors
192 +# Distributed under the terms of the GNU General Public License v2
193 +
194 +EAPI="7"
195 +
196 +CMAKE_ECLASS=cmake
197 +inherit cmake-multilib
198 +
199 +DESCRIPTION="A JSON implementation in C"
200 +HOMEPAGE="https://github.com/json-c/json-c/wiki"
201 +SRC_URI="https://s3.amazonaws.com/json-c_releases/releases/${P}.tar.gz"
202 +
203 +LICENSE="MIT"
204 +SLOT="0/5"
205 +KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sparc ~x86 ~amd64-linux ~x86-linux ~ppc-macos"
206 +IUSE="doc static-libs threads"
207 +
208 +PATCHES=(
209 + "${FILESDIR}/${P}_cmake-static-libs.patch"
210 + "${FILESDIR}/${P}_security-fix.patch"
211 +)
212 +
213 +MULTILIB_WRAPPED_HEADERS=(
214 + /usr/include/json-c/config.h
215 +)
216 +
217 +src_prepare() {
218 + cmake_src_prepare
219 +}
220 +
221 +multilib_src_configure() {
222 + local mycmakeargs=(
223 + -DBUILD_DOCUMENTATION=$(multilib_native_usex doc)
224 + -DBUILD_STATIC_LIBS=$(usex static-libs)
225 + -DDISABLE_WERROR=ON
226 + -DENABLE_THREADING=$(usex threads)
227 + )
228 +
229 + cmake_src_configure
230 +}
231 +
232 +multilib_src_compile() {
233 + cmake_src_compile
234 +}
235 +
236 +multilib_src_test() {
237 + multilib_is_native_abi && cmake_src_test
238 +}
239 +
240 +multilib_src_install_all() {
241 + use doc && HTML_DOCS=( "${S}"/doc/html/. )
242 + einstalldocs
243 +}