Skip to content

Commit 252209d

Browse files
committed
Fix integer overflow in the Proxy ownKeys() property enumeration
On 32bit hosts.
1 parent 62951e1 commit 252209d

3 files changed

Lines changed: 89 additions & 10 deletions

File tree

‎api-test.c‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,44 @@ static void backtrace_oom_current_exception(void)
996996
JS_FreeRuntime(rt);
997997
}
998998

999+
static void proxy_own_keys_huge_length(void)
1000+
{
1001+
static const char setup_code[] =
1002+
"globalThis.p = new Proxy({}, {\n"
1003+
" ownKeys() { return { length: 0x20000000 }; },\n"
1004+
"});";
1005+
JSMemoryUsage stats;
1006+
JSValue ret, exception;
1007+
JSRuntime *rt;
1008+
JSContext *ctx;
1009+
const char *str;
1010+
1011+
rt = new_runtime();
1012+
ctx = JS_NewContext(rt);
1013+
1014+
ret = eval(ctx, setup_code);
1015+
assert(!JS_IsException(ret));
1016+
JS_FreeValue(ctx, ret);
1017+
1018+
JS_ComputeMemoryUsage(rt, &stats);
1019+
JS_SetMemoryLimit(rt, (size_t)stats.malloc_size + 128 * 1024);
1020+
1021+
ret = eval(ctx, "Object.keys(p)");
1022+
assert(JS_IsException(ret));
1023+
JS_FreeValue(ctx, ret);
1024+
exception = JS_GetException(ctx);
1025+
str = JS_ToCString(ctx, exception);
1026+
assert(str);
1027+
/* the trap result has no index 0, so the first key must be rejected */
1028+
assert(!strcmp(str, "TypeError: proxy: properties must be strings or symbols"));
1029+
JS_FreeCString(ctx, str);
1030+
JS_FreeValue(ctx, exception);
1031+
JS_SetMemoryLimit(rt, 0);
1032+
1033+
JS_FreeContext(ctx);
1034+
JS_FreeRuntime(rt);
1035+
}
1036+
9991037
static int gop_get_own_property(JSContext *ctx, JSPropertyDescriptor *desc,
10001038
JSValueConst obj, JSAtom prop)
10011039
{
@@ -1452,6 +1490,7 @@ int main(void)
14521490
new_errors();
14531491
backtrace_oom_current_exception();
14541492
backtrace_oom_callsite_array();
1493+
proxy_own_keys_huge_length();
14551494
global_object_prototype();
14561495
slice_string_tocstring();
14571496
immutable_array_buffer();

‎quickjs.c‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52400,7 +52400,7 @@ static int js_proxy_get_own_property_names(JSContext *ctx,
5240052400
{
5240152401
JSProxyData *s;
5240252402
JSValue method, prop_array, val;
52403-
uint32_t len, i, len2;
52403+
uint32_t len, target_len, tab_size, i, len2;
5240452404
JSPropertyEnum *tab, *tab2;
5240552405
JSAtom atom;
5240652406
int res, is_extensible, idx;
@@ -52418,16 +52418,12 @@ static int js_proxy_get_own_property_names(JSContext *ctx,
5241852418
return -1;
5241952419
tab = NULL;
5242052420
len = 0;
52421+
tab_size = 0;
5242152422
tab2 = NULL;
5242252423
len2 = 0;
52423-
if (js_get_length32(ctx, &len, prop_array))
52424+
if (js_get_length32(ctx, &target_len, prop_array))
5242452425
goto fail;
52425-
if (len > 0) {
52426-
tab = js_mallocz(ctx, sizeof(tab[0]) * len);
52427-
if (!tab)
52428-
goto fail;
52429-
}
52430-
for(i = 0; i < len; i++) {
52426+
for(i = 0; i < target_len; i++) {
5243152427
val = JS_GetPropertyUint32(ctx, prop_array, i);
5243252428
if (JS_IsException(val))
5243352429
goto fail;
@@ -52440,8 +52436,26 @@ static int js_proxy_get_own_property_names(JSContext *ctx,
5244052436
JS_FreeValue(ctx, val);
5244152437
if (atom == JS_ATOM_NULL)
5244252438
goto fail;
52443-
tab[i].atom = atom;
52444-
tab[i].is_enumerable = false; /* XXX: redundant? */
52439+
if (len >= tab_size) {
52440+
int64_t new_size = max_int64(8, tab_size + (int64_t)tab_size / 2);
52441+
JSPropertyEnum *new_tab;
52442+
new_size = min_int64(new_size, target_len);
52443+
if (new_size > (int64_t)(SIZE_MAX / sizeof(tab[0]))) {
52444+
JS_FreeAtom(ctx, atom);
52445+
JS_ThrowOutOfMemory(ctx);
52446+
goto fail;
52447+
}
52448+
new_tab = js_realloc(ctx, tab, sizeof(tab[0]) * (size_t)new_size);
52449+
if (!new_tab) {
52450+
JS_FreeAtom(ctx, atom);
52451+
goto fail;
52452+
}
52453+
tab = new_tab;
52454+
tab_size = new_size;
52455+
}
52456+
tab[len].atom = atom;
52457+
tab[len].is_enumerable = false; /* XXX: redundant? */
52458+
len++;
5244552459
}
5244652460

5244752461
/* check duplicate properties (XXX: inefficient, could store the

‎tests/test_builtin.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,31 @@ function test_proxy_iter()
12091209
assert(a[1], "y");
12101210
}
12111211

1212+
function test_proxy_own_keys_huge_length()
1213+
{
1214+
for (const length of [0x20000000, 0x40000000, 0xfffffffe, 0xffffffff]) {
1215+
const p = new Proxy({}, { ownKeys() { return {length}; } });
1216+
/* index 0 is undefined, so this must fail on the very first entry */
1217+
assertThrows(TypeError, function() { Object.keys(p); });
1218+
assertThrows(TypeError, function() { Object.getOwnPropertyNames(p); });
1219+
}
1220+
1221+
const keys = ["a", "b", Symbol("c")];
1222+
const p = new Proxy({}, {
1223+
ownKeys() {
1224+
return new Proxy({length: 0xffffffff}, {
1225+
get(t, k) {
1226+
if (k === "length") return 0xffffffff;
1227+
const i = Number(k);
1228+
if (i < keys.length) return keys[i];
1229+
throw new RangeError("stop at " + i);
1230+
},
1231+
});
1232+
},
1233+
});
1234+
assertThrows(RangeError, function() { Object.getOwnPropertyNames(p); });
1235+
}
1236+
12121237
/* CVE-2023-31922 */
12131238
function test_proxy_is_array()
12141239
{
@@ -1318,6 +1343,7 @@ test_set();
13181343
test_weak_set();
13191344
test_generator();
13201345
test_proxy_iter();
1346+
test_proxy_own_keys_huge_length();
13211347
test_proxy_is_array();
13221348
test_finalization_registry();
13231349
test_exception_source_pos();

0 commit comments

Comments
 (0)