Unverified Commit 24cc0b98 authored by yiyuaner's avatar yiyuaner Committed by GitHub
Browse files

Fix integer overflow in _sdsMakeRoomFor (CVE-2021-41099) (#9558)

The existing overflow checks handled the greedy growing, but didn't handle
a case where the addition of the header size is what causes the overflow.
parent 5becb7c9
...@@ -239,7 +239,7 @@ void sdsclear(sds s) { ...@@ -239,7 +239,7 @@ void sdsclear(sds s) {
sds _sdsMakeRoomFor(sds s, size_t addlen, int greedy) { sds _sdsMakeRoomFor(sds s, size_t addlen, int greedy) {
void *sh, *newsh; void *sh, *newsh;
size_t avail = sdsavail(s); size_t avail = sdsavail(s);
size_t len, newlen; size_t len, newlen, reqlen;
char type, oldtype = s[-1] & SDS_TYPE_MASK; char type, oldtype = s[-1] & SDS_TYPE_MASK;
int hdrlen; int hdrlen;
size_t usable; size_t usable;
...@@ -249,7 +249,7 @@ sds _sdsMakeRoomFor(sds s, size_t addlen, int greedy) { ...@@ -249,7 +249,7 @@ sds _sdsMakeRoomFor(sds s, size_t addlen, int greedy) {
len = sdslen(s); len = sdslen(s);
sh = (char*)s-sdsHdrSize(oldtype); sh = (char*)s-sdsHdrSize(oldtype);
newlen = (len+addlen); reqlen = newlen = (len+addlen);
assert(newlen > len); /* Catch size_t overflow */ assert(newlen > len); /* Catch size_t overflow */
if (greedy == 1) { if (greedy == 1) {
if (newlen < SDS_MAX_PREALLOC) if (newlen < SDS_MAX_PREALLOC)
...@@ -266,7 +266,7 @@ sds _sdsMakeRoomFor(sds s, size_t addlen, int greedy) { ...@@ -266,7 +266,7 @@ sds _sdsMakeRoomFor(sds s, size_t addlen, int greedy) {
if (type == SDS_TYPE_5) type = SDS_TYPE_8; if (type == SDS_TYPE_5) type = SDS_TYPE_8;
hdrlen = sdsHdrSize(type); hdrlen = sdsHdrSize(type);
assert(hdrlen + newlen + 1 > len); /* Catch size_t overflow */ assert(hdrlen + newlen + 1 > reqlen); /* Catch size_t overflow */
if (oldtype==type) { if (oldtype==type) {
newsh = s_realloc_usable(sh, hdrlen+newlen+1, &usable); newsh = s_realloc_usable(sh, hdrlen+newlen+1, &usable);
if (newsh == NULL) return NULL; if (newsh == NULL) return NULL;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment