Skip to content

Commit 28fa43d

Browse files
bnoordhuisFabrice Bellard
and
Fabrice Bellard
authored
Fix buffer overflows in the string and BigInt deserializer caused by a missing length check and bad size calculation respectively. Fixes: #1018 Co-authored-by: Fabrice Bellard <[email protected]>
1 parent 29a2f97 commit 28fa43d

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

quickjs.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36257,6 +36257,10 @@ static JSString *JS_ReadString(BCReaderState *s)
3625736257
return NULL;
3625836258
is_wide_char = len & 1;
3625936259
len >>= 1;
36260+
if (len > JS_STRING_LEN_MAX) {
36261+
JS_ThrowInternalError(s->ctx, "string too long");
36262+
return NULL;
36263+
}
3626036264
p = js_alloc_string(s->ctx, len, is_wide_char);
3626136265
if (!p) {
3626236266
s->error_state = -1;
@@ -36368,8 +36372,7 @@ static JSValue JS_ReadBigInt(BCReaderState *s)
3636836372
bc_read_trace(s, "}\n");
3636936373
return __JS_NewShortBigInt(s->ctx, 0);
3637036374
}
36371-
p = js_bigint_new(s->ctx,
36372-
(len + (JS_LIMB_BITS / 8) - 1) / (JS_LIMB_BITS / 8));
36375+
p = js_bigint_new(s->ctx, (len - 1) / (JS_LIMB_BITS / 8) + 1);
3637336376
if (!p)
3637436377
goto fail;
3637536378
for(i = 0; i < len / (JS_LIMB_BITS / 8); i++) {

tests/test_bjson.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,17 @@ function bjson_test_bytecode()
285285
function bjson_test_fuzz()
286286
{
287287
var corpus = [
288-
"EBAAAAAABGA=",
289-
"EObm5oIt",
290-
"EAARABMGBgYGBgYGBgYGBv////8QABEALxH/vy8R/78=",
288+
"FBAAAAAABGA=",
289+
"FObm5oIt",
290+
"FAARABMGBgYGBgYGBgYGBv////8QABEALxH/vy8R/78=",
291+
"FAAIfwAK/////3//////////////////////////////3/8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGAAAAAAAAAAAAAAD5+fn5+fn5+fn5+fkAAAAAAAYAqw==",
291292
];
292293
for (var input of corpus) {
293294
var buf = base64decode(input);
294295
try {
295296
bjson.read(buf, 0, buf.byteLength);
296297
} catch (e) {
297-
// okay, ignore
298+
if (/invalid version/.test(e.message)) throw e; // corpus needs update
298299
}
299300
}
300301
}

0 commit comments

Comments
 (0)