Skip to content

Type confusion in RegExp.escape leads to heap OOB read #1571

Closed

Description

@xmzyshypnc

Summary

Type confusion in RegExp.escape: a rope string (JS_TAG_STRING_ROPE) is read as a flat JSString, leading to a heap out-of-bounds read (and, depending on the reinterpreted layout, an abort()). x + y is stored lazily as a rope (JSStringRope) once the operands exceed the rope threshold, instead of being flattened. js_regexp_escape (quickjs.c:48962) accepts its argument through the JS_IsString guard — which is true for ropes too — then does JS_VALUE_GET_STRING(argv[0]) and indexes it as a flat string. The JSStringRope is reinterpreted as a JSString, so p->len is the rope's full length but str8(p) does not point at a matching character buffer → the loop reads rope->len bytes past the end of an unrelated/short buffer.

Environment

quickjs-ng 0.15.1 (master)
OS    : Linux x86_64
Build : clang-21, cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS_RELEASE="-O2 -g"
Sanitizer build (ASan) reports it as a heap-buffer-overflow READ;
a plain -O2 build faults with SIGSEGV in the same spot.

PoC

// `a + a` is stored as a "rope" (lazy concatenation) once the halves exceed
// the rope threshold, instead of being flattened. RegExp.escape then reads it
// as a flat string.
var a = "a".repeat(50000);
RegExp.escape(a + a);        // heap-buffer-overflow read

Deterministic (6/6). The halves only need to be over the rope threshold — RegExp.escape("a".repeat(2000) + "a".repeat(2000)) already faults; larger sizes just make the overflow reach further past the allocation.

Crash

ERROR: AddressSanitizer: heap-buffer-overflow
READ of size 1 ... 0 bytes after a NNNN-byte region
  #0 js_regexp_escape   quickjs.c:48975   c = ... str8(p)[i]   (i < p->len)

Under a non-ASan build the same read is a SIGSEGV in js_regexp_escape. With a differently-shaped rope the reinterpreted kind field can land on an invalid value (≥ 3) and hit the abort() in strv() (quickjs.c:718) instead.

Backtrace

#0 js_regexp_escape     quickjs.c:48975      # c = p->is_wide_char ? str16(p)[i] : str8(p)[i]
#1 js_call_c_function   quickjs.c:17812
#2 JS_CallInternal      quickjs.c:18021
...

Root cause

js_regexp_escape (quickjs.c:48962):

if (!JS_IsString(argv[0]))
    return JS_ThrowTypeError(ctx, "not a string");
p = JS_VALUE_GET_STRING(argv[0]);                 // <-- argv[0] may be a rope
string_buffer_init2(ctx, b, 0, p->is_wide_char);
for (i = 0; i < p->len; i++) {
    c = p->is_wide_char ? (uint32_t)str16(p)[i] : (uint32_t)str8(p)[i];   // OOB read
    ...
}

JS_IsString resolves to tag_is_string() (quickjs.c:5048), which returns true for both JS_TAG_STRING and JS_TAG_STRING_ROPE:

static BOOL tag_is_string(uint32_t tag)
{
    return tag == JS_TAG_STRING || tag == JS_TAG_STRING_ROPE;
}

So a rope passes the guard. JS_VALUE_GET_STRING(argv[0]) then reinterprets the JSStringRope* as a JSString*. Both structs begin with a uint32_t len (quickjs.c:670 vs 692), so p->len reads the rope's full logical length (up to JS_STRING_LEN_MAX), but str8(p) does not resolve to a character buffer of that length — it walks the type-confused kind/is_wide_char bits into rope-internal pointers. The loop then reads p->len bytes from a base that points at (at most) a rope child / a shorter buffer → heap out-of-bounds read.

The engine already has the right tool for this: ropes are meant to be flattened with js_linearize_string_rope (quickjs.c:5356) before flat access — e.g. the JS_TAG_STRING_ROPE case in the ToString path (quickjs.c:14554) does exactly that. js_regexp_escape skips it.

For contrast, String.prototype.concat (js_string_concat, quickjs.c:46503) guards with the exact tag and routes ropes elsewhere:

if (JS_TAG_STRING != JS_VALUE_GET_TAG(argv[i]))
    goto slow_path;                 // ropes go to the slow (linearizing) path
p = JS_VALUE_GET_STRING(argv[i]);

RegExp.escape using the looser JS_IsString instead of the exact JS_TAG_STRING check is what lets the rope reach JS_VALUE_GET_STRING.

Impact

Attacker-controlled-length heap out-of-bounds read reachable from a single script-level builtin call (RegExp.escape(bigstr + bigstr)), no special setup. The read length is the rope length (up to ~1 GB) and the base pointer is the mis-resolved rope internals, so it is a memory-safety bug good for out-of-bounds read / info-leak and crash (DoS).

Suggested fix

I think the cleanest fix is to linearize the argument before reading it, exactly like the rest of the engine does for ropes. After the JS_IsString check:

JSValue str = js_dup(argv[0]);
if (JS_VALUE_GET_TAG(str) == JS_TAG_STRING_ROPE) {
    str = js_linearize_string_rope(ctx, str);   // returns a flat JS_TAG_STRING (new ref)
    if (JS_IsException(str))
        return str;
}
p = JS_VALUE_GET_STRING(str);
... existing loop ...
JS_FreeValue(ctx, str);
return string_buffer_end(b);

js_linearize_string_rope returns a flat string, so p is always a real JSString and p->len matches str8(p). (Tightening the guard to the exact JS_TAG_STRING tag like js_string_concat would also stop the crash, but linearizing keeps RegExp.escape accepting ropes, which is what the spec wants — it should accept any string.) I may be missing a ref-counting nicety in the sketch above, but linearize-before-index looks like the right shape.

Credit

xmzyshypnc(@xmzyshypnc) and Yanjie Zhao(@carol233) and Yiyang Liu(@lyyffee)

Activity

  1. added a commit that references this issue on Jul 16, 2026
    ee13c7f
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions