Skip to content

Commit d78dc96

Browse files
ppisarcxong
authored andcommitted
There was an integer overflow in yajl_buf_ensure_available() leading to allocating less memory than requested. Then data were written past the allocated heap buffer in yajl_buf_append(), the only caller of yajl_buf_ensure_available(). Another result of the overflow was an infinite loop without a return from yajl_buf_ensure_available(). yajl-ruby project, which bundles yajl, fixed it <brianmario/yajl-ruby#211> by checking for the integer overflow, fortifying buffer allocations, and report the failures to a caller. But then the caller yajl_buf_append() skips a memory write if yajl_buf_ensure_available() failed leading to a data corruption. A yajl fork mainter recommended calling memory allocation callbacks with the large memory request and let them to handle it. But that has the problem that it's not possible pass the overely large size to the callbacks. This patch catches the integer overflow and terminates the process with abort(). GHSA-jj47-x69x-mxrm Origin: ppisar/yajl@23cea2d Bug: lloyd/yajl#239 Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1040036
1 parent 2bd5857 commit d78dc96

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/cdogs/yajl/yajl_buf.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,17 @@ void yajl_buf_ensure_available(yajl_buf buf, size_t want)
4545

4646
need = buf->len;
4747

48-
while (want >= (need - buf->used)) need <<= 1;
48+
if (((buf->used > want) ? buf->used : want) > (size_t)(buf->used + want)) {
49+
/* We cannot allocate more memory than SIZE_MAX. */
50+
abort();
51+
}
52+
while (want >= (need - buf->used)) {
53+
if (need >= (size_t)((size_t)(-1)<<1)>>1) {
54+
/* need would overflow. */
55+
abort();
56+
}
57+
need <<= 1;
58+
}
4959

5060
if (need != buf->len) {
5161
buf->data = (unsigned char *) YA_REALLOC(buf->alloc, buf->data, need);

0 commit comments

Comments
 (0)