quickjs icon indicating copy to clipboard operation
quickjs copied to clipboard

[BUG] qjs: SIGSEGV in find_own_property during backtrace construction

Open Nievesjyl opened this issue 5 months ago • 2 comments

BUG description:

Running qjs under an 8MB memory limit on a JavaScript input causes a segmentation fault in find_own_property() while building an exception backtrace.

The crash occurs through JS_DefineProperty() and build_backtrace(), suggesting that an invalid object shape/property state is reached during error handling.

Steps to Reproduce:

The PoC attachment contains the input file that triggers the crash:

Null_Pointer.zip

COMMAND LINE:

./qjs --memory-limit 8M Null_Pointer

Expected behavior

qjs should handle malformed or adversarial scripts by reporting a script error or exiting cleanly, rather than dereferencing invalid object property state during backtrace construction.

version:

QuickJS version 2026-06-04 Git branch: master commit: https://github.com/bellard/quickjs/commit/04be246001599f5995fa2f2d8c91a0f198d3f34c

OS and version:

Ubuntu 22.04.5 LTS arch: x86_64

Compiler:

gcc 11.4.0 clang 14.0.0

GDB output excerpt:

Program received signal SIGSEGV, Segmentation fault.

#0 find_own_property ( p=0x2398db58, atom=57, ppr=) at quickjs.c:6134 #1 JS_DefineProperty ( ctx=ctx@entry=0x233cd370, this_obj=..., prop=prop@entry=57, val=..., getter=..., setter=..., flags=9987) at quickjs.c:10357 #2 0x00000000004d02dd in JS_DefinePropertyValue (...) at quickjs.c:10674 #3 build_backtrace ( ctx=0x1875a5f <bbMapFb+7633791>, error_obj=..., filename=0x0, line_num=0, col_num=0, backtrace_flags=0) at quickjs.c:7606 #4 JS_CallInternal (...) at quickjs.c:20486 #5 0x0000000000495c21 in JS_CallInternal (...) at quickjs.c:18199 #6 0x0000000000495c21 in JS_CallInternal (...) at quickjs.c:18199 #7 0x0000000000495c21 in JS_CallInternal (...) at quickjs.c:18199 #8 0x0000000000495c21 in JS_CallInternal (...) at quickjs.c:18199 #9 0x0000000000495c21 in JS_CallInternal (...) at quickjs.c:18199 #10 0x0000000000495c21 in JS_CallInternal (...) at quickjs.c:18199 #15 0x0000000000495c21 in JS_CallInternal (...) at quickjs.c:18199

Additional Information:

Crash type: SIGSEGV Crash location: find_own_property() Memory limit: 8M Fully reproducible.

Nievesjyl avatar Jun 28 '26 21:06 Nievesjyl

Hit this same crash independently (embedding with JS_SetMemoryLimit) and root-caused it: it's a use-after-free of the pending exception object, not shape corruption.

Sequence (confirmed with a gdb breakpoint on free_object, master 04be246):

  1. Bytecode throws OOM; rt->current_exception holds the only reference to the error object.
  2. The exception path in JS_CallInternal (quickjs.c:20668) passes it to build_backtrace() as a borrowed JSValueConst.
  3. Inside build_backtrace, JS_NewString() for the stack string hits the memory limit again: js_alloc_string → JS_ThrowOutOfMemory → JS_Throw, and JS_Throw does JS_FreeValue(ctx, rt->current_exception) (quickjs.c:7358) — freeing the object build_backtrace is still working on.
  4. JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_stack, …) (quickjs.c:7615) then runs on the freed object → SIGSEGV in find_own_property (p->shape is NULL; gdb shows free_mark=1).

Deep JS recursion is needed only because it makes the backtrace string large enough that its own allocation is the one that fails. Depending on which allocation inside build_backtrace fails instead, the same condition also surfaces as a null uncaught exception rather than a crash.

Source-only reproducer (no PoC file needed) — a recursive-descent parser under a 4MB limit; the crash needs the second OOM to land on the final JS_NewString, so sweep an allocation-size knob:

for pad in $(seq 50 25 2000); do
  ./qjs --memory-limit 4194304 -e "
var depth=500, padn=$pad;
var input='';
for (var i=0;i<depth;i++) input += \"<a x='yyyyyyyyyy'>\";
input += 'hello';
for (var i=0;i<depth;i++) input += '</a>';
var pos=0;
function parseElem(){
  var start=input.indexOf('>',pos); var tag=input.slice(pos,start+1); pos=start+1;
  var node={tag:tag,attrs:tag.split(\"'\"),children:[],pad:'p'.repeat(padn)};
  while(pos<input.length){
    if(input[pos]==='<'){ if(input[pos+1]==='/'){pos=input.indexOf('>',pos)+1; return node;} node.children.push(parseElem()); }
    else { var next=input.indexOf('<',pos); node.children.push(input.slice(pos,next)); pos=next; }
  }
  return node;
}
var trees=[]; for(;;){ pos=0; trees.push(parseElem()); }
" >/dev/null 2>&1
  rc=$?; [ $rc -ge 128 ] && echo "signal $((rc-128)) at pad=$pad"
done

Segfaults deterministically at pad=1400 on my x86_64 box (gcc 12); also reproduced on ARM64.

mark-os avatar Aug 13 '26 04:08 mark-os

fixed

bellard avatar Nov 02 '26 13:11 bellard