Skip to content

fix: heap-use-after-free in build_backtrace when dbuf OOM frees current_exception#1472

Open
mvanhorn wants to merge 1 commit into
quickjs-ng:masterfrom
mvanhorn:fix/1469-heap-use-after-free-in-build-backtrace-when-dbuf-o
Open

fix: heap-use-after-free in build_backtrace when dbuf OOM frees current_exception#1472
mvanhorn wants to merge 1 commit into
quickjs-ng:masterfrom
mvanhorn:fix/1469-heap-use-after-free-in-build-backtrace-when-dbuf-o

Conversation

@mvanhorn
Copy link
Copy Markdown
Contributor

Summary

If JS_NewError during build_backtrace triggered dbuf OOM, the OOM path freed the current exception (which is error_val on the caller's stack), then build_backtrace kept using the dangling error_val for both the prepareStackTrace call and the JS_DefinePropertyValue of the stack property. Fix is to duplicate error_val into a local error_obj, use that throughout the function, and free it at exit.

Why this matters

ASan flags this as a clean heap-use-after-free with a small repro that exercises Error.prepareStackTrace under low-memory conditions. The rt->in_build_stack_trace guard prevents recursion but not the freed-pointer reuse, since the free happens via JS_ThrowOutOfMemory -> exception slot replacement, not via recursion.

Changes

  • quickjs.c - add JSValue error_obj = JS_DupValue(ctx, error_val) at function entry, use error_obj for the two remaining references after the dbuf-can-fail region, free at exit.
  • api-test.c - add a regression test that constructs the OOM scenario and walks the backtrace path.

Testing

make all && ./run-test262 -c tests/test262.conf clean. ASan build with the new api-test passes.

Fixes #1469

…nt_exception

If JS_NewError() during build_backtrace triggered dbuf OOM, JS_ThrowOutOfMemory
freed the current exception (error_val from the caller's stack frame), then
the rest of build_backtrace continued using the freed error_val for the
prepareStackTrace call and the JS_DefinePropertyValue of the stack property.

The fix duplicates error_val into a local error_obj at function entry, uses
error_obj throughout the function, and frees it at exit.

Fixes quickjs-ng#1469
Copy link
Copy Markdown
Contributor

@saghul saghul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small, sugestion, LGTM otherwise, thanks!

Comment thread quickjs.c
if (rt->in_build_stack_trace)
return;
rt->in_build_stack_trace = true;
error_obj = JS_DupValue(ctx, error_val);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use js_dup.

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.

heap-use-after-free in build_backtrace

2 participants