Skip to content

quickjs-libc: fix use-after-free of the message port in handle_posted_message - #1952

Open
Perennos wants to merge 1 commit into
quickjs-ng:masterfrom
Perennos:fix-worker-port-uaf
Open

Perennos wants to merge 1 commit into
quickjs-ng:masterfrom
Perennos:fix-worker-port-uaf

Conversation

@Perennos

Copy link
Copy Markdown

Fix a use-after-free of the Worker message port in handle_posted_message().

Problem

handle_posted_message() reads port->on_message_func only after calling JS_ReadObject() and JS_NewObject(), either of which can trigger the cycle GC. If the owning Worker is reachable only through a self-cycle (for example w.self_ref = w; w = null;), the cycle GC finalizes it and js_worker_finalizer() frees port, making the later read a heap-use-after-free. ASan reports the read in handle_posted_message (quickjs-libc.c:2738) and the free in the cycle GC.

This matches the upstream bug reported in bellard/quickjs#527 and fixed in bellard/quickjs#627; the quickjs-ng code is identical and still unfixed.

Fix

Duplicate the handler reference (JS_DupValue(ctx, port->on_message_func)) before any allocation and release it at the end of the function, so port is never dereferenced afterward. The duplicated reference continues to protect the handler during invocation, and the existing comment about func freeing the handler is merged into the updated comment.

Testing

  • Added tests/test_worker_gc.js, a reduced reproducer from heap-use-after-free in handle_posted_message bellard/quickjs#527 picked up automatically by tests.conf.
  • Before this change, in an ASan build (cmake -B build-asan -DCMAKE_BUILD_TYPE=Debug -DQJS_ENABLE_ASAN=ON), build-asan/qjs tests/test_worker_gc.js fails on every run with heap-use-after-free in handle_posted_message, and build-asan/run-test262 -c tests.conf aborts with the same report.
  • After this change, tests/test_worker_gc.js passes 6 of 6 runs and build-asan/run-test262 -c tests.conf passes, including tests/test_worker.js.
  • Unrelated to this change: tests/test_std.js (second pipe handler, 100 ms timeout) failed in about one of three full ASan runs on a loaded machine both with and without this patch.

AI-assisted: drafted with an AI coding assistant; reproduced and tested locally before submitting.

…_message

The cycle GC can run during JS_ReadObject() or JS_NewObject() and finalize a
Worker that is only reachable through a self-cycle, which frees its message
handler. handle_posted_message() then read port->on_message_func from freed
memory. Take the reference to the handler function before any allocation and
do not touch port afterwards.

Add tests/test_worker_gc.js as a regression test.

Same bug as bellard/quickjs#527, fixed in bellard/quickjs#627.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant